TheAlgorithms / Python

All Algorithms implemented in Python
https://the-algorithms.com/
MIT License
183.96k stars 44.28k forks source link

mimetypes can't get demo.js type #11396

Closed WHG555 closed 3 months ago

WHG555 commented 4 months ago

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'
prkksh commented 4 months ago

@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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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