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