PyS*_*ter 5 python windows unicode python-3.x
在Windows 7中使用python 3.2我在IDLE中得到以下内容:
>>compile('pass', r'c:\temp\??\module1.py', 'exec')
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么编译语句尝试使用mbcs转换unicode文件名?我知道sys.getfilesystemencoding在Windows中返回'mbcs',但我认为在提供unicode文件名时不会使用它.
例如:
f = open(r'c:\temp\??\module1.py')
Run Code Online (Sandbox Code Playgroud)
作品.
要进行更完整的测试,请在utf8编码文件中保存以下内容,并使用标准python.exe 3.2版运行它
# -*- coding: utf8 -*-
fname = r'c:\temp\??\module1.py'
# I do have the a file named fname but you can comment out the following two lines
f = open(fname)
print('ok')
cmp = compile('pass', fname, 'exec')
print(cmp)
Run Code Online (Sandbox Code Playgroud)
输出:
ok
Traceback (most recent call last):
File "module8.py", line 6, in <module>
cmp = compile('pass', fname, 'exec')
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: inval
id character
Run Code Online (Sandbox Code Playgroud)
从Python问题10114开始,似乎逻辑是Python使用的所有文件名对于使用它们的平台应该是有效的.它使用文件系统编码进行编码,以便在Python的C内部使用.
我同意它可能不应该在Windows上抛出错误,因为任何Unicode文件名都是有效的.您可能希望使用Python提交错误报告.但要注意,必要的更改可能并不简单,因为任何使用文件名的C代码如果无法编码都必须要做.
| 归档时间: |
|
| 查看次数: |
5953 次 |
| 最近记录: |