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