blob: 2394206823eb44ba62dbd6c4116e362b88889aba [file] [log] [blame]
Guido van Rossum220ecc81997-11-18 21:03:39 +00001/***********************************************************
Martin v. Löwis92fab752008-03-08 10:40:41 +00002Copyright (C) 1997, 2002, 2003, 2007, 2008 Martin von Loewis
Guido van Rossum220ecc81997-11-18 21:03:39 +00003
4Permission to use, copy, modify, and distribute this software and its
5documentation for any purpose and without fee is hereby granted,
6provided that the above copyright notice appear in all copies.
7
8This software comes with no warranty. Use at your own risk.
Fredrik Lundh8f017a02000-07-08 19:57:37 +00009
Guido van Rossum220ecc81997-11-18 21:03:39 +000010******************************************************************/
11
Antoine Pitrou6a448d42009-10-19 19:43:09 +000012#define PY_SSIZE_T_CLEAN
Fred Drake68933b92000-08-10 21:41:08 +000013#include "Python.h"
14
Guido van Rossum220ecc81997-11-18 21:03:39 +000015#include <stdio.h>
Guido van Rossum220ecc81997-11-18 21:03:39 +000016#include <locale.h>
17#include <string.h>
Guido van Rossum5cd70f41998-06-19 04:33:30 +000018#include <ctype.h>
Fredrik Lundh8f017a02000-07-08 19:57:37 +000019
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020#ifdef HAVE_ERRNO_H
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#include <errno.h>
22#endif
23
Martin v. Löwis9b75dca2001-08-10 13:58:50 +000024#ifdef HAVE_LANGINFO_H
25#include <langinfo.h>
26#endif
27
Martin v. Löwis2e64c342002-03-27 18:49:02 +000028#ifdef HAVE_LIBINTL_H
29#include <libintl.h>
30#endif
31
Martin v. Löwis9c36c292002-12-21 18:34:06 +000032#ifdef HAVE_WCHAR_H
33#include <wchar.h>
34#endif
35
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000036#if defined(MS_WINDOWS)
Tim Peters7a1f9172002-07-14 22:14:19 +000037#define WIN32_LEAN_AND_MEAN
Fredrik Lundh8f017a02000-07-08 19:57:37 +000038#include <windows.h>
Guido van Rossum239a2181998-04-28 16:08:19 +000039#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +000040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(locale__doc__, "Support for POSIX locales.");
Guido van Rossum220ecc81997-11-18 21:03:39 +000042
43static PyObject *Error;
44
Martin v. Löwis92fab752008-03-08 10:40:41 +000045/* Convert a char* to a Unicode object according to the current locale */
46static PyObject*
47str2uni(const char* s)
48{
Antoine Pitroufff95302008-09-03 18:58:51 +000049#ifdef HAVE_BROKEN_MBSTOWCS
50 size_t needed = strlen(s);
51#else
Martin v. Löwis92fab752008-03-08 10:40:41 +000052 size_t needed = mbstowcs(NULL, s, 0);
Antoine Pitroufff95302008-09-03 18:58:51 +000053#endif
Martin v. Löwis92fab752008-03-08 10:40:41 +000054 size_t res1;
55 wchar_t smallbuf[30];
56 wchar_t *dest;
57 PyObject *res2;
58 if (needed == (size_t)-1) {
59 PyErr_SetString(PyExc_ValueError, "Cannot convert byte to string");
60 return NULL;
61 }
Martin v. Löwis5bacec12008-03-08 13:39:58 +000062 if (needed*sizeof(wchar_t) < sizeof(smallbuf))
Martin v. Löwis92fab752008-03-08 10:40:41 +000063 dest = smallbuf;
64 else {
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +000065 dest = PyMem_Malloc((needed+1)*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +000066 if (!dest)
67 return PyErr_NoMemory();
68 }
69 /* This shouldn't fail now */
70 res1 = mbstowcs(dest, s, needed+1);
Antoine Pitroufff95302008-09-03 18:58:51 +000071#ifdef HAVE_BROKEN_MBSTOWCS
72 assert(res1 != (size_t)-1);
73#else
Martin v. Löwisce7a1122008-03-08 10:59:49 +000074 assert(res1 == needed);
Antoine Pitroufff95302008-09-03 18:58:51 +000075#endif
Martin v. Löwis92fab752008-03-08 10:40:41 +000076 res2 = PyUnicode_FromWideChar(dest, res1);
77 if (dest != smallbuf)
78 PyMem_Free(dest);
79 return res2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080}
Martin v. Löwis92fab752008-03-08 10:40:41 +000081
Guido van Rossum220ecc81997-11-18 21:03:39 +000082/* support functions for formatting floating point numbers */
83
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084PyDoc_STRVAR(setlocale__doc__,
85"(integer,string=None) -> string. Activates/queries locale processing.");
Guido van Rossum220ecc81997-11-18 21:03:39 +000086
Guido van Rossum220ecc81997-11-18 21:03:39 +000087/* the grouping is terminated by either 0 or CHAR_MAX */
88static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +000089copy_grouping(char* s)
Guido van Rossum220ecc81997-11-18 21:03:39 +000090{
Fredrik Lundh8f017a02000-07-08 19:57:37 +000091 int i;
92 PyObject *result, *val = NULL;
93
94 if (s[0] == '\0')
95 /* empty string: no grouping at all */
96 return PyList_New(0);
97
98 for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++)
99 ; /* nothing */
100
101 result = PyList_New(i+1);
102 if (!result)
103 return NULL;
104
105 i = -1;
106 do {
107 i++;
Christian Heimes217cfd12007-12-02 14:31:20 +0000108 val = PyLong_FromLong(s[i]);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000109 if (!val)
110 break;
111 if (PyList_SetItem(result, i, val)) {
112 Py_DECREF(val);
Fredrik Lundh89610a42000-07-08 20:07:24 +0000113 val = NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000114 break;
115 }
116 } while (s[i] != '\0' && s[i] != CHAR_MAX);
117
118 if (!val) {
119 Py_DECREF(result);
120 return NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000121 }
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000122
123 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000124}
125
Guido van Rossum220ecc81997-11-18 21:03:39 +0000126static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000127PyLocale_setlocale(PyObject* self, PyObject* args)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000128{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000129 int category;
130 char *locale = NULL, *result;
131 PyObject *result_object;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000132
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000133 if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
Fredrik Lundh89610a42000-07-08 20:07:24 +0000134 return NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000135
Amaury Forgeot d'Arc64f3ca42009-12-01 21:59:18 +0000136#if defined(MS_WINDOWS)
137 if (category < LC_MIN || category > LC_MAX)
138 {
139 PyErr_SetString(Error, "invalid locale category");
140 return NULL;
141 }
142#endif
143
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000144 if (locale) {
145 /* set locale */
146 result = setlocale(category, locale);
147 if (!result) {
148 /* operation failed, no setting was changed */
Martin v. Löwis25f90d52003-09-03 04:50:13 +0000149 PyErr_SetString(Error, "unsupported locale setting");
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000150 return NULL;
151 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000152 result_object = str2uni(result);
Mark Hammond9a714752003-07-24 14:15:07 +0000153 if (!result_object)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000154 return NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000155 } else {
156 /* get locale */
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000157 result = setlocale(category, NULL);
158 if (!result) {
159 PyErr_SetString(Error, "locale query failed");
160 return NULL;
161 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000162 result_object = str2uni(result);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000163 }
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000164 return result_object;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000165}
166
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000167PyDoc_STRVAR(localeconv__doc__,
168"() -> dict. Returns numeric and monetary locale-specific parameters.");
Guido van Rossum220ecc81997-11-18 21:03:39 +0000169
170static PyObject*
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000171PyLocale_localeconv(PyObject* self)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000172{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000173 PyObject* result;
174 struct lconv *l;
175 PyObject *x;
176
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000177 result = PyDict_New();
Fredrik Lundh89610a42000-07-08 20:07:24 +0000178 if (!result)
179 return NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000180
181 /* if LC_NUMERIC is different in the C library, use saved value */
182 l = localeconv();
183
184 /* hopefully, the localeconv result survives the C library calls
185 involved herein */
186
187#define RESULT_STRING(s)\
Martin v. Löwis92fab752008-03-08 10:40:41 +0000188 x = str2uni(l->s); \
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000189 if (!x) goto failed;\
190 PyDict_SetItemString(result, #s, x);\
191 Py_XDECREF(x)
192
193#define RESULT_INT(i)\
Christian Heimes217cfd12007-12-02 14:31:20 +0000194 x = PyLong_FromLong(l->i);\
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000195 if (!x) goto failed;\
196 PyDict_SetItemString(result, #i, x);\
197 Py_XDECREF(x)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000198
199 /* Numeric information */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000200 RESULT_STRING(decimal_point);
201 RESULT_STRING(thousands_sep);
202 x = copy_grouping(l->grouping);
203 if (!x)
204 goto failed;
205 PyDict_SetItemString(result, "grouping", x);
206 Py_XDECREF(x);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000207
208 /* Monetary information */
209 RESULT_STRING(int_curr_symbol);
210 RESULT_STRING(currency_symbol);
211 RESULT_STRING(mon_decimal_point);
212 RESULT_STRING(mon_thousands_sep);
213 x = copy_grouping(l->mon_grouping);
214 if (!x)
215 goto failed;
216 PyDict_SetItemString(result, "mon_grouping", x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000217 Py_XDECREF(x);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000218 RESULT_STRING(positive_sign);
219 RESULT_STRING(negative_sign);
220 RESULT_INT(int_frac_digits);
221 RESULT_INT(frac_digits);
222 RESULT_INT(p_cs_precedes);
223 RESULT_INT(p_sep_by_space);
224 RESULT_INT(n_cs_precedes);
225 RESULT_INT(n_sep_by_space);
226 RESULT_INT(p_sign_posn);
227 RESULT_INT(n_sign_posn);
228 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000229
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000230 failed:
231 Py_XDECREF(result);
232 Py_XDECREF(x);
233 return NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000234}
235
Martin v. Löwis92fab752008-03-08 10:40:41 +0000236#if defined(HAVE_WCSCOLL)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237PyDoc_STRVAR(strcoll__doc__,
238"string,string -> int. Compares two strings according to the locale.");
Guido van Rossum220ecc81997-11-18 21:03:39 +0000239
240static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000241PyLocale_strcoll(PyObject* self, PyObject* args)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000242{
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000243 PyObject *os1, *os2, *result = NULL;
244 wchar_t *ws1 = NULL, *ws2 = NULL;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000245 Py_ssize_t len1, len2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246
Martin v. Löwis92fab752008-03-08 10:40:41 +0000247 if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2))
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000248 return NULL;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000249 /* Convert the unicode strings to wchar[]. */
250 len1 = PyUnicode_GET_SIZE(os1) + 1;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000251 ws1 = PyMem_MALLOC(len1 * sizeof(wchar_t));
252 if (!ws1) {
253 PyErr_NoMemory();
254 goto done;
255 }
256 if (PyUnicode_AsWideChar((PyUnicodeObject*)os1, ws1, len1) == -1)
257 goto done;
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +0000258 ws1[len1 - 1] = 0;
259 len2 = PyUnicode_GET_SIZE(os2) + 1;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000260 ws2 = PyMem_MALLOC(len2 * sizeof(wchar_t));
261 if (!ws2) {
262 PyErr_NoMemory();
263 goto done;
264 }
265 if (PyUnicode_AsWideChar((PyUnicodeObject*)os2, ws2, len2) == -1)
266 goto done;
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +0000267 ws2[len2 - 1] = 0;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000268 /* Collate the strings. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000269 result = PyLong_FromLong(wcscoll(ws1, ws2));
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000270 done:
271 /* Deallocate everything. */
272 if (ws1) PyMem_FREE(ws1);
273 if (ws2) PyMem_FREE(ws2);
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000274 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000275}
Martin v. Löwis92fab752008-03-08 10:40:41 +0000276#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000277
Martin v. Löwis92fab752008-03-08 10:40:41 +0000278#ifdef HAVE_WCSXFRM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000279PyDoc_STRVAR(strxfrm__doc__,
Mark Dickinson211c6252009-02-01 10:28:51 +0000280"strxfrm(string) -> string.\n\
281\n\
282Return a string that can be used as a key for locale-aware comparisons.");
Guido van Rossum220ecc81997-11-18 21:03:39 +0000283
284static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000285PyLocale_strxfrm(PyObject* self, PyObject* args)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000286{
Martin v. Löwis92fab752008-03-08 10:40:41 +0000287 Py_UNICODE *s0;
288 Py_ssize_t n0;
289 wchar_t *s, *buf = NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000290 size_t n1, n2;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000291 PyObject *result = NULL;
Daniel Stutzbach8515eae2010-08-24 21:57:33 +0000292#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T
Martin v. Löwis92fab752008-03-08 10:40:41 +0000293 Py_ssize_t i;
Georg Brandl91373912009-10-27 21:37:48 +0000294#endif
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000295
Martin v. Löwis92fab752008-03-08 10:40:41 +0000296 if (!PyArg_ParseTuple(args, "u#:strxfrm", &s0, &n0))
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000297 return NULL;
298
Daniel Stutzbach8515eae2010-08-24 21:57:33 +0000299#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
300 s = (wchar_t *) s0;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000301#else
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000302 s = PyMem_Malloc((n0+1)*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000303 if (!s)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000304 return PyErr_NoMemory();
Martin v. Löwis92fab752008-03-08 10:40:41 +0000305 for (i=0; i<=n0; i++)
306 s[i] = s0[i];
307#endif
308
309 /* assume no change in size, first */
310 n1 = wcslen(s) + 1;
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000311 buf = PyMem_Malloc(n1*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000312 if (!buf) {
313 PyErr_NoMemory();
314 goto exit;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000315 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000316 n2 = wcsxfrm(buf, s, n1);
317 if (n2 >= n1) {
318 /* more space needed */
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000319 buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000320 if (!buf) {
321 PyErr_NoMemory();
322 goto exit;
323 }
Martin v. Löwisdb1c3992009-05-23 10:38:26 +0000324 n2 = wcsxfrm(buf, s, n2+1);
Martin v. Löwis92fab752008-03-08 10:40:41 +0000325 }
326 result = PyUnicode_FromWideChar(buf, n2);
327 exit:
328 if (buf) PyMem_Free(buf);
Daniel Stutzbach8515eae2010-08-24 21:57:33 +0000329#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T
Martin v. Löwis92fab752008-03-08 10:40:41 +0000330 PyMem_Free(s);
331#endif
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000332 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000333}
Martin v. Löwis92fab752008-03-08 10:40:41 +0000334#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000335
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000336#if defined(MS_WINDOWS)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000337static PyObject*
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000338PyLocale_getdefaultlocale(PyObject* self)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000339{
340 char encoding[100];
341 char locale[100];
342
Tim Peters885d4572001-11-28 20:27:42 +0000343 PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000344
345 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
346 LOCALE_SISO639LANGNAME,
347 locale, sizeof(locale))) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000348 Py_ssize_t i = strlen(locale);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000349 locale[i++] = '_';
350 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
351 LOCALE_SISO3166CTRYNAME,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000352 locale+i, (int)(sizeof(locale)-i)))
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000353 return Py_BuildValue("ss", locale, encoding);
354 }
355
356 /* If we end up here, this windows version didn't know about
357 ISO639/ISO3166 names (it's probably Windows 95). Return the
358 Windows language identifier instead (a hexadecimal number) */
359
360 locale[0] = '0';
361 locale[1] = 'x';
362 if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
363 locale+2, sizeof(locale)-2)) {
364 return Py_BuildValue("ss", locale, encoding);
365 }
366
367 /* cannot determine the language code (very unlikely) */
368 Py_INCREF(Py_None);
369 return Py_BuildValue("Os", Py_None, encoding);
370}
371#endif
372
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000373#ifdef HAVE_LANGINFO_H
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000374#define LANGINFO(X) {#X, X}
Martin v. Löwis59683e82008-06-13 07:50:45 +0000375static struct langinfo_constant{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 char* name;
377 int value;
378} langinfo_constants[] =
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000379{
380 /* These constants should exist on any langinfo implementation */
381 LANGINFO(DAY_1),
382 LANGINFO(DAY_2),
383 LANGINFO(DAY_3),
384 LANGINFO(DAY_4),
385 LANGINFO(DAY_5),
386 LANGINFO(DAY_6),
387 LANGINFO(DAY_7),
388
389 LANGINFO(ABDAY_1),
390 LANGINFO(ABDAY_2),
391 LANGINFO(ABDAY_3),
392 LANGINFO(ABDAY_4),
393 LANGINFO(ABDAY_5),
394 LANGINFO(ABDAY_6),
395 LANGINFO(ABDAY_7),
396
397 LANGINFO(MON_1),
398 LANGINFO(MON_2),
399 LANGINFO(MON_3),
400 LANGINFO(MON_4),
401 LANGINFO(MON_5),
402 LANGINFO(MON_6),
403 LANGINFO(MON_7),
404 LANGINFO(MON_8),
405 LANGINFO(MON_9),
406 LANGINFO(MON_10),
407 LANGINFO(MON_11),
408 LANGINFO(MON_12),
409
410 LANGINFO(ABMON_1),
411 LANGINFO(ABMON_2),
412 LANGINFO(ABMON_3),
413 LANGINFO(ABMON_4),
414 LANGINFO(ABMON_5),
415 LANGINFO(ABMON_6),
416 LANGINFO(ABMON_7),
417 LANGINFO(ABMON_8),
418 LANGINFO(ABMON_9),
419 LANGINFO(ABMON_10),
420 LANGINFO(ABMON_11),
421 LANGINFO(ABMON_12),
422
423#ifdef RADIXCHAR
424 /* The following are not available with glibc 2.0 */
425 LANGINFO(RADIXCHAR),
426 LANGINFO(THOUSEP),
427 /* YESSTR and NOSTR are deprecated in glibc, since they are
428 a special case of message translation, which should be rather
429 done using gettext. So we don't expose it to Python in the
430 first place.
431 LANGINFO(YESSTR),
432 LANGINFO(NOSTR),
433 */
434 LANGINFO(CRNCYSTR),
435#endif
436
437 LANGINFO(D_T_FMT),
438 LANGINFO(D_FMT),
439 LANGINFO(T_FMT),
440 LANGINFO(AM_STR),
441 LANGINFO(PM_STR),
442
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000443 /* The following constants are available only with XPG4, but...
444 AIX 3.2. only has CODESET.
445 OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have
446 a few of the others.
447 Solution: ifdef-test them all. */
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000448#ifdef CODESET
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000449 LANGINFO(CODESET),
Martin v. Löwis496f9e42002-03-27 12:15:57 +0000450#endif
451#ifdef T_FMT_AMPM
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000452 LANGINFO(T_FMT_AMPM),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000453#endif
454#ifdef ERA
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000455 LANGINFO(ERA),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000456#endif
457#ifdef ERA_D_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000458 LANGINFO(ERA_D_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000459#endif
460#ifdef ERA_D_T_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000461 LANGINFO(ERA_D_T_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000462#endif
463#ifdef ERA_T_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000464 LANGINFO(ERA_T_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000465#endif
466#ifdef ALT_DIGITS
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000467 LANGINFO(ALT_DIGITS),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000468#endif
469#ifdef YESEXPR
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000470 LANGINFO(YESEXPR),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000471#endif
472#ifdef NOEXPR
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000473 LANGINFO(NOEXPR),
474#endif
475#ifdef _DATE_FMT
476 /* This is not available in all glibc versions that have CODESET. */
477 LANGINFO(_DATE_FMT),
478#endif
479 {0, 0}
480};
481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482PyDoc_STRVAR(nl_langinfo__doc__,
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000483"nl_langinfo(key) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000484"Return the value for the locale information associated with key.");
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000485
486static PyObject*
487PyLocale_nl_langinfo(PyObject* self, PyObject* args)
488{
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000489 int item, i;
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000490 if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))
491 return NULL;
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000492 /* Check whether this is a supported constant. GNU libc sometimes
493 returns numeric values in the char* return value, which would
Neal Norwitz7f9d29c2007-08-26 07:21:45 +0000494 crash PyUnicode_FromString. */
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000495 for (i = 0; langinfo_constants[i].name; i++)
Hye-Shik Changc3a87b82004-03-21 19:34:30 +0000496 if (langinfo_constants[i].value == item) {
497 /* Check NULL as a workaround for GNU libc's returning NULL
498 instead of an empty string for nl_langinfo(ERA). */
499 const char *result = nl_langinfo(item);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +0000500 result = result != NULL ? result : "";
Martin v. Löwis92fab752008-03-08 10:40:41 +0000501 return str2uni(result);
Hye-Shik Changc3a87b82004-03-21 19:34:30 +0000502 }
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000503 PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");
504 return NULL;
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000505}
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000506#endif /* HAVE_LANGINFO_H */
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000507
508#ifdef HAVE_LIBINTL_H
509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000510PyDoc_STRVAR(gettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000511"gettext(msg) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000512"Return translation of msg.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000513
514static PyObject*
515PyIntl_gettext(PyObject* self, PyObject *args)
516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 char *in;
518 if (!PyArg_ParseTuple(args, "s", &in))
519 return 0;
520 return str2uni(gettext(in));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000521}
522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000523PyDoc_STRVAR(dgettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000524"dgettext(domain, msg) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000525"Return translation of msg in domain.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000526
527static PyObject*
528PyIntl_dgettext(PyObject* self, PyObject *args)
529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 char *domain, *in;
531 if (!PyArg_ParseTuple(args, "zs", &domain, &in))
532 return 0;
533 return str2uni(dgettext(domain, in));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000534}
535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000536PyDoc_STRVAR(dcgettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000537"dcgettext(domain, msg, category) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000538"Return translation of msg in domain and category.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000539
540static PyObject*
541PyIntl_dcgettext(PyObject *self, PyObject *args)
542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 char *domain, *msgid;
544 int category;
545 if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))
546 return 0;
547 return str2uni(dcgettext(domain,msgid,category));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000548}
549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000550PyDoc_STRVAR(textdomain__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000551"textdomain(domain) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000552"Set the C library's textdmain to domain, returning the new domain.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000553
554static PyObject*
555PyIntl_textdomain(PyObject* self, PyObject* args)
556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 char *domain;
558 if (!PyArg_ParseTuple(args, "z", &domain))
559 return 0;
560 domain = textdomain(domain);
561 if (!domain) {
562 PyErr_SetFromErrno(PyExc_OSError);
563 return NULL;
564 }
565 return str2uni(domain);
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000566}
567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000568PyDoc_STRVAR(bindtextdomain__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000569"bindtextdomain(domain, dir) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000570"Bind the C library's domain to dir.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000571
572static PyObject*
573PyIntl_bindtextdomain(PyObject* self,PyObject*args)
574{
Victor Stinner9e19ca42010-06-11 22:09:51 +0000575 char *domain, *dirname, *current_dirname;
576 PyObject *dirname_obj, *dirname_bytes = NULL, *result;
577 if (!PyArg_ParseTuple(args, "sO", &domain, &dirname_obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 return 0;
579 if (!strlen(domain)) {
580 PyErr_SetString(Error, "domain must be a non-empty string");
581 return 0;
582 }
Victor Stinner9e19ca42010-06-11 22:09:51 +0000583 if (dirname_obj != Py_None) {
584 if (!PyUnicode_FSConverter(dirname_obj, &dirname_bytes))
585 return NULL;
586 dirname = PyBytes_AsString(dirname_bytes);
587 } else {
588 dirname_bytes = NULL;
589 dirname = NULL;
590 }
591 current_dirname = bindtextdomain(domain, dirname);
592 if (current_dirname == NULL) {
593 Py_XDECREF(dirname_bytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyErr_SetFromErrno(PyExc_OSError);
595 return NULL;
596 }
Victor Stinner9e19ca42010-06-11 22:09:51 +0000597 result = str2uni(current_dirname);
598 Py_XDECREF(dirname_bytes);
599 return result;
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000600}
601
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000602#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
603PyDoc_STRVAR(bind_textdomain_codeset__doc__,
604"bind_textdomain_codeset(domain, codeset) -> string\n"
605"Bind the C library's domain to codeset.");
606
607static PyObject*
608PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)
609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 char *domain,*codeset;
611 if (!PyArg_ParseTuple(args, "sz", &domain, &codeset))
612 return NULL;
613 codeset = bind_textdomain_codeset(domain, codeset);
614 if (codeset)
615 return str2uni(codeset);
616 Py_RETURN_NONE;
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000617}
618#endif
619
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000620#endif
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000621
Guido van Rossum220ecc81997-11-18 21:03:39 +0000622static struct PyMethodDef PyLocale_Methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 {"setlocale", (PyCFunction) PyLocale_setlocale,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000624 METH_VARARGS, setlocale__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 {"localeconv", (PyCFunction) PyLocale_localeconv,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000626 METH_NOARGS, localeconv__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000627#ifdef HAVE_WCSCOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 {"strcoll", (PyCFunction) PyLocale_strcoll,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000629 METH_VARARGS, strcoll__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000630#endif
631#ifdef HAVE_WCSXFRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 {"strxfrm", (PyCFunction) PyLocale_strxfrm,
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000633 METH_VARARGS, strxfrm__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000634#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635#if defined(MS_WINDOWS)
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000636 {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS},
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000637#endif
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000638#ifdef HAVE_LANGINFO_H
639 {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,
640 METH_VARARGS, nl_langinfo__doc__},
641#endif
Martin v. Löwisc6a7d7e2002-05-02 12:16:29 +0000642#ifdef HAVE_LIBINTL_H
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000643 {"gettext",(PyCFunction)PyIntl_gettext,METH_VARARGS,
644 gettext__doc__},
645 {"dgettext",(PyCFunction)PyIntl_dgettext,METH_VARARGS,
646 dgettext__doc__},
647 {"dcgettext",(PyCFunction)PyIntl_dcgettext,METH_VARARGS,
648 dcgettext__doc__},
649 {"textdomain",(PyCFunction)PyIntl_textdomain,METH_VARARGS,
650 textdomain__doc__},
651 {"bindtextdomain",(PyCFunction)PyIntl_bindtextdomain,METH_VARARGS,
652 bindtextdomain__doc__},
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000653#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
654 {"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset,
655 METH_VARARGS, bind_textdomain_codeset__doc__},
656#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000658 {NULL, NULL}
659};
660
Martin v. Löwis1a214512008-06-11 05:26:20 +0000661
662static struct PyModuleDef _localemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyModuleDef_HEAD_INIT,
664 "_locale",
665 locale__doc__,
666 -1,
667 PyLocale_Methods,
668 NULL,
669 NULL,
670 NULL,
671 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000672};
673
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000674PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000675PyInit__locale(void)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000676{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000677 PyObject *m, *d, *x;
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000678#ifdef HAVE_LANGINFO_H
679 int i;
680#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000681
Martin v. Löwis1a214512008-06-11 05:26:20 +0000682 m = PyModule_Create(&_localemodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000683 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000685
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000686 d = PyModule_GetDict(m);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000687
Christian Heimes217cfd12007-12-02 14:31:20 +0000688 x = PyLong_FromLong(LC_CTYPE);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000689 PyDict_SetItemString(d, "LC_CTYPE", x);
690 Py_XDECREF(x);
691
Christian Heimes217cfd12007-12-02 14:31:20 +0000692 x = PyLong_FromLong(LC_TIME);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000693 PyDict_SetItemString(d, "LC_TIME", x);
694 Py_XDECREF(x);
695
Christian Heimes217cfd12007-12-02 14:31:20 +0000696 x = PyLong_FromLong(LC_COLLATE);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000697 PyDict_SetItemString(d, "LC_COLLATE", x);
698 Py_XDECREF(x);
699
Christian Heimes217cfd12007-12-02 14:31:20 +0000700 x = PyLong_FromLong(LC_MONETARY);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000701 PyDict_SetItemString(d, "LC_MONETARY", x);
702 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000703
Guido van Rossum8d9c2e31997-12-09 19:35:11 +0000704#ifdef LC_MESSAGES
Christian Heimes217cfd12007-12-02 14:31:20 +0000705 x = PyLong_FromLong(LC_MESSAGES);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000706 PyDict_SetItemString(d, "LC_MESSAGES", x);
707 Py_XDECREF(x);
Guido van Rossum8d9c2e31997-12-09 19:35:11 +0000708#endif /* LC_MESSAGES */
Guido van Rossum220ecc81997-11-18 21:03:39 +0000709
Christian Heimes217cfd12007-12-02 14:31:20 +0000710 x = PyLong_FromLong(LC_NUMERIC);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000711 PyDict_SetItemString(d, "LC_NUMERIC", x);
712 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000713
Christian Heimes217cfd12007-12-02 14:31:20 +0000714 x = PyLong_FromLong(LC_ALL);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000715 PyDict_SetItemString(d, "LC_ALL", x);
716 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000717
Christian Heimes217cfd12007-12-02 14:31:20 +0000718 x = PyLong_FromLong(CHAR_MAX);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000719 PyDict_SetItemString(d, "CHAR_MAX", x);
720 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000721
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000722 Error = PyErr_NewException("locale.Error", NULL, NULL);
723 PyDict_SetItemString(d, "Error", Error);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000724
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000725#ifdef HAVE_LANGINFO_H
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000726 for (i = 0; langinfo_constants[i].name; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyModule_AddIntConstant(m, langinfo_constants[i].name,
728 langinfo_constants[i].value);
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000729 }
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000730#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000731 return m;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000732}
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734/*
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000735Local variables:
736c-basic-offset: 4
737indent-tabs-mode: nil
738End:
739*/