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