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