drj11 / pypng

Pure Python library for PNG image encoding/decoding
MIT License
450 stars 91 forks source link

png.Reader not guessing bytes #118

Open mulac opened 2 years ago

mulac commented 2 years ago

The docs says Reader.init takes bytes but fails on the isinstance(some_bytes, array) check.

Is this expected behaviour?

>>> import png
>>> png.Reader("tests/doop.png")
<png.Reader object at 0x7fd48d3cf100>
>>> with open("tests/doop.png", 'rb') as f:
...     png.Reader(f.read())
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/calum/.cache/pypoetry/virtualenvs/hsi-ZCFKj1YC-py3.10/lib/python3.10/site-packages/png.py", line 1375, in __init__
    raise ProtocolError("expecting filename, file or bytes array")
png.ProtocolError: ProtocolError: expecting filename, file or bytes array
drj11 commented 2 years ago

The design predates the type bytes (it's from early Python 2, and possibly Python 1.5.2), so it only works on array.array("B").

However, i can see that png.Reader(f.read()) could be useful, so i shall consider this a bug.

png.Reader(f) should work by the way; although it does then become unclear when you should close f.