Issue #5853: calling a function of the mimetypes module from several threads

at once could hit the recursion limit if the mimetypes database hadn't been
initialized before.
This commit is contained in:
Antoine Pitrou 2009-04-27 20:50:20 +00:00
parent cac7af6863
commit f2651e193c
2 changed files with 17 additions and 15 deletions

View File

@ -44,6 +44,7 @@ knownfiles = [
] ]
inited = False inited = False
_db = None
class MimeTypes: class MimeTypes:
@ -237,9 +238,9 @@ def guess_type(url, strict=True):
Optional `strict' argument when false adds a bunch of commonly found, but Optional `strict' argument when false adds a bunch of commonly found, but
non-standard types. non-standard types.
""" """
if not inited: if _db is None:
init() init()
return guess_type(url, strict) return _db.guess_type(url, strict)
def guess_all_extensions(type, strict=True): def guess_all_extensions(type, strict=True):
@ -255,9 +256,9 @@ def guess_all_extensions(type, strict=True):
Optional `strict' argument when false adds a bunch of commonly found, Optional `strict' argument when false adds a bunch of commonly found,
but non-standard types. but non-standard types.
""" """
if not inited: if _db is None:
init() init()
return guess_all_extensions(type, strict) return _db.guess_all_extensions(type, strict)
def guess_extension(type, strict=True): def guess_extension(type, strict=True):
"""Guess the extension for a file based on its MIME type. """Guess the extension for a file based on its MIME type.
@ -271,9 +272,9 @@ def guess_extension(type, strict=True):
Optional `strict' argument when false adds a bunch of commonly found, Optional `strict' argument when false adds a bunch of commonly found,
but non-standard types. but non-standard types.
""" """
if not inited: if _db is None:
init() init()
return guess_extension(type, strict) return _db.guess_extension(type, strict)
def add_type(type, ext, strict=True): def add_type(type, ext, strict=True):
"""Add a mapping between a type and an extension. """Add a mapping between a type and an extension.
@ -287,16 +288,15 @@ def add_type(type, ext, strict=True):
list of standard types, else to the list of non-standard list of standard types, else to the list of non-standard
types. types.
""" """
if not inited: if _db is None:
init() init()
return add_type(type, ext, strict) return _db.add_type(type, ext, strict)
def init(files=None): def init(files=None):
global guess_all_extensions, guess_extension, guess_type
global suffix_map, types_map, encodings_map, common_types global suffix_map, types_map, encodings_map, common_types
global add_type, inited global inited, _db
inited = True inited = True # so that MimeTypes.__init__() doesn't call us again
db = MimeTypes() db = MimeTypes()
if files is None: if files is None:
files = knownfiles files = knownfiles
@ -306,11 +306,9 @@ def init(files=None):
encodings_map = db.encodings_map encodings_map = db.encodings_map
suffix_map = db.suffix_map suffix_map = db.suffix_map
types_map = db.types_map[True] types_map = db.types_map[True]
guess_all_extensions = db.guess_all_extensions
guess_extension = db.guess_extension
guess_type = db.guess_type
add_type = db.add_type
common_types = db.types_map[False] common_types = db.types_map[False]
# Make the DB a global variable now that it is fully initialized
_db = db
def read_mime_types(file): def read_mime_types(file):

View File

@ -255,6 +255,10 @@ Core and Builtins
Library Library
------- -------
- Issue #5853: calling a function of the mimetypes module from several threads
at once could hit the recursion limit if the mimetypes database hadn't been
initialized before.
- Issue #5854: Updated __all__ to include some missing names and remove some - Issue #5854: Updated __all__ to include some missing names and remove some
names which should not be exported. names which should not be exported.