classification
Title: bytes join cannot join bytes
Type: behavior Stage:
Components: Versions: Python 3.1
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, perey, pitrou
Priority: normal Keywords:

Created on 2010-05-08 16:50 by perey, last changed 2010-05-08 19:21 by perey. This issue is now closed.

Files
File name Uploaded Description Edit
joinerror.py perey, 2010-05-08 16:50 Test cases (using doctest)
Messages (4)
msg105317 - (view) Author: Timothy Pederick (perey) Date: 2010-05-08 16:50
This code behaves as expected:
>>> ' '.join('cat')
'c a t'

This code does not:
>>> b'\x00'.join(b'cat')
Traceback (most recent call last):
  ...
    b'\x00'.join(b'cat')
TypeError: sequence item 0: expected bytes, int found

Instead, you have to do something like this:
>>> b'\x00'.join(bytes((i,)) for i in b'cat')
b'c\x00a\x00t'

The cause is that indexing a bytes object gives an int, not a bytes. I know, it's as designed, PEP 3137, etc. But in this specific instance, it causes Python to behave inconsistently (bytes vs. str) and unintuitively (to me, anyway).

(Same problem with bytes or bytearray in either position.)
msg105319 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-05-08 17:08
It's not a very useful thing to join a single string, though.
msg105323 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2010-05-08 18:33
While the bytes-returning-int semantics might be annoying in this case, but as you point out, Timothy, it's too late to change this.
msg105326 - (view) Author: Timothy Pederick (perey) Date: 2010-05-08 19:21
Brett: Well, I was more thinking along the lines of making join() recognise when it's been passed a bytes object, rather than changing the semantics of indexing bytes. That would be a bit overdramatic!

But if it's wontfix, it's wontfix.

Antoine: Yes, the str example was just for comparison, since the behaviour of bytes is generally supposed to be the same as str. (But I'm sure I could think of some silly examples where it could be used.) The bytes problem actually came up in code I was writing... though I later ripped it out and did it differently...
History
Date User Action Args
2010-05-08 19:21:55pereysetmessages: + msg105326
2010-05-08 18:33:57brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg105323

resolution: wont fix
2010-05-08 17:08:20pitrousetnosy: + pitrou
messages: + msg105319
2010-05-08 16:50:49pereycreate