Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 2 | Copyright (C) 1997, 2002, 2003, 2007, 2008 Martin von Loewis |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 3 | |
| 4 | Permission to use, copy, modify, and distribute this software and its |
| 5 | documentation for any purpose and without fee is hereby granted, |
| 6 | provided that the above copyright notice appear in all copies. |
| 7 | |
| 8 | This software comes with no warranty. Use at your own risk. |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 10 | ******************************************************************/ |
| 11 | |
Antoine Pitrou | 6a448d4 | 2009-10-19 19:43:09 +0000 | [diff] [blame] | 12 | #define PY_SSIZE_T_CLEAN |
Fred Drake | 68933b9 | 2000-08-10 21:41:08 +0000 | [diff] [blame] | 13 | #include "Python.h" |
| 14 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 16 | #include <locale.h> |
| 17 | #include <string.h> |
Guido van Rossum | 5cd70f4 | 1998-06-19 04:33:30 +0000 | [diff] [blame] | 18 | #include <ctype.h> |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 19 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | #ifdef HAVE_ERRNO_H |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #endif |
| 23 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 24 | #ifdef HAVE_LANGINFO_H |
| 25 | #include <langinfo.h> |
| 26 | #endif |
| 27 | |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 28 | #ifdef HAVE_LIBINTL_H |
| 29 | #include <libintl.h> |
| 30 | #endif |
| 31 | |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 32 | #ifdef HAVE_WCHAR_H |
| 33 | #include <wchar.h> |
| 34 | #endif |
| 35 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 36 | #if defined(MS_WINDOWS) |
Tim Peters | 7a1f917 | 2002-07-14 22:14:19 +0000 | [diff] [blame] | 37 | #define WIN32_LEAN_AND_MEAN |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 38 | #include <windows.h> |
Guido van Rossum | 239a218 | 1998-04-28 16:08:19 +0000 | [diff] [blame] | 39 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 40 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | PyDoc_STRVAR(locale__doc__, "Support for POSIX locales."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 42 | |
| 43 | static PyObject *Error; |
| 44 | |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 45 | /* Convert a char* to a Unicode object according to the current locale */ |
| 46 | static PyObject* |
| 47 | str2uni(const char* s) |
| 48 | { |
Antoine Pitrou | fff9530 | 2008-09-03 18:58:51 +0000 | [diff] [blame] | 49 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 50 | size_t needed = strlen(s); |
| 51 | #else |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 52 | size_t needed = mbstowcs(NULL, s, 0); |
Antoine Pitrou | fff9530 | 2008-09-03 18:58:51 +0000 | [diff] [blame] | 53 | #endif |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 54 | 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öwis | 5bacec1 | 2008-03-08 13:39:58 +0000 | [diff] [blame] | 62 | if (needed*sizeof(wchar_t) < sizeof(smallbuf)) |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 63 | dest = smallbuf; |
| 64 | else { |
Martin v. Löwis | a69e1ef | 2008-03-08 10:54:31 +0000 | [diff] [blame] | 65 | dest = PyMem_Malloc((needed+1)*sizeof(wchar_t)); |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 66 | if (!dest) |
| 67 | return PyErr_NoMemory(); |
| 68 | } |
| 69 | /* This shouldn't fail now */ |
| 70 | res1 = mbstowcs(dest, s, needed+1); |
Antoine Pitrou | fff9530 | 2008-09-03 18:58:51 +0000 | [diff] [blame] | 71 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 72 | assert(res1 != (size_t)-1); |
| 73 | #else |
Martin v. Löwis | ce7a112 | 2008-03-08 10:59:49 +0000 | [diff] [blame] | 74 | assert(res1 == needed); |
Antoine Pitrou | fff9530 | 2008-09-03 18:58:51 +0000 | [diff] [blame] | 75 | #endif |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 76 | res2 = PyUnicode_FromWideChar(dest, res1); |
| 77 | if (dest != smallbuf) |
| 78 | PyMem_Free(dest); |
| 79 | return res2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 82 | /* support functions for formatting floating point numbers */ |
| 83 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 84 | PyDoc_STRVAR(setlocale__doc__, |
| 85 | "(integer,string=None) -> string. Activates/queries locale processing."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 87 | /* the grouping is terminated by either 0 or CHAR_MAX */ |
| 88 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 89 | copy_grouping(char* s) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 90 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 91 | 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 Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 108 | val = PyLong_FromLong(s[i]); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 109 | if (!val) |
| 110 | break; |
| 111 | if (PyList_SetItem(result, i, val)) { |
| 112 | Py_DECREF(val); |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 113 | val = NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | } while (s[i] != '\0' && s[i] != CHAR_MAX); |
| 117 | |
| 118 | if (!val) { |
| 119 | Py_DECREF(result); |
| 120 | return NULL; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 121 | } |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 122 | |
| 123 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 126 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 127 | PyLocale_setlocale(PyObject* self, PyObject* args) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 128 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 129 | int category; |
| 130 | char *locale = NULL, *result; |
| 131 | PyObject *result_object; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 132 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 133 | if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale)) |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 134 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 135 | |
Amaury Forgeot d'Arc | 64f3ca4 | 2009-12-01 21:59:18 +0000 | [diff] [blame] | 136 | #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 Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 144 | if (locale) { |
| 145 | /* set locale */ |
| 146 | result = setlocale(category, locale); |
| 147 | if (!result) { |
| 148 | /* operation failed, no setting was changed */ |
Martin v. Löwis | 25f90d5 | 2003-09-03 04:50:13 +0000 | [diff] [blame] | 149 | PyErr_SetString(Error, "unsupported locale setting"); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 152 | result_object = str2uni(result); |
Mark Hammond | 9a71475 | 2003-07-24 14:15:07 +0000 | [diff] [blame] | 153 | if (!result_object) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 154 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 155 | } else { |
| 156 | /* get locale */ |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 157 | result = setlocale(category, NULL); |
| 158 | if (!result) { |
| 159 | PyErr_SetString(Error, "locale query failed"); |
| 160 | return NULL; |
| 161 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 162 | result_object = str2uni(result); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 163 | } |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 164 | return result_object; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 167 | PyDoc_STRVAR(localeconv__doc__, |
| 168 | "() -> dict. Returns numeric and monetary locale-specific parameters."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 169 | |
| 170 | static PyObject* |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 171 | PyLocale_localeconv(PyObject* self) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 172 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 173 | PyObject* result; |
| 174 | struct lconv *l; |
| 175 | PyObject *x; |
| 176 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 177 | result = PyDict_New(); |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 178 | if (!result) |
| 179 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 180 | |
| 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öwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 188 | x = str2uni(l->s); \ |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 189 | if (!x) goto failed;\ |
| 190 | PyDict_SetItemString(result, #s, x);\ |
| 191 | Py_XDECREF(x) |
| 192 | |
| 193 | #define RESULT_INT(i)\ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 194 | x = PyLong_FromLong(l->i);\ |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 195 | if (!x) goto failed;\ |
| 196 | PyDict_SetItemString(result, #i, x);\ |
| 197 | Py_XDECREF(x) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 198 | |
| 199 | /* Numeric information */ |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 200 | 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 Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 207 | |
| 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 Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 217 | Py_XDECREF(x); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 218 | 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 Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 229 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 230 | failed: |
| 231 | Py_XDECREF(result); |
| 232 | Py_XDECREF(x); |
| 233 | return NULL; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 236 | #if defined(HAVE_WCSCOLL) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 237 | PyDoc_STRVAR(strcoll__doc__, |
| 238 | "string,string -> int. Compares two strings according to the locale."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 239 | |
| 240 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 241 | PyLocale_strcoll(PyObject* self, PyObject* args) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 242 | { |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 243 | PyObject *os1, *os2, *result = NULL; |
| 244 | wchar_t *ws1 = NULL, *ws2 = NULL; |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 245 | Py_ssize_t len1, len2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 247 | if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2)) |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 248 | return NULL; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 249 | /* Convert the unicode strings to wchar[]. */ |
| 250 | len1 = PyUnicode_GET_SIZE(os1) + 1; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 251 | 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é Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 258 | ws1[len1 - 1] = 0; |
| 259 | len2 = PyUnicode_GET_SIZE(os2) + 1; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 260 | 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é Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 267 | ws2[len2 - 1] = 0; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 268 | /* Collate the strings. */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 269 | result = PyLong_FromLong(wcscoll(ws1, ws2)); |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 270 | done: |
| 271 | /* Deallocate everything. */ |
| 272 | if (ws1) PyMem_FREE(ws1); |
| 273 | if (ws2) PyMem_FREE(ws2); |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 274 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 275 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 276 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 277 | |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 278 | #ifdef HAVE_WCSXFRM |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 279 | PyDoc_STRVAR(strxfrm__doc__, |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 280 | "strxfrm(string) -> string.\n\ |
| 281 | \n\ |
| 282 | Return a string that can be used as a key for locale-aware comparisons."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 283 | |
| 284 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 285 | PyLocale_strxfrm(PyObject* self, PyObject* args) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 286 | { |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 287 | Py_UNICODE *s0; |
| 288 | Py_ssize_t n0; |
| 289 | wchar_t *s, *buf = NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 290 | size_t n1, n2; |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 291 | PyObject *result = NULL; |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame^] | 292 | #if Py_UNICODE_SIZE != SIZEOF_WCHAR_T |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 293 | Py_ssize_t i; |
Georg Brandl | 9137391 | 2009-10-27 21:37:48 +0000 | [diff] [blame] | 294 | #endif |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 295 | |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 296 | if (!PyArg_ParseTuple(args, "u#:strxfrm", &s0, &n0)) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 297 | return NULL; |
| 298 | |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame^] | 299 | #if Py_UNICODE_SIZE == SIZEOF_WCHAR_T |
| 300 | s = (wchar_t *) s0; |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 301 | #else |
Martin v. Löwis | a69e1ef | 2008-03-08 10:54:31 +0000 | [diff] [blame] | 302 | s = PyMem_Malloc((n0+1)*sizeof(wchar_t)); |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 303 | if (!s) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 304 | return PyErr_NoMemory(); |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 305 | 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öwis | a69e1ef | 2008-03-08 10:54:31 +0000 | [diff] [blame] | 311 | buf = PyMem_Malloc(n1*sizeof(wchar_t)); |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 312 | if (!buf) { |
| 313 | PyErr_NoMemory(); |
| 314 | goto exit; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 315 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 316 | n2 = wcsxfrm(buf, s, n1); |
| 317 | if (n2 >= n1) { |
| 318 | /* more space needed */ |
Martin v. Löwis | a69e1ef | 2008-03-08 10:54:31 +0000 | [diff] [blame] | 319 | buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t)); |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 320 | if (!buf) { |
| 321 | PyErr_NoMemory(); |
| 322 | goto exit; |
| 323 | } |
Martin v. Löwis | db1c399 | 2009-05-23 10:38:26 +0000 | [diff] [blame] | 324 | n2 = wcsxfrm(buf, s, n2+1); |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 325 | } |
| 326 | result = PyUnicode_FromWideChar(buf, n2); |
| 327 | exit: |
| 328 | if (buf) PyMem_Free(buf); |
Daniel Stutzbach | 8515eae | 2010-08-24 21:57:33 +0000 | [diff] [blame^] | 329 | #if Py_UNICODE_SIZE != SIZEOF_WCHAR_T |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 330 | PyMem_Free(s); |
| 331 | #endif |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 332 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 333 | } |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 334 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 335 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 336 | #if defined(MS_WINDOWS) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 337 | static PyObject* |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 338 | PyLocale_getdefaultlocale(PyObject* self) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 339 | { |
| 340 | char encoding[100]; |
| 341 | char locale[100]; |
| 342 | |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 343 | PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP()); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 344 | |
| 345 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
| 346 | LOCALE_SISO639LANGNAME, |
| 347 | locale, sizeof(locale))) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 348 | Py_ssize_t i = strlen(locale); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 349 | locale[i++] = '_'; |
| 350 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
| 351 | LOCALE_SISO3166CTRYNAME, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 352 | locale+i, (int)(sizeof(locale)-i))) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 353 | 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öwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 373 | #ifdef HAVE_LANGINFO_H |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 374 | #define LANGINFO(X) {#X, X} |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 375 | static struct langinfo_constant{ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | char* name; |
| 377 | int value; |
| 378 | } langinfo_constants[] = |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 379 | { |
| 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öwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 443 | /* 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öwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 448 | #ifdef CODESET |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 449 | LANGINFO(CODESET), |
Martin v. Löwis | 496f9e4 | 2002-03-27 12:15:57 +0000 | [diff] [blame] | 450 | #endif |
| 451 | #ifdef T_FMT_AMPM |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 452 | LANGINFO(T_FMT_AMPM), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 453 | #endif |
| 454 | #ifdef ERA |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 455 | LANGINFO(ERA), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 456 | #endif |
| 457 | #ifdef ERA_D_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 458 | LANGINFO(ERA_D_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 459 | #endif |
| 460 | #ifdef ERA_D_T_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 461 | LANGINFO(ERA_D_T_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 462 | #endif |
| 463 | #ifdef ERA_T_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 464 | LANGINFO(ERA_T_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 465 | #endif |
| 466 | #ifdef ALT_DIGITS |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 467 | LANGINFO(ALT_DIGITS), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 468 | #endif |
| 469 | #ifdef YESEXPR |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 470 | LANGINFO(YESEXPR), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 471 | #endif |
| 472 | #ifdef NOEXPR |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 473 | 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öwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 482 | PyDoc_STRVAR(nl_langinfo__doc__, |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 483 | "nl_langinfo(key) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 484 | "Return the value for the locale information associated with key."); |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 485 | |
| 486 | static PyObject* |
| 487 | PyLocale_nl_langinfo(PyObject* self, PyObject* args) |
| 488 | { |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 489 | int item, i; |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 490 | if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item)) |
| 491 | return NULL; |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 492 | /* Check whether this is a supported constant. GNU libc sometimes |
| 493 | returns numeric values in the char* return value, which would |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 494 | crash PyUnicode_FromString. */ |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 495 | for (i = 0; langinfo_constants[i].name; i++) |
Hye-Shik Chang | c3a87b8 | 2004-03-21 19:34:30 +0000 | [diff] [blame] | 496 | 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 Norwitz | 3d7a90d | 2007-10-27 05:40:06 +0000 | [diff] [blame] | 500 | result = result != NULL ? result : ""; |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 501 | return str2uni(result); |
Hye-Shik Chang | c3a87b8 | 2004-03-21 19:34:30 +0000 | [diff] [blame] | 502 | } |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 503 | PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant"); |
| 504 | return NULL; |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 505 | } |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 506 | #endif /* HAVE_LANGINFO_H */ |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 507 | |
| 508 | #ifdef HAVE_LIBINTL_H |
| 509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 510 | PyDoc_STRVAR(gettext__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 511 | "gettext(msg) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 512 | "Return translation of msg."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 513 | |
| 514 | static PyObject* |
| 515 | PyIntl_gettext(PyObject* self, PyObject *args) |
| 516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | char *in; |
| 518 | if (!PyArg_ParseTuple(args, "s", &in)) |
| 519 | return 0; |
| 520 | return str2uni(gettext(in)); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 523 | PyDoc_STRVAR(dgettext__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 524 | "dgettext(domain, msg) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 525 | "Return translation of msg in domain."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 526 | |
| 527 | static PyObject* |
| 528 | PyIntl_dgettext(PyObject* self, PyObject *args) |
| 529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | char *domain, *in; |
| 531 | if (!PyArg_ParseTuple(args, "zs", &domain, &in)) |
| 532 | return 0; |
| 533 | return str2uni(dgettext(domain, in)); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 536 | PyDoc_STRVAR(dcgettext__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 537 | "dcgettext(domain, msg, category) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 538 | "Return translation of msg in domain and category."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 539 | |
| 540 | static PyObject* |
| 541 | PyIntl_dcgettext(PyObject *self, PyObject *args) |
| 542 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | 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öwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 550 | PyDoc_STRVAR(textdomain__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 551 | "textdomain(domain) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 552 | "Set the C library's textdmain to domain, returning the new domain."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 553 | |
| 554 | static PyObject* |
| 555 | PyIntl_textdomain(PyObject* self, PyObject* args) |
| 556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | 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öwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 568 | PyDoc_STRVAR(bindtextdomain__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 569 | "bindtextdomain(domain, dir) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 570 | "Bind the C library's domain to dir."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 571 | |
| 572 | static PyObject* |
| 573 | PyIntl_bindtextdomain(PyObject* self,PyObject*args) |
| 574 | { |
Victor Stinner | 9e19ca4 | 2010-06-11 22:09:51 +0000 | [diff] [blame] | 575 | char *domain, *dirname, *current_dirname; |
| 576 | PyObject *dirname_obj, *dirname_bytes = NULL, *result; |
| 577 | if (!PyArg_ParseTuple(args, "sO", &domain, &dirname_obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | return 0; |
| 579 | if (!strlen(domain)) { |
| 580 | PyErr_SetString(Error, "domain must be a non-empty string"); |
| 581 | return 0; |
| 582 | } |
Victor Stinner | 9e19ca4 | 2010-06-11 22:09:51 +0000 | [diff] [blame] | 583 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | PyErr_SetFromErrno(PyExc_OSError); |
| 595 | return NULL; |
| 596 | } |
Victor Stinner | 9e19ca4 | 2010-06-11 22:09:51 +0000 | [diff] [blame] | 597 | result = str2uni(current_dirname); |
| 598 | Py_XDECREF(dirname_bytes); |
| 599 | return result; |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 602 | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
| 603 | PyDoc_STRVAR(bind_textdomain_codeset__doc__, |
| 604 | "bind_textdomain_codeset(domain, codeset) -> string\n" |
| 605 | "Bind the C library's domain to codeset."); |
| 606 | |
| 607 | static PyObject* |
| 608 | PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args) |
| 609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | 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 Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 617 | } |
| 618 | #endif |
| 619 | |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 620 | #endif |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 621 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 622 | static struct PyMethodDef PyLocale_Methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | {"setlocale", (PyCFunction) PyLocale_setlocale, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 624 | METH_VARARGS, setlocale__doc__}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | {"localeconv", (PyCFunction) PyLocale_localeconv, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 626 | METH_NOARGS, localeconv__doc__}, |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 627 | #ifdef HAVE_WCSCOLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | {"strcoll", (PyCFunction) PyLocale_strcoll, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 629 | METH_VARARGS, strcoll__doc__}, |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 630 | #endif |
| 631 | #ifdef HAVE_WCSXFRM |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | {"strxfrm", (PyCFunction) PyLocale_strxfrm, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 633 | METH_VARARGS, strxfrm__doc__}, |
Martin v. Löwis | 92fab75 | 2008-03-08 10:40:41 +0000 | [diff] [blame] | 634 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | #if defined(MS_WINDOWS) |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 636 | {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS}, |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 637 | #endif |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 638 | #ifdef HAVE_LANGINFO_H |
| 639 | {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo, |
| 640 | METH_VARARGS, nl_langinfo__doc__}, |
| 641 | #endif |
Martin v. Löwis | c6a7d7e | 2002-05-02 12:16:29 +0000 | [diff] [blame] | 642 | #ifdef HAVE_LIBINTL_H |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 643 | {"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 Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 653 | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
| 654 | {"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset, |
| 655 | METH_VARARGS, bind_textdomain_codeset__doc__}, |
| 656 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 658 | {NULL, NULL} |
| 659 | }; |
| 660 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 661 | |
| 662 | static struct PyModuleDef _localemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | PyModuleDef_HEAD_INIT, |
| 664 | "_locale", |
| 665 | locale__doc__, |
| 666 | -1, |
| 667 | PyLocale_Methods, |
| 668 | NULL, |
| 669 | NULL, |
| 670 | NULL, |
| 671 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 672 | }; |
| 673 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 674 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 675 | PyInit__locale(void) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 676 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 677 | PyObject *m, *d, *x; |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 678 | #ifdef HAVE_LANGINFO_H |
| 679 | int i; |
| 680 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 681 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 682 | m = PyModule_Create(&_localemodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 683 | if (m == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | return NULL; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 685 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 686 | d = PyModule_GetDict(m); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 687 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 688 | x = PyLong_FromLong(LC_CTYPE); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 689 | PyDict_SetItemString(d, "LC_CTYPE", x); |
| 690 | Py_XDECREF(x); |
| 691 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 692 | x = PyLong_FromLong(LC_TIME); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 693 | PyDict_SetItemString(d, "LC_TIME", x); |
| 694 | Py_XDECREF(x); |
| 695 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 696 | x = PyLong_FromLong(LC_COLLATE); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 697 | PyDict_SetItemString(d, "LC_COLLATE", x); |
| 698 | Py_XDECREF(x); |
| 699 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 700 | x = PyLong_FromLong(LC_MONETARY); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 701 | PyDict_SetItemString(d, "LC_MONETARY", x); |
| 702 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 703 | |
Guido van Rossum | 8d9c2e3 | 1997-12-09 19:35:11 +0000 | [diff] [blame] | 704 | #ifdef LC_MESSAGES |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 705 | x = PyLong_FromLong(LC_MESSAGES); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 706 | PyDict_SetItemString(d, "LC_MESSAGES", x); |
| 707 | Py_XDECREF(x); |
Guido van Rossum | 8d9c2e3 | 1997-12-09 19:35:11 +0000 | [diff] [blame] | 708 | #endif /* LC_MESSAGES */ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 709 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 710 | x = PyLong_FromLong(LC_NUMERIC); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 711 | PyDict_SetItemString(d, "LC_NUMERIC", x); |
| 712 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 713 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 714 | x = PyLong_FromLong(LC_ALL); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 715 | PyDict_SetItemString(d, "LC_ALL", x); |
| 716 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 717 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 718 | x = PyLong_FromLong(CHAR_MAX); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 719 | PyDict_SetItemString(d, "CHAR_MAX", x); |
| 720 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 721 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 722 | Error = PyErr_NewException("locale.Error", NULL, NULL); |
| 723 | PyDict_SetItemString(d, "Error", Error); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 724 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 725 | #ifdef HAVE_LANGINFO_H |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 726 | for (i = 0; langinfo_constants[i].name; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 727 | PyModule_AddIntConstant(m, langinfo_constants[i].name, |
| 728 | langinfo_constants[i].value); |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 729 | } |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 730 | #endif |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 731 | return m; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 732 | } |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | /* |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 735 | Local variables: |
| 736 | c-basic-offset: 4 |
| 737 | indent-tabs-mode: nil |
| 738 | End: |
| 739 | */ |