Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Module support implementation */ |
| 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 6 | #define FLAG_SIZE_T 1 |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 7 | typedef double va_double; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 8 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 9 | static PyObject *va_build_value(const char *, va_list, int); |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 11 | /* Package context -- the full module name for package imports */ |
| 12 | char *_Py_PackageContext = NULL; |
| 13 | |
Guido van Rossum | 40b33c6 | 1997-08-02 03:07:46 +0000 | [diff] [blame] | 14 | /* Py_InitModule4() parameters: |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 15 | - name is the module name |
| 16 | - methods is the list of top-level functions |
| 17 | - doc is the documentation string |
| 18 | - passthrough is passed as self to functions defined in the module |
| 19 | - api_version is the value of PYTHON_API_VERSION at the time the |
| 20 | module was compiled |
Guido van Rossum | 40b33c6 | 1997-08-02 03:07:46 +0000 | [diff] [blame] | 21 | |
| 22 | Return value is a borrowed reference to the module object; or NULL |
| 23 | if an error occurred (in Python 1.4 and before, errors were fatal). |
| 24 | Errors may still leak memory. |
Guido van Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 25 | */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 27 | static char api_version_warning[] = |
Marc-André Lemburg | e5006eb | 2001-07-31 13:24:44 +0000 | [diff] [blame] | 28 | "Python C API version mismatch for module %.100s:\ |
| 29 | This Python has API version %d, module %.100s has version %d."; |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 31 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 32 | Py_InitModule4(const char *name, PyMethodDef *methods, const char *doc, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 33 | PyObject *passthrough, int module_api_version) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 35 | PyObject *m, *d, *v, *n; |
| 36 | PyMethodDef *ml; |
R. David Murray | 64a1e7c | 2010-12-15 01:36:03 +0000 | [diff] [blame] | 37 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 38 | if (interp->modules == NULL) |
| 39 | Py_FatalError("Python import machinery not initialized"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 40 | if (module_api_version != PYTHON_API_VERSION) { |
| 41 | char message[512]; |
| 42 | PyOS_snprintf(message, sizeof(message), |
| 43 | api_version_warning, name, |
| 44 | PYTHON_API_VERSION, name, |
| 45 | module_api_version); |
| 46 | if (PyErr_Warn(PyExc_RuntimeWarning, message)) |
| 47 | return NULL; |
| 48 | } |
| 49 | /* Make sure name is fully qualified. |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 50 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 51 | This is a bit of a hack: when the shared library is loaded, |
| 52 | the module name is "package.module", but the module calls |
| 53 | Py_InitModule*() with just "module" for the name. The shared |
| 54 | library loader squirrels away the true name of the module in |
| 55 | _Py_PackageContext, and Py_InitModule*() will substitute this |
| 56 | (if the name actually matches). |
| 57 | */ |
| 58 | if (_Py_PackageContext != NULL) { |
| 59 | char *p = strrchr(_Py_PackageContext, '.'); |
| 60 | if (p != NULL && strcmp(name, p+1) == 0) { |
| 61 | name = _Py_PackageContext; |
| 62 | _Py_PackageContext = NULL; |
| 63 | } |
| 64 | } |
| 65 | if ((m = PyImport_AddModule(name)) == NULL) |
| 66 | return NULL; |
| 67 | d = PyModule_GetDict(m); |
| 68 | if (methods != NULL) { |
| 69 | n = PyString_FromString(name); |
| 70 | if (n == NULL) |
| 71 | return NULL; |
| 72 | for (ml = methods; ml->ml_name != NULL; ml++) { |
| 73 | if ((ml->ml_flags & METH_CLASS) || |
| 74 | (ml->ml_flags & METH_STATIC)) { |
| 75 | PyErr_SetString(PyExc_ValueError, |
| 76 | "module functions cannot set" |
| 77 | " METH_CLASS or METH_STATIC"); |
| 78 | Py_DECREF(n); |
| 79 | return NULL; |
| 80 | } |
| 81 | v = PyCFunction_NewEx(ml, passthrough, n); |
| 82 | if (v == NULL) { |
| 83 | Py_DECREF(n); |
| 84 | return NULL; |
| 85 | } |
| 86 | if (PyDict_SetItemString(d, ml->ml_name, v) != 0) { |
| 87 | Py_DECREF(v); |
| 88 | Py_DECREF(n); |
| 89 | return NULL; |
| 90 | } |
| 91 | Py_DECREF(v); |
| 92 | } |
| 93 | Py_DECREF(n); |
| 94 | } |
| 95 | if (doc != NULL) { |
| 96 | v = PyString_FromString(doc); |
| 97 | if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) { |
| 98 | Py_XDECREF(v); |
| 99 | return NULL; |
| 100 | } |
| 101 | Py_DECREF(v); |
| 102 | } |
| 103 | return m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 107 | /* Helper for mkvalue() to scan the length of a format */ |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 108 | |
Fred Drake | ceead6d | 2003-01-30 15:08:25 +0000 | [diff] [blame] | 109 | static int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 110 | countformat(const char *format, int endchar) |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 111 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 112 | int count = 0; |
| 113 | int level = 0; |
| 114 | while (level > 0 || *format != endchar) { |
| 115 | switch (*format) { |
| 116 | case '\0': |
| 117 | /* Premature end */ |
| 118 | PyErr_SetString(PyExc_SystemError, |
| 119 | "unmatched paren in format"); |
| 120 | return -1; |
| 121 | case '(': |
| 122 | case '[': |
| 123 | case '{': |
| 124 | if (level == 0) |
| 125 | count++; |
| 126 | level++; |
| 127 | break; |
| 128 | case ')': |
| 129 | case ']': |
| 130 | case '}': |
| 131 | level--; |
| 132 | break; |
| 133 | case '#': |
| 134 | case '&': |
| 135 | case ',': |
| 136 | case ':': |
| 137 | case ' ': |
| 138 | case '\t': |
| 139 | break; |
| 140 | default: |
| 141 | if (level == 0) |
| 142 | count++; |
| 143 | } |
| 144 | format++; |
| 145 | } |
| 146 | return count; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 150 | /* Generic function to create a value -- the inverse of getargs() */ |
| 151 | /* After an original idea and first implementation by Steven Miale */ |
| 152 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 153 | static PyObject *do_mktuple(const char**, va_list *, int, int, int); |
| 154 | static PyObject *do_mklist(const char**, va_list *, int, int, int); |
| 155 | static PyObject *do_mkdict(const char**, va_list *, int, int, int); |
| 156 | static PyObject *do_mkvalue(const char**, va_list *, int); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 158 | |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 159 | static void |
| 160 | do_ignore(const char **p_format, va_list *p_va, int endchar, int n, int flags) |
| 161 | { |
| 162 | PyObject *v; |
| 163 | int i; |
| 164 | assert(PyErr_Occurred()); |
| 165 | v = PyTuple_New(n); |
| 166 | for (i = 0; i < n; i++) { |
| 167 | PyObject *exception, *value, *tb, *w; |
| 168 | PyErr_Fetch(&exception, &value, &tb); |
| 169 | w = do_mkvalue(p_format, p_va, flags); |
| 170 | PyErr_Restore(exception, value, tb); |
| 171 | if (w != NULL) { |
| 172 | if (v != NULL) { |
| 173 | PyTuple_SET_ITEM(v, i, w); |
| 174 | } |
| 175 | else { |
| 176 | Py_DECREF(w); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | Py_XDECREF(v); |
| 181 | if (**p_format != endchar) { |
| 182 | PyErr_SetString(PyExc_SystemError, |
| 183 | "Unmatched paren in format"); |
| 184 | return; |
| 185 | } |
| 186 | if (endchar) |
| 187 | ++*p_format; |
| 188 | } |
| 189 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 190 | static PyObject * |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 191 | do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 192 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 193 | PyObject *d; |
| 194 | int i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 195 | if (n < 0) |
| 196 | return NULL; |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 197 | if (n % 2) { |
| 198 | PyErr_SetString(PyExc_SystemError, |
| 199 | "Bad dict format"); |
| 200 | do_ignore(p_format, p_va, endchar, n, flags); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 201 | return NULL; |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 202 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 203 | /* Note that we can't bail immediately on error as this will leak |
| 204 | refcounts on any 'N' arguments. */ |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 205 | if ((d = PyDict_New()) == NULL) { |
| 206 | do_ignore(p_format, p_va, endchar, n, flags); |
| 207 | return NULL; |
| 208 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 209 | for (i = 0; i < n; i+= 2) { |
| 210 | PyObject *k, *v; |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 211 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 212 | k = do_mkvalue(p_format, p_va, flags); |
| 213 | if (k == NULL) { |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 214 | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 215 | Py_DECREF(d); |
| 216 | return NULL; |
| 217 | } |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 218 | v = do_mkvalue(p_format, p_va, flags); |
| 219 | if (v == NULL || PyDict_SetItem(d, k, v) < 0) { |
| 220 | do_ignore(p_format, p_va, endchar, n - i - 2, flags); |
| 221 | Py_DECREF(k); |
| 222 | Py_XDECREF(v); |
| 223 | Py_DECREF(d); |
| 224 | return NULL; |
| 225 | } |
| 226 | Py_DECREF(k); |
| 227 | Py_DECREF(v); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 228 | } |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 229 | if (**p_format != endchar) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 230 | Py_DECREF(d); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 231 | PyErr_SetString(PyExc_SystemError, |
| 232 | "Unmatched paren in format"); |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 233 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 234 | } |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 235 | if (endchar) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 236 | ++*p_format; |
| 237 | return d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 240 | static PyObject * |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 241 | do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 242 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 243 | PyObject *v; |
| 244 | int i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 245 | if (n < 0) |
| 246 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 247 | /* Note that we can't bail immediately on error as this will leak |
| 248 | refcounts on any 'N' arguments. */ |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 249 | v = PyList_New(n); |
| 250 | if (v == NULL) { |
| 251 | do_ignore(p_format, p_va, endchar, n, flags); |
| 252 | return NULL; |
| 253 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 254 | for (i = 0; i < n; i++) { |
| 255 | PyObject *w = do_mkvalue(p_format, p_va, flags); |
| 256 | if (w == NULL) { |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 257 | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
| 258 | Py_DECREF(v); |
| 259 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 260 | } |
| 261 | PyList_SET_ITEM(v, i, w); |
| 262 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 263 | if (**p_format != endchar) { |
| 264 | Py_DECREF(v); |
| 265 | PyErr_SetString(PyExc_SystemError, |
| 266 | "Unmatched paren in format"); |
| 267 | return NULL; |
| 268 | } |
| 269 | if (endchar) |
| 270 | ++*p_format; |
| 271 | return v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 274 | #ifdef Py_USING_UNICODE |
Fred Drake | 25d3447 | 2000-04-28 14:42:37 +0000 | [diff] [blame] | 275 | static int |
| 276 | _ustrlen(Py_UNICODE *u) |
| 277 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 278 | int i = 0; |
| 279 | Py_UNICODE *v = u; |
| 280 | while (*v != 0) { i++; v++; } |
| 281 | return i; |
Fred Drake | 25d3447 | 2000-04-28 14:42:37 +0000 | [diff] [blame] | 282 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 283 | #endif |
Fred Drake | 25d3447 | 2000-04-28 14:42:37 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 285 | static PyObject * |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 286 | do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags) |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 287 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 288 | PyObject *v; |
| 289 | int i; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 290 | if (n < 0) |
| 291 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 292 | /* Note that we can't bail immediately on error as this will leak |
| 293 | refcounts on any 'N' arguments. */ |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 294 | if ((v = PyTuple_New(n)) == NULL) { |
| 295 | do_ignore(p_format, p_va, endchar, n, flags); |
| 296 | return NULL; |
| 297 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | for (i = 0; i < n; i++) { |
| 299 | PyObject *w = do_mkvalue(p_format, p_va, flags); |
| 300 | if (w == NULL) { |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 301 | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
| 302 | Py_DECREF(v); |
| 303 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 304 | } |
| 305 | PyTuple_SET_ITEM(v, i, w); |
| 306 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 307 | if (**p_format != endchar) { |
| 308 | Py_DECREF(v); |
| 309 | PyErr_SetString(PyExc_SystemError, |
| 310 | "Unmatched paren in format"); |
| 311 | return NULL; |
| 312 | } |
| 313 | if (endchar) |
| 314 | ++*p_format; |
| 315 | return v; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 318 | static PyObject * |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 319 | do_mkvalue(const char **p_format, va_list *p_va, int flags) |
Guido van Rossum | 899dcf3 | 1992-05-15 11:04:59 +0000 | [diff] [blame] | 320 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 321 | for (;;) { |
| 322 | switch (*(*p_format)++) { |
| 323 | case '(': |
| 324 | return do_mktuple(p_format, p_va, ')', |
| 325 | countformat(*p_format, ')'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 327 | case '[': |
| 328 | return do_mklist(p_format, p_va, ']', |
| 329 | countformat(*p_format, ']'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 331 | case '{': |
| 332 | return do_mkdict(p_format, p_va, '}', |
| 333 | countformat(*p_format, '}'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 334 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 335 | case 'b': |
| 336 | case 'B': |
| 337 | case 'h': |
| 338 | case 'i': |
| 339 | return PyInt_FromLong((long)va_arg(*p_va, int)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 341 | case 'H': |
| 342 | return PyInt_FromLong((long)va_arg(*p_va, unsigned int)); |
| 343 | |
| 344 | case 'I': |
| 345 | { |
| 346 | unsigned int n; |
| 347 | n = va_arg(*p_va, unsigned int); |
| 348 | if (n > (unsigned long)PyInt_GetMax()) |
| 349 | return PyLong_FromUnsignedLong((unsigned long)n); |
| 350 | else |
| 351 | return PyInt_FromLong(n); |
| 352 | } |
| 353 | |
| 354 | case 'n': |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 355 | #if SIZEOF_SIZE_T!=SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 356 | return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t)); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 357 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 358 | /* Fall through from 'n' to 'l' if Py_ssize_t is long */ |
| 359 | case 'l': |
| 360 | return PyInt_FromLong(va_arg(*p_va, long)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 362 | case 'k': |
| 363 | { |
| 364 | unsigned long n; |
| 365 | n = va_arg(*p_va, unsigned long); |
| 366 | if (n > (unsigned long)PyInt_GetMax()) |
| 367 | return PyLong_FromUnsignedLong(n); |
| 368 | else |
| 369 | return PyInt_FromLong(n); |
| 370 | } |
Jack Jansen | dbd6503 | 2003-04-17 22:01:10 +0000 | [diff] [blame] | 371 | |
Guido van Rossum | 3dbba6e | 1999-01-25 21:48:56 +0000 | [diff] [blame] | 372 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 373 | case 'L': |
| 374 | return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG)); |
Jack Jansen | dbd6503 | 2003-04-17 22:01:10 +0000 | [diff] [blame] | 375 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 376 | case 'K': |
| 377 | return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG)); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 378 | #endif |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 379 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 380 | case 'u': |
| 381 | { |
| 382 | PyObject *v; |
| 383 | Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *); |
| 384 | Py_ssize_t n; |
| 385 | if (**p_format == '#') { |
| 386 | ++*p_format; |
| 387 | if (flags & FLAG_SIZE_T) |
| 388 | n = va_arg(*p_va, Py_ssize_t); |
| 389 | else |
| 390 | n = va_arg(*p_va, int); |
| 391 | } |
| 392 | else |
| 393 | n = -1; |
| 394 | if (u == NULL) { |
| 395 | v = Py_None; |
| 396 | Py_INCREF(v); |
| 397 | } |
| 398 | else { |
| 399 | if (n < 0) |
| 400 | n = _ustrlen(u); |
| 401 | v = PyUnicode_FromUnicode(u, n); |
| 402 | } |
| 403 | return v; |
| 404 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 405 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 406 | case 'f': |
| 407 | case 'd': |
| 408 | return PyFloat_FromDouble( |
| 409 | (double)va_arg(*p_va, va_double)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 410 | |
Fred Drake | aec7924 | 2001-03-12 21:03:26 +0000 | [diff] [blame] | 411 | #ifndef WITHOUT_COMPLEX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 412 | case 'D': |
| 413 | return PyComplex_FromCComplex( |
| 414 | *((Py_complex *)va_arg(*p_va, Py_complex *))); |
Fred Drake | aec7924 | 2001-03-12 21:03:26 +0000 | [diff] [blame] | 415 | #endif /* WITHOUT_COMPLEX */ |
| 416 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 417 | case 'c': |
| 418 | { |
| 419 | char p[1]; |
| 420 | p[0] = (char)va_arg(*p_va, int); |
| 421 | return PyString_FromStringAndSize(p, 1); |
| 422 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 423 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 424 | case 's': |
| 425 | case 'z': |
| 426 | { |
| 427 | PyObject *v; |
| 428 | char *str = va_arg(*p_va, char *); |
| 429 | Py_ssize_t n; |
| 430 | if (**p_format == '#') { |
| 431 | ++*p_format; |
| 432 | if (flags & FLAG_SIZE_T) |
| 433 | n = va_arg(*p_va, Py_ssize_t); |
| 434 | else |
| 435 | n = va_arg(*p_va, int); |
| 436 | } |
| 437 | else |
| 438 | n = -1; |
| 439 | if (str == NULL) { |
| 440 | v = Py_None; |
| 441 | Py_INCREF(v); |
| 442 | } |
| 443 | else { |
| 444 | if (n < 0) { |
| 445 | size_t m = strlen(str); |
| 446 | if (m > PY_SSIZE_T_MAX) { |
| 447 | PyErr_SetString(PyExc_OverflowError, |
| 448 | "string too long for Python string"); |
| 449 | return NULL; |
| 450 | } |
| 451 | n = (Py_ssize_t)m; |
| 452 | } |
| 453 | v = PyString_FromStringAndSize(str, n); |
| 454 | } |
| 455 | return v; |
| 456 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 457 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 458 | case 'N': |
| 459 | case 'S': |
| 460 | case 'O': |
| 461 | if (**p_format == '&') { |
| 462 | typedef PyObject *(*converter)(void *); |
| 463 | converter func = va_arg(*p_va, converter); |
| 464 | void *arg = va_arg(*p_va, void *); |
| 465 | ++*p_format; |
| 466 | return (*func)(arg); |
| 467 | } |
| 468 | else { |
| 469 | PyObject *v; |
| 470 | v = va_arg(*p_va, PyObject *); |
| 471 | if (v != NULL) { |
| 472 | if (*(*p_format - 1) != 'N') |
| 473 | Py_INCREF(v); |
| 474 | } |
| 475 | else if (!PyErr_Occurred()) |
| 476 | /* If a NULL was passed |
| 477 | * because a call that should |
| 478 | * have constructed a value |
| 479 | * failed, that's OK, and we |
| 480 | * pass the error on; but if |
| 481 | * no error occurred it's not |
| 482 | * clear that the caller knew |
| 483 | * what she was doing. */ |
| 484 | PyErr_SetString(PyExc_SystemError, |
| 485 | "NULL object passed to Py_BuildValue"); |
| 486 | return v; |
| 487 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 488 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 489 | case ':': |
| 490 | case ',': |
| 491 | case ' ': |
| 492 | case '\t': |
| 493 | break; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 495 | default: |
| 496 | PyErr_SetString(PyExc_SystemError, |
| 497 | "bad format char passed to Py_BuildValue"); |
| 498 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 499 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 504 | |
Fred Drake | ceead6d | 2003-01-30 15:08:25 +0000 | [diff] [blame] | 505 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 506 | Py_BuildValue(const char *format, ...) |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 507 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 508 | va_list va; |
| 509 | PyObject* retval; |
| 510 | va_start(va, format); |
| 511 | retval = va_build_value(format, va, 0); |
| 512 | va_end(va); |
| 513 | return retval; |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | PyObject * |
| 517 | _Py_BuildValue_SizeT(const char *format, ...) |
| 518 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 519 | va_list va; |
| 520 | PyObject* retval; |
| 521 | va_start(va, format); |
| 522 | retval = va_build_value(format, va, FLAG_SIZE_T); |
| 523 | va_end(va); |
| 524 | return retval; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 525 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 526 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 527 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 528 | Py_VaBuildValue(const char *format, va_list va) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 529 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 530 | return va_build_value(format, va, 0); |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | PyObject * |
| 534 | _Py_VaBuildValue_SizeT(const char *format, va_list va) |
| 535 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 536 | return va_build_value(format, va, FLAG_SIZE_T); |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static PyObject * |
| 540 | va_build_value(const char *format, va_list va, int flags) |
| 541 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 542 | const char *f = format; |
| 543 | int n = countformat(f, '\0'); |
| 544 | va_list lva; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 545 | |
| 546 | #ifdef VA_LIST_IS_ARRAY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 547 | memcpy(lva, va, sizeof(va_list)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 548 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 549 | #ifdef __va_copy |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 550 | __va_copy(lva, va); |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 551 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 552 | lva = va; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 553 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 554 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 556 | if (n < 0) |
| 557 | return NULL; |
| 558 | if (n == 0) { |
| 559 | Py_INCREF(Py_None); |
| 560 | return Py_None; |
| 561 | } |
| 562 | if (n == 1) |
| 563 | return do_mkvalue(&f, &lva, flags); |
| 564 | return do_mktuple(&f, &lva, '\0', n, flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 568 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 569 | PyEval_CallFunction(PyObject *obj, const char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 570 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 571 | va_list vargs; |
| 572 | PyObject *args; |
| 573 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 574 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 575 | va_start(vargs, format); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 576 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 577 | args = Py_VaBuildValue(format, vargs); |
| 578 | va_end(vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 580 | if (args == NULL) |
| 581 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 583 | res = PyEval_CallObject(obj, args); |
| 584 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 586 | return res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 590 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 591 | PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 592 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 593 | va_list vargs; |
| 594 | PyObject *meth; |
| 595 | PyObject *args; |
| 596 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 598 | meth = PyObject_GetAttrString(obj, methodname); |
| 599 | if (meth == NULL) |
| 600 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 601 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 602 | va_start(vargs, format); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 604 | args = Py_VaBuildValue(format, vargs); |
| 605 | va_end(vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 607 | if (args == NULL) { |
| 608 | Py_DECREF(meth); |
| 609 | return NULL; |
| 610 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 612 | res = PyEval_CallObject(meth, args); |
| 613 | Py_DECREF(meth); |
| 614 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 616 | return res; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 617 | } |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 618 | |
| 619 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 620 | PyModule_AddObject(PyObject *m, const char *name, PyObject *o) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 621 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 622 | PyObject *dict; |
| 623 | if (!PyModule_Check(m)) { |
| 624 | PyErr_SetString(PyExc_TypeError, |
| 625 | "PyModule_AddObject() needs module as first arg"); |
| 626 | return -1; |
| 627 | } |
| 628 | if (!o) { |
| 629 | if (!PyErr_Occurred()) |
| 630 | PyErr_SetString(PyExc_TypeError, |
| 631 | "PyModule_AddObject() needs non-NULL value"); |
| 632 | return -1; |
| 633 | } |
Jeremy Hylton | c44dbc4 | 2003-06-21 21:35:25 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 635 | dict = PyModule_GetDict(m); |
| 636 | if (dict == NULL) { |
| 637 | /* Internal error -- modules must have a dict! */ |
| 638 | PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__", |
| 639 | PyModule_GetName(m)); |
| 640 | return -1; |
| 641 | } |
| 642 | if (PyDict_SetItemString(dict, name, o)) |
| 643 | return -1; |
| 644 | Py_DECREF(o); |
| 645 | return 0; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 648 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 649 | PyModule_AddIntConstant(PyObject *m, const char *name, long value) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 650 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 651 | PyObject *o = PyInt_FromLong(value); |
| 652 | if (!o) |
| 653 | return -1; |
| 654 | if (PyModule_AddObject(m, name, o) == 0) |
| 655 | return 0; |
| 656 | Py_DECREF(o); |
| 657 | return -1; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 660 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 661 | PyModule_AddStringConstant(PyObject *m, const char *name, const char *value) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 662 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 663 | PyObject *o = PyString_FromString(value); |
| 664 | if (!o) |
| 665 | return -1; |
| 666 | if (PyModule_AddObject(m, name, o) == 0) |
| 667 | return 0; |
| 668 | Py_DECREF(o); |
| 669 | return -1; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 670 | } |