benoitc / benoitc/restkit

IOError: [Errno 0] Error on write() in tee.TeeInput._tee

Open
#139 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
404
Forks
70
PR merge metrics
No merged PRs in 30d

Description

In very rare circumstances, the write() on the TemporaryFile files with an IOError, but no error code. I tracked the bug down to the following: When using a file object with read and write mode, then you have to flush or seek, when switching between reading and writing (see http://bugs.python.org/issue3207).

To fix this here, simply reset the filestream pointer by seeking to the position returned by tell(). More specifically, in tee.py:154 add `self.tmp.seek(self.tmp.tell())`.

If you want to reproduce the error yourself, use the following script. The html file needs to be bigger than two chunk sizes (32KB), so a file with 45 KB should do the trick.

``` python
import unittest
from restkit.tee import TeeInput
import os
import tempfile

class TempfileErrorCase(unittest.TestCase):

def test_ioerror(self):
with open("/path/to/large/html/file", "rb") as f:
i = TeeInput(f)
for _ in range(200):
i.readline()
i.tmp.flush()

def tearDown(self):
os.remove(self.tmpfile.name)

# Guard importing as main module
if __name__ == "__main__":
unittest.main()
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.