python-zstandard icon indicating copy to clipboard operation
python-zstandard copied to clipboard

`fileno` not available on `ZstdDecompressionReader`

Open jap opened this issue 1 year ago • 1 comments

According to the shipped typing stubs, ZstdDecompressionReader implements the typing.BinaryIO interface.

Unfortunately, it lacks the fileno() method that is promised by BinaryIO so this is a bit of a lie. This breaks when trying to do something like:

>>> f = open('foo.zstd', 'rb')
>>> d = zstandard.ZstdDecompressor()
>>> g = d.stream_reader(f)
>>> subprocess.run(['cat'], stdin = g)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/spaans/.pyenv/versions/3.12.1/lib/python3.12/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spaans/.pyenv/versions/3.12.1/lib/python3.12/subprocess.py", line 992, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spaans/.pyenv/versions/3.12.1/lib/python3.12/subprocess.py", line 1708, in _get_handles
    p2cread = stdin.fileno()
              ^^^^^^^^^^^^
AttributeError: 'zstd.ZstdDecompressionReader' object has no attribute 'fileno'

Note that StringIO in the standard library also doesn't support fileno, but has instead chosen to raise an Exception when it is used like this, so a possible solution could be to just mimic that behaviour.

jap avatar Dec 14 '23 16:12 jap

The "specification" for this is in the io module and says

Return the underlying file descriptor (an integer) of the stream if it exists. An OSError is raised if the IO object does not use a file descriptor.

enkore avatar Mar 25 '24 17:03 enkore