This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
标题: Add preferred extensions for MIME types
类型: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
状态: closed Resolution: fixed
Dependencies: 后续:
分配给: 抄送列表: The Compiler, Tom.Christie, ajaksu2, cvrebert, david.lindquist, elesbom, eric.araujo, evanj, ezio.melotti, iritkatriel, jlgijsbers, kxroberto, lambacck, leos, martin.panter, ptarjan, sandro.tosi, sascha_silbe, wichert
优先级: normal 关键字: patch

Created on 2004-10-08 15:44 by kxroberto, last changed 2022-04-11 14:56 by admin. This issue is now closed.

文件
文件名 上传时间 Description 编辑
issue1043134.patch elesbom, 2010-11-21 17:09 review
mimetypes.patch david.lindquist, 2014-03-01 18:22 review
Messages (21)
msg54278 - (view) Author: kxroberto (kxroberto) 日期: 2004-10-08 15:44
Instead of returning the first in the list of
extensions it should return the most reasonable . here:
to have a *.txt on disk after saveing?

msg54279 - (view) Author: Johannes Gijsbers (jlgijsbers) * (Python triager) 日期: 2004-10-09 15:26
Logged In: YES 
user_id=469548

How would you suggest finding out what the most reasonable
extension for a mime type is?
msg54280 - (view) Author: kxroberto (kxroberto) 日期: 2004-10-10 08:44
Logged In: YES 
user_id=972995

in mimetypes.py there is already a

common_types = {
    '.jpg' : 'image/jpg',
...

.txt could be added,
mayby guess_extension should first reverse-take it out of
there, not random ...?

background: my intent was to save MIME attachment as
(startable) temporary file. yet got wonderful .ksh's for
textfiles, and had to fumble ...

 
msg54281 - (view) Author: Johannes Gijsbers (jlgijsbers) * (Python triager) 日期: 2004-10-11 20:15
Logged In: YES 
user_id=469548

common_types is for adding some non-standard types, not for
determining which extension is most reasonable. I'll be
happy to look at a decent patch, but I'm moving this to
feature request until then.
msg54282 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) 日期: 2004-12-19 00:44
Logged In: YES 
user_id=341410

While I agree with the original poster that returning '.txt'
is preferable to the others in the list returned by
mimetypes.guess_all_extensions() at least 9 times out of 10,
being able to prioritize all of the types is not necessarily
the easiest thing to do for all of the possible returned lists.

Is using a custom comparison function along with the list
returned by guess_all_extensions() sufficient?
msg82101 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) 日期: 2009-02-14 18:21
Confirmed on trunk.
msg114379 - (view) Author: Mark Lawrence (BreamoreBoy) * 日期: 2010-08-19 16:51
I'll close this in a couple of weeks unless someone wants it kept open.
msg114461 - (view) Author: Stefan Krah (skrah) * (Python committer) 日期: 2010-08-20 21:50
I think you are closing too aggressively.


Python 3.2a0 (py3k:81783, Jun  6 2010, 16:07:26) 
[GCC 4.1.3 20080623 (prerelease) (Ubuntu 4.1.2-23ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mimetypes
>>> mimetypes.guess_extension('text/plain')
'.ksh'
>>>
msg121798 - (view) Author: Chris Lambacher (lambacck) * 日期: 2010-11-20 22:25
While I agree that getting .ksh is an unfortunate guess, I am not sure how you can guess in the face of many options (especially when the those options are parsed out of a mimetypes file or the windows registry). 

Perhaps there should be a "resonable_defaults" map that is checked first for very basic types where there are multiple extensions for a type?
msg121867 - (view) Author: Paul Tarjan (ptarjan) 日期: 2010-11-21 05:41
6 years old and still not fixed?

http://www.stdicon.com/mimetype/text/plain

Please return txt
msg121953 - (view) Author: Rafael dos Santos Gonçalves (elesbom) 日期: 2010-11-21 17:09
ksh is a text/plain to, all this extension are text/plain:
'.ksh', '.pl', '.bat', '.h', '.c', '.txt', '.asc', '.text', '.pot', '.brf'.
The problem is: the code return the first of list:
return extensions[0]
So, I add one boolean parameter in method guess_extension called all_exts. Putting True in this parameter the method returns a tuple with all possible extensions.

I hope helped
msg121966 - (view) Author: Chris Lambacher (lambacck) * 日期: 2010-11-21 19:18
Rafael,

There is already a method which returns all the extensions. What is required is a flag (or separate dict) which provides a canonical extension. The questions is whether it is sufficient to rely on the default provided mimetypes for the default in the face of mimetypes read out of the mimetypes files or windows registry.

I don't see a way to fix the bug, without also providing an API to "pick the winner" for those cases that are not provided in the default list.
msg140264 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) 日期: 2011-07-13 15:06
The proposed patch does not solve the issue.  In the current API, there is no way to do it, so this bug requires a new feature.  I think it would involve a new dict, like preferred_extensions, which would be seeded with default values, like .jpg for image/jpeg and .txt for text/plain, and a few functions/methods to query the dict or add items.
msg143665 - (view) Author: Leo Shklovskii (leos) 日期: 2011-09-07 08:20
I'm running into a similar issue with this function. My bug is that get_type('foo.png') returns image/x-png. This occurs on windows because there are mappings to both image/png and image/x-png in the registry (as there should be, since that key is actually a reverse mapping) and the code simply picks the first key that it enumerates over. This issue strikes in both directions.

Chris and others bring up a valid issue: how to decide what the winning result is?

I think the answer is pretty clear - you use the common_types mapping already in the file and expand it as appropriate. If the mimetype can't be found, only then do you go to the windows registry. The behavior on Linux is even stranger to me (now we'll dig through an arbitrary list of files that might contain MIME info or may have completely irrelevant data) but it's a pragmatic solution.

If someone needs to customize what guess_type returns, they can simply wrap the guess_type function in their own code or monkey patch if they don't have access to the source they're running. Changing such a mime type is a really advanced and unusual operation. If that's unacceptable, the code can provide a hook for an 'apache MIME config' file on windows in a standard place (either pythonpath, or %system% or wherever) that it will check before going to common_types or to the registry.

Making this change doesn't require changing the API at all, just the implementation changes.
msg212518 - (view) Author: David Lindquist (david.lindquist) * 日期: 2014-03-01 18:22
I don't think it is unreasonable to return a well-known extension for certain mime types, text/plain being the most obvious (and most in need of repair; .ksh??).

I've attached a patch based on the previous discussion.
msg214951 - (view) Author: Wichert Akkerman (wichert) 日期: 2014-03-27 13:06
Here is a related question on SO: http://stackoverflow.com/questions/352837/how-to-add-file-extensions-based-on-file-type-on-linux-unix
msg215571 - (view) Author: David Lindquist (david.lindquist) * 日期: 2014-04-04 22:02
Anyone interested in picking this up, or at least commenting on the approach I suggested in the patch? Seems like an easy fix for a long-standing bug.
msg226466 - (view) Author: Martin Panter (martin.panter) * (Python committer) 日期: 2014-09-06 02:39
See also <https://bugs.python.org/issue6626#msg91205>, which mentions using a list of tuples instead of a dictionary, which sounds like it might help with this issue. Doing it that way you might be able avoid some duplication in the lists.
msg277024 - (view) Author: Tom Christie (Tom.Christie) 日期: 2016-09-20 12:19
Confirming that I've also bumped into this for Python 3.5.

A docs update would seem to be the lowest-cost option to start with.

Right now `mimetypes.guess_extension()` isn't terribly useful, and it'd be better to at least know that upfront.
msg384346 - (view) Author: Florian Bruhin (The Compiler) * 日期: 2021-01-04 20:21
I think this has been fixed in Python 3.7+ via https://github.com/python/cpython/pull/14375 - at least for a couple of types.

Comparing Python 3.6 with the current state, the following changed (which can be used as an "override" dict before calling mimetypes.guess_extension):

    "application/manifest+json": ".webmanifest",  # not None
    "application/octet-stream": ".bin",  # not .a
    "application/postscript": ".ps",  # not .ai
    "application/vnd.ms-excel": ".xls",  # not .xlb
    "application/vnd.ms-powerpoint": ".ppt",  # not .pot
    "application/wasm": ".wasm",  # not None
    "application/x-hdf5": ".h5",  # not None
    "application/xml": ".xsl",  # not .rdf
    "audio/mpeg": ".mp3",  # not .mp2
    "image/jpeg": ".jpg",  # not .jpe
    "image/tiff": ".tiff",  # not .tif
    "text/html": ".html",  # not .htm
    "text/plain": ".txt",  # not .bat
    "video/mpeg": ".mpeg",  # not .m1v
msg410859 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) 日期: 2022-01-18 13:02
PR14375 indeed adds a test for this as well (test_preferred_extension).
历史
日期 用户 动作 参数
2022-04-11 14:56:07admin修改github: 40993
2022-01-18 13:02:58iritkatriel修改状态: open -> closed

versions: + Python 3.8, - Python 3.5
抄送: + iritkatriel

消息: + msg410859
resolution: fixed
stage: patch review -> resolved
2021-01-04 20:21:06The Compiler修改抄送: + The Compiler
消息: + msg384346
2019-05-02 04:17:48josiahcarlson修改抄送: - josiahcarlson
2018-07-08 13:08:22sascha_silbe修改抄送: + sascha_silbe
2017-03-16 04:25:15martin.panter链接issue29823 superseder
2016-09-20 12:19:02Tom.Christie修改抄送: + Tom.Christie
消息: + msg277024
2014-09-06 02:39:08martin.panter修改抄送: + martin.panter
消息: + msg226466
2014-06-19 22:20:15ezio.melotti修改stage: test needed -> patch review
versions: + Python 3.5, - Python 3.4
2014-05-16 05:16:59cvrebert修改抄送: + cvrebert
2014-05-13 22:13:11skrah修改抄送: - skrah
2014-04-04 22:02:49david.lindquist修改消息: + msg215571
2014-03-27 13:06:28wichert修改抄送: + wichert
消息: + msg214951
2014-03-01 18:22:42david.lindquist修改文件: + mimetypes.patch

抄送: + david.lindquist
消息: + msg212518

keywords: + patch
2014-02-03 19:07:15BreamoreBoy修改抄送: - BreamoreBoy
2012-10-11 11:52:01ezio.melotti修改抄送: + ezio.melotti

versions: + Python 3.4, - Python 2.7, Python 3.3
2012-10-10 15:37:11evanj修改抄送: + evanj
2011-09-07 08:20:06leos修改抄送: + leos

消息: + msg143665
versions: + Python 2.7
2011-08-20 22:40:11sandro.tosi修改抄送: + sandro.tosi
2011-07-13 15:06:30eric.araujo修改keywords: - patch, easy

消息: + msg140264
标题: mimetypes.guess_extension('text/plain') == '.ksh' ??? -> Add preferred extensions for MIME types
2011-03-09 03:27:09terry.reedy修改抄送: jlgijsbers, josiahcarlson, kxroberto, ajaksu2, eric.araujo, lambacck, skrah, ptarjan, BreamoreBoy, elesbom
versions: + Python 3.3, - Python 3.1, Python 2.7, Python 3.2
2010-11-21 19:18:33lambacck修改消息: + msg121966
2010-11-21 17:09:17elesbom修改文件: + issue1043134.patch

抄送: + elesbom
消息: + msg121953

keywords: + patch
2010-11-21 05:41:08ptarjan修改抄送: + ptarjan
消息: + msg121867
2010-11-21 01:15:53eric.araujo链接issue6799 superseder
2010-11-20 22:30:48eric.araujo修改抄送: + eric.araujo
2010-11-20 22:25:44lambacck修改抄送: + lambacck
消息: + msg121798
2010-08-20 21:50:29skrah修改状态: pending -> open
versions: + Python 3.1, Python 3.2
抄送: + skrah

消息: + msg114461
2010-08-19 16:51:17BreamoreBoy修改状态: open -> pending
抄送: + BreamoreBoy
消息: + msg114379

2009-04-22 16:04:08ajaksu2修改keywords: + easy
2009-02-14 18:21:36ajaksu2修改抄送: + ajaksu2
stage: test needed
消息: + msg82101
versions: + Python 2.7
2004-10-08 15:44:17kxroberto创建