This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Strange locale problem with Python 3
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.1, Python 3.2
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ggenellina, mark.dickinson, retoo, robin.stocker, skrah
Priority: normal Keywords:

Created on 2009-02-01 19:48 by retoo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg80917 - (view) Author: Reto Schüttel (retoo) Date: 2009-02-01 19:48
Hi

While helping Brandon Rhodes to port PyEphem[1] to Python 3 we struggled
over a
strange locale-related problem on OS X. PyEphem is a library which can do
astronomical computations like tracking the position of stars, planets and
earth satellites relative to the earth's position. When testing out the
Python
3 release of PyEphem I noticed that on my OS X laptop a lot of calculations
were wrong (not completely wrong, but clearly not accurate) compared to
Python
2.5. We (well mostly Brandon) were able to track down the problem to the TLE
parser (TLE are data file containing the orbital elements of an object)
which
appears to read most values wrong with python 3. In fact it cut of the
decimal
parts of all floats (1.123232 got 1, etc). Manually setting LANG and
LC_ALL to
C solved the problem.

It turns out that some parts of Python 3 got more locale dependent on some
platforms. The only platform I am aware of is OS X, on Linux Python 3
appears
to behave like Python 2.x did.

In case of PyEphem the problem was in the C extension which got more locale
dependent, for example atof() or scanf() with Python 3 now expected the
german
decimal-delimiter ',' instead of the '.' in floats (3.14 vs. 3,14). On the
other hand the constructor of float still expects '.' all the time. But the
built-in function strptime() honors locales with Python 3 and expects german
week day.

I've written a simple script and a simple C extension which illustrates the
problem. Both the extension and the script run python 2.x and python 3,
so you
can easily compare the result while executing the script in different
environments.

I was only able to reproduce the problem on OS X (10.5) and using a german
locale like "de_CH.UTF-8". When manually setting LC_ALL=C, the differences
disappears.

I can't imagine that his behavior was really intended, and I hope the
test case
helps you guys to identify/fix this problem.

Download the test case from:
http://github.com/retoo/py3k-locale-problem/tarball/master
or get it using git:
git://github.com/retoo/py3k-locale-problem.git

You can use the following steps to build it:

$ python2.5 setup.py build
$ python3.0 setup.py build

To run the tests with python 2.5, enter:
$ (cd build/lib*-2.5; python2.5 py3k_locale_problem.py)
... for 3.0  ...
$ (cd build/lib*-3.0; python3.0 py3k_locale_problem.py)

In the file 'results.txt' you can see the output from my OS X system.

Cheers,
Reto Schüttel

[1] http://rhodesmill.org/pyephem/
msg99316 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2010-02-13 13:35
As an aside, I would not use atof(). Better use something like:

char *end;

f = strtod(input, &end);
if (*end != '\0') {
    PyErr_Format(PyExc_ValueError, "Could not convert: %s", end);
    return NULL;
}


I've two questions:

1) Does the problem still appear in Python3.1 or Python3.2?

2) What is the output of:
>>> import locale
>>> locale.setlocale(locale.LC_ALL)
msg99331 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-02-13 20:44
Actually, for Python 3.x, rather than using strtod directly it would be better to use the PyOS_string_to_double C-API function.  That function is entirely locale-agnostic:  it should behave identically to the float constructor.

Python 3.0 is no longer supported:  it would be good to know whether this problem persists with Python 3.1 or the py3k svn branch.
msg99621 - (view) Author: Reto Schüttel (retoo) Date: 2010-02-20 16:00
Hey Mark, Stefan

I've got the example working again with 3.1 and it appears like the behaviour has been reverted to the pre-3.0 days. So the problem is fixed for me :)! 

with 3.1 (and 2.6) atof only accepts a '.' as a decimal delimiter, regardless of the configured locale.

Thanks for your help! I'll close this bug report.

Reto
History
Date User Action Args
2022-04-11 14:56:45adminsetgithub: 49375
2010-02-20 16:00:54retoosetstatus: open -> closed

messages: + msg99621
2010-02-13 20:44:16mark.dickinsonsetnosy: + mark.dickinson

messages: + msg99331
versions: + Python 3.1, Python 3.2, - Python 3.0
2010-02-13 13:35:16skrahsetnosy: + skrah
messages: + msg99316
2009-02-04 22:16:55ggenellinasetnosy: + ggenellina
2009-02-02 22:03:07robin.stockersetnosy: + robin.stocker
2009-02-01 19:48:28retoocreate