comparison Python/modsupport.c @ 61465:e38335d4df91 2.6

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