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