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