Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 239a218 | 1998-04-28 16:08:19 +0000 | [diff] [blame] | 2 | Copyright (C) 1997 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) |
| 218 | free (Py_FileSystemDefaultEncoding); |
| 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* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 248 | PyLocale_localeconv(PyObject* self, PyObject* args) |
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 | |
| 254 | if (!PyArg_NoArgs(args)) |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 255 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 256 | |
| 257 | result = PyDict_New(); |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 258 | if (!result) |
| 259 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 260 | |
| 261 | /* if LC_NUMERIC is different in the C library, use saved value */ |
| 262 | l = localeconv(); |
| 263 | |
| 264 | /* hopefully, the localeconv result survives the C library calls |
| 265 | involved herein */ |
| 266 | |
| 267 | #define RESULT_STRING(s)\ |
| 268 | x = PyString_FromString(l->s);\ |
| 269 | if (!x) goto failed;\ |
| 270 | PyDict_SetItemString(result, #s, x);\ |
| 271 | Py_XDECREF(x) |
| 272 | |
| 273 | #define RESULT_INT(i)\ |
| 274 | x = PyInt_FromLong(l->i);\ |
| 275 | if (!x) goto failed;\ |
| 276 | PyDict_SetItemString(result, #i, x);\ |
| 277 | Py_XDECREF(x) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 278 | |
| 279 | /* Numeric information */ |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 280 | if (saved_numeric){ |
| 281 | /* cannot use localeconv results */ |
| 282 | PyDict_SetItemString(result, "decimal_point", decimal_point); |
| 283 | PyDict_SetItemString(result, "grouping", grouping); |
| 284 | PyDict_SetItemString(result, "thousands_sep", thousands_sep); |
| 285 | } else { |
| 286 | RESULT_STRING(decimal_point); |
| 287 | RESULT_STRING(thousands_sep); |
| 288 | x = copy_grouping(l->grouping); |
| 289 | if (!x) |
| 290 | goto failed; |
| 291 | PyDict_SetItemString(result, "grouping", x); |
| 292 | Py_XDECREF(x); |
| 293 | } |
| 294 | |
| 295 | /* Monetary information */ |
| 296 | RESULT_STRING(int_curr_symbol); |
| 297 | RESULT_STRING(currency_symbol); |
| 298 | RESULT_STRING(mon_decimal_point); |
| 299 | RESULT_STRING(mon_thousands_sep); |
| 300 | x = copy_grouping(l->mon_grouping); |
| 301 | if (!x) |
| 302 | goto failed; |
| 303 | PyDict_SetItemString(result, "mon_grouping", x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 304 | Py_XDECREF(x); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 305 | RESULT_STRING(positive_sign); |
| 306 | RESULT_STRING(negative_sign); |
| 307 | RESULT_INT(int_frac_digits); |
| 308 | RESULT_INT(frac_digits); |
| 309 | RESULT_INT(p_cs_precedes); |
| 310 | RESULT_INT(p_sep_by_space); |
| 311 | RESULT_INT(n_cs_precedes); |
| 312 | RESULT_INT(n_sep_by_space); |
| 313 | RESULT_INT(p_sign_posn); |
| 314 | RESULT_INT(n_sign_posn); |
| 315 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 316 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 317 | failed: |
| 318 | Py_XDECREF(result); |
| 319 | Py_XDECREF(x); |
| 320 | return NULL; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 323 | static char strcoll__doc__[] = |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 324 | "string,string -> int. Compares two strings according to the locale." |
| 325 | ; |
| 326 | |
| 327 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 328 | PyLocale_strcoll(PyObject* self, PyObject* args) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 329 | { |
| 330 | char *s1,*s2; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 331 | |
| 332 | if (!PyArg_ParseTuple(args, "ss:strcoll", &s1, &s2)) |
| 333 | return NULL; |
| 334 | return PyInt_FromLong(strcoll(s1, s2)); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 337 | static char strxfrm__doc__[] = |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 338 | "string -> string. Returns a string that behaves for cmp locale-aware." |
| 339 | ; |
| 340 | |
| 341 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 342 | PyLocale_strxfrm(PyObject* self, PyObject* args) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 343 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 344 | char *s, *buf; |
| 345 | size_t n1, n2; |
| 346 | PyObject *result; |
| 347 | |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 348 | if (!PyArg_ParseTuple(args, "s:strxfrm", &s)) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 349 | return NULL; |
| 350 | |
| 351 | /* assume no change in size, first */ |
| 352 | n1 = strlen(s) + 1; |
| 353 | buf = PyMem_Malloc(n1); |
| 354 | if (!buf) |
| 355 | return PyErr_NoMemory(); |
| 356 | n2 = strxfrm(buf, s, n1); |
| 357 | if (n2 > n1) { |
| 358 | /* more space needed */ |
| 359 | buf = PyMem_Realloc(buf, n2); |
| 360 | if (!buf) |
| 361 | return PyErr_NoMemory(); |
| 362 | strxfrm(buf, s, n2); |
| 363 | } |
| 364 | result = PyString_FromString(buf); |
| 365 | PyMem_Free(buf); |
| 366 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 369 | #if defined(MS_WIN32) |
| 370 | static PyObject* |
| 371 | PyLocale_getdefaultlocale(PyObject* self, PyObject* args) |
| 372 | { |
| 373 | char encoding[100]; |
| 374 | char locale[100]; |
| 375 | |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 376 | if (!PyArg_NoArgs(args)) |
| 377 | return NULL; |
| 378 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 379 | sprintf(encoding, "cp%d", GetACP()); |
| 380 | |
| 381 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
| 382 | LOCALE_SISO639LANGNAME, |
| 383 | locale, sizeof(locale))) { |
| 384 | int i = strlen(locale); |
| 385 | locale[i++] = '_'; |
| 386 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
| 387 | LOCALE_SISO3166CTRYNAME, |
| 388 | locale+i, sizeof(locale)-i)) |
| 389 | return Py_BuildValue("ss", locale, encoding); |
| 390 | } |
| 391 | |
| 392 | /* If we end up here, this windows version didn't know about |
| 393 | ISO639/ISO3166 names (it's probably Windows 95). Return the |
| 394 | Windows language identifier instead (a hexadecimal number) */ |
| 395 | |
| 396 | locale[0] = '0'; |
| 397 | locale[1] = 'x'; |
| 398 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, |
| 399 | locale+2, sizeof(locale)-2)) { |
| 400 | return Py_BuildValue("ss", locale, encoding); |
| 401 | } |
| 402 | |
| 403 | /* cannot determine the language code (very unlikely) */ |
| 404 | Py_INCREF(Py_None); |
| 405 | return Py_BuildValue("Os", Py_None, encoding); |
| 406 | } |
| 407 | #endif |
| 408 | |
Jack Jansen | 307d7a4 | 2000-07-15 22:31:45 +0000 | [diff] [blame] | 409 | #if defined(macintosh) |
| 410 | static PyObject* |
| 411 | PyLocale_getdefaultlocale(PyObject* self, PyObject* args) |
| 412 | { |
| 413 | return Py_BuildValue("Os", Py_None, PyMac_getscript()); |
| 414 | } |
| 415 | #endif |
| 416 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 417 | #ifdef HAVE_LANGINFO_H |
| 418 | static char nl_langinfo__doc__[] = |
| 419 | "nl_langinfo(key) -> string\n" |
| 420 | "Return the value for the locale information associated with key." |
| 421 | ; |
| 422 | |
| 423 | static PyObject* |
| 424 | PyLocale_nl_langinfo(PyObject* self, PyObject* args) |
| 425 | { |
| 426 | int item; |
| 427 | if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item)) |
| 428 | return NULL; |
| 429 | return PyString_FromString(nl_langinfo(item)); |
| 430 | } |
| 431 | #endif |
| 432 | |
| 433 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 434 | static struct PyMethodDef PyLocale_Methods[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 435 | {"setlocale", (PyCFunction) PyLocale_setlocale, |
| 436 | METH_VARARGS, setlocale__doc__}, |
| 437 | {"localeconv", (PyCFunction) PyLocale_localeconv, |
| 438 | 0, localeconv__doc__}, |
| 439 | {"strcoll", (PyCFunction) PyLocale_strcoll, |
| 440 | METH_VARARGS, strcoll__doc__}, |
| 441 | {"strxfrm", (PyCFunction) PyLocale_strxfrm, |
| 442 | METH_VARARGS, strxfrm__doc__}, |
Jack Jansen | 307d7a4 | 2000-07-15 22:31:45 +0000 | [diff] [blame] | 443 | #if defined(MS_WIN32) || defined(macintosh) |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 444 | {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, 0}, |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 445 | #endif |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 446 | #ifdef HAVE_LANGINFO_H |
| 447 | {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo, |
| 448 | METH_VARARGS, nl_langinfo__doc__}, |
| 449 | #endif |
| 450 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 451 | {NULL, NULL} |
| 452 | }; |
| 453 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 454 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 455 | init_locale(void) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 456 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 457 | PyObject *m, *d, *x; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 458 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 459 | m = Py_InitModule("_locale", PyLocale_Methods); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 460 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 461 | d = PyModule_GetDict(m); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 462 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 463 | x = PyInt_FromLong(LC_CTYPE); |
| 464 | PyDict_SetItemString(d, "LC_CTYPE", x); |
| 465 | Py_XDECREF(x); |
| 466 | |
| 467 | x = PyInt_FromLong(LC_TIME); |
| 468 | PyDict_SetItemString(d, "LC_TIME", x); |
| 469 | Py_XDECREF(x); |
| 470 | |
| 471 | x = PyInt_FromLong(LC_COLLATE); |
| 472 | PyDict_SetItemString(d, "LC_COLLATE", x); |
| 473 | Py_XDECREF(x); |
| 474 | |
| 475 | x = PyInt_FromLong(LC_MONETARY); |
| 476 | PyDict_SetItemString(d, "LC_MONETARY", x); |
| 477 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | 8d9c2e3 | 1997-12-09 19:35:11 +0000 | [diff] [blame] | 479 | #ifdef LC_MESSAGES |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 480 | x = PyInt_FromLong(LC_MESSAGES); |
| 481 | PyDict_SetItemString(d, "LC_MESSAGES", x); |
| 482 | Py_XDECREF(x); |
Guido van Rossum | 8d9c2e3 | 1997-12-09 19:35:11 +0000 | [diff] [blame] | 483 | #endif /* LC_MESSAGES */ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 484 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 485 | x = PyInt_FromLong(LC_NUMERIC); |
| 486 | PyDict_SetItemString(d, "LC_NUMERIC", x); |
| 487 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 488 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 489 | x = PyInt_FromLong(LC_ALL); |
| 490 | PyDict_SetItemString(d, "LC_ALL", x); |
| 491 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 492 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 493 | x = PyInt_FromLong(CHAR_MAX); |
| 494 | PyDict_SetItemString(d, "CHAR_MAX", x); |
| 495 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 496 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 497 | Error = PyErr_NewException("locale.Error", NULL, NULL); |
| 498 | PyDict_SetItemString(d, "Error", Error); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 499 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 500 | x = PyString_FromString(locale__doc__); |
| 501 | PyDict_SetItemString(d, "__doc__", x); |
| 502 | Py_XDECREF(x); |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 503 | |
| 504 | #define ADDINT(X) PyModule_AddIntConstant(m, #X, X) |
| 505 | #ifdef HAVE_LANGINFO_H |
| 506 | /* These constants should exist on any langinfo implementation */ |
| 507 | ADDINT(DAY_1); |
| 508 | ADDINT(DAY_2); |
| 509 | ADDINT(DAY_3); |
| 510 | ADDINT(DAY_4); |
| 511 | ADDINT(DAY_5); |
| 512 | ADDINT(DAY_6); |
| 513 | ADDINT(DAY_7); |
| 514 | |
| 515 | ADDINT(ABDAY_1); |
| 516 | ADDINT(ABDAY_2); |
| 517 | ADDINT(ABDAY_3); |
| 518 | ADDINT(ABDAY_4); |
| 519 | ADDINT(ABDAY_5); |
| 520 | ADDINT(ABDAY_6); |
| 521 | ADDINT(ABDAY_7); |
| 522 | |
| 523 | ADDINT(MON_1); |
| 524 | ADDINT(MON_2); |
| 525 | ADDINT(MON_3); |
| 526 | ADDINT(MON_4); |
| 527 | ADDINT(MON_5); |
| 528 | ADDINT(MON_6); |
| 529 | ADDINT(MON_7); |
| 530 | ADDINT(MON_8); |
| 531 | ADDINT(MON_9); |
| 532 | ADDINT(MON_10); |
| 533 | ADDINT(MON_11); |
| 534 | ADDINT(MON_12); |
| 535 | |
| 536 | ADDINT(ABMON_1); |
| 537 | ADDINT(ABMON_2); |
| 538 | ADDINT(ABMON_3); |
| 539 | ADDINT(ABMON_4); |
| 540 | ADDINT(ABMON_5); |
| 541 | ADDINT(ABMON_6); |
| 542 | ADDINT(ABMON_7); |
| 543 | ADDINT(ABMON_8); |
| 544 | ADDINT(ABMON_9); |
| 545 | ADDINT(ABMON_10); |
| 546 | ADDINT(ABMON_11); |
| 547 | ADDINT(ABMON_12); |
| 548 | |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 549 | #ifdef RADIXCHAR |
| 550 | /* The following are not available with glibc 2.0 */ |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 551 | ADDINT(RADIXCHAR); |
| 552 | ADDINT(THOUSEP); |
| 553 | /* YESSTR and NOSTR are deprecated in glibc, since they are |
| 554 | a special case of message translation, which should be rather |
| 555 | done using gettext. So we don't expose it to Python in the |
| 556 | first place. |
| 557 | ADDINT(YESSTR); |
| 558 | ADDINT(NOSTR); |
| 559 | */ |
| 560 | ADDINT(CRNCYSTR); |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 561 | #endif |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 562 | |
| 563 | ADDINT(D_T_FMT); |
| 564 | ADDINT(D_FMT); |
| 565 | ADDINT(T_FMT); |
| 566 | ADDINT(AM_STR); |
| 567 | ADDINT(PM_STR); |
| 568 | |
| 569 | #ifdef CODESET |
| 570 | /* The following constants are available only with XPG4. */ |
| 571 | ADDINT(CODESET); |
| 572 | ADDINT(T_FMT_AMPM); |
| 573 | ADDINT(ERA); |
| 574 | ADDINT(ERA_D_FMT); |
| 575 | ADDINT(ERA_D_T_FMT); |
| 576 | ADDINT(ERA_T_FMT); |
| 577 | ADDINT(ALT_DIGITS); |
| 578 | ADDINT(YESEXPR); |
| 579 | ADDINT(NOEXPR); |
Martin v. Löwis | c4416d5 | 2001-08-10 19:41:45 +0000 | [diff] [blame] | 580 | #endif |
| 581 | #ifdef _DATE_FMT |
| 582 | /* This is not available in all glibc versions that have CODESET. */ |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 583 | ADDINT(_DATE_FMT); |
| 584 | #endif |
| 585 | #endif /* HAVE_LANGINFO_H */ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 586 | } |