Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.1k
Description
While working on typeshed stubs, I've noticed that_pyio.BufferedReader.read1
does not raiseValueError
after.close
. While_io.BufferedReader.read1
does that.
So, I went to see both implementation and tests.
C
source clearly checks for this here:
cpython/Modules/_io/bufferedio.c
Line 910 in0689b99
CHECK_CLOSED(self,"read of closed file") |
But, tests were not executed correctly. Tests were only coveringC
version:https://github.com/python/cpython/blame/0689b99bb8c4f6058af43a52effaa8a25609dbed/Lib/test/test_io.py#L1532-L1538
deftest_read_on_closed(self):# Issue #23796b=io.BufferedReader(io.BytesIO(b"12"))b.read(1)b.close()self.assertRaises(ValueError,b.peek)self.assertRaises(ValueError,b.read1,1)
While universal test would look like this:
deftest_read_on_closed(self):# Issue #23796b=self.BufferedReader(self.BytesIO(b"12"))b.read(1)b.close()self.assertRaises(ValueError,b.peek)self.assertRaises(ValueError,b.read1,1)
And now it is failing:
» ./python.exe -m test -v test_io -m test_read_on_closed== CPython 3.12.0a1+ (heads/main:7ea10567af, Oct 29 2022, 13:15:59) [Clang 11.0.0 (clang-1100.0.33.16)]== macOS-10.14.6-x86_64-i386-64bit little-endian== cwd: /Users/sobolev/Desktop/cpython/build/test_python_78918æ== CPU count: 4== encodings: locale=UTF-8, FS=utf-80:00:00 load avg: 2.19 Run tests sequentially0:00:00 load avg: 2.19 [1/1] test_iotest_read_on_closed (test.test_io.CBufferedReaderTest.test_read_on_closed) ... oktest_read_on_closed (test.test_io.PyBufferedReaderTest.test_read_on_closed) ... FAILtest_read_on_closed (test.test_io.CBufferedRandomTest.test_read_on_closed) ... oktest_read_on_closed (test.test_io.PyBufferedRandomTest.test_read_on_closed) ... FAIL======================================================================FAIL: test_read_on_closed (test.test_io.PyBufferedReaderTest.test_read_on_closed)----------------------------------------------------------------------Traceback (most recent call last): File "/Users/sobolev/Desktop/cpython/Lib/test/test_io.py", line 1537, in test_read_on_closed self.assertRaises(ValueError, b.peek)AssertionError: ValueError not raised by peek======================================================================FAIL: test_read_on_closed (test.test_io.PyBufferedRandomTest.test_read_on_closed)----------------------------------------------------------------------Traceback (most recent call last): File "/Users/sobolev/Desktop/cpython/Lib/test/test_io.py", line 1537, in test_read_on_closed self.assertRaises(ValueError, b.peek)AssertionError: ValueError not raised by peek----------------------------------------------------------------------Ran 4 tests in 0.010sFAILED (failures=2)
After that, I've tested other methods and here's a list of methods that work on closed in_pyio
, but raises on_io
:
- readinto1
- readinto
- read1
- peek
Relatedhttps://bugs.python.org/issue23796
Is it a bug or a feature? Is it worth fixing?
This clearly looks like a bug to me.
If others confirm, I have a patch ready :)