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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6 | #define FLAG_SIZE_T 1 |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 7 | typedef double va_double; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 8 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9 | static PyObject *va_build_value(const char *, va_list, int); |
| 10 | |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 11 | /* Package context -- the full module name for package imports */ |
| 12 | char *_Py_PackageContext = NULL; |
| 13 | |
Guido van Rossum | bf80e54 | 1993-02-08 15:49:17 +0000 | [diff] [blame] | 14 | /* Helper for mkvalue() to scan the length of a format */ |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 15 | |
Fred Drake | ceead6d | 2003-01-30 15:08:25 +0000 | [diff] [blame] | 16 | static int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 17 | countformat(const char *format, int endchar) |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 18 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | int count = 0; |
| 20 | int level = 0; |
| 21 | while (level > 0 || *format != endchar) { |
| 22 | switch (*format) { |
| 23 | case '\0': |
| 24 | /* Premature end */ |
| 25 | PyErr_SetString(PyExc_SystemError, |
| 26 | "unmatched paren in format"); |
| 27 | return -1; |
| 28 | case '(': |
| 29 | case '[': |
| 30 | case '{': |
| 31 | if (level == 0) |
| 32 | count++; |
| 33 | level++; |
| 34 | break; |
| 35 | case ')': |
| 36 | case ']': |
| 37 | case '}': |
| 38 | level--; |
| 39 | break; |
| 40 | case '#': |
| 41 | case '&': |
| 42 | case ',': |
| 43 | case ':': |
| 44 | case ' ': |
| 45 | case '\t': |
| 46 | break; |
| 47 | default: |
| 48 | if (level == 0) |
| 49 | count++; |
| 50 | } |
| 51 | format++; |
| 52 | } |
| 53 | return count; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 57 | /* Generic function to create a value -- the inverse of getargs() */ |
| 58 | /* After an original idea and first implementation by Steven Miale */ |
| 59 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 60 | static PyObject *do_mktuple(const char**, va_list *, int, int, int); |
| 61 | static PyObject *do_mklist(const char**, va_list *, int, int, int); |
| 62 | static PyObject *do_mkdict(const char**, va_list *, int, int, int); |
| 63 | static PyObject *do_mkvalue(const char**, va_list *, int); |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 65 | |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 66 | static void |
| 67 | do_ignore(const char **p_format, va_list *p_va, int endchar, int n, int flags) |
| 68 | { |
| 69 | PyObject *v; |
| 70 | int i; |
| 71 | assert(PyErr_Occurred()); |
| 72 | v = PyTuple_New(n); |
| 73 | for (i = 0; i < n; i++) { |
| 74 | PyObject *exception, *value, *tb, *w; |
| 75 | |
| 76 | PyErr_Fetch(&exception, &value, &tb); |
| 77 | w = do_mkvalue(p_format, p_va, flags); |
| 78 | PyErr_Restore(exception, value, tb); |
| 79 | if (w != NULL) { |
| 80 | if (v != NULL) { |
| 81 | PyTuple_SET_ITEM(v, i, w); |
| 82 | } |
| 83 | else { |
| 84 | Py_DECREF(w); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | Py_XDECREF(v); |
| 89 | if (**p_format != endchar) { |
| 90 | PyErr_SetString(PyExc_SystemError, |
| 91 | "Unmatched paren in format"); |
| 92 | return; |
| 93 | } |
| 94 | if (endchar) |
| 95 | ++*p_format; |
| 96 | } |
| 97 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 98 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 99 | do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | PyObject *d; |
| 102 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | if (n < 0) |
| 104 | return NULL; |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 105 | if (n % 2) { |
| 106 | PyErr_SetString(PyExc_SystemError, |
| 107 | "Bad dict format"); |
| 108 | do_ignore(p_format, p_va, endchar, n, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | return NULL; |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 110 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | /* Note that we can't bail immediately on error as this will leak |
| 112 | refcounts on any 'N' arguments. */ |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 113 | if ((d = PyDict_New()) == NULL) { |
| 114 | do_ignore(p_format, p_va, endchar, n, flags); |
| 115 | return NULL; |
| 116 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | for (i = 0; i < n; i+= 2) { |
| 118 | PyObject *k, *v; |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | k = do_mkvalue(p_format, p_va, flags); |
| 121 | if (k == NULL) { |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 122 | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | Py_DECREF(d); |
| 124 | return NULL; |
| 125 | } |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 126 | v = do_mkvalue(p_format, p_va, flags); |
| 127 | if (v == NULL || PyDict_SetItem(d, k, v) < 0) { |
| 128 | do_ignore(p_format, p_va, endchar, n - i - 2, flags); |
| 129 | Py_DECREF(k); |
| 130 | Py_XDECREF(v); |
| 131 | Py_DECREF(d); |
| 132 | return NULL; |
| 133 | } |
| 134 | Py_DECREF(k); |
| 135 | Py_DECREF(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | } |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 137 | if (**p_format != endchar) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | PyErr_SetString(PyExc_SystemError, |
| 140 | "Unmatched paren in format"); |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 141 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | } |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 143 | if (endchar) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | ++*p_format; |
| 145 | return d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 148 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 149 | do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | PyObject *v; |
| 152 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | if (n < 0) |
| 154 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | /* Note that we can't bail immediately on error as this will leak |
| 156 | refcounts on any 'N' arguments. */ |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 157 | v = PyList_New(n); |
| 158 | if (v == NULL) { |
| 159 | do_ignore(p_format, p_va, endchar, n, flags); |
| 160 | return NULL; |
| 161 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | for (i = 0; i < n; i++) { |
| 163 | PyObject *w = do_mkvalue(p_format, p_va, flags); |
| 164 | if (w == NULL) { |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 165 | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
| 166 | Py_DECREF(v); |
| 167 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | } |
| 169 | PyList_SET_ITEM(v, i, w); |
| 170 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | if (**p_format != endchar) { |
| 172 | Py_DECREF(v); |
| 173 | PyErr_SetString(PyExc_SystemError, |
| 174 | "Unmatched paren in format"); |
| 175 | return NULL; |
| 176 | } |
| 177 | if (endchar) |
| 178 | ++*p_format; |
| 179 | return v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 182 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 183 | do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags) |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 184 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | PyObject *v; |
| 186 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | if (n < 0) |
| 188 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* Note that we can't bail immediately on error as this will leak |
| 190 | refcounts on any 'N' arguments. */ |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 191 | if ((v = PyTuple_New(n)) == NULL) { |
| 192 | do_ignore(p_format, p_va, endchar, n, flags); |
| 193 | return NULL; |
| 194 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 196 | PyObject *w = do_mkvalue(p_format, p_va, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | if (w == NULL) { |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 198 | do_ignore(p_format, p_va, endchar, n - i - 1, flags); |
| 199 | Py_DECREF(v); |
| 200 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | } |
| 202 | PyTuple_SET_ITEM(v, i, w); |
| 203 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | if (**p_format != endchar) { |
| 205 | Py_DECREF(v); |
| 206 | PyErr_SetString(PyExc_SystemError, |
| 207 | "Unmatched paren in format"); |
| 208 | return NULL; |
| 209 | } |
| 210 | if (endchar) |
| 211 | ++*p_format; |
| 212 | return v; |
Guido van Rossum | fc61adb | 1992-04-13 15:53:41 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 215 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 216 | do_mkvalue(const char **p_format, va_list *p_va, int flags) |
Guido van Rossum | 899dcf3 | 1992-05-15 11:04:59 +0000 | [diff] [blame] | 217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | for (;;) { |
| 219 | switch (*(*p_format)++) { |
| 220 | case '(': |
| 221 | return do_mktuple(p_format, p_va, ')', |
| 222 | countformat(*p_format, ')'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | case '[': |
| 225 | return do_mklist(p_format, p_va, ']', |
| 226 | countformat(*p_format, ']'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | case '{': |
| 229 | return do_mkdict(p_format, p_va, '}', |
| 230 | countformat(*p_format, '}'), flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | case 'b': |
| 233 | case 'B': |
| 234 | case 'h': |
| 235 | case 'i': |
| 236 | return PyLong_FromLong((long)va_arg(*p_va, int)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | case 'H': |
| 239 | return PyLong_FromLong((long)va_arg(*p_va, unsigned int)); |
| 240 | |
| 241 | case 'I': |
| 242 | { |
| 243 | unsigned int n; |
| 244 | n = va_arg(*p_va, unsigned int); |
| 245 | return PyLong_FromUnsignedLong(n); |
| 246 | } |
| 247 | |
| 248 | case 'n': |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 249 | #if SIZEOF_SIZE_T!=SIZEOF_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t)); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 251 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | /* Fall through from 'n' to 'l' if Py_ssize_t is long */ |
| 253 | case 'l': |
| 254 | return PyLong_FromLong(va_arg(*p_va, long)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | case 'k': |
| 257 | { |
| 258 | unsigned long n; |
| 259 | n = va_arg(*p_va, unsigned long); |
| 260 | return PyLong_FromUnsignedLong(n); |
| 261 | } |
Jack Jansen | dbd6503 | 2003-04-17 22:01:10 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 3dbba6e | 1999-01-25 21:48:56 +0000 | [diff] [blame] | 263 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | case 'L': |
| 265 | 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] | 266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | case 'K': |
| 268 | return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG)); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 269 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | case 'u': |
| 271 | { |
| 272 | PyObject *v; |
| 273 | Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *); |
| 274 | Py_ssize_t n; |
| 275 | if (**p_format == '#') { |
| 276 | ++*p_format; |
| 277 | if (flags & FLAG_SIZE_T) |
| 278 | n = va_arg(*p_va, Py_ssize_t); |
| 279 | else |
| 280 | n = va_arg(*p_va, int); |
| 281 | } |
| 282 | else |
| 283 | n = -1; |
| 284 | if (u == NULL) { |
| 285 | v = Py_None; |
| 286 | Py_INCREF(v); |
| 287 | } |
| 288 | else { |
| 289 | if (n < 0) |
Victor Stinner | d9c563e | 2011-09-29 04:01:43 +0200 | [diff] [blame] | 290 | n = Py_UNICODE_strlen(u); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | v = PyUnicode_FromUnicode(u, n); |
| 292 | } |
| 293 | return v; |
| 294 | } |
| 295 | case 'f': |
| 296 | case 'd': |
| 297 | return PyFloat_FromDouble( |
| 298 | (double)va_arg(*p_va, va_double)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | case 'D': |
| 301 | return PyComplex_FromCComplex( |
| 302 | *((Py_complex *)va_arg(*p_va, Py_complex *))); |
Fred Drake | aec7924 | 2001-03-12 21:03:26 +0000 | [diff] [blame] | 303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | case 'c': |
| 305 | { |
| 306 | char p[1]; |
| 307 | p[0] = (char)va_arg(*p_va, int); |
| 308 | return PyBytes_FromStringAndSize(p, 1); |
| 309 | } |
| 310 | case 'C': |
| 311 | { |
| 312 | int i = va_arg(*p_va, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | return PyUnicode_FromOrdinal(i); |
| 314 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | case 's': |
| 317 | case 'z': |
Victor Stinner | 7eeb5b5 | 2010-06-07 19:57:46 +0000 | [diff] [blame] | 318 | case 'U': /* XXX deprecated alias */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | { |
| 320 | PyObject *v; |
| 321 | char *str = va_arg(*p_va, char *); |
| 322 | Py_ssize_t n; |
| 323 | if (**p_format == '#') { |
| 324 | ++*p_format; |
| 325 | if (flags & FLAG_SIZE_T) |
| 326 | n = va_arg(*p_va, Py_ssize_t); |
| 327 | else |
| 328 | n = va_arg(*p_va, int); |
| 329 | } |
| 330 | else |
| 331 | n = -1; |
| 332 | if (str == NULL) { |
| 333 | v = Py_None; |
| 334 | Py_INCREF(v); |
| 335 | } |
| 336 | else { |
| 337 | if (n < 0) { |
| 338 | size_t m = strlen(str); |
| 339 | if (m > PY_SSIZE_T_MAX) { |
| 340 | PyErr_SetString(PyExc_OverflowError, |
| 341 | "string too long for Python string"); |
| 342 | return NULL; |
| 343 | } |
| 344 | n = (Py_ssize_t)m; |
| 345 | } |
| 346 | v = PyUnicode_FromStringAndSize(str, n); |
| 347 | } |
| 348 | return v; |
| 349 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | case 'y': |
| 352 | { |
| 353 | PyObject *v; |
| 354 | char *str = va_arg(*p_va, char *); |
| 355 | Py_ssize_t n; |
| 356 | if (**p_format == '#') { |
| 357 | ++*p_format; |
| 358 | if (flags & FLAG_SIZE_T) |
| 359 | n = va_arg(*p_va, Py_ssize_t); |
| 360 | else |
| 361 | n = va_arg(*p_va, int); |
| 362 | } |
| 363 | else |
| 364 | n = -1; |
| 365 | if (str == NULL) { |
| 366 | v = Py_None; |
| 367 | Py_INCREF(v); |
| 368 | } |
| 369 | else { |
| 370 | if (n < 0) { |
| 371 | size_t m = strlen(str); |
| 372 | if (m > PY_SSIZE_T_MAX) { |
| 373 | PyErr_SetString(PyExc_OverflowError, |
| 374 | "string too long for Python bytes"); |
| 375 | return NULL; |
| 376 | } |
| 377 | n = (Py_ssize_t)m; |
| 378 | } |
| 379 | v = PyBytes_FromStringAndSize(str, n); |
| 380 | } |
| 381 | return v; |
| 382 | } |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | case 'N': |
| 385 | case 'S': |
| 386 | case 'O': |
| 387 | if (**p_format == '&') { |
| 388 | typedef PyObject *(*converter)(void *); |
| 389 | converter func = va_arg(*p_va, converter); |
| 390 | void *arg = va_arg(*p_va, void *); |
| 391 | ++*p_format; |
| 392 | return (*func)(arg); |
| 393 | } |
| 394 | else { |
| 395 | PyObject *v; |
| 396 | v = va_arg(*p_va, PyObject *); |
| 397 | if (v != NULL) { |
| 398 | if (*(*p_format - 1) != 'N') |
| 399 | Py_INCREF(v); |
| 400 | } |
| 401 | else if (!PyErr_Occurred()) |
| 402 | /* If a NULL was passed |
| 403 | * because a call that should |
| 404 | * have constructed a value |
| 405 | * failed, that's OK, and we |
| 406 | * pass the error on; but if |
| 407 | * no error occurred it's not |
| 408 | * clear that the caller knew |
| 409 | * what she was doing. */ |
| 410 | PyErr_SetString(PyExc_SystemError, |
| 411 | "NULL object passed to Py_BuildValue"); |
| 412 | return v; |
| 413 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | case ':': |
| 416 | case ',': |
| 417 | case ' ': |
| 418 | case '\t': |
| 419 | break; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | default: |
| 422 | PyErr_SetString(PyExc_SystemError, |
| 423 | "bad format char passed to Py_BuildValue"); |
| 424 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 430 | |
Fred Drake | ceead6d | 2003-01-30 15:08:25 +0000 | [diff] [blame] | 431 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 432 | Py_BuildValue(const char *format, ...) |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 433 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | va_list va; |
| 435 | PyObject* retval; |
| 436 | va_start(va, format); |
| 437 | retval = va_build_value(format, va, 0); |
| 438 | va_end(va); |
| 439 | return retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | PyObject * |
| 443 | _Py_BuildValue_SizeT(const char *format, ...) |
| 444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | va_list va; |
| 446 | PyObject* retval; |
| 447 | va_start(va, format); |
| 448 | retval = va_build_value(format, va, FLAG_SIZE_T); |
| 449 | va_end(va); |
| 450 | return retval; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 451 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 452 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 453 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 454 | Py_VaBuildValue(const char *format, va_list va) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | return va_build_value(format, va, 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | PyObject * |
| 460 | _Py_VaBuildValue_SizeT(const char *format, va_list va) |
| 461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | return va_build_value(format, va, FLAG_SIZE_T); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static PyObject * |
| 466 | va_build_value(const char *format, va_list va, int flags) |
| 467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | const char *f = format; |
| 469 | int n = countformat(f, '\0'); |
| 470 | va_list lva; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 471 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 472 | Py_VA_COPY(lva, va); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | if (n < 0) |
| 475 | return NULL; |
| 476 | if (n == 0) { |
| 477 | Py_INCREF(Py_None); |
| 478 | return Py_None; |
| 479 | } |
| 480 | if (n == 1) |
| 481 | return do_mkvalue(&f, &lva, flags); |
| 482 | return do_mktuple(&f, &lva, '\0', n, flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 486 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 487 | PyEval_CallFunction(PyObject *obj, const char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | va_list vargs; |
| 490 | PyObject *args; |
| 491 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | va_start(vargs, format); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | args = Py_VaBuildValue(format, vargs); |
| 496 | va_end(vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | if (args == NULL) |
| 499 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | res = PyEval_CallObject(obj, args); |
| 502 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | return res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 508 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 509 | PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | va_list vargs; |
| 512 | PyObject *meth; |
| 513 | PyObject *args; |
| 514 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | meth = PyObject_GetAttrString(obj, methodname); |
| 517 | if (meth == NULL) |
| 518 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | va_start(vargs, format); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | args = Py_VaBuildValue(format, vargs); |
| 523 | va_end(vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | if (args == NULL) { |
| 526 | Py_DECREF(meth); |
| 527 | return NULL; |
| 528 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 529 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | res = PyEval_CallObject(meth, args); |
| 531 | Py_DECREF(meth); |
| 532 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | return res; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 535 | } |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 536 | |
| 537 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 538 | PyModule_AddObject(PyObject *m, const char *name, PyObject *o) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | PyObject *dict; |
| 541 | if (!PyModule_Check(m)) { |
| 542 | PyErr_SetString(PyExc_TypeError, |
| 543 | "PyModule_AddObject() needs module as first arg"); |
| 544 | return -1; |
| 545 | } |
| 546 | if (!o) { |
| 547 | if (!PyErr_Occurred()) |
| 548 | PyErr_SetString(PyExc_TypeError, |
| 549 | "PyModule_AddObject() needs non-NULL value"); |
| 550 | return -1; |
| 551 | } |
Jeremy Hylton | c44dbc4 | 2003-06-21 21:35:25 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | dict = PyModule_GetDict(m); |
| 554 | if (dict == NULL) { |
| 555 | /* Internal error -- modules must have a dict! */ |
| 556 | PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__", |
| 557 | PyModule_GetName(m)); |
| 558 | return -1; |
| 559 | } |
| 560 | if (PyDict_SetItemString(dict, name, o)) |
| 561 | return -1; |
| 562 | Py_DECREF(o); |
| 563 | return 0; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 567 | PyModule_AddIntConstant(PyObject *m, const char *name, long value) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 568 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | PyObject *o = PyLong_FromLong(value); |
| 570 | if (!o) |
| 571 | return -1; |
| 572 | if (PyModule_AddObject(m, name, o) == 0) |
| 573 | return 0; |
| 574 | Py_DECREF(o); |
| 575 | return -1; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 579 | PyModule_AddStringConstant(PyObject *m, const char *name, const char *value) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | PyObject *o = PyUnicode_FromString(value); |
| 582 | if (!o) |
| 583 | return -1; |
| 584 | if (PyModule_AddObject(m, name, o) == 0) |
| 585 | return 0; |
| 586 | Py_DECREF(o); |
| 587 | return -1; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 588 | } |