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

mimetypes can't get demo.js type #11396

Closed
WHG555 opened this issue May 8, 2024 · 5 comments
Closed

mimetypes can't get demo.js type #11396

WHG555 opened this issue May 8, 2024 · 5 comments
Labels

Comments

@WHG555
Copy link

WHG555 commented May 8, 2024

Repository commit

none

Python version (python --version)

Python 3.12.3

Dependencies version (pip freeze)

none

Expected behavior

Python 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:20:11) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimetypes
>>> type, _ = mimetypes.guess_type('example.js')
>>> type
'text/javascript'

Actual behavior

Python 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:20:11) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimetypes
>>> type, _ = mimetypes.guess_type('example.js')
>>> type
'text/plain'
@WHG555 WHG555 added the bug label May 8, 2024
@prkksh
Copy link

prkksh commented May 10, 2024

@WHG555 I'm thinking this was fixed in newer versions of python3. You should check this doc: https://www.iana.org/assignments/media-types/media-types.xhtml

I tried the guess_type function in python 3.8.2

import mimetypes
import sys
print(sys.version)
mimetypes.init()
type = mimetypes.guess_type('example.js')
print(type)

output:

3.8.2 (default, Mar 13 2020, 10:14:16)
[GCC 9.3.0]
('application/javascript', None)

@WHG555
Copy link
Author

WHG555 commented May 12, 2024

I'm using version 3.12 and I'm still having this issue. It's on Windows, it will only appear.
Isn't 3.12 new to 3.8?

@WHG555
Copy link
Author

WHG555 commented May 12, 2024

Windows 10 1903

Python 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:20:11) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimetypes
>>> extension_map = mimetypes.guess_all_extensions('text/plain')
>>> extension_map
['.txt', '.bat', '.c', '.h', '.ksh', '.pl', '.srt', '.asm', '.c++', '.cc', '.cd', '.cod', '.cp', 
'.cpp', '.cs', '.csh', '.cshader', '.csproj', '.cxx', '.def', '.dsh', '.dshader', '.dsp', '.dsw', 
'.efu', '.filters', '.fx', '.gitattributes', '.gitignore', '.gitmodules', '.gsh', '.gshader', '.h++', 
'.hh', '.hlsl', '.hlsli', '.hpp', '.hsh', '.hshader', '.hxx', '.i', '.idl', '.inc', '.inl', '.ipp', 
'.js', '.jsproj', '.jsx', '.jsxbin', '.jsxinc', '.lst', '.mak', '.map', '.mdp', '.mk', '.odh',  
'.odl', '.pkgdef', '.pkgundef', '.pri', '.pro', '.psh', '.pshader', '.py', '.pyw', '.qbs', '.qml', 
'.qs', '.rc', '.rc2', '.rct', '.res', '.rgs', '.s', '.sln', '.sol', '.sor', '.srf', '.tlh', '.tli', 
'.ts', '.tsx', '.tt', '.ui', '.user', '.vb', '.vbproj', '.vcp', '.vcw', '.vsh', '.vshader']
>>> extension_map = mimetypes.guess_all_extensions('application/javascript')
>>> extension_map
[]

ubuntu

smile@pc:~$ python3
Python 3.6.9 (default, Mar 10 2023, 16:46:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimetypes
>>> extension_map = mimetypes.guess_all_extensions('text/plain')
>>> extension_map
['.bat', '.c', '.h', '.ksh', '.pl', '.txt', '.asc', '.text', '.pot', '.brf', '.srt']
>>> extension_map = mimetypes.guess_all_extensions('application/javascript')
>>> extension_map
['.js', '.mjs']
>>> exit()

smile@pc:~$ uname -a
Linux pc 5.4.0-150-generic #167~18.04.1-Ubuntu SMP Wed May 24 00:51:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

smile@pc:~$ lsb_release  -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.6 LTS
Release:        18.04
Codename:       bionic

.js is "text/plain" in Windows
and
"application/javascript" in Linux

@thejamesm
Copy link

thejamesm commented May 14, 2024

This is not the repo for Python; it's the Python section of a project called "The Algorithms". You can find the Python repo at: python/cpython

That aside, I should note that application/javascript is deprecated (as are many other variants) in favour of text/javascript. Your Windows install seems to be giving odd results; my Windows install (also 3.12.3) behaves as expected:

Python 3.12.3 (tags/v3.12.3:f6650f9, Apr  9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimetypes
>>> mimetypes.guess_all_extensions('text/plain')
['.txt', '.bat', '.c', '.h', '.ksh', '.pl', '.srt', '.cd', '.cs', '.csproj', '.efu', '.gitattributes', '.gitignore', '.gitmodules', '.m', '.pkgdef', '.pkgundef', '.resjson', '.sln', '.ts', '.tsx', '.tt', '.user', '.vb', '.vbproj', '.vspscc', '.vsscc', '.vssscc', '.XOML']
>>> mimetypes.guess_all_extensions('text/javascript')
['.js', '.mjs']
>>> mimetypes.guess_all_extensions('application/javascript')
[]
>>> mimetypes.guess_all_extensions('application/x-javascript')
[]

Having a quick glance over the source code, I notice two things.

First, there seems to be different logic for the instance method guess_all_extensions(type) (here) as for the bare function of the same name (here). Do you get different results if you instantiate a MimeTypes object and call that method? Like this:

import mimetypes
mt = mimetypes.MimeTypes()
mt.guess_all_extensions('text/plain')
# ['.txt', '.bat', '.c', '.h', '.ksh', '.pl', '.srt']
mt.guess_all_extensions('text/javascript')
# ['.js', '.mjs']

# Does this give you the MIME type you're expecting?
type, _ = mt.guess_type('example.js')

As you can see, I get different results for text/plain.

Second, it appears that the MIME types are in some situations read from the Windows registry: 1 2 3

I would take that, combined with my differing results, to suggest that what you're experiencing an issue with your Windows configuration rather than Python itself. I confess I don't know how Windows populates its MIME type database, but hopefully that's a pointer for where you should look next.

@tianyizheng02
Copy link
Contributor

Closing because this has nothing to do with the Python Algorithms repo.

@tianyizheng02 tianyizheng02 closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants