Code Monkey home page Code Monkey logo

Comments (5)

prkksh avatar prkksh commented on June 15, 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)

from python.

WHG555 avatar WHG555 commented on June 15, 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?

from python.

WHG555 avatar WHG555 commented on June 15, 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

from python.

thejamesm avatar thejamesm commented on June 15, 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.

from python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.