Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit f8155cd

Browse files
authored
Merge pull request #498 from cemsbr/exception-attr
Add attribute name to PackException message
2 parents 22a3313 + 1df799b commit f8155cd

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ UNRELEASED - Under development
77
******************************
88
Added
99
=====
10+
- Attribute name in PackException message.
1011

1112
Changed
1213
=======

pyof/foundation/base.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,10 @@ def pack(self, value=None):
186186
try:
187187
return struct.pack(self._fmt, value)
188188
except struct.error:
189-
msg = '{} could not pack {} = {}.'.format(type(self).__name__,
190-
type(value).__name__,
191-
value)
189+
expected_type = type(self).__name__
190+
actual_type = type(value).__name__
191+
msg_args = expected_type, value, actual_type
192+
msg = 'Expected {}, found value "{}" of type {}'.format(*msg_args)
192193
raise PackException(msg)
193194

194195
def unpack(self, buff, offset=0):
@@ -603,6 +604,22 @@ def _get_attributes(self):
603604
self._get_instance_attributes(),
604605
self.get_class_attributes())
605606

607+
def _get_named_attributes(self):
608+
"""Return generator for attribute's name, instance and class values.
609+
610+
Add attribute name to meth:`_get_attributes` for a better debugging
611+
message, so user can find the error easier.
612+
613+
Returns:
614+
generator: Tuple with attribute's name, instance and class values.
615+
616+
"""
617+
for cls, instance in zip(self.get_class_attributes(),
618+
self._get_instance_attributes()):
619+
attr_name, cls_value = cls
620+
instance_value = instance[1]
621+
yield attr_name, instance_value, cls_value
622+
606623
def _unpack_attribute(self, name, obj, buff, begin):
607624
attribute = deepcopy(obj)
608625
setattr(self, name, attribute)
@@ -667,8 +684,14 @@ def pack(self, value=None):
667684
else:
668685
message = b''
669686
# pylint: disable=no-member
670-
for instance_attr, class_attr in self._get_attributes():
671-
message += class_attr.pack(instance_attr)
687+
for attr_info in self._get_named_attributes():
688+
name, instance_value, class_value = attr_info
689+
try:
690+
message += class_value.pack(instance_value)
691+
except PackException as pack_exception:
692+
cls = type(self).__name__
693+
msg = f'{cls}.{name} - {pack_exception}'
694+
raise PackException(msg)
672695
return message
673696
elif isinstance(value, type(self)):
674697
return value.pack()

pyof/foundation/exceptions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,4 @@ class UnpackException(Exception):
5757
class PackException(Exception):
5858
"""Error while unpacking."""
5959

60-
def __str__(self):
61-
return "Pack error: " + super().__str__()
60+
pass

0 commit comments

Comments
 (0)