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