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