Commit 2cfffde3 authored by Jason Madden's avatar Jason Madden

We no longer need to gc before getting a libuv io object on PyPy: everything...

We no longer need to gc before getting a libuv io object on PyPy: everything is deterministically closed (or should be).
parent 509c1d66
......@@ -26,10 +26,11 @@ class GreenFileDescriptorIO(RawIOBase):
_read_event = None
_write_event = None
_closed = False
_seekable = None
def __init__(self, fileno, mode='r', closefd=True):
RawIOBase.__init__(self) # Python 2: pylint:disable=no-member,non-parent-init-called
self._closed = False
self._closefd = closefd
self._fileno = fileno
make_nonblocking(fileno)
......@@ -44,8 +45,6 @@ class GreenFileDescriptorIO(RawIOBase):
if writable:
self._write_event = io_watcher(fileno, 2)
self._seekable = None
def readable(self):
return self._read_event is not None
......@@ -81,11 +80,9 @@ class GreenFileDescriptorIO(RawIOBase):
self._read_event = self._write_event = None
if read_event is not None:
self.hub.cancel_wait(read_event, cancel_wait_ex)
read_event.close()
self.hub.cancel_wait(read_event, cancel_wait_ex, True)
if write_event is not None:
self.hub.cancel_wait(write_event, cancel_wait_ex)
write_event.close()
self.hub.cancel_wait(write_event, cancel_wait_ex, True)
fileno = self._fileno
if self._closefd:
......
......@@ -11,7 +11,6 @@ from operator import delitem
import signal
from gevent._ffi import _dbg # pylint: disable=unused-import
from gevent._compat import PYPY
from gevent._ffi.loop import AbstractLoop
from gevent.libuv import _corecffi # pylint:disable=no-name-in-module,import-error
from gevent._ffi.loop import assign_standard_callbacks
......@@ -62,19 +61,6 @@ def get_header_version():
def supported_backends():
return ['default']
if PYPY:
def gcBefore(f):
import functools
import gc
@functools.wraps(f)
def m(self, *args):
gc.collect()
return f(self, *args)
return m
else:
def gcBefore(f):
return f
class loop(AbstractLoop):
......@@ -447,7 +433,6 @@ class loop(AbstractLoop):
watcher._set_status(status)
@gcBefore
def io(self, fd, events, ref=True, priority=None):
# We rely on hard references here and explicit calls to
# close() on the returned object to correctly manage
......
......@@ -54,7 +54,7 @@ class libuv_error_wrapper(object):
str(ffi.string(libuv.uv_err_name(res)).decode('ascii')
+ ' '
+ ffi.string(libuv.uv_strerror(res)).decode('ascii'))
+ "Args: " + repr(args) + "KWARGS: " + repr(kwargs)
+ " Args: " + repr(args) + " KWARGS: " + repr(kwargs)
)
return res
......
......@@ -291,5 +291,5 @@ class TestCase(TestCaseMetaClass("NewBase", (BaseTestCase,), {})):
except AssertionError:
flaky.reraiseFlakyTestRaceCondition()
if not hasattr(TestCase, 'assertRaisesRegex'):
TestCase.assertRaisesRegex = TestCase.assertRaisesRegexp
assertRaisesRegex = getattr(BaseTestCase, 'assertRaisesRegex',
getattr(BaseTestCase, 'assertRaisesRegexp'))
......@@ -3,13 +3,13 @@ import os
import sys
import tempfile
import gc
import greentest
import gevent
from gevent.fileobject import FileObject, FileObjectThread
PYPY = hasattr(sys, 'pypy_version_info')
import greentest
from greentest.sysinfo import PY3
from greentest.flaky import reraiseFlakyTestRaceConditionLibuv
class Test(greentest.TestCase):
......@@ -41,18 +41,14 @@ class Test(greentest.TestCase):
gc.collect() # PyPy
if kwargs.get("close", True):
try:
with self.assertRaises((OSError, IOError)):
# expected, because FileObject already closed it
os.close(w)
except (OSError, IOError):
pass # expected, because FileObject already closed it
else:
raise AssertionError('os.close(%r) must not succeed on %r' % (w, ts))
else:
os.close(w)
fobj = FileObject(r, 'rb')
with FileObject(r, 'rb') as fobj:
self.assertEqual(fobj.read(), b'x')
fobj.close()
def test_del(self):
# Close should be true by default
......@@ -69,11 +65,9 @@ class Test(greentest.TestCase):
self._test_del(close=False)
else:
def test_del_noclose(self):
try:
with self.assertRaisesRegex(TypeError,
'FileObjectThread does not support close=False on an fd.'):
self._test_del(close=False)
self.fail("Shouldn't be able to create a FileObjectThread with close=False")
except TypeError as e:
self.assertEqual(str(e), 'FileObjectThread does not support close=False on an fd.')
def test_newlines(self):
r, w = os.pipe()
......@@ -101,10 +95,17 @@ class Test(greentest.TestCase):
native_data = f.read(1024)
with open(path, 'rb') as f_raw:
print("Opened", f_raw)
try:
f = FileObject(f_raw, 'rb')
if hasattr(f, 'seekable'):
# Py3
except ValueError:
# libuv on Travis can raise EPERM
# from FileObjectPosix. I can't produce it on mac os locally,
# don't know what the issue is. This started happening on Jan 19,
# in the branch that caused all watchers to be explicitly closed.
# That shouldn't have any effect on io watchers, though, which were
# already being explicitly closed.
reraiseFlakyTestRaceConditionLibuv()
if PY3 or FileObject is not FileObjectThread:
self.assertTrue(f.seekable())
f.seek(15)
self.assertEqual(15, f.tell())
......@@ -126,9 +127,9 @@ class Test(greentest.TestCase):
r, w = os.pipe()
x = FileObject(r)
y = FileObject(w, 'w')
assert hasattr(x, 'read1'), x
x.close()
y.close()
self._close_on_teardown(x)
self._close_on_teardown(y)
self.assertTrue(hasattr(x, 'read1'))
#if FileObject is not FileObjectThread:
def test_bufsize_0(self):
......@@ -136,6 +137,8 @@ class Test(greentest.TestCase):
r, w = os.pipe()
x = FileObject(r, 'rb', bufsize=0)
y = FileObject(w, 'wb', bufsize=0)
self._close_on_teardown(x)
self._close_on_teardown(y)
y.write(b'a')
b = x.read(1)
self.assertEqual(b, b'a')
......
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