Skip to content

Commit

Permalink
test__fileobject: Make readinto test use readable names; make readint…
Browse files Browse the repository at this point in the history
…o test not leak file descriptors; add change note for #1948.

Fixes #1948
  • Loading branch information
jamadden committed Jul 10, 2023
1 parent bc40651 commit c3fb121
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/changes/1948.bugfix
@@ -0,0 +1,2 @@
Make gevent's ``FileObjectThread`` (mostly used on Windows) implement
``readinto`` cooperatively. PR by Kirill Smelkov.
23 changes: 12 additions & 11 deletions src/gevent/tests/test__fileobject.py
Expand Up @@ -305,16 +305,17 @@ def test_readinto_serial(self):
os.write(fileno, b'hello world')
os.close(fileno)

buf = bytearray(32)
mbuf = memoryview(buf)

def assertReadInto(byte_count, expected_data):
bytes_read = f.readinto(mbuf[:byte_count])
self.assertEqual(bytes_read, len(expected_data))
self.assertEqual(buf[:bytes_read], expected_data)

with self._makeOne(path, 'rb') as f:
buf = bytearray(32)
mbuf = memoryview(buf)
def assertReadInto(n, dataok):
n_ = f.readinto(mbuf[:n])
self.assertEqual(n_, len(dataok))
self.assertEqual(buf[:n_], dataok)

assertReadInto(2, b'he')
assertReadInto(1, b'l')
assertReadInto(2, b'he')
assertReadInto(1, b'l')
assertReadInto(32, b'lo world')
assertReadInto(32, b'')

Expand Down Expand Up @@ -392,8 +393,8 @@ def test_readinto(self):
# if .readinto() is not cooperative spawned greenlet will not be able
# to run and call to .readinto() will block forever.
r, w = self._pipe()
rf = self._makeOne(r, 'rb')
wf = self._makeOne(w, 'wb')
rf = self._close_on_teardown(self._makeOne(r, 'rb'))
wf = self._close_on_teardown(self._makeOne(w, 'wb'))
g = gevent.spawn(Writer, wf, [b'hello'])

try:
Expand Down

0 comments on commit c3fb121

Please sign in to comment.