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