| 1 | n/a | /*********************************************************** |
|---|
| 2 | n/a | Copyright (C) 1997, 2002, 2003, 2007, 2008 Martin von Loewis |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | Permission to use, copy, modify, and distribute this software and its |
|---|
| 5 | n/a | documentation for any purpose and without fee is hereby granted, |
|---|
| 6 | n/a | provided that the above copyright notice appear in all copies. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | This software comes with no warranty. Use at your own risk. |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | ******************************************************************/ |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | #define PY_SSIZE_T_CLEAN |
|---|
| 13 | n/a | #include "Python.h" |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | #include <stdio.h> |
|---|
| 16 | n/a | #include <locale.h> |
|---|
| 17 | n/a | #include <string.h> |
|---|
| 18 | n/a | #include <ctype.h> |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | #ifdef HAVE_ERRNO_H |
|---|
| 21 | n/a | #include <errno.h> |
|---|
| 22 | n/a | #endif |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | #ifdef HAVE_LANGINFO_H |
|---|
| 25 | n/a | #include <langinfo.h> |
|---|
| 26 | n/a | #endif |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | #ifdef HAVE_LIBINTL_H |
|---|
| 29 | n/a | #include <libintl.h> |
|---|
| 30 | n/a | #endif |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | #ifdef HAVE_WCHAR_H |
|---|
| 33 | n/a | #include <wchar.h> |
|---|
| 34 | n/a | #endif |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | #if defined(MS_WINDOWS) |
|---|
| 37 | n/a | #define WIN32_LEAN_AND_MEAN |
|---|
| 38 | n/a | #include <windows.h> |
|---|
| 39 | n/a | #endif |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | PyDoc_STRVAR(locale__doc__, "Support for POSIX locales."); |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | static PyObject *Error; |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | /* support functions for formatting floating point numbers */ |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | PyDoc_STRVAR(setlocale__doc__, |
|---|
| 48 | n/a | "(integer,string=None) -> string. Activates/queries locale processing."); |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | /* the grouping is terminated by either 0 or CHAR_MAX */ |
|---|
| 51 | n/a | static PyObject* |
|---|
| 52 | n/a | copy_grouping(const char* s) |
|---|
| 53 | n/a | { |
|---|
| 54 | n/a | int i; |
|---|
| 55 | n/a | PyObject *result, *val = NULL; |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | if (s[0] == '\0') { |
|---|
| 58 | n/a | /* empty string: no grouping at all */ |
|---|
| 59 | n/a | return PyList_New(0); |
|---|
| 60 | n/a | } |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++) |
|---|
| 63 | n/a | ; /* nothing */ |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | result = PyList_New(i+1); |
|---|
| 66 | n/a | if (!result) |
|---|
| 67 | n/a | return NULL; |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | i = -1; |
|---|
| 70 | n/a | do { |
|---|
| 71 | n/a | i++; |
|---|
| 72 | n/a | val = PyLong_FromLong(s[i]); |
|---|
| 73 | n/a | if (!val) |
|---|
| 74 | n/a | break; |
|---|
| 75 | n/a | if (PyList_SetItem(result, i, val)) { |
|---|
| 76 | n/a | Py_DECREF(val); |
|---|
| 77 | n/a | val = NULL; |
|---|
| 78 | n/a | break; |
|---|
| 79 | n/a | } |
|---|
| 80 | n/a | } while (s[i] != '\0' && s[i] != CHAR_MAX); |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | if (!val) { |
|---|
| 83 | n/a | Py_DECREF(result); |
|---|
| 84 | n/a | return NULL; |
|---|
| 85 | n/a | } |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | return result; |
|---|
| 88 | n/a | } |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | static PyObject* |
|---|
| 91 | n/a | PyLocale_setlocale(PyObject* self, PyObject* args) |
|---|
| 92 | n/a | { |
|---|
| 93 | n/a | int category; |
|---|
| 94 | n/a | char *locale = NULL, *result; |
|---|
| 95 | n/a | PyObject *result_object; |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale)) |
|---|
| 98 | n/a | return NULL; |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | #if defined(MS_WINDOWS) |
|---|
| 101 | n/a | if (category < LC_MIN || category > LC_MAX) |
|---|
| 102 | n/a | { |
|---|
| 103 | n/a | PyErr_SetString(Error, "invalid locale category"); |
|---|
| 104 | n/a | return NULL; |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | #endif |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | if (locale) { |
|---|
| 109 | n/a | /* set locale */ |
|---|
| 110 | n/a | result = setlocale(category, locale); |
|---|
| 111 | n/a | if (!result) { |
|---|
| 112 | n/a | /* operation failed, no setting was changed */ |
|---|
| 113 | n/a | PyErr_SetString(Error, "unsupported locale setting"); |
|---|
| 114 | n/a | return NULL; |
|---|
| 115 | n/a | } |
|---|
| 116 | n/a | result_object = PyUnicode_DecodeLocale(result, NULL); |
|---|
| 117 | n/a | if (!result_object) |
|---|
| 118 | n/a | return NULL; |
|---|
| 119 | n/a | } else { |
|---|
| 120 | n/a | /* get locale */ |
|---|
| 121 | n/a | result = setlocale(category, NULL); |
|---|
| 122 | n/a | if (!result) { |
|---|
| 123 | n/a | PyErr_SetString(Error, "locale query failed"); |
|---|
| 124 | n/a | return NULL; |
|---|
| 125 | n/a | } |
|---|
| 126 | n/a | result_object = PyUnicode_DecodeLocale(result, NULL); |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | return result_object; |
|---|
| 129 | n/a | } |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | PyDoc_STRVAR(localeconv__doc__, |
|---|
| 132 | n/a | "() -> dict. Returns numeric and monetary locale-specific parameters."); |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | static PyObject* |
|---|
| 135 | n/a | PyLocale_localeconv(PyObject* self) |
|---|
| 136 | n/a | { |
|---|
| 137 | n/a | PyObject* result; |
|---|
| 138 | n/a | struct lconv *l; |
|---|
| 139 | n/a | PyObject *x; |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | result = PyDict_New(); |
|---|
| 142 | n/a | if (!result) |
|---|
| 143 | n/a | return NULL; |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | /* if LC_NUMERIC is different in the C library, use saved value */ |
|---|
| 146 | n/a | l = localeconv(); |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | /* hopefully, the localeconv result survives the C library calls |
|---|
| 149 | n/a | involved herein */ |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | #define RESULT(key, obj)\ |
|---|
| 152 | n/a | do { \ |
|---|
| 153 | n/a | if (obj == NULL) \ |
|---|
| 154 | n/a | goto failed; \ |
|---|
| 155 | n/a | if (PyDict_SetItemString(result, key, obj) < 0) { \ |
|---|
| 156 | n/a | Py_DECREF(obj); \ |
|---|
| 157 | n/a | goto failed; \ |
|---|
| 158 | n/a | } \ |
|---|
| 159 | n/a | Py_DECREF(obj); \ |
|---|
| 160 | n/a | } while (0) |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | #define RESULT_STRING(s)\ |
|---|
| 163 | n/a | do { \ |
|---|
| 164 | n/a | x = PyUnicode_DecodeLocale(l->s, NULL); \ |
|---|
| 165 | n/a | RESULT(#s, x); \ |
|---|
| 166 | n/a | } while (0) |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | #define RESULT_INT(i)\ |
|---|
| 169 | n/a | do { \ |
|---|
| 170 | n/a | x = PyLong_FromLong(l->i); \ |
|---|
| 171 | n/a | RESULT(#i, x); \ |
|---|
| 172 | n/a | } while (0) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | /* Numeric information */ |
|---|
| 175 | n/a | RESULT_STRING(decimal_point); |
|---|
| 176 | n/a | RESULT_STRING(thousands_sep); |
|---|
| 177 | n/a | x = copy_grouping(l->grouping); |
|---|
| 178 | n/a | RESULT("grouping", x); |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | /* Monetary information */ |
|---|
| 181 | n/a | RESULT_STRING(int_curr_symbol); |
|---|
| 182 | n/a | RESULT_STRING(currency_symbol); |
|---|
| 183 | n/a | RESULT_STRING(mon_decimal_point); |
|---|
| 184 | n/a | RESULT_STRING(mon_thousands_sep); |
|---|
| 185 | n/a | x = copy_grouping(l->mon_grouping); |
|---|
| 186 | n/a | RESULT("mon_grouping", x); |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | RESULT_STRING(positive_sign); |
|---|
| 189 | n/a | RESULT_STRING(negative_sign); |
|---|
| 190 | n/a | RESULT_INT(int_frac_digits); |
|---|
| 191 | n/a | RESULT_INT(frac_digits); |
|---|
| 192 | n/a | RESULT_INT(p_cs_precedes); |
|---|
| 193 | n/a | RESULT_INT(p_sep_by_space); |
|---|
| 194 | n/a | RESULT_INT(n_cs_precedes); |
|---|
| 195 | n/a | RESULT_INT(n_sep_by_space); |
|---|
| 196 | n/a | RESULT_INT(p_sign_posn); |
|---|
| 197 | n/a | RESULT_INT(n_sign_posn); |
|---|
| 198 | n/a | return result; |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | failed: |
|---|
| 201 | n/a | Py_XDECREF(result); |
|---|
| 202 | n/a | return NULL; |
|---|
| 203 | n/a | } |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | #if defined(HAVE_WCSCOLL) |
|---|
| 206 | n/a | PyDoc_STRVAR(strcoll__doc__, |
|---|
| 207 | n/a | "string,string -> int. Compares two strings according to the locale."); |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | static PyObject* |
|---|
| 210 | n/a | PyLocale_strcoll(PyObject* self, PyObject* args) |
|---|
| 211 | n/a | { |
|---|
| 212 | n/a | PyObject *os1, *os2, *result = NULL; |
|---|
| 213 | n/a | wchar_t *ws1 = NULL, *ws2 = NULL; |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2)) |
|---|
| 216 | n/a | return NULL; |
|---|
| 217 | n/a | /* Convert the unicode strings to wchar[]. */ |
|---|
| 218 | n/a | ws1 = PyUnicode_AsWideCharString(os1, NULL); |
|---|
| 219 | n/a | if (ws1 == NULL) |
|---|
| 220 | n/a | goto done; |
|---|
| 221 | n/a | ws2 = PyUnicode_AsWideCharString(os2, NULL); |
|---|
| 222 | n/a | if (ws2 == NULL) |
|---|
| 223 | n/a | goto done; |
|---|
| 224 | n/a | /* Collate the strings. */ |
|---|
| 225 | n/a | result = PyLong_FromLong(wcscoll(ws1, ws2)); |
|---|
| 226 | n/a | done: |
|---|
| 227 | n/a | /* Deallocate everything. */ |
|---|
| 228 | n/a | if (ws1) PyMem_FREE(ws1); |
|---|
| 229 | n/a | if (ws2) PyMem_FREE(ws2); |
|---|
| 230 | n/a | return result; |
|---|
| 231 | n/a | } |
|---|
| 232 | n/a | #endif |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | #ifdef HAVE_WCSXFRM |
|---|
| 235 | n/a | PyDoc_STRVAR(strxfrm__doc__, |
|---|
| 236 | n/a | "strxfrm(string) -> string.\n\ |
|---|
| 237 | n/a | \n\ |
|---|
| 238 | n/a | Return a string that can be used as a key for locale-aware comparisons."); |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | static PyObject* |
|---|
| 241 | n/a | PyLocale_strxfrm(PyObject* self, PyObject* args) |
|---|
| 242 | n/a | { |
|---|
| 243 | n/a | PyObject *str; |
|---|
| 244 | n/a | Py_ssize_t n1; |
|---|
| 245 | n/a | wchar_t *s = NULL, *buf = NULL; |
|---|
| 246 | n/a | size_t n2; |
|---|
| 247 | n/a | PyObject *result = NULL; |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | if (!PyArg_ParseTuple(args, "U:strxfrm", &str)) |
|---|
| 250 | n/a | return NULL; |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | s = PyUnicode_AsWideCharString(str, &n1); |
|---|
| 253 | n/a | if (s == NULL) |
|---|
| 254 | n/a | goto exit; |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | /* assume no change in size, first */ |
|---|
| 257 | n/a | n1 = n1 + 1; |
|---|
| 258 | n/a | buf = PyMem_New(wchar_t, n1); |
|---|
| 259 | n/a | if (!buf) { |
|---|
| 260 | n/a | PyErr_NoMemory(); |
|---|
| 261 | n/a | goto exit; |
|---|
| 262 | n/a | } |
|---|
| 263 | n/a | n2 = wcsxfrm(buf, s, n1); |
|---|
| 264 | n/a | if (n2 >= (size_t)n1) { |
|---|
| 265 | n/a | /* more space needed */ |
|---|
| 266 | n/a | wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t)); |
|---|
| 267 | n/a | if (!new_buf) { |
|---|
| 268 | n/a | PyErr_NoMemory(); |
|---|
| 269 | n/a | goto exit; |
|---|
| 270 | n/a | } |
|---|
| 271 | n/a | buf = new_buf; |
|---|
| 272 | n/a | n2 = wcsxfrm(buf, s, n2+1); |
|---|
| 273 | n/a | } |
|---|
| 274 | n/a | result = PyUnicode_FromWideChar(buf, n2); |
|---|
| 275 | n/a | exit: |
|---|
| 276 | n/a | if (buf) |
|---|
| 277 | n/a | PyMem_Free(buf); |
|---|
| 278 | n/a | if (s) |
|---|
| 279 | n/a | PyMem_Free(s); |
|---|
| 280 | n/a | return result; |
|---|
| 281 | n/a | } |
|---|
| 282 | n/a | #endif |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | #if defined(MS_WINDOWS) |
|---|
| 285 | n/a | static PyObject* |
|---|
| 286 | n/a | PyLocale_getdefaultlocale(PyObject* self) |
|---|
| 287 | n/a | { |
|---|
| 288 | n/a | char encoding[100]; |
|---|
| 289 | n/a | char locale[100]; |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP()); |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
|---|
| 294 | n/a | LOCALE_SISO639LANGNAME, |
|---|
| 295 | n/a | locale, sizeof(locale))) { |
|---|
| 296 | n/a | Py_ssize_t i = strlen(locale); |
|---|
| 297 | n/a | locale[i++] = '_'; |
|---|
| 298 | n/a | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
|---|
| 299 | n/a | LOCALE_SISO3166CTRYNAME, |
|---|
| 300 | n/a | locale+i, (int)(sizeof(locale)-i))) |
|---|
| 301 | n/a | return Py_BuildValue("ss", locale, encoding); |
|---|
| 302 | n/a | } |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | /* If we end up here, this windows version didn't know about |
|---|
| 305 | n/a | ISO639/ISO3166 names (it's probably Windows 95). Return the |
|---|
| 306 | n/a | Windows language identifier instead (a hexadecimal number) */ |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | locale[0] = '0'; |
|---|
| 309 | n/a | locale[1] = 'x'; |
|---|
| 310 | n/a | if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, |
|---|
| 311 | n/a | locale+2, sizeof(locale)-2)) { |
|---|
| 312 | n/a | return Py_BuildValue("ss", locale, encoding); |
|---|
| 313 | n/a | } |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | /* cannot determine the language code (very unlikely) */ |
|---|
| 316 | n/a | Py_INCREF(Py_None); |
|---|
| 317 | n/a | return Py_BuildValue("Os", Py_None, encoding); |
|---|
| 318 | n/a | } |
|---|
| 319 | n/a | #endif |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | #ifdef HAVE_LANGINFO_H |
|---|
| 322 | n/a | #define LANGINFO(X) {#X, X} |
|---|
| 323 | n/a | static struct langinfo_constant{ |
|---|
| 324 | n/a | char* name; |
|---|
| 325 | n/a | int value; |
|---|
| 326 | n/a | } langinfo_constants[] = |
|---|
| 327 | n/a | { |
|---|
| 328 | n/a | /* These constants should exist on any langinfo implementation */ |
|---|
| 329 | n/a | LANGINFO(DAY_1), |
|---|
| 330 | n/a | LANGINFO(DAY_2), |
|---|
| 331 | n/a | LANGINFO(DAY_3), |
|---|
| 332 | n/a | LANGINFO(DAY_4), |
|---|
| 333 | n/a | LANGINFO(DAY_5), |
|---|
| 334 | n/a | LANGINFO(DAY_6), |
|---|
| 335 | n/a | LANGINFO(DAY_7), |
|---|
| 336 | n/a | |
|---|
| 337 | n/a | LANGINFO(ABDAY_1), |
|---|
| 338 | n/a | LANGINFO(ABDAY_2), |
|---|
| 339 | n/a | LANGINFO(ABDAY_3), |
|---|
| 340 | n/a | LANGINFO(ABDAY_4), |
|---|
| 341 | n/a | LANGINFO(ABDAY_5), |
|---|
| 342 | n/a | LANGINFO(ABDAY_6), |
|---|
| 343 | n/a | LANGINFO(ABDAY_7), |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | LANGINFO(MON_1), |
|---|
| 346 | n/a | LANGINFO(MON_2), |
|---|
| 347 | n/a | LANGINFO(MON_3), |
|---|
| 348 | n/a | LANGINFO(MON_4), |
|---|
| 349 | n/a | LANGINFO(MON_5), |
|---|
| 350 | n/a | LANGINFO(MON_6), |
|---|
| 351 | n/a | LANGINFO(MON_7), |
|---|
| 352 | n/a | LANGINFO(MON_8), |
|---|
| 353 | n/a | LANGINFO(MON_9), |
|---|
| 354 | n/a | LANGINFO(MON_10), |
|---|
| 355 | n/a | LANGINFO(MON_11), |
|---|
| 356 | n/a | LANGINFO(MON_12), |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | LANGINFO(ABMON_1), |
|---|
| 359 | n/a | LANGINFO(ABMON_2), |
|---|
| 360 | n/a | LANGINFO(ABMON_3), |
|---|
| 361 | n/a | LANGINFO(ABMON_4), |
|---|
| 362 | n/a | LANGINFO(ABMON_5), |
|---|
| 363 | n/a | LANGINFO(ABMON_6), |
|---|
| 364 | n/a | LANGINFO(ABMON_7), |
|---|
| 365 | n/a | LANGINFO(ABMON_8), |
|---|
| 366 | n/a | LANGINFO(ABMON_9), |
|---|
| 367 | n/a | LANGINFO(ABMON_10), |
|---|
| 368 | n/a | LANGINFO(ABMON_11), |
|---|
| 369 | n/a | LANGINFO(ABMON_12), |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | #ifdef RADIXCHAR |
|---|
| 372 | n/a | /* The following are not available with glibc 2.0 */ |
|---|
| 373 | n/a | LANGINFO(RADIXCHAR), |
|---|
| 374 | n/a | LANGINFO(THOUSEP), |
|---|
| 375 | n/a | /* YESSTR and NOSTR are deprecated in glibc, since they are |
|---|
| 376 | n/a | a special case of message translation, which should be rather |
|---|
| 377 | n/a | done using gettext. So we don't expose it to Python in the |
|---|
| 378 | n/a | first place. |
|---|
| 379 | n/a | LANGINFO(YESSTR), |
|---|
| 380 | n/a | LANGINFO(NOSTR), |
|---|
| 381 | n/a | */ |
|---|
| 382 | n/a | LANGINFO(CRNCYSTR), |
|---|
| 383 | n/a | #endif |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | LANGINFO(D_T_FMT), |
|---|
| 386 | n/a | LANGINFO(D_FMT), |
|---|
| 387 | n/a | LANGINFO(T_FMT), |
|---|
| 388 | n/a | LANGINFO(AM_STR), |
|---|
| 389 | n/a | LANGINFO(PM_STR), |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | /* The following constants are available only with XPG4, but... |
|---|
| 392 | n/a | AIX 3.2. only has CODESET. |
|---|
| 393 | n/a | OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have |
|---|
| 394 | n/a | a few of the others. |
|---|
| 395 | n/a | Solution: ifdef-test them all. */ |
|---|
| 396 | n/a | #ifdef CODESET |
|---|
| 397 | n/a | LANGINFO(CODESET), |
|---|
| 398 | n/a | #endif |
|---|
| 399 | n/a | #ifdef T_FMT_AMPM |
|---|
| 400 | n/a | LANGINFO(T_FMT_AMPM), |
|---|
| 401 | n/a | #endif |
|---|
| 402 | n/a | #ifdef ERA |
|---|
| 403 | n/a | LANGINFO(ERA), |
|---|
| 404 | n/a | #endif |
|---|
| 405 | n/a | #ifdef ERA_D_FMT |
|---|
| 406 | n/a | LANGINFO(ERA_D_FMT), |
|---|
| 407 | n/a | #endif |
|---|
| 408 | n/a | #ifdef ERA_D_T_FMT |
|---|
| 409 | n/a | LANGINFO(ERA_D_T_FMT), |
|---|
| 410 | n/a | #endif |
|---|
| 411 | n/a | #ifdef ERA_T_FMT |
|---|
| 412 | n/a | LANGINFO(ERA_T_FMT), |
|---|
| 413 | n/a | #endif |
|---|
| 414 | n/a | #ifdef ALT_DIGITS |
|---|
| 415 | n/a | LANGINFO(ALT_DIGITS), |
|---|
| 416 | n/a | #endif |
|---|
| 417 | n/a | #ifdef YESEXPR |
|---|
| 418 | n/a | LANGINFO(YESEXPR), |
|---|
| 419 | n/a | #endif |
|---|
| 420 | n/a | #ifdef NOEXPR |
|---|
| 421 | n/a | LANGINFO(NOEXPR), |
|---|
| 422 | n/a | #endif |
|---|
| 423 | n/a | #ifdef _DATE_FMT |
|---|
| 424 | n/a | /* This is not available in all glibc versions that have CODESET. */ |
|---|
| 425 | n/a | LANGINFO(_DATE_FMT), |
|---|
| 426 | n/a | #endif |
|---|
| 427 | n/a | {0, 0} |
|---|
| 428 | n/a | }; |
|---|
| 429 | n/a | |
|---|
| 430 | n/a | PyDoc_STRVAR(nl_langinfo__doc__, |
|---|
| 431 | n/a | "nl_langinfo(key) -> string\n" |
|---|
| 432 | n/a | "Return the value for the locale information associated with key."); |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | static PyObject* |
|---|
| 435 | n/a | PyLocale_nl_langinfo(PyObject* self, PyObject* args) |
|---|
| 436 | n/a | { |
|---|
| 437 | n/a | int item, i; |
|---|
| 438 | n/a | if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item)) |
|---|
| 439 | n/a | return NULL; |
|---|
| 440 | n/a | /* Check whether this is a supported constant. GNU libc sometimes |
|---|
| 441 | n/a | returns numeric values in the char* return value, which would |
|---|
| 442 | n/a | crash PyUnicode_FromString. */ |
|---|
| 443 | n/a | for (i = 0; langinfo_constants[i].name; i++) |
|---|
| 444 | n/a | if (langinfo_constants[i].value == item) { |
|---|
| 445 | n/a | /* Check NULL as a workaround for GNU libc's returning NULL |
|---|
| 446 | n/a | instead of an empty string for nl_langinfo(ERA). */ |
|---|
| 447 | n/a | const char *result = nl_langinfo(item); |
|---|
| 448 | n/a | result = result != NULL ? result : ""; |
|---|
| 449 | n/a | return PyUnicode_DecodeLocale(result, NULL); |
|---|
| 450 | n/a | } |
|---|
| 451 | n/a | PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant"); |
|---|
| 452 | n/a | return NULL; |
|---|
| 453 | n/a | } |
|---|
| 454 | n/a | #endif /* HAVE_LANGINFO_H */ |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | #ifdef HAVE_LIBINTL_H |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | PyDoc_STRVAR(gettext__doc__, |
|---|
| 459 | n/a | "gettext(msg) -> string\n" |
|---|
| 460 | n/a | "Return translation of msg."); |
|---|
| 461 | n/a | |
|---|
| 462 | n/a | static PyObject* |
|---|
| 463 | n/a | PyIntl_gettext(PyObject* self, PyObject *args) |
|---|
| 464 | n/a | { |
|---|
| 465 | n/a | char *in; |
|---|
| 466 | n/a | if (!PyArg_ParseTuple(args, "s", &in)) |
|---|
| 467 | n/a | return 0; |
|---|
| 468 | n/a | return PyUnicode_DecodeLocale(gettext(in), NULL); |
|---|
| 469 | n/a | } |
|---|
| 470 | n/a | |
|---|
| 471 | n/a | PyDoc_STRVAR(dgettext__doc__, |
|---|
| 472 | n/a | "dgettext(domain, msg) -> string\n" |
|---|
| 473 | n/a | "Return translation of msg in domain."); |
|---|
| 474 | n/a | |
|---|
| 475 | n/a | static PyObject* |
|---|
| 476 | n/a | PyIntl_dgettext(PyObject* self, PyObject *args) |
|---|
| 477 | n/a | { |
|---|
| 478 | n/a | char *domain, *in; |
|---|
| 479 | n/a | if (!PyArg_ParseTuple(args, "zs", &domain, &in)) |
|---|
| 480 | n/a | return 0; |
|---|
| 481 | n/a | return PyUnicode_DecodeLocale(dgettext(domain, in), NULL); |
|---|
| 482 | n/a | } |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | PyDoc_STRVAR(dcgettext__doc__, |
|---|
| 485 | n/a | "dcgettext(domain, msg, category) -> string\n" |
|---|
| 486 | n/a | "Return translation of msg in domain and category."); |
|---|
| 487 | n/a | |
|---|
| 488 | n/a | static PyObject* |
|---|
| 489 | n/a | PyIntl_dcgettext(PyObject *self, PyObject *args) |
|---|
| 490 | n/a | { |
|---|
| 491 | n/a | char *domain, *msgid; |
|---|
| 492 | n/a | int category; |
|---|
| 493 | n/a | if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category)) |
|---|
| 494 | n/a | return 0; |
|---|
| 495 | n/a | return PyUnicode_DecodeLocale(dcgettext(domain,msgid,category), NULL); |
|---|
| 496 | n/a | } |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | PyDoc_STRVAR(textdomain__doc__, |
|---|
| 499 | n/a | "textdomain(domain) -> string\n" |
|---|
| 500 | n/a | "Set the C library's textdmain to domain, returning the new domain."); |
|---|
| 501 | n/a | |
|---|
| 502 | n/a | static PyObject* |
|---|
| 503 | n/a | PyIntl_textdomain(PyObject* self, PyObject* args) |
|---|
| 504 | n/a | { |
|---|
| 505 | n/a | char *domain; |
|---|
| 506 | n/a | if (!PyArg_ParseTuple(args, "z", &domain)) |
|---|
| 507 | n/a | return 0; |
|---|
| 508 | n/a | domain = textdomain(domain); |
|---|
| 509 | n/a | if (!domain) { |
|---|
| 510 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
|---|
| 511 | n/a | return NULL; |
|---|
| 512 | n/a | } |
|---|
| 513 | n/a | return PyUnicode_DecodeLocale(domain, NULL); |
|---|
| 514 | n/a | } |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | PyDoc_STRVAR(bindtextdomain__doc__, |
|---|
| 517 | n/a | "bindtextdomain(domain, dir) -> string\n" |
|---|
| 518 | n/a | "Bind the C library's domain to dir."); |
|---|
| 519 | n/a | |
|---|
| 520 | n/a | static PyObject* |
|---|
| 521 | n/a | PyIntl_bindtextdomain(PyObject* self,PyObject*args) |
|---|
| 522 | n/a | { |
|---|
| 523 | n/a | char *domain, *dirname, *current_dirname; |
|---|
| 524 | n/a | PyObject *dirname_obj, *dirname_bytes = NULL, *result; |
|---|
| 525 | n/a | if (!PyArg_ParseTuple(args, "sO", &domain, &dirname_obj)) |
|---|
| 526 | n/a | return 0; |
|---|
| 527 | n/a | if (!strlen(domain)) { |
|---|
| 528 | n/a | PyErr_SetString(Error, "domain must be a non-empty string"); |
|---|
| 529 | n/a | return 0; |
|---|
| 530 | n/a | } |
|---|
| 531 | n/a | if (dirname_obj != Py_None) { |
|---|
| 532 | n/a | if (!PyUnicode_FSConverter(dirname_obj, &dirname_bytes)) |
|---|
| 533 | n/a | return NULL; |
|---|
| 534 | n/a | dirname = PyBytes_AsString(dirname_bytes); |
|---|
| 535 | n/a | } else { |
|---|
| 536 | n/a | dirname_bytes = NULL; |
|---|
| 537 | n/a | dirname = NULL; |
|---|
| 538 | n/a | } |
|---|
| 539 | n/a | current_dirname = bindtextdomain(domain, dirname); |
|---|
| 540 | n/a | if (current_dirname == NULL) { |
|---|
| 541 | n/a | Py_XDECREF(dirname_bytes); |
|---|
| 542 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
|---|
| 543 | n/a | return NULL; |
|---|
| 544 | n/a | } |
|---|
| 545 | n/a | result = PyUnicode_DecodeLocale(current_dirname, NULL); |
|---|
| 546 | n/a | Py_XDECREF(dirname_bytes); |
|---|
| 547 | n/a | return result; |
|---|
| 548 | n/a | } |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
|---|
| 551 | n/a | PyDoc_STRVAR(bind_textdomain_codeset__doc__, |
|---|
| 552 | n/a | "bind_textdomain_codeset(domain, codeset) -> string\n" |
|---|
| 553 | n/a | "Bind the C library's domain to codeset."); |
|---|
| 554 | n/a | |
|---|
| 555 | n/a | static PyObject* |
|---|
| 556 | n/a | PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args) |
|---|
| 557 | n/a | { |
|---|
| 558 | n/a | char *domain,*codeset; |
|---|
| 559 | n/a | if (!PyArg_ParseTuple(args, "sz", &domain, &codeset)) |
|---|
| 560 | n/a | return NULL; |
|---|
| 561 | n/a | codeset = bind_textdomain_codeset(domain, codeset); |
|---|
| 562 | n/a | if (codeset) |
|---|
| 563 | n/a | return PyUnicode_DecodeLocale(codeset, NULL); |
|---|
| 564 | n/a | Py_RETURN_NONE; |
|---|
| 565 | n/a | } |
|---|
| 566 | n/a | #endif |
|---|
| 567 | n/a | |
|---|
| 568 | n/a | #endif |
|---|
| 569 | n/a | |
|---|
| 570 | n/a | static struct PyMethodDef PyLocale_Methods[] = { |
|---|
| 571 | n/a | {"setlocale", (PyCFunction) PyLocale_setlocale, |
|---|
| 572 | n/a | METH_VARARGS, setlocale__doc__}, |
|---|
| 573 | n/a | {"localeconv", (PyCFunction) PyLocale_localeconv, |
|---|
| 574 | n/a | METH_NOARGS, localeconv__doc__}, |
|---|
| 575 | n/a | #ifdef HAVE_WCSCOLL |
|---|
| 576 | n/a | {"strcoll", (PyCFunction) PyLocale_strcoll, |
|---|
| 577 | n/a | METH_VARARGS, strcoll__doc__}, |
|---|
| 578 | n/a | #endif |
|---|
| 579 | n/a | #ifdef HAVE_WCSXFRM |
|---|
| 580 | n/a | {"strxfrm", (PyCFunction) PyLocale_strxfrm, |
|---|
| 581 | n/a | METH_VARARGS, strxfrm__doc__}, |
|---|
| 582 | n/a | #endif |
|---|
| 583 | n/a | #if defined(MS_WINDOWS) |
|---|
| 584 | n/a | {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS}, |
|---|
| 585 | n/a | #endif |
|---|
| 586 | n/a | #ifdef HAVE_LANGINFO_H |
|---|
| 587 | n/a | {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo, |
|---|
| 588 | n/a | METH_VARARGS, nl_langinfo__doc__}, |
|---|
| 589 | n/a | #endif |
|---|
| 590 | n/a | #ifdef HAVE_LIBINTL_H |
|---|
| 591 | n/a | {"gettext",(PyCFunction)PyIntl_gettext,METH_VARARGS, |
|---|
| 592 | n/a | gettext__doc__}, |
|---|
| 593 | n/a | {"dgettext",(PyCFunction)PyIntl_dgettext,METH_VARARGS, |
|---|
| 594 | n/a | dgettext__doc__}, |
|---|
| 595 | n/a | {"dcgettext",(PyCFunction)PyIntl_dcgettext,METH_VARARGS, |
|---|
| 596 | n/a | dcgettext__doc__}, |
|---|
| 597 | n/a | {"textdomain",(PyCFunction)PyIntl_textdomain,METH_VARARGS, |
|---|
| 598 | n/a | textdomain__doc__}, |
|---|
| 599 | n/a | {"bindtextdomain",(PyCFunction)PyIntl_bindtextdomain,METH_VARARGS, |
|---|
| 600 | n/a | bindtextdomain__doc__}, |
|---|
| 601 | n/a | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
|---|
| 602 | n/a | {"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset, |
|---|
| 603 | n/a | METH_VARARGS, bind_textdomain_codeset__doc__}, |
|---|
| 604 | n/a | #endif |
|---|
| 605 | n/a | #endif |
|---|
| 606 | n/a | {NULL, NULL} |
|---|
| 607 | n/a | }; |
|---|
| 608 | n/a | |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | static struct PyModuleDef _localemodule = { |
|---|
| 611 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 612 | n/a | "_locale", |
|---|
| 613 | n/a | locale__doc__, |
|---|
| 614 | n/a | -1, |
|---|
| 615 | n/a | PyLocale_Methods, |
|---|
| 616 | n/a | NULL, |
|---|
| 617 | n/a | NULL, |
|---|
| 618 | n/a | NULL, |
|---|
| 619 | n/a | NULL |
|---|
| 620 | n/a | }; |
|---|
| 621 | n/a | |
|---|
| 622 | n/a | PyMODINIT_FUNC |
|---|
| 623 | n/a | PyInit__locale(void) |
|---|
| 624 | n/a | { |
|---|
| 625 | n/a | PyObject *m; |
|---|
| 626 | n/a | #ifdef HAVE_LANGINFO_H |
|---|
| 627 | n/a | int i; |
|---|
| 628 | n/a | #endif |
|---|
| 629 | n/a | |
|---|
| 630 | n/a | m = PyModule_Create(&_localemodule); |
|---|
| 631 | n/a | if (m == NULL) |
|---|
| 632 | n/a | return NULL; |
|---|
| 633 | n/a | |
|---|
| 634 | n/a | PyModule_AddIntMacro(m, LC_CTYPE); |
|---|
| 635 | n/a | PyModule_AddIntMacro(m, LC_TIME); |
|---|
| 636 | n/a | PyModule_AddIntMacro(m, LC_COLLATE); |
|---|
| 637 | n/a | PyModule_AddIntMacro(m, LC_MONETARY); |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | #ifdef LC_MESSAGES |
|---|
| 640 | n/a | PyModule_AddIntMacro(m, LC_MESSAGES); |
|---|
| 641 | n/a | #endif /* LC_MESSAGES */ |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | PyModule_AddIntMacro(m, LC_NUMERIC); |
|---|
| 644 | n/a | PyModule_AddIntMacro(m, LC_ALL); |
|---|
| 645 | n/a | PyModule_AddIntMacro(m, CHAR_MAX); |
|---|
| 646 | n/a | |
|---|
| 647 | n/a | Error = PyErr_NewException("locale.Error", NULL, NULL); |
|---|
| 648 | n/a | if (Error == NULL) { |
|---|
| 649 | n/a | Py_DECREF(m); |
|---|
| 650 | n/a | return NULL; |
|---|
| 651 | n/a | } |
|---|
| 652 | n/a | PyModule_AddObject(m, "Error", Error); |
|---|
| 653 | n/a | |
|---|
| 654 | n/a | #ifdef HAVE_LANGINFO_H |
|---|
| 655 | n/a | for (i = 0; langinfo_constants[i].name; i++) { |
|---|
| 656 | n/a | PyModule_AddIntConstant(m, langinfo_constants[i].name, |
|---|
| 657 | n/a | langinfo_constants[i].value); |
|---|
| 658 | n/a | } |
|---|
| 659 | n/a | #endif |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | if (PyErr_Occurred()) { |
|---|
| 662 | n/a | Py_DECREF(m); |
|---|
| 663 | n/a | return NULL; |
|---|
| 664 | n/a | } |
|---|
| 665 | n/a | return m; |
|---|
| 666 | n/a | } |
|---|
| 667 | n/a | |
|---|
| 668 | n/a | /* |
|---|
| 669 | n/a | Local variables: |
|---|
| 670 | n/a | c-basic-offset: 4 |
|---|
| 671 | n/a | indent-tabs-mode: nil |
|---|
| 672 | n/a | End: |
|---|
| 673 | n/a | */ |
|---|