New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
imp.find_module() fails with a UnicodeDecodeError when called with non-ASCII search paths #48602
Comments
imp.find_module() seems to cause an UnicodeDecodeError when the path [1] Python 3.0rc2 (r30rc2:67141, Nov 7 2008, 11:43:46) [MSC v.1500 32 |
The example works correctly on Linux (py3k trunk). The problem is maybe |
Indeed. It happens when the filesystem encoding is not utf-8. I have several changes in my local workspace about this, which also deal |
How can I test it on Linux? |
Oh, I found sys.setfilesystemencoding("latin-1")! But even with that, |
Well, latin-1 can decode any arbitrary array of bytes, so of course it |
I can reproduce this problem on Windows Vista, fresh py3k sources. |
From my understanding (after tracing/debugging) problem lies in import.c Conversion done with decode_mbcs (unicodeobject.c:4244) what uses MultiByteToWideChar with codepage CP_ACP. Problem is: converting >>> repr('h\u00e4kkinen'.encode('mbcs'))
"b'hakkinen'" MSDN says (http://msdn.microsoft.com/en- Writing encoding function in opposite to PyUnicode_DecodeFSDefault with Changing all filenames to unicode (with possible usage of fileio instead |
At the sprint, Andrew Svetlov, Martin von Loewis and I looked into this As long as the import code (written in C) uses bytes in the filesystem Two possible solutions would be to switch to Brett's importlib, or to (In 2.x the same problem exists, but is perhaps less real because module We also discovered another problem, which I'll report separately: the |
See also bpo-8611. |
About the mbcs encoding: issue bpo-850997 proposes to make it more strict. |
I closed issue bpo-850997, mbcs is now really strict by default: >>> 'h\u00e4kkinen'.encode('mbcs')
UnicodeEncodeError: ...
>>> 'h\u00e4kkinen'.encode('mbcs', 'replace')
"b'hakkinen'" PyUnicode_EncodeFSDefault(), PyUnicode_DecodeFSDefault() and os.fsencode() use mbcs with strict error handler on Windows. On other OS, these functions use surrogateescape error handler, but mbcs only supports strict and replace (to encode, and strict and ignore to decode). |
I wrote a patch to fix this issue, see bpo-9425. |
dirname = 'A-Za-z\xc4\xd6\xdc\xe4\xf6\xfc\xdf' Traceback (most recent call last):
File "D:\temp\python bug\test.py", line 19, in <module>
file_object, file_path, description = imp.find_module(basename, [dirname])
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character |
@serg Asminog: What is your Python version? What is your locale encoding (print(sys.getfilesystemencoding())? What is your Windows version? |
print(sys.getfilesystemencoding())
print(os.name)
print(sys.version)
print(sys.version_info)
print(sys.platform) mbcs ----------- |
Also Traceback (most recent call last):
File "D:\temp\python bug\test.py", line 20, in <module>
file_object, file_path, description = imp.find_module(basename, [dirname])
ImportError: No module named mymodule with python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] |
Oops, it's not sys.getfilesystemencoding(), but locale.getpreferredencoding() which is interesting. Can you give me your locale encoding? |
cp1251 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: