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