GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `_io` and `_pyio` inconsistenly raise `ValueError`s on closed files · Issue #98999 · python/cpython

_io and _pyio inconsistenly raise ValueErrors on closed files #98999

New issue
Copy link
New issue
Copy link
Closed
Closed
_io and _pyio inconsistenly raise ValueErrors on closed files#98999
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-IOtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@sobolevn
sobolevn
opened on Nov 2, 2022
Issue body actions

While working on typeshed stubs, I've noticed that _pyio.BufferedReader.read1 does not raise ValueError 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 in 0689b99

CHECK_CLOSED(self, "read of closed file")

But, tests were not executed correctly. Tests were only covering C version: https://github.com/python/cpython/blame/0689b99bb8c4f6058af43a52effaa8a25609dbed/Lib/test/test_io.py#L1532-L1538

    def test_read_on_closed(self):
        # Issue #23796
        b = 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:

    def test_read_on_closed(self):
        # Issue #23796
        b = 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-8
0:00:00 load avg: 2.19 Run tests sequentially
0:00:00 load avg: 2.19 [1/1] test_io
test_read_on_closed (test.test_io.CBufferedReaderTest.test_read_on_closed) ... ok
test_read_on_closed (test.test_io.PyBufferedReaderTest.test_read_on_closed) ... FAIL
test_read_on_closed (test.test_io.CBufferedRandomTest.test_read_on_closed) ... ok
test_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.010s

FAILED (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

Related https://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 :)

  • PR: gh-98999: Raise ValueError in _pyio on closed buffers #99009

Activity

  1. sobolevn
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-IO
    on Nov 2, 2022
  2. corona10 commented on Nov 2, 2022

    @corona10
    corona10
    on Nov 2, 2022
    Member
    More actions

    @sobolevn It is worth to fix it, Please send the patch.

  3. sobolevn
    added a commit that references this issue on Nov 2, 2022

    pythongh-98999: Raise `ValueError` in `_pyio` on closed buffers

    597ccf8
  4. sobolevn commented on Nov 2, 2022

    @sobolevn
    sobolevn
    on Nov 2, 2022
    MemberAuthor
    More actions

    Done :)
    Thank you!

  5. corona10
    added a commit that references this issue on Nov 3, 2022

    gh-98999: Raise `ValueError` in `_pyio` on closed buffers (gh-99009)

    2cfcaf5
  6. sobolevn
    closed this as completedon Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-IOtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.