_localemodule.c 18.3 KB
Newer Older
1
/***********************************************************
2
Copyright (C) 1997, 2002, 2003, 2007, 2008 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
#include <stdio.h>
#include <locale.h>
#include <string.h>
17
#include <ctype.h>
18

19
#ifdef HAVE_ERRNO_H
20 21 22
#include <errno.h>
#endif

23 24 25 26
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif

27 28 29 30
#ifdef HAVE_LIBINTL_H
#include <libintl.h>
#endif

31 32 33 34
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif

35
#if defined(__APPLE__)
36
#include <CoreFoundation/CoreFoundation.h>
37 38
#endif

39
#if defined(MS_WINDOWS)
40
#define WIN32_LEAN_AND_MEAN
41 42 43
#include <windows.h>
#endif

44
PyDoc_STRVAR(locale__doc__, "Support for POSIX locales.");
45 46 47

static PyObject *Error;

48 49 50 51
/* Convert a char* to a Unicode object according to the current locale */
static PyObject*
str2uni(const char* s)
{
52 53 54
#ifdef HAVE_BROKEN_MBSTOWCS
    size_t needed = strlen(s);
#else
55
    size_t needed = mbstowcs(NULL, s, 0);
56
#endif
57 58 59 60 61 62 63 64
    size_t res1;
    wchar_t smallbuf[30];
    wchar_t *dest;
    PyObject *res2;
    if (needed == (size_t)-1) {
        PyErr_SetString(PyExc_ValueError, "Cannot convert byte to string");
        return NULL;
    }
65
    if (needed*sizeof(wchar_t) < sizeof(smallbuf))
66 67
        dest = smallbuf;
    else {
68
        dest = PyMem_Malloc((needed+1)*sizeof(wchar_t));
69 70 71 72 73
        if (!dest)
            return PyErr_NoMemory();
    }
    /* This shouldn't fail now */
    res1 = mbstowcs(dest, s, needed+1);
74 75 76
#ifdef HAVE_BROKEN_MBSTOWCS
    assert(res1 != (size_t)-1);
#else
77
    assert(res1 == needed);
78
#endif
79 80 81 82 83 84
    res2 = PyUnicode_FromWideChar(dest, res1);
    if (dest != smallbuf)
        PyMem_Free(dest);
    return res2;
}        

85 86
/* support functions for formatting floating point numbers */

87 88
PyDoc_STRVAR(setlocale__doc__,
"(integer,string=None) -> string. Activates/queries locale processing.");
89 90 91

/* the grouping is terminated by either 0 or CHAR_MAX */
static PyObject*
92
copy_grouping(char* s)
93
{
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    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++;
111
        val = PyLong_FromLong(s[i]);
112 113 114 115
        if (!val)
            break;
        if (PyList_SetItem(result, i, val)) {
            Py_DECREF(val);
116
            val = NULL;
117 118 119 120 121 122 123
            break;
        }
    } while (s[i] != '\0' && s[i] != CHAR_MAX);

    if (!val) {
        Py_DECREF(result);
        return NULL;
124
    }
125 126

    return result;
127 128 129
}

static PyObject*
130
PyLocale_setlocale(PyObject* self, PyObject* args)
131
{
132 133 134 135 136
    int category;
    char *locale = NULL, *result;
    PyObject *result_object;

    if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
137
        return NULL;
138 139 140 141 142 143

    if (locale) {
        /* set locale */
        result = setlocale(category, locale);
        if (!result) {
            /* operation failed, no setting was changed */
144
            PyErr_SetString(Error, "unsupported locale setting");
145 146
            return NULL;
        }
147
        result_object = str2uni(result);
148
        if (!result_object)
149 150 151 152 153 154 155 156
            return NULL;
    } else {
        /* get locale */
        result = setlocale(category, NULL);
        if (!result) {
            PyErr_SetString(Error, "locale query failed");
            return NULL;
        }
157
        result_object = str2uni(result);
158
    }
159
    return result_object;
160 161
}

162 163
PyDoc_STRVAR(localeconv__doc__,
"() -> dict. Returns numeric and monetary locale-specific parameters.");
164 165

static PyObject*
166
PyLocale_localeconv(PyObject* self)
167
{
168 169 170 171 172
    PyObject* result;
    struct lconv *l;
    PyObject *x;

    result = PyDict_New();
173 174
    if (!result)
        return NULL;
175 176 177 178 179 180 181 182

    /* 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)\
183
    x = str2uni(l->s);   \
184 185 186 187 188
    if (!x) goto failed;\
    PyDict_SetItemString(result, #s, x);\
    Py_XDECREF(x)

#define RESULT_INT(i)\
189
    x = PyLong_FromLong(l->i);\
190 191 192
    if (!x) goto failed;\
    PyDict_SetItemString(result, #i, x);\
    Py_XDECREF(x)
193 194

    /* Numeric information */
195 196 197 198 199 200 201
    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);
202 203 204 205 206 207 208 209 210 211

    /* 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);
212
    Py_XDECREF(x);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    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;
229 230
}

231
#if defined(HAVE_WCSCOLL)
232 233
PyDoc_STRVAR(strcoll__doc__,
"string,string -> int. Compares two strings according to the locale.");
234 235

static PyObject*
236
PyLocale_strcoll(PyObject* self, PyObject* args)
237
{
238 239
    PyObject *os1, *os2, *result = NULL;
    wchar_t *ws1 = NULL, *ws2 = NULL;
240
    Py_ssize_t len1, len2;
241
    
242
    if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2))
243 244 245 246 247 248 249 250 251 252
        return NULL;
    /* Convert the unicode strings to wchar[]. */
    len1 = PyUnicode_GET_SIZE(os1) + 1;
    ws1 = PyMem_MALLOC(len1 * sizeof(wchar_t));
    if (!ws1) {
        PyErr_NoMemory();
        goto done;
    }
    if (PyUnicode_AsWideChar((PyUnicodeObject*)os1, ws1, len1) == -1)
        goto done;
253 254
    ws1[len1 - 1] = 0;
    len2 = PyUnicode_GET_SIZE(os2) + 1;
255 256 257 258 259 260 261
    ws2 = PyMem_MALLOC(len2 * sizeof(wchar_t));
    if (!ws2) {
        PyErr_NoMemory();
        goto done;
    }
    if (PyUnicode_AsWideChar((PyUnicodeObject*)os2, ws2, len2) == -1)
        goto done;
262
    ws2[len2 - 1] = 0;
263
    /* Collate the strings. */
264
    result = PyLong_FromLong(wcscoll(ws1, ws2));
265 266 267 268 269
  done:
    /* Deallocate everything. */
    if (ws1) PyMem_FREE(ws1);
    if (ws2) PyMem_FREE(ws2);
    return result;
270
}
271
#endif
272

273
#ifdef HAVE_WCSXFRM
274
PyDoc_STRVAR(strxfrm__doc__,
275 276 277
"strxfrm(string) -> string.\n\
\n\
Return a string that can be used as a key for locale-aware comparisons.");
278 279

static PyObject*
280
PyLocale_strxfrm(PyObject* self, PyObject* args)
281
{
282 283 284
    Py_UNICODE *s0;
    Py_ssize_t n0;
    wchar_t *s, *buf = NULL;
285
    size_t n1, n2;
286 287
    PyObject *result = NULL;
    Py_ssize_t i;
288

289
    if (!PyArg_ParseTuple(args, "u#:strxfrm", &s0, &n0))
290 291
        return NULL;

292 293 294
#ifdef HAVE_USABLE_WCHAR_T
    s = s0;
#else
295
    s = PyMem_Malloc((n0+1)*sizeof(wchar_t));
296 297 298 299 300 301
    if (!s)
        return PyErr_NoMemory();
    for (i=0; i<=n0; i++)
        s[i] = s0[i];
#endif

302
    /* assume no change in size, first */
303
    n1 = wcslen(s) + 1;
304
    buf = PyMem_Malloc(n1*sizeof(wchar_t));
305 306 307 308 309 310
    if (!buf) {
        PyErr_NoMemory();
        goto exit;
    }
    n2 = wcsxfrm(buf, s, n1);
    if (n2 >= n1) {
311
        /* more space needed */
312
        buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
313 314 315 316 317
        if (!buf) {
            PyErr_NoMemory();
            goto exit;
        }
        n2 = wcsxfrm(buf, s, n2);
318
    }
319 320 321 322 323 324
    result = PyUnicode_FromWideChar(buf, n2);
 exit:
    if (buf) PyMem_Free(buf);
#ifdef HAVE_USABLE_WCHAR_T
    PyMem_Free(s);
#endif
325
    return result;
326
}
327
#endif
328

329
#if defined(MS_WINDOWS)
330
static PyObject*
331
PyLocale_getdefaultlocale(PyObject* self)
332 333 334 335
{
    char encoding[100];
    char locale[100];

336
    PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
337 338 339 340

    if (GetLocaleInfo(LOCALE_USER_DEFAULT,
                      LOCALE_SISO639LANGNAME,
                      locale, sizeof(locale))) {
Martin v. Löwis's avatar
Martin v. Löwis committed
341
        Py_ssize_t i = strlen(locale);
342 343 344
        locale[i++] = '_';
        if (GetLocaleInfo(LOCALE_USER_DEFAULT,
                          LOCALE_SISO3166CTRYNAME,
Martin v. Löwis's avatar
Martin v. Löwis committed
345
                          locale+i, (int)(sizeof(locale)-i)))
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
            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

366
#if defined(__APPLE__)
367 368
/*
** Find out what the current script is.
369
** Donated by Fredrik Lundh.
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
*/
static char *mac_getscript(void)
{
    CFStringEncoding enc = CFStringGetSystemEncoding();
    static CFStringRef name = NULL;
    /* Return the code name for the encodings for which we have codecs. */
    switch(enc) {
    case kCFStringEncodingMacRoman: return "mac-roman";
    case kCFStringEncodingMacGreek: return "mac-greek";
    case kCFStringEncodingMacCyrillic: return "mac-cyrillic";
    case kCFStringEncodingMacTurkish: return "mac-turkish";
    case kCFStringEncodingMacIcelandic: return "mac-icelandic";
    /* XXX which one is mac-latin2? */
    }
    if (!name) {
385
        /* This leaks an object. */
386 387 388 389 390
        name = CFStringConvertEncodingToIANACharSetName(enc);
    }
    return (char *)CFStringGetCStringPtr(name, 0); 
}

391
static PyObject*
392
PyLocale_getdefaultlocale(PyObject* self)
393
{
394
    return Py_BuildValue("Os", Py_None, mac_getscript());
395 396 397
}
#endif

398
#ifdef HAVE_LANGINFO_H
399
#define LANGINFO(X) {#X, X}
400
static struct langinfo_constant{
401 402 403 404 405 406 407 408 409 410 411 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
	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),

468 469 470 471 472
    /* 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. */
473 474
#ifdef CODESET
    LANGINFO(CODESET),
475 476
#endif
#ifdef T_FMT_AMPM
477
    LANGINFO(T_FMT_AMPM),
478 479
#endif
#ifdef ERA
480
    LANGINFO(ERA),
481 482
#endif
#ifdef ERA_D_FMT
483
    LANGINFO(ERA_D_FMT),
484 485
#endif
#ifdef ERA_D_T_FMT
486
    LANGINFO(ERA_D_T_FMT),
487 488
#endif
#ifdef ERA_T_FMT
489
    LANGINFO(ERA_T_FMT),
490 491
#endif
#ifdef ALT_DIGITS
492
    LANGINFO(ALT_DIGITS),
493 494
#endif
#ifdef YESEXPR
495
    LANGINFO(YESEXPR),
496 497
#endif
#ifdef NOEXPR
498 499 500 501 502 503 504 505 506
    LANGINFO(NOEXPR),
#endif
#ifdef _DATE_FMT
    /* This is not available in all glibc versions that have CODESET. */
    LANGINFO(_DATE_FMT),
#endif
    {0, 0}
};

507
PyDoc_STRVAR(nl_langinfo__doc__,
508
"nl_langinfo(key) -> string\n"
509
"Return the value for the locale information associated with key.");
510 511 512 513

static PyObject*
PyLocale_nl_langinfo(PyObject* self, PyObject* args)
{
514
    int item, i;
515 516
    if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))
        return NULL;
517 518
    /* Check whether this is a supported constant. GNU libc sometimes
       returns numeric values in the char* return value, which would
Neal Norwitz's avatar
Neal Norwitz committed
519
       crash PyUnicode_FromString.  */
520
    for (i = 0; langinfo_constants[i].name; i++)
521 522 523 524
        if (langinfo_constants[i].value == item) {
            /* Check NULL as a workaround for GNU libc's returning NULL
               instead of an empty string for nl_langinfo(ERA).  */
            const char *result = nl_langinfo(item);
525
            result = result != NULL ? result : "";
526
            return str2uni(result);
527
        }
528 529
    PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");
    return NULL;
530
}
531
#endif /* HAVE_LANGINFO_H */
532 533 534

#ifdef HAVE_LIBINTL_H

535
PyDoc_STRVAR(gettext__doc__,
536
"gettext(msg) -> string\n"
537
"Return translation of msg.");
538 539 540 541 542

static PyObject*
PyIntl_gettext(PyObject* self, PyObject *args)
{
	char *in;
Georg Brandl's avatar
Georg Brandl committed
543
	if (!PyArg_ParseTuple(args, "s", &in))
544
		return 0;
545
	return str2uni(gettext(in));
546 547
}

548
PyDoc_STRVAR(dgettext__doc__,
549
"dgettext(domain, msg) -> string\n"
550
"Return translation of msg in domain.");
551 552 553 554 555

static PyObject*
PyIntl_dgettext(PyObject* self, PyObject *args)
{
	char *domain, *in;
Georg Brandl's avatar
Georg Brandl committed
556
	if (!PyArg_ParseTuple(args, "zs", &domain, &in))
557
		return 0;
558
	return str2uni(dgettext(domain, in));
559 560
}

561
PyDoc_STRVAR(dcgettext__doc__,
562
"dcgettext(domain, msg, category) -> string\n"
563
"Return translation of msg in domain and category.");
564 565 566 567 568 569

static PyObject*
PyIntl_dcgettext(PyObject *self, PyObject *args)
{
	char *domain, *msgid;
	int category;
Georg Brandl's avatar
Georg Brandl committed
570
	if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))
571
		return 0;
572
	return str2uni(dcgettext(domain,msgid,category));
573 574
}

575
PyDoc_STRVAR(textdomain__doc__,
576
"textdomain(domain) -> string\n"
577
"Set the C library's textdmain to domain, returning the new domain.");
578 579 580 581 582 583 584 585 586 587 588 589

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;
	}
590
	return str2uni(domain);
591 592
}

593
PyDoc_STRVAR(bindtextdomain__doc__,
594
"bindtextdomain(domain, dir) -> string\n"
595
"Bind the C library's domain to dir.");
596 597 598 599

static PyObject*
PyIntl_bindtextdomain(PyObject* self,PyObject*args)
{
Georg Brandl's avatar
Georg Brandl committed
600 601
	char *domain, *dirname;
	if (!PyArg_ParseTuple(args, "sz", &domain, &dirname))
602
		return 0;
Georg Brandl's avatar
Georg Brandl committed
603 604 605 606
	if (!strlen(domain)) {
		PyErr_SetString(Error, "domain must be a non-empty string");
		return 0;
	}
607 608 609 610 611
	dirname = bindtextdomain(domain, dirname);
	if (!dirname) {
		PyErr_SetFromErrno(PyExc_OSError);
		return NULL;
	}
612
	return str2uni(dirname);
613 614
}

615 616 617 618 619 620 621 622 623 624 625 626 627
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
PyDoc_STRVAR(bind_textdomain_codeset__doc__,
"bind_textdomain_codeset(domain, codeset) -> string\n"
"Bind the C library's domain to codeset.");

static PyObject*
PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)
{
	char *domain,*codeset;
	if (!PyArg_ParseTuple(args, "sz", &domain, &codeset))
		return NULL;
	codeset = bind_textdomain_codeset(domain, codeset);
	if (codeset)
628
		return str2uni(codeset);
629 630 631 632
	Py_RETURN_NONE;
}
#endif

633
#endif
634

635
static struct PyMethodDef PyLocale_Methods[] = {
636 637 638
  {"setlocale", (PyCFunction) PyLocale_setlocale, 
   METH_VARARGS, setlocale__doc__},
  {"localeconv", (PyCFunction) PyLocale_localeconv, 
639
   METH_NOARGS, localeconv__doc__},
640
#ifdef HAVE_WCSCOLL
641 642
  {"strcoll", (PyCFunction) PyLocale_strcoll, 
   METH_VARARGS, strcoll__doc__},
643 644
#endif
#ifdef HAVE_WCSXFRM
645 646
  {"strxfrm", (PyCFunction) PyLocale_strxfrm, 
   METH_VARARGS, strxfrm__doc__},
647
#endif
648
#if defined(MS_WINDOWS) || defined(__APPLE__)
649
  {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS},
650
#endif
651 652 653 654
#ifdef HAVE_LANGINFO_H
  {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,
   METH_VARARGS, nl_langinfo__doc__},
#endif
655
#ifdef HAVE_LIBINTL_H
656 657 658 659 660 661 662 663 664 665
  {"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__},
666 667 668 669
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  {"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset,
   METH_VARARGS, bind_textdomain_codeset__doc__},
#endif
670
#endif  
671 672 673
  {NULL, NULL}
};

674 675 676 677 678 679 680 681 682 683 684 685 686

static struct PyModuleDef _localemodule = {
	PyModuleDef_HEAD_INIT,
	"_locale",
	locale__doc__,
	-1,
	PyLocale_Methods,
	NULL,
	NULL,
	NULL,
	NULL
};

687
PyMODINIT_FUNC
688
PyInit__locale(void)
689
{
690
    PyObject *m, *d, *x;
691 692 693
#ifdef HAVE_LANGINFO_H
    int i;
#endif
694

695
    m = PyModule_Create(&_localemodule);
696
    if (m == NULL)
697
    	return NULL;
698

699
    d = PyModule_GetDict(m);
700

701
    x = PyLong_FromLong(LC_CTYPE);
702 703 704
    PyDict_SetItemString(d, "LC_CTYPE", x);
    Py_XDECREF(x);

705
    x = PyLong_FromLong(LC_TIME);
706 707 708
    PyDict_SetItemString(d, "LC_TIME", x);
    Py_XDECREF(x);

709
    x = PyLong_FromLong(LC_COLLATE);
710 711 712
    PyDict_SetItemString(d, "LC_COLLATE", x);
    Py_XDECREF(x);

713
    x = PyLong_FromLong(LC_MONETARY);
714 715
    PyDict_SetItemString(d, "LC_MONETARY", x);
    Py_XDECREF(x);
716

717
#ifdef LC_MESSAGES
718
    x = PyLong_FromLong(LC_MESSAGES);
719 720
    PyDict_SetItemString(d, "LC_MESSAGES", x);
    Py_XDECREF(x);
721
#endif /* LC_MESSAGES */
722

723
    x = PyLong_FromLong(LC_NUMERIC);
724 725
    PyDict_SetItemString(d, "LC_NUMERIC", x);
    Py_XDECREF(x);
726

727
    x = PyLong_FromLong(LC_ALL);
728 729
    PyDict_SetItemString(d, "LC_ALL", x);
    Py_XDECREF(x);
730

731
    x = PyLong_FromLong(CHAR_MAX);
732 733
    PyDict_SetItemString(d, "CHAR_MAX", x);
    Py_XDECREF(x);
734

735 736
    Error = PyErr_NewException("locale.Error", NULL, NULL);
    PyDict_SetItemString(d, "Error", Error);
737

738
#ifdef HAVE_LANGINFO_H
739 740 741 742
    for (i = 0; langinfo_constants[i].name; i++) {
	    PyModule_AddIntConstant(m, langinfo_constants[i].name,
				    langinfo_constants[i].value);
    }
743
#endif
744
    return m;
745
}
746 747 748 749 750 751 752

/* 
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/