`hasattr(stream, "buffer") fails with ValueError if buffer is detached
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
- #160467 di @scrieve — chiusa senza merge
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Python
- Stelle
- 77.7k
- Fork
- 38.3k
- Merge medio
- 13h 43m
- PR unite (30g)
- 704
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/cpython
-
topic-socket type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python/cpython#160561 · 7 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
tests topic-profiling type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python/cpython#160334 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
`iso2022_jp_3` encodes U+9B1D but decodes the result as U+9B1CForse già presa @kinqsradio l’ha presa oggi. Apertaextension-modules type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
Remove unused FVS_* macrosApertainterpreter-core type-feature
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
python/cpython#160109 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
extension-modules type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python/cpython#160001 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di python/cpython
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
btclib-org/btclib-node#2083 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
Gathin23/cleanyourtv#2089 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
jxxghp/MoviePilot#7212 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
cli dx rust
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
NekomyaDev/nudge#278 ·
I maintainer di solito rispondono entro 1 giorno