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