`hasattr(stream, "buffer")` fails with ValueError if buffer is detached
Maintainer antworten meist innerhalb von 1 Tag
@cmaloney arbeitet bereits daran.
Seit 28.10.2026.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Start by reading the linked pull request #160467 and the stream buffer property implementation and related tests it identifies. Reproduce the detached-buffer example in the issue on the affected Python versions, then use the relevant tests to confirm hasattr(stream, "buffer") no longer raises ValueError after detachment.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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 "<python-input-2>", line 1, in <module>
hasattr(stream, "buffer")
~~~~~~~^^^^^^^^^^^^^^^^^^
ValueError: underlying buffer has been detached
This 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.
I 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:
def isatty(stream):
# first check if buffer was detached
if hasattr(stream, "buffer") and stream.buffer is None:
return False
if not hasattr(stream, "isatty"):
return False
try:
return stream.isatty()
except ValueError: # Occurs if file is closed.
return False
I 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.
CPython versions tested on:
3.14, 3.15
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-160467
- Vorherrschende Sprache
- Python
- Sterne
- 77.8k
- Forks
- 37.7k
- Ø Merge
- 12 Std. 27 Min.
- Gemergte PRs (30 T.)
- 710
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/cpython
-
3.16 build type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
python/cpython#160829 · 4 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
dependencies infra topic-ensurepip
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
Maintainer antworten meist innerhalb von 1 Tag
-
symtable: repr() of a Symbol raises TypeError under -X lazy_imports=allEvtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offenstdlib topic-lazy-imports type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
python/cpython#160810 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
tarfile: extractfile(member).read() of a sparse member is slow due to repeated bytes concatenationOffenperformance stdlib triaged type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
python/cpython#160774 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
tests topic-profiling type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
python/cpython#160334 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Graphify-Labs/graphify#4860 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
:bug: Bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
scikit-image/scikit-image#8377 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Ingredient import validation accepts negative nutrient values and NaNEvtl. vergeben @LukeSekai hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
wger-project/wger#2564 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
apache/ponymail-foal#351 ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 4 Tagen