1 /***********************************************************
2 Copyright (C) 1997, 2002, 2003 Martin von Loewis
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.
8 This software comes with no warranty. Use at your own risk.
10 ******************************************************************/
23 #ifdef HAVE_LANGINFO_H
35 #if defined(MS_WINDOWS)
36 #define WIN32_LEAN_AND_MEAN
41 char *strdup(const char *);
44 PyDoc_STRVAR(locale__doc__
, "Support for POSIX locales.");
46 static PyObject
*Error
;
48 /* support functions for formatting floating point numbers */
50 PyDoc_STRVAR(setlocale__doc__
,
51 "(integer,string=None) -> string. Activates/queries locale processing.");
53 /* the grouping is terminated by either 0 or CHAR_MAX */
55 copy_grouping(char* s
)
58 PyObject
*result
, *val
= NULL
;
61 /* empty string: no grouping at all */
64 for (i
= 0; s
[i
] != '\0' && s
[i
] != CHAR_MAX
; i
++)
67 result
= PyList_New(i
+1);
74 val
= PyInt_FromLong(s
[i
]);
77 if (PyList_SetItem(result
, i
, val
)) {
82 } while (s
[i
] != '\0' && s
[i
] != CHAR_MAX
);
95 PyObject
*mods
, *strop
, *string
, *ulo
;
96 unsigned char ul
[256];
99 /* find the string and strop modules */
100 mods
= PyImport_GetModuleDict();
103 string
= PyDict_GetItemString(mods
, "string");
105 string
= PyModule_GetDict(string
);
106 strop
=PyDict_GetItemString(mods
, "strop");
108 strop
= PyModule_GetDict(strop
);
109 if (!string
&& !strop
)
112 /* create uppercase map string */
114 for (c
= 0; c
< 256; c
++) {
118 ulo
= PyString_FromStringAndSize((const char *)ul
, n
);
122 PyDict_SetItemString(string
, "uppercase", ulo
);
124 PyDict_SetItemString(strop
, "uppercase", ulo
);
127 /* create lowercase string */
129 for (c
= 0; c
< 256; c
++) {
133 ulo
= PyString_FromStringAndSize((const char *)ul
, n
);
137 PyDict_SetItemString(string
, "lowercase", ulo
);
139 PyDict_SetItemString(strop
, "lowercase", ulo
);
142 /* create letters string */
144 for (c
= 0; c
< 256; c
++) {
148 ulo
= PyString_FromStringAndSize((const char *)ul
, n
);
152 PyDict_SetItemString(string
, "letters", ulo
);
157 PyLocale_setlocale(PyObject
* self
, PyObject
* args
)
160 char *locale
= NULL
, *result
;
161 PyObject
*result_object
;
163 if (!PyArg_ParseTuple(args
, "i|z:setlocale", &category
, &locale
))
166 #if defined(MS_WINDOWS)
167 if (category
< LC_MIN
|| category
> LC_MAX
)
169 PyErr_SetString(Error
, "invalid locale category");
176 result
= setlocale(category
, locale
);
178 /* operation failed, no setting was changed */
179 PyErr_SetString(Error
, "unsupported locale setting");
182 result_object
= PyString_FromString(result
);
185 /* record changes to LC_CTYPE */
186 if (category
== LC_CTYPE
|| category
== LC_ALL
)
188 /* things that got wrong up to here are ignored */
192 result
= setlocale(category
, NULL
);
194 PyErr_SetString(Error
, "locale query failed");
197 result_object
= PyString_FromString(result
);
199 return result_object
;
202 PyDoc_STRVAR(localeconv__doc__
,
203 "() -> dict. Returns numeric and monetary locale-specific parameters.");
206 PyLocale_localeconv(PyObject
* self
)
212 result
= PyDict_New();
216 /* if LC_NUMERIC is different in the C library, use saved value */
219 /* hopefully, the localeconv result survives the C library calls
222 #define RESULT_STRING(s)\
223 x = PyString_FromString(l->s);\
224 if (!x) goto failed;\
225 PyDict_SetItemString(result, #s, x);\
228 #define RESULT_INT(i)\
229 x = PyInt_FromLong(l->i);\
230 if (!x) goto failed;\
231 PyDict_SetItemString(result, #i, x);\
234 /* Numeric information */
235 RESULT_STRING(decimal_point
);
236 RESULT_STRING(thousands_sep
);
237 x
= copy_grouping(l
->grouping
);
240 PyDict_SetItemString(result
, "grouping", x
);
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
);
251 PyDict_SetItemString(result
, "mon_grouping", 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
);
271 PyDoc_STRVAR(strcoll__doc__
,
272 "string,string -> int. Compares two strings according to the locale.");
275 PyLocale_strcoll(PyObject
* self
, PyObject
* args
)
277 #if !defined(HAVE_WCSCOLL) || !defined(Py_USING_UNICODE)
280 if (!PyArg_ParseTuple(args
, "ss:strcoll", &s1
, &s2
))
282 return PyInt_FromLong(strcoll(s1
, s2
));
284 PyObject
*os1
, *os2
, *result
= NULL
;
285 wchar_t *ws1
= NULL
, *ws2
= NULL
;
286 int rel1
= 0, rel2
= 0, len1
, len2
;
288 if (!PyArg_UnpackTuple(args
, "strcoll", 2, 2, &os1
, &os2
))
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");
298 /* Convert the non-unicode argument to unicode. */
299 if (!PyUnicode_Check(os1
)) {
300 os1
= PyUnicode_FromObject(os1
);
305 if (!PyUnicode_Check(os2
)) {
306 os2
= PyUnicode_FromObject(os2
);
315 /* Convert the unicode strings to wchar[]. */
316 len1
= PyUnicode_GET_SIZE(os1
) + 1;
317 ws1
= PyMem_MALLOC(len1
* sizeof(wchar_t));
322 if (PyUnicode_AsWideChar((PyUnicodeObject
*)os1
, ws1
, len1
) == -1)
325 len2
= PyUnicode_GET_SIZE(os2
) + 1;
326 ws2
= PyMem_MALLOC(len2
* sizeof(wchar_t));
331 if (PyUnicode_AsWideChar((PyUnicodeObject
*)os2
, ws2
, len2
) == -1)
334 /* Collate the strings. */
335 result
= PyInt_FromLong(wcscoll(ws1
, ws2
));
337 /* Deallocate everything. */
338 if (ws1
) PyMem_FREE(ws1
);
339 if (ws2
) PyMem_FREE(ws2
);
351 PyDoc_STRVAR(strxfrm__doc__
,
352 "string -> string. Returns a string that behaves for cmp locale-aware.");
355 PyLocale_strxfrm(PyObject
* self
, PyObject
* args
)
361 if (!PyArg_ParseTuple(args
, "s:strxfrm", &s
))
364 /* assume no change in size, first */
366 buf
= PyMem_Malloc(n1
);
368 return PyErr_NoMemory();
369 n2
= strxfrm(buf
, s
, n1
) + 1;
371 /* more space needed */
372 buf
= PyMem_Realloc(buf
, n2
);
374 return PyErr_NoMemory();
377 result
= PyString_FromString(buf
);
382 #if defined(MS_WINDOWS)
384 PyLocale_getdefaultlocale(PyObject
* self
)
389 PyOS_snprintf(encoding
, sizeof(encoding
), "cp%d", GetACP());
391 if (GetLocaleInfo(LOCALE_USER_DEFAULT
,
392 LOCALE_SISO639LANGNAME
,
393 locale
, sizeof(locale
))) {
394 Py_ssize_t i
= strlen(locale
);
396 if (GetLocaleInfo(LOCALE_USER_DEFAULT
,
397 LOCALE_SISO3166CTRYNAME
,
398 locale
+i
, (int)(sizeof(locale
)-i
)))
399 return Py_BuildValue("ss", locale
, encoding
);
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) */
408 if (GetLocaleInfo(LOCALE_USER_DEFAULT
, LOCALE_IDEFAULTLANGUAGE
,
409 locale
+2, sizeof(locale
)-2)) {
410 return Py_BuildValue("ss", locale
, encoding
);
413 /* cannot determine the language code (very unlikely) */
415 return Py_BuildValue("Os", Py_None
, encoding
);
419 #ifdef HAVE_LANGINFO_H
420 #define LANGINFO(X) {#X, X}
421 static struct langinfo_constant
{
424 } langinfo_constants
[] =
426 /* These constants should exist on any langinfo implementation */
470 /* The following are not available with glibc 2.0 */
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
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
493 Solution: ifdef-test them all. */
498 LANGINFO(T_FMT_AMPM
),
507 LANGINFO(ERA_D_T_FMT
),
513 LANGINFO(ALT_DIGITS
),
522 /* This is not available in all glibc versions that have CODESET. */
528 PyDoc_STRVAR(nl_langinfo__doc__
,
529 "nl_langinfo(key) -> string\n"
530 "Return the value for the locale information associated with key.");
533 PyLocale_nl_langinfo(PyObject
* self
, PyObject
* args
)
536 if (!PyArg_ParseTuple(args
, "i:nl_langinfo", &item
))
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
: "");
548 PyErr_SetString(PyExc_ValueError
, "unsupported langinfo constant");
551 #endif /* HAVE_LANGINFO_H */
553 #ifdef HAVE_LIBINTL_H
555 PyDoc_STRVAR(gettext__doc__
,
556 "gettext(msg) -> string\n"
557 "Return translation of msg.");
560 PyIntl_gettext(PyObject
* self
, PyObject
*args
)
563 if (!PyArg_ParseTuple(args
, "s", &in
))
565 return PyString_FromString(gettext(in
));
568 PyDoc_STRVAR(dgettext__doc__
,
569 "dgettext(domain, msg) -> string\n"
570 "Return translation of msg in domain.");
573 PyIntl_dgettext(PyObject
* self
, PyObject
*args
)
576 if (!PyArg_ParseTuple(args
, "zs", &domain
, &in
))
578 return PyString_FromString(dgettext(domain
, in
));
581 PyDoc_STRVAR(dcgettext__doc__
,
582 "dcgettext(domain, msg, category) -> string\n"
583 "Return translation of msg in domain and category.");
586 PyIntl_dcgettext(PyObject
*self
, PyObject
*args
)
588 char *domain
, *msgid
;
590 if (!PyArg_ParseTuple(args
, "zsi", &domain
, &msgid
, &category
))
592 return PyString_FromString(dcgettext(domain
,msgid
,category
));
595 PyDoc_STRVAR(textdomain__doc__
,
596 "textdomain(domain) -> string\n"
597 "Set the C library's textdmain to domain, returning the new domain.");
600 PyIntl_textdomain(PyObject
* self
, PyObject
* args
)
603 if (!PyArg_ParseTuple(args
, "z", &domain
))
605 domain
= textdomain(domain
);
607 PyErr_SetFromErrno(PyExc_OSError
);
610 return PyString_FromString(domain
);
613 PyDoc_STRVAR(bindtextdomain__doc__
,
614 "bindtextdomain(domain, dir) -> string\n"
615 "Bind the C library's domain to dir.");
618 PyIntl_bindtextdomain(PyObject
* self
,PyObject
*args
)
620 char *domain
, *dirname
;
621 if (!PyArg_ParseTuple(args
, "sz", &domain
, &dirname
))
623 if (!strlen(domain
)) {
624 PyErr_SetString(Error
, "domain must be a non-empty string");
627 dirname
= bindtextdomain(domain
, dirname
);
629 PyErr_SetFromErrno(PyExc_OSError
);
632 return PyString_FromString(dirname
);
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.");
641 PyIntl_bind_textdomain_codeset(PyObject
* self
,PyObject
*args
)
643 char *domain
,*codeset
;
644 if (!PyArg_ParseTuple(args
, "sz", &domain
, &codeset
))
646 codeset
= bind_textdomain_codeset(domain
, codeset
);
648 return PyString_FromString(codeset
);
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
},
667 #ifdef HAVE_LANGINFO_H
668 {"nl_langinfo", (PyCFunction
) PyLocale_nl_langinfo
,
669 METH_VARARGS
, nl_langinfo__doc__
},
671 #ifdef HAVE_LIBINTL_H
672 {"gettext",(PyCFunction
)PyIntl_gettext
,METH_VARARGS
,
674 {"dgettext",(PyCFunction
)PyIntl_dgettext
,METH_VARARGS
,
676 {"dcgettext",(PyCFunction
)PyIntl_dcgettext
,METH_VARARGS
,
678 {"textdomain",(PyCFunction
)PyIntl_textdomain
,METH_VARARGS
,
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__
},
694 #ifdef HAVE_LANGINFO_H
698 m
= Py_InitModule("_locale", PyLocale_Methods
);
702 d
= PyModule_GetDict(m
);
704 x
= PyInt_FromLong(LC_CTYPE
);
705 PyDict_SetItemString(d
, "LC_CTYPE", x
);
708 x
= PyInt_FromLong(LC_TIME
);
709 PyDict_SetItemString(d
, "LC_TIME", x
);
712 x
= PyInt_FromLong(LC_COLLATE
);
713 PyDict_SetItemString(d
, "LC_COLLATE", x
);
716 x
= PyInt_FromLong(LC_MONETARY
);
717 PyDict_SetItemString(d
, "LC_MONETARY", x
);
721 x
= PyInt_FromLong(LC_MESSAGES
);
722 PyDict_SetItemString(d
, "LC_MESSAGES", x
);
724 #endif /* LC_MESSAGES */
726 x
= PyInt_FromLong(LC_NUMERIC
);
727 PyDict_SetItemString(d
, "LC_NUMERIC", x
);
730 x
= PyInt_FromLong(LC_ALL
);
731 PyDict_SetItemString(d
, "LC_ALL", x
);
734 x
= PyInt_FromLong(CHAR_MAX
);
735 PyDict_SetItemString(d
, "CHAR_MAX", x
);
738 Error
= PyErr_NewException("locale.Error", NULL
, NULL
);
739 PyDict_SetItemString(d
, "Error", Error
);
741 x
= PyString_FromString(locale__doc__
);
742 PyDict_SetItemString(d
, "__doc__", x
);
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
);
756 indent-tabs-mode: nil