/src/cpython/Modules/_localemodule.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*********************************************************** |
2 | | Copyright (C) 1997, 2002, 2003, 2007, 2008 Martin von Loewis |
3 | | |
4 | | Permission to use, copy, modify, and distribute this software and its |
5 | | documentation for any purpose and without fee is hereby granted, |
6 | | provided that the above copyright notice appear in all copies. |
7 | | |
8 | | This software comes with no warranty. Use at your own risk. |
9 | | |
10 | | ******************************************************************/ |
11 | | |
12 | | #include "Python.h" |
13 | | #include "pycore_fileutils.h" // _Py_GetLocaleconvNumeric() |
14 | | #include "pycore_pymem.h" // _PyMem_Strdup() |
15 | | |
16 | | #include <locale.h> // setlocale() |
17 | | #include <string.h> // strlen() |
18 | | #ifdef HAVE_ERRNO_H |
19 | | # include <errno.h> // errno |
20 | | #endif |
21 | | #ifdef HAVE_LANGINFO_H |
22 | | # include <langinfo.h> // nl_langinfo() |
23 | | #endif |
24 | | #ifdef HAVE_LIBINTL_H |
25 | | # include <libintl.h> |
26 | | #endif |
27 | | #ifdef MS_WINDOWS |
28 | | # ifndef WIN32_LEAN_AND_MEAN |
29 | | # define WIN32_LEAN_AND_MEAN |
30 | | # endif |
31 | | # include <windows.h> |
32 | | #endif |
33 | | |
34 | | PyDoc_STRVAR(locale__doc__, "Support for POSIX locales."); |
35 | | |
36 | | typedef struct _locale_state { |
37 | | PyObject *Error; |
38 | | } _locale_state; |
39 | | |
40 | | static inline _locale_state* |
41 | | get_locale_state(PyObject *m) |
42 | 0 | { |
43 | 0 | void *state = PyModule_GetState(m); |
44 | 0 | assert(state != NULL); |
45 | 0 | return (_locale_state *)state; |
46 | 0 | } |
47 | | |
48 | | #include "clinic/_localemodule.c.h" |
49 | | |
50 | | /*[clinic input] |
51 | | module _locale |
52 | | [clinic start generated code]*/ |
53 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ed98569b726feada]*/ |
54 | | |
55 | | /* support functions for formatting floating-point numbers */ |
56 | | |
57 | | /* the grouping is terminated by either 0 or CHAR_MAX */ |
58 | | static PyObject* |
59 | | copy_grouping(const char* s) |
60 | 0 | { |
61 | 0 | int i; |
62 | 0 | PyObject *result, *val = NULL; |
63 | |
|
64 | 0 | if (s[0] == '\0') { |
65 | | /* empty string: no grouping at all */ |
66 | 0 | return PyList_New(0); |
67 | 0 | } |
68 | | |
69 | 0 | for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++) |
70 | 0 | ; /* nothing */ |
71 | |
|
72 | 0 | result = PyList_New(i+1); |
73 | 0 | if (!result) |
74 | 0 | return NULL; |
75 | | |
76 | 0 | i = -1; |
77 | 0 | do { |
78 | 0 | i++; |
79 | 0 | val = PyLong_FromLong(s[i]); |
80 | 0 | if (val == NULL) { |
81 | 0 | Py_DECREF(result); |
82 | 0 | return NULL; |
83 | 0 | } |
84 | 0 | PyList_SET_ITEM(result, i, val); |
85 | 0 | } while (s[i] != '\0' && s[i] != CHAR_MAX); |
86 | | |
87 | 0 | return result; |
88 | 0 | } |
89 | | |
90 | | #if defined(MS_WINDOWS) |
91 | | |
92 | | // 16 is the number of elements in the szCodePage field |
93 | | // of the __crt_locale_strings structure. |
94 | | #define MAX_CP_LEN 15 |
95 | | |
96 | | static int |
97 | | check_locale_name(const char *locale, const char *end) |
98 | | { |
99 | | size_t len = end ? (size_t)(end - locale) : strlen(locale); |
100 | | const char *dot = memchr(locale, '.', len); |
101 | | if (dot && locale + len - dot - 1 > MAX_CP_LEN) { |
102 | | return -1; |
103 | | } |
104 | | return 0; |
105 | | } |
106 | | |
107 | | static int |
108 | | check_locale_name_all(const char *locale) |
109 | | { |
110 | | const char *start = locale; |
111 | | while (1) { |
112 | | const char *end = strchr(start, ';'); |
113 | | if (check_locale_name(start, end) < 0) { |
114 | | return -1; |
115 | | } |
116 | | if (end == NULL) { |
117 | | break; |
118 | | } |
119 | | start = end + 1; |
120 | | } |
121 | | return 0; |
122 | | } |
123 | | #endif |
124 | | |
125 | | /*[clinic input] |
126 | | _locale.setlocale |
127 | | |
128 | | category: int |
129 | | locale: str(accept={str, NoneType}) = NULL |
130 | | / |
131 | | |
132 | | Activates/queries locale processing. |
133 | | [clinic start generated code]*/ |
134 | | |
135 | | static PyObject * |
136 | | _locale_setlocale_impl(PyObject *module, int category, const char *locale) |
137 | | /*[clinic end generated code: output=a0e777ae5d2ff117 input=dbe18f1d66c57a6a]*/ |
138 | 0 | { |
139 | 0 | char *result; |
140 | 0 | PyObject *result_object; |
141 | |
|
142 | | #if defined(MS_WINDOWS) |
143 | | if (category < LC_MIN || category > LC_MAX) |
144 | | { |
145 | | PyErr_SetString(get_locale_state(module)->Error, |
146 | | "invalid locale category"); |
147 | | return NULL; |
148 | | } |
149 | | if (locale) { |
150 | | if ((category == LC_ALL |
151 | | ? check_locale_name_all(locale) |
152 | | : check_locale_name(locale, NULL)) < 0) |
153 | | { |
154 | | /* Debug assertion failure on Windows. |
155 | | * _Py_BEGIN_SUPPRESS_IPH/_Py_END_SUPPRESS_IPH do not help. */ |
156 | | PyErr_SetString(get_locale_state(module)->Error, |
157 | | "unsupported locale setting"); |
158 | | return NULL; |
159 | | } |
160 | | } |
161 | | #endif |
162 | |
|
163 | 0 | if (locale) { |
164 | | /* set locale */ |
165 | 0 | result = setlocale(category, locale); |
166 | 0 | if (!result) { |
167 | | /* operation failed, no setting was changed */ |
168 | 0 | PyErr_SetString(get_locale_state(module)->Error, |
169 | 0 | "unsupported locale setting"); |
170 | 0 | return NULL; |
171 | 0 | } |
172 | 0 | result_object = PyUnicode_DecodeLocale(result, NULL); |
173 | 0 | if (!result_object) |
174 | 0 | return NULL; |
175 | 0 | } else { |
176 | | /* get locale */ |
177 | 0 | result = setlocale(category, NULL); |
178 | 0 | if (!result) { |
179 | 0 | PyErr_SetString(get_locale_state(module)->Error, |
180 | 0 | "locale query failed"); |
181 | 0 | return NULL; |
182 | 0 | } |
183 | 0 | result_object = PyUnicode_DecodeLocale(result, NULL); |
184 | 0 | } |
185 | 0 | return result_object; |
186 | 0 | } |
187 | | |
188 | | static int |
189 | | locale_is_ascii(const char *str) |
190 | 0 | { |
191 | 0 | return (strlen(str) == 1 && ((unsigned char)str[0]) <= 127); |
192 | 0 | } |
193 | | |
194 | | static int |
195 | | is_all_ascii(const char *str) |
196 | 0 | { |
197 | 0 | for (; *str; str++) { |
198 | 0 | if ((unsigned char)*str > 127) { |
199 | 0 | return 0; |
200 | 0 | } |
201 | 0 | } |
202 | 0 | return 1; |
203 | 0 | } |
204 | | |
205 | | static int |
206 | | locale_decode_monetary(PyObject *dict, struct lconv *lc) |
207 | 0 | { |
208 | 0 | #ifndef MS_WINDOWS |
209 | 0 | int change_locale; |
210 | 0 | change_locale = (!locale_is_ascii(lc->int_curr_symbol) |
211 | 0 | || !locale_is_ascii(lc->currency_symbol) |
212 | 0 | || !locale_is_ascii(lc->mon_decimal_point) |
213 | 0 | || !locale_is_ascii(lc->mon_thousands_sep)); |
214 | | |
215 | | /* Keep a copy of the LC_CTYPE locale */ |
216 | 0 | char *oldloc = NULL, *loc = NULL; |
217 | 0 | if (change_locale) { |
218 | 0 | oldloc = setlocale(LC_CTYPE, NULL); |
219 | 0 | if (!oldloc) { |
220 | 0 | PyErr_SetString(PyExc_RuntimeWarning, |
221 | 0 | "failed to get LC_CTYPE locale"); |
222 | 0 | return -1; |
223 | 0 | } |
224 | | |
225 | 0 | oldloc = _PyMem_Strdup(oldloc); |
226 | 0 | if (!oldloc) { |
227 | 0 | PyErr_NoMemory(); |
228 | 0 | return -1; |
229 | 0 | } |
230 | | |
231 | 0 | loc = setlocale(LC_MONETARY, NULL); |
232 | 0 | if (loc != NULL && strcmp(loc, oldloc) == 0) { |
233 | 0 | loc = NULL; |
234 | 0 | } |
235 | |
|
236 | 0 | if (loc != NULL) { |
237 | | /* Only set the locale temporarily the LC_CTYPE locale |
238 | | to the LC_MONETARY locale if the two locales are different and |
239 | | at least one string is non-ASCII. */ |
240 | 0 | setlocale(LC_CTYPE, loc); |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | 0 | #define GET_LOCALE_STRING(ATTR) PyUnicode_DecodeLocale(lc->ATTR, NULL) |
245 | | #else /* MS_WINDOWS */ |
246 | | /* Use _W_* fields of Windows struct lconv */ |
247 | | #define GET_LOCALE_STRING(ATTR) PyUnicode_FromWideChar(lc->_W_ ## ATTR, -1) |
248 | | #endif /* MS_WINDOWS */ |
249 | | |
250 | 0 | int res = -1; |
251 | |
|
252 | 0 | #define RESULT_STRING(ATTR) \ |
253 | 0 | do { \ |
254 | 0 | PyObject *obj; \ |
255 | 0 | obj = GET_LOCALE_STRING(ATTR); \ |
256 | 0 | if (obj == NULL) { \ |
257 | 0 | goto done; \ |
258 | 0 | } \ |
259 | 0 | if (PyDict_SetItemString(dict, Py_STRINGIFY(ATTR), obj) < 0) { \ |
260 | 0 | Py_DECREF(obj); \ |
261 | 0 | goto done; \ |
262 | 0 | } \ |
263 | 0 | Py_DECREF(obj); \ |
264 | 0 | } while (0) |
265 | |
|
266 | 0 | RESULT_STRING(int_curr_symbol); |
267 | 0 | RESULT_STRING(currency_symbol); |
268 | 0 | RESULT_STRING(mon_decimal_point); |
269 | 0 | RESULT_STRING(mon_thousands_sep); |
270 | 0 | #undef RESULT_STRING |
271 | 0 | #undef GET_LOCALE_STRING |
272 | | |
273 | 0 | res = 0; |
274 | |
|
275 | 0 | done: |
276 | 0 | #ifndef MS_WINDOWS |
277 | 0 | if (loc != NULL) { |
278 | 0 | setlocale(LC_CTYPE, oldloc); |
279 | 0 | } |
280 | 0 | PyMem_Free(oldloc); |
281 | 0 | #endif |
282 | 0 | return res; |
283 | 0 | } |
284 | | |
285 | | /*[clinic input] |
286 | | _locale.localeconv |
287 | | |
288 | | Returns numeric and monetary locale-specific parameters. |
289 | | [clinic start generated code]*/ |
290 | | |
291 | | static PyObject * |
292 | | _locale_localeconv_impl(PyObject *module) |
293 | | /*[clinic end generated code: output=43a54515e0a2aef5 input=f1132d15accf4444]*/ |
294 | 0 | { |
295 | 0 | PyObject* result; |
296 | 0 | struct lconv *lc; |
297 | 0 | PyObject *x; |
298 | |
|
299 | 0 | result = PyDict_New(); |
300 | 0 | if (!result) { |
301 | 0 | return NULL; |
302 | 0 | } |
303 | | |
304 | | /* if LC_NUMERIC is different in the C library, use saved value */ |
305 | 0 | lc = localeconv(); |
306 | | |
307 | | /* hopefully, the localeconv result survives the C library calls |
308 | | involved herein */ |
309 | |
|
310 | 0 | #define RESULT(key, obj)\ |
311 | 0 | do { \ |
312 | 0 | if (obj == NULL) \ |
313 | 0 | goto failed; \ |
314 | 0 | if (PyDict_SetItemString(result, key, obj) < 0) { \ |
315 | 0 | Py_DECREF(obj); \ |
316 | 0 | goto failed; \ |
317 | 0 | } \ |
318 | 0 | Py_DECREF(obj); \ |
319 | 0 | } while (0) |
320 | |
|
321 | | #ifdef MS_WINDOWS |
322 | | /* Use _W_* fields of Windows struct lconv */ |
323 | | #define GET_LOCALE_STRING(ATTR) PyUnicode_FromWideChar(lc->_W_ ## ATTR, -1) |
324 | | #else |
325 | 0 | #define GET_LOCALE_STRING(ATTR) PyUnicode_DecodeLocale(lc->ATTR, NULL) |
326 | 0 | #endif |
327 | 0 | #define RESULT_STRING(s)\ |
328 | 0 | do { \ |
329 | 0 | x = GET_LOCALE_STRING(s); \ |
330 | 0 | RESULT(#s, x); \ |
331 | 0 | } while (0) |
332 | |
|
333 | 0 | #define RESULT_INT(i)\ |
334 | 0 | do { \ |
335 | 0 | x = PyLong_FromLong(lc->i); \ |
336 | 0 | RESULT(#i, x); \ |
337 | 0 | } while (0) |
338 | | |
339 | | /* Monetary information: LC_MONETARY encoding */ |
340 | 0 | if (locale_decode_monetary(result, lc) < 0) { |
341 | 0 | goto failed; |
342 | 0 | } |
343 | 0 | x = copy_grouping(lc->mon_grouping); |
344 | 0 | RESULT("mon_grouping", x); |
345 | | |
346 | 0 | RESULT_STRING(positive_sign); |
347 | 0 | RESULT_STRING(negative_sign); |
348 | 0 | RESULT_INT(int_frac_digits); |
349 | 0 | RESULT_INT(frac_digits); |
350 | 0 | RESULT_INT(p_cs_precedes); |
351 | 0 | RESULT_INT(p_sep_by_space); |
352 | 0 | RESULT_INT(n_cs_precedes); |
353 | 0 | RESULT_INT(n_sep_by_space); |
354 | 0 | RESULT_INT(p_sign_posn); |
355 | 0 | RESULT_INT(n_sign_posn); |
356 | | |
357 | | /* Numeric information: LC_NUMERIC encoding */ |
358 | 0 | PyObject *decimal_point = NULL, *thousands_sep = NULL; |
359 | 0 | if (_Py_GetLocaleconvNumeric(lc, &decimal_point, &thousands_sep) < 0) { |
360 | 0 | Py_XDECREF(decimal_point); |
361 | 0 | Py_XDECREF(thousands_sep); |
362 | 0 | goto failed; |
363 | 0 | } |
364 | | |
365 | 0 | if (PyDict_SetItemString(result, "decimal_point", decimal_point) < 0) { |
366 | 0 | Py_DECREF(decimal_point); |
367 | 0 | Py_DECREF(thousands_sep); |
368 | 0 | goto failed; |
369 | 0 | } |
370 | 0 | Py_DECREF(decimal_point); |
371 | |
|
372 | 0 | if (PyDict_SetItemString(result, "thousands_sep", thousands_sep) < 0) { |
373 | 0 | Py_DECREF(thousands_sep); |
374 | 0 | goto failed; |
375 | 0 | } |
376 | 0 | Py_DECREF(thousands_sep); |
377 | |
|
378 | 0 | x = copy_grouping(lc->grouping); |
379 | 0 | RESULT("grouping", x); |
380 | | |
381 | 0 | return result; |
382 | | |
383 | 0 | failed: |
384 | 0 | Py_DECREF(result); |
385 | 0 | return NULL; |
386 | |
|
387 | 0 | #undef RESULT |
388 | 0 | #undef RESULT_STRING |
389 | 0 | #undef RESULT_INT |
390 | 0 | #undef GET_LOCALE_STRING |
391 | 0 | } |
392 | | |
393 | | #if defined(HAVE_WCSCOLL) |
394 | | |
395 | | /*[clinic input] |
396 | | _locale.strcoll |
397 | | |
398 | | os1: unicode |
399 | | os2: unicode |
400 | | / |
401 | | |
402 | | Compares two strings according to the locale. |
403 | | [clinic start generated code]*/ |
404 | | |
405 | | static PyObject * |
406 | | _locale_strcoll_impl(PyObject *module, PyObject *os1, PyObject *os2) |
407 | | /*[clinic end generated code: output=82ddc6d62c76d618 input=693cd02bcbf38dd8]*/ |
408 | 0 | { |
409 | 0 | PyObject *result = NULL; |
410 | 0 | wchar_t *ws1 = NULL, *ws2 = NULL; |
411 | | |
412 | | /* Convert the unicode strings to wchar[]. */ |
413 | 0 | ws1 = PyUnicode_AsWideCharString(os1, NULL); |
414 | 0 | if (ws1 == NULL) |
415 | 0 | goto done; |
416 | 0 | ws2 = PyUnicode_AsWideCharString(os2, NULL); |
417 | 0 | if (ws2 == NULL) |
418 | 0 | goto done; |
419 | | /* Collate the strings. */ |
420 | 0 | result = PyLong_FromLong(wcscoll(ws1, ws2)); |
421 | 0 | done: |
422 | | /* Deallocate everything. */ |
423 | 0 | if (ws1) PyMem_Free(ws1); |
424 | 0 | if (ws2) PyMem_Free(ws2); |
425 | 0 | return result; |
426 | 0 | } |
427 | | #endif |
428 | | |
429 | | #ifdef HAVE_WCSXFRM |
430 | | |
431 | | /*[clinic input] |
432 | | _locale.strxfrm |
433 | | |
434 | | string as str: unicode |
435 | | / |
436 | | |
437 | | Return a string that can be used as a key for locale-aware comparisons. |
438 | | [clinic start generated code]*/ |
439 | | |
440 | | static PyObject * |
441 | | _locale_strxfrm_impl(PyObject *module, PyObject *str) |
442 | | /*[clinic end generated code: output=3081866ebffc01af input=1378bbe6a88b4780]*/ |
443 | 0 | { |
444 | 0 | Py_ssize_t n1; |
445 | 0 | wchar_t *s = NULL, *buf = NULL; |
446 | 0 | size_t n2; |
447 | 0 | PyObject *result = NULL; |
448 | |
|
449 | 0 | s = PyUnicode_AsWideCharString(str, &n1); |
450 | 0 | if (s == NULL) |
451 | 0 | goto exit; |
452 | 0 | if (wcslen(s) != (size_t)n1) { |
453 | 0 | PyErr_SetString(PyExc_ValueError, |
454 | 0 | "embedded null character"); |
455 | 0 | goto exit; |
456 | 0 | } |
457 | | |
458 | | /* assume no change in size, first */ |
459 | 0 | n1 = n1 + 1; |
460 | 0 | buf = PyMem_New(wchar_t, n1); |
461 | 0 | if (!buf) { |
462 | 0 | PyErr_NoMemory(); |
463 | 0 | goto exit; |
464 | 0 | } |
465 | 0 | errno = 0; |
466 | 0 | n2 = wcsxfrm(buf, s, n1); |
467 | 0 | if (errno && errno != ERANGE) { |
468 | 0 | PyErr_SetFromErrno(PyExc_OSError); |
469 | 0 | goto exit; |
470 | 0 | } |
471 | 0 | if (n2 >= (size_t)n1) { |
472 | | /* more space needed */ |
473 | 0 | wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t)); |
474 | 0 | if (!new_buf) { |
475 | 0 | PyErr_NoMemory(); |
476 | 0 | goto exit; |
477 | 0 | } |
478 | 0 | buf = new_buf; |
479 | 0 | errno = 0; |
480 | 0 | n2 = wcsxfrm(buf, s, n2+1); |
481 | 0 | if (errno) { |
482 | 0 | PyErr_SetFromErrno(PyExc_OSError); |
483 | 0 | goto exit; |
484 | 0 | } |
485 | 0 | } |
486 | 0 | result = PyUnicode_FromWideChar(buf, n2); |
487 | 0 | exit: |
488 | 0 | PyMem_Free(buf); |
489 | 0 | PyMem_Free(s); |
490 | 0 | return result; |
491 | 0 | } |
492 | | #endif |
493 | | |
494 | | #if defined(MS_WINDOWS) |
495 | | |
496 | | /*[clinic input] |
497 | | _locale._getdefaultlocale |
498 | | |
499 | | [clinic start generated code]*/ |
500 | | |
501 | | static PyObject * |
502 | | _locale__getdefaultlocale_impl(PyObject *module) |
503 | | /*[clinic end generated code: output=e6254088579534c2 input=003ea41acd17f7c7]*/ |
504 | | { |
505 | | char encoding[20]; |
506 | | char locale[100]; |
507 | | |
508 | | PyOS_snprintf(encoding, sizeof(encoding), "cp%u", GetACP()); |
509 | | |
510 | | if (GetLocaleInfoA(LOCALE_USER_DEFAULT, |
511 | | LOCALE_SISO639LANGNAME, |
512 | | locale, sizeof(locale))) { |
513 | | Py_ssize_t i = strlen(locale); |
514 | | locale[i++] = '_'; |
515 | | if (GetLocaleInfoA(LOCALE_USER_DEFAULT, |
516 | | LOCALE_SISO3166CTRYNAME, |
517 | | locale+i, (int)(sizeof(locale)-i))) |
518 | | return Py_BuildValue("ss", locale, encoding); |
519 | | } |
520 | | |
521 | | /* If we end up here, this windows version didn't know about |
522 | | ISO639/ISO3166 names (it's probably Windows 95). Return the |
523 | | Windows language identifier instead (a hexadecimal number) */ |
524 | | |
525 | | locale[0] = '0'; |
526 | | locale[1] = 'x'; |
527 | | if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, |
528 | | locale+2, sizeof(locale)-2)) { |
529 | | return Py_BuildValue("ss", locale, encoding); |
530 | | } |
531 | | |
532 | | /* cannot determine the language code (very unlikely) */ |
533 | | Py_INCREF(Py_None); |
534 | | return Py_BuildValue("Os", Py_None, encoding); |
535 | | } |
536 | | #endif |
537 | | |
538 | | #ifdef HAVE_LANGINFO_H |
539 | | #define LANGINFO(X, Y) {#X, X, Y} |
540 | | static struct langinfo_constant{ |
541 | | const char *name; |
542 | | int value; |
543 | | int category; |
544 | | } langinfo_constants[] = |
545 | | { |
546 | | /* These constants should exist on any langinfo implementation */ |
547 | | LANGINFO(DAY_1, LC_TIME), |
548 | | LANGINFO(DAY_2, LC_TIME), |
549 | | LANGINFO(DAY_3, LC_TIME), |
550 | | LANGINFO(DAY_4, LC_TIME), |
551 | | LANGINFO(DAY_5, LC_TIME), |
552 | | LANGINFO(DAY_6, LC_TIME), |
553 | | LANGINFO(DAY_7, LC_TIME), |
554 | | |
555 | | LANGINFO(ABDAY_1, LC_TIME), |
556 | | LANGINFO(ABDAY_2, LC_TIME), |
557 | | LANGINFO(ABDAY_3, LC_TIME), |
558 | | LANGINFO(ABDAY_4, LC_TIME), |
559 | | LANGINFO(ABDAY_5, LC_TIME), |
560 | | LANGINFO(ABDAY_6, LC_TIME), |
561 | | LANGINFO(ABDAY_7, LC_TIME), |
562 | | |
563 | | LANGINFO(MON_1, LC_TIME), |
564 | | LANGINFO(MON_2, LC_TIME), |
565 | | LANGINFO(MON_3, LC_TIME), |
566 | | LANGINFO(MON_4, LC_TIME), |
567 | | LANGINFO(MON_5, LC_TIME), |
568 | | LANGINFO(MON_6, LC_TIME), |
569 | | LANGINFO(MON_7, LC_TIME), |
570 | | LANGINFO(MON_8, LC_TIME), |
571 | | LANGINFO(MON_9, LC_TIME), |
572 | | LANGINFO(MON_10, LC_TIME), |
573 | | LANGINFO(MON_11, LC_TIME), |
574 | | LANGINFO(MON_12, LC_TIME), |
575 | | |
576 | | LANGINFO(ABMON_1, LC_TIME), |
577 | | LANGINFO(ABMON_2, LC_TIME), |
578 | | LANGINFO(ABMON_3, LC_TIME), |
579 | | LANGINFO(ABMON_4, LC_TIME), |
580 | | LANGINFO(ABMON_5, LC_TIME), |
581 | | LANGINFO(ABMON_6, LC_TIME), |
582 | | LANGINFO(ABMON_7, LC_TIME), |
583 | | LANGINFO(ABMON_8, LC_TIME), |
584 | | LANGINFO(ABMON_9, LC_TIME), |
585 | | LANGINFO(ABMON_10, LC_TIME), |
586 | | LANGINFO(ABMON_11, LC_TIME), |
587 | | LANGINFO(ABMON_12, LC_TIME), |
588 | | |
589 | | #ifdef RADIXCHAR |
590 | | /* The following are not available with glibc 2.0 */ |
591 | | LANGINFO(RADIXCHAR, LC_NUMERIC), |
592 | | LANGINFO(THOUSEP, LC_NUMERIC), |
593 | | /* YESSTR and NOSTR are deprecated in glibc, since they are |
594 | | a special case of message translation, which should be rather |
595 | | done using gettext. So we don't expose it to Python in the |
596 | | first place. |
597 | | LANGINFO(YESSTR, LC_MESSAGES), |
598 | | LANGINFO(NOSTR, LC_MESSAGES), |
599 | | */ |
600 | | LANGINFO(CRNCYSTR, LC_MONETARY), |
601 | | #endif |
602 | | |
603 | | LANGINFO(D_T_FMT, LC_TIME), |
604 | | LANGINFO(D_FMT, LC_TIME), |
605 | | LANGINFO(T_FMT, LC_TIME), |
606 | | LANGINFO(AM_STR, LC_TIME), |
607 | | LANGINFO(PM_STR, LC_TIME), |
608 | | |
609 | | /* The following constants are available only with XPG4, but... |
610 | | OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have |
611 | | a few of the others. |
612 | | Solution: ifdef-test them all. */ |
613 | | #ifdef CODESET |
614 | | LANGINFO(CODESET, LC_CTYPE), |
615 | | #endif |
616 | | #ifdef T_FMT_AMPM |
617 | | LANGINFO(T_FMT_AMPM, LC_TIME), |
618 | | #endif |
619 | | #ifdef ERA |
620 | | LANGINFO(ERA, LC_TIME), |
621 | | #endif |
622 | | #ifdef ERA_D_FMT |
623 | | LANGINFO(ERA_D_FMT, LC_TIME), |
624 | | #endif |
625 | | #ifdef ERA_D_T_FMT |
626 | | LANGINFO(ERA_D_T_FMT, LC_TIME), |
627 | | #endif |
628 | | #ifdef ERA_T_FMT |
629 | | LANGINFO(ERA_T_FMT, LC_TIME), |
630 | | #endif |
631 | | #ifdef ALT_DIGITS |
632 | | LANGINFO(ALT_DIGITS, LC_TIME), |
633 | | #endif |
634 | | #ifdef YESEXPR |
635 | | LANGINFO(YESEXPR, LC_MESSAGES), |
636 | | #endif |
637 | | #ifdef NOEXPR |
638 | | LANGINFO(NOEXPR, LC_MESSAGES), |
639 | | #endif |
640 | | #ifdef _DATE_FMT |
641 | | /* This is not available in all glibc versions that have CODESET. */ |
642 | | LANGINFO(_DATE_FMT, LC_TIME), |
643 | | #endif |
644 | | {0, 0, 0} |
645 | | }; |
646 | | |
647 | | /* Temporary make the LC_CTYPE locale to be the same as |
648 | | * the locale of the specified category. */ |
649 | | static int |
650 | | change_locale(int category, char **oldloc) |
651 | 0 | { |
652 | | /* Keep a copy of the LC_CTYPE locale */ |
653 | 0 | *oldloc = setlocale(LC_CTYPE, NULL); |
654 | 0 | if (!*oldloc) { |
655 | 0 | PyErr_SetString(PyExc_RuntimeError, "failed to get LC_CTYPE locale"); |
656 | 0 | return -1; |
657 | 0 | } |
658 | 0 | *oldloc = _PyMem_Strdup(*oldloc); |
659 | 0 | if (!*oldloc) { |
660 | 0 | PyErr_NoMemory(); |
661 | 0 | return -1; |
662 | 0 | } |
663 | | |
664 | | /* Set a new locale if it is different. */ |
665 | 0 | char *loc = setlocale(category, NULL); |
666 | 0 | if (loc == NULL || strcmp(loc, *oldloc) == 0) { |
667 | 0 | PyMem_Free(*oldloc); |
668 | 0 | *oldloc = NULL; |
669 | 0 | return 0; |
670 | 0 | } |
671 | | |
672 | 0 | setlocale(LC_CTYPE, loc); |
673 | 0 | return 1; |
674 | 0 | } |
675 | | |
676 | | /* Restore the old LC_CTYPE locale. */ |
677 | | static void |
678 | | restore_locale(char *oldloc) |
679 | 0 | { |
680 | 0 | if (oldloc != NULL) { |
681 | 0 | setlocale(LC_CTYPE, oldloc); |
682 | 0 | PyMem_Free(oldloc); |
683 | 0 | } |
684 | 0 | } |
685 | | |
686 | | #ifdef __GLIBC__ |
687 | | #if defined(ALT_DIGITS) || defined(ERA) |
688 | | static PyObject * |
689 | | decode_strings(const char *result, size_t max_count) |
690 | 0 | { |
691 | | /* Convert a sequence of NUL-separated C strings to a Python string |
692 | | * containing semicolon separated items. */ |
693 | 0 | size_t i = 0; |
694 | 0 | size_t count = 0; |
695 | 0 | for (; count < max_count && result[i]; count++) { |
696 | 0 | i += strlen(result + i) + 1; |
697 | 0 | } |
698 | 0 | char *buf = PyMem_Malloc(i); |
699 | 0 | if (buf == NULL) { |
700 | 0 | PyErr_NoMemory(); |
701 | 0 | return NULL; |
702 | 0 | } |
703 | 0 | memcpy(buf, result, i); |
704 | | /* Replace all NULs with semicolons. */ |
705 | 0 | i = 0; |
706 | 0 | while (--count) { |
707 | 0 | i += strlen(buf + i); |
708 | 0 | buf[i++] = ';'; |
709 | 0 | } |
710 | 0 | PyObject *pyresult = PyUnicode_DecodeLocale(buf, NULL); |
711 | 0 | PyMem_Free(buf); |
712 | 0 | return pyresult; |
713 | 0 | } |
714 | | #endif |
715 | | #endif |
716 | | |
717 | | /*[clinic input] |
718 | | _locale.nl_langinfo |
719 | | |
720 | | key as item: int |
721 | | / |
722 | | |
723 | | Return the value for the locale information associated with key. |
724 | | [clinic start generated code]*/ |
725 | | |
726 | | static PyObject * |
727 | | _locale_nl_langinfo_impl(PyObject *module, int item) |
728 | | /*[clinic end generated code: output=6aea457b47e077a3 input=00798143eecfeddc]*/ |
729 | 0 | { |
730 | 0 | int i; |
731 | | /* Check whether this is a supported constant. GNU libc sometimes |
732 | | returns numeric values in the char* return value, which would |
733 | | crash PyUnicode_FromString. */ |
734 | 0 | for (i = 0; langinfo_constants[i].name; i++) { |
735 | 0 | if (langinfo_constants[i].value == item) { |
736 | | /* Check NULL as a workaround for GNU libc's returning NULL |
737 | | instead of an empty string for nl_langinfo(ERA). */ |
738 | 0 | const char *result = nl_langinfo(item); |
739 | 0 | result = result != NULL ? result : ""; |
740 | 0 | char *oldloc = NULL; |
741 | 0 | if (langinfo_constants[i].category != LC_CTYPE |
742 | 0 | && *result && ( |
743 | 0 | #ifdef __GLIBC__ |
744 | | // gh-133740: Always change the locale for ALT_DIGITS and ERA |
745 | 0 | # ifdef ALT_DIGITS |
746 | 0 | item == ALT_DIGITS || |
747 | 0 | # endif |
748 | 0 | # ifdef ERA |
749 | 0 | item == ERA || |
750 | 0 | # endif |
751 | 0 | #endif |
752 | 0 | !is_all_ascii(result)) |
753 | 0 | && change_locale(langinfo_constants[i].category, &oldloc) < 0) |
754 | 0 | { |
755 | 0 | return NULL; |
756 | 0 | } |
757 | 0 | PyObject *pyresult; |
758 | 0 | #ifdef __GLIBC__ |
759 | | /* According to the POSIX specification the result must be |
760 | | * a sequence of semicolon-separated strings. |
761 | | * But in Glibc they are NUL-separated. */ |
762 | 0 | #ifdef ALT_DIGITS |
763 | 0 | if (item == ALT_DIGITS && *result) { |
764 | 0 | pyresult = decode_strings(result, 100); |
765 | 0 | } |
766 | 0 | else |
767 | 0 | #endif |
768 | 0 | #ifdef ERA |
769 | 0 | if (item == ERA && *result) { |
770 | 0 | pyresult = decode_strings(result, SIZE_MAX); |
771 | 0 | } |
772 | 0 | else |
773 | 0 | #endif |
774 | 0 | #endif |
775 | 0 | { |
776 | 0 | pyresult = PyUnicode_DecodeLocale(result, NULL); |
777 | 0 | } |
778 | 0 | restore_locale(oldloc); |
779 | 0 | return pyresult; |
780 | 0 | } |
781 | 0 | } |
782 | 0 | PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant"); |
783 | 0 | return NULL; |
784 | 0 | } |
785 | | #endif /* HAVE_LANGINFO_H */ |
786 | | |
787 | | #ifdef HAVE_LIBINTL_H |
788 | | |
789 | | /*[clinic input] |
790 | | _locale.gettext |
791 | | |
792 | | msg as in: str |
793 | | / |
794 | | |
795 | | gettext(msg) -> string |
796 | | |
797 | | Return translation of msg. |
798 | | [clinic start generated code]*/ |
799 | | |
800 | | static PyObject * |
801 | | _locale_gettext_impl(PyObject *module, const char *in) |
802 | | /*[clinic end generated code: output=493bb4b38a4704fe input=949fc8efc2bb3bc3]*/ |
803 | 0 | { |
804 | 0 | return PyUnicode_DecodeLocale(gettext(in), NULL); |
805 | 0 | } |
806 | | |
807 | | /*[clinic input] |
808 | | _locale.dgettext |
809 | | |
810 | | domain: str(accept={str, NoneType}) |
811 | | msg as in: str |
812 | | / |
813 | | |
814 | | dgettext(domain, msg) -> string |
815 | | |
816 | | Return translation of msg in domain. |
817 | | [clinic start generated code]*/ |
818 | | |
819 | | static PyObject * |
820 | | _locale_dgettext_impl(PyObject *module, const char *domain, const char *in) |
821 | | /*[clinic end generated code: output=3c0cd5287b972c8f input=a277388a635109d8]*/ |
822 | 0 | { |
823 | 0 | return PyUnicode_DecodeLocale(dgettext(domain, in), NULL); |
824 | 0 | } |
825 | | |
826 | | /*[clinic input] |
827 | | _locale.dcgettext |
828 | | |
829 | | domain: str(accept={str, NoneType}) |
830 | | msg as msgid: str |
831 | | category: int |
832 | | / |
833 | | |
834 | | Return translation of msg in domain and category. |
835 | | [clinic start generated code]*/ |
836 | | |
837 | | static PyObject * |
838 | | _locale_dcgettext_impl(PyObject *module, const char *domain, |
839 | | const char *msgid, int category) |
840 | | /*[clinic end generated code: output=0f4cc4fce0aa283f input=ec5f8fed4336de67]*/ |
841 | 0 | { |
842 | 0 | return PyUnicode_DecodeLocale(dcgettext(domain,msgid,category), NULL); |
843 | 0 | } |
844 | | |
845 | | /*[clinic input] |
846 | | _locale.textdomain |
847 | | |
848 | | domain: str(accept={str, NoneType}) |
849 | | / |
850 | | |
851 | | Set the C library's textdmain to domain, returning the new domain. |
852 | | [clinic start generated code]*/ |
853 | | |
854 | | static PyObject * |
855 | | _locale_textdomain_impl(PyObject *module, const char *domain) |
856 | | /*[clinic end generated code: output=7992df06aadec313 input=66359716f5eb1d38]*/ |
857 | 0 | { |
858 | 0 | domain = textdomain(domain); |
859 | 0 | if (!domain) { |
860 | 0 | PyErr_SetFromErrno(PyExc_OSError); |
861 | 0 | return NULL; |
862 | 0 | } |
863 | 0 | return PyUnicode_DecodeLocale(domain, NULL); |
864 | 0 | } |
865 | | |
866 | | /*[clinic input] |
867 | | _locale.bindtextdomain |
868 | | |
869 | | domain: str |
870 | | dir as dirname_obj: object |
871 | | / |
872 | | |
873 | | Bind the C library's domain to dir. |
874 | | [clinic start generated code]*/ |
875 | | |
876 | | static PyObject * |
877 | | _locale_bindtextdomain_impl(PyObject *module, const char *domain, |
878 | | PyObject *dirname_obj) |
879 | | /*[clinic end generated code: output=6d6f3c7b345d785c input=c0dff085acfe272b]*/ |
880 | 0 | { |
881 | 0 | const char *dirname, *current_dirname; |
882 | 0 | PyObject *dirname_bytes = NULL, *result; |
883 | |
|
884 | 0 | if (!strlen(domain)) { |
885 | 0 | PyErr_SetString(get_locale_state(module)->Error, |
886 | 0 | "domain must be a non-empty string"); |
887 | 0 | return 0; |
888 | 0 | } |
889 | 0 | if (dirname_obj != Py_None) { |
890 | 0 | if (!PyUnicode_FSConverter(dirname_obj, &dirname_bytes)) |
891 | 0 | return NULL; |
892 | 0 | dirname = PyBytes_AsString(dirname_bytes); |
893 | 0 | } else { |
894 | 0 | dirname_bytes = NULL; |
895 | 0 | dirname = NULL; |
896 | 0 | } |
897 | 0 | current_dirname = bindtextdomain(domain, dirname); |
898 | 0 | if (current_dirname == NULL) { |
899 | 0 | PyErr_SetFromErrno(PyExc_OSError); |
900 | 0 | Py_XDECREF(dirname_bytes); |
901 | 0 | return NULL; |
902 | 0 | } |
903 | 0 | result = PyUnicode_DecodeLocale(current_dirname, NULL); |
904 | 0 | Py_XDECREF(dirname_bytes); |
905 | 0 | return result; |
906 | 0 | } |
907 | | |
908 | | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
909 | | |
910 | | /*[clinic input] |
911 | | _locale.bind_textdomain_codeset |
912 | | |
913 | | domain: str |
914 | | codeset: str(accept={str, NoneType}) |
915 | | / |
916 | | |
917 | | Bind the C library's domain to codeset. |
918 | | [clinic start generated code]*/ |
919 | | |
920 | | static PyObject * |
921 | | _locale_bind_textdomain_codeset_impl(PyObject *module, const char *domain, |
922 | | const char *codeset) |
923 | | /*[clinic end generated code: output=fa452f9c8b1b9e89 input=23fbe3540400f259]*/ |
924 | 0 | { |
925 | 0 | codeset = bind_textdomain_codeset(domain, codeset); |
926 | 0 | if (codeset) { |
927 | 0 | return PyUnicode_DecodeLocale(codeset, NULL); |
928 | 0 | } |
929 | 0 | Py_RETURN_NONE; |
930 | 0 | } |
931 | | #endif // HAVE_BIND_TEXTDOMAIN_CODESET |
932 | | |
933 | | #endif // HAVE_LIBINTL_H |
934 | | |
935 | | |
936 | | /*[clinic input] |
937 | | _locale.getencoding |
938 | | |
939 | | Get the current locale encoding. |
940 | | [clinic start generated code]*/ |
941 | | |
942 | | static PyObject * |
943 | | _locale_getencoding_impl(PyObject *module) |
944 | | /*[clinic end generated code: output=86b326b971872e46 input=6503d11e5958b360]*/ |
945 | 0 | { |
946 | 0 | return _Py_GetLocaleEncodingObject(); |
947 | 0 | } |
948 | | |
949 | | |
950 | | static struct PyMethodDef PyLocale_Methods[] = { |
951 | | _LOCALE_SETLOCALE_METHODDEF |
952 | | _LOCALE_LOCALECONV_METHODDEF |
953 | | #ifdef HAVE_WCSCOLL |
954 | | _LOCALE_STRCOLL_METHODDEF |
955 | | #endif |
956 | | #ifdef HAVE_WCSXFRM |
957 | | _LOCALE_STRXFRM_METHODDEF |
958 | | #endif |
959 | | #if defined(MS_WINDOWS) |
960 | | _LOCALE__GETDEFAULTLOCALE_METHODDEF |
961 | | #endif |
962 | | #ifdef HAVE_LANGINFO_H |
963 | | _LOCALE_NL_LANGINFO_METHODDEF |
964 | | #endif |
965 | | #ifdef HAVE_LIBINTL_H |
966 | | _LOCALE_GETTEXT_METHODDEF |
967 | | _LOCALE_DGETTEXT_METHODDEF |
968 | | _LOCALE_DCGETTEXT_METHODDEF |
969 | | _LOCALE_TEXTDOMAIN_METHODDEF |
970 | | _LOCALE_BINDTEXTDOMAIN_METHODDEF |
971 | | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
972 | | _LOCALE_BIND_TEXTDOMAIN_CODESET_METHODDEF |
973 | | #endif |
974 | | #endif |
975 | | _LOCALE_GETENCODING_METHODDEF |
976 | | {NULL, NULL} |
977 | | }; |
978 | | |
979 | | static int |
980 | | _locale_exec(PyObject *module) |
981 | 0 | { |
982 | 0 | #ifdef HAVE_LANGINFO_H |
983 | 0 | int i; |
984 | 0 | #endif |
985 | 0 | #define ADD_INT(module, value) \ |
986 | 0 | do { \ |
987 | 0 | if (PyModule_AddIntConstant(module, #value, value) < 0) { \ |
988 | 0 | return -1; \ |
989 | 0 | } \ |
990 | 0 | } while (0) |
991 | |
|
992 | 0 | ADD_INT(module, LC_CTYPE); |
993 | 0 | ADD_INT(module, LC_TIME); |
994 | 0 | ADD_INT(module, LC_COLLATE); |
995 | 0 | ADD_INT(module, LC_MONETARY); |
996 | | |
997 | 0 | #ifdef LC_MESSAGES |
998 | 0 | ADD_INT(module, LC_MESSAGES); |
999 | 0 | #endif /* LC_MESSAGES */ |
1000 | | |
1001 | 0 | ADD_INT(module, LC_NUMERIC); |
1002 | 0 | ADD_INT(module, LC_ALL); |
1003 | 0 | ADD_INT(module, CHAR_MAX); |
1004 | | |
1005 | 0 | _locale_state *state = get_locale_state(module); |
1006 | 0 | state->Error = PyErr_NewException("locale.Error", NULL, NULL); |
1007 | 0 | if (PyModule_AddObjectRef(module, "Error", state->Error) < 0) { |
1008 | 0 | return -1; |
1009 | 0 | } |
1010 | | |
1011 | 0 | #ifdef HAVE_LANGINFO_H |
1012 | 0 | for (i = 0; langinfo_constants[i].name; i++) { |
1013 | 0 | if (PyModule_AddIntConstant(module, |
1014 | 0 | langinfo_constants[i].name, |
1015 | 0 | langinfo_constants[i].value) < 0) { |
1016 | 0 | return -1; |
1017 | 0 | } |
1018 | 0 | } |
1019 | 0 | #endif |
1020 | | |
1021 | 0 | if (PyErr_Occurred()) { |
1022 | 0 | return -1; |
1023 | 0 | } |
1024 | 0 | return 0; |
1025 | |
|
1026 | 0 | #undef ADD_INT |
1027 | 0 | } |
1028 | | |
1029 | | static struct PyModuleDef_Slot _locale_slots[] = { |
1030 | | {Py_mod_exec, _locale_exec}, |
1031 | | {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, |
1032 | | {Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
1033 | | {0, NULL} |
1034 | | }; |
1035 | | |
1036 | | static int |
1037 | | locale_traverse(PyObject *module, visitproc visit, void *arg) |
1038 | 0 | { |
1039 | 0 | _locale_state *state = get_locale_state(module); |
1040 | 0 | Py_VISIT(state->Error); |
1041 | 0 | return 0; |
1042 | 0 | } |
1043 | | |
1044 | | static int |
1045 | | locale_clear(PyObject *module) |
1046 | 0 | { |
1047 | 0 | _locale_state *state = get_locale_state(module); |
1048 | 0 | Py_CLEAR(state->Error); |
1049 | 0 | return 0; |
1050 | 0 | } |
1051 | | |
1052 | | static void |
1053 | | locale_free(void *module) |
1054 | 0 | { |
1055 | 0 | locale_clear((PyObject*)module); |
1056 | 0 | } |
1057 | | |
1058 | | static struct PyModuleDef _localemodule = { |
1059 | | PyModuleDef_HEAD_INIT, |
1060 | | "_locale", |
1061 | | locale__doc__, |
1062 | | sizeof(_locale_state), |
1063 | | PyLocale_Methods, |
1064 | | _locale_slots, |
1065 | | locale_traverse, |
1066 | | locale_clear, |
1067 | | locale_free, |
1068 | | }; |
1069 | | |
1070 | | PyMODINIT_FUNC |
1071 | | PyInit__locale(void) |
1072 | 0 | { |
1073 | 0 | return PyModuleDef_Init(&_localemodule); |
1074 | 0 | } |
1075 | | |
1076 | | /* |
1077 | | Local variables: |
1078 | | c-basic-offset: 4 |
1079 | | indent-tabs-mode: nil |
1080 | | End: |
1081 | | */ |