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/
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)
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.
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