Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(21)

Side by Side Diff: Lib/mimetypes.py

Issue 9291: mimetypes initialization fails on Windows because of non-Latin characters in registry
Patch Set: Created 7 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Guess the MIME type of a file. 1 """Guess the MIME type of a file.
2 2
3 This module defines two useful functions: 3 This module defines two useful functions:
4 4
5 guess_type(url, strict=1) -- guess the MIME type and encoding of a URL. 5 guess_type(url, strict=1) -- guess the MIME type and encoding of a URL.
6 6
7 guess_extension(type, strict=1) -- guess the extension for a given MIME type. 7 guess_extension(type, strict=1) -- guess the extension for a given MIME type.
8 8
9 It also contains the following, for tuning the behavior: 9 It also contains the following, for tuning the behavior:
10 10
11 Data: 11 Data:
12 12
13 knownfiles -- list of files to parse 13 knownfiles -- list of files to parse
14 inited -- flag set when init() has been called 14 inited -- flag set when init() has been called
15 suffix_map -- dictionary mapping suffixes to suffixes 15 suffix_map -- dictionary mapping suffixes to suffixes
16 encodings_map -- dictionary mapping suffixes to encodings 16 encodings_map -- dictionary mapping suffixes to encodings
17 types_map -- dictionary mapping suffixes to types 17 types_map -- dictionary mapping suffixes to types
18 18
19 Functions: 19 Functions:
20 20
21 init([files]) -- parse a list of files, default knownfiles (on Windows, the 21 init([files]) -- parse a list of files, default knownfiles (on Windows, the
22 default values are taken from the registry) 22 default values are taken from the registry)
23 read_mime_types(file) -- parse one file, return a dictionary or None 23 read_mime_types(file) -- parse one file, return a dictionary or None
24 """ 24 """
25 25
26 import os 26 import os
27 import sys 27 import sys
28 import posixpath 28 import posixpath
29 import urllib 29 import urllib
30 from itertools import count
30 try: 31 try:
31 import _winreg 32 import _winreg
32 except ImportError: 33 except ImportError:
33 _winreg = None 34 _winreg = None
34 35
35 __all__ = [ 36 __all__ = [
36 "guess_type","guess_extension","guess_all_extensions", 37 "guess_type","guess_extension","guess_all_extensions",
37 "add_type","read_mime_types","init" 38 "add_type","read_mime_types","init"
38 ] 39 ]
39 40
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 If strict is true, information will be added to 233 If strict is true, information will be added to
233 list of standard types, else to the list of non-standard 234 list of standard types, else to the list of non-standard
234 types. 235 types.
235 """ 236 """
236 237
237 # Windows only 238 # Windows only
238 if not _winreg: 239 if not _winreg:
239 return 240 return
240 241
241 def enum_types(mimedb): 242 def enum_types(mimedb):
242 i = 0 243 for i in count():
243 while True:
244 try: 244 try:
245 ctype = _winreg.EnumKey(mimedb, i) 245 yield _winreg.EnumKey(mimedb, i)
246 except EnvironmentError: 246 except EnvironmentError:
247 break 247 break
248 try:
249 ctype = ctype.encode(default_encoding) # omit in 3.x!
250 except UnicodeEncodeError:
251 pass
252 else:
253 yield ctype
254 i += 1
255 248
256 default_encoding = sys.getdefaultencoding() 249 default_encoding = sys.getdefaultencoding()
257 with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, 250 with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
258 r'MIME\Database\Content Type') as mimedb: 251 r'MIME\Database\Content Type') as mimedb:
259 for ctype in enum_types(mimedb): 252 for ctype in enum_types(mimedb):
260 try: 253 try:
261 with _winreg.OpenKey(mimedb, ctype) as key: 254 with _winreg.OpenKey(mimedb, ctype) as key:
262 suffix, datatype = _winreg.QueryValueEx(key, 255 suffix, datatype = _winreg.QueryValueEx(key,
263 'Extension') 256 'Extension')
264 except EnvironmentError: 257 except EnvironmentError:
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 extension = 1 575 extension = 1
583 for gtype in args: 576 for gtype in args:
584 if extension: 577 if extension:
585 guess = guess_extension(gtype, strict) 578 guess = guess_extension(gtype, strict)
586 if not guess: print "I don't know anything about type", gtype 579 if not guess: print "I don't know anything about type", gtype
587 else: print guess 580 else: print guess
588 else: 581 else:
589 guess, encoding = guess_type(gtype, strict) 582 guess, encoding = guess_type(gtype, strict)
590 if not guess: print "I don't know anything about type", gtype 583 if not guess: print "I don't know anything about type", gtype
591 else: print 'type:', guess, 'encoding:', encoding 584 else: print 'type:', guess, 'encoding:', encoding
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7