Skip to content
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

For Python 3.13: A drop-in replacement for imghdr.what() #72

Closed
cclauss opened this issue May 16, 2024 · 4 comments
Closed

For Python 3.13: A drop-in replacement for imghdr.what() #72

cclauss opened this issue May 16, 2024 · 4 comments

Comments

@cclauss
Copy link
Contributor

cclauss commented May 16, 2024

Given the discussion in #67 about imghdr being removed from the Python Standard Library, it might be quite helpful to have a drop-in replacement for imghdr.what(). It would provide a smooth transition to Py3.13 if developers could confidently replace all instances of imghdr.what() with puremagic.what() -- same args, same results.

@NebularNerd
Copy link
Contributor

Oddly I was thinking of this the other day, I've not used imghdr does it literally just return an extension?

@cclauss
Copy link
Contributor Author

cclauss commented May 17, 2024

% python3.12 -c"import imghdr ; print(imghdr.what(None, b'\xff\xd8\xff\xdb'))"

<string>:1: DeprecationWarning: 'imghdr' is deprecated and slated for removal in Python 3.13
jpeg

Source code: https://github.com/python/cpython/blob/3.12/Lib/imghdr.py

% python3.12

>>> import imghdr
<stdin>:1: DeprecationWarning: 'imghdr' is deprecated and slated for removal in Python 3.13
>>> ", ".join(sorted(test_func.__name__[5:] for test_func in imghdr.tests))
'bmp, exr, gif, jpeg, pbm, pgm, png, ppm, rast, rgb, tiff, webp, xbm'

@NebularNerd
Copy link
Contributor

That's super basic, at a guess we could go with a very basic shim:

import puremagic as imghdr

Add a def for .what and just return the highest confidence extension minus the . from the database. That would provide a very basic drop-in replacment with the added power of the larger database. Folks could then transition to a proper PureMagic implementation.

@cdgriffith cdgriffith mentioned this issue Jun 16, 2024
cdgriffith added a commit that referenced this issue Jun 16, 2024
- Adding #72 #75 #76 #81 `.what()` to be a drop in replacement for `imghdr.what()` (thanks to Christian Clauss and Andy - NebularNerd)
- Adding #67 Test on Python 3.13 beta (thanks to Christian Clauss)
- Adding #77 from __future__ import annotations (thanks to Christian Clauss
- Fixing #66 Confidence sorting (thanks to Andy - NebularNerd)

---------

Co-authored-by: Andy <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
@cdgriffith
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants