This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ValueError: byte must be in range(0, 256)
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, akira, peter.otten, pitrou, skrah, vstinner, zart
Priority: normal Keywords:

Created on 2014-04-08 08:54 by zart, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (17)
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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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]
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65376
2014-04-11 17:46:00skrahsetmessages: + msg215938
2014-04-11 17:08:13pitrousetstatus: open -> closed
resolution: not a bug
messages: + msg215933
2014-04-11 17:07:00pitrousetmessages: + msg215932
2014-04-11 15:52:51BreamoreBoysetnosy: + BreamoreBoy
messages: + msg215929
2014-04-11 15:25:02akirasetnosy: + akira
messages: + msg215927
2014-04-11 13:24:02vstinnersetmessages: + msg215925
2014-04-11 06:21:09pitrousetmessages: + msg215918
2014-04-11 05:27:15zartsetmessages: + msg215914
2014-04-10 19:47:33pitrousetmessages: + msg215906
2014-04-10 19:46:40skrahsetmessages: + msg215905
2014-04-10 19:37:43pitrousetnosy: + pitrou
messages: + msg215901
2014-04-08 10:54:01vstinnersetmessages: + msg215751
2014-04-08 09:56:58peter.ottensetnosy: + peter.otten
messages: + msg215749
2014-04-08 09:39:25vstinnersetmessages: + msg215748
2014-04-08 09:38:24skrahsetnosy: + skrah
messages: + msg215747
2014-04-08 08:57:00vstinnersetnosy: + vstinner
messages: + msg215745
2014-04-08 08:54:10zartcreate