[issue34938] Fix mimetype.init() to account for from import

YoSTEALTH report at bugs.python.org
Mon Oct 8 18:13:09 EDT 2018


New submission from YoSTEALTH <ritesh at stealthcentral.com>:

When a user uses from import, there is a flaw in how mimetype.init() updates its global references.

# Option-1 (flawed)
# -----------------
from mimetypes import init, types_map

print(types_map.get('.gz'))  # None
init()  # <- initialize
print(types_map.get('.gz'))  # None

# Option-2
# --------
import mimetypes

print(mimetypes.types_map.get('.gz'))  # None
mimetypes.init()  # <- initialize
print(mimetypes.types_map.get('.gz'))  # application/gzip

As you can see in https://github.com/python/cpython/blob/master/Lib/mimetypes.py#L344 line:358 global reference is reassigned and thus it prevents `from mimetype import types_map` from being updated and using old `types_map` reference.

Potential solution would be to `types_map.update(new dict content)` vs reassigning the variable.

----------
messages: 327375
nosy: YoSTEALTH
priority: normal
severity: normal
status: open
title: Fix mimetype.init() to account for from import
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34938>
_______________________________________


More information about the Python-bugs-list mailing list