Skip to content

Unguarded use of nl_langinfo on _Py_GetLocaleEncoding #149616

@AGraber

Description

@AGraber

Bug report

Bug description:

When building on a platform that is not Windows and HAVE_LANGINFO_H is not defined, building can fail because _Py_GetLocaleEncoding attempts to use nl_langinfo regardless:

cpython/Python/fileutils.c

Lines 906 to 918 in 7241f27

#ifdef MS_WINDOWS
wchar_t encoding[23];
unsigned int ansi_codepage = GetACP();
swprintf(encoding, Py_ARRAY_LENGTH(encoding), L"cp%u", ansi_codepage);
encoding[Py_ARRAY_LENGTH(encoding) - 1] = 0;
return _PyMem_RawWcsdup(encoding);
#else
const char *encoding = nl_langinfo(CODESET);
if (!encoding || encoding[0] == '\0') {
// Use UTF-8 if nl_langinfo() returns an empty string. It can happen on
// macOS if the LC_CTYPE locale is not supported.
return _PyMem_RawWcsdup(L"utf-8");
}

Maybe if there's no other way to tell it can just default to UTF-8 like Android or etc.

CPython versions tested on:

3.14

Operating systems tested on:

Other

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions