[New-bugs-announce] [issue45034] Improve struct.pack out of range error messages
Steven D'Aprano
report at bugs.python.org
Fri Aug 27 21:02:05 EDT 2021
New submission from Steven D'Aprano <steve+python at pearwood.info>:
Packing errors using struct in 3.9 seem to be unnecessarily obfuscated to me.
>>> import struct
>>> struct.pack('H', 70000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: ushort format requires 0 <= number <= (0x7fff * 2 + 1)
Why "0x7fff * 2 + 1"? Why not the more straightforward "0xffff" or 65536? (I have a slight preference for hex.)
Compare that to:
>>> struct.pack('I', 4300000000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: 'I' format requires 0 <= number <= 4294967295
which at least gives the actual value, but it would perhaps be a bit more useful in hex 0xffffffff.
For the long-long format, the error message just gives up:
>>> struct.pack('Q', 2**65)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: argument out of range
Could be improved by:
'Q' format requires 0 <= number <= 0xffff_ffff_ffff_ffff
----------
components: Library (Lib)
messages: 400452
nosy: steven.daprano
priority: normal
severity: normal
status: open
title: Improve struct.pack out of range error messages
type: enhancement
versions: Python 3.11
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45034>
_______________________________________
More information about the New-bugs-announce
mailing list