/Modules/_localemodule.c

http://unladen-swallow.googlecode.com/ · C · 786 lines · 620 code · 94 blank · 72 comment · 98 complexity · c743b8b23c4fb46da46b59b099407e07 MD5 · raw file

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