python / python/cpython

'_Stream' object has no attribute 'seekable'

Abierto
#124,293 7 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

3.12 3.13 3.14 stdlib type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug report

Bug description:

From a recent application, I have found tarfile._Stream doesn't have a seekable() method. A quick reproduction of the problem is as follows:

import tarfile
import io

data = b"Test data."
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w") as tf:
    ti = tarfile.TarInfo(name="test.txt")
    ti.size = len(data)
    tf.addfile(tarinfo=ti, fileobj=io.BytesIO(data))

buf.seek(0)
tf = tarfile.open(fileobj=buf, mode="r|")
for entry in tf:
    f = tf.extractfile(entry)  # `f` is in type of `class ExFileObject(io.BufferedReader)`
    f.seekable()  # AttributeError
    f.seek(0)  # AttributeError

Output:

Traceback (most recent call last):
  File "a.py", line 17, in <module>
    print(f.seekable())
  File "AppData\Local\Programs\Python\Python310\lib\tarfile.py", line 641, in seekable
    return self.fileobj.seekable()
AttributeError: '_Stream' object has no attribute 'seekable'

When opening tarfile with r| mode, ExFileObject.fileobj would be a _Stream object. So when calling ExFileObject.seekable(), as _Stream object dun have seekable() method, it will raise AttributeError.

Also, as BufferedReader.seek() will also check its raw.seekable() (i.e. in Modules/_io/bufferedio.c, func _io__Buffered_seek_impl(), there is a call to _PyIOBase_check_seekable(state, self->raw, Py_True)), f.seek() in the above example will also raise AttributeError.

I have thought of if we can simply define seekable() in _Stream object, but as it is only "forward-seekable", I am not sure if directly returning a true/false in _Stream.seekable() is appropriate or not. However, this issue does break the IOBase interface and there should be a solution.

Thanks for everyone's time and effort for looking into this.

CPython versions tested on:

3.10, 3.11, 3.12, 3.13, CPython main branch

Operating systems tested on:

Linux, Windows

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza ejecutando la reproducción proporcionada de tarfile en r| mode y, a continuación, inspecciona tarfile._Stream y ExFileObject en tarfile.py junto con _io__Buffered_seek_impl() en Modules/_io/bufferedio.c. Determina el comportamiento de seek adecuado para los streams de solo avance y verifica que los AttributeErrors indicados de seekable() y seek() se resuelvan sin romper la semántica de streaming.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.