On Thu, 3 Mar 2022 at 20:28, Steven D'Aprano <st...@pearwood.info> wrote: > > On Thu, Mar 03, 2022 at 08:27:50AM -0000, Kevin Mills wrote: > > > The code I'm currently working on involves parsing binary data. If I > > ask for, say, 4 bytes, it's because I actually need 4 bytes and if the > > file doesn't have 4 bytes for me, it's malformed. Because `f.read(4)` > > can silently return less than 4 bytes and I don't want to have to > > explicitly double check every read, I'm using a wrapper function. > > This is not a terrible idea. Other languages, like Pascal, have facility > for reading fixed-size chunks from a file, reading integers or floats > from a file. But there are some design questions that need to be sorted > out. > > If you're reading from, say, a serial port, and it only has three bytes, > should it hang until a fourth byte is available, or raise an exception > (thus losing the first three bytes)? >
My preferred colour for this particular bikeshed: Block. This API doesn't make sense for with non-blocking FDs (just use normal read() and accept short returns), so what it does in that situation probably doesn't matter; I'd be inclined to have it return the three bytes, and document that for non-blocking FDs, this is equivalent to read(n) and may not return the full length. I'd be most likely to use this sort of API with pipes, where the normal thing to do is to block until the other process has pushed the next block of data out. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/XH6AANQ2XSROANNOGA7GQML46YE6ATQO/ Code of Conduct: http://python.org/psf/codeofconduct/