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