Title: _pyio buffered classes raise AttributeError after detach() instead of ValueError · Issue #160472 · python/cpython · GitHub
Open Graph Title: _pyio buffered classes raise AttributeError after detach() instead of ValueError · Issue #160472 · python/cpython
X Title: _pyio buffered classes raise AttributeError after detach() instead of ValueError · Issue #160472 · python/cpython
Description: Bug description After detach(), the pure-Python buffered classes in _pyio raise AttributeError from the None raw stream, where the C implementations raise ValueError. The two implementations therefore disagree on the kind of error, not j...
Open Graph Description: Bug description After detach(), the pure-Python buffered classes in _pyio raise AttributeError from the None raw stream, where the C implementations raise ValueError. The two implementations theref...
X Description: Bug description After detach(), the pure-Python buffered classes in _pyio raise AttributeError from the None raw stream, where the C implementations raise ValueError. The two implementations theref...
Opengraph URL: https://github.com/python/cpython/issues/160472
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_pyio buffered classes raise AttributeError after detach() instead of ValueError","articleBody":"## Bug description\n\nAfter `detach()`, the pure-Python buffered classes in `_pyio` raise `AttributeError`\nfrom the `None` raw stream, where the C implementations raise `ValueError`. The two\nimplementations therefore disagree on the *kind* of error, not just the wording.\n\n```python\nimport io, _pyio\n\nfor mod in (io, _pyio):\n b = mod.BufferedReader(mod.BytesIO(b\"abcdef\"))\n b.detach()\n for op, args in [(\"read\", (1,)), (\"seek\", (0,)), (\"truncate\", ()), (\"flush\", ())]:\n try:\n getattr(b, op)(*args)\n result = \"no error\"\n except Exception as exc:\n result = type(exc).__name__\n print(mod.__name__, op, \"->\", result)\n```\n\n```text\nio read -> ValueError\nio seek -> ValueError\nio truncate -> ValueError\nio flush -> ValueError\n_pyio read -> AttributeError\n_pyio seek -> AttributeError\n_pyio truncate -> AttributeError\n_pyio flush -> AttributeError\n```\n\nSame shape for `_pyio.BufferedWriter` (`write`, `flush`, `seek`, `tell`),\n`_pyio.BufferedRandom` and `_pyio.TextIOWrapper.tell()`.\n\n## Cause\n\nIn `Lib/_pyio.py`, `BufferedReader.detach()` does\n\n```python\ndef detach(self):\n if self.raw is None:\n raise ValueError(\"raw stream already detached\")\n self.flush()\n raw = self._raw\n self._raw = None\n return raw\n```\n\nso the class already knows the detached state, but only `detach()` itself checks\nit. Every other method reaches `self.raw` and fails on the `None`, producing\n`AttributeError: 'NoneType' object has no attribute '...'`.\n\n## Expected behaviour\n\n`ValueError`, matching the C implementation and matching the documented state of\nthe object (\"After the raw stream has been detached, the buffer is in an unusable\nstate\"). The check C performs is the `CHECK_ATTACHED` macro in\n`Modules/_io/bufferedio.c`.\n\n## Why it matters beyond error text\n\n`test_io` runs the same test classes against both implementations (`C` and\n`Py` variants in `Lib/test/test_io/`), so any test that asserts `ValueError` for a\ndetached operation cannot be shared today. A `_checkDetached()` helper next to the\nexisting `_checkClosed()` would let those tests be written once.\n\n## Scope\n\nI have not opened a patch, following the guidance in #160467 that the expected\nbehaviour should be agreed on the issue first. If the `ValueError` above is the\ndirection a triager wants, I can prepare the fix plus a test in `test_textio.py`\nand `test_bufferedio.py` that runs against both implementations.\n\nFound by differential testing `_io` against `_pyio` over random operation\nsequences.\n\n### CPython versions tested on\n\nCPython main branch (3.16.0a1+)\n\n### Operating systems tested on\n\nLinux\n","author":{"url":"https://github.com/scrieve","@type":"Person","name":"scrieve"},"datePublished":"2026-10-23T19:41:07Z","url":"https://github.com/python/cpython/issues/160472"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d2ea1bde-4e7f-fefc-7dc8-8b6bd41ad6de |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB24:21AC3F:40148BA:511775E:6B01FA19 |
| html-safe-nonce | f29580a25adaf3cecc58f3ceffe366b59b57e4e75bf4255f667539881b360ec4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjI0OjIxQUMzRjo0MDE0OEJBOjUxMTc3NUU6NkIwMUZBMTkiLCJ2aXNpdG9yX2lkIjoiMzU5NjQyOTIwODI3Mzk0MzA2NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fe7bd6a100b05474e2ce26201f81af8c5fe2070c11b8fdfba933cf7e79c2886f |
| hovercard-subject-tag | issue:6004339769 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/160472/issue_layout |
| twitter:image | https://opengraph.githubassets.com/141e4367e82b4c62ed8ac8e2056aa55bba2611266757dc324e3ebfeb53cc5cd0/python/cpython/issues/160472 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/141e4367e82b4c62ed8ac8e2056aa55bba2611266757dc324e3ebfeb53cc5cd0/python/cpython/issues/160472 |
| og:image:alt | Bug description After detach(), the pure-Python buffered classes in _pyio raise AttributeError from the None raw stream, where the C implementations raise ValueError. The two implementations theref... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | scrieve |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width