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

Windows 10 Mimetypes for .webp return empty #128703

Open
ewwink opened this issue Jan 10, 2025 · 10 comments
Open

Windows 10 Mimetypes for .webp return empty #128703

ewwink opened this issue Jan 10, 2025 · 10 comments
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ewwink
Copy link

ewwink commented Jan 10, 2025

Bug report

Bug description:

on Windows 10 guess_file_type() checking .webp file return tuple('', None) not tuple('image/webp', None)

The problem is db.read_windows_registry() overwrite default types_map .webp = 'image/webp' into .webp = ''

cpython/Lib/mimetypes.py

Lines 390 to 393 in 65da5db

if files is None or _db is None:
db = MimeTypes()
# Quick return if not supported
db.read_windows_registry()

CPython versions tested on:

3.11, 3.12, 3.13

Operating systems tested on:

Windows

Linked PRs

@ewwink ewwink added the type-bug An unexpected behavior, bug, or error label Jan 10, 2025
@ewwink ewwink changed the title Windows 10 Mimetypes for .webp return empy Windows 10 Mimetypes for .webp return empty Jan 10, 2025
@ZeroIntensity ZeroIntensity added OS-windows stdlib Python modules in the Lib dir labels Jan 10, 2025
@zooba
Copy link
Member

zooba commented Jan 10, 2025

Can you check the value of HKEY_CLASSES_ROOT\.webp's ContentType value on your machine?

mimetypes intentionally prefers the system database, so if you've installed something that modified your system, then you would expect to see the changes.

FWIW, my own machines specify image/webp in that value.

@ewwink
Copy link
Author

ewwink commented Jan 10, 2025

it has ContentType and Content Type (with space)
image

for other extensions it use Content Type (with space)

image

@zooba
Copy link
Member

zooba commented Jan 10, 2025

Right, so something has registered the type incorrectly. There isn't really any easy way to find out who did it, but deleting the empty Content Type entry and renaming the ContentType one should fix your system.

@ewwink
Copy link
Author

ewwink commented Jan 10, 2025

yes that should fix the problem, but how if the extension has no Content Type value, for example the .ras

image

or even it has no Content Type

image

wouldn't it be better to check for empty value, if yes then do not overwrite the original value of types_map = _types_map_default = {...

@ewwink
Copy link
Author

ewwink commented Jan 12, 2025

Python 3.12 on Ubuntu (fresh install)

# python -V
Python 3.12.8
# cat /usr/lib/python3.12/mimetypes.py | grep webp
        '.webp': 'image/webp',
# python
>>> import mimetypes
>>> mimetypes.guess_type('picture.jpg')
('image/jpeg', None)
>>> mimetypes.guess_type('picture.webp')
(None, None)

update:
3.10: no support for .webp
3.11, 3.12: need strict=False mimetypes.guess_type('picture.webp', strict=False)
3.13: work without additional arg

Quick Fix for Python 3.12 or lower, locate the original path of mimetypes.py and replace with version 3.13

# wget -O /usr/lib/pythonX.XX/mimetypes.py https://raw.githubusercontent.com/python/cpython/refs/heads/3.13/Lib/mimetypes.py

@zooba
Copy link
Member

zooba commented Jan 14, 2025

Ignoring empty values is a reasonable idea. PRs welcome from anyone (there's both Python and C versions of this code, IIRC).

@rruuaanng
Copy link
Contributor

I will take a look at it later. If it's allowed, please leave it for me.
(That is, after reading the registry, check whether the read Content Type field is an empty string. I hope I didn't misunderstand)

@ewwink
Copy link
Author

ewwink commented Jan 14, 2025

I will take a look at it later. If it's allowed, please leave it for me. (That is, after reading the registry, check whether the read Content Type field is an empty string. I hope I didn't misunderstand)

yes that's right, problematic code

self.types_map[strict][ext] = type

I replace with

        if not type and ext in self.types_map[strict]:
            type = self.types_map[strict][ext]
        self.types_map[strict][ext] = type

do you think it is good idea to also update another version?
3.10: Add support for .webp and other extension like in 3.13 + check empty value
3.11, 3.12: Remove strict=False requirement + check empty value
3.13: only check empty value

@rruuaanng
Copy link
Contributor

only check empty value

I think this is all I need, we should fix the bug first and then make some improvements to it later, but this requires more discussion.

@rruuaanng
Copy link
Contributor

@ewwink Thank you for reporting the issue, and I appreciate the time you spent completing the code. To be precise, this is your achievement. So, thank you for giving me the opportunity :)

@zooba zooba added 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants