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: __iter__ on a byte file object using a method to return an iterator
Type: enhancement Stage:
Components: IO Versions: Python 3.3, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Julian, benjamin.peterson, r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2010-11-21 00:42 by Julian, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg121820 - (view) Author: Julian Berman (Julian) * Date: 2010-11-21 00:42
Iterating over a byte file object using __iter__ is rather useless, and a bit confusing perhaps.

It'd be nice to propose two things:

1. Having __iter__ raise an exception if the file was opened with the "b" flag.

2. Adding a new by_bytes() method to file objects, which would return an iterator that yielded the next byte in the file, introducing the ability to write a similar `for byte in byte_file_obj.by_bytes()` that is currently possible when iterating by line over non-byte files.

It's quite easy to do (2) currently, obviously, but perhaps it's worthy of consideration for inclusion.
msg121822 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-21 00:51
FWIW, an idiom I use in Py2.x is:

   for block in iter(partial(f.read, BLKSIZ), ''):
        . . .

This works with both single bytes at time and multiple bytes at a time.
msg121823 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-11-21 01:02
BufferedIOBase readline and __iter__ has to stay for backwards compatibility (especially with python 2). As for by_bytes(), I suggest you post it to python-ideas.
msg121836 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-11-21 02:19
For the record, I don't find the behavior of __iter__ on a binary file at all confusing.  It's the same behavior I see if I open the file in, say, vi.  So it is in fact the behavior I expect, and I would be surprised if it didn't work.

Whether it is *useful* is a different story, but I'd be willing to bet that there are or will be python3 programs that do use it.
History
Date User Action Args
2022-04-11 14:57:09adminsetgithub: 54685
2010-11-21 02:19:02r.david.murraysetnosy: + r.david.murray
messages: + msg121836
2010-11-21 01:02:13benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg121823

resolution: rejected
2010-11-21 00:51:49rhettingersetnosy: + rhettinger
messages: + msg121822
2010-11-21 00:42:43Juliancreate