Commit ab6536fd authored by Jason Madden's avatar Jason Madden

Simplify reraising logic for GreenFileDescriptorIO.seek.

Don't introduce a reference cycle with the traceback.

In the test, don't use sys.stdout as a non-seekable stream; putting it into non-blocking mode messes up the console. Instead create a pipe for this purpose.

Simplify test cases by splitting FOPosix and FOThread.
parent 8bc2b3cd
...@@ -67,6 +67,10 @@ ...@@ -67,6 +67,10 @@
`UserWarning` when using the libuv backend. Reported in `UserWarning` when using the libuv backend. Reported in
:issue:`1321` by ZeroNet. :issue:`1321` by ZeroNet.
- Fix ``FileObjectPosix.seek`` raising `OSError` when it should have
been `IOError` on Python 2. Reported by, and PR by, Ricardo Kirkner.
See :issue:`1323`.
1.3.7 (2018-10-12) 1.3.7 (2018-10-12)
================== ==================
......
...@@ -9,7 +9,7 @@ from io import DEFAULT_BUFFER_SIZE ...@@ -9,7 +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._compat import 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
...@@ -144,14 +144,15 @@ class GreenFileDescriptorIO(RawIOBase): ...@@ -144,14 +144,15 @@ class GreenFileDescriptorIO(RawIOBase):
def seek(self, offset, whence=0): def seek(self, offset, whence=0):
try: try:
return os.lseek(self._fileno, offset, whence) return os.lseek(self._fileno, offset, whence)
except OSError as ex: except IOError: # pylint:disable=try-except-raise
if not PY3: raise
except OSError as ex: # pylint:disable=duplicate-except
# Python 2.x # Python 2.x
# make sure on Python 2.x we raise an IOError # make sure on Python 2.x we raise an IOError
exc_info = sys.exc_info() # as documented for RawIOBase.
reraise(IOError, IOError(*ex.args), tb=exc_info[2]) # See https://github.com/gevent/gevent/issues/1323
# otherwise just re-raise the original exception reraise(IOError, IOError(*ex.args), sys.exc_info()[2])
raise
class FlushingBufferedWriter(BufferedWriter): class FlushingBufferedWriter(BufferedWriter):
...@@ -160,6 +161,7 @@ class FlushingBufferedWriter(BufferedWriter): ...@@ -160,6 +161,7 @@ class FlushingBufferedWriter(BufferedWriter):
self.flush() self.flush()
return ret return ret
class FileObjectPosix(FileObjectBase): class FileObjectPosix(FileObjectBase):
""" """
A file-like object that operates on non-blocking files but A file-like object that operates on non-blocking files but
......
...@@ -6,14 +6,13 @@ import gc ...@@ -6,14 +6,13 @@ import gc
import unittest import unittest
import gevent import gevent
from gevent.fileobject import FileObject, FileObjectThread from gevent import fileobject
import gevent.testing as greentest import gevent.testing as greentest
from gevent.testing.sysinfo import PY3 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
...@@ -22,7 +21,20 @@ except NameError: ...@@ -22,7 +21,20 @@ except NameError:
"Python 2 fallback" "Python 2 fallback"
class Test(greentest.TestCase): def writer(fobj, line):
for character in line:
fobj.write(character)
fobj.flush()
fobj.close()
class TestFileObjectThread(greentest.TestCase):
def _getTargetClass(self):
return fileobject.FileObjectThread
def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)
def _test_del(self, **kwargs): def _test_del(self, **kwargs):
pipe = os.pipe() pipe = os.pipe()
...@@ -37,7 +49,7 @@ class Test(greentest.TestCase): ...@@ -37,7 +49,7 @@ class Test(greentest.TestCase):
def _do_test_del(self, pipe, **kwargs): def _do_test_del(self, pipe, **kwargs):
r, w = pipe r, w = pipe
s = FileObject(w, 'wb', **kwargs) s = self._makeOne(w, 'wb', **kwargs)
s.write(b'x') s.write(b'x')
try: try:
s.flush() s.flush()
...@@ -61,7 +73,7 @@ class Test(greentest.TestCase): ...@@ -61,7 +73,7 @@ class Test(greentest.TestCase):
else: else:
os.close(w) os.close(w)
with FileObject(r, 'rb') as fobj: with self._makeOne(r, 'rb') as fobj:
self.assertEqual(fobj.read(), b'x') self.assertEqual(fobj.read(), b'x')
# We only use FileObjectThread on Win32. Sometimes the # We only use FileObjectThread on Win32. Sometimes the
...@@ -81,13 +93,9 @@ class Test(greentest.TestCase): ...@@ -81,13 +93,9 @@ class Test(greentest.TestCase):
def test_del_close(self): def test_del_close(self):
self._test_del(close=True) self._test_del(close=True)
if FileObject is not FileObjectThread:
# FileObjectThread uses os.fdopen() when passed a file-descriptor, which returns # FileObjectThread uses os.fdopen() when passed a file-descriptor, which returns
# an object with a destructor that can't be bypassed, so we can't even # an object with a destructor that can't be bypassed, so we can't even
# create one that way # create one that way
def test_del_noclose(self):
self._test_del(close=False)
else:
def test_del_noclose(self): def test_del_noclose(self):
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
'FileObjectThread does not support close=False on an fd.'): 'FileObjectThread does not support close=False on an fd.'):
...@@ -97,13 +105,13 @@ class Test(greentest.TestCase): ...@@ -97,13 +105,13 @@ class Test(greentest.TestCase):
import warnings import warnings
r, w = os.pipe() r, w = os.pipe()
lines = [b'line1\n', b'line2\r', b'line3\r\n', b'line4\r\nline5', b'\nline6'] lines = [b'line1\n', b'line2\r', b'line3\r\n', b'line4\r\nline5', b'\nline6']
g = gevent.spawn(writer, FileObject(w, 'wb'), lines) g = gevent.spawn(writer, self._makeOne(w, 'wb'), lines)
try: try:
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
# U is deprecated in Python 3, shows up on FileObjectThread # U is deprecated in Python 3, shows up on FileObjectThread
fobj = FileObject(r, 'rU') fobj = self._makeOne(r, 'rU')
result = fobj.read() result = fobj.read()
fobj.close() fobj.close()
self.assertEqual('line1\nline2\nline3\nline4\nline5\nline6', result) self.assertEqual('line1\nline2\nline3\nline4\nline5\nline6', result)
...@@ -127,7 +135,7 @@ class Test(greentest.TestCase): ...@@ -127,7 +135,7 @@ class Test(greentest.TestCase):
with open(path, 'rb') as f_raw: with open(path, 'rb') as f_raw:
try: try:
f = FileObject(f_raw, 'rb') f = self._makeOne(f_raw, 'rb')
except ValueError: except ValueError:
# libuv on Travis can raise EPERM # libuv on Travis can raise EPERM
# from FileObjectPosix. I can't produce it on mac os locally, # from FileObjectPosix. I can't produce it on mac os locally,
...@@ -136,7 +144,7 @@ class Test(greentest.TestCase): ...@@ -136,7 +144,7 @@ class Test(greentest.TestCase):
# That shouldn't have any effect on io watchers, though, which were # That shouldn't have any effect on io watchers, though, which were
# already being explicitly closed. # already being explicitly closed.
reraiseFlakyTestRaceConditionLibuv() reraiseFlakyTestRaceConditionLibuv()
if PY3 or FileObject is not FileObjectThread: if PY3 or not isinstance(f, fileobject.FileObjectThread):
self.assertTrue(f.seekable()) self.assertTrue(f.seekable())
f.seek(15) f.seek(15)
self.assertEqual(15, f.tell()) self.assertEqual(15, f.tell())
...@@ -145,38 +153,19 @@ class Test(greentest.TestCase): ...@@ -145,38 +153,19 @@ 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()
x = FileObject(r) x = self._makeOne(r)
y = FileObject(w, 'w') y = self._makeOne(w, 'w')
x.close() x.close()
y.close() y.close()
def test_read1(self): def test_read1(self):
# Issue #840 # Issue #840
r, w = os.pipe() r, w = os.pipe()
x = FileObject(r) x = self._makeOne(r)
y = FileObject(w, 'w') y = self._makeOne(w, 'w')
self._close_on_teardown(x) self._close_on_teardown(x)
self._close_on_teardown(y) self._close_on_teardown(y)
self.assertTrue(hasattr(x, 'read1')) self.assertTrue(hasattr(x, 'read1'))
...@@ -185,8 +174,8 @@ class Test(greentest.TestCase): ...@@ -185,8 +174,8 @@ class Test(greentest.TestCase):
def test_bufsize_0(self): def test_bufsize_0(self):
# Issue #840 # Issue #840
r, w = os.pipe() r, w = os.pipe()
x = FileObject(r, 'rb', bufsize=0) x = self._makeOne(r, 'rb', bufsize=0)
y = FileObject(w, 'wb', bufsize=0) y = self._makeOne(w, 'wb', bufsize=0)
self._close_on_teardown(x) self._close_on_teardown(x)
self._close_on_teardown(y) self._close_on_teardown(y)
y.write(b'a') y.write(b'a')
...@@ -197,11 +186,44 @@ class Test(greentest.TestCase): ...@@ -197,11 +186,44 @@ class Test(greentest.TestCase):
b = x.read(1) b = x.read(1)
self.assertEqual(b, b'2') self.assertEqual(b, b'2')
def writer(fobj, line):
for character in line: @unittest.skipUnless(
fobj.write(character) hasattr(fileobject, 'FileObjectPosix'),
fobj.flush() "Needs FileObjectPosix"
fobj.close() )
class TestFileObjectPosix(TestFileObjectThread):
def _getTargetClass(self):
return fileobject.FileObjectPosix
def test_del_noclose(self):
self._test_del(close=False)
def test_seek_raises_ioerror(self):
# https://github.com/gevent/gevent/issues/1323
# Get a non-seekable file descriptor
r, w = os.pipe()
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)
with self.assertRaises(OSError) as ctx:
os.lseek(r, 0, os.SEEK_SET)
os_ex = ctx.exception
with self.assertRaises(IOError) as ctx:
f = self._makeOne(r, 'r', close=False)
# Seek directly using the underlying GreenFileDescriptorIO;
# the buffer may do different things, depending
# on the version of Python (especially 3.7+)
f.fileio.seek(0)
io_ex = ctx.exception
self.assertEqual(io_ex.errno, os_ex.errno)
self.assertEqual(io_ex.strerror, os_ex.strerror)
self.assertEqual(io_ex.args, os_ex.args)
self.assertEqual(str(io_ex), str(os_ex))
class TestTextMode(unittest.TestCase): class TestTextMode(unittest.TestCase):
......
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