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