[issue45034] Improve struct.pack out of range error messages

2021-09-07 Thread Mark Dickinson


Mark Dickinson  added the comment:

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 `0x` (or even 
better, `0x___`) is definitely more useful than 
`18446744073709551615`, I think I'd still find `-128` more immediately readable 
than `-0x80`.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45034] Improve struct.pack out of range error messages

2021-09-07 Thread Mark Dickinson


Mark Dickinson  added the comment:


New changeset 8ca6b61e3fd7f1e2876126cee82da8d812c8462f by Nikita Sobolev in 
branch 'main':
bpo-45034: Fix how upper limit is formatted for `struct.pack("H", ...)` 
(GH-28178)
https://github.com/python/cpython/commit/8ca6b61e3fd7f1e2876126cee82da8d812c8462f


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45034] Improve struct.pack out of range error messages

2021-09-05 Thread Nikita Sobolev


Change by Nikita Sobolev :


--
keywords: +patch
pull_requests: +26605
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/28178

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45034] Improve struct.pack out of range error messages

2021-09-04 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

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.

--
nosy: +mark.dickinson, meador.inge

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45034] Improve struct.pack out of range error messages

2021-09-04 Thread Nikita Sobolev


Nikita Sobolev  added the comment:

I would like to work on this if nobody else has already started :)

--
nosy: +sobolevn

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45034] Improve struct.pack out of range error messages

2021-09-04 Thread Dong-hee Na


Change by Dong-hee Na :


--
nosy: +corona10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45034] Improve struct.pack out of range error messages

2021-09-03 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

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.
Lines 611 and 612 are the source for the 1st example.  From comments before 
line 365, there can be issues with lefts shifts, but '0x', '0x_', 
and '0x___' could be hard-coded strings that would cover all 
'normal' systems.

Grepping "argument out of range" returns
F:\dev\3x\Modules\_struct.c: 168: "argument out of 
range");
F:\dev\3x\Modules\_struct.c: 192: "argument out of 
range");
F:\dev\3x\Modules\_struct.c: 215: "argument out of 
range");
F:\dev\3x\Modules\_struct.c: 238: "argument out of 
range");
F:\dev\3x\Modules\_struct.c: 261: "argument out of 
range");
F:\dev\3x\Modules\_struct.c: 284: "argument out of 
range");

It is nnclear to me without more reading why some codes lead to this less 
helpful message.

--
nosy: +terry.reedy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45034] Improve struct.pack out of range error messages

2021-08-27 Thread Steven D'Aprano


New submission from Steven D'Aprano :

Packing errors using struct in 3.9 seem to be unnecessarily obfuscated to me.

>>> import struct
>>> struct.pack('H', 7)
Traceback (most recent call last):
  File "", line 1, in 
struct.error: ushort format requires 0 <= number <= (0x7fff * 2 + 1)


Why "0x7fff * 2 + 1"? Why not the more straightforward "0x" or 65536? (I 
have a slight preference for hex.)

Compare that to:

>>> struct.pack('I', 43)
Traceback (most recent call last):
  File "", line 1, in 
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 0x.

For the long-long format, the error message just gives up:

>>> struct.pack('Q', 2**65)
Traceback (most recent call last):
  File "", line 1, in 
struct.error: argument out of range

Could be improved by:

'Q' format requires 0 <= number <= 0x___

--
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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com