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