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