Title: `hasattr(stream, "buffer")` fails with ValueError if buffer is detached · Issue #160427 · python/cpython · GitHub
Open Graph Title: `hasattr(stream, "buffer")` fails with ValueError if buffer is detached · Issue #160427 · python/cpython
X Title: `hasattr(stream, "buffer")` fails with ValueError if buffer is detached · Issue #160427 · python/cpython
Description: Bug report >>> stream = open("example.txt", "w") >>> hasattr(stream, "buffer") True >>> stream.detach() <_io.BufferedWriter name='example.txt'> >>> hasattr(stream, "buffer") Traceback (most recent call last): File "
Open Graph Description: Bug report >>> stream = open("example.txt", "w") >>> hasattr(stream, "buffer") True >>> stream.detach() <_io.BufferedWriter name='example.txt'> >>> hasattr(stream, "buffer") Traceback (most recent ...
X Description: Bug report >>> stream = open("example.txt", "w") >>> hasattr(stream, "buffer") True >>> stream.detach() <_io.BufferedWriter name='exa...
Opengraph URL: https://github.com/python/cpython/issues/160427
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`hasattr(stream, \"buffer\")` fails with ValueError if buffer is detached","articleBody":"### Bug report\n\n```python\n>>> stream = open(\"example.txt\", \"w\")\n>>> hasattr(stream, \"buffer\")\nTrue\n>>> stream.detach()\n\u003c_io.BufferedWriter name='example.txt'>\n>>> hasattr(stream, \"buffer\")\nTraceback (most recent call last):\n File \"\u003cpython-input-2>\", line 1, in \u003cmodule>\n hasattr(stream, \"buffer\")\n ~~~~~~~^^^^^^^^^^^^^^^^^^\nValueError: underlying buffer has been detached\n```\n\nThis occurs with Python 3.14.9 and 3.15.1, but not with earlier versions such as 3.14.8 and 3.15.0. Tested both on Linux and on Windows. I consider `hasattr` failing with an exception pretty bad, especially when using it with standard library objects, and the behavior changing in a bug fix release makes it even worse.\n\nI noticed this when a unit test for our custom `isatty` utility started to fail (https://github.com/robotframework/robotframework/issues/5825). We have that utility so that we can safely check is a stream connected to console or not. It used to look like this:\n\n```python\ndef isatty(stream):\n # first check if buffer was detached\n if hasattr(stream, \"buffer\") and stream.buffer is None:\n return False\n if not hasattr(stream, \"isatty\"):\n return False\n try:\n return stream.isatty()\n except ValueError: # Occurs if file is closed.\n return False\n```\n\nI already fixed this in our end by simply removing the separate detached buffer check. `stream.isatty()` raised a ValueError if its buffer is detached, similarly when the stream is closed, and we already handle that. Apparently the separate check was added before handling closed streams and nobody noticed earlier that the check is redundant.\n\n### CPython versions tested on:\n\n3.14, 3.15\n\n### Operating systems tested on:\n\nLinux, Windows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160467\n* gh-160802\n* gh-161449\n* gh-161450\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/pekkaklarck","@type":"Person","name":"pekkaklarck"},"datePublished":"2026-10-23T10:36:38Z","url":"https://github.com/python/cpython/issues/160427"}
| 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:7ff4043f-f6db-14b5-e0da-6f81f61be919 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CEA8:DB09F:3751310:46A017E:6B01FA3F |
| html-safe-nonce | a098bd898819d8f3fdb6b816a42ba83ea11ee7e4625b7a64e11d7d2718520b2b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUE4OkRCMDlGOjM3NTEzMTA6NDZBMDE3RTo2QjAxRkEzRiIsInZpc2l0b3JfaWQiOiI2MjI3NTcxMjQ1MTkwMzQ3MzI3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2938ad35a6805645201b903f33f4bda53f19786911f13c5e240f48cb9d498cd4 |
| hovercard-subject-tag | issue:5996240055 |
| 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/160427/issue_layout |
| twitter:image | https://opengraph.githubassets.com/308abb529c08c406b733363b7bf742f800bc0148cdd6f4ee63960ebc260e5ca9/python/cpython/issues/160427 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/308abb529c08c406b733363b7bf742f800bc0148cdd6f4ee63960ebc260e5ca9/python/cpython/issues/160427 |
| og:image:alt | Bug report >>> stream = open("example.txt", "w") >>> hasattr(stream, "buffer") True >>> stream.detach() <_io.BufferedWriter name='example.txt'> >>> hasattr(stream, "buffer") Traceback (most recent ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pekkaklarck |
| 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