Skip to content
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

locale.nl_langinfo() can't decode value #69998

Open
serhiy-storchaka opened this issue Dec 5, 2015 · 4 comments
Open

locale.nl_langinfo() can't decode value #69998

serhiy-storchaka opened this issue Dec 5, 2015 · 4 comments
Assignees
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 25812
Nosy @malemburg, @loewis, @warsaw, @vstinner, @bitdancer, @serhiy-storchaka, @nnja

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:

assignee = 'https://github.com/nnja'
closed_at = None
created_at = <Date 2015-12-05.21:19:05.273>
labels = ['3.8', 'type-bug', 'library']
title = "locale.nl_langinfo() can't decode value"
updated_at = <Date 2019-01-09.11:53:37.581>
user = 'https://github.com/serhiy-storchaka'

bugs.python.org fields:

activity = <Date 2019-01-09.11:53:37.581>
actor = 'vstinner'
assignee = 'nnja'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2015-12-05.21:19:05.273>
creator = 'serhiy.storchaka'
dependencies = []
files = []
hgrepos = []
issue_num = 25812
keywords = []
message_count = 4.0
messages = ['255979', '267017', '267192', '333308']
nosy_count = 7.0
nosy_names = ['lemburg', 'loewis', 'barry', 'vstinner', 'r.david.murray', 'serhiy.storchaka', 'nnja']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue25812'
versions = ['Python 3.8']

@serhiy-storchaka
Copy link
Member Author

>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, 'uk_UA')
'uk_UA'
>>> locale.getlocale(locale.LC_NUMERIC)
('uk_UA', 'KOI8-U')
>>> locale.nl_langinfo(locale.THOUSEP)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'locale' codec can't decode byte 0x9a in position 0: Invalid or incomplete multibyte or wide character

Looks as locale.nl_langinfo() always uses the UTF-8 encoding (or may be locale.getpreferredencoding()).

@serhiy-storchaka serhiy-storchaka added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Dec 5, 2015
@nnja
Copy link
Contributor

nnja commented Jun 3, 2016

Adding the test below to test__locale.py will reproduce the issue under the following conditions:

  • The locale uk_UA is installed on your system.

  • 'uk_UA': (',', '\xa0') is added to the known_numerics dictionary in this test file

    @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
    def test_lc_numeric_not_char_nl_langinfo(self):
    # Test nl_langinfo against known values/
    # It should still work if there's a mismatch between
    # String & Numeric Locales
    tested = False
    for loc in candidate_locales:
    try:
    setlocale(LC_NUMERIC, loc)
    except Error:
    continue
    for li, lc in ((RADIXCHAR, "decimal_point"),
    (THOUSEP, "thousands_sep")):
    if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc):
    tested = True
    if not tested:
    self.skipTest('no suitable locales')

@bitdancer
Copy link
Member

Thanks, Nina. We do have support in test.support for running a test with a specific locale (run_with_locale), so this could be turned into a unit test patch if you or someone else is willing to do that.

@warsaw warsaw added the 3.8 only security fixes label Sep 28, 2018
@vstinner
Copy link
Member

vstinner commented Jan 9, 2019

Since this bug has been reported, locale.localeconv() has been fixed in bpo-31900 to temporarily set LC_CTYPE to LC_NUMERIC to decode numeric fields of localeconv() from the proper encoding. I guess that a similar fix can be applied to locale.nl_langinfo(): set LC_CTYPE to LC_NUMERIC if the parameter is a numeric field.

I only knew locale.nl_langinfo(locale.CODESET). I didn't know that this function accepted other arguments :-)

I even wrote an article about these locale bugs :-)
#5191

See also bpo-35697: "decimal: formatter error if LC_NUMERIC uses a different encoding than LC_CTYPE".

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: No status
Development

No branches or pull requests

5 participants