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