_localemodule.c 18.1 KB
Newer Older
1
/***********************************************************
2
Copyright (C) 1997, 2002 Martin von Loewis
3 4 5 6 7 8

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies.

This software comes with no warranty. Use at your own risk.
9

10 11
******************************************************************/

12 13
#include "Python.h"

14 15 16 17
#include <stdio.h>
#include <errno.h>
#include <locale.h>
#include <string.h>
18
#include <ctype.h>
19

20 21 22 23
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif

24 25 26 27
#ifdef HAVE_LIBINTL_H
#include <libintl.h>
#endif

28
#if defined(MS_WINDOWS)
29
#define WIN32_LEAN_AND_MEAN
30 31 32
#include <windows.h>
#endif

33
#ifdef macintosh
34
#include "macglue.h"
35
#endif
36

37 38 39 40
#ifdef RISCOS
char *strdup(const char *);
#endif

41
PyDoc_STRVAR(locale__doc__, "Support for POSIX locales.");
42 43 44 45 46

static PyObject *Error;

/* support functions for formatting floating point numbers */

47 48
PyDoc_STRVAR(setlocale__doc__,
"(integer,string=None) -> string. Activates/queries locale processing.");
49 50

/* to record the LC_NUMERIC settings */
51 52 53
static PyObject* grouping = NULL;
static PyObject* thousands_sep = NULL;
static PyObject* decimal_point = NULL;
54
/* if non-null, indicates that LC_NUMERIC is different from "C" */
55
static char* saved_numeric = NULL;
56 57 58

/* the grouping is terminated by either 0 or CHAR_MAX */
static PyObject*
59
copy_grouping(char* s)
60
{
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    int i;
    PyObject *result, *val = NULL;

    if (s[0] == '\0')
        /* empty string: no grouping at all */
        return PyList_New(0);

    for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++)
        ; /* nothing */

    result = PyList_New(i+1);
    if (!result)
        return NULL;

    i = -1;
    do {
        i++;
        val = PyInt_FromLong(s[i]);
        if (!val)
            break;
        if (PyList_SetItem(result, i, val)) {
            Py_DECREF(val);
83
            val = NULL;
84 85 86 87 88 89 90
            break;
        }
    } while (s[i] != '\0' && s[i] != CHAR_MAX);

    if (!val) {
        Py_DECREF(result);
        return NULL;
91
    }
92 93

    return result;
94 95 96
}

static void
97
fixup_ulcase(void)
98
{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    PyObject *mods, *strop, *string, *ulo;
    unsigned char ul[256];
    int n, c;

    /* find the string and strop modules */
    mods = PyImport_GetModuleDict();
    if (!mods)
        return;
    string = PyDict_GetItemString(mods, "string");
    if (string)
        string = PyModule_GetDict(string);
    strop=PyDict_GetItemString(mods, "strop");
    if (strop)
        strop = PyModule_GetDict(strop);
    if (!string && !strop)
        return;

    /* create uppercase map string */
    n = 0;
    for (c = 0; c < 256; c++) {
        if (isupper(c))
            ul[n++] = c;
    }
    ulo = PyString_FromStringAndSize((const char *)ul, n);
123
    if (!ulo)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        return;
    if (string)
        PyDict_SetItemString(string, "uppercase", ulo);
    if (strop)
        PyDict_SetItemString(strop, "uppercase", ulo);
    Py_DECREF(ulo);

    /* create lowercase string */
    n = 0;
    for (c = 0; c < 256; c++) {
        if (islower(c))
            ul[n++] = c;
    }
    ulo = PyString_FromStringAndSize((const char *)ul, n);
    if (!ulo)
        return;
    if (string)
        PyDict_SetItemString(string, "lowercase", ulo);
    if (strop)
        PyDict_SetItemString(strop, "lowercase", ulo);
    Py_DECREF(ulo);

    /* create letters string */
    n = 0;
    for (c = 0; c < 256; c++) {
        if (isalpha(c))
            ul[n++] = c;
    }
    ulo = PyString_FromStringAndSize((const char *)ul, n);
    if (!ulo)
        return;
    if (string)
        PyDict_SetItemString(string, "letters", ulo);
    Py_DECREF(ulo);
158
}
159 160 161 162

#if defined(HAVE_LANGINFO_H) && defined(CODESET)
static int fileencoding_uses_locale = 0;
#endif
163 164

static PyObject*
165
PyLocale_setlocale(PyObject* self, PyObject* args)
166
{
167 168 169 170 171 172
    int category;
    char *locale = NULL, *result;
    PyObject *result_object;
    struct lconv *lc;

    if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
173
        return NULL;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

    if (locale) {
        /* set locale */
        result = setlocale(category, locale);
        if (!result) {
            /* operation failed, no setting was changed */
            PyErr_SetString(Error, "locale setting not supported");
            return NULL;
        }
        result_object = PyString_FromString(result);
        if (!result)
            return NULL;
        /* record changes to LC_NUMERIC */
        if (category == LC_NUMERIC || category == LC_ALL) {
            if (strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0) {
                /* user just asked for default numeric locale */
                if (saved_numeric)
                    free(saved_numeric);
                saved_numeric = NULL;
            } else {
                /* remember values */
                lc = localeconv();
                Py_XDECREF(grouping);
                grouping = copy_grouping(lc->grouping);
                Py_XDECREF(thousands_sep);
                thousands_sep = PyString_FromString(lc->thousands_sep);
                Py_XDECREF(decimal_point);
                decimal_point = PyString_FromString(lc->decimal_point);
                saved_numeric = strdup(locale);
                /* restore to "C" */
                setlocale(LC_NUMERIC, "C");
            }
        }
        /* record changes to LC_CTYPE */
        if (category == LC_CTYPE || category == LC_ALL)
            fixup_ulcase();
        /* things that got wrong up to here are ignored */
        PyErr_Clear();
212 213 214 215 216 217 218 219 220
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
	if (Py_FileSystemDefaultEncoding == NULL)
	    fileencoding_uses_locale = 1;
	if (fileencoding_uses_locale) {
	    char *codeset = nl_langinfo(CODESET);
	    PyObject *enc = NULL;
	    if (*codeset && (enc = PyCodec_Encoder(codeset))) {
		/* Release previous file encoding */
		if (Py_FileSystemDefaultEncoding)
221
		    free((char *)Py_FileSystemDefaultEncoding);
222 223 224 225 226 227
		Py_FileSystemDefaultEncoding = strdup(codeset);
		Py_DECREF(enc);
	    } else
		PyErr_Clear();
	}
#endif
228 229 230 231 232 233 234 235 236 237 238 239 240 241
    } else {
        /* get locale */
        /* restore LC_NUMERIC first, if appropriate */
        if (saved_numeric)
            setlocale(LC_NUMERIC, saved_numeric);
        result = setlocale(category, NULL);
        if (!result) {
            PyErr_SetString(Error, "locale query failed");
            return NULL;
        }
        result_object = PyString_FromString(result);
        /* restore back to "C" */
        if (saved_numeric)
            setlocale(LC_NUMERIC, "C");
242
    }
243
    return result_object;
244 245
}

246 247
PyDoc_STRVAR(localeconv__doc__,
"() -> dict. Returns numeric and monetary locale-specific parameters.");
248 249

static PyObject*
250
PyLocale_localeconv(PyObject* self)
251
{
252 253 254 255 256
    PyObject* result;
    struct lconv *l;
    PyObject *x;

    result = PyDict_New();
257 258
    if (!result)
        return NULL;
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

    /* if LC_NUMERIC is different in the C library, use saved value */
    l = localeconv();

    /* hopefully, the localeconv result survives the C library calls
       involved herein */

#define RESULT_STRING(s)\
    x = PyString_FromString(l->s);\
    if (!x) goto failed;\
    PyDict_SetItemString(result, #s, x);\
    Py_XDECREF(x)

#define RESULT_INT(i)\
    x = PyInt_FromLong(l->i);\
    if (!x) goto failed;\
    PyDict_SetItemString(result, #i, x);\
    Py_XDECREF(x)
277 278

    /* Numeric information */
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    if (saved_numeric){
        /* cannot use localeconv results */
        PyDict_SetItemString(result, "decimal_point", decimal_point);
        PyDict_SetItemString(result, "grouping", grouping);
        PyDict_SetItemString(result, "thousands_sep", thousands_sep);
    } else {
        RESULT_STRING(decimal_point);
        RESULT_STRING(thousands_sep);
        x = copy_grouping(l->grouping);
        if (!x)
            goto failed;
        PyDict_SetItemString(result, "grouping", x);
        Py_XDECREF(x);
    }

    /* Monetary information */
    RESULT_STRING(int_curr_symbol);
    RESULT_STRING(currency_symbol);
    RESULT_STRING(mon_decimal_point);
    RESULT_STRING(mon_thousands_sep);
    x = copy_grouping(l->mon_grouping);
    if (!x)
        goto failed;
    PyDict_SetItemString(result, "mon_grouping", x);
303
    Py_XDECREF(x);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    RESULT_STRING(positive_sign);
    RESULT_STRING(negative_sign);
    RESULT_INT(int_frac_digits);
    RESULT_INT(frac_digits);
    RESULT_INT(p_cs_precedes);
    RESULT_INT(p_sep_by_space);
    RESULT_INT(n_cs_precedes);
    RESULT_INT(n_sep_by_space);
    RESULT_INT(p_sign_posn);
    RESULT_INT(n_sign_posn);
    return result;

  failed:
    Py_XDECREF(result);
    Py_XDECREF(x);
    return NULL;
320 321
}

322 323
PyDoc_STRVAR(strcoll__doc__,
"string,string -> int. Compares two strings according to the locale.");
324 325

static PyObject*
326
PyLocale_strcoll(PyObject* self, PyObject* args)
327 328
{
  char *s1,*s2;
329 330 331 332

  if (!PyArg_ParseTuple(args, "ss:strcoll", &s1, &s2))
      return NULL;
  return PyInt_FromLong(strcoll(s1, s2));
333 334
}

335 336
PyDoc_STRVAR(strxfrm__doc__,
"string -> string. Returns a string that behaves for cmp locale-aware.");
337 338

static PyObject*
339
PyLocale_strxfrm(PyObject* self, PyObject* args)
340
{
341 342 343 344
    char *s, *buf;
    size_t n1, n2;
    PyObject *result;

345
    if (!PyArg_ParseTuple(args, "s:strxfrm", &s))
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        return NULL;

    /* assume no change in size, first */
    n1 = strlen(s) + 1;
    buf = PyMem_Malloc(n1);
    if (!buf)
        return PyErr_NoMemory();
    n2 = strxfrm(buf, s, n1);
    if (n2 > n1) {
        /* more space needed */
        buf = PyMem_Realloc(buf, n2);
        if (!buf)
            return PyErr_NoMemory();
        strxfrm(buf, s, n2);
    }
    result = PyString_FromString(buf);
    PyMem_Free(buf);
    return result;
364 365
}

366
#if defined(MS_WINDOWS)
367
static PyObject*
368
PyLocale_getdefaultlocale(PyObject* self)
369 370 371 372
{
    char encoding[100];
    char locale[100];

373
    PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

    if (GetLocaleInfo(LOCALE_USER_DEFAULT,
                      LOCALE_SISO639LANGNAME,
                      locale, sizeof(locale))) {
        int i = strlen(locale);
        locale[i++] = '_';
        if (GetLocaleInfo(LOCALE_USER_DEFAULT,
                          LOCALE_SISO3166CTRYNAME,
                          locale+i, sizeof(locale)-i))
            return Py_BuildValue("ss", locale, encoding);
    }

    /* If we end up here, this windows version didn't know about
       ISO639/ISO3166 names (it's probably Windows 95).  Return the
       Windows language identifier instead (a hexadecimal number) */

    locale[0] = '0';
    locale[1] = 'x';
    if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
                      locale+2, sizeof(locale)-2)) {
        return Py_BuildValue("ss", locale, encoding);
    }

    /* cannot determine the language code (very unlikely) */
    Py_INCREF(Py_None);
    return Py_BuildValue("Os", Py_None, encoding);
}
#endif

403 404
#if defined(macintosh)
static PyObject*
405
PyLocale_getdefaultlocale(PyObject* self)
406 407 408 409 410
{
    return Py_BuildValue("Os", Py_None, PyMac_getscript());
}
#endif

411
#ifdef HAVE_LANGINFO_H
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
#define LANGINFO(X) {#X, X}
struct langinfo_constant{
	char* name;
	int value;
} langinfo_constants[] = 
{
    /* These constants should exist on any langinfo implementation */
    LANGINFO(DAY_1),
    LANGINFO(DAY_2),
    LANGINFO(DAY_3),
    LANGINFO(DAY_4),
    LANGINFO(DAY_5),
    LANGINFO(DAY_6),
    LANGINFO(DAY_7),

    LANGINFO(ABDAY_1),
    LANGINFO(ABDAY_2),
    LANGINFO(ABDAY_3),
    LANGINFO(ABDAY_4),
    LANGINFO(ABDAY_5),
    LANGINFO(ABDAY_6),
    LANGINFO(ABDAY_7),

    LANGINFO(MON_1),
    LANGINFO(MON_2),
    LANGINFO(MON_3),
    LANGINFO(MON_4),
    LANGINFO(MON_5),
    LANGINFO(MON_6),
    LANGINFO(MON_7),
    LANGINFO(MON_8),
    LANGINFO(MON_9),
    LANGINFO(MON_10),
    LANGINFO(MON_11),
    LANGINFO(MON_12),

    LANGINFO(ABMON_1),
    LANGINFO(ABMON_2),
    LANGINFO(ABMON_3),
    LANGINFO(ABMON_4),
    LANGINFO(ABMON_5),
    LANGINFO(ABMON_6),
    LANGINFO(ABMON_7),
    LANGINFO(ABMON_8),
    LANGINFO(ABMON_9),
    LANGINFO(ABMON_10),
    LANGINFO(ABMON_11),
    LANGINFO(ABMON_12),

#ifdef RADIXCHAR
    /* The following are not available with glibc 2.0 */
    LANGINFO(RADIXCHAR),
    LANGINFO(THOUSEP),
    /* YESSTR and NOSTR are deprecated in glibc, since they are
       a special case of message translation, which should be rather
       done using gettext. So we don't expose it to Python in the
       first place.
    LANGINFO(YESSTR),
    LANGINFO(NOSTR),
    */
    LANGINFO(CRNCYSTR),
#endif

    LANGINFO(D_T_FMT),
    LANGINFO(D_FMT),
    LANGINFO(T_FMT),
    LANGINFO(AM_STR),
    LANGINFO(PM_STR),

481 482 483 484 485
    /* The following constants are available only with XPG4, but...
       AIX 3.2. only has CODESET.
       OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have
       a few of the others.
       Solution: ifdef-test them all. */
486 487
#ifdef CODESET
    LANGINFO(CODESET),
488 489
#endif
#ifdef T_FMT_AMPM
490
    LANGINFO(T_FMT_AMPM),
491 492
#endif
#ifdef ERA
493
    LANGINFO(ERA),
494 495
#endif
#ifdef ERA_D_FMT
496
    LANGINFO(ERA_D_FMT),
497 498
#endif
#ifdef ERA_D_T_FMT
499
    LANGINFO(ERA_D_T_FMT),
500 501
#endif
#ifdef ERA_T_FMT
502
    LANGINFO(ERA_T_FMT),
503 504
#endif
#ifdef ALT_DIGITS
505
    LANGINFO(ALT_DIGITS),
506 507
#endif
#ifdef YESEXPR
508
    LANGINFO(YESEXPR),
509 510
#endif
#ifdef NOEXPR
511 512 513 514 515 516 517 518 519
    LANGINFO(NOEXPR),
#endif
#ifdef _DATE_FMT
    /* This is not available in all glibc versions that have CODESET. */
    LANGINFO(_DATE_FMT),
#endif
    {0, 0}
};

520
PyDoc_STRVAR(nl_langinfo__doc__,
521
"nl_langinfo(key) -> string\n"
522
"Return the value for the locale information associated with key.");
523 524 525 526

static PyObject*
PyLocale_nl_langinfo(PyObject* self, PyObject* args)
{
527
    int item, i;
528 529
    if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))
        return NULL;
530 531 532 533 534 535 536 537
    /* Check whether this is a supported constant. GNU libc sometimes
       returns numeric values in the char* return value, which would
       crash PyString_FromString.  */
    for (i = 0; langinfo_constants[i].name; i++)
	    if (langinfo_constants[i].value == item)
		    return PyString_FromString(nl_langinfo(item));
    PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");
    return NULL;
538
}
539
#endif /* HAVE_LANGINFO_H */
540 541 542

#ifdef HAVE_LIBINTL_H

543
PyDoc_STRVAR(gettext__doc__,
544
"gettext(msg) -> string\n"
545
"Return translation of msg.");
546 547 548 549 550 551 552 553 554 555

static PyObject*
PyIntl_gettext(PyObject* self, PyObject *args)
{
	char *in;
	if (!PyArg_ParseTuple(args, "z", &in))
		return 0;
	return PyString_FromString(gettext(in));
}

556
PyDoc_STRVAR(dgettext__doc__,
557
"dgettext(domain, msg) -> string\n"
558
"Return translation of msg in domain.");
559 560 561 562 563 564 565 566 567 568

static PyObject*
PyIntl_dgettext(PyObject* self, PyObject *args)
{
	char *domain, *in;
	if (!PyArg_ParseTuple(args, "zz", &domain, &in))
		return 0;
	return PyString_FromString(dgettext(domain, in));
}

569
PyDoc_STRVAR(dcgettext__doc__,
570
"dcgettext(domain, msg, category) -> string\n"
571
"Return translation of msg in domain and category.");
572 573 574 575 576 577 578 579 580 581 582

static PyObject*
PyIntl_dcgettext(PyObject *self, PyObject *args)
{
	char *domain, *msgid;
	int category;
	if (!PyArg_ParseTuple(args, "zzi", &domain, &msgid, &category))
		return 0;
	return PyString_FromString(dcgettext(domain,msgid,category));
}

583
PyDoc_STRVAR(textdomain__doc__,
584
"textdomain(domain) -> string\n"
585
"Set the C library's textdmain to domain, returning the new domain.");
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

static PyObject*
PyIntl_textdomain(PyObject* self, PyObject* args)
{
	char *domain;
	if (!PyArg_ParseTuple(args, "z", &domain))
		return 0;
	domain = textdomain(domain);
	if (!domain) {
		PyErr_SetFromErrno(PyExc_OSError);
		return NULL;
	}
	return PyString_FromString(domain);
}

601
PyDoc_STRVAR(bindtextdomain__doc__,
602
"bindtextdomain(domain, dir) -> string\n"
603
"Bind the C library's domain to dir.");
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

static PyObject*
PyIntl_bindtextdomain(PyObject* self,PyObject*args)
{
	char *domain,*dirname;
	if (!PyArg_ParseTuple(args, "zz", &domain, &dirname))
		return 0;
	dirname = bindtextdomain(domain, dirname);
	if (!dirname) {
		PyErr_SetFromErrno(PyExc_OSError);
		return NULL;
	}
	return PyString_FromString(dirname);
}

#endif
620

621
static struct PyMethodDef PyLocale_Methods[] = {
622 623 624
  {"setlocale", (PyCFunction) PyLocale_setlocale, 
   METH_VARARGS, setlocale__doc__},
  {"localeconv", (PyCFunction) PyLocale_localeconv, 
625
   METH_NOARGS, localeconv__doc__},
626 627 628 629
  {"strcoll", (PyCFunction) PyLocale_strcoll, 
   METH_VARARGS, strcoll__doc__},
  {"strxfrm", (PyCFunction) PyLocale_strxfrm, 
   METH_VARARGS, strxfrm__doc__},
630
#if defined(MS_WINDOWS) || defined(macintosh)
631
  {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS},
632
#endif
633 634 635 636
#ifdef HAVE_LANGINFO_H
  {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,
   METH_VARARGS, nl_langinfo__doc__},
#endif
637
#ifdef HAVE_LIBINTL_H
638 639 640 641 642 643 644 645 646 647 648
  {"gettext",(PyCFunction)PyIntl_gettext,METH_VARARGS,
    gettext__doc__},
  {"dgettext",(PyCFunction)PyIntl_dgettext,METH_VARARGS,
   dgettext__doc__},
  {"dcgettext",(PyCFunction)PyIntl_dcgettext,METH_VARARGS,
    dcgettext__doc__},
  {"textdomain",(PyCFunction)PyIntl_textdomain,METH_VARARGS,
   textdomain__doc__},
  {"bindtextdomain",(PyCFunction)PyIntl_bindtextdomain,METH_VARARGS,
   bindtextdomain__doc__},
#endif  
649 650 651
  {NULL, NULL}
};

652
PyMODINIT_FUNC
653
init_locale(void)
654
{
655
    PyObject *m, *d, *x;
656 657 658
#ifdef HAVE_LANGINFO_H
    int i;
#endif
659

660
    m = Py_InitModule("_locale", PyLocale_Methods);
661

662
    d = PyModule_GetDict(m);
663

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    x = PyInt_FromLong(LC_CTYPE);
    PyDict_SetItemString(d, "LC_CTYPE", x);
    Py_XDECREF(x);

    x = PyInt_FromLong(LC_TIME);
    PyDict_SetItemString(d, "LC_TIME", x);
    Py_XDECREF(x);

    x = PyInt_FromLong(LC_COLLATE);
    PyDict_SetItemString(d, "LC_COLLATE", x);
    Py_XDECREF(x);

    x = PyInt_FromLong(LC_MONETARY);
    PyDict_SetItemString(d, "LC_MONETARY", x);
    Py_XDECREF(x);
679

680
#ifdef LC_MESSAGES
681 682 683
    x = PyInt_FromLong(LC_MESSAGES);
    PyDict_SetItemString(d, "LC_MESSAGES", x);
    Py_XDECREF(x);
684
#endif /* LC_MESSAGES */
685

686 687 688
    x = PyInt_FromLong(LC_NUMERIC);
    PyDict_SetItemString(d, "LC_NUMERIC", x);
    Py_XDECREF(x);
689

690 691 692
    x = PyInt_FromLong(LC_ALL);
    PyDict_SetItemString(d, "LC_ALL", x);
    Py_XDECREF(x);
693

694 695 696
    x = PyInt_FromLong(CHAR_MAX);
    PyDict_SetItemString(d, "CHAR_MAX", x);
    Py_XDECREF(x);
697

698 699
    Error = PyErr_NewException("locale.Error", NULL, NULL);
    PyDict_SetItemString(d, "Error", Error);
700

701 702 703
    x = PyString_FromString(locale__doc__);
    PyDict_SetItemString(d, "__doc__", x);
    Py_XDECREF(x);
704 705

#ifdef HAVE_LANGINFO_H
706 707 708 709
    for (i = 0; langinfo_constants[i].name; i++) {
	    PyModule_AddIntConstant(m, langinfo_constants[i].name,
				    langinfo_constants[i].value);
    }
710
#endif
711
}