blob: fa6ab8fc6d89c048a952bf4092c4269f355c5061 [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;
80}
81
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
136 if (locale) {
137 /* set locale */
138 result = setlocale(category, locale);
139 if (!result) {
140 /* operation failed, no setting was changed */
Martin v. Löwis25f90d52003-09-03 04:50:13 +0000141 PyErr_SetString(Error, "unsupported locale setting");
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000142 return NULL;
143 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000144 result_object = str2uni(result);
Mark Hammond9a714752003-07-24 14:15:07 +0000145 if (!result_object)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000146 return NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000147 } else {
148 /* get locale */
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000149 result = setlocale(category, NULL);
150 if (!result) {
151 PyErr_SetString(Error, "locale query failed");
152 return NULL;
153 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000154 result_object = str2uni(result);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000155 }
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000156 return result_object;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000157}
158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000159PyDoc_STRVAR(localeconv__doc__,
160"() -> dict. Returns numeric and monetary locale-specific parameters.");
Guido van Rossum220ecc81997-11-18 21:03:39 +0000161
162static PyObject*
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000163PyLocale_localeconv(PyObject* self)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000164{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000165 PyObject* result;
166 struct lconv *l;
167 PyObject *x;
168
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000169 result = PyDict_New();
Fredrik Lundh89610a42000-07-08 20:07:24 +0000170 if (!result)
171 return NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000172
173 /* if LC_NUMERIC is different in the C library, use saved value */
174 l = localeconv();
175
176 /* hopefully, the localeconv result survives the C library calls
177 involved herein */
178
179#define RESULT_STRING(s)\
Martin v. Löwis92fab752008-03-08 10:40:41 +0000180 x = str2uni(l->s); \
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000181 if (!x) goto failed;\
182 PyDict_SetItemString(result, #s, x);\
183 Py_XDECREF(x)
184
185#define RESULT_INT(i)\
Christian Heimes217cfd12007-12-02 14:31:20 +0000186 x = PyLong_FromLong(l->i);\
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000187 if (!x) goto failed;\
188 PyDict_SetItemString(result, #i, x);\
189 Py_XDECREF(x)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000190
191 /* Numeric information */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000192 RESULT_STRING(decimal_point);
193 RESULT_STRING(thousands_sep);
194 x = copy_grouping(l->grouping);
195 if (!x)
196 goto failed;
197 PyDict_SetItemString(result, "grouping", x);
198 Py_XDECREF(x);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000199
200 /* Monetary information */
201 RESULT_STRING(int_curr_symbol);
202 RESULT_STRING(currency_symbol);
203 RESULT_STRING(mon_decimal_point);
204 RESULT_STRING(mon_thousands_sep);
205 x = copy_grouping(l->mon_grouping);
206 if (!x)
207 goto failed;
208 PyDict_SetItemString(result, "mon_grouping", x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000209 Py_XDECREF(x);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000210 RESULT_STRING(positive_sign);
211 RESULT_STRING(negative_sign);
212 RESULT_INT(int_frac_digits);
213 RESULT_INT(frac_digits);
214 RESULT_INT(p_cs_precedes);
215 RESULT_INT(p_sep_by_space);
216 RESULT_INT(n_cs_precedes);
217 RESULT_INT(n_sep_by_space);
218 RESULT_INT(p_sign_posn);
219 RESULT_INT(n_sign_posn);
220 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000221
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000222 failed:
223 Py_XDECREF(result);
224 Py_XDECREF(x);
225 return NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000226}
227
Martin v. Löwis92fab752008-03-08 10:40:41 +0000228#if defined(HAVE_WCSCOLL)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000229PyDoc_STRVAR(strcoll__doc__,
230"string,string -> int. Compares two strings according to the locale.");
Guido van Rossum220ecc81997-11-18 21:03:39 +0000231
232static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000233PyLocale_strcoll(PyObject* self, PyObject* args)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000234{
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000235 PyObject *os1, *os2, *result = NULL;
236 wchar_t *ws1 = NULL, *ws2 = NULL;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000237 Py_ssize_t len1, len2;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000238
Martin v. Löwis92fab752008-03-08 10:40:41 +0000239 if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2))
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000240 return NULL;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000241 /* Convert the unicode strings to wchar[]. */
242 len1 = PyUnicode_GET_SIZE(os1) + 1;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000243 ws1 = PyMem_MALLOC(len1 * sizeof(wchar_t));
244 if (!ws1) {
245 PyErr_NoMemory();
246 goto done;
247 }
248 if (PyUnicode_AsWideChar((PyUnicodeObject*)os1, ws1, len1) == -1)
249 goto done;
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +0000250 ws1[len1 - 1] = 0;
251 len2 = PyUnicode_GET_SIZE(os2) + 1;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000252 ws2 = PyMem_MALLOC(len2 * sizeof(wchar_t));
253 if (!ws2) {
254 PyErr_NoMemory();
255 goto done;
256 }
257 if (PyUnicode_AsWideChar((PyUnicodeObject*)os2, ws2, len2) == -1)
258 goto done;
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +0000259 ws2[len2 - 1] = 0;
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000260 /* Collate the strings. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000261 result = PyLong_FromLong(wcscoll(ws1, ws2));
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000262 done:
263 /* Deallocate everything. */
264 if (ws1) PyMem_FREE(ws1);
265 if (ws2) PyMem_FREE(ws2);
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000266 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000267}
Martin v. Löwis92fab752008-03-08 10:40:41 +0000268#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000269
Martin v. Löwis92fab752008-03-08 10:40:41 +0000270#ifdef HAVE_WCSXFRM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271PyDoc_STRVAR(strxfrm__doc__,
Mark Dickinson211c6252009-02-01 10:28:51 +0000272"strxfrm(string) -> string.\n\
273\n\
274Return a string that can be used as a key for locale-aware comparisons.");
Guido van Rossum220ecc81997-11-18 21:03:39 +0000275
276static PyObject*
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000277PyLocale_strxfrm(PyObject* self, PyObject* args)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000278{
Martin v. Löwis92fab752008-03-08 10:40:41 +0000279 Py_UNICODE *s0;
280 Py_ssize_t n0;
281 wchar_t *s, *buf = NULL;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000282 size_t n1, n2;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000283 PyObject *result = NULL;
Georg Brandl91373912009-10-27 21:37:48 +0000284#ifndef HAVE_USABLE_WCHAR_T
Martin v. Löwis92fab752008-03-08 10:40:41 +0000285 Py_ssize_t i;
Georg Brandl91373912009-10-27 21:37:48 +0000286#endif
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000287
Martin v. Löwis92fab752008-03-08 10:40:41 +0000288 if (!PyArg_ParseTuple(args, "u#:strxfrm", &s0, &n0))
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000289 return NULL;
290
Martin v. Löwis92fab752008-03-08 10:40:41 +0000291#ifdef HAVE_USABLE_WCHAR_T
292 s = s0;
293#else
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000294 s = PyMem_Malloc((n0+1)*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000295 if (!s)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000296 return PyErr_NoMemory();
Martin v. Löwis92fab752008-03-08 10:40:41 +0000297 for (i=0; i<=n0; i++)
298 s[i] = s0[i];
299#endif
300
301 /* assume no change in size, first */
302 n1 = wcslen(s) + 1;
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000303 buf = PyMem_Malloc(n1*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000304 if (!buf) {
305 PyErr_NoMemory();
306 goto exit;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000307 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000308 n2 = wcsxfrm(buf, s, n1);
309 if (n2 >= n1) {
310 /* more space needed */
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000311 buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000312 if (!buf) {
313 PyErr_NoMemory();
314 goto exit;
315 }
Martin v. Löwisdb1c3992009-05-23 10:38:26 +0000316 n2 = wcsxfrm(buf, s, n2+1);
Martin v. Löwis92fab752008-03-08 10:40:41 +0000317 }
318 result = PyUnicode_FromWideChar(buf, n2);
319 exit:
320 if (buf) PyMem_Free(buf);
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000321#ifndef HAVE_USABLE_WCHAR_T
Martin v. Löwis92fab752008-03-08 10:40:41 +0000322 PyMem_Free(s);
323#endif
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000324 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000325}
Martin v. Löwis92fab752008-03-08 10:40:41 +0000326#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000327
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000328#if defined(MS_WINDOWS)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000329static PyObject*
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000330PyLocale_getdefaultlocale(PyObject* self)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000331{
332 char encoding[100];
333 char locale[100];
334
Tim Peters885d4572001-11-28 20:27:42 +0000335 PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000336
337 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
338 LOCALE_SISO639LANGNAME,
339 locale, sizeof(locale))) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000340 Py_ssize_t i = strlen(locale);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000341 locale[i++] = '_';
342 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
343 LOCALE_SISO3166CTRYNAME,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000344 locale+i, (int)(sizeof(locale)-i)))
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000345 return Py_BuildValue("ss", locale, encoding);
346 }
347
348 /* If we end up here, this windows version didn't know about
349 ISO639/ISO3166 names (it's probably Windows 95). Return the
350 Windows language identifier instead (a hexadecimal number) */
351
352 locale[0] = '0';
353 locale[1] = 'x';
354 if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
355 locale+2, sizeof(locale)-2)) {
356 return Py_BuildValue("ss", locale, encoding);
357 }
358
359 /* cannot determine the language code (very unlikely) */
360 Py_INCREF(Py_None);
361 return Py_BuildValue("Os", Py_None, encoding);
362}
363#endif
364
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000365#ifdef HAVE_LANGINFO_H
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000366#define LANGINFO(X) {#X, X}
Martin v. Löwis59683e82008-06-13 07:50:45 +0000367static struct langinfo_constant{
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000368 char* name;
369 int value;
370} langinfo_constants[] =
371{
372 /* These constants should exist on any langinfo implementation */
373 LANGINFO(DAY_1),
374 LANGINFO(DAY_2),
375 LANGINFO(DAY_3),
376 LANGINFO(DAY_4),
377 LANGINFO(DAY_5),
378 LANGINFO(DAY_6),
379 LANGINFO(DAY_7),
380
381 LANGINFO(ABDAY_1),
382 LANGINFO(ABDAY_2),
383 LANGINFO(ABDAY_3),
384 LANGINFO(ABDAY_4),
385 LANGINFO(ABDAY_5),
386 LANGINFO(ABDAY_6),
387 LANGINFO(ABDAY_7),
388
389 LANGINFO(MON_1),
390 LANGINFO(MON_2),
391 LANGINFO(MON_3),
392 LANGINFO(MON_4),
393 LANGINFO(MON_5),
394 LANGINFO(MON_6),
395 LANGINFO(MON_7),
396 LANGINFO(MON_8),
397 LANGINFO(MON_9),
398 LANGINFO(MON_10),
399 LANGINFO(MON_11),
400 LANGINFO(MON_12),
401
402 LANGINFO(ABMON_1),
403 LANGINFO(ABMON_2),
404 LANGINFO(ABMON_3),
405 LANGINFO(ABMON_4),
406 LANGINFO(ABMON_5),
407 LANGINFO(ABMON_6),
408 LANGINFO(ABMON_7),
409 LANGINFO(ABMON_8),
410 LANGINFO(ABMON_9),
411 LANGINFO(ABMON_10),
412 LANGINFO(ABMON_11),
413 LANGINFO(ABMON_12),
414
415#ifdef RADIXCHAR
416 /* The following are not available with glibc 2.0 */
417 LANGINFO(RADIXCHAR),
418 LANGINFO(THOUSEP),
419 /* YESSTR and NOSTR are deprecated in glibc, since they are
420 a special case of message translation, which should be rather
421 done using gettext. So we don't expose it to Python in the
422 first place.
423 LANGINFO(YESSTR),
424 LANGINFO(NOSTR),
425 */
426 LANGINFO(CRNCYSTR),
427#endif
428
429 LANGINFO(D_T_FMT),
430 LANGINFO(D_FMT),
431 LANGINFO(T_FMT),
432 LANGINFO(AM_STR),
433 LANGINFO(PM_STR),
434
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000435 /* The following constants are available only with XPG4, but...
436 AIX 3.2. only has CODESET.
437 OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have
438 a few of the others.
439 Solution: ifdef-test them all. */
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000440#ifdef CODESET
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000441 LANGINFO(CODESET),
Martin v. Löwis496f9e42002-03-27 12:15:57 +0000442#endif
443#ifdef T_FMT_AMPM
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000444 LANGINFO(T_FMT_AMPM),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000445#endif
446#ifdef ERA
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000447 LANGINFO(ERA),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000448#endif
449#ifdef ERA_D_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000450 LANGINFO(ERA_D_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000451#endif
452#ifdef ERA_D_T_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000453 LANGINFO(ERA_D_T_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000454#endif
455#ifdef ERA_T_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000456 LANGINFO(ERA_T_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000457#endif
458#ifdef ALT_DIGITS
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000459 LANGINFO(ALT_DIGITS),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000460#endif
461#ifdef YESEXPR
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000462 LANGINFO(YESEXPR),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000463#endif
464#ifdef NOEXPR
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000465 LANGINFO(NOEXPR),
466#endif
467#ifdef _DATE_FMT
468 /* This is not available in all glibc versions that have CODESET. */
469 LANGINFO(_DATE_FMT),
470#endif
471 {0, 0}
472};
473
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000474PyDoc_STRVAR(nl_langinfo__doc__,
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000475"nl_langinfo(key) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000476"Return the value for the locale information associated with key.");
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000477
478static PyObject*
479PyLocale_nl_langinfo(PyObject* self, PyObject* args)
480{
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000481 int item, i;
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000482 if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))
483 return NULL;
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000484 /* Check whether this is a supported constant. GNU libc sometimes
485 returns numeric values in the char* return value, which would
Neal Norwitz7f9d29c2007-08-26 07:21:45 +0000486 crash PyUnicode_FromString. */
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000487 for (i = 0; langinfo_constants[i].name; i++)
Hye-Shik Changc3a87b82004-03-21 19:34:30 +0000488 if (langinfo_constants[i].value == item) {
489 /* Check NULL as a workaround for GNU libc's returning NULL
490 instead of an empty string for nl_langinfo(ERA). */
491 const char *result = nl_langinfo(item);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +0000492 result = result != NULL ? result : "";
Martin v. Löwis92fab752008-03-08 10:40:41 +0000493 return str2uni(result);
Hye-Shik Changc3a87b82004-03-21 19:34:30 +0000494 }
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000495 PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");
496 return NULL;
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000497}
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000498#endif /* HAVE_LANGINFO_H */
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000499
500#ifdef HAVE_LIBINTL_H
501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000502PyDoc_STRVAR(gettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000503"gettext(msg) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504"Return translation of msg.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000505
506static PyObject*
507PyIntl_gettext(PyObject* self, PyObject *args)
508{
509 char *in;
Georg Brandl3dbca812008-07-23 16:10:53 +0000510 if (!PyArg_ParseTuple(args, "s", &in))
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000511 return 0;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000512 return str2uni(gettext(in));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000513}
514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000515PyDoc_STRVAR(dgettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000516"dgettext(domain, msg) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517"Return translation of msg in domain.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000518
519static PyObject*
520PyIntl_dgettext(PyObject* self, PyObject *args)
521{
522 char *domain, *in;
Georg Brandl3dbca812008-07-23 16:10:53 +0000523 if (!PyArg_ParseTuple(args, "zs", &domain, &in))
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000524 return 0;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000525 return str2uni(dgettext(domain, in));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000526}
527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000528PyDoc_STRVAR(dcgettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000529"dcgettext(domain, msg, category) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000530"Return translation of msg in domain and category.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000531
532static PyObject*
533PyIntl_dcgettext(PyObject *self, PyObject *args)
534{
535 char *domain, *msgid;
536 int category;
Georg Brandl3dbca812008-07-23 16:10:53 +0000537 if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000538 return 0;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000539 return str2uni(dcgettext(domain,msgid,category));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000540}
541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000542PyDoc_STRVAR(textdomain__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000543"textdomain(domain) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000544"Set the C library's textdmain to domain, returning the new domain.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000545
546static PyObject*
547PyIntl_textdomain(PyObject* self, PyObject* args)
548{
549 char *domain;
550 if (!PyArg_ParseTuple(args, "z", &domain))
551 return 0;
552 domain = textdomain(domain);
553 if (!domain) {
554 PyErr_SetFromErrno(PyExc_OSError);
555 return NULL;
556 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000557 return str2uni(domain);
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000558}
559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000560PyDoc_STRVAR(bindtextdomain__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000561"bindtextdomain(domain, dir) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000562"Bind the C library's domain to dir.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000563
564static PyObject*
565PyIntl_bindtextdomain(PyObject* self,PyObject*args)
566{
Georg Brandl3dbca812008-07-23 16:10:53 +0000567 char *domain, *dirname;
568 if (!PyArg_ParseTuple(args, "sz", &domain, &dirname))
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000569 return 0;
Georg Brandl3dbca812008-07-23 16:10:53 +0000570 if (!strlen(domain)) {
571 PyErr_SetString(Error, "domain must be a non-empty string");
572 return 0;
573 }
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000574 dirname = bindtextdomain(domain, dirname);
575 if (!dirname) {
576 PyErr_SetFromErrno(PyExc_OSError);
577 return NULL;
578 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000579 return str2uni(dirname);
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000580}
581
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000582#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
583PyDoc_STRVAR(bind_textdomain_codeset__doc__,
584"bind_textdomain_codeset(domain, codeset) -> string\n"
585"Bind the C library's domain to codeset.");
586
587static PyObject*
588PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)
589{
590 char *domain,*codeset;
591 if (!PyArg_ParseTuple(args, "sz", &domain, &codeset))
592 return NULL;
593 codeset = bind_textdomain_codeset(domain, codeset);
594 if (codeset)
Martin v. Löwis92fab752008-03-08 10:40:41 +0000595 return str2uni(codeset);
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000596 Py_RETURN_NONE;
597}
598#endif
599
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000600#endif
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000601
Guido van Rossum220ecc81997-11-18 21:03:39 +0000602static struct PyMethodDef PyLocale_Methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000603 {"setlocale", (PyCFunction) PyLocale_setlocale,
604 METH_VARARGS, setlocale__doc__},
605 {"localeconv", (PyCFunction) PyLocale_localeconv,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000606 METH_NOARGS, localeconv__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000607#ifdef HAVE_WCSCOLL
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000608 {"strcoll", (PyCFunction) PyLocale_strcoll,
609 METH_VARARGS, strcoll__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000610#endif
611#ifdef HAVE_WCSXFRM
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000612 {"strxfrm", (PyCFunction) PyLocale_strxfrm,
613 METH_VARARGS, strxfrm__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000614#endif
Ronald Oussorenfe8a3d62009-06-07 15:29:46 +0000615#if defined(MS_WINDOWS)
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000616 {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS},
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000617#endif
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000618#ifdef HAVE_LANGINFO_H
619 {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,
620 METH_VARARGS, nl_langinfo__doc__},
621#endif
Martin v. Löwisc6a7d7e2002-05-02 12:16:29 +0000622#ifdef HAVE_LIBINTL_H
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000623 {"gettext",(PyCFunction)PyIntl_gettext,METH_VARARGS,
624 gettext__doc__},
625 {"dgettext",(PyCFunction)PyIntl_dgettext,METH_VARARGS,
626 dgettext__doc__},
627 {"dcgettext",(PyCFunction)PyIntl_dcgettext,METH_VARARGS,
628 dcgettext__doc__},
629 {"textdomain",(PyCFunction)PyIntl_textdomain,METH_VARARGS,
630 textdomain__doc__},
631 {"bindtextdomain",(PyCFunction)PyIntl_bindtextdomain,METH_VARARGS,
632 bindtextdomain__doc__},
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000633#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
634 {"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset,
635 METH_VARARGS, bind_textdomain_codeset__doc__},
636#endif
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000637#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000638 {NULL, NULL}
639};
640
Martin v. Löwis1a214512008-06-11 05:26:20 +0000641
642static struct PyModuleDef _localemodule = {
643 PyModuleDef_HEAD_INIT,
644 "_locale",
645 locale__doc__,
646 -1,
647 PyLocale_Methods,
648 NULL,
649 NULL,
650 NULL,
651 NULL
652};
653
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000654PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000655PyInit__locale(void)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000656{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000657 PyObject *m, *d, *x;
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000658#ifdef HAVE_LANGINFO_H
659 int i;
660#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000661
Martin v. Löwis1a214512008-06-11 05:26:20 +0000662 m = PyModule_Create(&_localemodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000663 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000664 return NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000665
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000666 d = PyModule_GetDict(m);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000667
Christian Heimes217cfd12007-12-02 14:31:20 +0000668 x = PyLong_FromLong(LC_CTYPE);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000669 PyDict_SetItemString(d, "LC_CTYPE", x);
670 Py_XDECREF(x);
671
Christian Heimes217cfd12007-12-02 14:31:20 +0000672 x = PyLong_FromLong(LC_TIME);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000673 PyDict_SetItemString(d, "LC_TIME", x);
674 Py_XDECREF(x);
675
Christian Heimes217cfd12007-12-02 14:31:20 +0000676 x = PyLong_FromLong(LC_COLLATE);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000677 PyDict_SetItemString(d, "LC_COLLATE", x);
678 Py_XDECREF(x);
679
Christian Heimes217cfd12007-12-02 14:31:20 +0000680 x = PyLong_FromLong(LC_MONETARY);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000681 PyDict_SetItemString(d, "LC_MONETARY", x);
682 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000683
Guido van Rossum8d9c2e31997-12-09 19:35:11 +0000684#ifdef LC_MESSAGES
Christian Heimes217cfd12007-12-02 14:31:20 +0000685 x = PyLong_FromLong(LC_MESSAGES);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000686 PyDict_SetItemString(d, "LC_MESSAGES", x);
687 Py_XDECREF(x);
Guido van Rossum8d9c2e31997-12-09 19:35:11 +0000688#endif /* LC_MESSAGES */
Guido van Rossum220ecc81997-11-18 21:03:39 +0000689
Christian Heimes217cfd12007-12-02 14:31:20 +0000690 x = PyLong_FromLong(LC_NUMERIC);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000691 PyDict_SetItemString(d, "LC_NUMERIC", x);
692 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000693
Christian Heimes217cfd12007-12-02 14:31:20 +0000694 x = PyLong_FromLong(LC_ALL);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000695 PyDict_SetItemString(d, "LC_ALL", x);
696 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000697
Christian Heimes217cfd12007-12-02 14:31:20 +0000698 x = PyLong_FromLong(CHAR_MAX);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000699 PyDict_SetItemString(d, "CHAR_MAX", x);
700 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000701
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000702 Error = PyErr_NewException("locale.Error", NULL, NULL);
703 PyDict_SetItemString(d, "Error", Error);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000704
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000705#ifdef HAVE_LANGINFO_H
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000706 for (i = 0; langinfo_constants[i].name; i++) {
707 PyModule_AddIntConstant(m, langinfo_constants[i].name,
708 langinfo_constants[i].value);
709 }
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000710#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000711 return m;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000712}
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000713
714/*
715Local variables:
716c-basic-offset: 4
717indent-tabs-mode: nil
718End:
719*/