[issue19567] mimetypes.init() raise unhandled excption in windows

2013-11-13 Thread R. David Murray

R. David Murray added the comment:

This is a duplicate of issue 9291.  Can you answer Victor's question over there 
for us?

--
nosy: +r.david.murray
resolution:  - duplicate
stage:  - committed/rejected
status: open - closed
superseder:  - mimetypes initialization fails on Windows because of non-Latin 
characters in registry

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19567
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue19567] mimetypes.init() raise unhandled excption in windows

2013-11-12 Thread adamhj

New submission from adamhj:

my system is windows 2k3 sp2, python version is 2.7.6

i found this bug when trying to install the newest setuptools


X:\xxxez_setup.py
Extracting in d:\docume~1\xxx\locals~1\temp\tmpcyxs8s
Now working in d:\docume~1\xxx\locals~1\temp\tmpcyxs8s\setuptools-1.3.2
Installing Setuptools
Traceback (most recent call last):
  File setup.py, line 17, in module
exec(init_file.read(), command_ns)
  File string, line 8, in module
  File 
d:\docume~1\xxx\locals~1\temp\tmpcyxs8s\setuptools-1.3.2\setuptools\__init__.py,
 line 11, in module
from setuptools.extension import Extension
  File 
d:\docume~1\xxx\locals~1\temp\tmpcyxs8s\setuptools-1.3.2\setuptools\extension.py,
 line 5, in module
from setuptools.dist import _get_unpatched
  File 
d:\docume~1\xxx\locals~1\temp\tmpcyxs8s\setuptools-1.3.2\setuptools\dist.py, 
line 15, in module
from setuptools.compat import numeric_types, basestring
  File 
d:\docume~1\xxx\locals~1\temp\tmpcyxs8s\setuptools-1.3.2\setuptools\compat.py,
 line 19, in module
from SimpleHTTPServer import SimpleHTTPRequestHandler
  File D:\Python27\lib\SimpleHTTPServer.py, line 27, in module
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  File D:\Python27\lib\SimpleHTTPServer.py, line 208, in 
SimpleHTTPRequestHandler
mimetypes.init() # try to read system mime.types
  File D:\Python27\lib\mimetypes.py, line 358, in init
db.read_windows_registry()
  File D:\Python27\lib\mimetypes.py, line 258, in read_windows_registry
for subkeyname in enum_types(hkcr):
  File D:\Python27\lib\mimetypes.py, line 249, in enum_types
ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 33: 
ordinal not in range(128)
Something went wrong during the installation.
See the error message above.


then i see into the code, the exception is raised in this function


def enum_types(mimedb):
i = 0
while True:
try:
ctype = _winreg.EnumKey(mimedb, i)
except EnvironmentError:
break
try:
ctype = ctype.encode(default_encoding) # omit in 3.x!
except UnicodeEncodeError:
pass
else:
yield ctype
i += 1


i checked my registry, there is an key in HKCR whose name is in 
Chinese(encoding GBK), which causes this problem(btw, the default_encoding is 
'ascii', assigned from sys.getdefaultencoding())

i don't know is it legal to use a non-ascii string as the name of a registry 
key in windows, but i think there is some problem in these piece of code. why 
the variable ctype need to be decoded here? i checked the _winreg.EnumKey() 
function, it returns byte string:

 _winreg.EnumKey(key,8911)
'\xbb\xad\xb0\xe5\xa1\xa3\xce\xc4\xb5\xb5'
#this is the problem key which cause the exception

so python tries to encode it(with ascii encoding) first, and then the exception 
is raised(and unhandled)

shouldn't we just remove the try..encode..except paragraph?

--
components: Library (Lib)
messages: 202731
nosy: adamhj
priority: normal
severity: normal
status: open
title: mimetypes.init() raise unhandled excption in windows
type: behavior
versions: Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19567
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com