LCOV - code coverage report
Current view: top level - Modules - _localemodule.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 37 271 13.7 %
Date: 2017-04-19 Functions: 1 14 7.1 %

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

Generated by: LCOV version 1.10