blob: d90a49df969339fa10663962da8eaa3f5f6b602b [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;
284 Py_ssize_t i;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000285
Martin v. Löwis92fab752008-03-08 10:40:41 +0000286 if (!PyArg_ParseTuple(args, "u#:strxfrm", &s0, &n0))
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000287 return NULL;
288
Martin v. Löwis92fab752008-03-08 10:40:41 +0000289#ifdef HAVE_USABLE_WCHAR_T
290 s = s0;
291#else
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000292 s = PyMem_Malloc((n0+1)*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000293 if (!s)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000294 return PyErr_NoMemory();
Martin v. Löwis92fab752008-03-08 10:40:41 +0000295 for (i=0; i<=n0; i++)
296 s[i] = s0[i];
297#endif
298
299 /* assume no change in size, first */
300 n1 = wcslen(s) + 1;
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000301 buf = PyMem_Malloc(n1*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000302 if (!buf) {
303 PyErr_NoMemory();
304 goto exit;
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000305 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000306 n2 = wcsxfrm(buf, s, n1);
307 if (n2 >= n1) {
308 /* more space needed */
Martin v. Löwisa69e1ef2008-03-08 10:54:31 +0000309 buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
Martin v. Löwis92fab752008-03-08 10:40:41 +0000310 if (!buf) {
311 PyErr_NoMemory();
312 goto exit;
313 }
Martin v. Löwisdb1c3992009-05-23 10:38:26 +0000314 n2 = wcsxfrm(buf, s, n2+1);
Martin v. Löwis92fab752008-03-08 10:40:41 +0000315 }
316 result = PyUnicode_FromWideChar(buf, n2);
317 exit:
318 if (buf) PyMem_Free(buf);
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000319#ifndef HAVE_USABLE_WCHAR_T
Martin v. Löwis92fab752008-03-08 10:40:41 +0000320 PyMem_Free(s);
321#endif
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000322 return result;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000323}
Martin v. Löwis92fab752008-03-08 10:40:41 +0000324#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000325
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000326#if defined(MS_WINDOWS)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000327static PyObject*
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000328PyLocale_getdefaultlocale(PyObject* self)
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000329{
330 char encoding[100];
331 char locale[100];
332
Tim Peters885d4572001-11-28 20:27:42 +0000333 PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000334
335 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
336 LOCALE_SISO639LANGNAME,
337 locale, sizeof(locale))) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000338 Py_ssize_t i = strlen(locale);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000339 locale[i++] = '_';
340 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
341 LOCALE_SISO3166CTRYNAME,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000342 locale+i, (int)(sizeof(locale)-i)))
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000343 return Py_BuildValue("ss", locale, encoding);
344 }
345
346 /* If we end up here, this windows version didn't know about
347 ISO639/ISO3166 names (it's probably Windows 95). Return the
348 Windows language identifier instead (a hexadecimal number) */
349
350 locale[0] = '0';
351 locale[1] = 'x';
352 if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
353 locale+2, sizeof(locale)-2)) {
354 return Py_BuildValue("ss", locale, encoding);
355 }
356
357 /* cannot determine the language code (very unlikely) */
358 Py_INCREF(Py_None);
359 return Py_BuildValue("Os", Py_None, encoding);
360}
361#endif
362
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000363#ifdef HAVE_LANGINFO_H
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000364#define LANGINFO(X) {#X, X}
Martin v. Löwis59683e82008-06-13 07:50:45 +0000365static struct langinfo_constant{
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000366 char* name;
367 int value;
368} langinfo_constants[] =
369{
370 /* These constants should exist on any langinfo implementation */
371 LANGINFO(DAY_1),
372 LANGINFO(DAY_2),
373 LANGINFO(DAY_3),
374 LANGINFO(DAY_4),
375 LANGINFO(DAY_5),
376 LANGINFO(DAY_6),
377 LANGINFO(DAY_7),
378
379 LANGINFO(ABDAY_1),
380 LANGINFO(ABDAY_2),
381 LANGINFO(ABDAY_3),
382 LANGINFO(ABDAY_4),
383 LANGINFO(ABDAY_5),
384 LANGINFO(ABDAY_6),
385 LANGINFO(ABDAY_7),
386
387 LANGINFO(MON_1),
388 LANGINFO(MON_2),
389 LANGINFO(MON_3),
390 LANGINFO(MON_4),
391 LANGINFO(MON_5),
392 LANGINFO(MON_6),
393 LANGINFO(MON_7),
394 LANGINFO(MON_8),
395 LANGINFO(MON_9),
396 LANGINFO(MON_10),
397 LANGINFO(MON_11),
398 LANGINFO(MON_12),
399
400 LANGINFO(ABMON_1),
401 LANGINFO(ABMON_2),
402 LANGINFO(ABMON_3),
403 LANGINFO(ABMON_4),
404 LANGINFO(ABMON_5),
405 LANGINFO(ABMON_6),
406 LANGINFO(ABMON_7),
407 LANGINFO(ABMON_8),
408 LANGINFO(ABMON_9),
409 LANGINFO(ABMON_10),
410 LANGINFO(ABMON_11),
411 LANGINFO(ABMON_12),
412
413#ifdef RADIXCHAR
414 /* The following are not available with glibc 2.0 */
415 LANGINFO(RADIXCHAR),
416 LANGINFO(THOUSEP),
417 /* YESSTR and NOSTR are deprecated in glibc, since they are
418 a special case of message translation, which should be rather
419 done using gettext. So we don't expose it to Python in the
420 first place.
421 LANGINFO(YESSTR),
422 LANGINFO(NOSTR),
423 */
424 LANGINFO(CRNCYSTR),
425#endif
426
427 LANGINFO(D_T_FMT),
428 LANGINFO(D_FMT),
429 LANGINFO(T_FMT),
430 LANGINFO(AM_STR),
431 LANGINFO(PM_STR),
432
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000433 /* The following constants are available only with XPG4, but...
434 AIX 3.2. only has CODESET.
435 OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have
436 a few of the others.
437 Solution: ifdef-test them all. */
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000438#ifdef CODESET
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000439 LANGINFO(CODESET),
Martin v. Löwis496f9e42002-03-27 12:15:57 +0000440#endif
441#ifdef T_FMT_AMPM
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000442 LANGINFO(T_FMT_AMPM),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000443#endif
444#ifdef ERA
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000445 LANGINFO(ERA),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000446#endif
447#ifdef ERA_D_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000448 LANGINFO(ERA_D_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000449#endif
450#ifdef ERA_D_T_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000451 LANGINFO(ERA_D_T_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000452#endif
453#ifdef ERA_T_FMT
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000454 LANGINFO(ERA_T_FMT),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000455#endif
456#ifdef ALT_DIGITS
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000457 LANGINFO(ALT_DIGITS),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000458#endif
459#ifdef YESEXPR
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000460 LANGINFO(YESEXPR),
Martin v. Löwis2ea2c9d2002-04-19 21:04:41 +0000461#endif
462#ifdef NOEXPR
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000463 LANGINFO(NOEXPR),
464#endif
465#ifdef _DATE_FMT
466 /* This is not available in all glibc versions that have CODESET. */
467 LANGINFO(_DATE_FMT),
468#endif
469 {0, 0}
470};
471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000472PyDoc_STRVAR(nl_langinfo__doc__,
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000473"nl_langinfo(key) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000474"Return the value for the locale information associated with key.");
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000475
476static PyObject*
477PyLocale_nl_langinfo(PyObject* self, PyObject* args)
478{
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000479 int item, i;
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000480 if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))
481 return NULL;
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000482 /* Check whether this is a supported constant. GNU libc sometimes
483 returns numeric values in the char* return value, which would
Neal Norwitz7f9d29c2007-08-26 07:21:45 +0000484 crash PyUnicode_FromString. */
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000485 for (i = 0; langinfo_constants[i].name; i++)
Hye-Shik Changc3a87b82004-03-21 19:34:30 +0000486 if (langinfo_constants[i].value == item) {
487 /* Check NULL as a workaround for GNU libc's returning NULL
488 instead of an empty string for nl_langinfo(ERA). */
489 const char *result = nl_langinfo(item);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +0000490 result = result != NULL ? result : "";
Martin v. Löwis92fab752008-03-08 10:40:41 +0000491 return str2uni(result);
Hye-Shik Changc3a87b82004-03-21 19:34:30 +0000492 }
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000493 PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");
494 return NULL;
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000495}
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000496#endif /* HAVE_LANGINFO_H */
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000497
498#ifdef HAVE_LIBINTL_H
499
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000500PyDoc_STRVAR(gettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000501"gettext(msg) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000502"Return translation of msg.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000503
504static PyObject*
505PyIntl_gettext(PyObject* self, PyObject *args)
506{
507 char *in;
Georg Brandl3dbca812008-07-23 16:10:53 +0000508 if (!PyArg_ParseTuple(args, "s", &in))
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000509 return 0;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000510 return str2uni(gettext(in));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000511}
512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000513PyDoc_STRVAR(dgettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000514"dgettext(domain, msg) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000515"Return translation of msg in domain.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000516
517static PyObject*
518PyIntl_dgettext(PyObject* self, PyObject *args)
519{
520 char *domain, *in;
Georg Brandl3dbca812008-07-23 16:10:53 +0000521 if (!PyArg_ParseTuple(args, "zs", &domain, &in))
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000522 return 0;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000523 return str2uni(dgettext(domain, in));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000524}
525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000526PyDoc_STRVAR(dcgettext__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000527"dcgettext(domain, msg, category) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000528"Return translation of msg in domain and category.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000529
530static PyObject*
531PyIntl_dcgettext(PyObject *self, PyObject *args)
532{
533 char *domain, *msgid;
534 int category;
Georg Brandl3dbca812008-07-23 16:10:53 +0000535 if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000536 return 0;
Martin v. Löwis92fab752008-03-08 10:40:41 +0000537 return str2uni(dcgettext(domain,msgid,category));
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000538}
539
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000540PyDoc_STRVAR(textdomain__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000541"textdomain(domain) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000542"Set the C library's textdmain to domain, returning the new domain.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000543
544static PyObject*
545PyIntl_textdomain(PyObject* self, PyObject* args)
546{
547 char *domain;
548 if (!PyArg_ParseTuple(args, "z", &domain))
549 return 0;
550 domain = textdomain(domain);
551 if (!domain) {
552 PyErr_SetFromErrno(PyExc_OSError);
553 return NULL;
554 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000555 return str2uni(domain);
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000556}
557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000558PyDoc_STRVAR(bindtextdomain__doc__,
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000559"bindtextdomain(domain, dir) -> string\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000560"Bind the C library's domain to dir.");
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000561
562static PyObject*
563PyIntl_bindtextdomain(PyObject* self,PyObject*args)
564{
Georg Brandl3dbca812008-07-23 16:10:53 +0000565 char *domain, *dirname;
566 if (!PyArg_ParseTuple(args, "sz", &domain, &dirname))
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000567 return 0;
Georg Brandl3dbca812008-07-23 16:10:53 +0000568 if (!strlen(domain)) {
569 PyErr_SetString(Error, "domain must be a non-empty string");
570 return 0;
571 }
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000572 dirname = bindtextdomain(domain, dirname);
573 if (!dirname) {
574 PyErr_SetFromErrno(PyExc_OSError);
575 return NULL;
576 }
Martin v. Löwis92fab752008-03-08 10:40:41 +0000577 return str2uni(dirname);
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000578}
579
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000580#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
581PyDoc_STRVAR(bind_textdomain_codeset__doc__,
582"bind_textdomain_codeset(domain, codeset) -> string\n"
583"Bind the C library's domain to codeset.");
584
585static PyObject*
586PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)
587{
588 char *domain,*codeset;
589 if (!PyArg_ParseTuple(args, "sz", &domain, &codeset))
590 return NULL;
591 codeset = bind_textdomain_codeset(domain, codeset);
592 if (codeset)
Martin v. Löwis92fab752008-03-08 10:40:41 +0000593 return str2uni(codeset);
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000594 Py_RETURN_NONE;
595}
596#endif
597
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000598#endif
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000599
Guido van Rossum220ecc81997-11-18 21:03:39 +0000600static struct PyMethodDef PyLocale_Methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000601 {"setlocale", (PyCFunction) PyLocale_setlocale,
602 METH_VARARGS, setlocale__doc__},
603 {"localeconv", (PyCFunction) PyLocale_localeconv,
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000604 METH_NOARGS, localeconv__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000605#ifdef HAVE_WCSCOLL
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000606 {"strcoll", (PyCFunction) PyLocale_strcoll,
607 METH_VARARGS, strcoll__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000608#endif
609#ifdef HAVE_WCSXFRM
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000610 {"strxfrm", (PyCFunction) PyLocale_strxfrm,
611 METH_VARARGS, strxfrm__doc__},
Martin v. Löwis92fab752008-03-08 10:40:41 +0000612#endif
Ronald Oussorenfe8a3d62009-06-07 15:29:46 +0000613#if defined(MS_WINDOWS)
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000614 {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS},
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000615#endif
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000616#ifdef HAVE_LANGINFO_H
617 {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,
618 METH_VARARGS, nl_langinfo__doc__},
619#endif
Martin v. Löwisc6a7d7e2002-05-02 12:16:29 +0000620#ifdef HAVE_LIBINTL_H
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000621 {"gettext",(PyCFunction)PyIntl_gettext,METH_VARARGS,
622 gettext__doc__},
623 {"dgettext",(PyCFunction)PyIntl_dgettext,METH_VARARGS,
624 dgettext__doc__},
625 {"dcgettext",(PyCFunction)PyIntl_dcgettext,METH_VARARGS,
626 dcgettext__doc__},
627 {"textdomain",(PyCFunction)PyIntl_textdomain,METH_VARARGS,
628 textdomain__doc__},
629 {"bindtextdomain",(PyCFunction)PyIntl_bindtextdomain,METH_VARARGS,
630 bindtextdomain__doc__},
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000631#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
632 {"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset,
633 METH_VARARGS, bind_textdomain_codeset__doc__},
634#endif
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000635#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000636 {NULL, NULL}
637};
638
Martin v. Löwis1a214512008-06-11 05:26:20 +0000639
640static struct PyModuleDef _localemodule = {
641 PyModuleDef_HEAD_INIT,
642 "_locale",
643 locale__doc__,
644 -1,
645 PyLocale_Methods,
646 NULL,
647 NULL,
648 NULL,
649 NULL
650};
651
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000652PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000653PyInit__locale(void)
Guido van Rossum220ecc81997-11-18 21:03:39 +0000654{
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000655 PyObject *m, *d, *x;
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000656#ifdef HAVE_LANGINFO_H
657 int i;
658#endif
Guido van Rossum220ecc81997-11-18 21:03:39 +0000659
Martin v. Löwis1a214512008-06-11 05:26:20 +0000660 m = PyModule_Create(&_localemodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000661 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000662 return NULL;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000663
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000664 d = PyModule_GetDict(m);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000665
Christian Heimes217cfd12007-12-02 14:31:20 +0000666 x = PyLong_FromLong(LC_CTYPE);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000667 PyDict_SetItemString(d, "LC_CTYPE", x);
668 Py_XDECREF(x);
669
Christian Heimes217cfd12007-12-02 14:31:20 +0000670 x = PyLong_FromLong(LC_TIME);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000671 PyDict_SetItemString(d, "LC_TIME", x);
672 Py_XDECREF(x);
673
Christian Heimes217cfd12007-12-02 14:31:20 +0000674 x = PyLong_FromLong(LC_COLLATE);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000675 PyDict_SetItemString(d, "LC_COLLATE", x);
676 Py_XDECREF(x);
677
Christian Heimes217cfd12007-12-02 14:31:20 +0000678 x = PyLong_FromLong(LC_MONETARY);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000679 PyDict_SetItemString(d, "LC_MONETARY", x);
680 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000681
Guido van Rossum8d9c2e31997-12-09 19:35:11 +0000682#ifdef LC_MESSAGES
Christian Heimes217cfd12007-12-02 14:31:20 +0000683 x = PyLong_FromLong(LC_MESSAGES);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000684 PyDict_SetItemString(d, "LC_MESSAGES", x);
685 Py_XDECREF(x);
Guido van Rossum8d9c2e31997-12-09 19:35:11 +0000686#endif /* LC_MESSAGES */
Guido van Rossum220ecc81997-11-18 21:03:39 +0000687
Christian Heimes217cfd12007-12-02 14:31:20 +0000688 x = PyLong_FromLong(LC_NUMERIC);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000689 PyDict_SetItemString(d, "LC_NUMERIC", x);
690 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000691
Christian Heimes217cfd12007-12-02 14:31:20 +0000692 x = PyLong_FromLong(LC_ALL);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000693 PyDict_SetItemString(d, "LC_ALL", x);
694 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000695
Christian Heimes217cfd12007-12-02 14:31:20 +0000696 x = PyLong_FromLong(CHAR_MAX);
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000697 PyDict_SetItemString(d, "CHAR_MAX", x);
698 Py_XDECREF(x);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000699
Fredrik Lundh8f017a02000-07-08 19:57:37 +0000700 Error = PyErr_NewException("locale.Error", NULL, NULL);
701 PyDict_SetItemString(d, "Error", Error);
Guido van Rossum220ecc81997-11-18 21:03:39 +0000702
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000703#ifdef HAVE_LANGINFO_H
Martin v. Löwisdc0b61d2002-03-12 22:05:02 +0000704 for (i = 0; langinfo_constants[i].name; i++) {
705 PyModule_AddIntConstant(m, langinfo_constants[i].name,
706 langinfo_constants[i].value);
707 }
Martin v. Löwisf95dd0a2001-08-15 17:14:33 +0000708#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +0000709 return m;
Guido van Rossum220ecc81997-11-18 21:03:39 +0000710}
Martin v. Löwis9c36c292002-12-21 18:34:06 +0000711
712/*
713Local variables:
714c-basic-offset: 4
715indent-tabs-mode: nil
716End:
717*/