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