-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Improve struct.pack out of range error messages #89197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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:
|
I agree, including use of hex, if possible, for unsigned (non-negative) values. Grepping 'format requires' returns
F:\dev\3x\Modules\_struct.c: 365: "'%c' format requires 0 <= number <= %zu",
F:\dev\3x\Modules\_struct.c: 371: "'%c' format requires %zd <= number <= %zd",
F:\dev\3x\Modules\_struct.c: 550: "byte format requires -128 <= number <= 127");
F:\dev\3x\Modules\_struct.c: 565: "ubyte format requires 0 <= number <= 255");
F:\dev\3x\Modules\_struct.c: 577: "char format requires a bytes object of length 1");
F:\dev\3x\Modules\_struct.c: 593: "short format requires " Py_STRINGIFY(SHRT_MIN)
F:\dev\3x\Modules\_struct.c: 611: "ushort format requires 0 <= number <= "
Py_STRINGIFY(USHRT_MAX)); I believe l365 is the source for the 2nd example. AFAIK, 'zu' is not valid for either C printf or Python % formating. Grepping "argument out of range" returns It is nnclear to me without more reading why some codes lead to this less helpful message. |
I would like to work on this if nobody else has already started :) |
Go ahead, with the understanding that there is no guaranteed acceptance of the change, even with a good patch. There may be reasons for the status quo that neither Steven nor I are aware of. I don't think that either of the listed experts, added as nosy, are very active. In any case, revised or new tests will be needed. |
The out-of-range error messages for unsigned short and short have been fixed, thanks to Nikita Sobolev. They resulted from a rather odd use of the Py_STRINGIFY macro, which meant that not only were the messages obscure, but they differed from system to system: e.g., on my machine I get: "struct.error: ushort format requires 0 <= number <= (32767 *2 +1)", and "struct.error: short format requires (-32767 -1) <= number <= 32767", complete with the weird spacing. There's still room for normalising the other messages and/or converting the limits to hex if anyone wants to provide a PR. I'm unsure about using hex universally: while |
@mdickinson @sobolevn The issue seems to be solved. We can close the issue. |
@furkanonder Thanks for the ping. Indeed I think the changes in #98252 mean this issue can be closed. |
struct.pack("H", ...)
#28178Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: