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(__APPLE__)
36 #include <CoreFoundation/CoreFoundation.h>
39 #if defined(MS_WINDOWS)
40 #define WIN32_LEAN_AND_MEAN
45 char *strdup(const char *);
48 PyDoc_STRVAR(locale__doc__
, "Support for POSIX locales.");
50 static PyObject
*Error
;
52 /* support functions for formatting floating point numbers */
54 PyDoc_STRVAR(setlocale__doc__
,
55 "(integer,string=None) -> string. Activates/queries locale processing.");
57 /* the grouping is terminated by either 0 or CHAR_MAX */
59 copy_grouping(char* s
)
62 PyObject
*result
, *val
= NULL
;
65 /* empty string: no grouping at all */
68 for (i
= 0; s
[i
] != '\0' && s
[i
] != CHAR_MAX
; i
++)
71 result
= PyList_New(i
+1);
78 val
= PyInt_FromLong(s
[i
]);
81 if (PyList_SetItem(result
, i
, val
)) {
86 } while (s
[i
] != '\0' && s
[i
] != CHAR_MAX
);
99 PyObject
*mods
, *strop
, *string
, *ulo
;
100 unsigned char ul
[256];
103 /* find the string and strop modules */
104 mods
= PyImport_GetModuleDict();
107 string
= PyDict_GetItemString(mods
, "string");
109 string
= PyModule_GetDict(string
);
110 strop
=PyDict_GetItemString(mods
, "strop");
112 strop
= PyModule_GetDict(strop
);
113 if (!string
&& !strop
)
116 /* create uppercase map string */
118 for (c
= 0; c
< 256; c
++) {
122 ulo
= PyString_FromStringAndSize((const char *)ul
, n
);
126 PyDict_SetItemString(string
, "uppercase", ulo
);
128 PyDict_SetItemString(strop
, "uppercase", ulo
);
131 /* create lowercase string */
133 for (c
= 0; c
< 256; c
++) {
137 ulo
= PyString_FromStringAndSize((const char *)ul
, n
);
141 PyDict_SetItemString(string
, "lowercase", ulo
);
143 PyDict_SetItemString(strop
, "lowercase", ulo
);
146 /* create letters string */
148 for (c
= 0; c
< 256; c
++) {
152 ulo
= PyString_FromStringAndSize((const char *)ul
, n
);
156 PyDict_SetItemString(string
, "letters", ulo
);
161 PyLocale_setlocale(PyObject
* self
, PyObject
* args
)
164 char *locale
= NULL
, *result
;
165 PyObject
*result_object
;
167 if (!PyArg_ParseTuple(args
, "i|z:setlocale", &category
, &locale
))
172 result
= setlocale(category
, locale
);
174 /* operation failed, no setting was changed */
175 PyErr_SetString(Error
, "unsupported locale setting");
178 result_object
= PyString_FromString(result
);
181 /* record changes to LC_CTYPE */
182 if (category
== LC_CTYPE
|| category
== LC_ALL
)
184 /* things that got wrong up to here are ignored */
188 result
= setlocale(category
, NULL
);
190 PyErr_SetString(Error
, "locale query failed");
193 result_object
= PyString_FromString(result
);
195 return result_object
;
198 PyDoc_STRVAR(localeconv__doc__
,
199 "() -> dict. Returns numeric and monetary locale-specific parameters.");
202 PyLocale_localeconv(PyObject
* self
)
208 result
= PyDict_New();
212 /* if LC_NUMERIC is different in the C library, use saved value */
215 /* hopefully, the localeconv result survives the C library calls
218 #define RESULT_STRING(s)\
219 x = PyString_FromString(l->s);\
220 if (!x) goto failed;\
221 PyDict_SetItemString(result, #s, x);\
224 #define RESULT_INT(i)\
225 x = PyInt_FromLong(l->i);\
226 if (!x) goto failed;\
227 PyDict_SetItemString(result, #i, x);\
230 /* Numeric information */
231 RESULT_STRING(decimal_point
);
232 RESULT_STRING(thousands_sep
);
233 x
= copy_grouping(l
->grouping
);
236 PyDict_SetItemString(result
, "grouping", x
);
239 /* Monetary information */
240 RESULT_STRING(int_curr_symbol
);
241 RESULT_STRING(currency_symbol
);
242 RESULT_STRING(mon_decimal_point
);
243 RESULT_STRING(mon_thousands_sep
);
244 x
= copy_grouping(l
->mon_grouping
);
247 PyDict_SetItemString(result
, "mon_grouping", x
);
249 RESULT_STRING(positive_sign
);
250 RESULT_STRING(negative_sign
);
251 RESULT_INT(int_frac_digits
);
252 RESULT_INT(frac_digits
);
253 RESULT_INT(p_cs_precedes
);
254 RESULT_INT(p_sep_by_space
);
255 RESULT_INT(n_cs_precedes
);
256 RESULT_INT(n_sep_by_space
);
257 RESULT_INT(p_sign_posn
);
258 RESULT_INT(n_sign_posn
);
267 PyDoc_STRVAR(strcoll__doc__
,
268 "string,string -> int. Compares two strings according to the locale.");
271 PyLocale_strcoll(PyObject
* self
, PyObject
* args
)
273 #if !defined(HAVE_WCSCOLL) || !defined(Py_USING_UNICODE)
276 if (!PyArg_ParseTuple(args
, "ss:strcoll", &s1
, &s2
))
278 return PyInt_FromLong(strcoll(s1
, s2
));
280 PyObject
*os1
, *os2
, *result
= NULL
;
281 wchar_t *ws1
= NULL
, *ws2
= NULL
;
282 int rel1
= 0, rel2
= 0, len1
, len2
;
284 if (!PyArg_UnpackTuple(args
, "strcoll", 2, 2, &os1
, &os2
))
286 /* If both arguments are byte strings, use strcoll. */
287 if (PyString_Check(os1
) && PyString_Check(os2
))
288 return PyInt_FromLong(strcoll(PyString_AS_STRING(os1
),
289 PyString_AS_STRING(os2
)));
290 /* If neither argument is unicode, it's an error. */
291 if (!PyUnicode_Check(os1
) && !PyUnicode_Check(os2
)) {
292 PyErr_SetString(PyExc_ValueError
, "strcoll arguments must be strings");
294 /* Convert the non-unicode argument to unicode. */
295 if (!PyUnicode_Check(os1
)) {
296 os1
= PyUnicode_FromObject(os1
);
301 if (!PyUnicode_Check(os2
)) {
302 os2
= PyUnicode_FromObject(os2
);
309 /* Convert the unicode strings to wchar[]. */
310 len1
= PyUnicode_GET_SIZE(os1
) + 1;
311 ws1
= PyMem_MALLOC(len1
* sizeof(wchar_t));
316 if (PyUnicode_AsWideChar((PyUnicodeObject
*)os1
, ws1
, len1
) == -1)
319 len2
= PyUnicode_GET_SIZE(os2
) + 1;
320 ws2
= PyMem_MALLOC(len2
* sizeof(wchar_t));
325 if (PyUnicode_AsWideChar((PyUnicodeObject
*)os2
, ws2
, len2
) == -1)
328 /* Collate the strings. */
329 result
= PyInt_FromLong(wcscoll(ws1
, ws2
));
331 /* Deallocate everything. */
332 if (ws1
) PyMem_FREE(ws1
);
333 if (ws2
) PyMem_FREE(ws2
);
345 PyDoc_STRVAR(strxfrm__doc__
,
346 "string -> string. Returns a string that behaves for cmp locale-aware.");
349 PyLocale_strxfrm(PyObject
* self
, PyObject
* args
)
355 if (!PyArg_ParseTuple(args
, "s:strxfrm", &s
))
358 /* assume no change in size, first */
360 buf
= PyMem_Malloc(n1
);
362 return PyErr_NoMemory();
363 n2
= strxfrm(buf
, s
, n1
) + 1;
365 /* more space needed */
366 buf
= PyMem_Realloc(buf
, n2
);
368 return PyErr_NoMemory();
371 result
= PyString_FromString(buf
);
376 #if defined(MS_WINDOWS)
378 PyLocale_getdefaultlocale(PyObject
* self
)
383 PyOS_snprintf(encoding
, sizeof(encoding
), "cp%d", GetACP());
385 if (GetLocaleInfo(LOCALE_USER_DEFAULT
,
386 LOCALE_SISO639LANGNAME
,
387 locale
, sizeof(locale
))) {
388 Py_ssize_t i
= strlen(locale
);
390 if (GetLocaleInfo(LOCALE_USER_DEFAULT
,
391 LOCALE_SISO3166CTRYNAME
,
392 locale
+i
, (int)(sizeof(locale
)-i
)))
393 return Py_BuildValue("ss", locale
, encoding
);
396 /* If we end up here, this windows version didn't know about
397 ISO639/ISO3166 names (it's probably Windows 95). Return the
398 Windows language identifier instead (a hexadecimal number) */
402 if (GetLocaleInfo(LOCALE_USER_DEFAULT
, LOCALE_IDEFAULTLANGUAGE
,
403 locale
+2, sizeof(locale
)-2)) {
404 return Py_BuildValue("ss", locale
, encoding
);
407 /* cannot determine the language code (very unlikely) */
409 return Py_BuildValue("Os", Py_None
, encoding
);
413 #if defined(__APPLE__)
415 ** Find out what the current script is.
416 ** Donated by Fredrik Lundh.
418 static char *mac_getscript(void)
420 CFStringEncoding enc
= CFStringGetSystemEncoding();
421 static CFStringRef name
= NULL
;
422 /* Return the code name for the encodings for which we have codecs. */
424 case kCFStringEncodingMacRoman
: return "mac-roman";
425 case kCFStringEncodingMacGreek
: return "mac-greek";
426 case kCFStringEncodingMacCyrillic
: return "mac-cyrillic";
427 case kCFStringEncodingMacTurkish
: return "mac-turkish";
428 case kCFStringEncodingMacIcelandic
: return "mac-icelandic";
429 /* XXX which one is mac-latin2? */
432 /* This leaks an object. */
433 name
= CFStringConvertEncodingToIANACharSetName(enc
);
435 return (char *)CFStringGetCStringPtr(name
, 0);
439 PyLocale_getdefaultlocale(PyObject
* self
)
441 return Py_BuildValue("Os", Py_None
, mac_getscript());
445 #ifdef HAVE_LANGINFO_H
446 #define LANGINFO(X) {#X, X}
447 struct langinfo_constant
{
450 } langinfo_constants
[] =
452 /* These constants should exist on any langinfo implementation */
496 /* The following are not available with glibc 2.0 */
499 /* YESSTR and NOSTR are deprecated in glibc, since they are
500 a special case of message translation, which should be rather
501 done using gettext. So we don't expose it to Python in the
515 /* The following constants are available only with XPG4, but...
516 AIX 3.2. only has CODESET.
517 OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have
519 Solution: ifdef-test them all. */
524 LANGINFO(T_FMT_AMPM
),
533 LANGINFO(ERA_D_T_FMT
),
539 LANGINFO(ALT_DIGITS
),
548 /* This is not available in all glibc versions that have CODESET. */
554 PyDoc_STRVAR(nl_langinfo__doc__
,
555 "nl_langinfo(key) -> string\n"
556 "Return the value for the locale information associated with key.");
559 PyLocale_nl_langinfo(PyObject
* self
, PyObject
* args
)
562 if (!PyArg_ParseTuple(args
, "i:nl_langinfo", &item
))
564 /* Check whether this is a supported constant. GNU libc sometimes
565 returns numeric values in the char* return value, which would
566 crash PyString_FromString. */
567 for (i
= 0; langinfo_constants
[i
].name
; i
++)
568 if (langinfo_constants
[i
].value
== item
) {
569 /* Check NULL as a workaround for GNU libc's returning NULL
570 instead of an empty string for nl_langinfo(ERA). */
571 const char *result
= nl_langinfo(item
);
572 return PyString_FromString(result
!= NULL
? result
: "");
574 PyErr_SetString(PyExc_ValueError
, "unsupported langinfo constant");
577 #endif /* HAVE_LANGINFO_H */
579 #ifdef HAVE_LIBINTL_H
581 PyDoc_STRVAR(gettext__doc__
,
582 "gettext(msg) -> string\n"
583 "Return translation of msg.");
586 PyIntl_gettext(PyObject
* self
, PyObject
*args
)
589 if (!PyArg_ParseTuple(args
, "z", &in
))
591 return PyString_FromString(gettext(in
));
594 PyDoc_STRVAR(dgettext__doc__
,
595 "dgettext(domain, msg) -> string\n"
596 "Return translation of msg in domain.");
599 PyIntl_dgettext(PyObject
* self
, PyObject
*args
)
602 if (!PyArg_ParseTuple(args
, "zz", &domain
, &in
))
604 return PyString_FromString(dgettext(domain
, in
));
607 PyDoc_STRVAR(dcgettext__doc__
,
608 "dcgettext(domain, msg, category) -> string\n"
609 "Return translation of msg in domain and category.");
612 PyIntl_dcgettext(PyObject
*self
, PyObject
*args
)
614 char *domain
, *msgid
;
616 if (!PyArg_ParseTuple(args
, "zzi", &domain
, &msgid
, &category
))
618 return PyString_FromString(dcgettext(domain
,msgid
,category
));
621 PyDoc_STRVAR(textdomain__doc__
,
622 "textdomain(domain) -> string\n"
623 "Set the C library's textdmain to domain, returning the new domain.");
626 PyIntl_textdomain(PyObject
* self
, PyObject
* args
)
629 if (!PyArg_ParseTuple(args
, "z", &domain
))
631 domain
= textdomain(domain
);
633 PyErr_SetFromErrno(PyExc_OSError
);
636 return PyString_FromString(domain
);
639 PyDoc_STRVAR(bindtextdomain__doc__
,
640 "bindtextdomain(domain, dir) -> string\n"
641 "Bind the C library's domain to dir.");
644 PyIntl_bindtextdomain(PyObject
* self
,PyObject
*args
)
646 char *domain
,*dirname
;
647 if (!PyArg_ParseTuple(args
, "zz", &domain
, &dirname
))
649 dirname
= bindtextdomain(domain
, dirname
);
651 PyErr_SetFromErrno(PyExc_OSError
);
654 return PyString_FromString(dirname
);
657 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
658 PyDoc_STRVAR(bind_textdomain_codeset__doc__
,
659 "bind_textdomain_codeset(domain, codeset) -> string\n"
660 "Bind the C library's domain to codeset.");
663 PyIntl_bind_textdomain_codeset(PyObject
* self
,PyObject
*args
)
665 char *domain
,*codeset
;
666 if (!PyArg_ParseTuple(args
, "sz", &domain
, &codeset
))
668 codeset
= bind_textdomain_codeset(domain
, codeset
);
670 return PyString_FromString(codeset
);
677 static struct PyMethodDef PyLocale_Methods
[] = {
678 {"setlocale", (PyCFunction
) PyLocale_setlocale
,
679 METH_VARARGS
, setlocale__doc__
},
680 {"localeconv", (PyCFunction
) PyLocale_localeconv
,
681 METH_NOARGS
, localeconv__doc__
},
682 {"strcoll", (PyCFunction
) PyLocale_strcoll
,
683 METH_VARARGS
, strcoll__doc__
},
684 {"strxfrm", (PyCFunction
) PyLocale_strxfrm
,
685 METH_VARARGS
, strxfrm__doc__
},
686 #if defined(MS_WINDOWS) || defined(__APPLE__)
687 {"_getdefaultlocale", (PyCFunction
) PyLocale_getdefaultlocale
, METH_NOARGS
},
689 #ifdef HAVE_LANGINFO_H
690 {"nl_langinfo", (PyCFunction
) PyLocale_nl_langinfo
,
691 METH_VARARGS
, nl_langinfo__doc__
},
693 #ifdef HAVE_LIBINTL_H
694 {"gettext",(PyCFunction
)PyIntl_gettext
,METH_VARARGS
,
696 {"dgettext",(PyCFunction
)PyIntl_dgettext
,METH_VARARGS
,
698 {"dcgettext",(PyCFunction
)PyIntl_dcgettext
,METH_VARARGS
,
700 {"textdomain",(PyCFunction
)PyIntl_textdomain
,METH_VARARGS
,
702 {"bindtextdomain",(PyCFunction
)PyIntl_bindtextdomain
,METH_VARARGS
,
703 bindtextdomain__doc__
},
704 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
705 {"bind_textdomain_codeset",(PyCFunction
)PyIntl_bind_textdomain_codeset
,
706 METH_VARARGS
, bind_textdomain_codeset__doc__
},
716 #ifdef HAVE_LANGINFO_H
720 m
= Py_InitModule("_locale", PyLocale_Methods
);
724 d
= PyModule_GetDict(m
);
726 x
= PyInt_FromLong(LC_CTYPE
);
727 PyDict_SetItemString(d
, "LC_CTYPE", x
);
730 x
= PyInt_FromLong(LC_TIME
);
731 PyDict_SetItemString(d
, "LC_TIME", x
);
734 x
= PyInt_FromLong(LC_COLLATE
);
735 PyDict_SetItemString(d
, "LC_COLLATE", x
);
738 x
= PyInt_FromLong(LC_MONETARY
);
739 PyDict_SetItemString(d
, "LC_MONETARY", x
);
743 x
= PyInt_FromLong(LC_MESSAGES
);
744 PyDict_SetItemString(d
, "LC_MESSAGES", x
);
746 #endif /* LC_MESSAGES */
748 x
= PyInt_FromLong(LC_NUMERIC
);
749 PyDict_SetItemString(d
, "LC_NUMERIC", x
);
752 x
= PyInt_FromLong(LC_ALL
);
753 PyDict_SetItemString(d
, "LC_ALL", x
);
756 x
= PyInt_FromLong(CHAR_MAX
);
757 PyDict_SetItemString(d
, "CHAR_MAX", x
);
760 Error
= PyErr_NewException("locale.Error", NULL
, NULL
);
761 PyDict_SetItemString(d
, "Error", Error
);
763 x
= PyString_FromString(locale__doc__
);
764 PyDict_SetItemString(d
, "__doc__", x
);
767 #ifdef HAVE_LANGINFO_H
768 for (i
= 0; langinfo_constants
[i
].name
; i
++) {
769 PyModule_AddIntConstant(m
, langinfo_constants
[i
].name
,
770 langinfo_constants
[i
].value
);
778 indent-tabs-mode: nil