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
Title: mimetypes.guess_all_extensions potentially mutates list
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: lukasz.langa, miss-islington, rmccampbell7, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-12-12 19:06 by rmccampbell7, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28286 merged serhiy.storchaka, 2021-09-11 07:59
PR 28289 merged miss-islington, 2021-09-11 14:44
PR 28290 merged miss-islington, 2021-09-11 14:44
Messages (6)
msg331715 - (view) Author: Ryan McCampbell (rmccampbell7) Date: 2018-12-12 19:06
The mimetypes.guess_all_extensions function is defined as:

def guess_all_extensions(self, type, strict=True):
    type = type.lower()
    extensions = self.types_map_inv[True].get(type, [])
    if not strict:
        for ext in self.types_map_inv[False].get(type, []):
            if ext not in extensions:
                extensions.append(ext)
    return extensions

If any mime type exists in both the strict and non-strict types_map_inv and it is called with strict=False, then it will modify the strict list in-place which effects future calls even with strict=True. While this doesn't manifest as an error for me because the dictionaries are non-overlapping, it is a potential error; it is also vulnerable to people accidentally modifying the returned list. The list should be copied after the first lookup.
msg401632 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-11 08:01
Also, mutating the result of guess_all_extensions() mutated the internal state of the MimeTypes object and affected the following calls of guess_all_extensions().
msg401640 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-11 14:44
New changeset 97ea18ecede8bfd33d5ab2dd0e7e2aada2051111 by Serhiy Storchaka in branch 'main':
bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list (GH-28286)
https://github.com/python/cpython/commit/97ea18ecede8bfd33d5ab2dd0e7e2aada2051111
msg401717 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-13 17:24
New changeset 9cd8fb8d6356c17dafa1be727cab3d438f6df53f by Miss Islington (bot) in branch '3.9':
bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list (GH-28286) (GH-28290)
https://github.com/python/cpython/commit/9cd8fb8d6356c17dafa1be727cab3d438f6df53f
msg401718 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-13 17:24
New changeset 06c26f4d2909eae196ac81c9ed9b41e747e42685 by Miss Islington (bot) in branch '3.10':
bpo-35474: Fix mimetypes.guess_all_extensions() potentially mutating list (GH-28286) (GH-28289)
https://github.com/python/cpython/commit/06c26f4d2909eae196ac81c9ed9b41e747e42685
msg401719 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-13 17:25
Thanks, Serhiy! ✨ 🍰 ✨
History
Date User Action Args
2022-04-11 14:59:09adminsetgithub: 79655
2021-09-13 17:25:10lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg401719

stage: patch review -> resolved
2021-09-13 17:24:33lukasz.langasetmessages: + msg401718
2021-09-13 17:24:14lukasz.langasetnosy: + lukasz.langa
messages: + msg401717
2021-09-11 14:44:55miss-islingtonsetpull_requests: + pull_request26706
2021-09-11 14:44:51miss-islingtonsetnosy: + miss-islington

pull_requests: + pull_request26705
stage: patch review
2021-09-11 14:44:47serhiy.storchakasetmessages: + msg401640
2021-09-11 08:01:24serhiy.storchakasetstage: patch review -> (no value)
messages: + msg401632
versions: + Python 3.9, Python 3.10, Python 3.11
2021-09-11 07:59:12serhiy.storchakasetkeywords: + patch
nosy: + serhiy.storchaka

pull_requests: + pull_request26702
stage: patch review
2018-12-12 19:06:57rmccampbell7create