New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
imghdr.what doesn't accept bytes paths #64196
Comments
imghdr.what check explicitly for string path, while >>> x
b'\xc2\xba'
>>> imghdr.what(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tank/libs/cpython/Lib/imghdr.py", line 15, in what
location = file.tell()
AttributeError: 'bytes' object has no attribute 'tell'
>>> open(x)
<_io.TextIOWrapper name=b'\xc2\xba' mode='r' encoding='UTF-8'> A reason why this should be supported can be found in this message: http://bugs.python.org/msg191691. |
Why limit to str and bytes. Open can accept integer as well. I am asking not suggesting. >>> with open('/tmp/cutecat.txt', 'wb') as f:
... f.write(b'cutecat')
...
7
>>> a = open('/tmp/cutecat.txt')
>>> a.fileno()
3
>>> b = open(3)
>>> b.read()
'cutecat' |
Thanks, Vajrasky, I forgot about the fact that open can open file descriptors. Here's a patch which fixes this, also adds a test for BytesIO and uses os.fsencode instead of .encode(). |
imghdr_bytes.patch should use os.fsencode() to encode the filename, not filename.encode(). (You may use a valid sound file of the Python suite test instead of generating an invalid sounf file.) imghdr_files.patch looks overkill. I don't think that it's useful to support integer file descriptor. Being able to pass an open file is enough. imghdr_bytes.patch is fine. |
Hi, Victor! The second patch does use os.fsencode. I'll update the first one with this change. |
Added the new version.
Oh, that's sndhdr, currently imghdr doesn't have valid image files in the Python suite test (there is another issue for adding a test file for this module). If it's required, sure, I'll add a few. |
Updated patch to use real image files from issue bpo-19990. |
Patch updated. It removes the check added in http://hg.python.org/cpython/rev/94813eab5a58 and simplifies the test for bytes file path. |
I'm not sure imghdr.what() should support bytes path. The open() builtin, most os and os.path functions support string and bytes paths, but many other modules (including pathlib) support only string paths. |
There are other modules with support for bytes filenames in their API: bz2 Also, given the fact that sndhdr supports them and its purpose is similar with imghdr, it would be a surprise |
See also a discussion at Python-Dev: Looks as there are no need to add bytes path support in such high-level API. In any case you can call os.fsdecode() on path argument. |
Right, I've read the thread you posted and I agree. My use cases aren't strong enough and it's enough for me to call os.fsdecode. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: