Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 2 | Copyright (C) 1997, 2002, 2003, 2007 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 | |
Fred Drake | 68933b9 | 2000-08-10 21:41:08 +0000 | [diff] [blame] | 12 | #include "Python.h" |
| 13 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 15 | #include <locale.h> |
| 16 | #include <string.h> |
Guido van Rossum | 5cd70f4 | 1998-06-19 04:33:30 +0000 | [diff] [blame] | 17 | #include <ctype.h> |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 18 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 19 | #ifdef HAVE_ERRNO_H |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #endif |
| 22 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 23 | #ifdef HAVE_LANGINFO_H |
| 24 | #include <langinfo.h> |
| 25 | #endif |
| 26 | |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 27 | #ifdef HAVE_LIBINTL_H |
| 28 | #include <libintl.h> |
| 29 | #endif |
| 30 | |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 31 | #ifdef HAVE_WCHAR_H |
| 32 | #include <wchar.h> |
| 33 | #endif |
| 34 | |
Jack Jansen | 7107c1a | 2003-11-20 13:31:00 +0000 | [diff] [blame] | 35 | #if defined(__APPLE__) |
Jack Jansen | 59f072a | 2004-07-15 13:31:39 +0000 | [diff] [blame] | 36 | #include <CoreFoundation/CoreFoundation.h> |
Jack Jansen | 7107c1a | 2003-11-20 13:31:00 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 39 | #if defined(MS_WINDOWS) |
Tim Peters | 7a1f917 | 2002-07-14 22:14:19 +0000 | [diff] [blame] | 40 | #define WIN32_LEAN_AND_MEAN |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 41 | #include <windows.h> |
Guido van Rossum | 239a218 | 1998-04-28 16:08:19 +0000 | [diff] [blame] | 42 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 43 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 44 | PyDoc_STRVAR(locale__doc__, "Support for POSIX locales."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 45 | |
| 46 | static PyObject *Error; |
| 47 | |
| 48 | /* support functions for formatting floating point numbers */ |
| 49 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 50 | PyDoc_STRVAR(setlocale__doc__, |
| 51 | "(integer,string=None) -> string. Activates/queries locale processing."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 53 | /* the grouping is terminated by either 0 or CHAR_MAX */ |
| 54 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 55 | copy_grouping(char* s) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 56 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 57 | int i; |
| 58 | PyObject *result, *val = NULL; |
| 59 | |
| 60 | if (s[0] == '\0') |
| 61 | /* empty string: no grouping at all */ |
| 62 | return PyList_New(0); |
| 63 | |
| 64 | for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++) |
| 65 | ; /* nothing */ |
| 66 | |
| 67 | result = PyList_New(i+1); |
| 68 | if (!result) |
| 69 | return NULL; |
| 70 | |
| 71 | i = -1; |
| 72 | do { |
| 73 | i++; |
| 74 | val = PyInt_FromLong(s[i]); |
| 75 | if (!val) |
| 76 | break; |
| 77 | if (PyList_SetItem(result, i, val)) { |
| 78 | Py_DECREF(val); |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 79 | val = NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | } while (s[i] != '\0' && s[i] != CHAR_MAX); |
| 83 | |
| 84 | if (!val) { |
| 85 | Py_DECREF(result); |
| 86 | return NULL; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 87 | } |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 88 | |
| 89 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 92 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 93 | PyLocale_setlocale(PyObject* self, PyObject* args) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 94 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 95 | int category; |
| 96 | char *locale = NULL, *result; |
| 97 | PyObject *result_object; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 98 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 99 | if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale)) |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 100 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 101 | |
| 102 | if (locale) { |
| 103 | /* set locale */ |
| 104 | result = setlocale(category, locale); |
| 105 | if (!result) { |
| 106 | /* operation failed, no setting was changed */ |
Martin v. Löwis | 25f90d5 | 2003-09-03 04:50:13 +0000 | [diff] [blame] | 107 | PyErr_SetString(Error, "unsupported locale setting"); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 110 | result_object = PyUnicode_FromString(result); |
Mark Hammond | 9a71475 | 2003-07-24 14:15:07 +0000 | [diff] [blame] | 111 | if (!result_object) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 112 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 113 | } else { |
| 114 | /* get locale */ |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 115 | result = setlocale(category, NULL); |
| 116 | if (!result) { |
| 117 | PyErr_SetString(Error, "locale query failed"); |
| 118 | return NULL; |
| 119 | } |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 120 | result_object = PyUnicode_FromString(result); |
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 | return result_object; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 125 | PyDoc_STRVAR(localeconv__doc__, |
| 126 | "() -> dict. Returns numeric and monetary locale-specific parameters."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 127 | |
| 128 | static PyObject* |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 129 | PyLocale_localeconv(PyObject* self) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 130 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 131 | PyObject* result; |
| 132 | struct lconv *l; |
| 133 | PyObject *x; |
| 134 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 135 | result = PyDict_New(); |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 136 | if (!result) |
| 137 | return NULL; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 138 | |
| 139 | /* if LC_NUMERIC is different in the C library, use saved value */ |
| 140 | l = localeconv(); |
| 141 | |
| 142 | /* hopefully, the localeconv result survives the C library calls |
| 143 | involved herein */ |
| 144 | |
| 145 | #define RESULT_STRING(s)\ |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 146 | x = PyUnicode_FromString(l->s);\ |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 147 | if (!x) goto failed;\ |
| 148 | PyDict_SetItemString(result, #s, x);\ |
| 149 | Py_XDECREF(x) |
| 150 | |
| 151 | #define RESULT_INT(i)\ |
| 152 | x = PyInt_FromLong(l->i);\ |
| 153 | if (!x) goto failed;\ |
| 154 | PyDict_SetItemString(result, #i, x);\ |
| 155 | Py_XDECREF(x) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 156 | |
| 157 | /* Numeric information */ |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 158 | RESULT_STRING(decimal_point); |
| 159 | RESULT_STRING(thousands_sep); |
| 160 | x = copy_grouping(l->grouping); |
| 161 | if (!x) |
| 162 | goto failed; |
| 163 | PyDict_SetItemString(result, "grouping", x); |
| 164 | Py_XDECREF(x); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 165 | |
| 166 | /* Monetary information */ |
| 167 | RESULT_STRING(int_curr_symbol); |
| 168 | RESULT_STRING(currency_symbol); |
| 169 | RESULT_STRING(mon_decimal_point); |
| 170 | RESULT_STRING(mon_thousands_sep); |
| 171 | x = copy_grouping(l->mon_grouping); |
| 172 | if (!x) |
| 173 | goto failed; |
| 174 | PyDict_SetItemString(result, "mon_grouping", x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 175 | Py_XDECREF(x); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 176 | RESULT_STRING(positive_sign); |
| 177 | RESULT_STRING(negative_sign); |
| 178 | RESULT_INT(int_frac_digits); |
| 179 | RESULT_INT(frac_digits); |
| 180 | RESULT_INT(p_cs_precedes); |
| 181 | RESULT_INT(p_sep_by_space); |
| 182 | RESULT_INT(n_cs_precedes); |
| 183 | RESULT_INT(n_sep_by_space); |
| 184 | RESULT_INT(p_sign_posn); |
| 185 | RESULT_INT(n_sign_posn); |
| 186 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 187 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 188 | failed: |
| 189 | Py_XDECREF(result); |
| 190 | Py_XDECREF(x); |
| 191 | return NULL; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 194 | PyDoc_STRVAR(strcoll__doc__, |
| 195 | "string,string -> int. Compares two strings according to the locale."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 196 | |
| 197 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 198 | PyLocale_strcoll(PyObject* self, PyObject* args) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 199 | { |
Guido van Rossum | 8d30cc0 | 2007-05-03 17:49:24 +0000 | [diff] [blame] | 200 | #if !defined(HAVE_WCSCOLL) |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 201 | char *s1,*s2; |
| 202 | |
| 203 | if (!PyArg_ParseTuple(args, "ss:strcoll", &s1, &s2)) |
| 204 | return NULL; |
| 205 | return PyInt_FromLong(strcoll(s1, s2)); |
| 206 | #else |
| 207 | PyObject *os1, *os2, *result = NULL; |
| 208 | wchar_t *ws1 = NULL, *ws2 = NULL; |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 209 | int len1, len2; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 210 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 211 | if (!PyArg_UnpackTuple(args, "strcoll", 2, 2, &os1, &os2)) |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 212 | return NULL; |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 213 | /* Both arguments must be unicode, or it's an error. */ |
| 214 | if (!PyUnicode_Check(os1) || !PyUnicode_Check(os2)) { |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 215 | PyErr_SetString(PyExc_ValueError, "strcoll arguments must be strings"); |
| 216 | } |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 217 | /* Convert the unicode strings to wchar[]. */ |
| 218 | len1 = PyUnicode_GET_SIZE(os1) + 1; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 219 | ws1 = PyMem_MALLOC(len1 * sizeof(wchar_t)); |
| 220 | if (!ws1) { |
| 221 | PyErr_NoMemory(); |
| 222 | goto done; |
| 223 | } |
| 224 | if (PyUnicode_AsWideChar((PyUnicodeObject*)os1, ws1, len1) == -1) |
| 225 | goto done; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 226 | ws1[len1 - 1] = 0; |
| 227 | len2 = PyUnicode_GET_SIZE(os2) + 1; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 228 | ws2 = PyMem_MALLOC(len2 * sizeof(wchar_t)); |
| 229 | if (!ws2) { |
| 230 | PyErr_NoMemory(); |
| 231 | goto done; |
| 232 | } |
| 233 | if (PyUnicode_AsWideChar((PyUnicodeObject*)os2, ws2, len2) == -1) |
| 234 | goto done; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 235 | ws2[len2 - 1] = 0; |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 236 | /* Collate the strings. */ |
| 237 | result = PyInt_FromLong(wcscoll(ws1, ws2)); |
| 238 | done: |
| 239 | /* Deallocate everything. */ |
| 240 | if (ws1) PyMem_FREE(ws1); |
| 241 | if (ws2) PyMem_FREE(ws2); |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 242 | return result; |
| 243 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 247 | PyDoc_STRVAR(strxfrm__doc__, |
| 248 | "string -> string. Returns a string that behaves for cmp locale-aware."); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 249 | |
| 250 | static PyObject* |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 251 | PyLocale_strxfrm(PyObject* self, PyObject* args) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 252 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 253 | char *s, *buf; |
| 254 | size_t n1, n2; |
| 255 | PyObject *result; |
| 256 | |
Fredrik Lundh | 89610a4 | 2000-07-08 20:07:24 +0000 | [diff] [blame] | 257 | if (!PyArg_ParseTuple(args, "s:strxfrm", &s)) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 258 | return NULL; |
| 259 | |
| 260 | /* assume no change in size, first */ |
| 261 | n1 = strlen(s) + 1; |
| 262 | buf = PyMem_Malloc(n1); |
| 263 | if (!buf) |
| 264 | return PyErr_NoMemory(); |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 265 | n2 = strxfrm(buf, s, n1) + 1; |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 266 | if (n2 > n1) { |
| 267 | /* more space needed */ |
| 268 | buf = PyMem_Realloc(buf, n2); |
| 269 | if (!buf) |
| 270 | return PyErr_NoMemory(); |
| 271 | strxfrm(buf, s, n2); |
| 272 | } |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 273 | result = PyUnicode_FromString(buf); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 274 | PyMem_Free(buf); |
| 275 | return result; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 278 | #if defined(MS_WINDOWS) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 279 | static PyObject* |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 280 | PyLocale_getdefaultlocale(PyObject* self) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 281 | { |
| 282 | char encoding[100]; |
| 283 | char locale[100]; |
| 284 | |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 285 | PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP()); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 286 | |
| 287 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
| 288 | LOCALE_SISO639LANGNAME, |
| 289 | locale, sizeof(locale))) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 290 | Py_ssize_t i = strlen(locale); |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 291 | locale[i++] = '_'; |
| 292 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, |
| 293 | LOCALE_SISO3166CTRYNAME, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 294 | locale+i, (int)(sizeof(locale)-i))) |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 295 | return Py_BuildValue("ss", locale, encoding); |
| 296 | } |
| 297 | |
| 298 | /* If we end up here, this windows version didn't know about |
| 299 | ISO639/ISO3166 names (it's probably Windows 95). Return the |
| 300 | Windows language identifier instead (a hexadecimal number) */ |
| 301 | |
| 302 | locale[0] = '0'; |
| 303 | locale[1] = 'x'; |
| 304 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, |
| 305 | locale+2, sizeof(locale)-2)) { |
| 306 | return Py_BuildValue("ss", locale, encoding); |
| 307 | } |
| 308 | |
| 309 | /* cannot determine the language code (very unlikely) */ |
| 310 | Py_INCREF(Py_None); |
| 311 | return Py_BuildValue("Os", Py_None, encoding); |
| 312 | } |
| 313 | #endif |
| 314 | |
Martin v. Löwis | 52ea7e9 | 2002-11-26 09:05:36 +0000 | [diff] [blame] | 315 | #if defined(__APPLE__) |
Jack Jansen | 59f072a | 2004-07-15 13:31:39 +0000 | [diff] [blame] | 316 | /* |
| 317 | ** Find out what the current script is. |
Brett Cannon | 269ab62 | 2004-08-27 05:00:22 +0000 | [diff] [blame] | 318 | ** Donated by Fredrik Lundh. |
Jack Jansen | 59f072a | 2004-07-15 13:31:39 +0000 | [diff] [blame] | 319 | */ |
| 320 | static char *mac_getscript(void) |
| 321 | { |
| 322 | CFStringEncoding enc = CFStringGetSystemEncoding(); |
| 323 | static CFStringRef name = NULL; |
| 324 | /* Return the code name for the encodings for which we have codecs. */ |
| 325 | switch(enc) { |
| 326 | case kCFStringEncodingMacRoman: return "mac-roman"; |
| 327 | case kCFStringEncodingMacGreek: return "mac-greek"; |
| 328 | case kCFStringEncodingMacCyrillic: return "mac-cyrillic"; |
| 329 | case kCFStringEncodingMacTurkish: return "mac-turkish"; |
| 330 | case kCFStringEncodingMacIcelandic: return "mac-icelandic"; |
| 331 | /* XXX which one is mac-latin2? */ |
| 332 | } |
| 333 | if (!name) { |
Brett Cannon | 5ce2587 | 2005-03-01 03:16:34 +0000 | [diff] [blame] | 334 | /* This leaks an object. */ |
Jack Jansen | 59f072a | 2004-07-15 13:31:39 +0000 | [diff] [blame] | 335 | name = CFStringConvertEncodingToIANACharSetName(enc); |
| 336 | } |
| 337 | return (char *)CFStringGetCStringPtr(name, 0); |
| 338 | } |
| 339 | |
Jack Jansen | 307d7a4 | 2000-07-15 22:31:45 +0000 | [diff] [blame] | 340 | static PyObject* |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 341 | PyLocale_getdefaultlocale(PyObject* self) |
Jack Jansen | 307d7a4 | 2000-07-15 22:31:45 +0000 | [diff] [blame] | 342 | { |
Jack Jansen | 59f072a | 2004-07-15 13:31:39 +0000 | [diff] [blame] | 343 | return Py_BuildValue("Os", Py_None, mac_getscript()); |
Jack Jansen | 307d7a4 | 2000-07-15 22:31:45 +0000 | [diff] [blame] | 344 | } |
| 345 | #endif |
| 346 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 347 | #ifdef HAVE_LANGINFO_H |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 348 | #define LANGINFO(X) {#X, X} |
| 349 | struct langinfo_constant{ |
| 350 | char* name; |
| 351 | int value; |
| 352 | } langinfo_constants[] = |
| 353 | { |
| 354 | /* These constants should exist on any langinfo implementation */ |
| 355 | LANGINFO(DAY_1), |
| 356 | LANGINFO(DAY_2), |
| 357 | LANGINFO(DAY_3), |
| 358 | LANGINFO(DAY_4), |
| 359 | LANGINFO(DAY_5), |
| 360 | LANGINFO(DAY_6), |
| 361 | LANGINFO(DAY_7), |
| 362 | |
| 363 | LANGINFO(ABDAY_1), |
| 364 | LANGINFO(ABDAY_2), |
| 365 | LANGINFO(ABDAY_3), |
| 366 | LANGINFO(ABDAY_4), |
| 367 | LANGINFO(ABDAY_5), |
| 368 | LANGINFO(ABDAY_6), |
| 369 | LANGINFO(ABDAY_7), |
| 370 | |
| 371 | LANGINFO(MON_1), |
| 372 | LANGINFO(MON_2), |
| 373 | LANGINFO(MON_3), |
| 374 | LANGINFO(MON_4), |
| 375 | LANGINFO(MON_5), |
| 376 | LANGINFO(MON_6), |
| 377 | LANGINFO(MON_7), |
| 378 | LANGINFO(MON_8), |
| 379 | LANGINFO(MON_9), |
| 380 | LANGINFO(MON_10), |
| 381 | LANGINFO(MON_11), |
| 382 | LANGINFO(MON_12), |
| 383 | |
| 384 | LANGINFO(ABMON_1), |
| 385 | LANGINFO(ABMON_2), |
| 386 | LANGINFO(ABMON_3), |
| 387 | LANGINFO(ABMON_4), |
| 388 | LANGINFO(ABMON_5), |
| 389 | LANGINFO(ABMON_6), |
| 390 | LANGINFO(ABMON_7), |
| 391 | LANGINFO(ABMON_8), |
| 392 | LANGINFO(ABMON_9), |
| 393 | LANGINFO(ABMON_10), |
| 394 | LANGINFO(ABMON_11), |
| 395 | LANGINFO(ABMON_12), |
| 396 | |
| 397 | #ifdef RADIXCHAR |
| 398 | /* The following are not available with glibc 2.0 */ |
| 399 | LANGINFO(RADIXCHAR), |
| 400 | LANGINFO(THOUSEP), |
| 401 | /* YESSTR and NOSTR are deprecated in glibc, since they are |
| 402 | a special case of message translation, which should be rather |
| 403 | done using gettext. So we don't expose it to Python in the |
| 404 | first place. |
| 405 | LANGINFO(YESSTR), |
| 406 | LANGINFO(NOSTR), |
| 407 | */ |
| 408 | LANGINFO(CRNCYSTR), |
| 409 | #endif |
| 410 | |
| 411 | LANGINFO(D_T_FMT), |
| 412 | LANGINFO(D_FMT), |
| 413 | LANGINFO(T_FMT), |
| 414 | LANGINFO(AM_STR), |
| 415 | LANGINFO(PM_STR), |
| 416 | |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 417 | /* The following constants are available only with XPG4, but... |
| 418 | AIX 3.2. only has CODESET. |
| 419 | OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have |
| 420 | a few of the others. |
| 421 | Solution: ifdef-test them all. */ |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 422 | #ifdef CODESET |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 423 | LANGINFO(CODESET), |
Martin v. Löwis | 496f9e4 | 2002-03-27 12:15:57 +0000 | [diff] [blame] | 424 | #endif |
| 425 | #ifdef T_FMT_AMPM |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 426 | LANGINFO(T_FMT_AMPM), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 427 | #endif |
| 428 | #ifdef ERA |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 429 | LANGINFO(ERA), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 430 | #endif |
| 431 | #ifdef ERA_D_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 432 | LANGINFO(ERA_D_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 433 | #endif |
| 434 | #ifdef ERA_D_T_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 435 | LANGINFO(ERA_D_T_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 436 | #endif |
| 437 | #ifdef ERA_T_FMT |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 438 | LANGINFO(ERA_T_FMT), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 439 | #endif |
| 440 | #ifdef ALT_DIGITS |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 441 | LANGINFO(ALT_DIGITS), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 442 | #endif |
| 443 | #ifdef YESEXPR |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 444 | LANGINFO(YESEXPR), |
Martin v. Löwis | 2ea2c9d | 2002-04-19 21:04:41 +0000 | [diff] [blame] | 445 | #endif |
| 446 | #ifdef NOEXPR |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 447 | LANGINFO(NOEXPR), |
| 448 | #endif |
| 449 | #ifdef _DATE_FMT |
| 450 | /* This is not available in all glibc versions that have CODESET. */ |
| 451 | LANGINFO(_DATE_FMT), |
| 452 | #endif |
| 453 | {0, 0} |
| 454 | }; |
| 455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 456 | PyDoc_STRVAR(nl_langinfo__doc__, |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 457 | "nl_langinfo(key) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 458 | "Return the value for the locale information associated with key."); |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 459 | |
| 460 | static PyObject* |
| 461 | PyLocale_nl_langinfo(PyObject* self, PyObject* args) |
| 462 | { |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 463 | int item, i; |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 464 | if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item)) |
| 465 | return NULL; |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 466 | /* Check whether this is a supported constant. GNU libc sometimes |
| 467 | returns numeric values in the char* return value, which would |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 468 | crash PyUnicode_FromString. */ |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 469 | for (i = 0; langinfo_constants[i].name; i++) |
Hye-Shik Chang | c3a87b8 | 2004-03-21 19:34:30 +0000 | [diff] [blame] | 470 | if (langinfo_constants[i].value == item) { |
| 471 | /* Check NULL as a workaround for GNU libc's returning NULL |
| 472 | instead of an empty string for nl_langinfo(ERA). */ |
| 473 | const char *result = nl_langinfo(item); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 474 | /* XXX may have to convert this to wcs first. */ |
| 475 | return PyUnicode_FromString(result != NULL ? result : ""); |
Hye-Shik Chang | c3a87b8 | 2004-03-21 19:34:30 +0000 | [diff] [blame] | 476 | } |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 477 | PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant"); |
| 478 | return NULL; |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 479 | } |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 480 | #endif /* HAVE_LANGINFO_H */ |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 481 | |
| 482 | #ifdef HAVE_LIBINTL_H |
| 483 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 484 | PyDoc_STRVAR(gettext__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 485 | "gettext(msg) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 486 | "Return translation of msg."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 487 | |
| 488 | static PyObject* |
| 489 | PyIntl_gettext(PyObject* self, PyObject *args) |
| 490 | { |
| 491 | char *in; |
| 492 | if (!PyArg_ParseTuple(args, "z", &in)) |
| 493 | return 0; |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 494 | return PyUnicode_FromString(gettext(in)); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 497 | PyDoc_STRVAR(dgettext__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 498 | "dgettext(domain, msg) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 499 | "Return translation of msg in domain."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 500 | |
| 501 | static PyObject* |
| 502 | PyIntl_dgettext(PyObject* self, PyObject *args) |
| 503 | { |
| 504 | char *domain, *in; |
| 505 | if (!PyArg_ParseTuple(args, "zz", &domain, &in)) |
| 506 | return 0; |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 507 | return PyUnicode_FromString(dgettext(domain, in)); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 510 | PyDoc_STRVAR(dcgettext__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 511 | "dcgettext(domain, msg, category) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 512 | "Return translation of msg in domain and category."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 513 | |
| 514 | static PyObject* |
| 515 | PyIntl_dcgettext(PyObject *self, PyObject *args) |
| 516 | { |
| 517 | char *domain, *msgid; |
| 518 | int category; |
| 519 | if (!PyArg_ParseTuple(args, "zzi", &domain, &msgid, &category)) |
| 520 | return 0; |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 521 | return PyUnicode_FromString(dcgettext(domain,msgid,category)); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 524 | PyDoc_STRVAR(textdomain__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 525 | "textdomain(domain) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 526 | "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] | 527 | |
| 528 | static PyObject* |
| 529 | PyIntl_textdomain(PyObject* self, PyObject* args) |
| 530 | { |
| 531 | char *domain; |
| 532 | if (!PyArg_ParseTuple(args, "z", &domain)) |
| 533 | return 0; |
| 534 | domain = textdomain(domain); |
| 535 | if (!domain) { |
| 536 | PyErr_SetFromErrno(PyExc_OSError); |
| 537 | return NULL; |
| 538 | } |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 539 | return PyUnicode_FromString(domain); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 542 | PyDoc_STRVAR(bindtextdomain__doc__, |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 543 | "bindtextdomain(domain, dir) -> string\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 544 | "Bind the C library's domain to dir."); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 545 | |
| 546 | static PyObject* |
| 547 | PyIntl_bindtextdomain(PyObject* self,PyObject*args) |
| 548 | { |
| 549 | char *domain,*dirname; |
| 550 | if (!PyArg_ParseTuple(args, "zz", &domain, &dirname)) |
| 551 | return 0; |
| 552 | dirname = bindtextdomain(domain, dirname); |
| 553 | if (!dirname) { |
| 554 | PyErr_SetFromErrno(PyExc_OSError); |
| 555 | return NULL; |
| 556 | } |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 557 | return PyUnicode_FromString(dirname); |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 560 | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
| 561 | PyDoc_STRVAR(bind_textdomain_codeset__doc__, |
| 562 | "bind_textdomain_codeset(domain, codeset) -> string\n" |
| 563 | "Bind the C library's domain to codeset."); |
| 564 | |
| 565 | static PyObject* |
| 566 | PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args) |
| 567 | { |
| 568 | char *domain,*codeset; |
| 569 | if (!PyArg_ParseTuple(args, "sz", &domain, &codeset)) |
| 570 | return NULL; |
| 571 | codeset = bind_textdomain_codeset(domain, codeset); |
| 572 | if (codeset) |
Neal Norwitz | 7f9d29c | 2007-08-26 07:21:45 +0000 | [diff] [blame] | 573 | return PyUnicode_FromString(codeset); |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 574 | Py_RETURN_NONE; |
| 575 | } |
| 576 | #endif |
| 577 | |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 578 | #endif |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 579 | |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 580 | static struct PyMethodDef PyLocale_Methods[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 581 | {"setlocale", (PyCFunction) PyLocale_setlocale, |
| 582 | METH_VARARGS, setlocale__doc__}, |
| 583 | {"localeconv", (PyCFunction) PyLocale_localeconv, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 584 | METH_NOARGS, localeconv__doc__}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 585 | {"strcoll", (PyCFunction) PyLocale_strcoll, |
| 586 | METH_VARARGS, strcoll__doc__}, |
| 587 | {"strxfrm", (PyCFunction) PyLocale_strxfrm, |
| 588 | METH_VARARGS, strxfrm__doc__}, |
Martin v. Löwis | 52ea7e9 | 2002-11-26 09:05:36 +0000 | [diff] [blame] | 589 | #if defined(MS_WINDOWS) || defined(__APPLE__) |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 590 | {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS}, |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 591 | #endif |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 592 | #ifdef HAVE_LANGINFO_H |
| 593 | {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo, |
| 594 | METH_VARARGS, nl_langinfo__doc__}, |
| 595 | #endif |
Martin v. Löwis | c6a7d7e | 2002-05-02 12:16:29 +0000 | [diff] [blame] | 596 | #ifdef HAVE_LIBINTL_H |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 597 | {"gettext",(PyCFunction)PyIntl_gettext,METH_VARARGS, |
| 598 | gettext__doc__}, |
| 599 | {"dgettext",(PyCFunction)PyIntl_dgettext,METH_VARARGS, |
| 600 | dgettext__doc__}, |
| 601 | {"dcgettext",(PyCFunction)PyIntl_dcgettext,METH_VARARGS, |
| 602 | dcgettext__doc__}, |
| 603 | {"textdomain",(PyCFunction)PyIntl_textdomain,METH_VARARGS, |
| 604 | textdomain__doc__}, |
| 605 | {"bindtextdomain",(PyCFunction)PyIntl_bindtextdomain,METH_VARARGS, |
| 606 | bindtextdomain__doc__}, |
Gustavo Niemeyer | 7bd33c5 | 2004-07-22 18:44:01 +0000 | [diff] [blame] | 607 | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET |
| 608 | {"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset, |
| 609 | METH_VARARGS, bind_textdomain_codeset__doc__}, |
| 610 | #endif |
Martin v. Löwis | 2e64c34 | 2002-03-27 18:49:02 +0000 | [diff] [blame] | 611 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 612 | {NULL, NULL} |
| 613 | }; |
| 614 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 615 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 616 | init_locale(void) |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 617 | { |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 618 | PyObject *m, *d, *x; |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 619 | #ifdef HAVE_LANGINFO_H |
| 620 | int i; |
| 621 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 622 | |
Neal Norwitz | f9e115a | 2007-08-23 18:08:11 +0000 | [diff] [blame] | 623 | m = Py_InitModule3("_locale", PyLocale_Methods, locale__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 624 | if (m == NULL) |
| 625 | return; |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 626 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 627 | d = PyModule_GetDict(m); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 628 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 629 | x = PyInt_FromLong(LC_CTYPE); |
| 630 | PyDict_SetItemString(d, "LC_CTYPE", x); |
| 631 | Py_XDECREF(x); |
| 632 | |
| 633 | x = PyInt_FromLong(LC_TIME); |
| 634 | PyDict_SetItemString(d, "LC_TIME", x); |
| 635 | Py_XDECREF(x); |
| 636 | |
| 637 | x = PyInt_FromLong(LC_COLLATE); |
| 638 | PyDict_SetItemString(d, "LC_COLLATE", x); |
| 639 | Py_XDECREF(x); |
| 640 | |
| 641 | x = PyInt_FromLong(LC_MONETARY); |
| 642 | PyDict_SetItemString(d, "LC_MONETARY", x); |
| 643 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 644 | |
Guido van Rossum | 8d9c2e3 | 1997-12-09 19:35:11 +0000 | [diff] [blame] | 645 | #ifdef LC_MESSAGES |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 646 | x = PyInt_FromLong(LC_MESSAGES); |
| 647 | PyDict_SetItemString(d, "LC_MESSAGES", x); |
| 648 | Py_XDECREF(x); |
Guido van Rossum | 8d9c2e3 | 1997-12-09 19:35:11 +0000 | [diff] [blame] | 649 | #endif /* LC_MESSAGES */ |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 650 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 651 | x = PyInt_FromLong(LC_NUMERIC); |
| 652 | PyDict_SetItemString(d, "LC_NUMERIC", x); |
| 653 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 654 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 655 | x = PyInt_FromLong(LC_ALL); |
| 656 | PyDict_SetItemString(d, "LC_ALL", x); |
| 657 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 658 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 659 | x = PyInt_FromLong(CHAR_MAX); |
| 660 | PyDict_SetItemString(d, "CHAR_MAX", x); |
| 661 | Py_XDECREF(x); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 662 | |
Fredrik Lundh | 8f017a0 | 2000-07-08 19:57:37 +0000 | [diff] [blame] | 663 | Error = PyErr_NewException("locale.Error", NULL, NULL); |
| 664 | PyDict_SetItemString(d, "Error", Error); |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 665 | |
Martin v. Löwis | 9b75dca | 2001-08-10 13:58:50 +0000 | [diff] [blame] | 666 | #ifdef HAVE_LANGINFO_H |
Martin v. Löwis | dc0b61d | 2002-03-12 22:05:02 +0000 | [diff] [blame] | 667 | for (i = 0; langinfo_constants[i].name; i++) { |
| 668 | PyModule_AddIntConstant(m, langinfo_constants[i].name, |
| 669 | langinfo_constants[i].value); |
| 670 | } |
Martin v. Löwis | f95dd0a | 2001-08-15 17:14:33 +0000 | [diff] [blame] | 671 | #endif |
Guido van Rossum | 220ecc8 | 1997-11-18 21:03:39 +0000 | [diff] [blame] | 672 | } |
Martin v. Löwis | 9c36c29 | 2002-12-21 18:34:06 +0000 | [diff] [blame] | 673 | |
| 674 | /* |
| 675 | Local variables: |
| 676 | c-basic-offset: 4 |
| 677 | indent-tabs-mode: nil |
| 678 | End: |
| 679 | */ |