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