New submission from Steven D'Aprano <steve+pyt...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue45034>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to