diff --git a/Lib/locale.py b/Lib/locale.py index dea3ee55cf4d24..ee95c8d6cf2879 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -557,6 +557,8 @@ def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')): 1766. code and encoding can be None in case the values cannot be determined. + Returns `None` on Windows if failed to get locale info. + """ return _getdefaultlocale(envvars) @@ -569,12 +571,10 @@ def _getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')): code, encoding = _locale._getdefaultlocale() except (ImportError, AttributeError): pass + except OSError: + return None else: - # make sure the code/encoding values are valid - if sys.platform == "win32" and code and code[:2] == "0x": - # map windows language identifier to language name - code = windows_locale.get(int(code, 0)) - # ...add other platform-specific processing here, if + # add other platform-specific processing here, if # necessary... return code, encoding diff --git a/Misc/NEWS.d/next/Library/2026-02-21-17-34-53.gh-issue-123853.6RUwWh.rst b/Misc/NEWS.d/next/Library/2026-02-21-17-34-53.gh-issue-123853.6RUwWh.rst new file mode 100644 index 00000000000000..1babcbfd8e678a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-21-17-34-53.gh-issue-123853.6RUwWh.rst @@ -0,0 +1 @@ +Removed Windows 95 compatibility for :func:`locale.getdefaultlocale`. diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c index 7174eebd0c94de..be90dc62f33342 100644 --- a/Modules/_localemodule.c +++ b/Modules/_localemodule.c @@ -29,6 +29,7 @@ This software comes with no warranty. Use at your own risk. # define WIN32_LEAN_AND_MEAN # endif # include +# include "pyerrors.h" // PyErr_SetFromWindowsErr() #endif PyDoc_STRVAR(locale__doc__, "Support for POSIX locales."); @@ -555,20 +556,9 @@ _locale__getdefaultlocale_impl(PyObject *module) return Py_BuildValue("ss", locale, encoding); } - /* If we end up here, this windows version didn't know about - ISO639/ISO3166 names (it's probably Windows 95). Return the - Windows language identifier instead (a hexadecimal number) */ - - locale[0] = '0'; - locale[1] = 'x'; - if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, - locale+2, sizeof(locale)-2)) { - return Py_BuildValue("ss", locale, encoding); - } - /* cannot determine the language code (very unlikely) */ - Py_INCREF(Py_None); - return Py_BuildValue("Os", Py_None, encoding); + /* if any of GetLocaleInfoA above failed */ + return PyErr_SetFromWindowsErr(0); } #endif