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