~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Modules/_localemodule.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

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