blob: cfbef9f432ddac64658d1fe14492adde06108f0b [file] [log] [blame]
Guido van Rossum220ecc81997-11-18 21:03:39 +00001/***********************************************************
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +00002Copyright (C) 1997, 2002 Martin von Loewis
Guido van Rossum220ecc81997-11-18 21:03:39 +00003
4Permission to use, copy, modify, and distribute this software and its
5documentation for any purpose and without fee is hereby granted,
6provided that the above copyright notice appear in all copies.
7
8This software comes with no warranty. Use at your own risk.
Fredrik Lundh8f017a02000-07-08 19:57:37 +00009
Guido van Rossum220ecc81997-11-18 21:03:39 +000010******************************************************************/
11
Fred Drake68933b92000-08-10 21:41:08 +000012#include "Python.h"
13
Guido van Rossum220ecc81997-11-18 21:03:39 +000014#include <stdio.h>
15#include <errno.h>
16#include <locale.h>
17#include <string.h>
Guido van Rossum5cd70f41998-06-19 04:33:30 +000018#include <ctype.h>
Fredrik Lundh8f017a02000-07-08 19:57:37 +000019
Martin v. Löwis9b75dca2001-08-10 13:58:50 +000020#ifdef HAVE_LANGINFO_H
21#include <langinfo.h>
22#endif
23
Fredrik Lundh8f017a02000-07-08 19:57:37 +000024#if defined(MS_WIN32)
25#define WINDOWS_LEAN_AND_MEAN
26#include <windows.h>
Guido van Rossum239a2181998-04-28 16:08:19 +000027#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +000028
Fredrik Lundh8f017a02000-07-08 19:57:37 +000029#ifdef macintosh
Jack Jansen307d7a42000-07-15 22:31:45 +000030#include "macglue.h"
Fredrik Lundh8f017a02000-07-08 19:57:37 +000031#endif
32
Guido van Rossum1ca8bb32001-03-02 06:28:17 +000033#ifdef RISCOS
34char *strdup(const char *);
35#endif
36
Fredrik Lundh8f017a02000-07-08 19:57:37 +000037static char locale__doc__[] = "Support for POSIX locales.";
Guido van Rossum220ecc81997-11-18 21:03:39 +000038
39static PyObject *Error;
40
41/* support functions for formatting floating point numbers */
42
Fredrik Lundh8f017a02000-07-08 19:57:37 +000043static char setlocale__doc__[] =
Guido van Rossum220ecc81997-11-18 21:03:39 +000044"(integer,string=None) -> string. Activates/queries locale processing."
45;
46
47/* to record the LC_NUMERIC settings */
Fredrik Lundh8f017a02000-07-08 19:57:37 +000048static PyObject* grouping = NULL;
49static PyObject* thousands_sep = NULL;
50static PyObject* decimal_point = NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +000051/* if non-null, indicates that LC_NUMERIC is different from "C" */
Fredrik Lundh8f017a02000-07-08 19:57:37 +000052static char* saved_numeric = NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +000053
54/* the grouping is terminated by either 0 or CHAR_MAX */
55static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +000056copy_grouping(char* s)
Guido van Rossum220ecc81997-11-18 21:03:39 +000057{
Fredrik Lundh8f017a02000-07-08 19:57:37 +000058 int i;
59 PyObject *result, *val = NULL;
60
61 if (s[0] == '\0')
62 /* empty string: no grouping at all */
63 return PyList_New(0);
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);
Fredrik Lundh89610a42000-07-08 20:07:24 +000080 val = NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +000081 break;
82 }
83 } while (s[i] != '\0' && s[i] != CHAR_MAX);
84
85 if (!val) {
86 Py_DECREF(result);
87 return NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +000088 }
Fredrik Lundh8f017a02000-07-08 19:57:37 +000089
90 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +000091}
92
93static void
Fredrik Lundh8f017a02000-07-08 19:57:37 +000094fixup_ulcase(void)
Guido van Rossum220ecc81997-11-18 21:03:39 +000095{
Fredrik Lundh8f017a02000-07-08 19:57:37 +000096 PyObject *mods, *strop, *string, *ulo;
97 unsigned char ul[256];
98 int n, c;
Guido van Rossum220ecc81997-11-18 21:03:39 +000099
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000100 /* 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);
Fredrik Lundh89610a42000-07-08 20:07:24 +0000120 if (!ulo)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000121 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);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000155}
Martin v. Löwis7c82a3e02001-09-05 17:09:48 +0000156
157#if defined(HAVE_LANGINFO_H) && defined(CODESET)
158static int fileencoding_uses_locale = 0;
159#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000160
161static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000162PyLocale_setlocale(PyObject* self, PyObject* args)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000163{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000164 int category;
165 char *locale = NULL, *result;
166 PyObject *result_object;
167 struct lconv *lc;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000168
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000169 if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
Fredrik Lundh89610a42000-07-08 20:07:24 +0000170 return NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000171
172 if (locale) {
173 /* set locale */
174 result = setlocale(category, locale);
175 if (!result) {
176 /* operation failed, no setting was changed */
177 PyErr_SetString(Error, "locale setting not supported");
178 return NULL;
179 }
180 result_object = PyString_FromString(result);
181 if (!result)
182 return NULL;
183 /* record changes to LC_NUMERIC */
184 if (category == LC_NUMERIC || category == LC_ALL) {
185 if (strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0) {
186 /* user just asked for default numeric locale */
187 if (saved_numeric)
188 free(saved_numeric);
189 saved_numeric = NULL;
190 } else {
191 /* remember values */
192 lc = localeconv();
193 Py_XDECREF(grouping);
194 grouping = copy_grouping(lc->grouping);
195 Py_XDECREF(thousands_sep);
196 thousands_sep = PyString_FromString(lc->thousands_sep);
197 Py_XDECREF(decimal_point);
198 decimal_point = PyString_FromString(lc->decimal_point);
199 saved_numeric = strdup(locale);
200 /* restore to "C" */
201 setlocale(LC_NUMERIC, "C");
202 }
203 }
204 /* record changes to LC_CTYPE */
205 if (category == LC_CTYPE || category == LC_ALL)
206 fixup_ulcase();
207 /* things that got wrong up to here are ignored */
208 PyErr_Clear();
Martin v. Löwis7c82a3e02001-09-05 17:09:48 +0000209#if defined(HAVE_LANGINFO_H) && defined(CODESET)
210 if (Py_FileSystemDefaultEncoding == NULL)
211 fileencoding_uses_locale = 1;
212 if (fileencoding_uses_locale) {
213 char *codeset = nl_langinfo(CODESET);
214 PyObject *enc = NULL;
215 if (*codeset && (enc = PyCodec_Encoder(codeset))) {
216 /* Release previous file encoding */
217 if (Py_FileSystemDefaultEncoding)
Guido van Rossum461591e2001-09-20 19:18:30 +0000218 free((char *)Py_FileSystemDefaultEncoding);
Martin v. Löwis7c82a3e02001-09-05 17:09:48 +0000219 Py_FileSystemDefaultEncoding = strdup(codeset);
220 Py_DECREF(enc);
221 } else
222 PyErr_Clear();
223 }
224#endif
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000225 } else {
226 /* get locale */
227 /* restore LC_NUMERIC first, if appropriate */
228 if (saved_numeric)
229 setlocale(LC_NUMERIC, saved_numeric);
230 result = setlocale(category, NULL);
231 if (!result) {
232 PyErr_SetString(Error, "locale query failed");
233 return NULL;
234 }
235 result_object = PyString_FromString(result);
236 /* restore back to "C" */
237 if (saved_numeric)
238 setlocale(LC_NUMERIC, "C");
Guido van Rossum220ecc81997-11-18 21:03:39 +0000239 }
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000240 return result_object;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000241}
242
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000243static char localeconv__doc__[] =
Guido van Rossum220ecc81997-11-18 21:03:39 +0000244"() -> dict. Returns numeric and monetary locale-specific parameters."
245;
246
247static PyObject*
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000248PyLocale_localeconv(PyObject* self)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000249{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000250 PyObject* result;
251 struct lconv *l;
252 PyObject *x;
253
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000254 result = PyDict_New();
Fredrik Lundh89610a42000-07-08 20:07:24 +0000255 if (!result)
256 return NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000257
258 /* if LC_NUMERIC is different in the C library, use saved value */
259 l = localeconv();
260
261 /* hopefully, the localeconv result survives the C library calls
262 involved herein */
263
264#define RESULT_STRING(s)\
265 x = PyString_FromString(l->s);\
266 if (!x) goto failed;\
267 PyDict_SetItemString(result, #s, x);\
268 Py_XDECREF(x)
269
270#define RESULT_INT(i)\
271 x = PyInt_FromLong(l->i);\
272 if (!x) goto failed;\
273 PyDict_SetItemString(result, #i, x);\
274 Py_XDECREF(x)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000275
276 /* Numeric information */
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000277 if (saved_numeric){
278 /* cannot use localeconv results */
279 PyDict_SetItemString(result, "decimal_point", decimal_point);
280 PyDict_SetItemString(result, "grouping", grouping);
281 PyDict_SetItemString(result, "thousands_sep", thousands_sep);
282 } else {
283 RESULT_STRING(decimal_point);
284 RESULT_STRING(thousands_sep);
285 x = copy_grouping(l->grouping);
286 if (!x)
287 goto failed;
288 PyDict_SetItemString(result, "grouping", x);
289 Py_XDECREF(x);
290 }
291
292 /* Monetary information */
293 RESULT_STRING(int_curr_symbol);
294 RESULT_STRING(currency_symbol);
295 RESULT_STRING(mon_decimal_point);
296 RESULT_STRING(mon_thousands_sep);
297 x = copy_grouping(l->mon_grouping);
298 if (!x)
299 goto failed;
300 PyDict_SetItemString(result, "mon_grouping", x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000301 Py_XDECREF(x);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000302 RESULT_STRING(positive_sign);
303 RESULT_STRING(negative_sign);
304 RESULT_INT(int_frac_digits);
305 RESULT_INT(frac_digits);
306 RESULT_INT(p_cs_precedes);
307 RESULT_INT(p_sep_by_space);
308 RESULT_INT(n_cs_precedes);
309 RESULT_INT(n_sep_by_space);
310 RESULT_INT(p_sign_posn);
311 RESULT_INT(n_sign_posn);
312 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000313
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000314 failed:
315 Py_XDECREF(result);
316 Py_XDECREF(x);
317 return NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000318}
319
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000320static char strcoll__doc__[] =
Guido van Rossum220ecc81997-11-18 21:03:39 +0000321"string,string -> int. Compares two strings according to the locale."
322;
323
324static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000325PyLocale_strcoll(PyObject* self, PyObject* args)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000326{
327 char *s1,*s2;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000328
329 if (!PyArg_ParseTuple(args, "ss:strcoll", &s1, &s2))
330 return NULL;
331 return PyInt_FromLong(strcoll(s1, s2));
Guido van Rossum220ecc81997-11-18 21:03:39 +0000332}
333
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000334static char strxfrm__doc__[] =
Guido van Rossum220ecc81997-11-18 21:03:39 +0000335"string -> string. Returns a string that behaves for cmp locale-aware."
336;
337
338static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000339PyLocale_strxfrm(PyObject* self, PyObject* args)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000340{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000341 char *s, *buf;
342 size_t n1, n2;
343 PyObject *result;
344
Fredrik Lundh89610a42000-07-08 20:07:24 +0000345 if (!PyArg_ParseTuple(args, "s:strxfrm", &s))
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000346 return NULL;
347
348 /* assume no change in size, first */
349 n1 = strlen(s) + 1;
350 buf = PyMem_Malloc(n1);
351 if (!buf)
352 return PyErr_NoMemory();
353 n2 = strxfrm(buf, s, n1);
354 if (n2 > n1) {
355 /* more space needed */
356 buf = PyMem_Realloc(buf, n2);
357 if (!buf)
358 return PyErr_NoMemory();
359 strxfrm(buf, s, n2);
360 }
361 result = PyString_FromString(buf);
362 PyMem_Free(buf);
363 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000364}
365
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000366#if defined(MS_WIN32)
367static PyObject*
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000368PyLocale_getdefaultlocale(PyObject* self)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000369{
370 char encoding[100];
371 char locale[100];
372
Tim Peters885d4572001-11-28 20:27:42 +0000373 PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000374
375 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
376 LOCALE_SISO639LANGNAME,
377 locale, sizeof(locale))) {
378 int i = strlen(locale);
379 locale[i++] = '_';
380 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
381 LOCALE_SISO3166CTRYNAME,
382 locale+i, sizeof(locale)-i))
383 return Py_BuildValue("ss", locale, encoding);
384 }
385
386 /* If we end up here, this windows version didn't know about
387 ISO639/ISO3166 names (it's probably Windows 95). Return the
388 Windows language identifier instead (a hexadecimal number) */
389
390 locale[0] = '0';
391 locale[1] = 'x';
392 if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
393 locale+2, sizeof(locale)-2)) {
394 return Py_BuildValue("ss", locale, encoding);
395 }
396
397 /* cannot determine the language code (very unlikely) */
398 Py_INCREF(Py_None);
399 return Py_BuildValue("Os", Py_None, encoding);
400}
401#endif
402
Jack Jansen307d7a42000-07-15 22:31:45 +0000403#if defined(macintosh)
404static PyObject*
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000405PyLocale_getdefaultlocale(PyObject* self)
Jack Jansen307d7a42000-07-15 22:31:45 +0000406{
407 return Py_BuildValue("Os", Py_None, PyMac_getscript());
408}
409#endif
410
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000411#ifdef HAVE_LANGINFO_H
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000412#define LANGINFO(X) {#X, X}
413struct langinfo_constant{
414 char* name;
415 int value;
416} langinfo_constants[] =
417{
418 /* These constants should exist on any langinfo implementation */
419 LANGINFO(DAY_1),
420 LANGINFO(DAY_2),
421 LANGINFO(DAY_3),
422 LANGINFO(DAY_4),
423 LANGINFO(DAY_5),
424 LANGINFO(DAY_6),
425 LANGINFO(DAY_7),
426
427 LANGINFO(ABDAY_1),
428 LANGINFO(ABDAY_2),
429 LANGINFO(ABDAY_3),
430 LANGINFO(ABDAY_4),
431 LANGINFO(ABDAY_5),
432 LANGINFO(ABDAY_6),
433 LANGINFO(ABDAY_7),
434
435 LANGINFO(MON_1),
436 LANGINFO(MON_2),
437 LANGINFO(MON_3),
438 LANGINFO(MON_4),
439 LANGINFO(MON_5),
440 LANGINFO(MON_6),
441 LANGINFO(MON_7),
442 LANGINFO(MON_8),
443 LANGINFO(MON_9),
444 LANGINFO(MON_10),
445 LANGINFO(MON_11),
446 LANGINFO(MON_12),
447
448 LANGINFO(ABMON_1),
449 LANGINFO(ABMON_2),
450 LANGINFO(ABMON_3),
451 LANGINFO(ABMON_4),
452 LANGINFO(ABMON_5),
453 LANGINFO(ABMON_6),
454 LANGINFO(ABMON_7),
455 LANGINFO(ABMON_8),
456 LANGINFO(ABMON_9),
457 LANGINFO(ABMON_10),
458 LANGINFO(ABMON_11),
459 LANGINFO(ABMON_12),
460
461#ifdef RADIXCHAR
462 /* The following are not available with glibc 2.0 */
463 LANGINFO(RADIXCHAR),
464 LANGINFO(THOUSEP),
465 /* YESSTR and NOSTR are deprecated in glibc, since they are
466 a special case of message translation, which should be rather
467 done using gettext. So we don't expose it to Python in the
468 first place.
469 LANGINFO(YESSTR),
470 LANGINFO(NOSTR),
471 */
472 LANGINFO(CRNCYSTR),
473#endif
474
475 LANGINFO(D_T_FMT),
476 LANGINFO(D_FMT),
477 LANGINFO(T_FMT),
478 LANGINFO(AM_STR),
479 LANGINFO(PM_STR),
480
481#ifdef CODESET
482 /* The following constants are available only with XPG4. */
483 LANGINFO(CODESET),
484 LANGINFO(T_FMT_AMPM),
485 LANGINFO(ERA),
486 LANGINFO(ERA_D_FMT),
487 LANGINFO(ERA_D_T_FMT),
488 LANGINFO(ERA_T_FMT),
489 LANGINFO(ALT_DIGITS),
490 LANGINFO(YESEXPR),
491 LANGINFO(NOEXPR),
492#endif
493#ifdef _DATE_FMT
494 /* This is not available in all glibc versions that have CODESET. */
495 LANGINFO(_DATE_FMT),
496#endif
497 {0, 0}
498};
499
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000500static char nl_langinfo__doc__[] =
501"nl_langinfo(key) -> string\n"
502"Return the value for the locale information associated with key."
503;
504
505static PyObject*
506PyLocale_nl_langinfo(PyObject* self, PyObject* args)
507{
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000508 int item, i;
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000509 if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))
510 return NULL;
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000511 /* Check whether this is a supported constant. GNU libc sometimes
512 returns numeric values in the char* return value, which would
513 crash PyString_FromString. */
514 for (i = 0; langinfo_constants[i].name; i++)
515 if (langinfo_constants[i].value == item)
516 return PyString_FromString(nl_langinfo(item));
517 PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");
518 return NULL;
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000519}
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000520#endif /* HAVE_LANGINFO_H */
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000521
522
Guido van Rossum220ecc81997-11-18 21:03:39 +0000523static struct PyMethodDef PyLocale_Methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000524 {"setlocale", (PyCFunction) PyLocale_setlocale,
525 METH_VARARGS, setlocale__doc__},
526 {"localeconv", (PyCFunction) PyLocale_localeconv,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000527 METH_NOARGS, localeconv__doc__},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000528 {"strcoll", (PyCFunction) PyLocale_strcoll,
529 METH_VARARGS, strcoll__doc__},
530 {"strxfrm", (PyCFunction) PyLocale_strxfrm,
531 METH_VARARGS, strxfrm__doc__},
Jack Jansen307d7a42000-07-15 22:31:45 +0000532#if defined(MS_WIN32) || defined(macintosh)
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000533 {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS},
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000534#endif
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000535#ifdef HAVE_LANGINFO_H
536 {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,
537 METH_VARARGS, nl_langinfo__doc__},
538#endif
539
Guido van Rossum220ecc81997-11-18 21:03:39 +0000540 {NULL, NULL}
541};
542
Guido van Rossum3886bb61998-12-04 18:50:17 +0000543DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000544init_locale(void)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000545{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000546 PyObject *m, *d, *x;
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000547#ifdef HAVE_LANGINFO_H
548 int i;
549#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000550
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000551 m = Py_InitModule("_locale", PyLocale_Methods);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000552
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000553 d = PyModule_GetDict(m);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000554
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000555 x = PyInt_FromLong(LC_CTYPE);
556 PyDict_SetItemString(d, "LC_CTYPE", x);
557 Py_XDECREF(x);
558
559 x = PyInt_FromLong(LC_TIME);
560 PyDict_SetItemString(d, "LC_TIME", x);
561 Py_XDECREF(x);
562
563 x = PyInt_FromLong(LC_COLLATE);
564 PyDict_SetItemString(d, "LC_COLLATE", x);
565 Py_XDECREF(x);
566
567 x = PyInt_FromLong(LC_MONETARY);
568 PyDict_SetItemString(d, "LC_MONETARY", x);
569 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000570
Guido van Rossum8d9c2e31997-12-09 19:35:11 +0000571#ifdef LC_MESSAGES
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000572 x = PyInt_FromLong(LC_MESSAGES);
573 PyDict_SetItemString(d, "LC_MESSAGES", x);
574 Py_XDECREF(x);
Guido van Rossum8d9c2e31997-12-09 19:35:11 +0000575#endif /* LC_MESSAGES */
Guido van Rossum220ecc81997-11-18 21:03:39 +0000576
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000577 x = PyInt_FromLong(LC_NUMERIC);
578 PyDict_SetItemString(d, "LC_NUMERIC", x);
579 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000580
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000581 x = PyInt_FromLong(LC_ALL);
582 PyDict_SetItemString(d, "LC_ALL", x);
583 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000584
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000585 x = PyInt_FromLong(CHAR_MAX);
586 PyDict_SetItemString(d, "CHAR_MAX", x);
587 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000588
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000589 Error = PyErr_NewException("locale.Error", NULL, NULL);
590 PyDict_SetItemString(d, "Error", Error);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000591
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000592 x = PyString_FromString(locale__doc__);
593 PyDict_SetItemString(d, "__doc__", x);
594 Py_XDECREF(x);
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000595
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000596#ifdef HAVE_LANGINFO_H
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000597 for (i = 0; langinfo_constants[i].name; i++) {
598 PyModule_AddIntConstant(m, langinfo_constants[i].name,
599 langinfo_constants[i].value);
600 }
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000601#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000602}