* See [[Talk:Features/PythonEncodingUsesSystemLocale]] <!-- This adds a link to the "discussion" tab associated with your page. This provides the ability to have ongoing comments or conversation without bogging down the main feature page -->

* See [[Talk:Features/PythonEncodingUsesSystemLocale]] <!-- This adds a link to the "discussion" tab associated with your page. This provides the ability to have ongoing comments or conversation without bogging down the main feature page -->

+

The disadvantage, such as it is, of this is that it means python2 scripts and programs written on Fedora can work fine here but fail with UnicodeErrors when trying to run it on a non-Fedora machine. Note that upstream python has refused to make this change. AFAIK, we'd be the first to make this change (although some of the python-dev posters propose that the distributions should change on their own; while others say that would be wrong).

+

+

One thing I don't like about this is that we lose consistency with upstream and the other distributions without even gaining consitstency between our python3 and python2 handling. python3 hardcodes utf8 for the system locale. This feature would make the encoding dependent on locale everywhere (unless overridden by a third party module like pango). This means that porting code from python2 to python3 would be transitioning from a locale-dependent encoding to hardcoded utf8 which could lead to bugs that no one outside of Fedora has experience with. I'd rather that we either change python2 to hardcode utf8 like python3 does or also change python3 to use the locale.

It is proposed to change the first conditional to if 1: in our CPython 2 build, so that Fedora's Python by default reads the locale from the environment and uses that encoding. This will generally mean UTF-8 is used, rather than ascii.

Background

CPython's "default encoding"

The C implementation of Python 2 has two ways it can represent text strings:

the classic legacy str object in which each character is represented as a single byte in an undefined character set. This is represented internally as a struct PyStringObject

unicode objects where each character is represented as either 16-bit or 32-bit word in the Unicode character set (UCS). This is represented internally as a struct PyUnicodeObject. We use UCS4 (32-bit) in Fedora's builds of Python.

Python 2 will encode and decode between unicode objects and str objects based on what Python believes the character set and character encoding are for the str object.

CPython 2's implementation has an internal read-only variable called unicode_default_encoding which is returned by sys.getdefaultencoding() (for brevity sake I'm going to refer to this variable as default_encoding). Whenever Python passes a string to an external API or receives a string from an external API, e.g. any string ultimately passed to a C function and the C binding has not explicitly specified its encode/decode requirements then Python consults the unicode_default_encoding variable to decide how to encode/decode that string. That means any time you print a string, open a file, call a function in a CPython binding it is subject to the default encoding.

(In Python 3, the str object became a struct PyUnicodeObject, and struct PyStringObject became a bytes object)

The unicode_default_encoding is set in site.py to ascii for historical reasons. Then site.py makes the default_encoding read-only by removing it from the sys module name space. This means you cannot call sys.setdefaultencoding() without generating an exception. This also means Python's default encoding is locked to ascii.

The reason for this appears to be an optimization within CPython: at the C level a struct PyUnicodeObject actually carries two copies of the string:

its UCS-{2,4} representation (this is the Py_UNICODE *str field), and

its encoded representation after encoding it according to the value in the global unicode_default_encoding variable; this is the PyObject *defenc field.

Think of this as a cached value of the string in the default encoding. The first time a unicode object is subject to encode/decode it caches the encoded value of the string to avoid having to encode/decode every time the unicode object needs to accessed in its encoded form. This cached value is invalidated when the unicode string content changes but there is no mechanism to invalidate it when the default encoding changes (hence, I believe, the restrictions on changing the default encoding, and the possibility that any struct PyUnicodeObject instances created prior to the modification of the default encoding may exhibit incorrect behavior with respect to encoding).

In Python 3, the default value of unicode_default_encoding is "utf-8" (this has been in the py3k branch of CPython's implementation since revision 55108); we do not plan to touch site.py for python3.

The system locale's encoding

In Fedora there is the notion of the "locale", embodying various localization parameters for the whole operating system. From the perspective of the "operating system locale", there is an "encoding", separate to that of the CPython runtime. From this perspective of operating system locale, our default encoding is UTF-8. This is normally set via login scripts in /etc/profile.d. The user if they wish may choose to override the system default.
In both instances the default language and encoding is exported via an environment variable:

[david@brick ~]$ echo $LANG
en_US.utf8

It's possible to query this locale information from Python using the locale module:

PyGTK and Pango

A significant "gotcha" here is that the pango Python module forces the global default encoding variable to be 'utf-8'. It can do this because it's implemented in CPython where there are no restrictions; it directly calls PyUnicode_SetDefaultEncoding

/* set the default python encoding to utf-8 */
PyUnicode_SetDefaultEncoding("utf-8");

Let's take a little test drive and see things in action for ourselves:

/* Make sure the encoding is valid. As side effect, this also
loads the encoding into the codec registry cache. */
v = _PyCodec_Lookup(encoding);

then copies the encoding into the buffer: "unicode_default_encoding"; this buffer supplies the return value for PyUnicode_GetDefaultEncoding(), which is used in many places inside the unicode implementation, plus in bytearrayobject.c: bytearray_decode() and in stringobject.c: PyString_AsDecodedObject() and PyString_AsEncodedObject() so it would seem that there's at least some risk in changing this setting.

ASCII vs UTF-8

UTF-8 is identical by design to ASCII when the set of characters is composed only from the ASCII character set: code points 0-127 are all represented in UTF-8 as bytes 0-127, identical to ASCII. So any string which was encodable in "ascii" will also be encodable in "utf-8", and the encodings will be byte-for-byte identical. Data containing bytes in the range 128-255 were not valid "ascii", and attempts to decode them to unicode would have failed.

An internationalized application is highly likely to store and emit characters outside of code points 0-127. With the current setting, scripts that do so will work when run directly at a TTY (since sys.stdout then has UTF-8 encoding), but will fail with a UnicodeEncodeError when run as a cronjob, or as part of a shell pipeline.

Applications which used i18n unicode strings previously could only have worked correctly if they were manually encoding to UTF-8 on every output call, they should also see no regression. Applications which load unicode
strings from translation catalogs would never have worked correctly and will now work.

Note, the only way existing applications could have worked correctly is:

They load unicode strings and manually convert to UTF-8 on output. Fixing the correct default encoding will remove the need for manual conversion on every output call.

They load their i18n strings from a message catalog in UTF-8 format. This is typically specified as the codeset parameter in gettext.bind_textdomain_codeset() or gettext.install(). In this case the strings loaded from the catalog are not <unicode> instances, but are normal python <str> instances. When gettext is told to return strings via _() using the UTF-8 codeset python represents them as 'str' not 'unicode', in other words they are sequences of octets. When output the default encoding is not applied because they are not unicode strings, rather they are vanilla strings. Thus output works in our environment because their entire lifetime in python is as UTF-8.

They imported pango at a suitably early placed during the running of the script, which internally rewrote the default encoding to be UTF-8.

However, there are many good reasons to work with i18n strings as <unicode> instances, not
byte sequences within <str> instances which happen to be represented as UTF-8 (e.g. can't count the
number of characters, can't concatenate, etc.). Thus applications should be able to represent their i18n strings as unicode (internally as UCS-4) and output correctly with correct translation to UTF-8 automatically applied by python, not manually.

The PyArg_ and Py_BuildValue APIs

There are numerous Python modules which wrap libraries, some modules provided as part of the core python package, and some from add-on rpms.

In order to wrap the libraries, the module implementations must convert data between struct PyObject instances and the data types that the libraries use.

The standard way to convert from a struct PyObject to a "native" data type is the PyArg_ API:

The "s", "s#', and "s*" formats (and the "z" variants) will handle a struct PyUnicodeObject as input by encoding the data using the default encoding and generating a C-style NUL-terminated string. By changing from "ascii" to "UTF-8" we convert cases that would fail before, and make them work.

The "u" variants work on unicode and UCS-4 data, or require the caller to specify an encoding.

"et" passes the data from PyStringObject instances without recoding; I don't see how changes from "ascii" to "UTF-8" can cause a problem here.

The Py_BuildValue API works the other way, taking "native" types and converting back to struct PyObject instances. In each case, I believe that it is safe to change the default encoding from ascii to UTF-8.

Benefit to Fedora

With this change, developers will find it significantly easier to use Fedora to write Python scripts: scripts will behave the same way when run within shell pipelines or during cron jobs as when the script is invoked directly from a terminal - a source of mysterious errors will go away.

Scope

(I plan to raise this on the upstream Python development list)

In theory this is just a one-byte change in the site.py shipped in the python rpm.

We do not plan to make the change in the python3 rpm although this has the same code in its site.py; the existing implementation defaults to UTF-8, which matches out defaults.

How To Test

Given that this one-line change makes a deep and subtle change to the internals of Python, the best way of testing this is to get it into Rawhide ASAP and for people to test their Python code on a version of Python with the change.

If anyone encounters a regression related to this change, please file a bug immediately, and let dmalcolm@redhat.com know.

I have been testing with this change on my main development box and have not yet seen any regressions. John Dennis has also tested this and reports no regressions.

Smoketest

Run python -c "import sys; print(sys.getdefaultencoding())"

It should report UTF8, not ascii (assuming that LANG ends with "utf8")

The same test should be runnable with python3, and report utf-8

Shell pipelines

The following shell pipeline should display the first 3 letters of the Greek alphabet (alpha, beta, gamma) within "less"

User Experience

Most users should notice no change. People maintaining Python scripts should find that mysterious errors for scripts that only occur when inside shell pipelines or during cron jobs go away, and that they now work as they do when running the script manually.

If anyone encounters a regression related to this change, please file a bug immediately, and let dmalcolm@redhat.com know.

Dependencies

None: this is a one-line change in our python rpm.

Contingency Plan

In theory this is a one-line change in the site.py file shipped in our python rpm, and so it can be backed out by reverting that one line change.

(It may be that Python applications develop a dependency on our Python having made this change and so would be broken by reverting)

Documentation

Extensive information on this can be found at Features/PythonEncodingUsesSystemLocale.

Release Notes

Python 2's site.py has been changed so that Python 2's default encoding now respects the encoding from the LANG environment variable, typically using UTF-8, rather than defaulting to ASCII. This should eliminate a common source of UnicodeEncodeError problems seen when running Python within shell pipelines.

Comments and Discussion

The disadvantage, such as it is, of this is that it means python2 scripts and programs written on Fedora can work fine here but fail with UnicodeErrors when trying to run it on a non-Fedora machine. Note that upstream python has refused to make this change. AFAIK, we'd be the first to make this change (although some of the python-dev posters propose that the distributions should change on their own; while others say that would be wrong).

One thing I don't like about this is that we lose consistency with upstream and the other distributions without even gaining consitstency between our python3 and python2 handling. python3 hardcodes utf8 for the system locale. This feature would make the encoding dependent on locale everywhere (unless overridden by a third party module like pango). This means that porting code from python2 to python3 would be transitioning from a locale-dependent encoding to hardcoded utf8 which could lead to bugs that no one outside of Fedora has experience with. I'd rather that we either change python2 to hardcode utf8 like python3 does or also change python3 to use the locale.