python / python/cpython

Migrate to modern win32/winrt API for `localemodule`

Open
#144,728 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules OS-windows type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

AFAIK, CPython has dropped support for Windows 7 since 3.9.0, while _localemodule.c contains compatibility fix for Windows NT pre-4.0. Which flag was introduced since Windows 98.

Here I listed some modern way to get locale name in BCP-47 / RFC 1766 format, is it possible to replace the legacy hex code mapping?

import ctypes
from ctypes import wintypes
from locale import getdefaultlocale, getlocale, LC_CTYPE
import warnings

# Introduced since Windows 10, BCP-47
# depends on:
# - winrt-windows-system-userprofile
# - winrt-windows-foundation
# - winrt-windows-foundation-collections
from winrt.windows.system.userprofile import GlobalizationPreferences

warnings.filterwarnings("ignore", category=DeprecationWarning)

# Load kernel32.dll
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)

# Introduced since Windows Vista, RFC 1766
# Define GetUserDefaultLocaleName signature
# int GetUserDefaultLocaleName(LPWSTR lpLocaleName, int cchLocaleName);
kernel32.GetUserDefaultLocaleName.argtypes = [wintypes.LPWSTR, ctypes.c_int]
kernel32.GetUserDefaultLocaleName.restype = ctypes.c_int

# Allocate buffer for locale name
LOCALE_NAME_MAX_LENGTH = 85  # per Windows docs


def user_default_locale() -> str:
    """
    Get user default locale encoded in BCP-47.
    """
    buffer = ctypes.create_unicode_buffer(LOCALE_NAME_MAX_LENGTH)

    # Call the function
    result = kernel32.GetUserDefaultLocaleName(buffer, LOCALE_NAME_MAX_LENGTH)

    if result == 0:
        # Call failed
        error_code = ctypes.get_last_error()
        raise ctypes.WinError(error_code)

    return buffer.value


def bcp47():
    return GlobalizationPreferences.languages[0]
>>> getdefaultlocale()
('zh_CN', 'cp936')
>>> getlocale(LC_CTYPE)
('Chinese (Simplified)_China', '936')
>>> user_default_locale()
'zh-CN'
>>> bcp47()
'zh-Hans-CN'
Has this already been discussed elsewhere?

Yes

Links to previous discussion of this feature:

gh-90817 gh-130796 https://github.com/python/cpython/issues/123853#issuecomment-3132027944

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Modules/_localemodule.c, especially the compatibility mapping for Windows NT pre-4.0. Review the linked discussions in gh-90817, gh-130796, and issue 123853, then compare GetUserDefaultLocaleName with GlobalizationPreferences. The work is complete only once the project agrees on a modern API and the legacy hex-code mapping is replaced consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
internationalization, operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.