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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | case 'L': |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 264 | return PyLong_FromLongLong((long long)va_arg(*p_va, long long)); |
Jack Jansen | dbd6503 | 2003-04-17 22:01:10 +0000 | [diff] [blame] | 265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | case 'K': |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 267 | return PyLong_FromUnsignedLongLong((long long)va_arg(*p_va, unsigned long long)); |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | case 'u': |
| 270 | { |
| 271 | PyObject *v; |
| 272 | Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *); |
| 273 | Py_ssize_t n; |
| 274 | if (**p_format == '#') { |
| 275 | ++*p_format; |
| 276 | if (flags & FLAG_SIZE_T) |
| 277 | n = va_arg(*p_va, Py_ssize_t); |
| 278 | else |
| 279 | n = va_arg(*p_va, int); |
| 280 | } |
| 281 | else |
| 282 | n = -1; |
| 283 | if (u == NULL) { |
| 284 | v = Py_None; |
| 285 | Py_INCREF(v); |
| 286 | } |
| 287 | else { |
| 288 | if (n < 0) |
Victor Stinner | d9c563e | 2011-09-29 04:01:43 +0200 | [diff] [blame] | 289 | n = Py_UNICODE_strlen(u); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | v = PyUnicode_FromUnicode(u, n); |
| 291 | } |
| 292 | return v; |
| 293 | } |
| 294 | case 'f': |
| 295 | case 'd': |
| 296 | return PyFloat_FromDouble( |
| 297 | (double)va_arg(*p_va, va_double)); |
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 'D': |
| 300 | return PyComplex_FromCComplex( |
| 301 | *((Py_complex *)va_arg(*p_va, Py_complex *))); |
Fred Drake | aec7924 | 2001-03-12 21:03:26 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | case 'c': |
| 304 | { |
| 305 | char p[1]; |
| 306 | p[0] = (char)va_arg(*p_va, int); |
| 307 | return PyBytes_FromStringAndSize(p, 1); |
| 308 | } |
| 309 | case 'C': |
| 310 | { |
| 311 | int i = va_arg(*p_va, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | return PyUnicode_FromOrdinal(i); |
| 313 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | case 's': |
| 316 | case 'z': |
Victor Stinner | 7eeb5b5 | 2010-06-07 19:57:46 +0000 | [diff] [blame] | 317 | case 'U': /* XXX deprecated alias */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | { |
| 319 | PyObject *v; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 320 | const char *str = va_arg(*p_va, const char *); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | Py_ssize_t n; |
| 322 | if (**p_format == '#') { |
| 323 | ++*p_format; |
| 324 | if (flags & FLAG_SIZE_T) |
| 325 | n = va_arg(*p_va, Py_ssize_t); |
| 326 | else |
| 327 | n = va_arg(*p_va, int); |
| 328 | } |
| 329 | else |
| 330 | n = -1; |
| 331 | if (str == NULL) { |
| 332 | v = Py_None; |
| 333 | Py_INCREF(v); |
| 334 | } |
| 335 | else { |
| 336 | if (n < 0) { |
| 337 | size_t m = strlen(str); |
| 338 | if (m > PY_SSIZE_T_MAX) { |
| 339 | PyErr_SetString(PyExc_OverflowError, |
| 340 | "string too long for Python string"); |
| 341 | return NULL; |
| 342 | } |
| 343 | n = (Py_ssize_t)m; |
| 344 | } |
| 345 | v = PyUnicode_FromStringAndSize(str, n); |
| 346 | } |
| 347 | return v; |
| 348 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | case 'y': |
| 351 | { |
| 352 | PyObject *v; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 353 | const char *str = va_arg(*p_va, const char *); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | Py_ssize_t n; |
| 355 | if (**p_format == '#') { |
| 356 | ++*p_format; |
| 357 | if (flags & FLAG_SIZE_T) |
| 358 | n = va_arg(*p_va, Py_ssize_t); |
| 359 | else |
| 360 | n = va_arg(*p_va, int); |
| 361 | } |
| 362 | else |
| 363 | n = -1; |
| 364 | if (str == NULL) { |
| 365 | v = Py_None; |
| 366 | Py_INCREF(v); |
| 367 | } |
| 368 | else { |
| 369 | if (n < 0) { |
| 370 | size_t m = strlen(str); |
| 371 | if (m > PY_SSIZE_T_MAX) { |
| 372 | PyErr_SetString(PyExc_OverflowError, |
| 373 | "string too long for Python bytes"); |
| 374 | return NULL; |
| 375 | } |
| 376 | n = (Py_ssize_t)m; |
| 377 | } |
| 378 | v = PyBytes_FromStringAndSize(str, n); |
| 379 | } |
| 380 | return v; |
| 381 | } |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | case 'N': |
| 384 | case 'S': |
| 385 | case 'O': |
| 386 | if (**p_format == '&') { |
| 387 | typedef PyObject *(*converter)(void *); |
| 388 | converter func = va_arg(*p_va, converter); |
| 389 | void *arg = va_arg(*p_va, void *); |
| 390 | ++*p_format; |
| 391 | return (*func)(arg); |
| 392 | } |
| 393 | else { |
| 394 | PyObject *v; |
| 395 | v = va_arg(*p_va, PyObject *); |
| 396 | if (v != NULL) { |
| 397 | if (*(*p_format - 1) != 'N') |
| 398 | Py_INCREF(v); |
| 399 | } |
| 400 | else if (!PyErr_Occurred()) |
| 401 | /* If a NULL was passed |
| 402 | * because a call that should |
| 403 | * have constructed a value |
| 404 | * failed, that's OK, and we |
| 405 | * pass the error on; but if |
| 406 | * no error occurred it's not |
| 407 | * clear that the caller knew |
| 408 | * what she was doing. */ |
| 409 | PyErr_SetString(PyExc_SystemError, |
| 410 | "NULL object passed to Py_BuildValue"); |
| 411 | return v; |
| 412 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | case ':': |
| 415 | case ',': |
| 416 | case ' ': |
| 417 | case '\t': |
| 418 | break; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | default: |
| 421 | PyErr_SetString(PyExc_SystemError, |
| 422 | "bad format char passed to Py_BuildValue"); |
| 423 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 429 | |
Fred Drake | ceead6d | 2003-01-30 15:08:25 +0000 | [diff] [blame] | 430 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 431 | Py_BuildValue(const char *format, ...) |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | va_list va; |
| 434 | PyObject* retval; |
| 435 | va_start(va, format); |
| 436 | retval = va_build_value(format, va, 0); |
| 437 | va_end(va); |
| 438 | return retval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | PyObject * |
| 442 | _Py_BuildValue_SizeT(const char *format, ...) |
| 443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | va_list va; |
| 445 | PyObject* retval; |
| 446 | va_start(va, format); |
| 447 | retval = va_build_value(format, va, FLAG_SIZE_T); |
| 448 | va_end(va); |
| 449 | return retval; |
Guido van Rossum | 3cfe6fa | 1992-04-13 10:48:55 +0000 | [diff] [blame] | 450 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 451 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 452 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 453 | Py_VaBuildValue(const char *format, va_list va) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | return va_build_value(format, va, 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | PyObject * |
| 459 | _Py_VaBuildValue_SizeT(const char *format, va_list va) |
| 460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | return va_build_value(format, va, FLAG_SIZE_T); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static PyObject * |
| 465 | va_build_value(const char *format, va_list va, int flags) |
| 466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | const char *f = format; |
| 468 | int n = countformat(f, '\0'); |
| 469 | va_list lva; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 470 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 471 | Py_VA_COPY(lva, va); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | if (n < 0) |
| 474 | return NULL; |
| 475 | if (n == 0) { |
| 476 | Py_INCREF(Py_None); |
| 477 | return Py_None; |
| 478 | } |
| 479 | if (n == 1) |
| 480 | return do_mkvalue(&f, &lva, flags); |
| 481 | return do_mktuple(&f, &lva, '\0', n, flags); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 485 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 486 | PyEval_CallFunction(PyObject *obj, const char *format, ...) |
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 | va_list vargs; |
| 489 | PyObject *args; |
| 490 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | va_start(vargs, 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 | args = Py_VaBuildValue(format, vargs); |
| 495 | va_end(vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | if (args == NULL) |
| 498 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | res = PyEval_CallObject(obj, args); |
| 501 | Py_DECREF(args); |
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 | return res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 507 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 508 | PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | va_list vargs; |
| 511 | PyObject *meth; |
| 512 | PyObject *args; |
| 513 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | meth = PyObject_GetAttrString(obj, methodname); |
| 516 | if (meth == NULL) |
| 517 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | va_start(vargs, format); |
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 | args = Py_VaBuildValue(format, vargs); |
| 522 | va_end(vargs); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | if (args == NULL) { |
| 525 | Py_DECREF(meth); |
| 526 | return NULL; |
| 527 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | res = PyEval_CallObject(meth, args); |
| 530 | Py_DECREF(meth); |
| 531 | Py_DECREF(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | return res; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 534 | } |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 535 | |
| 536 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 537 | PyModule_AddObject(PyObject *m, const char *name, PyObject *o) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | PyObject *dict; |
| 540 | if (!PyModule_Check(m)) { |
| 541 | PyErr_SetString(PyExc_TypeError, |
| 542 | "PyModule_AddObject() needs module as first arg"); |
| 543 | return -1; |
| 544 | } |
| 545 | if (!o) { |
| 546 | if (!PyErr_Occurred()) |
| 547 | PyErr_SetString(PyExc_TypeError, |
| 548 | "PyModule_AddObject() needs non-NULL value"); |
| 549 | return -1; |
| 550 | } |
Jeremy Hylton | c44dbc4 | 2003-06-21 21:35:25 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | dict = PyModule_GetDict(m); |
| 553 | if (dict == NULL) { |
| 554 | /* Internal error -- modules must have a dict! */ |
| 555 | PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__", |
| 556 | PyModule_GetName(m)); |
| 557 | return -1; |
| 558 | } |
| 559 | if (PyDict_SetItemString(dict, name, o)) |
| 560 | return -1; |
| 561 | Py_DECREF(o); |
| 562 | return 0; |
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_AddIntConstant(PyObject *m, const char *name, long 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 = PyLong_FromLong(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 | } |
| 576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 578 | PyModule_AddStringConstant(PyObject *m, const char *name, const char *value) |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | PyObject *o = PyUnicode_FromString(value); |
| 581 | if (!o) |
| 582 | return -1; |
| 583 | if (PyModule_AddObject(m, name, o) == 0) |
| 584 | return 0; |
| 585 | Py_DECREF(o); |
| 586 | return -1; |
Fred Drake | 9e28515 | 2000-09-23 03:24:27 +0000 | [diff] [blame] | 587 | } |