Issue 1230446: tarfile.py: ExFileObject's tell() is wrong after readline() (original) (raw)

This patch is intended to be the solution to a problem that Niklas Volbers reported on comp.lang.python on June 1st. ExFileObject is the pseudo file object that is returned by TarFile.extractfile() and allows a user to read a file from the tar archive using a file interface. Niklas discovered that the tell() method didn't give correct results if he used it in conjunction with readline(). The cause for this is that readline() buffers the file data in 100 char blocks to be able to split it into lines. Thus, tell() always returns the file position in 100 byte steps. While I was looking for a fix to that problem in tarfile.py, I discovered that ExFileObject has another flaw as well: I read in readline()'s docstring that mixing calls to read() and readline() is not allowed, which is nowhere documented. I decided to put a little effort into a rewrite of ExFileObject. Here it is, tests included.