[Python-Dev] Add a new "locale" codec?
Victor Stinner
victor.stinner at haypocalc.com
Wed Feb 8 14:25:36 CET 2012
2012/2/8 Simon Cross <hodgestar+pythondev at gmail.com>:
> I think I'm -1 on a "locale" encoding because it refers to different
> actual encodings depending on where and when it's run, which seems
> surprising, and there's already a more explicit way to achieve the
> same effect.
The following code is just an example to explain how locale is
supposed to work, but the implementation is completly different:
encoding = locale.getpreferredencoding(False)
... execute some code ...
text = bytes.decode(encoding)
bytes = text.encode(encoding)
The current locale is process-wide: if a thread changes the locale,
all threads are affected. Some functions have to use the current
locale encoding, and not the locale encoding read at startup. Examples
with C functions: strerror(), strftime(), tzname, etc.
My codec implementation uses mbstowcs() and wcstombs() which don't
touch the current locale, but just use it. Said diffferently, the
locale codec would just give access to these functions.
> The documentation on .getpreferredencoding() says some scary things
> about needing to call .setlocale() sometimes but doesn't really say
> when or why.
locale.getpreferredencoding() always call setlocale() by default.
locale.getpreferredencoding(False) doesn't call setlocale().
setlocale() is not called on Windows or if locale.CODESET is not
available (it is available on FreeBSD, Mac OS X, Linux, etc.).
> Could any of those cases make "locale" do weird things because it doesn't call setlocale()?
Sorry, I don't understand what do you mean by "weird things". The
"locale" codec doesn't touch the locale.
More information about the Python-Dev
mailing list