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*
copy_grouping(char * s)55 copy_grouping(char* s)
56 {
57     int i;
58     PyObject *result, *val = NULL;
59 
60     if (s[0] == '\0') {
61         /* empty string: no grouping at all */
62         return PyList_New(0);
63     }
64 
65     for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++)
66         ; /* nothing */
67 
68     result = PyList_New(i+1);
69     if (!result)
70         return NULL;
71 
72     i = -1;
73     do {
74         i++;
75         val = PyInt_FromLong(s[i]);
76         if (!val)
77             break;
78         if (PyList_SetItem(result, i, val)) {
79             Py_DECREF(val);
80             val = NULL;
81             break;
82         }
83     } while (s[i] != '\0' && s[i] != CHAR_MAX);
84 
85     if (!val) {
86         Py_DECREF(result);
87         return NULL;
88     }
89 
90     return result;
91 }
92 
93 static void
fixup_ulcase(void)94 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     mods = PyImport_GetModuleDict();
102     if (!mods)
103         return;
104     string = PyDict_GetItemString(mods, "string");
105     if (string)
106         string = PyModule_GetDict(string);
107     strop=PyDict_GetItemString(mods, "strop");
108     if (strop)
109         strop = PyModule_GetDict(strop);
110     if (!string && !strop)
111         return;
112 
113     /* create uppercase map string */
114     n = 0;
115     for (c = 0; c < 256; c++) {
116         if (isupper(c))
117             ul[n++] = c;
118     }
119     ulo = PyString_FromStringAndSize((const char *)ul, n);
120     if (!ulo)
121         return;
122     if (string)
123         PyDict_SetItemString(string, "uppercase", ulo);
124     if (strop)
125         PyDict_SetItemString(strop, "uppercase", ulo);
126     Py_DECREF(ulo);
127 
128     /* create lowercase string */
129     n = 0;
130     for (c = 0; c < 256; c++) {
131         if (islower(c))
132             ul[n++] = c;
133     }
134     ulo = PyString_FromStringAndSize((const char *)ul, n);
135     if (!ulo)
136         return;
137     if (string)
138         PyDict_SetItemString(string, "lowercase", ulo);
139     if (strop)
140         PyDict_SetItemString(strop, "lowercase", ulo);
141     Py_DECREF(ulo);
142 
143     /* create letters string */
144     n = 0;
145     for (c = 0; c < 256; c++) {
146         if (isalpha(c))
147             ul[n++] = c;
148     }
149     ulo = PyString_FromStringAndSize((const char *)ul, n);
150     if (!ulo)
151         return;
152     if (string)
153         PyDict_SetItemString(string, "letters", ulo);
154     Py_DECREF(ulo);
155 }
156 
157 static PyObject*
PyLocale_setlocale(PyObject * self,PyObject * args)158 PyLocale_setlocale(PyObject* self, PyObject* args)
159 {
160     int category;
161     char *locale = NULL, *result;
162     PyObject *result_object;
163 
164     if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
165         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     if (locale) {
176         /* set locale */
177         result = setlocale(category, locale);
178         if (!result) {
179             /* operation failed, no setting was changed */
180             PyErr_SetString(Error, "unsupported locale setting");
181             return NULL;
182         }
183         result_object = PyString_FromString(result);
184         if (!result_object)
185             return NULL;
186         /* record changes to LC_CTYPE */
187         if (category == LC_CTYPE || category == LC_ALL)
188             fixup_ulcase();
189         /* things that got wrong up to here are ignored */
190         PyErr_Clear();
191     } else {
192         /* get locale */
193         result = setlocale(category, NULL);
194         if (!result) {
195             PyErr_SetString(Error, "locale query failed");
196             return NULL;
197         }
198         result_object = PyString_FromString(result);
199     }
200     return result_object;
201 }
202 
203 PyDoc_STRVAR(localeconv__doc__,
204 "() -> dict. Returns numeric and monetary locale-specific parameters.");
205 
206 static PyObject*
PyLocale_localeconv(PyObject * self)207 PyLocale_localeconv(PyObject* self)
208 {
209     PyObject* result;
210     struct lconv *l;
211     PyObject *x;
212 
213     result = PyDict_New();
214     if (!result)
215         return NULL;
216 
217     /* if LC_NUMERIC is different in the C library, use saved value */
218     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     RESULT_STRING(decimal_point);
237     RESULT_STRING(thousands_sep);
238     x = copy_grouping(l->grouping);
239     if (!x)
240         goto failed;
241     PyDict_SetItemString(result, "grouping", x);
242     Py_XDECREF(x);
243 
244     /* Monetary information */
245     RESULT_STRING(int_curr_symbol);
246     RESULT_STRING(currency_symbol);
247     RESULT_STRING(mon_decimal_point);
248     RESULT_STRING(mon_thousands_sep);
249     x = copy_grouping(l->mon_grouping);
250     if (!x)
251         goto failed;
252     PyDict_SetItemString(result, "mon_grouping", x);
253     Py_XDECREF(x);
254     RESULT_STRING(positive_sign);
255     RESULT_STRING(negative_sign);
256     RESULT_INT(int_frac_digits);
257     RESULT_INT(frac_digits);
258     RESULT_INT(p_cs_precedes);
259     RESULT_INT(p_sep_by_space);
260     RESULT_INT(n_cs_precedes);
261     RESULT_INT(n_sep_by_space);
262     RESULT_INT(p_sign_posn);
263     RESULT_INT(n_sign_posn);
264     return result;
265 
266   failed:
267     Py_XDECREF(result);
268     Py_XDECREF(x);
269     return NULL;
270 }
271 
272 PyDoc_STRVAR(strcoll__doc__,
273 "string,string -> int. Compares two strings according to the locale.");
274 
275 static PyObject*
PyLocale_strcoll(PyObject * self,PyObject * args)276 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     PyObject *os1, *os2, *result = NULL;
286     wchar_t *ws1 = NULL, *ws2 = NULL;
287     int rel1 = 0, rel2 = 0, len1, len2;
288 
289     if (!PyArg_UnpackTuple(args, "strcoll", 2, 2, &os1, &os2))
290         return NULL;
291     /* If both arguments are byte strings, use strcoll.  */
292     if (PyString_Check(os1) && PyString_Check(os2))
293         return PyInt_FromLong(strcoll(PyString_AS_STRING(os1),
294                                       PyString_AS_STRING(os2)));
295     /* If neither argument is unicode, it's an error.  */
296     if (!PyUnicode_Check(os1) && !PyUnicode_Check(os2)) {
297         PyErr_SetString(PyExc_ValueError, "strcoll arguments must be strings");
298     }
299     /* Convert the non-unicode argument to unicode. */
300     if (!PyUnicode_Check(os1)) {
301         os1 = PyUnicode_FromObject(os1);
302         if (!os1)
303             return NULL;
304         rel1 = 1;
305     }
306     if (!PyUnicode_Check(os2)) {
307         os2 = PyUnicode_FromObject(os2);
308         if (!os2) {
309             if (rel1) {
310                 Py_DECREF(os1);
311             }
312             return NULL;
313         }
314         rel2 = 1;
315     }
316     /* Convert the unicode strings to wchar[]. */
317     len1 = PyUnicode_GET_SIZE(os1) + 1;
318     ws1 = PyMem_NEW(wchar_t, len1);
319     if (!ws1) {
320         PyErr_NoMemory();
321         goto done;
322     }
323     if (PyUnicode_AsWideChar((PyUnicodeObject*)os1, ws1, len1) == -1)
324         goto done;
325     ws1[len1 - 1] = 0;
326     len2 = PyUnicode_GET_SIZE(os2) + 1;
327     ws2 = PyMem_NEW(wchar_t, len2);
328     if (!ws2) {
329         PyErr_NoMemory();
330         goto done;
331     }
332     if (PyUnicode_AsWideChar((PyUnicodeObject*)os2, ws2, len2) == -1)
333         goto done;
334     ws2[len2 - 1] = 0;
335     /* Collate the strings. */
336     result = PyInt_FromLong(wcscoll(ws1, ws2));
337   done:
338     /* Deallocate everything. */
339     if (ws1) PyMem_FREE(ws1);
340     if (ws2) PyMem_FREE(ws2);
341     if (rel1) {
342         Py_DECREF(os1);
343     }
344     if (rel2) {
345         Py_DECREF(os2);
346     }
347     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*
PyLocale_strxfrm(PyObject * self,PyObject * args)356 PyLocale_strxfrm(PyObject* self, PyObject* args)
357 {
358     char *s, *buf;
359     size_t n1, n2;
360     PyObject *result;
361 
362     if (!PyArg_ParseTuple(args, "s:strxfrm", &s))
363         return NULL;
364 
365     /* assume no change in size, first */
366     n1 = strlen(s) + 1;
367     buf = PyMem_Malloc(n1);
368     if (!buf)
369         return PyErr_NoMemory();
370     n2 = strxfrm(buf, s, n1) + 1;
371     if (n2 > n1) {
372         /* more space needed */
373         buf = PyMem_Realloc(buf, n2);
374         if (!buf)
375             return PyErr_NoMemory();
376         strxfrm(buf, s, n2);
377     }
378     result = PyString_FromString(buf);
379     PyMem_Free(buf);
380     return result;
381 }
382 
383 #if defined(MS_WINDOWS)
384 static PyObject*
PyLocale_getdefaultlocale(PyObject * self)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*
PyLocale_nl_langinfo(PyObject * self,PyObject * args)534 PyLocale_nl_langinfo(PyObject* self, PyObject* args)
535 {
536     int item, i;
537     if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))
538         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     for (i = 0; langinfo_constants[i].name; i++)
543         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             const char *result = nl_langinfo(item);
547             return PyString_FromString(result != NULL ? result : "");
548         }
549     PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");
550     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*
PyIntl_gettext(PyObject * self,PyObject * args)561 PyIntl_gettext(PyObject* self, PyObject *args)
562 {
563     char *in;
564     if (!PyArg_ParseTuple(args, "s", &in))
565         return 0;
566     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*
PyIntl_dgettext(PyObject * self,PyObject * args)574 PyIntl_dgettext(PyObject* self, PyObject *args)
575 {
576     char *domain, *in;
577     if (!PyArg_ParseTuple(args, "zs", &domain, &in))
578         return 0;
579     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*
PyIntl_dcgettext(PyObject * self,PyObject * args)587 PyIntl_dcgettext(PyObject *self, PyObject *args)
588 {
589     char *domain, *msgid;
590     int category;
591     if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))
592         return 0;
593     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*
PyIntl_textdomain(PyObject * self,PyObject * args)601 PyIntl_textdomain(PyObject* self, PyObject* args)
602 {
603     char *domain;
604     if (!PyArg_ParseTuple(args, "z", &domain))
605         return 0;
606     domain = textdomain(domain);
607     if (!domain) {
608         PyErr_SetFromErrno(PyExc_OSError);
609         return NULL;
610     }
611     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*
PyIntl_bindtextdomain(PyObject * self,PyObject * args)619 PyIntl_bindtextdomain(PyObject* self,PyObject*args)
620 {
621     char *domain, *dirname;
622     if (!PyArg_ParseTuple(args, "sz", &domain, &dirname))
623         return 0;
624     if (!strlen(domain)) {
625         PyErr_SetString(Error, "domain must be a non-empty string");
626         return 0;
627     }
628     dirname = bindtextdomain(domain, dirname);
629     if (!dirname) {
630         PyErr_SetFromErrno(PyExc_OSError);
631         return NULL;
632     }
633     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*
PyIntl_bind_textdomain_codeset(PyObject * self,PyObject * args)642 PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)
643 {
644     char *domain,*codeset;
645     if (!PyArg_ParseTuple(args, "sz", &domain, &codeset))
646         return NULL;
647     codeset = bind_textdomain_codeset(domain, codeset);
648     if (codeset)
649         return PyString_FromString(codeset);
650     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
init_locale(void)692 init_locale(void)
693 {
694     PyObject *m, *d, *x;
695 #ifdef HAVE_LANGINFO_H
696     int i;
697 #endif
698 
699     m = Py_InitModule("_locale", PyLocale_Methods);
700     if (m == NULL)
701     return;
702 
703     d = PyModule_GetDict(m);
704 
705     x = PyInt_FromLong(LC_CTYPE);
706     PyDict_SetItemString(d, "LC_CTYPE", x);
707     Py_XDECREF(x);
708 
709     x = PyInt_FromLong(LC_TIME);
710     PyDict_SetItemString(d, "LC_TIME", x);
711     Py_XDECREF(x);
712 
713     x = PyInt_FromLong(LC_COLLATE);
714     PyDict_SetItemString(d, "LC_COLLATE", x);
715     Py_XDECREF(x);
716 
717     x = PyInt_FromLong(LC_MONETARY);
718     PyDict_SetItemString(d, "LC_MONETARY", x);
719     Py_XDECREF(x);
720 
721 #ifdef LC_MESSAGES
722     x = PyInt_FromLong(LC_MESSAGES);
723     PyDict_SetItemString(d, "LC_MESSAGES", x);
724     Py_XDECREF(x);
725 #endif /* LC_MESSAGES */
726 
727     x = PyInt_FromLong(LC_NUMERIC);
728     PyDict_SetItemString(d, "LC_NUMERIC", x);
729     Py_XDECREF(x);
730 
731     x = PyInt_FromLong(LC_ALL);
732     PyDict_SetItemString(d, "LC_ALL", x);
733     Py_XDECREF(x);
734 
735     x = PyInt_FromLong(CHAR_MAX);
736     PyDict_SetItemString(d, "CHAR_MAX", x);
737     Py_XDECREF(x);
738 
739     Error = PyErr_NewException("locale.Error", NULL, NULL);
740     PyDict_SetItemString(d, "Error", Error);
741 
742     x = PyString_FromString(locale__doc__);
743     PyDict_SetItemString(d, "__doc__", x);
744     Py_XDECREF(x);
745 
746 #ifdef HAVE_LANGINFO_H
747     for (i = 0; langinfo_constants[i].name; i++) {
748         PyModule_AddIntConstant(m, langinfo_constants[i].name,
749                                 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 */
760