Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

_io and_pyio inconsistenly raiseValueErrors on closed files #98999

Closed
Labels
stdlibPython modules in the Lib dirtopic-IOtype-bugAn unexpected behavior, bug, or error
@sobolevn

Description

@sobolevn

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:

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp