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 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 159 | static PyObject * |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 160 | 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] | 161 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 162 | PyObject *d; |
| 163 | int i; |
| 164 | int itemfailed = 0; |
| 165 | if (n < 0) |
| 166 | return NULL; |
| 167 | if ((d = PyDict_New()) == NULL) |
| 168 | return NULL; |
| 169 | /* Note that we can't bail immediately on error as this will leak |
| 170 | refcounts on any 'N' arguments. */ |
| 171 | for (i = 0; i < n; i+= 2) { |
| 172 | PyObject *k, *v; |
| 173 | int err; |
| 174 | k = do_mkvalue(p_format, p_va, flags); |
| 175 | if (k == NULL) { |
| 176 | itemfailed = 1; |
| 177 | Py_INCREF(Py_None); |
| 178 | k = Py_None; |
| 179 | } |
| 180 | v = do_mkvalue(p_format, p_va, flags); |
| 181 | if (v == NULL) { |
| 182 | itemfailed = 1; |
| 183 | Py_INCREF(Py_None); |
| 184 | v = Py_None; |
| 185 | } |
| 186 | err = PyDict_SetItem(d, k, v); |
| 187 | Py_DECREF(k); |
| 188 | Py_DECREF(v); |
| 189 | if (err < 0 || itemfailed) { |
| 190 | Py_DECREF(d); |
| 191 | return NULL; |
| 192 | } |
| 193 | } |
| 194 | if (d != NULL && **p_format != endchar) { |
| 195 | Py_DECREF(d); |
| 196 | d = NULL; |
| 197 | PyErr_SetString(PyExc_SystemError, |
| 198 | "Unmatched paren in format"); |
| 199 | } |
| 200 | else if (endchar) |
| 201 | ++*p_format; |
| 202 | return d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 205 | static PyObject * |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 206 | 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] | 207 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 208 | PyObject *v; |
| 209 | int i; |
| 210 | int itemfailed = 0; |
| 211 | if (n < 0) |
| 212 | return NULL; |
| 213 | v = PyList_New(n); |
| 214 | if (v == NULL) |
| 215 | return NULL; |
| 216 | /* Note that we can't bail immediately on error as this will leak |
| 217 | refcounts on any 'N' arguments. */ |
| 218 | for (i = 0; i < n; i++) { |
| 219 | PyObject *w = do_mkvalue(p_format, p_va, flags); |
| 220 | if (w == NULL) { |
| 221 | itemfailed = 1; |
| 222 | Py_INCREF(Py_None); |
| 223 | w = Py_None; |
| 224 | } |
| 225 | PyList_SET_ITEM(v, i, w); |
| 226 | } |
Neal Norwitz | 3e90fa5 | 2006-03-06 23:07:34 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 228 | if (itemfailed) { |
| 229 | /* do_mkvalue() should have already set an error */ |
| 230 | Py_DECREF(v); |
| 231 | return NULL; |
| 232 | } |
| 233 | if (**p_format != endchar) { |
| 234 | Py_DECREF(v); |
| 235 | PyErr_SetString(PyExc_SystemError, |
| 236 | "Unmatched paren in format"); |
| 237 | return NULL; |
| 238 | } |
| 239 | if (endchar) |
| 240 | ++*p_format; |
| 241 | return v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 244 | #ifdef Py_USING_UNICODE |
Fred Drake | 25d3447 | 2000-04-28 14:42:37 +0000 | [diff] [blame] | 245 | static int |
| 246 | _ustrlen(Py_UNICODE *u) |
| 247 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 248 | int i = 0; |
| 249 | Py_UNICODE *v = u; |
| 250 | while (*v != 0) { i++; v++; } |
| 251 | return i; |
Fred Drake | 25d3447 | 2000-04-28 14:42:37 +0000 | [diff] [blame] | 252 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 253 | #endif |
Fred Drake | 25d3447 | 2000-04-28 14:42:37 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 255 | static PyObject * |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 256 | 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] | 257 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 258 | PyObject *v; |
| 259 | int i; |
| 260 | int itemfailed = 0; |
| 261 | if (n < 0) |
| 262 | return NULL; |
| 263 | if ((v = PyTuple_New(n)) == NULL) |
| 264 | return NULL; |
| 265 | /* Note that we can't bail immediately on error as this will leak |
| 266 | refcounts on any 'N' arguments. */ |
| 267 | for (i = 0; i < n; i++) { |
| 268 | PyObject *w = do_mkvalue(p_format, p_va, flags); |
| 269 | if (w == NULL) { |
| 270 | itemfailed = 1; |
| 271 | Py_INCREF(Py_None); |
| 272 | w = Py_None; |
| 273 | } |
| 274 | PyTuple_SET_ITEM(v, i, w); |
| 275 | } |
| 276 | if (itemfailed) { |
| 277 | /* do_mkvalue() should have already set an error */ |
| 278 | Py_DECREF(v); |
| 279 | return NULL; |
| 280 | } |
| 281 | if (**p_format != endchar) { |
| 282 | Py_DECREF(v); |
| 283 | PyErr_SetString(PyExc_SystemError, |
| 284 | "Unmatched paren in format"); |
| 285 | return NULL; |
| 286 | } |
| 287 | if (endchar) |
| 288 | ++*p_format; |
| 289 | return v; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 292 | static PyObject * |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 293 | 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] | 294 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 295 | for (;;) { |
| 296 | switch (*(*p_format)++) { |
| 297 | case '(': |
| 298 | return do_mktuple(p_format, p_va, ')', |
| 299 | countformat(*p_format, ')'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 301 | case '[': |
| 302 | return do_mklist(p_format, p_va, ']', |
| 303 | countformat(*p_format, ']'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 305 | case '{': |
| 306 | return do_mkdict(p_format, p_va, '}', |
| 307 | countformat(*p_format, '}'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 309 | case 'b': |
| 310 | case 'B': |
| 311 | case 'h': |
| 312 | case 'i': |
| 313 | return PyInt_FromLong((long)va_arg(*p_va, int)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 315 | case 'H': |
| 316 | return PyInt_FromLong((long)va_arg(*p_va, unsigned int)); |
| 317 | |
| 318 | case 'I': |
| 319 | { |
| 320 | unsigned int n; |
| 321 | n = va_arg(*p_va, unsigned int); |
| 322 | if (n > (unsigned long)PyInt_GetMax()) |
| 323 | return PyLong_FromUnsignedLong((unsigned long)n); |
| 324 | else |
| 325 | return PyInt_FromLong(n); |
| 326 | } |
| 327 | |
| 328 | case 'n': |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 329 | #if SIZEOF_SIZE_T!=SIZEOF_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 330 | 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] | 331 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 332 | /* Fall through from 'n' to 'l' if Py_ssize_t is long */ |
| 333 | case 'l': |
| 334 | return PyInt_FromLong(va_arg(*p_va, long)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 336 | case 'k': |
| 337 | { |
| 338 | unsigned long n; |
| 339 | n = va_arg(*p_va, unsigned long); |
| 340 | if (n > (unsigned long)PyInt_GetMax()) |
| 341 | return PyLong_FromUnsignedLong(n); |
| 342 | else |
| 343 | return PyInt_FromLong(n); |
| 344 | } |
Jack Jansen | dbd6503 | 2003-04-17 22:01:10 +0000 | [diff] [blame] | 345 | |
Guido van Rossum | 3dbba6e | 1999-01-25 21:48:56 +0000 | [diff] [blame] | 346 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 347 | case 'L': |
| 348 | 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] | 349 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 350 | case 'K': |
| 351 | 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] | 352 | #endif |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 353 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 354 | case 'u': |
| 355 | { |
| 356 | PyObject *v; |
| 357 | Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *); |
| 358 | Py_ssize_t n; |
| 359 | if (**p_format == '#') { |
| 360 | ++*p_format; |
| 361 | if (flags & FLAG_SIZE_T) |
| 362 | n = va_arg(*p_va, Py_ssize_t); |
| 363 | else |
| 364 | n = va_arg(*p_va, int); |
| 365 | } |
| 366 | else |
| 367 | n = -1; |
| 368 | if (u == NULL) { |
| 369 | v = Py_None; |
| 370 | Py_INCREF(v); |
| 371 | } |
| 372 | else { |
| 373 | if (n < 0) |
| 374 | n = _ustrlen(u); |
| 375 | v = PyUnicode_FromUnicode(u, n); |
| 376 | } |
| 377 | return v; |
| 378 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 379 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 380 | case 'f': |
| 381 | case 'd': |
| 382 | return PyFloat_FromDouble( |
| 383 | (double)va_arg(*p_va, va_double)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 384 | |
Fred Drake | aec7924 | 2001-03-12 21:03:26 +0000 | [diff] [blame] | 385 | #ifndef WITHOUT_COMPLEX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 386 | case 'D': |
| 387 | return PyComplex_FromCComplex( |
| 388 | *((Py_complex *)va_arg(*p_va, Py_complex *))); |
Fred Drake | aec7924 | 2001-03-12 21:03:26 +0000 | [diff] [blame] | 389 | #endif /* WITHOUT_COMPLEX */ |
| 390 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 391 | case 'c': |
| 392 | { |
| 393 | char p[1]; |
| 394 | p[0] = (char)va_arg(*p_va, int); |
| 395 | return PyString_FromStringAndSize(p, 1); |
| 396 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 397 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 398 | case 's': |
| 399 | case 'z': |
| 400 | { |
| 401 | PyObject *v; |
| 402 | char *str = va_arg(*p_va, char *); |
| 403 | Py_ssize_t n; |
| 404 | if (**p_format == '#') { |
| 405 | ++*p_format; |
| 406 | if (flags & FLAG_SIZE_T) |
| 407 | n = va_arg(*p_va, Py_ssize_t); |
| 408 | else |
| 409 | n = va_arg(*p_va, int); |
| 410 | } |
| 411 | else |
| 412 | n = -1; |
| 413 | if (str == NULL) { |
| 414 | v = Py_None; |
| 415 | Py_INCREF(v); |
| 416 | } |
| 417 | else { |
| 418 | if (n < 0) { |
| 419 | size_t m = strlen(str); |
| 420 | if (m > PY_SSIZE_T_MAX) { |
| 421 | PyErr_SetString(PyExc_OverflowError, |
| 422 | "string too long for Python string"); |
| 423 | return NULL; |
| 424 | } |
| 425 | n = (Py_ssize_t)m; |
| 426 | } |
| 427 | v = PyString_FromStringAndSize(str, n); |
| 428 | } |
| 429 | return v; |
| 430 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 432 | case 'N': |
| 433 | case 'S': |
| 434 | case 'O': |
| 435 | if (**p_format == '&') { |
| 436 | typedef PyObject *(*converter)(void *); |
| 437 | converter func = va_arg(*p_va, converter); |
| 438 | void *arg = va_arg(*p_va, void *); |
| 439 | ++*p_format; |
| 440 | return (*func)(arg); |
| 441 | } |
| 442 | else { |
| 443 | PyObject *v; |
| 444 | v = va_arg(*p_va, PyObject *); |
| 445 | if (v != NULL) { |
| 446 | if (*(*p_format - 1) != 'N') |
| 447 | Py_INCREF(v); |
| 448 | } |
| 449 | else if (!PyErr_Occurred()) |
| 450 | /* If a NULL was passed |
| 451 | * because a call that should |
| 452 | * have constructed a value |
| 453 | * failed, that's OK, and we |
| 454 | * pass the error on; but if |
| 455 | * no error occurred it's not |
| 456 | * clear that the caller knew |
| 457 | * what she was doing. */ |
| 458 | PyErr_SetString(PyExc_SystemError, |
| 459 | "NULL object passed to Py_BuildValue"); |
| 460 | return v; |
| 461 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 462 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 463 | case ':': |
| 464 | case ',': |
| 465 | case ' ': |
| 466 | case '\t': |
| 467 | break; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 468 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 469 | default: |
| 470 | PyErr_SetString(PyExc_SystemError, |
| 471 | "bad format char passed to Py_BuildValue"); |
| 472 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 473 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 478 | |
Fred Drake | ceead6d | 2003-01-30 15:08:25 +0000 | [diff] [blame] | 479 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 480 | Py_BuildValue(const char *format, ...) |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 481 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 482 | va_list va; |
| 483 | PyObject* retval; |
| 484 | va_start(va, format); |
| 485 | retval = va_build_value(format, va, 0); |
| 486 | va_end(va); |
| 487 | return retval; |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | PyObject * |
| 491 | _Py_BuildValue_SizeT(const char *format, ...) |
| 492 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 493 | va_list va; |
| 494 | PyObject* retval; |
| 495 | va_start(va, format); |
| 496 | retval = va_build_value(format, va, FLAG_SIZE_T); |
| 497 | va_end(va); |
| 498 | return retval; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 499 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 500 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 501 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 502 | Py_VaBuildValue(const char *format, va_list va) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 503 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 504 | return va_build_value(format, va, 0); |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | PyObject * |
| 508 | _Py_VaBuildValue_SizeT(const char *format, va_list va) |
| 509 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 510 | return va_build_value(format, va, FLAG_SIZE_T); |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static PyObject * |
| 514 | va_build_value(const char *format, va_list va, int flags) |
| 515 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 516 | const char *f = format; |
| 517 | int n = countformat(f, '\0'); |
| 518 | va_list lva; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 519 | |
| 520 | #ifdef VA_LIST_IS_ARRAY |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 521 | memcpy(lva, va, sizeof(va_list)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 522 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 523 | #ifdef __va_copy |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 524 | __va_copy(lva, va); |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 525 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 526 | lva = va; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 527 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 528 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 529 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 530 | if (n < 0) |
| 531 | return NULL; |
| 532 | if (n == 0) { |
| 533 | Py_INCREF(Py_None); |
| 534 | return Py_None; |
| 535 | } |
| 536 | if (n == 1) |
| 537 | return do_mkvalue(&f, &lva, flags); |
| 538 | return do_mktuple(&f, &lva, '\0', n, flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 542 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 543 | PyEval_CallFunction(PyObject *obj, const char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 544 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 545 | va_list vargs; |
| 546 | PyObject *args; |
| 547 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 548 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 549 | va_start(vargs, format); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 551 | args = Py_VaBuildValue(format, vargs); |
| 552 | va_end(vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 554 | if (args == NULL) |
| 555 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 556 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 557 | res = PyEval_CallObject(obj, args); |
| 558 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 559 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 560 | return res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 564 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 565 | PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 566 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 567 | va_list vargs; |
| 568 | PyObject *meth; |
| 569 | PyObject *args; |
| 570 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 571 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 572 | meth = PyObject_GetAttrString(obj, methodname); |
| 573 | if (meth == NULL) |
| 574 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 576 | va_start(vargs, format); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 578 | args = Py_VaBuildValue(format, vargs); |
| 579 | va_end(vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 581 | if (args == NULL) { |
| 582 | Py_DECREF(meth); |
| 583 | return NULL; |
| 584 | } |
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 | res = PyEval_CallObject(meth, args); |
| 587 | Py_DECREF(meth); |
| 588 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 589 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 590 | return res; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 591 | } |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 592 | |
| 593 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 594 | PyModule_AddObject(PyObject *m, const char *name, PyObject *o) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 595 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 596 | PyObject *dict; |
| 597 | if (!PyModule_Check(m)) { |
| 598 | PyErr_SetString(PyExc_TypeError, |
| 599 | "PyModule_AddObject() needs module as first arg"); |
| 600 | return -1; |
| 601 | } |
| 602 | if (!o) { |
| 603 | if (!PyErr_Occurred()) |
| 604 | PyErr_SetString(PyExc_TypeError, |
| 605 | "PyModule_AddObject() needs non-NULL value"); |
| 606 | return -1; |
| 607 | } |
Jeremy Hylton | c44dbc4 | 2003-06-21 21:35:25 +0000 | [diff] [blame] | 608 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 609 | dict = PyModule_GetDict(m); |
| 610 | if (dict == NULL) { |
| 611 | /* Internal error -- modules must have a dict! */ |
| 612 | PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__", |
| 613 | PyModule_GetName(m)); |
| 614 | return -1; |
| 615 | } |
| 616 | if (PyDict_SetItemString(dict, name, o)) |
| 617 | return -1; |
| 618 | Py_DECREF(o); |
| 619 | return 0; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 622 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 623 | PyModule_AddIntConstant(PyObject *m, const char *name, long value) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 624 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 625 | PyObject *o = PyInt_FromLong(value); |
| 626 | if (!o) |
| 627 | return -1; |
| 628 | if (PyModule_AddObject(m, name, o) == 0) |
| 629 | return 0; |
| 630 | Py_DECREF(o); |
| 631 | return -1; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 634 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 635 | PyModule_AddStringConstant(PyObject *m, const char *name, const char *value) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 636 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 637 | PyObject *o = PyString_FromString(value); |
| 638 | if (!o) |
| 639 | return -1; |
| 640 | if (PyModule_AddObject(m, name, o) == 0) |
| 641 | return 0; |
| 642 | Py_DECREF(o); |
| 643 | return -1; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 644 | } |