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