Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ def _guess_file_type(self, path, strict, splitext):
else:
encoding = None
ext = ext.lower()
types_map = self.types_map[True]
types_map = self.types_map[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types_map is populated using boolean indexing: self.types_map[strict][ext] = type with strict having a default argument of True.

So I am not sure making only this change makes the code more readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the change as proposed is arguably detrimental to understandability, overall.

If we were writing this from scratch in modern Python we'd probably use an enumeration and use it as the allowed values for the 'strict' keyword...which would optimally be named something like 'strictness' so that, eg, MimeTypes(strictness=STRICT) would read sensibly. But making that kind of change at this point is probably more than we want to do, though you could propose something.

Maybe just a more verbose comment where those two tuples are defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok this kind of explains why the True/False literals were used for indexing here. What about:

iff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index f3cdad66261..61afeca2b72 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -104,8 +104,9 @@ def add_type(self, type, ext, strict=True):
 
         if not type:
             return
-        self.types_map[strict][ext] = type
-        exts = self.types_map_inv[strict].setdefault(type, [])
+        map_index = 1 if strict else 0
+        self.types_map[map_index][ext] = type
+        exts = self.types_map_inv[map_index].setdefault(type, [])
         if ext not in exts:
             exts.append(ext)

?

if ext in types_map:
return types_map[ext], encoding
elif strict:
return None, encoding
types_map = self.types_map[False]
types_map = self.types_map[0]
if ext in types_map:
return types_map[ext], encoding
else:
Expand All @@ -211,9 +211,9 @@ def guess_all_extensions(self, type, strict=True):
but non-standard types.
"""
type = type.lower()
extensions = list(self.types_map_inv[True].get(type, []))
extensions = list(self.types_map_inv[1].get(type, []))
if not strict:
for ext in self.types_map_inv[False].get(type, []):
for ext in self.types_map_inv[0].get(type, []):
if ext not in extensions:
extensions.append(ext)
return extensions
Expand Down
Loading