-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
localeconv() should decode numeric fields from LC_NUMERIC encoding, not from LC_CTYPE encoding #76081
Comments
Original bug report: https://bugzilla.redhat.com/show_bug.cgi?id=1484497 It seems that on the development branch of Fedora, when we updated glibc from 2.26 to 2.26.90, test_float_with_comma started failing. Details from the original bug report: Under certain circumstances, when LC_NUMERIC is fr_FR.ISO8859-1 but LC_ALL is C.UTF-8, locale.localeconv() fails with Apparently, the thousands separator (or something else) in the lconv is "\xa0" (unbreakable space in fr_FR.ISO8859-1), and it's being decoded with UTF-8. This is tripped by Python's test suite, namely test_float.GeneralFloatCases.test_float_with_comma |
I can reproduce the bug with Python 3.6 on Fedora 26 and these locales:
Good: LC_NUMERIC = LC_CTYPE = LC_ALL = "es_MX.utf8" haypo@selma$ env -i python3 -c 'import locale; locale.setlocale(locale.LC_ALL, "es_MX.utf8"); print(ascii(locale.localeconv()["thousands_sep"]))' => '\u2009' Bug: LC_NUMERIC = "es_MX.utf8" but LC_CTYPE = LC_ALL = "fr_FR" haypo@selma$ env -i python3 -c 'import locale; locale.setlocale(locale.LC_ALL, "fr_FR"); locale.setlocale(locale.LC_NUMERIC, "es_MX.utf8"); print(ascii(locale.localeconv()["thousands_sep"]))' => '\xe2\x80\x89' |
Tested the PR on a system with glibc 2.26.90 where the test was failing, and it successfully passed. |
Same as bpo-7442, I think. |
Oh wow, this bug is older than what I expected :-) I added support for non-ASCII thousands separator in 2012: |
inconsistent_locale_encodings.py of closed issue bpo-7442 is interesting: I copy it here. |
Pinging here. Is there some way I can help to move the issue forward? |
Oh. Another Python function is impacted by the bug, str.format: $ env -i python3 -c 'import locale; locale.setlocale(locale.LC_ALL, "fr_FR"); locale.setlocale(locale.LC_NUMERIC, "es_MX.utf8"); print(ascii(f"{1000:n}"))'
'1\xe2\x80\x89000' It should be '1\u2009000' ('1', '\u2009', '000'). |
I completed my change. It now fixes locale.localeconv(), str.format() for int, float, complex and decimal.Decimal: vstinner@apu$ ./python lc_numeric.py |
Oops lc_numeric.py contains a typo: d = decimal.Decimal(1234)
print("Decimal.__format__: %a" % f"{i:n}") => it should be f"{d:n}" |
Just FYI: LC_ALL has precedence over all other more specific LC_* settings: http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html Please confirm the bug without having LC_ALL or LANG set. Thanks. |
lc_numeric.py uses: locale.setlocale(locale.LC_ALL, "fr_FR") Are you talking about that? What is the problem with this configuration? I'm sure that there is a bug :-) You aren't able to reproduce it? What is your operating system? |
I just wanted to note that the description and title may cause a wrong interpretation of what should happen: If you first set LC_ALL and then one of the other categories such as LC_NUMERIC, locale C functions will still use the LC_ALL setting for everything. LC_NUMERIC does not override the LC_ALL setting. I tested this on OpenSUSE and get the same wrong results. Apparently, locale.localeconv() does not respect the above order. That's a bug. I'm not sure whether the OP's quoted behavior is a bug, though, since if the locale encoding is not UTF-8, you cannot really expect using UTF-8 numeric separators to output correctly. |
On Mon, Jan 15, 2018 at 12:37:28PM +0000, Marc-Andre Lemburg wrote:
I have the exact same questions as Marc-Andre. This is one of the reasons why I Both views are reasonable IMO. |
Marc-Andre Lemburg: "If you first set LC_ALL and then one of the other categories such as LC_NUMERIC, locale C functions will still use the LC_ALL setting for everything. LC_NUMERIC does not override the LC_ALL setting." The root of this issue is https://bugzilla.redhat.com/show_bug.cgi?id=1484497#c0: Petr Viktorin reproducer scripts uses Python locale.setlocale(), not environment variables: locale.setlocale(locale.LC_ALL, 'C.UTF-8')
locale.setlocale(locale.LC_NUMERIC, 'fr_FR.ISO8859-1') |
Example of Fedora 27 and Python 3.6: vstinner@apu$ env -i LC_NUMERIC=uk_UA.koi8u python3 -c 'import locale; print(locale.setlocale(locale.LC_ALL, "")); print(locale.getpreferredencoding(), ascii(locale.localeconv()["thousands_sep"]))'
LC_CTYPE=C.UTF-8;LC_NUMERIC=uk_UA.koi8u;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib64/python3.6/locale.py", line 110, in localeconv
d = _localeconv()
UnicodeDecodeError: 'locale' codec can't decode byte 0x9a in position 0: Invalid or incomplete multibyte or wide character "env -i" starts Python in an empty environment. It seems like LC_CTYPE defaults to C.UTF-8 in this case.
With my PR, it works: vstinner@apu$ env -i LC_NUMERIC=uk_UA.koi8u ./python -c 'import locale; print(locale.setlocale(locale.LC_ALL, "")); print(locale.getpreferredencoding(), ascii(locale.localeconv()["thousands_sep"]))' => thousands_sep byte string b'\x9A' is decoded as the Uniode string '\xa0'. vstinner@apu$ env -i LC_NUMERIC=uk_UA.koi8u ./python -c 'import locale; locale.setlocale(locale.LC_ALL, ""); print(ascii(f"{1234:n}"))' => the number is properly formatted vstinner@apu$ env -i LC_NUMERIC=uk_UA.koi8u ./python -c 'import locale; locale.setlocale(locale.LC_ALL, ""); print(f"{1234:n}")' It's possible to display the result using print(). |
Ok, it seems that the C setlocale() itself does not follow the conventions set forth for environment variables: http://pubs.opengroup.org/onlinepubs/7908799/xsh/setlocale.html (see the example at the bottom) So the behavior shown by Python's setlocale() is fine. However, that still doesn't magically make this work: locale.setlocale(locale.LC_ALL, 'C.UTF-8')
locale.setlocale(locale.LC_NUMERIC, 'fr_FR.ISO8859-1') If LC_NUMERIC uses a different encoding than LC_ALL, there's really no surprise in having numeric formatting fail. localeconv() will output the set encoding for the numeric string conversion and Python will decode this using the locale encoding set by LC_ALL. If those two are different, you run into problems. I would not consider this a bug in Python, but rather in the locale settings passed to setlocale(). |
The technical issue here is that the libc has no "stateless" function to process bytes and text with one specific locale. All functions rely on the *current* locales. To decode byte strings, we use mbstowcs(), and this function relies on the current LC_CTYPE locale, whereas decimal_point and thousands_sep should be decoded from the current LC_NUMERIC locale. |
Indeed. The major problem with all libc locale functions is that they are not thread safe. The GIL does help a bit protecting against corrupted data, though. |
Past 10 years, I repeated to every single user I met that "Python 3 is right, your system setup is wrong". But that's a waste of time. People continue to associate Python3 and Unicode to annoying bugs, because they don't understand how locales work. Instead of having to repeat to each user that "hum, maybe your config is wrong", I prefer to support this non convential setup and work as expected ("it just works"). With my latest implementation, setlocale() is only done when LC_CTYPE and LC_NUMERIC are different, which is the corner case which "shouldn't occur in practice". |
Sounds like a good compromise :-) |
I tested localeconv() with PR 4174 on FreeBSD:locale.setlocale(locale.LC_ALL, "C")
|
Test on Linux (Fedora 27, glibc 2.26): locale.setlocale(locale.LC_ALL, "fr_FR")
locale.setlocale(locale.LC_NUMERIC, "es_MX.utf8") It works as expected, result: decimal_point: '.' Python 3.6 returns mojibake: decimal_point: '.' Python 2.7 raw strings, thousands_sep = b'\xE2\x80\x89'. |
On macOS 10.13.2, I failed to find any non-ASCII decimal_point or thousands_sep in localeconv(). I wrote a script to find all non-ASCII data in all locales: |
lc_numeric.py contains a typo, used fixed lc_numeric2.py instead to test my PR 5191 which fixes decimal.Decimal. |
That's not true. There is a rich set of *_l functions that take a locale_t object and operate on that locale. |
Victor:
Andreas Schwab:
Oh. Do you want to work on a patch to use these functions? If yes, please open a new issue to enhance the code. |
See also bpo-28604: localeconv() doesn't support LC_MONETARY encoding different than LC_CTYPE encoding. |
The initial bug has been fixed, I close the issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: