Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

`hasattr(stream, "buffer") fails with ValueError if buffer is detached

Aperta
#160,427 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

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

extension-modules topic-IO type-bug
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

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.