Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 2 | Copyright (C) 1997, 2002, 2003, 2007, 2008 Martin von Loewis |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 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. |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 10 | ******************************************************************/ |
| 11 | |
Antoine Pitrou | 6a448d4 | 2009-10-19 19:43:09 +0000 | [diff] [blame] | 12 | #define PY_SSIZE_T_CLEAN |
Fred Drake | 68933b9 | 2000-08-10 21:41:08 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 14 | #include "pycore_fileutils.h" |
Fred Drake | 68933b9 | 2000-08-10 21:41:08 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 17 | #include <locale.h> |
| 18 | #include <string.h> |
Guido van Rossum | 5cd70f4 | 1998-06-19 04:33:30 +0000 | [diff] [blame] | 19 | #include <ctype.h> |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | #ifdef HAVE_ERRNO_H |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #endif |
| 24 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 25 | #ifdef HAVE_LANGINFO_H |
| 26 | #include <langinfo.h> |
| 27 | #endif |
| 28 | |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 29 | #ifdef HAVE_LIBINTL_H |
| 30 | #include <libintl.h> |
| 31 | #endif |
| 32 | |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 33 | #ifdef HAVE_WCHAR_H |
| 34 | #include <wchar.h> |
| 35 | #endif |
| 36 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 37 | #if defined(MS_WINDOWS) |
Tim Peters | 7a1f917 | 2002-07-14 22:14:19 +0000 | [diff] [blame] | 38 | #define WIN32_LEAN_AND_MEAN |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 39 | #include <windows.h> |
Guido van Rossum | 239a218 | 1998-04-28 16:08:19 +0000 | [diff] [blame] | 40 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 42 | PyDoc_STRVAR(locale__doc__, "Support for POSIX locales."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 43 | |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 44 | typedef struct _locale_state { |
| 45 | PyObject *Error; |
| 46 | } _locale_state; |
| 47 | |
| 48 | static inline _locale_state* |
| 49 | get_locale_state(PyObject *m) |
| 50 | { |
| 51 | void *state = PyModule_GetState(m); |
| 52 | assert(state != NULL); |
| 53 | return (_locale_state *)state; |
| 54 | } |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 55 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 56 | #include "clinic/_localemodule.c.h" |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 57 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 58 | /*[clinic input] |
| 59 | module _locale |
| 60 | [clinic start generated code]*/ |
| 61 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ed98569b726feada]*/ |
| 62 | |
| 63 | /* support functions for formatting floating point numbers */ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 65 | /* the grouping is terminated by either 0 or CHAR_MAX */ |
| 66 | static PyObject* |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 67 | copy_grouping(const char* s) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 68 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 69 | int i; |
| 70 | PyObject *result, *val = NULL; |
| 71 | |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 72 | if (s[0] == '\0') { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 73 | /* empty string: no grouping at all */ |
| 74 | return PyList_New(0); |
Martin Panter | 6d57fe1 | 2016-09-17 03:26:16 +0000 | [diff] [blame] | 75 | } |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 76 | |
| 77 | for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++) |
| 78 | ; /* nothing */ |
| 79 | |
| 80 | result = PyList_New(i+1); |
| 81 | if (!result) |
| 82 | return NULL; |
| 83 | |
| 84 | i = -1; |
| 85 | do { |
| 86 | i++; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 87 | val = PyLong_FromLong(s[i]); |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 88 | if (val == NULL) { |
| 89 | Py_DECREF(result); |
| 90 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 91 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 92 | PyList_SET_ITEM(result, i, val); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 93 | } while (s[i] != '\0' && s[i] != CHAR_MAX); |
| 94 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 95 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 98 | /*[clinic input] |
| 99 | _locale.setlocale |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 100 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 101 | category: int |
| 102 | locale: str(accept={str, NoneType}) = NULL |
| 103 | / |
| 104 | |
| 105 | Activates/queries locale processing. |
| 106 | [clinic start generated code]*/ |
| 107 | |
| 108 | static PyObject * |
| 109 | _locale_setlocale_impl(PyObject *module, int category, const char *locale) |
| 110 | /*[clinic end generated code: output=a0e777ae5d2ff117 input=dbe18f1d66c57a6a]*/ |
| 111 | { |
| 112 | char *result; |
| 113 | PyObject *result_object; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 114 | |
Amaury Forgeot d'Arc | 64f3ca4 | 2009-12-01 21:59:18 +0000 | [diff] [blame] | 115 | #if defined(MS_WINDOWS) |
| 116 | if (category < LC_MIN || category > LC_MAX) |
| 117 | { |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 118 | PyErr_SetString(get_locale_state(module)->Error, |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 119 | "invalid locale category"); |
Amaury Forgeot d'Arc | 64f3ca4 | 2009-12-01 21:59:18 +0000 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | #endif |
| 123 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 124 | if (locale) { |
| 125 | /* set locale */ |
| 126 | result = setlocale(category, locale); |
| 127 | if (!result) { |
| 128 | /* operation failed, no setting was changed */ |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 129 | PyErr_SetString(get_locale_state(module)->Error, |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 130 | "unsupported locale setting"); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 133 | result_object = PyUnicode_DecodeLocale(result, NULL); |
Mark Hammond | 9a71475 | 2003-07-24 14:15:07 +0000 | [diff] [blame] | 134 | if (!result_object) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 135 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 136 | } else { |
| 137 | /* get locale */ |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 138 | result = setlocale(category, NULL); |
| 139 | if (!result) { |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 140 | PyErr_SetString(get_locale_state(module)->Error, |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 141 | "locale query failed"); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 142 | return NULL; |
| 143 | } |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 144 | result_object = PyUnicode_DecodeLocale(result, NULL); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 145 | } |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 146 | return result_object; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 149 | static int |
| 150 | locale_is_ascii(const char *str) |
| 151 | { |
| 152 | return (strlen(str) == 1 && ((unsigned char)str[0]) <= 127); |
| 153 | } |
| 154 | |
| 155 | static int |
| 156 | locale_decode_monetary(PyObject *dict, struct lconv *lc) |
| 157 | { |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 158 | #ifndef MS_WINDOWS |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 159 | int change_locale; |
| 160 | change_locale = (!locale_is_ascii(lc->int_curr_symbol) |
| 161 | || !locale_is_ascii(lc->currency_symbol) |
| 162 | || !locale_is_ascii(lc->mon_decimal_point) |
| 163 | || !locale_is_ascii(lc->mon_thousands_sep)); |
| 164 | |
| 165 | /* Keep a copy of the LC_CTYPE locale */ |
| 166 | char *oldloc = NULL, *loc = NULL; |
| 167 | if (change_locale) { |
| 168 | oldloc = setlocale(LC_CTYPE, NULL); |
| 169 | if (!oldloc) { |
| 170 | PyErr_SetString(PyExc_RuntimeWarning, |
| 171 | "failed to get LC_CTYPE locale"); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | oldloc = _PyMem_Strdup(oldloc); |
| 176 | if (!oldloc) { |
| 177 | PyErr_NoMemory(); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | loc = setlocale(LC_MONETARY, NULL); |
| 182 | if (loc != NULL && strcmp(loc, oldloc) == 0) { |
| 183 | loc = NULL; |
| 184 | } |
| 185 | |
| 186 | if (loc != NULL) { |
| 187 | /* Only set the locale temporarily the LC_CTYPE locale |
| 188 | to the LC_MONETARY locale if the two locales are different and |
| 189 | at least one string is non-ASCII. */ |
| 190 | setlocale(LC_CTYPE, loc); |
| 191 | } |
| 192 | } |
| 193 | |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 194 | #define GET_LOCALE_STRING(ATTR) PyUnicode_DecodeLocale(lc->ATTR, NULL) |
| 195 | #else /* MS_WINDOWS */ |
| 196 | /* Use _W_* fields of Windows struct lconv */ |
| 197 | #define GET_LOCALE_STRING(ATTR) PyUnicode_FromWideChar(lc->_W_ ## ATTR, -1) |
| 198 | #endif /* MS_WINDOWS */ |
| 199 | |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 200 | int res = -1; |
| 201 | |
| 202 | #define RESULT_STRING(ATTR) \ |
| 203 | do { \ |
| 204 | PyObject *obj; \ |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 205 | obj = GET_LOCALE_STRING(ATTR); \ |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 206 | if (obj == NULL) { \ |
| 207 | goto done; \ |
| 208 | } \ |
| 209 | if (PyDict_SetItemString(dict, Py_STRINGIFY(ATTR), obj) < 0) { \ |
| 210 | Py_DECREF(obj); \ |
| 211 | goto done; \ |
| 212 | } \ |
| 213 | Py_DECREF(obj); \ |
| 214 | } while (0) |
| 215 | |
| 216 | RESULT_STRING(int_curr_symbol); |
| 217 | RESULT_STRING(currency_symbol); |
| 218 | RESULT_STRING(mon_decimal_point); |
| 219 | RESULT_STRING(mon_thousands_sep); |
| 220 | #undef RESULT_STRING |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 221 | #undef GET_LOCALE_STRING |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 222 | |
| 223 | res = 0; |
| 224 | |
| 225 | done: |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 226 | #ifndef MS_WINDOWS |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 227 | if (loc != NULL) { |
| 228 | setlocale(LC_CTYPE, oldloc); |
| 229 | } |
| 230 | PyMem_Free(oldloc); |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 231 | #endif |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 232 | return res; |
| 233 | } |
| 234 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 235 | /*[clinic input] |
| 236 | _locale.localeconv |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 237 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 238 | Returns numeric and monetary locale-specific parameters. |
| 239 | [clinic start generated code]*/ |
| 240 | |
| 241 | static PyObject * |
| 242 | _locale_localeconv_impl(PyObject *module) |
| 243 | /*[clinic end generated code: output=43a54515e0a2aef5 input=f1132d15accf4444]*/ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 244 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 245 | PyObject* result; |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 246 | struct lconv *lc; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 247 | PyObject *x; |
| 248 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 249 | result = PyDict_New(); |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 250 | if (!result) { |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 251 | return NULL; |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 252 | } |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 253 | |
| 254 | /* if LC_NUMERIC is different in the C library, use saved value */ |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 255 | lc = localeconv(); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 256 | |
| 257 | /* hopefully, the localeconv result survives the C library calls |
| 258 | involved herein */ |
| 259 | |
Victor Stinner | d594f24 | 2013-07-17 00:55:57 +0200 | [diff] [blame] | 260 | #define RESULT(key, obj)\ |
| 261 | do { \ |
| 262 | if (obj == NULL) \ |
| 263 | goto failed; \ |
Victor Stinner | f38a5c2 | 2013-10-29 19:28:20 +0100 | [diff] [blame] | 264 | if (PyDict_SetItemString(result, key, obj) < 0) { \ |
| 265 | Py_DECREF(obj); \ |
Victor Stinner | d594f24 | 2013-07-17 00:55:57 +0200 | [diff] [blame] | 266 | goto failed; \ |
Victor Stinner | f38a5c2 | 2013-10-29 19:28:20 +0100 | [diff] [blame] | 267 | } \ |
Victor Stinner | d594f24 | 2013-07-17 00:55:57 +0200 | [diff] [blame] | 268 | Py_DECREF(obj); \ |
| 269 | } while (0) |
| 270 | |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 271 | #ifdef MS_WINDOWS |
| 272 | /* Use _W_* fields of Windows struct lconv */ |
| 273 | #define GET_LOCALE_STRING(ATTR) PyUnicode_FromWideChar(lc->_W_ ## ATTR, -1) |
| 274 | #else |
| 275 | #define GET_LOCALE_STRING(ATTR) PyUnicode_DecodeLocale(lc->ATTR, NULL) |
| 276 | #endif |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 277 | #define RESULT_STRING(s)\ |
Victor Stinner | d594f24 | 2013-07-17 00:55:57 +0200 | [diff] [blame] | 278 | do { \ |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 279 | x = GET_LOCALE_STRING(s); \ |
Victor Stinner | d594f24 | 2013-07-17 00:55:57 +0200 | [diff] [blame] | 280 | RESULT(#s, x); \ |
| 281 | } while (0) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 282 | |
| 283 | #define RESULT_INT(i)\ |
Victor Stinner | d594f24 | 2013-07-17 00:55:57 +0200 | [diff] [blame] | 284 | do { \ |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 285 | x = PyLong_FromLong(lc->i); \ |
Victor Stinner | d594f24 | 2013-07-17 00:55:57 +0200 | [diff] [blame] | 286 | RESULT(#i, x); \ |
| 287 | } while (0) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 288 | |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 289 | /* Monetary information: LC_MONETARY encoding */ |
| 290 | if (locale_decode_monetary(result, lc) < 0) { |
| 291 | goto failed; |
| 292 | } |
| 293 | x = copy_grouping(lc->mon_grouping); |
Victor Stinner | d594f24 | 2013-07-17 00:55:57 +0200 | [diff] [blame] | 294 | RESULT("mon_grouping", x); |
| 295 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 296 | RESULT_STRING(positive_sign); |
| 297 | RESULT_STRING(negative_sign); |
| 298 | RESULT_INT(int_frac_digits); |
| 299 | RESULT_INT(frac_digits); |
| 300 | RESULT_INT(p_cs_precedes); |
| 301 | RESULT_INT(p_sep_by_space); |
| 302 | RESULT_INT(n_cs_precedes); |
| 303 | RESULT_INT(n_sep_by_space); |
| 304 | RESULT_INT(p_sign_posn); |
| 305 | RESULT_INT(n_sign_posn); |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 306 | |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 307 | /* Numeric information: LC_NUMERIC encoding */ |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 308 | PyObject *decimal_point = NULL, *thousands_sep = NULL; |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 309 | if (_Py_GetLocaleconvNumeric(lc, &decimal_point, &thousands_sep) < 0) { |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 310 | Py_XDECREF(decimal_point); |
| 311 | Py_XDECREF(thousands_sep); |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 312 | goto failed; |
| 313 | } |
| 314 | |
| 315 | if (PyDict_SetItemString(result, "decimal_point", decimal_point) < 0) { |
| 316 | Py_DECREF(decimal_point); |
| 317 | Py_DECREF(thousands_sep); |
| 318 | goto failed; |
| 319 | } |
| 320 | Py_DECREF(decimal_point); |
| 321 | |
| 322 | if (PyDict_SetItemString(result, "thousands_sep", thousands_sep) < 0) { |
| 323 | Py_DECREF(thousands_sep); |
| 324 | goto failed; |
| 325 | } |
| 326 | Py_DECREF(thousands_sep); |
| 327 | |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 328 | x = copy_grouping(lc->grouping); |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 329 | RESULT("grouping", x); |
| 330 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 331 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 332 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 333 | failed: |
Victor Stinner | cb064fc | 2018-01-15 15:58:02 +0100 | [diff] [blame] | 334 | Py_DECREF(result); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 335 | return NULL; |
Victor Stinner | 02e6bf7 | 2018-11-20 16:20:16 +0100 | [diff] [blame] | 336 | |
| 337 | #undef RESULT |
| 338 | #undef RESULT_STRING |
| 339 | #undef RESULT_INT |
TIGirardi | f231203 | 2020-10-20 08:39:52 -0300 | [diff] [blame] | 340 | #undef GET_LOCALE_STRING |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 343 | #if defined(HAVE_WCSCOLL) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 344 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 345 | /*[clinic input] |
| 346 | _locale.strcoll |
| 347 | |
| 348 | os1: unicode |
| 349 | os2: unicode |
| 350 | / |
| 351 | |
| 352 | Compares two strings according to the locale. |
| 353 | [clinic start generated code]*/ |
| 354 | |
| 355 | static PyObject * |
| 356 | _locale_strcoll_impl(PyObject *module, PyObject *os1, PyObject *os2) |
| 357 | /*[clinic end generated code: output=82ddc6d62c76d618 input=693cd02bcbf38dd8]*/ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 358 | { |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 359 | PyObject *result = NULL; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 360 | wchar_t *ws1 = NULL, *ws2 = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 362 | /* Convert the unicode strings to wchar[]. */ |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 363 | ws1 = PyUnicode_AsWideCharString(os1, NULL); |
Victor Stinner | 449057f | 2010-09-29 10:30:43 +0000 | [diff] [blame] | 364 | if (ws1 == NULL) |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 365 | goto done; |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 366 | ws2 = PyUnicode_AsWideCharString(os2, NULL); |
Victor Stinner | 449057f | 2010-09-29 10:30:43 +0000 | [diff] [blame] | 367 | if (ws2 == NULL) |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 368 | goto done; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 369 | /* Collate the strings. */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 370 | result = PyLong_FromLong(wcscoll(ws1, ws2)); |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 371 | done: |
| 372 | /* Deallocate everything. */ |
| 373 | if (ws1) PyMem_FREE(ws1); |
| 374 | if (ws2) PyMem_FREE(ws2); |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 375 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 376 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 377 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 378 | |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 379 | #ifdef HAVE_WCSXFRM |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 380 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 381 | /*[clinic input] |
| 382 | _locale.strxfrm |
| 383 | |
| 384 | string as str: unicode |
| 385 | / |
| 386 | |
| 387 | Return a string that can be used as a key for locale-aware comparisons. |
| 388 | [clinic start generated code]*/ |
| 389 | |
| 390 | static PyObject * |
| 391 | _locale_strxfrm_impl(PyObject *module, PyObject *str) |
| 392 | /*[clinic end generated code: output=3081866ebffc01af input=1378bbe6a88b4780]*/ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 393 | { |
Victor Stinner | 6364927 | 2011-09-29 23:32:06 +0200 | [diff] [blame] | 394 | Py_ssize_t n1; |
| 395 | wchar_t *s = NULL, *buf = NULL; |
| 396 | size_t n2; |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 397 | PyObject *result = NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 398 | |
Victor Stinner | 6364927 | 2011-09-29 23:32:06 +0200 | [diff] [blame] | 399 | s = PyUnicode_AsWideCharString(str, &n1); |
| 400 | if (s == NULL) |
| 401 | goto exit; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 402 | if (wcslen(s) != (size_t)n1) { |
| 403 | PyErr_SetString(PyExc_ValueError, |
| 404 | "embedded null character"); |
| 405 | goto exit; |
| 406 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 407 | |
| 408 | /* assume no change in size, first */ |
Victor Stinner | 6364927 | 2011-09-29 23:32:06 +0200 | [diff] [blame] | 409 | n1 = n1 + 1; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 410 | buf = PyMem_New(wchar_t, n1); |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 411 | if (!buf) { |
| 412 | PyErr_NoMemory(); |
| 413 | goto exit; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 414 | } |
Serhiy Storchaka | be487a6 | 2017-03-06 21:21:41 +0200 | [diff] [blame] | 415 | errno = 0; |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 416 | n2 = wcsxfrm(buf, s, n1); |
Benjamin Peterson | ad4a0cc | 2017-03-07 22:24:44 -0800 | [diff] [blame] | 417 | if (errno && errno != ERANGE) { |
Serhiy Storchaka | be487a6 | 2017-03-06 21:21:41 +0200 | [diff] [blame] | 418 | PyErr_SetFromErrno(PyExc_OSError); |
| 419 | goto exit; |
| 420 | } |
Victor Stinner | 2c5d3cb | 2011-10-11 22:35:52 +0200 | [diff] [blame] | 421 | if (n2 >= (size_t)n1) { |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 422 | /* more space needed */ |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 423 | wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t)); |
| 424 | if (!new_buf) { |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 425 | PyErr_NoMemory(); |
| 426 | goto exit; |
| 427 | } |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 428 | buf = new_buf; |
Serhiy Storchaka | be487a6 | 2017-03-06 21:21:41 +0200 | [diff] [blame] | 429 | errno = 0; |
Martin v. Löwis | db1c399 | 2009-05-23 10:38:26 +0000 | [diff] [blame] | 430 | n2 = wcsxfrm(buf, s, n2+1); |
Serhiy Storchaka | be487a6 | 2017-03-06 21:21:41 +0200 | [diff] [blame] | 431 | if (errno) { |
| 432 | PyErr_SetFromErrno(PyExc_OSError); |
| 433 | goto exit; |
| 434 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 435 | } |
| 436 | result = PyUnicode_FromWideChar(buf, n2); |
Victor Stinner | 6364927 | 2011-09-29 23:32:06 +0200 | [diff] [blame] | 437 | exit: |
Serhiy Storchaka | be487a6 | 2017-03-06 21:21:41 +0200 | [diff] [blame] | 438 | PyMem_Free(buf); |
| 439 | PyMem_Free(s); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 440 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 441 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 442 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 443 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 444 | #if defined(MS_WINDOWS) |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 445 | |
| 446 | /*[clinic input] |
| 447 | _locale._getdefaultlocale |
| 448 | |
| 449 | [clinic start generated code]*/ |
| 450 | |
| 451 | static PyObject * |
| 452 | _locale__getdefaultlocale_impl(PyObject *module) |
| 453 | /*[clinic end generated code: output=e6254088579534c2 input=003ea41acd17f7c7]*/ |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 454 | { |
Victor Stinner | 9e4994d | 2018-08-28 23:26:33 +0200 | [diff] [blame] | 455 | char encoding[20]; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 456 | char locale[100]; |
| 457 | |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 458 | PyOS_snprintf(encoding, sizeof(encoding), "cp%u", GetACP()); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 459 | |
| 460 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
| 461 | LOCALE_SISO639LANGNAME, |
| 462 | locale, sizeof(locale))) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 463 | Py_ssize_t i = strlen(locale); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 464 | locale[i++] = '_'; |
| 465 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
| 466 | LOCALE_SISO3166CTRYNAME, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 467 | locale+i, (int)(sizeof(locale)-i))) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 468 | return Py_BuildValue("ss", locale, encoding); |
| 469 | } |
| 470 | |
| 471 | /* If we end up here, this windows version didn't know about |
| 472 | ISO639/ISO3166 names (it's probably Windows 95). Return the |
| 473 | Windows language identifier instead (a hexadecimal number) */ |
| 474 | |
| 475 | locale[0] = '0'; |
| 476 | locale[1] = 'x'; |
| 477 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, |
| 478 | locale+2, sizeof(locale)-2)) { |
| 479 | return Py_BuildValue("ss", locale, encoding); |
| 480 | } |
| 481 | |
| 482 | /* cannot determine the language code (very unlikely) */ |
| 483 | Py_INCREF(Py_None); |
| 484 | return Py_BuildValue("Os", Py_None, encoding); |
| 485 | } |
| 486 | #endif |
| 487 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 488 | #ifdef HAVE_LANGINFO_H |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 489 | #define LANGINFO(X) {#X, X} |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 490 | static struct langinfo_constant{ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | char* name; |
| 492 | int value; |
| 493 | } langinfo_constants[] = |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 494 | { |
| 495 | /* These constants should exist on any langinfo implementation */ |
| 496 | LANGINFO(DAY_1), |
| 497 | LANGINFO(DAY_2), |
| 498 | LANGINFO(DAY_3), |
| 499 | LANGINFO(DAY_4), |
| 500 | LANGINFO(DAY_5), |
| 501 | LANGINFO(DAY_6), |
| 502 | LANGINFO(DAY_7), |
| 503 | |
| 504 | LANGINFO(ABDAY_1), |
| 505 | LANGINFO(ABDAY_2), |
| 506 | LANGINFO(ABDAY_3), |
| 507 | LANGINFO(ABDAY_4), |
| 508 | LANGINFO(ABDAY_5), |
| 509 | LANGINFO(ABDAY_6), |
| 510 | LANGINFO(ABDAY_7), |
| 511 | |
| 512 | LANGINFO(MON_1), |
| 513 | LANGINFO(MON_2), |
| 514 | LANGINFO(MON_3), |
| 515 | LANGINFO(MON_4), |
| 516 | LANGINFO(MON_5), |
| 517 | LANGINFO(MON_6), |
| 518 | LANGINFO(MON_7), |
| 519 | LANGINFO(MON_8), |
| 520 | LANGINFO(MON_9), |
| 521 | LANGINFO(MON_10), |
| 522 | LANGINFO(MON_11), |
| 523 | LANGINFO(MON_12), |
| 524 | |
| 525 | LANGINFO(ABMON_1), |
| 526 | LANGINFO(ABMON_2), |
| 527 | LANGINFO(ABMON_3), |
| 528 | LANGINFO(ABMON_4), |
| 529 | LANGINFO(ABMON_5), |
| 530 | LANGINFO(ABMON_6), |
| 531 | LANGINFO(ABMON_7), |
| 532 | LANGINFO(ABMON_8), |
| 533 | LANGINFO(ABMON_9), |
| 534 | LANGINFO(ABMON_10), |
| 535 | LANGINFO(ABMON_11), |
| 536 | LANGINFO(ABMON_12), |
| 537 | |
| 538 | #ifdef RADIXCHAR |
| 539 | /* The following are not available with glibc 2.0 */ |
| 540 | LANGINFO(RADIXCHAR), |
| 541 | LANGINFO(THOUSEP), |
| 542 | /* YESSTR and NOSTR are deprecated in glibc, since they are |
| 543 | a special case of message translation, which should be rather |
| 544 | done using gettext. So we don't expose it to Python in the |
| 545 | first place. |
| 546 | LANGINFO(YESSTR), |
| 547 | LANGINFO(NOSTR), |
| 548 | */ |
| 549 | LANGINFO(CRNCYSTR), |
| 550 | #endif |
| 551 | |
| 552 | LANGINFO(D_T_FMT), |
| 553 | LANGINFO(D_FMT), |
| 554 | LANGINFO(T_FMT), |
| 555 | LANGINFO(AM_STR), |
| 556 | LANGINFO(PM_STR), |
| 557 | |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 558 | /* The following constants are available only with XPG4, but... |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 559 | OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have |
| 560 | a few of the others. |
| 561 | Solution: ifdef-test them all. */ |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 562 | #ifdef CODESET |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 563 | LANGINFO(CODESET), |
Martin v. Löwis | 496f9e4 | 2002-03-27 12:15:57 +0000 | [diff] [blame] | 564 | #endif |
| 565 | #ifdef T_FMT_AMPM |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 566 | LANGINFO(T_FMT_AMPM), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 567 | #endif |
| 568 | #ifdef ERA |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 569 | LANGINFO(ERA), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 570 | #endif |
| 571 | #ifdef ERA_D_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 572 | LANGINFO(ERA_D_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 573 | #endif |
| 574 | #ifdef ERA_D_T_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 575 | LANGINFO(ERA_D_T_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 576 | #endif |
| 577 | #ifdef ERA_T_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 578 | LANGINFO(ERA_T_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 579 | #endif |
| 580 | #ifdef ALT_DIGITS |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 581 | LANGINFO(ALT_DIGITS), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 582 | #endif |
| 583 | #ifdef YESEXPR |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 584 | LANGINFO(YESEXPR), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 585 | #endif |
| 586 | #ifdef NOEXPR |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 587 | LANGINFO(NOEXPR), |
| 588 | #endif |
| 589 | #ifdef _DATE_FMT |
| 590 | /* This is not available in all glibc versions that have CODESET. */ |
| 591 | LANGINFO(_DATE_FMT), |
| 592 | #endif |
| 593 | {0, 0} |
| 594 | }; |
| 595 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 596 | /*[clinic input] |
| 597 | _locale.nl_langinfo |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 598 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 599 | key as item: int |
| 600 | / |
| 601 | |
| 602 | Return the value for the locale information associated with key. |
| 603 | [clinic start generated code]*/ |
| 604 | |
| 605 | static PyObject * |
| 606 | _locale_nl_langinfo_impl(PyObject *module, int item) |
| 607 | /*[clinic end generated code: output=6aea457b47e077a3 input=00798143eecfeddc]*/ |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 608 | { |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 609 | int i; |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 610 | /* Check whether this is a supported constant. GNU libc sometimes |
| 611 | returns numeric values in the char* return value, which would |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 612 | crash PyUnicode_FromString. */ |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 613 | for (i = 0; langinfo_constants[i].name; i++) |
Hye-Shik Chang | c3a87b8 | 2004-03-21 19:34:30 +0000 | [diff] [blame] | 614 | if (langinfo_constants[i].value == item) { |
| 615 | /* Check NULL as a workaround for GNU libc's returning NULL |
| 616 | instead of an empty string for nl_langinfo(ERA). */ |
| 617 | const char *result = nl_langinfo(item); |
Neal Norwitz | 3d7a90d | 2007-10-27 05:40:06 +0000 | [diff] [blame] | 618 | result = result != NULL ? result : ""; |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 619 | return PyUnicode_DecodeLocale(result, NULL); |
Hye-Shik Chang | c3a87b8 | 2004-03-21 19:34:30 +0000 | [diff] [blame] | 620 | } |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 621 | PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant"); |
| 622 | return NULL; |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 623 | } |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 624 | #endif /* HAVE_LANGINFO_H */ |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 625 | |
| 626 | #ifdef HAVE_LIBINTL_H |
| 627 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 628 | /*[clinic input] |
| 629 | _locale.gettext |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 630 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 631 | msg as in: str |
| 632 | / |
| 633 | |
| 634 | gettext(msg) -> string |
| 635 | |
| 636 | Return translation of msg. |
| 637 | [clinic start generated code]*/ |
| 638 | |
| 639 | static PyObject * |
| 640 | _locale_gettext_impl(PyObject *module, const char *in) |
| 641 | /*[clinic end generated code: output=493bb4b38a4704fe input=949fc8efc2bb3bc3]*/ |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 642 | { |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 643 | return PyUnicode_DecodeLocale(gettext(in), NULL); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 646 | /*[clinic input] |
| 647 | _locale.dgettext |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 648 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 649 | domain: str(accept={str, NoneType}) |
| 650 | msg as in: str |
| 651 | / |
| 652 | |
| 653 | dgettext(domain, msg) -> string |
| 654 | |
| 655 | Return translation of msg in domain. |
| 656 | [clinic start generated code]*/ |
| 657 | |
| 658 | static PyObject * |
| 659 | _locale_dgettext_impl(PyObject *module, const char *domain, const char *in) |
| 660 | /*[clinic end generated code: output=3c0cd5287b972c8f input=a277388a635109d8]*/ |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 661 | { |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 662 | return PyUnicode_DecodeLocale(dgettext(domain, in), NULL); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 665 | /*[clinic input] |
| 666 | _locale.dcgettext |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 667 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 668 | domain: str(accept={str, NoneType}) |
| 669 | msg as msgid: str |
| 670 | category: int |
| 671 | / |
| 672 | |
| 673 | Return translation of msg in domain and category. |
| 674 | [clinic start generated code]*/ |
| 675 | |
| 676 | static PyObject * |
| 677 | _locale_dcgettext_impl(PyObject *module, const char *domain, |
| 678 | const char *msgid, int category) |
| 679 | /*[clinic end generated code: output=0f4cc4fce0aa283f input=ec5f8fed4336de67]*/ |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 680 | { |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 681 | return PyUnicode_DecodeLocale(dcgettext(domain,msgid,category), NULL); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 684 | /*[clinic input] |
| 685 | _locale.textdomain |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 686 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 687 | domain: str(accept={str, NoneType}) |
| 688 | / |
| 689 | |
| 690 | Set the C library's textdmain to domain, returning the new domain. |
| 691 | [clinic start generated code]*/ |
| 692 | |
| 693 | static PyObject * |
| 694 | _locale_textdomain_impl(PyObject *module, const char *domain) |
| 695 | /*[clinic end generated code: output=7992df06aadec313 input=66359716f5eb1d38]*/ |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | domain = textdomain(domain); |
| 698 | if (!domain) { |
| 699 | PyErr_SetFromErrno(PyExc_OSError); |
| 700 | return NULL; |
| 701 | } |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 702 | return PyUnicode_DecodeLocale(domain, NULL); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 705 | /*[clinic input] |
| 706 | _locale.bindtextdomain |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 707 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 708 | domain: str |
| 709 | dir as dirname_obj: object |
| 710 | / |
| 711 | |
| 712 | Bind the C library's domain to dir. |
| 713 | [clinic start generated code]*/ |
| 714 | |
| 715 | static PyObject * |
| 716 | _locale_bindtextdomain_impl(PyObject *module, const char *domain, |
| 717 | PyObject *dirname_obj) |
| 718 | /*[clinic end generated code: output=6d6f3c7b345d785c input=c0dff085acfe272b]*/ |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 719 | { |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 720 | const char *dirname, *current_dirname; |
| 721 | PyObject *dirname_bytes = NULL, *result; |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | if (!strlen(domain)) { |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 724 | PyErr_SetString(get_locale_state(module)->Error, |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 725 | "domain must be a non-empty string"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | return 0; |
| 727 | } |
Victor Stinner | 9e19ca4 | 2010-06-11 22:09:51 +0000 | [diff] [blame] | 728 | if (dirname_obj != Py_None) { |
| 729 | if (!PyUnicode_FSConverter(dirname_obj, &dirname_bytes)) |
| 730 | return NULL; |
| 731 | dirname = PyBytes_AsString(dirname_bytes); |
| 732 | } else { |
| 733 | dirname_bytes = NULL; |
| 734 | dirname = NULL; |
| 735 | } |
| 736 | current_dirname = bindtextdomain(domain, dirname); |
| 737 | if (current_dirname == NULL) { |
| 738 | Py_XDECREF(dirname_bytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | PyErr_SetFromErrno(PyExc_OSError); |
| 740 | return NULL; |
| 741 | } |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 742 | result = PyUnicode_DecodeLocale(current_dirname, NULL); |
Victor Stinner | 9e19ca4 | 2010-06-11 22:09:51 +0000 | [diff] [blame] | 743 | Py_XDECREF(dirname_bytes); |
| 744 | return result; |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 747 | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 748 | |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 749 | /*[clinic input] |
| 750 | _locale.bind_textdomain_codeset |
| 751 | |
| 752 | domain: str |
| 753 | codeset: str(accept={str, NoneType}) |
| 754 | / |
| 755 | |
| 756 | Bind the C library's domain to codeset. |
| 757 | [clinic start generated code]*/ |
| 758 | |
| 759 | static PyObject * |
| 760 | _locale_bind_textdomain_codeset_impl(PyObject *module, const char *domain, |
| 761 | const char *codeset) |
| 762 | /*[clinic end generated code: output=fa452f9c8b1b9e89 input=23fbe3540400f259]*/ |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 763 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | codeset = bind_textdomain_codeset(domain, codeset); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 765 | if (codeset) { |
Victor Stinner | a9c895d | 2012-02-14 02:33:38 +0100 | [diff] [blame] | 766 | return PyUnicode_DecodeLocale(codeset, NULL); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 767 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | Py_RETURN_NONE; |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 769 | } |
Victor Stinner | b62bdf7 | 2020-10-31 01:32:11 +0100 | [diff] [blame] | 770 | #endif // HAVE_BIND_TEXTDOMAIN_CODESET |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 771 | |
Victor Stinner | b62bdf7 | 2020-10-31 01:32:11 +0100 | [diff] [blame] | 772 | #endif // HAVE_LIBINTL_H |
| 773 | |
| 774 | |
| 775 | /*[clinic input] |
| 776 | _locale._get_locale_encoding |
| 777 | |
| 778 | Get the current locale encoding. |
| 779 | [clinic start generated code]*/ |
| 780 | |
| 781 | static PyObject * |
| 782 | _locale__get_locale_encoding_impl(PyObject *module) |
| 783 | /*[clinic end generated code: output=e8e2f6f6f184591a input=513d9961d2f45c76]*/ |
| 784 | { |
Victor Stinner | 82458b6 | 2020-11-01 20:59:35 +0100 | [diff] [blame] | 785 | return _Py_GetLocaleEncodingObject(); |
Victor Stinner | b62bdf7 | 2020-10-31 01:32:11 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 788 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 789 | static struct PyMethodDef PyLocale_Methods[] = { |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 790 | _LOCALE_SETLOCALE_METHODDEF |
| 791 | _LOCALE_LOCALECONV_METHODDEF |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 792 | #ifdef HAVE_WCSCOLL |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 793 | _LOCALE_STRCOLL_METHODDEF |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 794 | #endif |
| 795 | #ifdef HAVE_WCSXFRM |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 796 | _LOCALE_STRXFRM_METHODDEF |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 797 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | #if defined(MS_WINDOWS) |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 799 | _LOCALE__GETDEFAULTLOCALE_METHODDEF |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 800 | #endif |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 801 | #ifdef HAVE_LANGINFO_H |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 802 | _LOCALE_NL_LANGINFO_METHODDEF |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 803 | #endif |
Martin v. Löwis | c6a7d7e | 2002-05-02 12:16:29 +0000 | [diff] [blame] | 804 | #ifdef HAVE_LIBINTL_H |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 805 | _LOCALE_GETTEXT_METHODDEF |
| 806 | _LOCALE_DGETTEXT_METHODDEF |
| 807 | _LOCALE_DCGETTEXT_METHODDEF |
| 808 | _LOCALE_TEXTDOMAIN_METHODDEF |
| 809 | _LOCALE_BINDTEXTDOMAIN_METHODDEF |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 810 | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
Zackery Spytz | bbceef6 | 2020-07-15 03:07:34 -0600 | [diff] [blame] | 811 | _LOCALE_BIND_TEXTDOMAIN_CODESET_METHODDEF |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 812 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | #endif |
Victor Stinner | b62bdf7 | 2020-10-31 01:32:11 +0100 | [diff] [blame] | 814 | _LOCALE__GET_LOCALE_ENCODING_METHODDEF |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 815 | {NULL, NULL} |
| 816 | }; |
| 817 | |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 818 | static int |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 819 | _locale_exec(PyObject *module) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 820 | { |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 821 | #ifdef HAVE_LANGINFO_H |
| 822 | int i; |
| 823 | #endif |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 824 | #define ADD_INT(module, value) \ |
| 825 | do { \ |
| 826 | if (PyModule_AddIntConstant(module, #value, value) < 0) { \ |
| 827 | return -1; \ |
| 828 | } \ |
| 829 | } while (0) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 830 | |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 831 | ADD_INT(module, LC_CTYPE); |
| 832 | ADD_INT(module, LC_TIME); |
| 833 | ADD_INT(module, LC_COLLATE); |
| 834 | ADD_INT(module, LC_MONETARY); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 835 | |
Guido van Rossum | 8d9c2e3 | 1997-12-09 19:35:11 +0000 | [diff] [blame] | 836 | #ifdef LC_MESSAGES |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 837 | ADD_INT(module, LC_MESSAGES); |
Guido van Rossum | 8d9c2e3 | 1997-12-09 19:35:11 +0000 | [diff] [blame] | 838 | #endif /* LC_MESSAGES */ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 839 | |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 840 | ADD_INT(module, LC_NUMERIC); |
| 841 | ADD_INT(module, LC_ALL); |
| 842 | ADD_INT(module, CHAR_MAX); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 843 | |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 844 | _locale_state *state = get_locale_state(module); |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 845 | state->Error = PyErr_NewException("locale.Error", NULL, NULL); |
| 846 | if (state->Error == NULL) { |
| 847 | return -1; |
Christian Heimes | ff4fddd | 2016-09-09 00:24:12 +0200 | [diff] [blame] | 848 | } |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 849 | Py_INCREF(get_locale_state(module)->Error); |
| 850 | if (PyModule_AddObject(module, "Error", get_locale_state(module)->Error) < 0) { |
| 851 | Py_DECREF(get_locale_state(module)->Error); |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 852 | return -1; |
| 853 | } |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 854 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 855 | #ifdef HAVE_LANGINFO_H |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 856 | for (i = 0; langinfo_constants[i].name; i++) { |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 857 | if (PyModule_AddIntConstant(module, |
| 858 | langinfo_constants[i].name, |
| 859 | langinfo_constants[i].value) < 0) { |
| 860 | return -1; |
| 861 | } |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 862 | } |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 863 | #endif |
Christian Heimes | ff4fddd | 2016-09-09 00:24:12 +0200 | [diff] [blame] | 864 | |
| 865 | if (PyErr_Occurred()) { |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 866 | return -1; |
Christian Heimes | ff4fddd | 2016-09-09 00:24:12 +0200 | [diff] [blame] | 867 | } |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 868 | return 0; |
Hai Shi | 7a6f3bc | 2020-04-03 02:00:47 +0800 | [diff] [blame] | 869 | |
| 870 | #undef ADD_INT |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | static struct PyModuleDef_Slot _locale_slots[] = { |
| 874 | {Py_mod_exec, _locale_exec}, |
| 875 | {0, NULL} |
| 876 | }; |
| 877 | |
| 878 | static int |
Hai Shi | 13397ee | 2020-03-20 01:11:33 +0800 | [diff] [blame] | 879 | locale_traverse(PyObject *module, visitproc visit, void *arg) |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 880 | { |
Hai Shi | 13397ee | 2020-03-20 01:11:33 +0800 | [diff] [blame] | 881 | _locale_state *state = get_locale_state(module); |
Victor Stinner | 5b1ef20 | 2020-03-17 18:09:46 +0100 | [diff] [blame] | 882 | Py_VISIT(state->Error); |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static int |
Hai Shi | 13397ee | 2020-03-20 01:11:33 +0800 | [diff] [blame] | 887 | locale_clear(PyObject *module) |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 888 | { |
Hai Shi | 13397ee | 2020-03-20 01:11:33 +0800 | [diff] [blame] | 889 | _locale_state *state = get_locale_state(module); |
Victor Stinner | 5b1ef20 | 2020-03-17 18:09:46 +0100 | [diff] [blame] | 890 | Py_CLEAR(state->Error); |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static void |
Hai Shi | 13397ee | 2020-03-20 01:11:33 +0800 | [diff] [blame] | 895 | locale_free(PyObject *module) |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 896 | { |
Hai Shi | 13397ee | 2020-03-20 01:11:33 +0800 | [diff] [blame] | 897 | locale_clear(module); |
Hai Shi | a158168 | 2020-03-12 00:46:06 +0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | static struct PyModuleDef _localemodule = { |
| 901 | PyModuleDef_HEAD_INIT, |
| 902 | "_locale", |
| 903 | locale__doc__, |
| 904 | sizeof(_locale_state), |
| 905 | PyLocale_Methods, |
| 906 | _locale_slots, |
| 907 | locale_traverse, |
| 908 | locale_clear, |
| 909 | (freefunc)locale_free, |
| 910 | }; |
| 911 | |
| 912 | PyMODINIT_FUNC |
| 913 | PyInit__locale(void) |
| 914 | { |
| 915 | return PyModuleDef_Init(&_localemodule); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 916 | } |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | /* |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 919 | Local variables: |
| 920 | c-basic-offset: 4 |
| 921 | indent-tabs-mode: nil |
| 922 | End: |
| 923 | */ |