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