python / python/cpython

Migrate to modern win32/winrt API for `localemodule`

Aperta
#144,728 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

extension-modules OS-windows type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da Modules/_localemodule.c, in particolare dalla mappatura di compatibilità per Windows NT pre-4.0. Esamina le discussioni collegate in gh-90817, gh-130796 e issue 123853, quindi confronta GetUserDefaultLocaleName con GlobalizationPreferences. Il lavoro sarà completo solo quando il progetto avrà concordato una API moderna e la mappatura legacy dei codici esadecimali sarà stata sostituita in modo coerente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
c, python
Ambito
internationalization, operating-systems
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.