Ticket #21517 (closed enhancement: fixed)
python25, python26: japanese locale errors
| Reported by: | null.atou@… | Owned by: | jwa@… |
|---|---|---|---|
| Priority: | Normal | Milestone: | |
| Component: | ports | Version: | 1.8.0 |
| Keywords: | Cc: | mcalhoun@… | |
| Port: | python26 python25 |
Description
When the python script eyeD3 execute under Japanese environment, 'LookupError: unknown encoding: x-mac-japanese' has occurred.
% __CF_USER_TEXT_ENCODING=$UID:0:0 eyeD3 hoge.mp3 <--- encoding is 'mac-roman'
(snip, no error but many Mojibake occur!)
% __CF_USER_TEXT_ENCODING=$UID:1:14 eyeD3 hoge.mp3 <--- In Japanese environment, __CF_USER_TEXT_ENCODING already set
(snip)
Uncaught exception: unknown encoding: x-mac-japanese
Traceback (most recent call last):
File "/opt/local/bin/eyeD3", line 1215, in <module>
retval = main();
File "/opt/local/bin/eyeD3", line 1192, in main
retval = app.handleFile(f);
File "/opt/local/bin/eyeD3", line 566, in handleFile
self.printTag(self.tag);
File "/opt/local/bin/eyeD3", line 937, in printTag
"replace"),
LookupError: unknown encoding: x-mac-japanese
This is because python26 (and also python25) don't look LANG env-var(ja_JP.UTF-8 in many case in Japan), but get an encoding name 'x-mac-japanese' from CoreFoundation CFStringGetSystemEncoding() and CFStringConvertEncodingToIANACharSetName() (see 'Lib/locale.py' and a source 'Modules/_localemodule.c'). Then, unfortunately, a python only knows codecs in the codec table http://docs.python.org/library/codecs#standard-encodings. In the table, there are no 'x-mac-japanese' or 'x-mac-trad-chinese' or 'x-mac-korean' etc... So a simple test is here:
% __CF_USER_TEXT_ENCODING=$UID:1:14 python -c 'import locale; print "getdefaultlocale is", locale.getdefaultlocale(), ", getpreferredencoding :", locale.getpreferredencoding();'
getdefaultlocale is (None, 'x-mac-japanese') , getpreferredencoding : x-mac-japanese
% __CF_USER_TEXT_ENCODING=$UID:1:14 /usr/bin/python -c 'import locale; print "getdefaultlocale is", locale.getdefaultlocale(), ", getpreferredencoding :", locale.getpreferredencoding();'
getdefaultlocale is ('ja_JP', 'UTF8') , getpreferredencoding : UTF-8
Yes, apple's python2.6.1 in Snow Leopard (and also apple's python2.5.1 in Leopard) looks not CF_... but LANG.
I referred to web pages (in Japanese) here and here, and made a simple patch arround locale problem. Under this patch, python26 looks LANG env-var and get well-known encoding 'UTF-8', so, no error, no mojibake is occurred in eyeD3:-)
By the way, there is no unknown encoding error in python31 because a similar change applies in Modules/_localemodule.c in version 3.1.


