Commit 8bc2b3cd authored by Ricardo Kirkner's avatar Ricardo Kirkner

Cast OSError raised by os.lseek into IOError so we're consistent with Python 2.x stdlib

parent ce5a9c40
from __future__ import absolute_import from __future__ import absolute_import
import os import os
import sys
import io import io
from io import BufferedReader from io import BufferedReader
from io import BufferedWriter from io import BufferedWriter
...@@ -8,6 +9,7 @@ from io import DEFAULT_BUFFER_SIZE ...@@ -8,6 +9,7 @@ from io import DEFAULT_BUFFER_SIZE
from io import RawIOBase from io import RawIOBase
from io import UnsupportedOperation from io import UnsupportedOperation
from gevent._compat import PY3, reraise
from gevent._fileobjectcommon import cancel_wait_ex from gevent._fileobjectcommon import cancel_wait_ex
from gevent._fileobjectcommon import FileObjectBase from gevent._fileobjectcommon import FileObjectBase
from gevent.hub import get_hub from gevent.hub import get_hub
...@@ -140,7 +142,16 @@ class GreenFileDescriptorIO(RawIOBase): ...@@ -140,7 +142,16 @@ class GreenFileDescriptorIO(RawIOBase):
self.hub.wait(self._write_event) self.hub.wait(self._write_event)
def seek(self, offset, whence=0): def seek(self, offset, whence=0):
return os.lseek(self._fileno, offset, whence) try:
return os.lseek(self._fileno, offset, whence)
except OSError as ex:
if not PY3:
# Python 2.x
# make sure on Python 2.x we raise an IOError
exc_info = sys.exc_info()
reraise(IOError, IOError(*ex.args), tb=exc_info[2])
# otherwise just re-raise the original exception
raise
class FlushingBufferedWriter(BufferedWriter): class FlushingBufferedWriter(BufferedWriter):
......
...@@ -13,6 +13,7 @@ from gevent.testing.sysinfo import PY3 ...@@ -13,6 +13,7 @@ from gevent.testing.sysinfo import PY3
from gevent.testing.flaky import reraiseFlakyTestRaceConditionLibuv from gevent.testing.flaky import reraiseFlakyTestRaceConditionLibuv
from gevent.testing.skipping import skipOnLibuvOnCIOnPyPy from gevent.testing.skipping import skipOnLibuvOnCIOnPyPy
from gevent.testing.skipping import skipOnWindows from gevent.testing.skipping import skipOnWindows
from gevent.testing.skipping import skipOnPy37
try: try:
ResourceWarning ResourceWarning
...@@ -144,6 +145,25 @@ class Test(greentest.TestCase): ...@@ -144,6 +145,25 @@ class Test(greentest.TestCase):
self.assertEqual(native_data, s) self.assertEqual(native_data, s)
self.assertEqual(native_data, fileobj_data) self.assertEqual(native_data, fileobj_data)
@skipOnPy37
@skipOnWindows("FileObject not used on Win32")
def test_seek_raises_ioerror(self):
# Issue #1323
fd = sys.stdout
with self.assertRaises(OSError) as ctx:
os.lseek(fd.fileno(), 0, 0)
os_ex = ctx.exception
with self.assertRaises(IOError) as ctx:
f = FileObject(fd, 'r')
f.seek(0)
io_ex = ctx.exception
self.assertEqual(io_ex.args, os_ex.args)
self.assertEqual(io_ex.errno, os_ex.errno)
self.assertEqual(io_ex.strerror, os_ex.strerror)
self.assertEqual(str(io_ex), str(os_ex))
def test_close_pipe(self): def test_close_pipe(self):
# Issue #190, 203 # Issue #190, 203
r, w = os.pipe() r, w = os.pipe()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment