msg215744 - (view) |
Author: Konstantin Zemlyak (zart) |
Date: 2014-04-08 08:54 |
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> bytearray([256])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: byte must be in range(0, 256)
>>> bytes([256])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: bytes must be in range(0, 256)
>>>
Tested on 2.7, 3.2, 3.3, 3.4. Frankly, this looks like an off-by-one error to me.
|
msg215745 - (view) |
Author: STINNER Victor (vstinner) * |
Date: 2014-04-08 08:57 |
256 is not part of the range(0, 256): 0..255.
$ python
>>> print(list(range(0,256)))
[0, 1, 2, 3, ..., 253, 254, 255]
It doesn't make sense to store the number 256 in a byte string.
|
msg215747 - (view) |
Author: Stefan Krah (skrah) * |
Date: 2014-04-08 09:38 |
I think it's possible to misunderstand this error message if the
reader does not realize that range(0, 256) is to be taken literally.
Perhaps we should just use closed intervals of the form [0, 255]
everywhere.
|
msg215748 - (view) |
Author: STINNER Victor (vstinner) * |
Date: 2014-04-08 09:39 |
"Perhaps we should just use closed intervals of the form [0, 255] everywhere."
I only saw range(0, 256) in Python. At school, I used [0, 255]. I prefer [0, 255] syntax, it's more explicit :-)
|
msg215749 - (view) |
Author: Peter Otten (peter.otten) * |
Date: 2014-04-08 09:56 |
As every beginner will learn about (and probably overuse) range() pretty soon I think it's OK to use that form.
The math-inspired notation [0, 255] may be misinterpreted as a list. You also lose the consistency of preferring half-open intervals everywhere.
The third alternative I see,
0 <= x < 256
has the problem that it requires a name (the 'x') that may not correspond to a variable in the source code. You'd also need to clarify that x must be an int to make the message bulletproof.
I think this should be left as is.
|
msg215751 - (view) |
Author: STINNER Victor (vstinner) * |
Date: 2014-04-08 10:54 |
> The math-inspired notation [0, 255] may be misinterpreted as a list. You also lose the consistency of preferring half-open intervals everywhere.
Not if you write "x must in the range [0; 255]".
|
msg215901 - (view) |
Author: Antoine Pitrou (pitrou) * |
Date: 2014-04-10 19:37 |
Uh, from the context, it's clear that the error is that 256 doesn't fit in the average 8-bits byte. Is there an actual problem here?
(if you don't know what a byte is, you probably shouldn't use that form of the bytes() constructor)
|
msg215905 - (view) |
Author: Stefan Krah (skrah) * |
Date: 2014-04-10 19:46 |
There's no real problem. I find "must be in the range [0, 255]" easier to
understand, but I also would not make a big effort to change this.
As for me, we can close this issue.
|
msg215906 - (view) |
Author: Antoine Pitrou (pitrou) * |
Date: 2014-04-10 19:47 |
> There's no real problem. I find "must be in the range [0, 255]" easier to
> understand, but I also would not make a big effort to change this.
The problem is that [0, 255] looks very much like a Python list, not a
range.
|
msg215914 - (view) |
Author: Konstantin Zemlyak (zart) |
Date: 2014-04-11 05:27 |
To clarify few things:
- Yes, I know that 256 doesn't fit into byte. I was checking how bytes/bytearray are handling overflow.
- I know that range() is a half-open interval.
Yet this error message still gave me a "wtf" moment because I didn't realize it was talking about python's range() builtin and not about mathematical term. So even though this message is technically 100% correct it still doesn't feel right.
May I propose a message "byte must be in range [0-255]" instead? This won't be confused with either python's range() nor [] list notation.
|
msg215918 - (view) |
Author: Antoine Pitrou (pitrou) * |
Date: 2014-04-11 06:21 |
> Yet this error message still gave me a "wtf" moment because I didn't
realize it was talking about python's range() builtin and not about
mathematical term. So even though this message is technically 100%
correct it still doesn't feel right.
If that "wtf" moment makes you more familiar with the range() builtin
and the fact that it's a full-blown sequence, for example that it
allows writing "256 in range(0, 256)", then it's a rather good thing IMO.
|
msg215925 - (view) |
Author: STINNER Victor (vstinner) * |
Date: 2014-04-11 13:24 |
I suggested "must be in the range [0; 255]" which is not a valid Python
list: ";" is the instruction operator and there is "range" word in front of
the "list". In my opinion, it's much easier to read and understand it.
Another example is the Unicode range(0x110000). "range [0; 0x10ffff]" would
be better. Or maybe "must be in the range U+0000-U+10ffff" but it requires
to know the Unicode U+HHHH syntax and the input type may be int, not a
character.
|
msg215927 - (view) |
Author: Akira Li (akira) * |
Date: 2014-04-11 15:25 |
"byte must be in range [0, 256)"
- it hints at the builtin `range()` -- the intuition works for those who knows what `range()` does
- it uses the standard math notation for half-open intervals [1] -- no Python knowledge required (among other things)
- it is not a valid Python -- no confusion with a list, tuple literals
- Dijkstra explains why half-open intervals are preferable [2]
Another alternative is to use `range(0x100)` and require that to understand the error message, you should know Python and the hex notation.
[1]: http://en.wikipedia.org/wiki/Interval_(mathematics)#Notations_for_intervals
[2]: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
|
msg215929 - (view) |
Author: Mark Lawrence (BreamoreBoy) * |
Date: 2014-04-11 15:52 |
How about plain English? "byte must be between 0 and 255 inclusive"
|
msg215932 - (view) |
Author: Antoine Pitrou (pitrou) * |
Date: 2014-04-11 17:06 |
> I suggested "must be in the range [0; 255]" which is not a valid Python
> list: ";" is the instruction operator and there is "range" word in front of
> the "list". In my opinion, it's much easier to read and understand it.
I'm -1 on it. It may not be a valid Python list, but it definitely *looks* like a valid Python list, which makes it a double "wtf" IMO.
|
msg215933 - (view) |
Author: Antoine Pitrou (pitrou) * |
Date: 2014-04-11 17:08 |
Ok, seeing how the alternative proposals are all worse than the statu quo, and how this is going a massive bikeshedding anyway, I'd rather close the issue.
|
msg215938 - (view) |
Author: Stefan Krah (skrah) * |
Date: 2014-04-11 17:46 |
Antoine Pitrou <report@bugs.python.org> wrote:
> > I suggested "must be in the range [0; 255]" which is not a valid Python
> > list: ";" is the instruction operator and there is "range" word in front of
> > the "list". In my opinion, it's much easier to read and understand it.
>
> I'm -1 on it. It may not be a valid Python list, but it definitely *looks* like a valid Python list, which makes it a double "wtf" IMO.
It's a valid OCaml list;; :P
[Glad that the issue is closed]
|
|
Date |
User |
Action |
Args |
2022-04-11 14:58:01 | admin | set | github: 65376 |
2014-04-11 17:46:00 | skrah | set | messages:
+ msg215938 |
2014-04-11 17:08:13 | pitrou | set | status: open -> closed resolution: not a bug messages:
+ msg215933
|
2014-04-11 17:07:00 | pitrou | set | messages:
+ msg215932 |
2014-04-11 15:52:51 | BreamoreBoy | set | nosy:
+ BreamoreBoy messages:
+ msg215929
|
2014-04-11 15:25:02 | akira | set | nosy:
+ akira messages:
+ msg215927
|
2014-04-11 13:24:02 | vstinner | set | messages:
+ msg215925 |
2014-04-11 06:21:09 | pitrou | set | messages:
+ msg215918 |
2014-04-11 05:27:15 | zart | set | messages:
+ msg215914 |
2014-04-10 19:47:33 | pitrou | set | messages:
+ msg215906 |
2014-04-10 19:46:40 | skrah | set | messages:
+ msg215905 |
2014-04-10 19:37:43 | pitrou | set | nosy:
+ pitrou messages:
+ msg215901
|
2014-04-08 10:54:01 | vstinner | set | messages:
+ msg215751 |
2014-04-08 09:56:58 | peter.otten | set | nosy:
+ peter.otten messages:
+ msg215749
|
2014-04-08 09:39:25 | vstinner | set | messages:
+ msg215748 |
2014-04-08 09:38:24 | skrah | set | nosy:
+ skrah messages:
+ msg215747
|
2014-04-08 08:57:00 | vstinner | set | nosy:
+ vstinner messages:
+ msg215745
|
2014-04-08 08:54:10 | zart | create | |