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
))
168 result
= setlocale(category
, locale
);
170 /* operation failed, no setting was changed */
171 PyErr_SetString(Error
, "unsupported locale setting");
174 result_object
= PyString_FromString(result
);
177 /* record changes to LC_CTYPE */
178 if (category
== LC_CTYPE
|| category
== LC_ALL
)
180 /* things that got wrong up to here are ignored */
184 result
= setlocale(category
, NULL
);
186 PyErr_SetString(Error
, "locale query failed");
189 result_object
= PyString_FromString(result
);
191 return result_object
;
194 PyDoc_STRVAR(localeconv__doc__
,
195 "() -> dict. Returns numeric and monetary locale-specific parameters.");
198 PyLocale_localeconv(PyObject
* self
)
204 result
= PyDict_New();
208 /* if LC_NUMERIC is different in the C library, use saved value */
211 /* hopefully, the localeconv result survives the C library calls
214 #define RESULT_STRING(s)\
215 x = PyString_FromString(l->s);\
216 if (!x) goto failed;\
217 PyDict_SetItemString(result, #s, x);\
220 #define RESULT_INT(i)\
221 x = PyInt_FromLong(l->i);\
222 if (!x) goto failed;\
223 PyDict_SetItemString(result, #i, x);\
226 /* Numeric information */
227 RESULT_STRING(decimal_point
);
228 RESULT_STRING(thousands_sep
);
229 x
= copy_grouping(l
->grouping
);
232 PyDict_SetItemString(result
, "grouping", x
);
235 /* Monetary information */
236 RESULT_STRING(int_curr_symbol
);
237 RESULT_STRING(currency_symbol
);
238 RESULT_STRING(mon_decimal_point
);
239 RESULT_STRING(mon_thousands_sep
);
240 x
= copy_grouping(l
->mon_grouping
);
243 PyDict_SetItemString(result
, "mon_grouping", x
);
245 RESULT_STRING(positive_sign
);
246 RESULT_STRING(negative_sign
);
247 RESULT_INT(int_frac_digits
);
248 RESULT_INT(frac_digits
);
249 RESULT_INT(p_cs_precedes
);
250 RESULT_INT(p_sep_by_space
);
251 RESULT_INT(n_cs_precedes
);
252 RESULT_INT(n_sep_by_space
);
253 RESULT_INT(p_sign_posn
);
254 RESULT_INT(n_sign_posn
);
263 PyDoc_STRVAR(strcoll__doc__
,
264 "string,string -> int. Compares two strings according to the locale.");
267 PyLocale_strcoll(PyObject
* self
, PyObject
* args
)
269 #if !defined(HAVE_WCSCOLL) || !defined(Py_USING_UNICODE)
272 if (!PyArg_ParseTuple(args
, "ss:strcoll", &s1
, &s2
))
274 return PyInt_FromLong(strcoll(s1
, s2
));
276 PyObject
*os1
, *os2
, *result
= NULL
;
277 wchar_t *ws1
= NULL
, *ws2
= NULL
;
278 int rel1
= 0, rel2
= 0, len1
, len2
;
280 if (!PyArg_UnpackTuple(args
, "strcoll", 2, 2, &os1
, &os2
))
282 /* If both arguments are byte strings, use strcoll. */
283 if (PyString_Check(os1
) && PyString_Check(os2
))
284 return PyInt_FromLong(strcoll(PyString_AS_STRING(os1
),
285 PyString_AS_STRING(os2
)));
286 /* If neither argument is unicode, it's an error. */
287 if (!PyUnicode_Check(os1
) && !PyUnicode_Check(os2
)) {
288 PyErr_SetString(PyExc_ValueError
, "strcoll arguments must be strings");
290 /* Convert the non-unicode argument to unicode. */
291 if (!PyUnicode_Check(os1
)) {
292 os1
= PyUnicode_FromObject(os1
);
297 if (!PyUnicode_Check(os2
)) {
298 os2
= PyUnicode_FromObject(os2
);
307 /* Convert the unicode strings to wchar[]. */
308 len1
= PyUnicode_GET_SIZE(os1
) + 1;
309 ws1
= PyMem_MALLOC(len1
* sizeof(wchar_t));
314 if (PyUnicode_AsWideChar((PyUnicodeObject
*)os1
, ws1
, len1
) == -1)
317 len2
= PyUnicode_GET_SIZE(os2
) + 1;
318 ws2
= PyMem_MALLOC(len2
* sizeof(wchar_t));
323 if (PyUnicode_AsWideChar((PyUnicodeObject
*)os2
, ws2
, len2
) == -1)
326 /* Collate the strings. */
327 result
= PyInt_FromLong(wcscoll(ws1
, ws2
));
329 /* Deallocate everything. */
330 if (ws1
) PyMem_FREE(ws1
);
331 if (ws2
) PyMem_FREE(ws2
);
343 PyDoc_STRVAR(strxfrm__doc__
,
344 "string -> string. Returns a string that behaves for cmp locale-aware.");
347 PyLocale_strxfrm(PyObject
* self
, PyObject
* args
)
353 if (!PyArg_ParseTuple(args
, "s:strxfrm", &s
))
356 /* assume no change in size, first */
358 buf
= PyMem_Malloc(n1
);
360 return PyErr_NoMemory();
361 n2
= strxfrm(buf
, s
, n1
) + 1;
363 /* more space needed */
364 buf
= PyMem_Realloc(buf
, n2
);
366 return PyErr_NoMemory();
369 result
= PyString_FromString(buf
);
374 #if defined(MS_WINDOWS)
376 PyLocale_getdefaultlocale(PyObject
* self
)
381 PyOS_snprintf(encoding
, sizeof(encoding
), "cp%d", GetACP());
383 if (GetLocaleInfo(LOCALE_USER_DEFAULT
,
384 LOCALE_SISO639LANGNAME
,
385 locale
, sizeof(locale
))) {
386 Py_ssize_t i
= strlen(locale
);
388 if (GetLocaleInfo(LOCALE_USER_DEFAULT
,
389 LOCALE_SISO3166CTRYNAME
,
390 locale
+i
, (int)(sizeof(locale
)-i
)))
391 return Py_BuildValue("ss", locale
, encoding
);
394 /* If we end up here, this windows version didn't know about
395 ISO639/ISO3166 names (it's probably Windows 95). Return the
396 Windows language identifier instead (a hexadecimal number) */
400 if (GetLocaleInfo(LOCALE_USER_DEFAULT
, LOCALE_IDEFAULTLANGUAGE
,
401 locale
+2, sizeof(locale
)-2)) {
402 return Py_BuildValue("ss", locale
, encoding
);
405 /* cannot determine the language code (very unlikely) */
407 return Py_BuildValue("Os", Py_None
, encoding
);
411 #ifdef HAVE_LANGINFO_H
412 #define LANGINFO(X) {#X, X}
413 static struct langinfo_constant
{
416 } langinfo_constants
[] =
418 /* These constants should exist on any langinfo implementation */
462 /* The following are not available with glibc 2.0 */
465 /* YESSTR and NOSTR are deprecated in glibc, since they are
466 a special case of message translation, which should be rather
467 done using gettext. So we don't expose it to Python in the
481 /* The following constants are available only with XPG4, but...
482 AIX 3.2. only has CODESET.
483 OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have
485 Solution: ifdef-test them all. */
490 LANGINFO(T_FMT_AMPM
),
499 LANGINFO(ERA_D_T_FMT
),
505 LANGINFO(ALT_DIGITS
),
514 /* This is not available in all glibc versions that have CODESET. */
520 PyDoc_STRVAR(nl_langinfo__doc__
,
521 "nl_langinfo(key) -> string\n"
522 "Return the value for the locale information associated with key.");
525 PyLocale_nl_langinfo(PyObject
* self
, PyObject
* args
)
528 if (!PyArg_ParseTuple(args
, "i:nl_langinfo", &item
))
530 /* Check whether this is a supported constant. GNU libc sometimes
531 returns numeric values in the char* return value, which would
532 crash PyString_FromString. */
533 for (i
= 0; langinfo_constants
[i
].name
; i
++)
534 if (langinfo_constants
[i
].value
== item
) {
535 /* Check NULL as a workaround for GNU libc's returning NULL
536 instead of an empty string for nl_langinfo(ERA). */
537 const char *result
= nl_langinfo(item
);
538 return PyString_FromString(result
!= NULL
? result
: "");
540 PyErr_SetString(PyExc_ValueError
, "unsupported langinfo constant");
543 #endif /* HAVE_LANGINFO_H */
545 #ifdef HAVE_LIBINTL_H
547 PyDoc_STRVAR(gettext__doc__
,
548 "gettext(msg) -> string\n"
549 "Return translation of msg.");
552 PyIntl_gettext(PyObject
* self
, PyObject
*args
)
555 if (!PyArg_ParseTuple(args
, "s", &in
))
557 return PyString_FromString(gettext(in
));
560 PyDoc_STRVAR(dgettext__doc__
,
561 "dgettext(domain, msg) -> string\n"
562 "Return translation of msg in domain.");
565 PyIntl_dgettext(PyObject
* self
, PyObject
*args
)
568 if (!PyArg_ParseTuple(args
, "zs", &domain
, &in
))
570 return PyString_FromString(dgettext(domain
, in
));
573 PyDoc_STRVAR(dcgettext__doc__
,
574 "dcgettext(domain, msg, category) -> string\n"
575 "Return translation of msg in domain and category.");
578 PyIntl_dcgettext(PyObject
*self
, PyObject
*args
)
580 char *domain
, *msgid
;
582 if (!PyArg_ParseTuple(args
, "zsi", &domain
, &msgid
, &category
))
584 return PyString_FromString(dcgettext(domain
,msgid
,category
));
587 PyDoc_STRVAR(textdomain__doc__
,
588 "textdomain(domain) -> string\n"
589 "Set the C library's textdmain to domain, returning the new domain.");
592 PyIntl_textdomain(PyObject
* self
, PyObject
* args
)
595 if (!PyArg_ParseTuple(args
, "z", &domain
))
597 domain
= textdomain(domain
);
599 PyErr_SetFromErrno(PyExc_OSError
);
602 return PyString_FromString(domain
);
605 PyDoc_STRVAR(bindtextdomain__doc__
,
606 "bindtextdomain(domain, dir) -> string\n"
607 "Bind the C library's domain to dir.");
610 PyIntl_bindtextdomain(PyObject
* self
,PyObject
*args
)
612 char *domain
, *dirname
;
613 if (!PyArg_ParseTuple(args
, "sz", &domain
, &dirname
))
615 if (!strlen(domain
)) {
616 PyErr_SetString(Error
, "domain must be a non-empty string");
619 dirname
= bindtextdomain(domain
, dirname
);
621 PyErr_SetFromErrno(PyExc_OSError
);
624 return PyString_FromString(dirname
);
627 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
628 PyDoc_STRVAR(bind_textdomain_codeset__doc__
,
629 "bind_textdomain_codeset(domain, codeset) -> string\n"
630 "Bind the C library's domain to codeset.");
633 PyIntl_bind_textdomain_codeset(PyObject
* self
,PyObject
*args
)
635 char *domain
,*codeset
;
636 if (!PyArg_ParseTuple(args
, "sz", &domain
, &codeset
))
638 codeset
= bind_textdomain_codeset(domain
, codeset
);
640 return PyString_FromString(codeset
);
647 static struct PyMethodDef PyLocale_Methods
[] = {
648 {"setlocale", (PyCFunction
) PyLocale_setlocale
,
649 METH_VARARGS
, setlocale__doc__
},
650 {"localeconv", (PyCFunction
) PyLocale_localeconv
,
651 METH_NOARGS
, localeconv__doc__
},
652 {"strcoll", (PyCFunction
) PyLocale_strcoll
,
653 METH_VARARGS
, strcoll__doc__
},
654 {"strxfrm", (PyCFunction
) PyLocale_strxfrm
,
655 METH_VARARGS
, strxfrm__doc__
},
656 #if defined(MS_WINDOWS)
657 {"_getdefaultlocale", (PyCFunction
) PyLocale_getdefaultlocale
, METH_NOARGS
},
659 #ifdef HAVE_LANGINFO_H
660 {"nl_langinfo", (PyCFunction
) PyLocale_nl_langinfo
,
661 METH_VARARGS
, nl_langinfo__doc__
},
663 #ifdef HAVE_LIBINTL_H
664 {"gettext",(PyCFunction
)PyIntl_gettext
,METH_VARARGS
,
666 {"dgettext",(PyCFunction
)PyIntl_dgettext
,METH_VARARGS
,
668 {"dcgettext",(PyCFunction
)PyIntl_dcgettext
,METH_VARARGS
,
670 {"textdomain",(PyCFunction
)PyIntl_textdomain
,METH_VARARGS
,
672 {"bindtextdomain",(PyCFunction
)PyIntl_bindtextdomain
,METH_VARARGS
,
673 bindtextdomain__doc__
},
674 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
675 {"bind_textdomain_codeset",(PyCFunction
)PyIntl_bind_textdomain_codeset
,
676 METH_VARARGS
, bind_textdomain_codeset__doc__
},
686 #ifdef HAVE_LANGINFO_H
690 m
= Py_InitModule("_locale", PyLocale_Methods
);
694 d
= PyModule_GetDict(m
);
696 x
= PyInt_FromLong(LC_CTYPE
);
697 PyDict_SetItemString(d
, "LC_CTYPE", x
);
700 x
= PyInt_FromLong(LC_TIME
);
701 PyDict_SetItemString(d
, "LC_TIME", x
);
704 x
= PyInt_FromLong(LC_COLLATE
);
705 PyDict_SetItemString(d
, "LC_COLLATE", x
);
708 x
= PyInt_FromLong(LC_MONETARY
);
709 PyDict_SetItemString(d
, "LC_MONETARY", x
);
713 x
= PyInt_FromLong(LC_MESSAGES
);
714 PyDict_SetItemString(d
, "LC_MESSAGES", x
);
716 #endif /* LC_MESSAGES */
718 x
= PyInt_FromLong(LC_NUMERIC
);
719 PyDict_SetItemString(d
, "LC_NUMERIC", x
);
722 x
= PyInt_FromLong(LC_ALL
);
723 PyDict_SetItemString(d
, "LC_ALL", x
);
726 x
= PyInt_FromLong(CHAR_MAX
);
727 PyDict_SetItemString(d
, "CHAR_MAX", x
);
730 Error
= PyErr_NewException("locale.Error", NULL
, NULL
);
731 PyDict_SetItemString(d
, "Error", Error
);
733 x
= PyString_FromString(locale__doc__
);
734 PyDict_SetItemString(d
, "__doc__", x
);
737 #ifdef HAVE_LANGINFO_H
738 for (i
= 0; langinfo_constants
[i
].name
; i
++) {
739 PyModule_AddIntConstant(m
, langinfo_constants
[i
].name
,
740 langinfo_constants
[i
].value
);
748 indent-tabs-mode: nil