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