Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Module support implementation */ |
| 33 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 36 | #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */ |
| 37 | typedef extended va_double; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 38 | #else |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 39 | typedef double va_double; |
| 40 | #endif |
| 41 | |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 42 | /* Package context -- the full module name for package imports */ |
| 43 | char *_Py_PackageContext = NULL; |
| 44 | |
Guido van Rossum | 40b33c6 | 1997-08-02 03:07:46 +0000 | [diff] [blame] | 45 | /* Py_InitModule4() parameters: |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 46 | - name is the module name |
| 47 | - methods is the list of top-level functions |
| 48 | - doc is the documentation string |
| 49 | - passthrough is passed as self to functions defined in the module |
| 50 | - api_version is the value of PYTHON_API_VERSION at the time the |
| 51 | module was compiled |
Guido van Rossum | 40b33c6 | 1997-08-02 03:07:46 +0000 | [diff] [blame] | 52 | |
| 53 | Return value is a borrowed reference to the module object; or NULL |
| 54 | if an error occurred (in Python 1.4 and before, errors were fatal). |
| 55 | Errors may still leak memory. |
Guido van Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 56 | */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 58 | static char api_version_warning[] = |
| 59 | "WARNING: Python C API version mismatch for module %s:\n\ |
Sjoerd Mullender | 996e6dc | 1996-01-23 16:07:29 +0000 | [diff] [blame] | 60 | This Python has API version %d, module %s has version %d.\n"; |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 62 | PyObject * |
| 63 | Py_InitModule4(name, methods, doc, passthrough, module_api_version) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 65 | PyMethodDef *methods; |
Guido van Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 66 | char *doc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 67 | PyObject *passthrough; |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 68 | int module_api_version; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 69 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 70 | PyObject *m, *d, *v; |
| 71 | PyMethodDef *ml; |
Guido van Rossum | 970a0a2 | 1995-01-09 17:47:20 +0000 | [diff] [blame] | 72 | if (module_api_version != PYTHON_API_VERSION) |
| 73 | fprintf(stderr, api_version_warning, |
| 74 | name, PYTHON_API_VERSION, name, module_api_version); |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 75 | if (_Py_PackageContext != NULL) { |
| 76 | char *p = strrchr(_Py_PackageContext, '.'); |
| 77 | if (p != NULL && strcmp(name, p+1) == 0) { |
| 78 | name = _Py_PackageContext; |
| 79 | _Py_PackageContext = NULL; |
| 80 | } |
| 81 | } |
Guido van Rossum | 40b33c6 | 1997-08-02 03:07:46 +0000 | [diff] [blame] | 82 | if ((m = PyImport_AddModule(name)) == NULL) |
| 83 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 84 | d = PyModule_GetDict(m); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | for (ml = methods; ml->ml_name != NULL; ml++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 86 | v = PyCFunction_New(ml, passthrough); |
Guido van Rossum | 40b33c6 | 1997-08-02 03:07:46 +0000 | [diff] [blame] | 87 | if (v == NULL) |
| 88 | return NULL; |
| 89 | if (PyDict_SetItemString(d, ml->ml_name, v) != 0) |
| 90 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 91 | Py_DECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | } |
Guido van Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 93 | if (doc != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 94 | v = PyString_FromString(doc); |
| 95 | if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) |
Guido van Rossum | 40b33c6 | 1997-08-02 03:07:46 +0000 | [diff] [blame] | 96 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 97 | Py_DECREF(v); |
Guido van Rossum | 50620fa | 1995-01-07 12:43:18 +0000 | [diff] [blame] | 98 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 99 | return m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 103 | /* Helper for mkvalue() to scan the length of a format */ |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 105 | static int countformat Py_PROTO((char *format, int endchar)); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 106 | static int countformat(format, endchar) |
| 107 | char *format; |
| 108 | int endchar; |
| 109 | { |
| 110 | int count = 0; |
| 111 | int level = 0; |
| 112 | while (level > 0 || *format != endchar) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 113 | switch (*format) { |
| 114 | case '\0': |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 115 | /* Premature end */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 116 | PyErr_SetString(PyExc_SystemError, |
| 117 | "unmatched paren in format"); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 118 | return -1; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 119 | case '(': |
| 120 | case '[': |
| 121 | case '{': |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 122 | if (level == 0) |
| 123 | count++; |
| 124 | level++; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 125 | break; |
| 126 | case ')': |
| 127 | case ']': |
| 128 | case '}': |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 129 | level--; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 130 | break; |
| 131 | case '#': |
Guido van Rossum | d1b9393 | 1995-01-20 16:56:02 +0000 | [diff] [blame] | 132 | case '&': |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 133 | case ',': |
| 134 | case ':': |
| 135 | case ' ': |
| 136 | case '\t': |
| 137 | break; |
| 138 | default: |
| 139 | if (level == 0) |
| 140 | count++; |
| 141 | } |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 142 | format++; |
| 143 | } |
| 144 | return count; |
| 145 | } |
| 146 | |
| 147 | |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 148 | /* Generic function to create a value -- the inverse of getargs() */ |
| 149 | /* After an original idea and first implementation by Steven Miale */ |
| 150 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 151 | static PyObject *do_mktuple Py_PROTO((char**, va_list *, int, int)); |
| 152 | static PyObject *do_mklist Py_PROTO((char**, va_list *, int, int)); |
| 153 | static PyObject *do_mkdict Py_PROTO((char**, va_list *, int, int)); |
| 154 | static PyObject *do_mkvalue Py_PROTO((char**, va_list *)); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 157 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 158 | do_mkdict(p_format, p_va, endchar, n) |
| 159 | char **p_format; |
| 160 | va_list *p_va; |
| 161 | int endchar; |
| 162 | int n; |
| 163 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 164 | PyObject *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 165 | int i; |
| 166 | if (n < 0) |
| 167 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 168 | if ((d = PyDict_New()) == NULL) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 169 | return NULL; |
| 170 | for (i = 0; i < n; i+= 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 171 | PyObject *k, *v; |
Guido van Rossum | db847bd | 1997-11-20 20:35:45 +0000 | [diff] [blame] | 172 | int err; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 173 | k = do_mkvalue(p_format, p_va); |
| 174 | if (k == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 175 | Py_DECREF(d); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | v = do_mkvalue(p_format, p_va); |
| 179 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 180 | Py_DECREF(k); |
| 181 | Py_DECREF(d); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
Guido van Rossum | db847bd | 1997-11-20 20:35:45 +0000 | [diff] [blame] | 184 | err = PyDict_SetItem(d, k, v); |
| 185 | Py_DECREF(k); |
| 186 | Py_DECREF(v); |
| 187 | if (err < 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 188 | Py_DECREF(d); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 189 | return NULL; |
| 190 | } |
| 191 | } |
| 192 | if (d != NULL && **p_format != endchar) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 193 | Py_DECREF(d); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 194 | d = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 195 | PyErr_SetString(PyExc_SystemError, |
| 196 | "Unmatched paren in format"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 197 | } |
| 198 | else if (endchar) |
| 199 | ++*p_format; |
| 200 | return d; |
| 201 | } |
| 202 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 203 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 204 | do_mklist(p_format, p_va, endchar, n) |
| 205 | char **p_format; |
| 206 | va_list *p_va; |
| 207 | int endchar; |
| 208 | int n; |
| 209 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 210 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 211 | int i; |
| 212 | if (n < 0) |
| 213 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 214 | if ((v = PyList_New(n)) == NULL) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 215 | return NULL; |
| 216 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 217 | PyObject *w = do_mkvalue(p_format, p_va); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 218 | if (w == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 219 | Py_DECREF(v); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 220 | return NULL; |
| 221 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 222 | PyList_SetItem(v, i, w); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 223 | } |
| 224 | if (v != NULL && **p_format != endchar) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 225 | Py_DECREF(v); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 226 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 227 | PyErr_SetString(PyExc_SystemError, |
| 228 | "Unmatched paren in format"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 229 | } |
| 230 | else if (endchar) |
| 231 | ++*p_format; |
| 232 | return v; |
| 233 | } |
| 234 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 235 | static PyObject * |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 236 | do_mktuple(p_format, p_va, endchar, n) |
| 237 | char **p_format; |
| 238 | va_list *p_va; |
| 239 | int endchar; |
| 240 | int n; |
| 241 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 242 | PyObject *v; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 243 | int i; |
| 244 | if (n < 0) |
| 245 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 246 | if ((v = PyTuple_New(n)) == NULL) |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 247 | return NULL; |
| 248 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 249 | PyObject *w = do_mkvalue(p_format, p_va); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 250 | if (w == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 251 | Py_DECREF(v); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 252 | return NULL; |
| 253 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 254 | PyTuple_SetItem(v, i, w); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 255 | } |
| 256 | if (v != NULL && **p_format != endchar) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 257 | Py_DECREF(v); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 258 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 259 | PyErr_SetString(PyExc_SystemError, |
| 260 | "Unmatched paren in format"); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 261 | } |
| 262 | else if (endchar) |
| 263 | ++*p_format; |
| 264 | return v; |
| 265 | } |
| 266 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 267 | static PyObject * |
Guido van Rossum | 899dcf3 | 1992-05-15 11:04:59 +0000 | [diff] [blame] | 268 | do_mkvalue(p_format, p_va) |
| 269 | char **p_format; |
| 270 | va_list *p_va; |
| 271 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 272 | for (;;) { |
| 273 | switch (*(*p_format)++) { |
| 274 | case '(': |
| 275 | return do_mktuple(p_format, p_va, ')', |
| 276 | countformat(*p_format, ')')); |
| 277 | |
| 278 | case '[': |
| 279 | return do_mklist(p_format, p_va, ']', |
| 280 | countformat(*p_format, ']')); |
| 281 | |
| 282 | case '{': |
| 283 | return do_mkdict(p_format, p_va, '}', |
| 284 | countformat(*p_format, '}')); |
| 285 | |
| 286 | case 'b': |
| 287 | case 'h': |
| 288 | case 'i': |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 289 | return PyInt_FromLong((long)va_arg(*p_va, int)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 290 | |
| 291 | case 'l': |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 292 | return PyInt_FromLong((long)va_arg(*p_va, long)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 294 | #if HAVE_LONG_LONG |
| 295 | case 'L': |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 296 | return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG)); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 297 | #endif |
| 298 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 299 | case 'f': |
| 300 | case 'd': |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 301 | return PyFloat_FromDouble( |
| 302 | (double)va_arg(*p_va, va_double)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 303 | |
| 304 | case 'c': |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 305 | { |
| 306 | char p[1]; |
| 307 | p[0] = va_arg(*p_va, int); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 308 | return PyString_FromStringAndSize(p, 1); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 309 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 310 | |
| 311 | case 's': |
| 312 | case 'z': |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 313 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 314 | PyObject *v; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 315 | char *str = va_arg(*p_va, char *); |
| 316 | int n; |
| 317 | if (**p_format == '#') { |
| 318 | ++*p_format; |
| 319 | n = va_arg(*p_va, int); |
| 320 | } |
| 321 | else |
| 322 | n = -1; |
| 323 | if (str == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 324 | v = Py_None; |
| 325 | Py_INCREF(v); |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 326 | } |
| 327 | else { |
| 328 | if (n < 0) |
| 329 | n = strlen(str); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 330 | v = PyString_FromStringAndSize(str, n); |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 331 | } |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 332 | return v; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 333 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 334 | |
| 335 | case 'S': |
| 336 | case 'O': |
Guido van Rossum | d1b9393 | 1995-01-20 16:56:02 +0000 | [diff] [blame] | 337 | if (**p_format == '&') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 338 | typedef PyObject *(*converter) Py_PROTO((void *)); |
Guido van Rossum | d1b9393 | 1995-01-20 16:56:02 +0000 | [diff] [blame] | 339 | converter func = va_arg(*p_va, converter); |
| 340 | void *arg = va_arg(*p_va, void *); |
| 341 | ++*p_format; |
| 342 | return (*func)(arg); |
| 343 | } |
| 344 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 345 | PyObject *v; |
| 346 | v = va_arg(*p_va, PyObject *); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 347 | if (v != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 348 | Py_INCREF(v); |
| 349 | else if (!PyErr_Occurred()) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 350 | /* If a NULL was passed |
| 351 | * because a call that should |
| 352 | * have constructed a value |
| 353 | * failed, that's OK, and we |
| 354 | * pass the error on; but if |
| 355 | * no error occurred it's not |
| 356 | * clear that the caller knew |
| 357 | * what she was doing. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 358 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 233f4b5 | 1998-07-07 22:32:19 +0000 | [diff] [blame] | 359 | "NULL object passed to Py_BuildValue"); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 360 | return v; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 361 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 362 | |
| 363 | case ':': |
| 364 | case ',': |
| 365 | case ' ': |
| 366 | case '\t': |
| 367 | break; |
| 368 | |
| 369 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 370 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 233f4b5 | 1998-07-07 22:32:19 +0000 | [diff] [blame] | 371 | "bad format char passed to Py_BuildValue"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 372 | return NULL; |
| 373 | |
| 374 | } |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 375 | } |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 378 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 379 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 380 | /* VARARGS 2 */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 381 | PyObject *Py_BuildValue(char *format, ...) |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 382 | #else |
| 383 | /* VARARGS */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 384 | PyObject *Py_BuildValue(va_alist) va_dcl |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 385 | #endif |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 386 | { |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 387 | va_list va; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 388 | PyObject* retval; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 389 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 390 | va_start(va, format); |
| 391 | #else |
| 392 | char *format; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 393 | va_start(va); |
| 394 | format = va_arg(va, char *); |
| 395 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 396 | retval = Py_VaBuildValue(format, va); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 397 | va_end(va); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 398 | return retval; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 399 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 400 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 401 | PyObject * |
| 402 | Py_VaBuildValue(format, va) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 403 | char *format; |
| 404 | va_list va; |
| 405 | { |
| 406 | char *f = format; |
| 407 | int n = countformat(f, '\0'); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 408 | va_list lva; |
| 409 | |
| 410 | #ifdef VA_LIST_IS_ARRAY |
| 411 | memcpy(lva, va, sizeof(va_list)); |
| 412 | #else |
| 413 | lva = va; |
| 414 | #endif |
| 415 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 416 | if (n < 0) |
| 417 | return NULL; |
| 418 | if (n == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 419 | Py_INCREF(Py_None); |
| 420 | return Py_None; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 421 | } |
| 422 | if (n == 1) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 423 | return do_mkvalue(&f, &lva); |
| 424 | return do_mktuple(&f, &lva, '\0', n); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 429 | PyObject * |
| 430 | PyEval_CallFunction(PyObject *obj, char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 431 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 432 | PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 433 | PyEval_CallFunction(obj, format, va_alist) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 434 | PyObject *obj; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 435 | char *format; |
| 436 | va_dcl |
| 437 | #endif |
| 438 | { |
| 439 | va_list vargs; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 440 | PyObject *args; |
| 441 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 442 | |
| 443 | #ifdef HAVE_STDARG_PROTOTYPES |
| 444 | va_start(vargs, format); |
| 445 | #else |
| 446 | va_start(vargs); |
| 447 | #endif |
| 448 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 449 | args = Py_VaBuildValue(format, vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 450 | va_end(vargs); |
| 451 | |
| 452 | if (args == NULL) |
| 453 | return NULL; |
| 454 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 455 | res = PyEval_CallObject(obj, args); |
| 456 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 457 | |
| 458 | return res; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | #ifdef HAVE_STDARG_PROTOTYPES |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 463 | PyObject * |
Guido van Rossum | c4099e6 | 1998-08-08 20:51:26 +0000 | [diff] [blame] | 464 | PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 465 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 466 | PyObject * |
Guido van Rossum | c4099e6 | 1998-08-08 20:51:26 +0000 | [diff] [blame] | 467 | PyEval_CallMethod(obj, methodname, format, va_alist) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 468 | PyObject *obj; |
Guido van Rossum | c4099e6 | 1998-08-08 20:51:26 +0000 | [diff] [blame] | 469 | char *methodname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 470 | char *format; |
| 471 | va_dcl |
| 472 | #endif |
| 473 | { |
| 474 | va_list vargs; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 475 | PyObject *meth; |
| 476 | PyObject *args; |
| 477 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | c4099e6 | 1998-08-08 20:51:26 +0000 | [diff] [blame] | 479 | meth = PyObject_GetAttrString(obj, methodname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 480 | if (meth == NULL) |
| 481 | return NULL; |
| 482 | |
| 483 | #ifdef HAVE_STDARG_PROTOTYPES |
| 484 | va_start(vargs, format); |
| 485 | #else |
| 486 | va_start(vargs); |
| 487 | #endif |
| 488 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 489 | args = Py_VaBuildValue(format, vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 490 | va_end(vargs); |
| 491 | |
| 492 | if (args == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 493 | Py_DECREF(meth); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 494 | return NULL; |
| 495 | } |
| 496 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 497 | res = PyEval_CallObject(meth, args); |
| 498 | Py_DECREF(meth); |
| 499 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 500 | |
| 501 | return res; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 502 | } |