[issue23578] struct.pack error messages do not indicate which argument was invalid

Serhiy Storchaka report at bugs.python.org
Sat Feb 25 10:10:39 EST 2017


Serhiy Storchaka added the comment:

1. Using global variable doesn't look good to me.

2. "at offset 1" looks confusing to me. What is offset? Is this an offset of packed item in created bytes object? The you shouldn't get the odd number for the '!h' format.

I think it would be better to report the number of the packed item. struct.pack() already formats similar errors when pass unsuitable number of items.

>>> struct.pack('<hb', 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: pack expected 2 items for packing (got 1)
>>> struct.pack('<hb', 1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: pack expected 2 items for packing (got 3)

3. It is not safe to use the fixed length array for formatting error message. Once the underlying error message can be changed and will overflow the buffer. The "%zd" format in sprintf() is not portable.

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23578>
_______________________________________


More information about the Python-bugs-list mailing list