Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(169990)

Delta Between Two Patch Sets: Python/bltinmodule.c

Issue 23722: During metaclass.__init__, super() of the constructed class does not work
Left Patch Set: Created 4 years, 9 months ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Objects/typeobject.c ('k') | Python/compile.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* Built-in functions */ 1 /* Built-in functions */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 #include "Python-ast.h" 4 #include "Python-ast.h"
5 5
6 #include "node.h" 6 #include "node.h"
7 #include "code.h" 7 #include "code.h"
8 8
9 #include "asdl.h" 9 #include "asdl.h"
10 #include "ast.h" 10 #include "ast.h"
11 11
12 #include <ctype.h> 12 #include <ctype.h>
13 13
14 #ifdef HAVE_LANGINFO_H 14 #ifdef HAVE_LANGINFO_H
15 #include <langinfo.h> /* CODESET */ 15 #include <langinfo.h> /* CODESET */
16 #endif 16 #endif
17 17
18 /* The default encoding used by the platform file system APIs 18 /* The default encoding used by the platform file system APIs
19 Can remain NULL for all platforms that don't have such a concept 19 Can remain NULL for all platforms that don't have such a concept
20 20
21 Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the 21 Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the
22 values for Py_FileSystemDefaultEncoding! 22 values for Py_FileSystemDefaultEncoding!
23 */ 23 */
24 #ifdef HAVE_MBCS 24 #if defined(__APPLE__)
25 const char *Py_FileSystemDefaultEncoding = "mbcs"; 25 const char *Py_FileSystemDefaultEncoding = "utf-8";
26 int Py_HasFileSystemDefaultEncoding = 1; 26 int Py_HasFileSystemDefaultEncoding = 1;
27 #elif defined(__APPLE__) 27 #elif defined(MS_WINDOWS)
28 /* may be changed by initfsencoding(), but should never be free()d */
28 const char *Py_FileSystemDefaultEncoding = "utf-8"; 29 const char *Py_FileSystemDefaultEncoding = "utf-8";
29 int Py_HasFileSystemDefaultEncoding = 1; 30 int Py_HasFileSystemDefaultEncoding = 1;
30 #else 31 #else
31 const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */ 32 const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */
32 int Py_HasFileSystemDefaultEncoding = 0; 33 int Py_HasFileSystemDefaultEncoding = 0;
33 #endif 34 #endif
35 const char *Py_FileSystemDefaultEncodeErrors = "surrogateescape";
34 36
35 _Py_IDENTIFIER(__builtins__); 37 _Py_IDENTIFIER(__builtins__);
36 _Py_IDENTIFIER(__dict__); 38 _Py_IDENTIFIER(__dict__);
37 _Py_IDENTIFIER(__prepare__); 39 _Py_IDENTIFIER(__prepare__);
38 _Py_IDENTIFIER(__round__); 40 _Py_IDENTIFIER(__round__);
39 _Py_IDENTIFIER(encoding); 41 _Py_IDENTIFIER(encoding);
40 _Py_IDENTIFIER(errors); 42 _Py_IDENTIFIER(errors);
41 _Py_IDENTIFIER(fileno); 43 _Py_IDENTIFIER(fileno);
42 _Py_IDENTIFIER(flush); 44 _Py_IDENTIFIER(flush);
43 _Py_IDENTIFIER(metaclass); 45 _Py_IDENTIFIER(metaclass);
44 _Py_IDENTIFIER(sort); 46 _Py_IDENTIFIER(sort);
45 _Py_IDENTIFIER(stdin); 47 _Py_IDENTIFIER(stdin);
46 _Py_IDENTIFIER(stdout); 48 _Py_IDENTIFIER(stdout);
47 _Py_IDENTIFIER(stderr); 49 _Py_IDENTIFIER(stderr);
48 50
51 #include "clinic/bltinmodule.c.h"
52
49 /* AC: cannot convert yet, waiting for *args support */ 53 /* AC: cannot convert yet, waiting for *args support */
50 static PyObject * 54 static PyObject *
51 builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) 55 builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
52 { 56 {
53 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *cell; 57 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns;
54 PyObject *cls = NULL; 58 PyObject *cls = NULL, *cell = NULL;
55 Py_ssize_t nargs; 59 Py_ssize_t nargs;
56 int isclass = 0; /* initialize to prevent gcc warning */ 60 int isclass = 0; /* initialize to prevent gcc warning */
57 61
58 assert(args != NULL); 62 assert(args != NULL);
59 if (!PyTuple_Check(args)) { 63 if (!PyTuple_Check(args)) {
60 PyErr_SetString(PyExc_TypeError, 64 PyErr_SetString(PyExc_TypeError,
61 "__build_class__: args is not a tuple"); 65 "__build_class__: args is not a tuple");
62 return NULL; 66 return NULL;
63 } 67 }
64 nargs = PyTuple_GET_SIZE(args); 68 nargs = PyTuple_GET_SIZE(args);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 ns = PyDict_New(); 150 ns = PyDict_New();
147 } 151 }
148 else { 152 else {
149 Py_DECREF(meta); 153 Py_DECREF(meta);
150 Py_XDECREF(mkw); 154 Py_XDECREF(mkw);
151 Py_DECREF(bases); 155 Py_DECREF(bases);
152 return NULL; 156 return NULL;
153 } 157 }
154 } 158 }
155 else { 159 else {
156 PyObject *pargs = PyTuple_Pack(2, name, bases); 160 PyObject *pargs[2] = {name, bases};
157 if (pargs == NULL) { 161 ns = _PyObject_FastCallDict(prep, pargs, 2, mkw);
158 Py_DECREF(prep);
159 Py_DECREF(meta);
160 Py_XDECREF(mkw);
161 Py_DECREF(bases);
162 return NULL;
163 }
164 ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
165 Py_DECREF(pargs);
166 Py_DECREF(prep); 162 Py_DECREF(prep);
167 } 163 }
168 if (ns == NULL) { 164 if (ns == NULL) {
169 Py_DECREF(meta); 165 Py_DECREF(meta);
170 Py_XDECREF(mkw); 166 Py_XDECREF(mkw);
171 Py_DECREF(bases); 167 Py_DECREF(bases);
172 return NULL; 168 return NULL;
173 } 169 }
174 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(f unc), ns, 170 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(f unc), ns,
175 NULL, 0, NULL, 0, NULL, 0, NULL, 171 NULL, 0, NULL, 0, NULL, 0, NULL,
176 PyFunction_GET_CLOSURE(func)); 172 PyFunction_GET_CLOSURE(func));
177 if (cell != NULL) { 173 if (cell != NULL) {
178 PyObject *margs; 174 PyObject *margs[3] = {name, bases, ns};
179 margs = PyTuple_Pack(3, name, bases, ns); 175 cls = _PyObject_FastCallDict(meta, margs, 3, mkw);
180 if (margs != NULL) { 176 if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
181 cls = PyEval_CallObjectWithKeywords(meta, margs, mkw); 177 PyObject *cell_cls = PyCell_GET(cell);
182 Py_DECREF(margs); 178 if (cell_cls != cls) {
183 } 179 /* TODO: In 3.7, the deprecation warning becomes a RuntimeError
storchaka 2016/12/04 14:10:59 A dot is needed since the next line is a different
184 Py_DECREF(cell); 180 * At that point, cell_error won't be needed.
185 } 181 */
182 int cell_error;
183 if (cell_cls == NULL) {
184 const char *msg =
185 "__class__ not set defining %.200R as %.200R. "
186 "Was __classcell__ propagated to type.__new__?";
187 cell_error = PyErr_WarnFormat(
188 PyExc_DeprecationWarning, 1, msg, name, cls);
189 } else {
190 const char *msg =
191 "__class__ set to %.200R defining %.200R as %.200R.";
storchaka 2016/12/04 14:10:59 What about the ending dot? It usually is not used
192 PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
193 cell_error = 1;
194 }
195 if (cell_error) {
196 Py_DECREF(cls);
197 cls = NULL;
198 goto error;
199 } else {
200 /* Fill in the cell, since type.__new__ didn't do it */
201 PyCell_Set(cell, cls);
202 }
203 }
204 }
205 }
206 error:
207 Py_XDECREF(cell);
186 Py_DECREF(ns); 208 Py_DECREF(ns);
187 Py_DECREF(meta); 209 Py_DECREF(meta);
188 Py_XDECREF(mkw); 210 Py_XDECREF(mkw);
189 Py_DECREF(bases); 211 Py_DECREF(bases);
190 return cls; 212 return cls;
191 } 213 }
192 214
193 PyDoc_STRVAR(build_class_doc, 215 PyDoc_STRVAR(build_class_doc,
194 "__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\ 216 "__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
195 \n\ 217 \n\
(...skipping 28 matching lines...) Expand all
224 When importing a module from a package, note that __import__('A.B', ...)\n\ 246 When importing a module from a package, note that __import__('A.B', ...)\n\
225 returns package A when fromlist is empty, but its submodule B when\n\ 247 returns package A when fromlist is empty, but its submodule B when\n\
226 fromlist is not empty. Level is used to determine whether to perform \n\ 248 fromlist is not empty. Level is used to determine whether to perform \n\
227 absolute or relative imports. 0 is absolute while a positive number\n\ 249 absolute or relative imports. 0 is absolute while a positive number\n\
228 is the number of parent directories to search relative to the current module."); 250 is the number of parent directories to search relative to the current module.");
229 251
230 252
231 /*[clinic input] 253 /*[clinic input]
232 abs as builtin_abs 254 abs as builtin_abs
233 255
234 x: 'O' 256 x: object
235 / 257 /
236 258
237 Return the absolute value of the argument. 259 Return the absolute value of the argument.
238 [clinic start generated code]*/ 260 [clinic start generated code]*/
239 261
240 PyDoc_STRVAR(builtin_abs__doc__, 262 static PyObject *
241 "abs($module, x, /)\n" 263 builtin_abs(PyObject *module, PyObject *x)
242 "--\n" 264 /*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
243 "\n"
244 "Return the absolute value of the argument.");
245
246 #define BUILTIN_ABS_METHODDEF \
247 {"abs", (PyCFunction)builtin_abs, METH_O, builtin_abs__doc__},
248
249 static PyObject *
250 builtin_abs(PyModuleDef *module, PyObject *x)
251 /*[clinic end generated code: output=f85095528ce7e2e5 input=aa29cc07869b4732]*/
252 { 265 {
253 return PyNumber_Absolute(x); 266 return PyNumber_Absolute(x);
254 } 267 }
255 268
256 /*[clinic input] 269 /*[clinic input]
257 all as builtin_all 270 all as builtin_all
258 271
259 iterable: 'O' 272 iterable: object
260 / 273 /
261 274
262 Return True if bool(x) is True for all values x in the iterable. 275 Return True if bool(x) is True for all values x in the iterable.
263 276
264 If the iterable is empty, return True. 277 If the iterable is empty, return True.
265 [clinic start generated code]*/ 278 [clinic start generated code]*/
266 279
267 PyDoc_STRVAR(builtin_all__doc__, 280 static PyObject *
268 "all($module, iterable, /)\n" 281 builtin_all(PyObject *module, PyObject *iterable)
269 "--\n" 282 /*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
270 "\n"
271 "Return True if bool(x) is True for all values x in the iterable.\n"
272 "\n"
273 "If the iterable is empty, return True.");
274
275 #define BUILTIN_ALL_METHODDEF \
276 {"all", (PyCFunction)builtin_all, METH_O, builtin_all__doc__},
277
278 static PyObject *
279 builtin_all(PyModuleDef *module, PyObject *iterable)
280 /*[clinic end generated code: output=d001db739ba83b46 input=dd506dc9998d42bd]*/
281 { 283 {
282 PyObject *it, *item; 284 PyObject *it, *item;
283 PyObject *(*iternext)(PyObject *); 285 PyObject *(*iternext)(PyObject *);
284 int cmp; 286 int cmp;
285 287
286 it = PyObject_GetIter(iterable); 288 it = PyObject_GetIter(iterable);
287 if (it == NULL) 289 if (it == NULL)
288 return NULL; 290 return NULL;
289 iternext = *Py_TYPE(it)->tp_iternext; 291 iternext = *Py_TYPE(it)->tp_iternext;
290 292
(...skipping 18 matching lines...) Expand all
309 PyErr_Clear(); 311 PyErr_Clear();
310 else 312 else
311 return NULL; 313 return NULL;
312 } 314 }
313 Py_RETURN_TRUE; 315 Py_RETURN_TRUE;
314 } 316 }
315 317
316 /*[clinic input] 318 /*[clinic input]
317 any as builtin_any 319 any as builtin_any
318 320
319 iterable: 'O' 321 iterable: object
320 / 322 /
321 323
322 Return True if bool(x) is True for any x in the iterable. 324 Return True if bool(x) is True for any x in the iterable.
323 325
324 If the iterable is empty, return False. 326 If the iterable is empty, return False.
325 [clinic start generated code]*/ 327 [clinic start generated code]*/
326 328
327 PyDoc_STRVAR(builtin_any__doc__, 329 static PyObject *
328 "any($module, iterable, /)\n" 330 builtin_any(PyObject *module, PyObject *iterable)
329 "--\n" 331 /*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/
330 "\n"
331 "Return True if bool(x) is True for any x in the iterable.\n"
332 "\n"
333 "If the iterable is empty, return False.");
334
335 #define BUILTIN_ANY_METHODDEF \
336 {"any", (PyCFunction)builtin_any, METH_O, builtin_any__doc__},
337
338 static PyObject *
339 builtin_any(PyModuleDef *module, PyObject *iterable)
340 /*[clinic end generated code: output=3a4b6dbe6a0d6f61 input=8fe8460f3fbbced8]*/
341 { 332 {
342 PyObject *it, *item; 333 PyObject *it, *item;
343 PyObject *(*iternext)(PyObject *); 334 PyObject *(*iternext)(PyObject *);
344 int cmp; 335 int cmp;
345 336
346 it = PyObject_GetIter(iterable); 337 it = PyObject_GetIter(iterable);
347 if (it == NULL) 338 if (it == NULL)
348 return NULL; 339 return NULL;
349 iternext = *Py_TYPE(it)->tp_iternext; 340 iternext = *Py_TYPE(it)->tp_iternext;
350 341
351 for (;;) { 342 for (;;) {
352 item = iternext(it); 343 item = iternext(it);
353 if (item == NULL) 344 if (item == NULL)
354 break; 345 break;
355 cmp = PyObject_IsTrue(item); 346 cmp = PyObject_IsTrue(item);
356 Py_DECREF(item); 347 Py_DECREF(item);
357 if (cmp < 0) { 348 if (cmp < 0) {
358 Py_DECREF(it); 349 Py_DECREF(it);
359 return NULL; 350 return NULL;
360 } 351 }
361 if (cmp == 1) { 352 if (cmp > 0) {
362 Py_DECREF(it); 353 Py_DECREF(it);
363 Py_RETURN_TRUE; 354 Py_RETURN_TRUE;
364 } 355 }
365 } 356 }
366 Py_DECREF(it); 357 Py_DECREF(it);
367 if (PyErr_Occurred()) { 358 if (PyErr_Occurred()) {
368 if (PyErr_ExceptionMatches(PyExc_StopIteration)) 359 if (PyErr_ExceptionMatches(PyExc_StopIteration))
369 PyErr_Clear(); 360 PyErr_Clear();
370 else 361 else
371 return NULL; 362 return NULL;
372 } 363 }
373 Py_RETURN_FALSE; 364 Py_RETURN_FALSE;
374 } 365 }
375 366
376 /*[clinic input] 367 /*[clinic input]
377 ascii as builtin_ascii 368 ascii as builtin_ascii
378 369
379 obj: 'O' 370 obj: object
380 / 371 /
381 372
382 Return an ASCII-only representation of an object. 373 Return an ASCII-only representation of an object.
383 374
384 As repr(), return a string containing a printable representation of an 375 As repr(), return a string containing a printable representation of an
385 object, but escape the non-ASCII characters in the string returned by 376 object, but escape the non-ASCII characters in the string returned by
386 repr() using \\x, \\u or \\U escapes. This generates a string similar 377 repr() using \\x, \\u or \\U escapes. This generates a string similar
387 to that returned by repr() in Python 2. 378 to that returned by repr() in Python 2.
388 [clinic start generated code]*/ 379 [clinic start generated code]*/
389 380
390 PyDoc_STRVAR(builtin_ascii__doc__, 381 static PyObject *
391 "ascii($module, obj, /)\n" 382 builtin_ascii(PyObject *module, PyObject *obj)
392 "--\n" 383 /*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
393 "\n"
394 "Return an ASCII-only representation of an object.\n"
395 "\n"
396 "As repr(), return a string containing a printable representation of an\n"
397 "object, but escape the non-ASCII characters in the string returned by\n"
398 "repr() using \\\\x, \\\\u or \\\\U escapes. This generates a string similar\n"
399 "to that returned by repr() in Python 2.");
400
401 #define BUILTIN_ASCII_METHODDEF \
402 {"ascii", (PyCFunction)builtin_ascii, METH_O, builtin_ascii__doc__},
403
404 static PyObject *
405 builtin_ascii(PyModuleDef *module, PyObject *obj)
406 /*[clinic end generated code: output=f0e6754154c2d30b input=0cbdc1420a306325]*/
407 { 384 {
408 return PyObject_ASCII(obj); 385 return PyObject_ASCII(obj);
409 } 386 }
410 387
411 388
412 /*[clinic input] 389 /*[clinic input]
413 bin as builtin_bin 390 bin as builtin_bin
414 391
415 number: 'O' 392 number: object
416 / 393 /
417 394
418 Return the binary representation of an integer. 395 Return the binary representation of an integer.
419 396
420 >>> bin(2796202) 397 >>> bin(2796202)
421 '0b1010101010101010101010' 398 '0b1010101010101010101010'
422 [clinic start generated code]*/ 399 [clinic start generated code]*/
423 400
424 PyDoc_STRVAR(builtin_bin__doc__, 401 static PyObject *
425 "bin($module, number, /)\n" 402 builtin_bin(PyObject *module, PyObject *number)
426 "--\n" 403 /*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
427 "\n"
428 "Return the binary representation of an integer.\n"
429 "\n"
430 " >>> bin(2796202)\n"
431 " \'0b1010101010101010101010\'");
432
433 #define BUILTIN_BIN_METHODDEF \
434 {"bin", (PyCFunction)builtin_bin, METH_O, builtin_bin__doc__},
435
436 static PyObject *
437 builtin_bin(PyModuleDef *module, PyObject *number)
438 /*[clinic end generated code: output=18fed0e943650da1 input=2a6362ae9a9c9203]*/
439 { 404 {
440 return PyNumber_ToBase(number, 2); 405 return PyNumber_ToBase(number, 2);
441 } 406 }
442 407
443 408
444 /*[clinic input] 409 /*[clinic input]
445 callable as builtin_callable 410 callable as builtin_callable
446 411
447 obj: 'O' 412 obj: object
448 / 413 /
449 414
450 Return whether the object is callable (i.e., some kind of function). 415 Return whether the object is callable (i.e., some kind of function).
451 416
452 Note that classes are callable, as are instances of classes with a 417 Note that classes are callable, as are instances of classes with a
453 __call__() method. 418 __call__() method.
454 [clinic start generated code]*/ 419 [clinic start generated code]*/
455 420
456 PyDoc_STRVAR(builtin_callable__doc__, 421 static PyObject *
457 "callable($module, obj, /)\n" 422 builtin_callable(PyObject *module, PyObject *obj)
458 "--\n" 423 /*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
459 "\n"
460 "Return whether the object is callable (i.e., some kind of function).\n"
461 "\n"
462 "Note that classes are callable, as are instances of classes with a\n"
463 "__call__() method.");
464
465 #define BUILTIN_CALLABLE_METHODDEF \
466 {"callable", (PyCFunction)builtin_callable, METH_O, builtin_callable__doc__} ,
467
468 static PyObject *
469 builtin_callable(PyModuleDef *module, PyObject *obj)
470 /*[clinic end generated code: output=b3a92cbe635f32af input=bb3bb528fffdade4]*/
471 { 424 {
472 return PyBool_FromLong((long)PyCallable_Check(obj)); 425 return PyBool_FromLong((long)PyCallable_Check(obj));
473 } 426 }
474 427
475 428
476 typedef struct { 429 typedef struct {
477 PyObject_HEAD 430 PyObject_HEAD
478 PyObject *func; 431 PyObject *func;
479 PyObject *it; 432 PyObject *it;
480 } filterobject; 433 } filterobject;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 return 0; 480 return 0;
528 } 481 }
529 482
530 static PyObject * 483 static PyObject *
531 filter_next(filterobject *lz) 484 filter_next(filterobject *lz)
532 { 485 {
533 PyObject *item; 486 PyObject *item;
534 PyObject *it = lz->it; 487 PyObject *it = lz->it;
535 long ok; 488 long ok;
536 PyObject *(*iternext)(PyObject *); 489 PyObject *(*iternext)(PyObject *);
490 int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
537 491
538 iternext = *Py_TYPE(it)->tp_iternext; 492 iternext = *Py_TYPE(it)->tp_iternext;
539 for (;;) { 493 for (;;) {
540 item = iternext(it); 494 item = iternext(it);
541 if (item == NULL) 495 if (item == NULL)
542 return NULL; 496 return NULL;
543 497
544 if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) { 498 if (checktrue) {
545 ok = PyObject_IsTrue(item); 499 ok = PyObject_IsTrue(item);
546 } else { 500 } else {
547 PyObject *good; 501 PyObject *good;
548 good = PyObject_CallFunctionObjArgs(lz->func, 502 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
549 item, NULL);
550 if (good == NULL) { 503 if (good == NULL) {
551 Py_DECREF(item); 504 Py_DECREF(item);
552 return NULL; 505 return NULL;
553 } 506 }
554 ok = PyObject_IsTrue(good); 507 ok = PyObject_IsTrue(good);
555 Py_DECREF(good); 508 Py_DECREF(good);
556 } 509 }
557 if (ok > 0) 510 if (ok > 0)
558 return item; 511 return item;
559 Py_DECREF(item); 512 Py_DECREF(item);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 0, /* tp_init */ 575 0, /* tp_init */
623 PyType_GenericAlloc, /* tp_alloc */ 576 PyType_GenericAlloc, /* tp_alloc */
624 filter_new, /* tp_new */ 577 filter_new, /* tp_new */
625 PyObject_GC_Del, /* tp_free */ 578 PyObject_GC_Del, /* tp_free */
626 }; 579 };
627 580
628 581
629 /*[clinic input] 582 /*[clinic input]
630 format as builtin_format 583 format as builtin_format
631 584
632 value: 'O' 585 value: object
633 format_spec: unicode(c_default="NULL") = '' 586 format_spec: unicode(c_default="NULL") = ''
634 / 587 /
635 588
636 Return value.__format__(format_spec) 589 Return value.__format__(format_spec)
637 590
638 format_spec defaults to the empty string 591 format_spec defaults to the empty string
639 [clinic start generated code]*/ 592 [clinic start generated code]*/
640 593
641 PyDoc_STRVAR(builtin_format__doc__, 594 static PyObject *
642 "format($module, value, format_spec=\'\', /)\n" 595 builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
643 "--\n" 596 /*[clinic end generated code: output=2f40bdfa4954b077 input=6325e751a1b29b86]*/
644 "\n"
645 "Return value.__format__(format_spec)\n"
646 "\n"
647 "format_spec defaults to the empty string");
648
649 #define BUILTIN_FORMAT_METHODDEF \
650 {"format", (PyCFunction)builtin_format, METH_VARARGS, builtin_format__doc__} ,
651
652 static PyObject *
653 builtin_format_impl(PyModuleDef *module, PyObject *value, PyObject *format_spec) ;
654
655 static PyObject *
656 builtin_format(PyModuleDef *module, PyObject *args)
657 {
658 PyObject *return_value = NULL;
659 PyObject *value;
660 PyObject *format_spec = NULL;
661
662 if (!PyArg_ParseTuple(args,
663 "O|U:format",
664 &value, &format_spec))
665 goto exit;
666 return_value = builtin_format_impl(module, value, format_spec);
667
668 exit:
669 return return_value;
670 }
671
672 static PyObject *
673 builtin_format_impl(PyModuleDef *module, PyObject *value, PyObject *format_spec)
674 /*[clinic end generated code: output=39723a58c72e8871 input=e23f2f11e0098c64]*/
675 { 597 {
676 return PyObject_Format(value, format_spec); 598 return PyObject_Format(value, format_spec);
677 } 599 }
678 600
679 /*[clinic input] 601 /*[clinic input]
680 chr as builtin_chr 602 chr as builtin_chr
681 603
682 i: 'i' 604 i: int
683 / 605 /
684 606
685 Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff. 607 Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
686 [clinic start generated code]*/ 608 [clinic start generated code]*/
687 609
688 PyDoc_STRVAR(builtin_chr__doc__, 610 static PyObject *
689 "chr($module, i, /)\n" 611 builtin_chr_impl(PyObject *module, int i)
690 "--\n" 612 /*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
691 "\n"
692 "Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
693
694 #define BUILTIN_CHR_METHODDEF \
695 {"chr", (PyCFunction)builtin_chr, METH_VARARGS, builtin_chr__doc__},
696
697 static PyObject *
698 builtin_chr_impl(PyModuleDef *module, int i);
699
700 static PyObject *
701 builtin_chr(PyModuleDef *module, PyObject *args)
702 {
703 PyObject *return_value = NULL;
704 int i;
705
706 if (!PyArg_ParseTuple(args,
707 "i:chr",
708 &i))
709 goto exit;
710 return_value = builtin_chr_impl(module, i);
711
712 exit:
713 return return_value;
714 }
715
716 static PyObject *
717 builtin_chr_impl(PyModuleDef *module, int i)
718 /*[clinic end generated code: output=4d6bbe948f56e2ae input=9b1ced29615adf66]*/
719 { 613 {
720 return PyUnicode_FromOrdinal(i); 614 return PyUnicode_FromOrdinal(i);
721 } 615 }
722 616
723 617
724 static const char * 618 static const char *
725 source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompil erFlags *cf, Py_buffer *view) 619 source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompil erFlags *cf, PyObject **cmd_copy)
726 { 620 {
727 const char *str; 621 const char *str;
728 Py_ssize_t size; 622 Py_ssize_t size;
729 623 Py_buffer view;
624
625 *cmd_copy = NULL;
730 if (PyUnicode_Check(cmd)) { 626 if (PyUnicode_Check(cmd)) {
731 cf->cf_flags |= PyCF_IGNORE_COOKIE; 627 cf->cf_flags |= PyCF_IGNORE_COOKIE;
732 str = PyUnicode_AsUTF8AndSize(cmd, &size); 628 str = PyUnicode_AsUTF8AndSize(cmd, &size);
733 if (str == NULL) 629 if (str == NULL)
734 return NULL; 630 return NULL;
735 } 631 }
736 else if (PyObject_GetBuffer(cmd, view, PyBUF_SIMPLE) == 0) { 632 else if (PyBytes_Check(cmd)) {
737 str = (const char *)view->buf; 633 str = PyBytes_AS_STRING(cmd);
738 size = view->len; 634 size = PyBytes_GET_SIZE(cmd);
635 }
636 else if (PyByteArray_Check(cmd)) {
637 str = PyByteArray_AS_STRING(cmd);
638 size = PyByteArray_GET_SIZE(cmd);
639 }
640 else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
641 /* Copy to NUL-terminated buffer. */
642 *cmd_copy = PyBytes_FromStringAndSize(
643 (const char *)view.buf, view.len);
644 PyBuffer_Release(&view);
645 if (*cmd_copy == NULL) {
646 return NULL;
647 }
648 str = PyBytes_AS_STRING(*cmd_copy);
649 size = PyBytes_GET_SIZE(*cmd_copy);
739 } 650 }
740 else { 651 else {
741 PyErr_Format(PyExc_TypeError, 652 PyErr_Format(PyExc_TypeError,
742 "%s() arg 1 must be a %s object", 653 "%s() arg 1 must be a %s object",
743 funcname, what); 654 funcname, what);
744 return NULL; 655 return NULL;
745 } 656 }
746 657
747 if (strlen(str) != (size_t)size) { 658 if (strlen(str) != (size_t)size) {
748 PyErr_SetString(PyExc_ValueError, 659 PyErr_SetString(PyExc_ValueError,
749 "source code string cannot contain null bytes"); 660 "source code string cannot contain null bytes");
750 PyBuffer_Release(view); 661 Py_CLEAR(*cmd_copy);
751 return NULL; 662 return NULL;
752 } 663 }
753 return str; 664 return str;
754 } 665 }
755 666
756 /*[clinic input] 667 /*[clinic input]
757 compile as builtin_compile 668 compile as builtin_compile
758 669
759 source: 'O' 670 source: object
760 filename: object(converter="PyUnicode_FSDecoder") 671 filename: object(converter="PyUnicode_FSDecoder")
761 mode: 's' 672 mode: str
762 flags: 'i' = 0 673 flags: int = 0
763 dont_inherit: 'i' = 0 674 dont_inherit: int(c_default="0") = False
764 optimize: 'i' = -1 675 optimize: int = -1
765 676
766 Compile source into a code object that can be executed by exec() or eval(). 677 Compile source into a code object that can be executed by exec() or eval().
767 678
768 The source code may represent a Python module, statement or expression. 679 The source code may represent a Python module, statement or expression.
769 The filename will be used for run-time error messages. 680 The filename will be used for run-time error messages.
770 The mode must be 'exec' to compile a module, 'single' to compile a 681 The mode must be 'exec' to compile a module, 'single' to compile a
771 single (interactive) statement, or 'eval' to compile an expression. 682 single (interactive) statement, or 'eval' to compile an expression.
772 The flags argument, if present, controls which future statements influence 683 The flags argument, if present, controls which future statements influence
773 the compilation of the code. 684 the compilation of the code.
774 The dont_inherit argument, if non-zero, stops the compilation inheriting 685 The dont_inherit argument, if true, stops the compilation inheriting
775 the effects of any future statements in effect in the code calling 686 the effects of any future statements in effect in the code calling
776 compile; if absent or zero these statements do influence the compilation, 687 compile; if absent or false these statements do influence the compilation,
777 in addition to any features explicitly specified. 688 in addition to any features explicitly specified.
778 [clinic start generated code]*/ 689 [clinic start generated code]*/
779 690
780 PyDoc_STRVAR(builtin_compile__doc__, 691 static PyObject *
781 "compile($module, /, source, filename, mode, flags=0, dont_inherit=0,\n" 692 builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
782 " optimize=-1)\n" 693 const char *mode, int flags, int dont_inherit,
783 "--\n" 694 int optimize)
784 "\n" 695 /*[clinic end generated code: output=1fa176e33452bb63 input=9d53e8cfb3c86414]*/
785 "Compile source into a code object that can be executed by exec() or eval().\n" 696 {
786 "\n" 697 PyObject *source_copy;
787 "The source code may represent a Python module, statement or expression.\n"
788 "The filename will be used for run-time error messages.\n"
789 "The mode must be \'exec\' to compile a module, \'single\' to compile a\n"
790 "single (interactive) statement, or \'eval\' to compile an expression.\n"
791 "The flags argument, if present, controls which future statements influence\n"
792 "the compilation of the code.\n"
793 "The dont_inherit argument, if non-zero, stops the compilation inheriting\n"
794 "the effects of any future statements in effect in the code calling\n"
795 "compile; if absent or zero these statements do influence the compilation,\n"
796 "in addition to any features explicitly specified.");
797
798 #define BUILTIN_COMPILE_METHODDEF \
799 {"compile", (PyCFunction)builtin_compile, METH_VARARGS|METH_KEYWORDS, builti n_compile__doc__},
800
801 static PyObject *
802 builtin_compile_impl(PyModuleDef *module, PyObject *source, PyObject *filename, const char *mode, int flags, int dont_inherit, int optimize);
803
804 static PyObject *
805 builtin_compile(PyModuleDef *module, PyObject *args, PyObject *kwargs)
806 {
807 PyObject *return_value = NULL;
808 static char *_keywords[] = {"source", "filename", "mode", "flags", "dont_inh erit", "optimize", NULL};
809 PyObject *source;
810 PyObject *filename;
811 const char *mode;
812 int flags = 0;
813 int dont_inherit = 0;
814 int optimize = -1;
815
816 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
817 "OO&s|iii:compile", _keywords,
818 &source, PyUnicode_FSDecoder, &filename, &mode, &flags, &dont_inherit, & optimize))
819 goto exit;
820 return_value = builtin_compile_impl(module, source, filename, mode, flags, d ont_inherit, optimize);
821
822 exit:
823 return return_value;
824 }
825
826 static PyObject *
827 builtin_compile_impl(PyModuleDef *module, PyObject *source, PyObject *filename, const char *mode, int flags, int dont_inherit, int optimize)
828 /*[clinic end generated code: output=c72d197809d178fc input=c6212a9d21472f7e]*/
829 {
830 Py_buffer view = {NULL, NULL};
831 const char *str; 698 const char *str;
832 int compile_mode = -1; 699 int compile_mode = -1;
833 int is_ast; 700 int is_ast;
834 PyCompilerFlags cf; 701 PyCompilerFlags cf;
835 int start[] = {Py_file_input, Py_eval_input, Py_single_input}; 702 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
836 PyObject *result; 703 PyObject *result;
837 704
838 cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8; 705 cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
839 706
840 if (flags & 707 if (flags &
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 PyArena_Free(arena); 759 PyArena_Free(arena);
893 goto error; 760 goto error;
894 } 761 }
895 result = (PyObject*)PyAST_CompileObject(mod, filename, 762 result = (PyObject*)PyAST_CompileObject(mod, filename,
896 &cf, optimize, arena); 763 &cf, optimize, arena);
897 PyArena_Free(arena); 764 PyArena_Free(arena);
898 } 765 }
899 goto finally; 766 goto finally;
900 } 767 }
901 768
902 str = source_as_string(source, "compile", "string, bytes or AST", &cf, &view ); 769 str = source_as_string(source, "compile", "string, bytes or AST", &cf, &sour ce_copy);
903 if (str == NULL) 770 if (str == NULL)
904 goto error; 771 goto error;
905 772
906 result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, opt imize); 773 result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, opt imize);
907 PyBuffer_Release(&view); 774 Py_XDECREF(source_copy);
908 goto finally; 775 goto finally;
909 776
910 error: 777 error:
911 result = NULL; 778 result = NULL;
912 finally: 779 finally:
913 Py_DECREF(filename); 780 Py_DECREF(filename);
914 return result; 781 return result;
915 } 782 }
916 783
917 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */ 784 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */
(...skipping 17 matching lines...) Expand all
935 "the default dir() logic is used and returns:\n" 802 "the default dir() logic is used and returns:\n"
936 " for a module object: the module's attributes.\n" 803 " for a module object: the module's attributes.\n"
937 " for a class object: its attributes, and recursively the attributes\n" 804 " for a class object: its attributes, and recursively the attributes\n"
938 " of its bases.\n" 805 " of its bases.\n"
939 " for any other object: its attributes, its class's attributes, and\n" 806 " for any other object: its attributes, its class's attributes, and\n"
940 " recursively the attributes of its class's base classes."); 807 " recursively the attributes of its class's base classes.");
941 808
942 /*[clinic input] 809 /*[clinic input]
943 divmod as builtin_divmod 810 divmod as builtin_divmod
944 811
945 x: 'O' 812 x: object
946 y: 'O' 813 y: object
947 / 814 /
948 815
949 Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x. 816 Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
950 [clinic start generated code]*/ 817 [clinic start generated code]*/
951 818
952 PyDoc_STRVAR(builtin_divmod__doc__, 819 static PyObject *
953 "divmod($module, x, y, /)\n" 820 builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
954 "--\n" 821 /*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
955 "\n"
956 "Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
957
958 #define BUILTIN_DIVMOD_METHODDEF \
959 {"divmod", (PyCFunction)builtin_divmod, METH_VARARGS, builtin_divmod__doc__} ,
960
961 static PyObject *
962 builtin_divmod_impl(PyModuleDef *module, PyObject *x, PyObject *y);
963
964 static PyObject *
965 builtin_divmod(PyModuleDef *module, PyObject *args)
966 {
967 PyObject *return_value = NULL;
968 PyObject *x;
969 PyObject *y;
970
971 if (!PyArg_UnpackTuple(args, "divmod",
972 2, 2,
973 &x, &y))
974 goto exit;
975 return_value = builtin_divmod_impl(module, x, y);
976
977 exit:
978 return return_value;
979 }
980
981 static PyObject *
982 builtin_divmod_impl(PyModuleDef *module, PyObject *x, PyObject *y)
983 /*[clinic end generated code: output=77e8d408b1338886 input=c9c617b7bb74c615]*/
984 { 822 {
985 return PyNumber_Divmod(x, y); 823 return PyNumber_Divmod(x, y);
986 } 824 }
987 825
988 826
989 /*[clinic input] 827 /*[clinic input]
990 eval as builtin_eval 828 eval as builtin_eval
991 829
992 source: 'O' 830 source: object
993 globals: 'O' = None 831 globals: object = None
994 locals: 'O' = None 832 locals: object = None
995 / 833 /
996 834
997 Evaluate the given source in the context of globals and locals. 835 Evaluate the given source in the context of globals and locals.
998 836
999 The source may be a string representing a Python expression 837 The source may be a string representing a Python expression
1000 or a code object as returned by compile(). 838 or a code object as returned by compile().
1001 The globals must be a dictionary and locals can be any mapping, 839 The globals must be a dictionary and locals can be any mapping,
1002 defaulting to the current globals and locals. 840 defaulting to the current globals and locals.
1003 If only globals is given, locals defaults to it. 841 If only globals is given, locals defaults to it.
1004 [clinic start generated code]*/ 842 [clinic start generated code]*/
1005 843
1006 PyDoc_STRVAR(builtin_eval__doc__, 844 static PyObject *
1007 "eval($module, source, globals=None, locals=None, /)\n" 845 builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
1008 "--\n" 846 PyObject *locals)
1009 "\n" 847 /*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
1010 "Evaluate the given source in the context of globals and locals.\n" 848 {
1011 "\n" 849 PyObject *result, *source_copy;
1012 "The source may be a string representing a Python expression\n"
1013 "or a code object as returned by compile().\n"
1014 "The globals must be a dictionary and locals can be any mapping,\n"
1015 "defaulting to the current globals and locals.\n"
1016 "If only globals is given, locals defaults to it.");
1017
1018 #define BUILTIN_EVAL_METHODDEF \
1019 {"eval", (PyCFunction)builtin_eval, METH_VARARGS, builtin_eval__doc__},
1020
1021 static PyObject *
1022 builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals, PyOb ject *locals);
1023
1024 static PyObject *
1025 builtin_eval(PyModuleDef *module, PyObject *args)
1026 {
1027 PyObject *return_value = NULL;
1028 PyObject *source;
1029 PyObject *globals = Py_None;
1030 PyObject *locals = Py_None;
1031
1032 if (!PyArg_UnpackTuple(args, "eval",
1033 1, 3,
1034 &source, &globals, &locals))
1035 goto exit;
1036 return_value = builtin_eval_impl(module, source, globals, locals);
1037
1038 exit:
1039 return return_value;
1040 }
1041
1042 static PyObject *
1043 builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals, PyOb ject *locals)
1044 /*[clinic end generated code: output=644fd59012538ce6 input=31e42c1d2125b50b]*/
1045 {
1046 PyObject *result, *tmp = NULL;
1047 Py_buffer view = {NULL, NULL};
1048 const char *str; 850 const char *str;
1049 PyCompilerFlags cf; 851 PyCompilerFlags cf;
1050 852
1051 if (locals != Py_None && !PyMapping_Check(locals)) { 853 if (locals != Py_None && !PyMapping_Check(locals)) {
1052 PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); 854 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
1053 return NULL; 855 return NULL;
1054 } 856 }
1055 if (globals != Py_None && !PyDict_Check(globals)) { 857 if (globals != Py_None && !PyDict_Check(globals)) {
1056 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ? 858 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
1057 "globals must be a real dict; try eval(expr, {}, mapping)" 859 "globals must be a real dict; try eval(expr, {}, mapping)"
(...skipping 27 matching lines...) Expand all
1085 if (PyCode_Check(source)) { 887 if (PyCode_Check(source)) {
1086 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { 888 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
1087 PyErr_SetString(PyExc_TypeError, 889 PyErr_SetString(PyExc_TypeError,
1088 "code object passed to eval() may not contain free variables"); 890 "code object passed to eval() may not contain free variables");
1089 return NULL; 891 return NULL;
1090 } 892 }
1091 return PyEval_EvalCode(source, globals, locals); 893 return PyEval_EvalCode(source, globals, locals);
1092 } 894 }
1093 895
1094 cf.cf_flags = PyCF_SOURCE_IS_UTF8; 896 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
1095 str = source_as_string(source, "eval", "string, bytes or code", &cf, &view); 897 str = source_as_string(source, "eval", "string, bytes or code", &cf, &source _copy);
1096 if (str == NULL) 898 if (str == NULL)
1097 return NULL; 899 return NULL;
1098 900
1099 while (*str == ' ' || *str == '\t') 901 while (*str == ' ' || *str == '\t')
1100 str++; 902 str++;
1101 903
1102 (void)PyEval_MergeCompilerFlags(&cf); 904 (void)PyEval_MergeCompilerFlags(&cf);
1103 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); 905 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
1104 PyBuffer_Release(&view); 906 Py_XDECREF(source_copy);
1105 Py_XDECREF(tmp);
1106 return result; 907 return result;
1107 } 908 }
1108 909
1109 /*[clinic input] 910 /*[clinic input]
1110 exec as builtin_exec 911 exec as builtin_exec
1111 912
1112 source: 'O' 913 source: object
1113 globals: 'O' = None 914 globals: object = None
1114 locals: 'O' = None 915 locals: object = None
1115 / 916 /
1116 917
1117 Execute the given source in the context of globals and locals. 918 Execute the given source in the context of globals and locals.
1118 919
1119 The source may be a string representing one or more Python statements 920 The source may be a string representing one or more Python statements
1120 or a code object as returned by compile(). 921 or a code object as returned by compile().
1121 The globals must be a dictionary and locals can be any mapping, 922 The globals must be a dictionary and locals can be any mapping,
1122 defaulting to the current globals and locals. 923 defaulting to the current globals and locals.
1123 If only globals is given, locals defaults to it. 924 If only globals is given, locals defaults to it.
1124 [clinic start generated code]*/ 925 [clinic start generated code]*/
1125 926
1126 PyDoc_STRVAR(builtin_exec__doc__, 927 static PyObject *
1127 "exec($module, source, globals=None, locals=None, /)\n" 928 builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
1128 "--\n" 929 PyObject *locals)
1129 "\n" 930 /*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
1130 "Execute the given source in the context of globals and locals.\n"
1131 "\n"
1132 "The source may be a string representing one or more Python statements\n"
1133 "or a code object as returned by compile().\n"
1134 "The globals must be a dictionary and locals can be any mapping,\n"
1135 "defaulting to the current globals and locals.\n"
1136 "If only globals is given, locals defaults to it.");
1137
1138 #define BUILTIN_EXEC_METHODDEF \
1139 {"exec", (PyCFunction)builtin_exec, METH_VARARGS, builtin_exec__doc__},
1140
1141 static PyObject *
1142 builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals, PyOb ject *locals);
1143
1144 static PyObject *
1145 builtin_exec(PyModuleDef *module, PyObject *args)
1146 {
1147 PyObject *return_value = NULL;
1148 PyObject *source;
1149 PyObject *globals = Py_None;
1150 PyObject *locals = Py_None;
1151
1152 if (!PyArg_UnpackTuple(args, "exec",
1153 1, 3,
1154 &source, &globals, &locals))
1155 goto exit;
1156 return_value = builtin_exec_impl(module, source, globals, locals);
1157
1158 exit:
1159 return return_value;
1160 }
1161
1162 static PyObject *
1163 builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals, PyOb ject *locals)
1164 /*[clinic end generated code: output=0281b48bfa8e3c87 input=536e057b5e00d89e]*/
1165 { 931 {
1166 PyObject *v; 932 PyObject *v;
1167 933
1168 if (globals == Py_None) { 934 if (globals == Py_None) {
1169 globals = PyEval_GetGlobals(); 935 globals = PyEval_GetGlobals();
1170 if (locals == Py_None) { 936 if (locals == Py_None) {
1171 locals = PyEval_GetLocals(); 937 locals = PyEval_GetLocals();
1172 if (locals == NULL) 938 if (locals == NULL)
1173 return NULL; 939 return NULL;
1174 } 940 }
(...skipping 26 matching lines...) Expand all
1201 if (PyCode_Check(source)) { 967 if (PyCode_Check(source)) {
1202 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { 968 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
1203 PyErr_SetString(PyExc_TypeError, 969 PyErr_SetString(PyExc_TypeError,
1204 "code object passed to exec() may not " 970 "code object passed to exec() may not "
1205 "contain free variables"); 971 "contain free variables");
1206 return NULL; 972 return NULL;
1207 } 973 }
1208 v = PyEval_EvalCode(source, globals, locals); 974 v = PyEval_EvalCode(source, globals, locals);
1209 } 975 }
1210 else { 976 else {
1211 Py_buffer view = {NULL, NULL}; 977 PyObject *source_copy;
1212 const char *str; 978 const char *str;
1213 PyCompilerFlags cf; 979 PyCompilerFlags cf;
1214 cf.cf_flags = PyCF_SOURCE_IS_UTF8; 980 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
1215 str = source_as_string(source, "exec", 981 str = source_as_string(source, "exec",
1216 "string, bytes or code", &cf, &view); 982 "string, bytes or code", &cf,
983 &source_copy);
1217 if (str == NULL) 984 if (str == NULL)
1218 return NULL; 985 return NULL;
1219 if (PyEval_MergeCompilerFlags(&cf)) 986 if (PyEval_MergeCompilerFlags(&cf))
1220 v = PyRun_StringFlags(str, Py_file_input, globals, 987 v = PyRun_StringFlags(str, Py_file_input, globals,
1221 locals, &cf); 988 locals, &cf);
1222 else 989 else
1223 v = PyRun_String(str, Py_file_input, globals, locals); 990 v = PyRun_String(str, Py_file_input, globals, locals);
1224 PyBuffer_Release(&view); 991 Py_XDECREF(source_copy);
1225 } 992 }
1226 if (v == NULL) 993 if (v == NULL)
1227 return NULL; 994 return NULL;
1228 Py_DECREF(v); 995 Py_DECREF(v);
1229 Py_RETURN_NONE; 996 Py_RETURN_NONE;
1230 } 997 }
1231 998
1232 999
1233 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */ 1000 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */
1234 static PyObject * 1001 static PyObject *
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1266 1033
1267 /*[clinic input] 1034 /*[clinic input]
1268 globals as builtin_globals 1035 globals as builtin_globals
1269 1036
1270 Return the dictionary containing the current scope's global variables. 1037 Return the dictionary containing the current scope's global variables.
1271 1038
1272 NOTE: Updates to this dictionary *will* affect name lookups in the current 1039 NOTE: Updates to this dictionary *will* affect name lookups in the current
1273 global scope and vice-versa. 1040 global scope and vice-versa.
1274 [clinic start generated code]*/ 1041 [clinic start generated code]*/
1275 1042
1276 PyDoc_STRVAR(builtin_globals__doc__, 1043 static PyObject *
1277 "globals($module, /)\n" 1044 builtin_globals_impl(PyObject *module)
1278 "--\n" 1045 /*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
1279 "\n"
1280 "Return the dictionary containing the current scope\'s global variables.\n"
1281 "\n"
1282 "NOTE: Updates to this dictionary *will* affect name lookups in the current\n"
1283 "global scope and vice-versa.");
1284
1285 #define BUILTIN_GLOBALS_METHODDEF \
1286 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, builtin_globals__doc_ _},
1287
1288 static PyObject *
1289 builtin_globals_impl(PyModuleDef *module);
1290
1291 static PyObject *
1292 builtin_globals(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
1293 {
1294 return builtin_globals_impl(module);
1295 }
1296
1297 static PyObject *
1298 builtin_globals_impl(PyModuleDef *module)
1299 /*[clinic end generated code: output=048640f58b1f20ad input=9327576f92bb48ba]*/
1300 { 1046 {
1301 PyObject *d; 1047 PyObject *d;
1302 1048
1303 d = PyEval_GetGlobals(); 1049 d = PyEval_GetGlobals();
1304 Py_XINCREF(d); 1050 Py_XINCREF(d);
1305 return d; 1051 return d;
1306 } 1052 }
1307 1053
1308 1054
1309 /*[clinic input] 1055 /*[clinic input]
1310 hasattr as builtin_hasattr 1056 hasattr as builtin_hasattr
1311 1057
1312 obj: 'O' 1058 obj: object
1313 name: 'O' 1059 name: object
1314 / 1060 /
1315 1061
1316 Return whether the object has an attribute with the given name. 1062 Return whether the object has an attribute with the given name.
1317 1063
1318 This is done by calling getattr(obj, name) and catching AttributeError. 1064 This is done by calling getattr(obj, name) and catching AttributeError.
1319 [clinic start generated code]*/ 1065 [clinic start generated code]*/
1320 1066
1321 PyDoc_STRVAR(builtin_hasattr__doc__, 1067 static PyObject *
1322 "hasattr($module, obj, name, /)\n" 1068 builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1323 "--\n" 1069 /*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
1324 "\n"
1325 "Return whether the object has an attribute with the given name.\n"
1326 "\n"
1327 "This is done by calling getattr(obj, name) and catching AttributeError.");
1328
1329 #define BUILTIN_HASATTR_METHODDEF \
1330 {"hasattr", (PyCFunction)builtin_hasattr, METH_VARARGS, builtin_hasattr__doc __},
1331
1332 static PyObject *
1333 builtin_hasattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name);
1334
1335 static PyObject *
1336 builtin_hasattr(PyModuleDef *module, PyObject *args)
1337 {
1338 PyObject *return_value = NULL;
1339 PyObject *obj;
1340 PyObject *name;
1341
1342 if (!PyArg_UnpackTuple(args, "hasattr",
1343 2, 2,
1344 &obj, &name))
1345 goto exit;
1346 return_value = builtin_hasattr_impl(module, obj, name);
1347
1348 exit:
1349 return return_value;
1350 }
1351
1352 static PyObject *
1353 builtin_hasattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name)
1354 /*[clinic end generated code: output=e0bd996ef73d1217 input=b50bad5f739ea10d]*/
1355 { 1070 {
1356 PyObject *v; 1071 PyObject *v;
1357 1072
1358 if (!PyUnicode_Check(name)) { 1073 if (!PyUnicode_Check(name)) {
1359 PyErr_SetString(PyExc_TypeError, 1074 PyErr_SetString(PyExc_TypeError,
1360 "hasattr(): attribute name must be string"); 1075 "hasattr(): attribute name must be string");
1361 return NULL; 1076 return NULL;
1362 } 1077 }
1363 v = PyObject_GetAttr(obj, name); 1078 v = PyObject_GetAttr(obj, name);
1364 if (v == NULL) { 1079 if (v == NULL) {
1365 if (PyErr_ExceptionMatches(PyExc_AttributeError)) { 1080 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1366 PyErr_Clear(); 1081 PyErr_Clear();
1367 Py_RETURN_FALSE; 1082 Py_RETURN_FALSE;
1368 } 1083 }
1369 return NULL; 1084 return NULL;
1370 } 1085 }
1371 Py_DECREF(v); 1086 Py_DECREF(v);
1372 Py_RETURN_TRUE; 1087 Py_RETURN_TRUE;
1373 } 1088 }
1374 1089
1375 1090
1376 /* AC: gdb's integration with CPython relies on builtin_id having 1091 /* AC: gdb's integration with CPython relies on builtin_id having
1377 * the *exact* parameter names of "self" and "v", so we ensure we 1092 * the *exact* parameter names of "self" and "v", so we ensure we
1378 * preserve those name rather than using the AC defaults. 1093 * preserve those name rather than using the AC defaults.
1379 */ 1094 */
1380 /*[clinic input] 1095 /*[clinic input]
1381 id as builtin_id 1096 id as builtin_id
1382 1097
1383 self: self(type="PyModuleDef *") 1098 self: self(type="PyModuleDef *")
1384 obj as v: 'O' 1099 obj as v: object
1385 / 1100 /
1386 1101
1387 Return the identity of an object. 1102 Return the identity of an object.
1388 1103
1389 This is guaranteed to be unique among simultaneously existing objects. 1104 This is guaranteed to be unique among simultaneously existing objects.
1390 (CPython uses the object's memory address.) 1105 (CPython uses the object's memory address.)
1391 [clinic start generated code]*/ 1106 [clinic start generated code]*/
1392 1107
1393 PyDoc_STRVAR(builtin_id__doc__,
1394 "id($module, obj, /)\n"
1395 "--\n"
1396 "\n"
1397 "Return the identity of an object.\n"
1398 "\n"
1399 "This is guaranteed to be unique among simultaneously existing objects.\n"
1400 "(CPython uses the object\'s memory address.)");
1401
1402 #define BUILTIN_ID_METHODDEF \
1403 {"id", (PyCFunction)builtin_id, METH_O, builtin_id__doc__},
1404
1405 static PyObject * 1108 static PyObject *
1406 builtin_id(PyModuleDef *self, PyObject *v) 1109 builtin_id(PyModuleDef *self, PyObject *v)
1407 /*[clinic end generated code: output=f54da09c91992e63 input=a1f988d98357341d]*/ 1110 /*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
1408 { 1111 {
1409 return PyLong_FromVoidPtr(v); 1112 return PyLong_FromVoidPtr(v);
1410 } 1113 }
1411 1114
1412 1115
1413 /* map object ************************************************************/ 1116 /* map object ************************************************************/
1414 1117
1415 typedef struct { 1118 typedef struct {
1416 PyObject_HEAD 1119 PyObject_HEAD
1417 PyObject *iters; 1120 PyObject *iters;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1476 map_traverse(mapobject *lz, visitproc visit, void *arg) 1179 map_traverse(mapobject *lz, visitproc visit, void *arg)
1477 { 1180 {
1478 Py_VISIT(lz->iters); 1181 Py_VISIT(lz->iters);
1479 Py_VISIT(lz->func); 1182 Py_VISIT(lz->func);
1480 return 0; 1183 return 0;
1481 } 1184 }
1482 1185
1483 static PyObject * 1186 static PyObject *
1484 map_next(mapobject *lz) 1187 map_next(mapobject *lz)
1485 { 1188 {
1486 PyObject *val; 1189 PyObject *small_stack[5];
1487 PyObject *argtuple; 1190 PyObject **stack;
1488 PyObject *result; 1191 Py_ssize_t niters, nargs, i;
1489 Py_ssize_t numargs, i; 1192 PyObject *result = NULL;
1490 1193
1491 numargs = PyTuple_Size(lz->iters); 1194 niters = PyTuple_GET_SIZE(lz->iters);
1492 argtuple = PyTuple_New(numargs); 1195 if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1493 if (argtuple == NULL) 1196 stack = small_stack;
1494 return NULL; 1197 }
1495 1198 else {
1496 for (i=0 ; i<numargs ; i++) { 1199 stack = PyMem_Malloc(niters * sizeof(stack[0]));
1497 val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i)); 1200 if (stack == NULL) {
1201 PyErr_NoMemory();
1202 return NULL;
1203 }
1204 }
1205
1206 nargs = 0;
1207 for (i=0; i < niters; i++) {
1208 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1209 PyObject *val = Py_TYPE(it)->tp_iternext(it);
1498 if (val == NULL) { 1210 if (val == NULL) {
1499 Py_DECREF(argtuple); 1211 goto exit;
1500 return NULL; 1212 }
1501 } 1213 stack[i] = val;
1502 PyTuple_SET_ITEM(argtuple, i, val); 1214 nargs++;
1503 } 1215 }
1504 result = PyObject_Call(lz->func, argtuple, NULL); 1216
1505 Py_DECREF(argtuple); 1217 result = _PyObject_FastCall(lz->func, stack, nargs);
1218
1219 exit:
1220 for (i=0; i < nargs; i++) {
1221 Py_DECREF(stack[i]);
1222 }
1223 if (stack != small_stack) {
1224 PyMem_Free(stack);
1225 }
1506 return result; 1226 return result;
1507 } 1227 }
1508 1228
1509 static PyObject * 1229 static PyObject *
1510 map_reduce(mapobject *lz) 1230 map_reduce(mapobject *lz)
1511 { 1231 {
1512 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters); 1232 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1513 PyObject *args = PyTuple_New(numargs+1); 1233 PyObject *args = PyTuple_New(numargs+1);
1514 Py_ssize_t i; 1234 Py_ssize_t i;
1515 if (args == NULL) 1235 if (args == NULL)
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1620 PyDoc_STRVAR(next_doc, 1340 PyDoc_STRVAR(next_doc,
1621 "next(iterator[, default])\n\ 1341 "next(iterator[, default])\n\
1622 \n\ 1342 \n\
1623 Return the next item from the iterator. If default is given and the iterator\n\ 1343 Return the next item from the iterator. If default is given and the iterator\n\
1624 is exhausted, it is returned instead of raising StopIteration."); 1344 is exhausted, it is returned instead of raising StopIteration.");
1625 1345
1626 1346
1627 /*[clinic input] 1347 /*[clinic input]
1628 setattr as builtin_setattr 1348 setattr as builtin_setattr
1629 1349
1630 obj: 'O' 1350 obj: object
1631 name: 'O' 1351 name: object
1632 value: 'O' 1352 value: object
1633 / 1353 /
1634 1354
1635 Sets the named attribute on the given object to the specified value. 1355 Sets the named attribute on the given object to the specified value.
1636 1356
1637 setattr(x, 'y', v) is equivalent to ``x.y = v'' 1357 setattr(x, 'y', v) is equivalent to ``x.y = v''
1638 [clinic start generated code]*/ 1358 [clinic start generated code]*/
1639 1359
1640 PyDoc_STRVAR(builtin_setattr__doc__, 1360 static PyObject *
1641 "setattr($module, obj, name, value, /)\n" 1361 builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
1642 "--\n" 1362 PyObject *value)
1643 "\n" 1363 /*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
1644 "Sets the named attribute on the given object to the specified value.\n"
1645 "\n"
1646 "setattr(x, \'y\', v) is equivalent to ``x.y = v\'\'");
1647
1648 #define BUILTIN_SETATTR_METHODDEF \
1649 {"setattr", (PyCFunction)builtin_setattr, METH_VARARGS, builtin_setattr__doc __},
1650
1651 static PyObject *
1652 builtin_setattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name, PyObjec t *value);
1653
1654 static PyObject *
1655 builtin_setattr(PyModuleDef *module, PyObject *args)
1656 {
1657 PyObject *return_value = NULL;
1658 PyObject *obj;
1659 PyObject *name;
1660 PyObject *value;
1661
1662 if (!PyArg_UnpackTuple(args, "setattr",
1663 3, 3,
1664 &obj, &name, &value))
1665 goto exit;
1666 return_value = builtin_setattr_impl(module, obj, name, value);
1667
1668 exit:
1669 return return_value;
1670 }
1671
1672 static PyObject *
1673 builtin_setattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name, PyObjec t *value)
1674 /*[clinic end generated code: output=4336dcbbf7691d2d input=fbe7e53403116b93]*/
1675 { 1364 {
1676 if (PyObject_SetAttr(obj, name, value) != 0) 1365 if (PyObject_SetAttr(obj, name, value) != 0)
1677 return NULL; 1366 return NULL;
1678 Py_INCREF(Py_None); 1367 Py_INCREF(Py_None);
1679 return Py_None; 1368 return Py_None;
1680 } 1369 }
1681 1370
1682 1371
1683 /*[clinic input] 1372 /*[clinic input]
1684 delattr as builtin_delattr 1373 delattr as builtin_delattr
1685 1374
1686 obj: 'O' 1375 obj: object
1687 name: 'O' 1376 name: object
1688 / 1377 /
1689 1378
1690 Deletes the named attribute from the given object. 1379 Deletes the named attribute from the given object.
1691 1380
1692 delattr(x, 'y') is equivalent to ``del x.y'' 1381 delattr(x, 'y') is equivalent to ``del x.y''
1693 [clinic start generated code]*/ 1382 [clinic start generated code]*/
1694 1383
1695 PyDoc_STRVAR(builtin_delattr__doc__, 1384 static PyObject *
1696 "delattr($module, obj, name, /)\n" 1385 builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1697 "--\n" 1386 /*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
1698 "\n"
1699 "Deletes the named attribute from the given object.\n"
1700 "\n"
1701 "delattr(x, \'y\') is equivalent to ``del x.y\'\'");
1702
1703 #define BUILTIN_DELATTR_METHODDEF \
1704 {"delattr", (PyCFunction)builtin_delattr, METH_VARARGS, builtin_delattr__doc __},
1705
1706 static PyObject *
1707 builtin_delattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name);
1708
1709 static PyObject *
1710 builtin_delattr(PyModuleDef *module, PyObject *args)
1711 {
1712 PyObject *return_value = NULL;
1713 PyObject *obj;
1714 PyObject *name;
1715
1716 if (!PyArg_UnpackTuple(args, "delattr",
1717 2, 2,
1718 &obj, &name))
1719 goto exit;
1720 return_value = builtin_delattr_impl(module, obj, name);
1721
1722 exit:
1723 return return_value;
1724 }
1725
1726 static PyObject *
1727 builtin_delattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name)
1728 /*[clinic end generated code: output=319c2d884aa769cf input=647af2ce9183a823]*/
1729 { 1387 {
1730 if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0) 1388 if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
1731 return NULL; 1389 return NULL;
1732 Py_INCREF(Py_None); 1390 Py_INCREF(Py_None);
1733 return Py_None; 1391 return Py_None;
1734 } 1392 }
1735 1393
1736 1394
1737 /*[clinic input] 1395 /*[clinic input]
1738 hash as builtin_hash 1396 hash as builtin_hash
1739 1397
1740 obj: 'O' 1398 obj: object
1741 / 1399 /
1742 1400
1743 Return the hash value for the given object. 1401 Return the hash value for the given object.
1744 1402
1745 Two objects that compare equal must also have the same hash value, but the 1403 Two objects that compare equal must also have the same hash value, but the
1746 reverse is not necessarily true. 1404 reverse is not necessarily true.
1747 [clinic start generated code]*/ 1405 [clinic start generated code]*/
1748 1406
1749 PyDoc_STRVAR(builtin_hash__doc__, 1407 static PyObject *
1750 "hash($module, obj, /)\n" 1408 builtin_hash(PyObject *module, PyObject *obj)
1751 "--\n" 1409 /*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
1752 "\n"
1753 "Return the hash value for the given object.\n"
1754 "\n"
1755 "Two objects that compare equal must also have the same hash value, but the\n"
1756 "reverse is not necessarily true.");
1757
1758 #define BUILTIN_HASH_METHODDEF \
1759 {"hash", (PyCFunction)builtin_hash, METH_O, builtin_hash__doc__},
1760
1761 static PyObject *
1762 builtin_hash(PyModuleDef *module, PyObject *obj)
1763 /*[clinic end generated code: output=1ec467611c13468b input=ccc4d2b9a351df4e]*/
1764 { 1410 {
1765 Py_hash_t x; 1411 Py_hash_t x;
1766 1412
1767 x = PyObject_Hash(obj); 1413 x = PyObject_Hash(obj);
1768 if (x == -1) 1414 if (x == -1)
1769 return NULL; 1415 return NULL;
1770 return PyLong_FromSsize_t(x); 1416 return PyLong_FromSsize_t(x);
1771 } 1417 }
1772 1418
1773 1419
1774 /*[clinic input] 1420 /*[clinic input]
1775 hex as builtin_hex 1421 hex as builtin_hex
1776 1422
1777 number: 'O' 1423 number: object
1778 / 1424 /
1779 1425
1780 Return the hexadecimal representation of an integer. 1426 Return the hexadecimal representation of an integer.
1781 1427
1782 >>> hex(12648430) 1428 >>> hex(12648430)
1783 '0xc0ffee' 1429 '0xc0ffee'
1784 [clinic start generated code]*/ 1430 [clinic start generated code]*/
1785 1431
1786 PyDoc_STRVAR(builtin_hex__doc__, 1432 static PyObject *
1787 "hex($module, number, /)\n" 1433 builtin_hex(PyObject *module, PyObject *number)
1788 "--\n" 1434 /*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
1789 "\n"
1790 "Return the hexadecimal representation of an integer.\n"
1791 "\n"
1792 " >>> hex(12648430)\n"
1793 " \'0xc0ffee\'");
1794
1795 #define BUILTIN_HEX_METHODDEF \
1796 {"hex", (PyCFunction)builtin_hex, METH_O, builtin_hex__doc__},
1797
1798 static PyObject *
1799 builtin_hex(PyModuleDef *module, PyObject *number)
1800 /*[clinic end generated code: output=f18e9439aeaa2c6c input=e816200b0a728ebe]*/
1801 { 1435 {
1802 return PyNumber_ToBase(number, 16); 1436 return PyNumber_ToBase(number, 16);
1803 } 1437 }
1804 1438
1805 1439
1806 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */ 1440 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */
1807 static PyObject * 1441 static PyObject *
1808 builtin_iter(PyObject *self, PyObject *args) 1442 builtin_iter(PyObject *self, PyObject *args)
1809 { 1443 {
1810 PyObject *v, *w = NULL; 1444 PyObject *v, *w = NULL;
(...skipping 15 matching lines...) Expand all
1826 iter(callable, sentinel) -> iterator\n\ 1460 iter(callable, sentinel) -> iterator\n\
1827 \n\ 1461 \n\
1828 Get an iterator from an object. In the first form, the argument must\n\ 1462 Get an iterator from an object. In the first form, the argument must\n\
1829 supply its own iterator, or be a sequence.\n\ 1463 supply its own iterator, or be a sequence.\n\
1830 In the second form, the callable is called until it returns the sentinel."); 1464 In the second form, the callable is called until it returns the sentinel.");
1831 1465
1832 1466
1833 /*[clinic input] 1467 /*[clinic input]
1834 len as builtin_len 1468 len as builtin_len
1835 1469
1836 obj: 'O' 1470 obj: object
1837 / 1471 /
1838 1472
1839 Return the number of items in a container. 1473 Return the number of items in a container.
1840 [clinic start generated code]*/ 1474 [clinic start generated code]*/
1841 1475
1842 PyDoc_STRVAR(builtin_len__doc__, 1476 static PyObject *
1843 "len($module, obj, /)\n" 1477 builtin_len(PyObject *module, PyObject *obj)
1844 "--\n" 1478 /*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
1845 "\n"
1846 "Return the number of items in a container.");
1847
1848 #define BUILTIN_LEN_METHODDEF \
1849 {"len", (PyCFunction)builtin_len, METH_O, builtin_len__doc__},
1850
1851 static PyObject *
1852 builtin_len(PyModuleDef *module, PyObject *obj)
1853 /*[clinic end generated code: output=5a38b0db40761705 input=2e5ff15db9a2de22]*/
1854 { 1479 {
1855 Py_ssize_t res; 1480 Py_ssize_t res;
1856 1481
1857 res = PyObject_Size(obj); 1482 res = PyObject_Size(obj);
1858 if (res < 0 && PyErr_Occurred()) 1483 if (res < 0 && PyErr_Occurred())
1859 return NULL; 1484 return NULL;
1860 return PyLong_FromSsize_t(res); 1485 return PyLong_FromSsize_t(res);
1861 } 1486 }
1862 1487
1863 1488
1864 /*[clinic input] 1489 /*[clinic input]
1865 locals as builtin_locals 1490 locals as builtin_locals
1866 1491
1867 Return a dictionary containing the current scope's local variables. 1492 Return a dictionary containing the current scope's local variables.
1868 1493
1869 NOTE: Whether or not updates to this dictionary will affect name lookups in 1494 NOTE: Whether or not updates to this dictionary will affect name lookups in
1870 the local scope and vice-versa is *implementation dependent* and not 1495 the local scope and vice-versa is *implementation dependent* and not
1871 covered by any backwards compatibility guarantees. 1496 covered by any backwards compatibility guarantees.
1872 [clinic start generated code]*/ 1497 [clinic start generated code]*/
1873 1498
1874 PyDoc_STRVAR(builtin_locals__doc__, 1499 static PyObject *
1875 "locals($module, /)\n" 1500 builtin_locals_impl(PyObject *module)
1876 "--\n" 1501 /*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
1877 "\n"
1878 "Return a dictionary containing the current scope\'s local variables.\n"
1879 "\n"
1880 "NOTE: Whether or not updates to this dictionary will affect name lookups in\n"
1881 "the local scope and vice-versa is *implementation dependent* and not\n"
1882 "covered by any backwards compatibility guarantees.");
1883
1884 #define BUILTIN_LOCALS_METHODDEF \
1885 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, builtin_locals__doc__},
1886
1887 static PyObject *
1888 builtin_locals_impl(PyModuleDef *module);
1889
1890 static PyObject *
1891 builtin_locals(PyModuleDef *module, PyObject *Py_UNUSED(ignored))
1892 {
1893 return builtin_locals_impl(module);
1894 }
1895
1896 static PyObject *
1897 builtin_locals_impl(PyModuleDef *module)
1898 /*[clinic end generated code: output=8ac52522924346e2 input=7874018d478d5c4b]*/
1899 { 1502 {
1900 PyObject *d; 1503 PyObject *d;
1901 1504
1902 d = PyEval_GetLocals(); 1505 d = PyEval_GetLocals();
1903 Py_XINCREF(d); 1506 Py_XINCREF(d);
1904 return d; 1507 return d;
1905 } 1508 }
1906 1509
1907 1510
1908 static PyObject * 1511 static PyObject *
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
2036 \n\ 1639 \n\
2037 With a single iterable argument, return its biggest item. The\n\ 1640 With a single iterable argument, return its biggest item. The\n\
2038 default keyword-only argument specifies an object to return if\n\ 1641 default keyword-only argument specifies an object to return if\n\
2039 the provided iterable is empty.\n\ 1642 the provided iterable is empty.\n\
2040 With two or more arguments, return the largest argument."); 1643 With two or more arguments, return the largest argument.");
2041 1644
2042 1645
2043 /*[clinic input] 1646 /*[clinic input]
2044 oct as builtin_oct 1647 oct as builtin_oct
2045 1648
2046 number: 'O' 1649 number: object
2047 / 1650 /
2048 1651
2049 Return the octal representation of an integer. 1652 Return the octal representation of an integer.
2050 1653
2051 >>> oct(342391) 1654 >>> oct(342391)
2052 '0o1234567' 1655 '0o1234567'
2053 [clinic start generated code]*/ 1656 [clinic start generated code]*/
2054 1657
2055 PyDoc_STRVAR(builtin_oct__doc__, 1658 static PyObject *
2056 "oct($module, number, /)\n" 1659 builtin_oct(PyObject *module, PyObject *number)
2057 "--\n" 1660 /*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
2058 "\n"
2059 "Return the octal representation of an integer.\n"
2060 "\n"
2061 " >>> oct(342391)\n"
2062 " \'0o1234567\'");
2063
2064 #define BUILTIN_OCT_METHODDEF \
2065 {"oct", (PyCFunction)builtin_oct, METH_O, builtin_oct__doc__},
2066
2067 static PyObject *
2068 builtin_oct(PyModuleDef *module, PyObject *number)
2069 /*[clinic end generated code: output=b99234d1d70a6673 input=a3a372b521b3dd13]*/
2070 { 1661 {
2071 return PyNumber_ToBase(number, 8); 1662 return PyNumber_ToBase(number, 8);
2072 } 1663 }
2073 1664
2074 1665
2075 /*[clinic input] 1666 /*[clinic input]
2076 ord as builtin_ord 1667 ord as builtin_ord
2077 1668
2078 c: 'O' 1669 c: object
2079 / 1670 /
2080 1671
2081 Return the Unicode code point for a one-character string. 1672 Return the Unicode code point for a one-character string.
2082 [clinic start generated code]*/ 1673 [clinic start generated code]*/
2083 1674
2084 PyDoc_STRVAR(builtin_ord__doc__, 1675 static PyObject *
2085 "ord($module, c, /)\n" 1676 builtin_ord(PyObject *module, PyObject *c)
2086 "--\n" 1677 /*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
2087 "\n"
2088 "Return the Unicode code point for a one-character string.");
2089
2090 #define BUILTIN_ORD_METHODDEF \
2091 {"ord", (PyCFunction)builtin_ord, METH_O, builtin_ord__doc__},
2092
2093 static PyObject *
2094 builtin_ord(PyModuleDef *module, PyObject *c)
2095 /*[clinic end generated code: output=a8466d23bd76db3f input=762355f87451efa3]*/
2096 { 1678 {
2097 long ord; 1679 long ord;
2098 Py_ssize_t size; 1680 Py_ssize_t size;
2099 1681
2100 if (PyBytes_Check(c)) { 1682 if (PyBytes_Check(c)) {
2101 size = PyBytes_GET_SIZE(c); 1683 size = PyBytes_GET_SIZE(c);
2102 if (size == 1) { 1684 if (size == 1) {
2103 ord = (long)((unsigned char)*PyBytes_AS_STRING(c)); 1685 ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
2104 return PyLong_FromLong(ord); 1686 return PyLong_FromLong(ord);
2105 } 1687 }
(...skipping 26 matching lines...) Expand all
2132 "ord() expected a character, " 1714 "ord() expected a character, "
2133 "but string of length %zd found", 1715 "but string of length %zd found",
2134 size); 1716 size);
2135 return NULL; 1717 return NULL;
2136 } 1718 }
2137 1719
2138 1720
2139 /*[clinic input] 1721 /*[clinic input]
2140 pow as builtin_pow 1722 pow as builtin_pow
2141 1723
2142 x: 'O' 1724 x: object
2143 y: 'O' 1725 y: object
2144 z: 'O' = None 1726 z: object = None
2145 / 1727 /
2146 1728
2147 Equivalent to x**y (with two arguments) or x**y % z (with three arguments) 1729 Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
2148 1730
2149 Some types, such as ints, are able to use a more efficient algorithm when 1731 Some types, such as ints, are able to use a more efficient algorithm when
2150 invoked using the three argument form. 1732 invoked using the three argument form.
2151 [clinic start generated code]*/ 1733 [clinic start generated code]*/
2152 1734
2153 PyDoc_STRVAR(builtin_pow__doc__, 1735 static PyObject *
2154 "pow($module, x, y, z=None, /)\n" 1736 builtin_pow_impl(PyObject *module, PyObject *x, PyObject *y, PyObject *z)
2155 "--\n" 1737 /*[clinic end generated code: output=50a14d5d130d404b input=653d57d38d41fc07]*/
2156 "\n"
2157 "Equivalent to x**y (with two arguments) or x**y % z (with three arguments)\n"
2158 "\n"
2159 "Some types, such as ints, are able to use a more efficient algorithm when\n"
2160 "invoked using the three argument form.");
2161
2162 #define BUILTIN_POW_METHODDEF \
2163 {"pow", (PyCFunction)builtin_pow, METH_VARARGS, builtin_pow__doc__},
2164
2165 static PyObject *
2166 builtin_pow_impl(PyModuleDef *module, PyObject *x, PyObject *y, PyObject *z);
2167
2168 static PyObject *
2169 builtin_pow(PyModuleDef *module, PyObject *args)
2170 {
2171 PyObject *return_value = NULL;
2172 PyObject *x;
2173 PyObject *y;
2174 PyObject *z = Py_None;
2175
2176 if (!PyArg_UnpackTuple(args, "pow",
2177 2, 3,
2178 &x, &y, &z))
2179 goto exit;
2180 return_value = builtin_pow_impl(module, x, y, z);
2181
2182 exit:
2183 return return_value;
2184 }
2185
2186 static PyObject *
2187 builtin_pow_impl(PyModuleDef *module, PyObject *x, PyObject *y, PyObject *z)
2188 /*[clinic end generated code: output=d0cdf314311dedba input=561a942d5f5c1899]*/
2189 { 1738 {
2190 return PyNumber_Power(x, y, z); 1739 return PyNumber_Power(x, y, z);
2191 } 1740 }
2192 1741
2193 1742
2194 /* AC: cannot convert yet, waiting for *args support */ 1743 /* AC: cannot convert yet, waiting for *args support */
2195 static PyObject * 1744 static PyObject *
2196 builtin_print(PyObject *self, PyObject *args, PyObject *kwds) 1745 builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
2197 { 1746 {
2198 static char *kwlist[] = {"sep", "end", "file", "flush", 0}; 1747 static char *kwlist[] = {"sep", "end", "file", "flush", 0};
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
2258 err = PyFile_WriteObject(end, file, Py_PRINT_RAW); 1807 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
2259 if (err) 1808 if (err)
2260 return NULL; 1809 return NULL;
2261 1810
2262 if (flush != NULL) { 1811 if (flush != NULL) {
2263 PyObject *tmp; 1812 PyObject *tmp;
2264 int do_flush = PyObject_IsTrue(flush); 1813 int do_flush = PyObject_IsTrue(flush);
2265 if (do_flush == -1) 1814 if (do_flush == -1)
2266 return NULL; 1815 return NULL;
2267 else if (do_flush) { 1816 else if (do_flush) {
2268 tmp = _PyObject_CallMethodId(file, &PyId_flush, ""); 1817 tmp = _PyObject_CallMethodId(file, &PyId_flush, NULL);
2269 if (tmp == NULL) 1818 if (tmp == NULL)
2270 return NULL; 1819 return NULL;
2271 else 1820 else
2272 Py_DECREF(tmp); 1821 Py_DECREF(tmp);
2273 } 1822 }
2274 } 1823 }
2275 1824
2276 Py_RETURN_NONE; 1825 Py_RETURN_NONE;
2277 } 1826 }
2278 1827
(...skipping 16 matching lines...) Expand all
2295 1844
2296 Read a string from standard input. The trailing newline is stripped. 1845 Read a string from standard input. The trailing newline is stripped.
2297 1846
2298 The prompt string, if given, is printed to standard output without a 1847 The prompt string, if given, is printed to standard output without a
2299 trailing newline before reading input. 1848 trailing newline before reading input.
2300 1849
2301 If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. 1850 If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
2302 On *nix systems, readline is used if available. 1851 On *nix systems, readline is used if available.
2303 [clinic start generated code]*/ 1852 [clinic start generated code]*/
2304 1853
2305 PyDoc_STRVAR(builtin_input__doc__, 1854 static PyObject *
2306 "input($module, prompt=None, /)\n" 1855 builtin_input_impl(PyObject *module, PyObject *prompt)
2307 "--\n" 1856 /*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
2308 "\n"
2309 "Read a string from standard input. The trailing newline is stripped.\n"
2310 "\n"
2311 "The prompt string, if given, is printed to standard output without a\n"
2312 "trailing newline before reading input.\n"
2313 "\n"
2314 "If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.\n"
2315 "On *nix systems, readline is used if available.");
2316
2317 #define BUILTIN_INPUT_METHODDEF \
2318 {"input", (PyCFunction)builtin_input, METH_VARARGS, builtin_input__doc__},
2319
2320 static PyObject *
2321 builtin_input_impl(PyModuleDef *module, PyObject *prompt);
2322
2323 static PyObject *
2324 builtin_input(PyModuleDef *module, PyObject *args)
2325 {
2326 PyObject *return_value = NULL;
2327 PyObject *prompt = NULL;
2328
2329 if (!PyArg_UnpackTuple(args, "input",
2330 0, 1,
2331 &prompt))
2332 goto exit;
2333 return_value = builtin_input_impl(module, prompt);
2334
2335 exit:
2336 return return_value;
2337 }
2338
2339 static PyObject *
2340 builtin_input_impl(PyModuleDef *module, PyObject *prompt)
2341 /*[clinic end generated code: output=69323bf5695f7c9c input=5e8bb70c2908fe3c]*/
2342 { 1857 {
2343 PyObject *fin = _PySys_GetObjectId(&PyId_stdin); 1858 PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
2344 PyObject *fout = _PySys_GetObjectId(&PyId_stdout); 1859 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
2345 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); 1860 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
2346 PyObject *tmp; 1861 PyObject *tmp;
2347 long fd; 1862 long fd;
2348 int tty; 1863 int tty;
2349 1864
2350 /* Check that stdin/out/err are intact */ 1865 /* Check that stdin/out/err are intact */
2351 if (fin == NULL || fin == Py_None) { 1866 if (fin == NULL || fin == Py_None) {
2352 PyErr_SetString(PyExc_RuntimeError, 1867 PyErr_SetString(PyExc_RuntimeError,
2353 "input(): lost sys.stdin"); 1868 "input(): lost sys.stdin");
2354 return NULL; 1869 return NULL;
2355 } 1870 }
2356 if (fout == NULL || fout == Py_None) { 1871 if (fout == NULL || fout == Py_None) {
2357 PyErr_SetString(PyExc_RuntimeError, 1872 PyErr_SetString(PyExc_RuntimeError,
2358 "input(): lost sys.stdout"); 1873 "input(): lost sys.stdout");
2359 return NULL; 1874 return NULL;
2360 } 1875 }
2361 if (ferr == NULL || ferr == Py_None) { 1876 if (ferr == NULL || ferr == Py_None) {
2362 PyErr_SetString(PyExc_RuntimeError, 1877 PyErr_SetString(PyExc_RuntimeError,
2363 "input(): lost sys.stderr"); 1878 "input(): lost sys.stderr");
2364 return NULL; 1879 return NULL;
2365 } 1880 }
2366 1881
2367 /* First of all, flush stderr */ 1882 /* First of all, flush stderr */
2368 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, ""); 1883 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
2369 if (tmp == NULL) 1884 if (tmp == NULL)
2370 PyErr_Clear(); 1885 PyErr_Clear();
2371 else 1886 else
2372 Py_DECREF(tmp); 1887 Py_DECREF(tmp);
2373 1888
2374 /* We should only use (GNU) readline if Python's sys.stdin and 1889 /* We should only use (GNU) readline if Python's sys.stdin and
2375 sys.stdout are the same as C's stdin and stdout, because we 1890 sys.stdout are the same as C's stdin and stdout, because we
2376 need to pass it those. */ 1891 need to pass it those. */
2377 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, ""); 1892 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL);
2378 if (tmp == NULL) { 1893 if (tmp == NULL) {
2379 PyErr_Clear(); 1894 PyErr_Clear();
2380 tty = 0; 1895 tty = 0;
2381 } 1896 }
2382 else { 1897 else {
2383 fd = PyLong_AsLong(tmp); 1898 fd = PyLong_AsLong(tmp);
2384 Py_DECREF(tmp); 1899 Py_DECREF(tmp);
2385 if (fd < 0 && PyErr_Occurred()) 1900 if (fd < 0 && PyErr_Occurred())
2386 return NULL; 1901 return NULL;
2387 tty = fd == fileno(stdin) && isatty(fd); 1902 tty = fd == fileno(stdin) && isatty(fd);
2388 } 1903 }
2389 if (tty) { 1904 if (tty) {
2390 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, ""); 1905 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
2391 if (tmp == NULL) 1906 if (tmp == NULL) {
2392 PyErr_Clear(); 1907 PyErr_Clear();
1908 tty = 0;
1909 }
2393 else { 1910 else {
2394 fd = PyLong_AsLong(tmp); 1911 fd = PyLong_AsLong(tmp);
2395 Py_DECREF(tmp); 1912 Py_DECREF(tmp);
2396 if (fd < 0 && PyErr_Occurred()) 1913 if (fd < 0 && PyErr_Occurred())
2397 return NULL; 1914 return NULL;
2398 tty = fd == fileno(stdout) && isatty(fd); 1915 tty = fd == fileno(stdout) && isatty(fd);
2399 } 1916 }
2400 } 1917 }
2401 1918
2402 /* If we're interactive, use (GNU) readline */ 1919 /* If we're interactive, use (GNU) readline */
2403 if (tty) { 1920 if (tty) {
2404 PyObject *po = NULL; 1921 PyObject *po = NULL;
2405 char *promptstr; 1922 char *promptstr;
2406 char *s = NULL; 1923 char *s = NULL;
2407 PyObject *stdin_encoding = NULL, *stdin_errors = NULL; 1924 PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
2408 PyObject *stdout_encoding = NULL, *stdout_errors = NULL; 1925 PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
2409 char *stdin_encoding_str, *stdin_errors_str; 1926 char *stdin_encoding_str, *stdin_errors_str;
2410 PyObject *result; 1927 PyObject *result;
2411 size_t len; 1928 size_t len;
2412 1929
2413 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding); 1930 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
2414 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors); 1931 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
2415 if (!stdin_encoding || !stdin_errors) 1932 if (!stdin_encoding || !stdin_errors)
2416 /* stdin is a text stream, so it must have an 1933 /* stdin is a text stream, so it must have an
2417 encoding. */ 1934 encoding. */
2418 goto _readline_errors; 1935 goto _readline_errors;
2419 stdin_encoding_str = _PyUnicode_AsString(stdin_encoding); 1936 stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2420 stdin_errors_str = _PyUnicode_AsString(stdin_errors); 1937 stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
2421 if (!stdin_encoding_str || !stdin_errors_str) 1938 if (!stdin_encoding_str || !stdin_errors_str)
2422 goto _readline_errors; 1939 goto _readline_errors;
2423 tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); 1940 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
2424 if (tmp == NULL) 1941 if (tmp == NULL)
2425 PyErr_Clear(); 1942 PyErr_Clear();
2426 else 1943 else
2427 Py_DECREF(tmp); 1944 Py_DECREF(tmp);
2428 if (prompt != NULL) { 1945 if (prompt != NULL) {
2429 /* We have a prompt, encode it as stdout would */ 1946 /* We have a prompt, encode it as stdout would */
2430 char *stdout_encoding_str, *stdout_errors_str; 1947 char *stdout_encoding_str, *stdout_errors_str;
2431 PyObject *stringpo; 1948 PyObject *stringpo;
2432 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding); 1949 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
2433 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors); 1950 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
2434 if (!stdout_encoding || !stdout_errors) 1951 if (!stdout_encoding || !stdout_errors)
2435 goto _readline_errors; 1952 goto _readline_errors;
2436 stdout_encoding_str = _PyUnicode_AsString(stdout_encoding); 1953 stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2437 stdout_errors_str = _PyUnicode_AsString(stdout_errors); 1954 stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
2438 if (!stdout_encoding_str || !stdout_errors_str) 1955 if (!stdout_encoding_str || !stdout_errors_str)
2439 goto _readline_errors; 1956 goto _readline_errors;
2440 stringpo = PyObject_Str(prompt); 1957 stringpo = PyObject_Str(prompt);
2441 if (stringpo == NULL) 1958 if (stringpo == NULL)
2442 goto _readline_errors; 1959 goto _readline_errors;
2443 po = PyUnicode_AsEncodedString(stringpo, 1960 po = PyUnicode_AsEncodedString(stringpo,
2444 stdout_encoding_str, stdout_errors_str); 1961 stdout_encoding_str, stdout_errors_str);
2445 Py_CLEAR(stdout_encoding); 1962 Py_CLEAR(stdout_encoding);
2446 Py_CLEAR(stdout_errors); 1963 Py_CLEAR(stdout_errors);
2447 Py_CLEAR(stringpo); 1964 Py_CLEAR(stringpo);
2448 if (po == NULL) 1965 if (po == NULL)
2449 goto _readline_errors; 1966 goto _readline_errors;
2450 promptstr = PyBytes_AsString(po); 1967 assert(PyBytes_Check(po));
2451 if (promptstr == NULL) 1968 promptstr = PyBytes_AS_STRING(po);
2452 goto _readline_errors;
2453 } 1969 }
2454 else { 1970 else {
2455 po = NULL; 1971 po = NULL;
2456 promptstr = ""; 1972 promptstr = "";
2457 } 1973 }
2458 s = PyOS_Readline(stdin, stdout, promptstr); 1974 s = PyOS_Readline(stdin, stdout, promptstr);
2459 if (s == NULL) { 1975 if (s == NULL) {
2460 PyErr_CheckSignals(); 1976 PyErr_CheckSignals();
2461 if (!PyErr_Occurred()) 1977 if (!PyErr_Occurred())
2462 PyErr_SetNone(PyExc_KeyboardInterrupt); 1978 PyErr_SetNone(PyExc_KeyboardInterrupt);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2494 Py_XDECREF(stdout_errors); 2010 Py_XDECREF(stdout_errors);
2495 Py_XDECREF(po); 2011 Py_XDECREF(po);
2496 return NULL; 2012 return NULL;
2497 } 2013 }
2498 2014
2499 /* Fallback if we're not interactive */ 2015 /* Fallback if we're not interactive */
2500 if (prompt != NULL) { 2016 if (prompt != NULL) {
2501 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0) 2017 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
2502 return NULL; 2018 return NULL;
2503 } 2019 }
2504 tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); 2020 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
2505 if (tmp == NULL) 2021 if (tmp == NULL)
2506 PyErr_Clear(); 2022 PyErr_Clear();
2507 else 2023 else
2508 Py_DECREF(tmp); 2024 Py_DECREF(tmp);
2509 return PyFile_GetLine(fin, -1); 2025 return PyFile_GetLine(fin, -1);
2510 } 2026 }
2511 2027
2512 2028
2513 /*[clinic input] 2029 /*[clinic input]
2514 repr as builtin_repr 2030 repr as builtin_repr
2515 2031
2516 obj: 'O' 2032 obj: object
2517 / 2033 /
2518 2034
2519 Return the canonical string representation of the object. 2035 Return the canonical string representation of the object.
2520 2036
2521 For many object types, including most builtins, eval(repr(obj)) == obj. 2037 For many object types, including most builtins, eval(repr(obj)) == obj.
2522 [clinic start generated code]*/ 2038 [clinic start generated code]*/
2523 2039
2524 PyDoc_STRVAR(builtin_repr__doc__, 2040 static PyObject *
2525 "repr($module, obj, /)\n" 2041 builtin_repr(PyObject *module, PyObject *obj)
2526 "--\n" 2042 /*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
2527 "\n"
2528 "Return the canonical string representation of the object.\n"
2529 "\n"
2530 "For many object types, including most builtins, eval(repr(obj)) == obj.");
2531
2532 #define BUILTIN_REPR_METHODDEF \
2533 {"repr", (PyCFunction)builtin_repr, METH_O, builtin_repr__doc__},
2534
2535 static PyObject *
2536 builtin_repr(PyModuleDef *module, PyObject *obj)
2537 /*[clinic end generated code: output=988980120f39e2fa input=a2bca0f38a5a924d]*/
2538 { 2043 {
2539 return PyObject_Repr(obj); 2044 return PyObject_Repr(obj);
2540 } 2045 }
2541 2046
2542 2047
2543 /* AC: cannot convert yet, as needs PEP 457 group support in inspect 2048 /* AC: cannot convert yet, as needs PEP 457 group support in inspect
2544 * or a semantic change to accept None for "ndigits" 2049 * or a semantic change to accept None for "ndigits"
2545 */ 2050 */
2546 static PyObject * 2051 static PyObject *
2547 builtin_round(PyObject *self, PyObject *args, PyObject *kwds) 2052 builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
(...skipping 13 matching lines...) Expand all
2561 2066
2562 round = _PyObject_LookupSpecial(number, &PyId___round__); 2067 round = _PyObject_LookupSpecial(number, &PyId___round__);
2563 if (round == NULL) { 2068 if (round == NULL) {
2564 if (!PyErr_Occurred()) 2069 if (!PyErr_Occurred())
2565 PyErr_Format(PyExc_TypeError, 2070 PyErr_Format(PyExc_TypeError,
2566 "type %.100s doesn't define __round__ method", 2071 "type %.100s doesn't define __round__ method",
2567 Py_TYPE(number)->tp_name); 2072 Py_TYPE(number)->tp_name);
2568 return NULL; 2073 return NULL;
2569 } 2074 }
2570 2075
2571 if (ndigits == NULL) 2076 if (ndigits == NULL || ndigits == Py_None)
2572 result = PyObject_CallFunctionObjArgs(round, NULL); 2077 result = PyObject_CallFunctionObjArgs(round, NULL);
2573 else 2078 else
2574 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL); 2079 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);
2575 Py_DECREF(round); 2080 Py_DECREF(round);
2576 return result; 2081 return result;
2577 } 2082 }
2578 2083
2579 PyDoc_STRVAR(round_doc, 2084 PyDoc_STRVAR(round_doc,
2580 "round(number[, ndigits]) -> number\n\ 2085 "round(number[, ndigits]) -> number\n\
2581 \n\ 2086 \n\
2582 Round a number to a given precision in decimal digits (default 0 digits).\n\ 2087 Round a number to a given precision in decimal digits (default 0 digits).\n\
2583 This returns an int when called with one argument, otherwise the\n\ 2088 This returns an int when called with one argument, otherwise the\n\
2584 same type as the number. ndigits may be negative."); 2089 same type as the number. ndigits may be negative.");
2585 2090
2586 2091
2587 /*AC: we need to keep the kwds dict intact to easily call into the 2092 /*AC: we need to keep the kwds dict intact to easily call into the
2588 * list.sort method, which isn't currently supported in AC. So we just use 2093 * list.sort method, which isn't currently supported in AC. So we just use
2589 * the initially generated signature with a custom implementation. 2094 * the initially generated signature with a custom implementation.
2590 */ 2095 */
2591 /* [disabled clinic input] 2096 /* [disabled clinic input]
2592 sorted as builtin_sorted 2097 sorted as builtin_sorted
2593 2098
2594 iterable as seq: 'O' 2099 iterable as seq: object
2595 key as keyfunc: 'O' = None 2100 key as keyfunc: object = None
2596 reverse: 'O' = False 2101 reverse: object = False
2597 2102
2598 Return a new list containing all items from the iterable in ascending order. 2103 Return a new list containing all items from the iterable in ascending order.
2599 2104
2600 A custom key function can be supplied to customise the sort order, and the 2105 A custom key function can be supplied to customize the sort order, and the
2601 reverse flag can be set to request the result in descending order. 2106 reverse flag can be set to request the result in descending order.
2602 [end disabled clinic input]*/ 2107 [end disabled clinic input]*/
2603 2108
2604 PyDoc_STRVAR(builtin_sorted__doc__, 2109 PyDoc_STRVAR(builtin_sorted__doc__,
2605 "sorted($module, iterable, key=None, reverse=False)\n" 2110 "sorted($module, iterable, key=None, reverse=False)\n"
2606 "--\n" 2111 "--\n"
2607 "\n" 2112 "\n"
2608 "Return a new list containing all items from the iterable in ascending order.\n" 2113 "Return a new list containing all items from the iterable in ascending order.\n"
2609 "\n" 2114 "\n"
2610 "A custom key function can be supplied to customise the sort order, and the\n" 2115 "A custom key function can be supplied to customize the sort order, and the\n"
2611 "reverse flag can be set to request the result in descending order."); 2116 "reverse flag can be set to request the result in descending order.");
2612 2117
2613 #define BUILTIN_SORTED_METHODDEF \ 2118 #define BUILTIN_SORTED_METHODDEF \
2614 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS|METH_KEYWORDS, builtin_ sorted__doc__}, 2119 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS|METH_KEYWORDS, builtin_ sorted__doc__},
2615 2120
2616 static PyObject * 2121 static PyObject *
2617 builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) 2122 builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
2618 { 2123 {
2619 PyObject *newlist, *v, *seq, *keyfunc=NULL, *newargs; 2124 PyObject *newlist, *v, *seq, *keyfunc=NULL, **newargs;
2620 PyObject *callable; 2125 PyObject *callable;
2621 static char *kwlist[] = {"iterable", "key", "reverse", 0}; 2126 static char *kwlist[] = {"iterable", "key", "reverse", 0};
2622 int reverse; 2127 int reverse;
2128 Py_ssize_t nargs;
2623 2129
2624 /* args 1-3 should match listsort in Objects/listobject.c */ 2130 /* args 1-3 should match listsort in Objects/listobject.c */
2625 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted", 2131 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted",
2626 kwlist, &seq, &keyfunc, &reverse)) 2132 kwlist, &seq, &keyfunc, &reverse))
2627 return NULL; 2133 return NULL;
2628 2134
2629 newlist = PySequence_List(seq); 2135 newlist = PySequence_List(seq);
2630 if (newlist == NULL) 2136 if (newlist == NULL)
2631 return NULL; 2137 return NULL;
2632 2138
2633 callable = _PyObject_GetAttrId(newlist, &PyId_sort); 2139 callable = _PyObject_GetAttrId(newlist, &PyId_sort);
2634 if (callable == NULL) { 2140 if (callable == NULL) {
2635 Py_DECREF(newlist); 2141 Py_DECREF(newlist);
2636 return NULL; 2142 return NULL;
2637 } 2143 }
2638 2144
2639 newargs = PyTuple_GetSlice(args, 1, 4); 2145 newargs = &PyTuple_GET_ITEM(args, 1);
2640 if (newargs == NULL) { 2146 nargs = PyTuple_GET_SIZE(args) - 1;
2641 Py_DECREF(newlist); 2147 v = _PyObject_FastCallDict(callable, newargs, nargs, kwds);
2642 Py_DECREF(callable);
2643 return NULL;
2644 }
2645
2646 v = PyObject_Call(callable, newargs, kwds);
2647 Py_DECREF(newargs);
2648 Py_DECREF(callable); 2148 Py_DECREF(callable);
2649 if (v == NULL) { 2149 if (v == NULL) {
2650 Py_DECREF(newlist); 2150 Py_DECREF(newlist);
2651 return NULL; 2151 return NULL;
2652 } 2152 }
2653 Py_DECREF(v); 2153 Py_DECREF(v);
2654 return newlist; 2154 return newlist;
2655 } 2155 }
2656 2156
2657 2157
(...skipping 26 matching lines...) Expand all
2684 PyDoc_STRVAR(vars_doc, 2184 PyDoc_STRVAR(vars_doc,
2685 "vars([object]) -> dictionary\n\ 2185 "vars([object]) -> dictionary\n\
2686 \n\ 2186 \n\
2687 Without arguments, equivalent to locals().\n\ 2187 Without arguments, equivalent to locals().\n\
2688 With an argument, equivalent to object.__dict__."); 2188 With an argument, equivalent to object.__dict__.");
2689 2189
2690 2190
2691 /*[clinic input] 2191 /*[clinic input]
2692 sum as builtin_sum 2192 sum as builtin_sum
2693 2193
2694 iterable: 'O' 2194 iterable: object
2695 start: object(c_default="NULL") = 0 2195 start: object(c_default="NULL") = 0
2696 / 2196 /
2697 2197
2698 Return the sum of a 'start' value (default: 0) plus an iterable of numbers 2198 Return the sum of a 'start' value (default: 0) plus an iterable of numbers
2699 2199
2700 When the iterable is empty, return the start value. 2200 When the iterable is empty, return the start value.
2701 This function is intended specifically for use with numeric values and may 2201 This function is intended specifically for use with numeric values and may
2702 reject non-numeric types. 2202 reject non-numeric types.
2703 [clinic start generated code]*/ 2203 [clinic start generated code]*/
2704 2204
2705 PyDoc_STRVAR(builtin_sum__doc__, 2205 static PyObject *
2706 "sum($module, iterable, start=0, /)\n" 2206 builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
2707 "--\n" 2207 /*[clinic end generated code: output=df758cec7d1d302f input=3b5b7a9d7611c73a]*/
2708 "\n"
2709 "Return the sum of a \'start\' value (default: 0) plus an iterable of numbers\n"
2710 "\n"
2711 "When the iterable is empty, return the start value.\n"
2712 "This function is intended specifically for use with numeric values and may\n"
2713 "reject non-numeric types.");
2714
2715 #define BUILTIN_SUM_METHODDEF \
2716 {"sum", (PyCFunction)builtin_sum, METH_VARARGS, builtin_sum__doc__},
2717
2718 static PyObject *
2719 builtin_sum_impl(PyModuleDef *module, PyObject *iterable, PyObject *start);
2720
2721 static PyObject *
2722 builtin_sum(PyModuleDef *module, PyObject *args)
2723 {
2724 PyObject *return_value = NULL;
2725 PyObject *iterable;
2726 PyObject *start = NULL;
2727
2728 if (!PyArg_UnpackTuple(args, "sum",
2729 1, 2,
2730 &iterable, &start))
2731 goto exit;
2732 return_value = builtin_sum_impl(module, iterable, start);
2733
2734 exit:
2735 return return_value;
2736 }
2737
2738 static PyObject *
2739 builtin_sum_impl(PyModuleDef *module, PyObject *iterable, PyObject *start)
2740 /*[clinic end generated code: output=b42652a0d5f64f6b input=90ae7a242cfcf025]*/
2741 { 2208 {
2742 PyObject *result = start; 2209 PyObject *result = start;
2743 PyObject *temp, *item, *iter; 2210 PyObject *temp, *item, *iter;
2744 2211
2745 iter = PyObject_GetIter(iterable); 2212 iter = PyObject_GetIter(iterable);
2746 if (iter == NULL) 2213 if (iter == NULL)
2747 return NULL; 2214 return NULL;
2748 2215
2749 if (result == NULL) { 2216 if (result == NULL) {
2750 result = PyLong_FromLong(0); 2217 result = PyLong_FromLong(0);
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
2894 break; 2361 break;
2895 } 2362 }
2896 Py_DECREF(iter); 2363 Py_DECREF(iter);
2897 return result; 2364 return result;
2898 } 2365 }
2899 2366
2900 2367
2901 /*[clinic input] 2368 /*[clinic input]
2902 isinstance as builtin_isinstance 2369 isinstance as builtin_isinstance
2903 2370
2904 obj: 'O' 2371 obj: object
2905 class_or_tuple: 'O' 2372 class_or_tuple: object
2906 / 2373 /
2907 2374
2908 Return whether an object is an instance of a class or of a subclass thereof. 2375 Return whether an object is an instance of a class or of a subclass thereof.
2909 2376
2910 A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to 2377 A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
2911 check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B) 2378 check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
2912 or ...`` etc. 2379 or ...`` etc.
2913 [clinic start generated code]*/ 2380 [clinic start generated code]*/
2914 2381
2915 PyDoc_STRVAR(builtin_isinstance__doc__, 2382 static PyObject *
2916 "isinstance($module, obj, class_or_tuple, /)\n" 2383 builtin_isinstance_impl(PyObject *module, PyObject *obj,
2917 "--\n" 2384 PyObject *class_or_tuple)
2918 "\n" 2385 /*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
2919 "Return whether an object is an instance of a class or of a subclass thereof.\n"
2920 "\n"
2921 "A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to\n"
2922 "check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)\n"
2923 "or ...`` etc.");
2924
2925 #define BUILTIN_ISINSTANCE_METHODDEF \
2926 {"isinstance", (PyCFunction)builtin_isinstance, METH_VARARGS, builtin_isinst ance__doc__},
2927
2928 static PyObject *
2929 builtin_isinstance_impl(PyModuleDef *module, PyObject *obj, PyObject *class_or_t uple);
2930
2931 static PyObject *
2932 builtin_isinstance(PyModuleDef *module, PyObject *args)
2933 {
2934 PyObject *return_value = NULL;
2935 PyObject *obj;
2936 PyObject *class_or_tuple;
2937
2938 if (!PyArg_UnpackTuple(args, "isinstance",
2939 2, 2,
2940 &obj, &class_or_tuple))
2941 goto exit;
2942 return_value = builtin_isinstance_impl(module, obj, class_or_tuple);
2943
2944 exit:
2945 return return_value;
2946 }
2947
2948 static PyObject *
2949 builtin_isinstance_impl(PyModuleDef *module, PyObject *obj, PyObject *class_or_t uple)
2950 /*[clinic end generated code: output=847df57fef8ddea7 input=cf9eb0ad6bb9bad6]*/
2951 { 2386 {
2952 int retval; 2387 int retval;
2953 2388
2954 retval = PyObject_IsInstance(obj, class_or_tuple); 2389 retval = PyObject_IsInstance(obj, class_or_tuple);
2955 if (retval < 0) 2390 if (retval < 0)
2956 return NULL; 2391 return NULL;
2957 return PyBool_FromLong(retval); 2392 return PyBool_FromLong(retval);
2958 } 2393 }
2959 2394
2960 2395
2961 /*[clinic input] 2396 /*[clinic input]
2962 issubclass as builtin_issubclass 2397 issubclass as builtin_issubclass
2963 2398
2964 cls: 'O' 2399 cls: object
2965 class_or_tuple: 'O' 2400 class_or_tuple: object
2966 / 2401 /
2967 2402
2968 Return whether 'cls' is a derived from another class or is the same class. 2403 Return whether 'cls' is a derived from another class or is the same class.
2969 2404
2970 A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to 2405 A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
2971 check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B) 2406 check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
2972 or ...`` etc. 2407 or ...`` etc.
2973 [clinic start generated code]*/ 2408 [clinic start generated code]*/
2974 2409
2975 PyDoc_STRVAR(builtin_issubclass__doc__, 2410 static PyObject *
2976 "issubclass($module, cls, class_or_tuple, /)\n" 2411 builtin_issubclass_impl(PyObject *module, PyObject *cls,
2977 "--\n" 2412 PyObject *class_or_tuple)
2978 "\n" 2413 /*[clinic end generated code: output=358412410cd7a250 input=af5f35e9ceaddaf6]*/
2979 "Return whether \'cls\' is a derived from another class or is the same class.\n"
2980 "\n"
2981 "A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to\n"
2982 "check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)\n"
2983 "or ...`` etc.");
2984
2985 #define BUILTIN_ISSUBCLASS_METHODDEF \
2986 {"issubclass", (PyCFunction)builtin_issubclass, METH_VARARGS, builtin_issubc lass__doc__},
2987
2988 static PyObject *
2989 builtin_issubclass_impl(PyModuleDef *module, PyObject *cls, PyObject *class_or_t uple);
2990
2991 static PyObject *
2992 builtin_issubclass(PyModuleDef *module, PyObject *args)
2993 {
2994 PyObject *return_value = NULL;
2995 PyObject *cls;
2996 PyObject *class_or_tuple;
2997
2998 if (!PyArg_UnpackTuple(args, "issubclass",
2999 2, 2,
3000 &cls, &class_or_tuple))
3001 goto exit;
3002 return_value = builtin_issubclass_impl(module, cls, class_or_tuple);
3003
3004 exit:
3005 return return_value;
3006 }
3007
3008 static PyObject *
3009 builtin_issubclass_impl(PyModuleDef *module, PyObject *cls, PyObject *class_or_t uple)
3010 /*[clinic end generated code: output=a0f8c03692e35474 input=923d03fa41fc352a]*/
3011 { 2414 {
3012 int retval; 2415 int retval;
3013 2416
3014 retval = PyObject_IsSubclass(cls, class_or_tuple); 2417 retval = PyObject_IsSubclass(cls, class_or_tuple);
3015 if (retval < 0) 2418 if (retval < 0)
3016 return NULL; 2419 return NULL;
3017 return PyBool_FromLong(retval); 2420 return PyBool_FromLong(retval);
3018 } 2421 }
3019 2422
3020 2423
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
3329 SETBUILTIN("set", &PySet_Type); 2732 SETBUILTIN("set", &PySet_Type);
3330 SETBUILTIN("slice", &PySlice_Type); 2733 SETBUILTIN("slice", &PySlice_Type);
3331 SETBUILTIN("staticmethod", &PyStaticMethod_Type); 2734 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
3332 SETBUILTIN("str", &PyUnicode_Type); 2735 SETBUILTIN("str", &PyUnicode_Type);
3333 SETBUILTIN("super", &PySuper_Type); 2736 SETBUILTIN("super", &PySuper_Type);
3334 SETBUILTIN("tuple", &PyTuple_Type); 2737 SETBUILTIN("tuple", &PyTuple_Type);
3335 SETBUILTIN("type", &PyType_Type); 2738 SETBUILTIN("type", &PyType_Type);
3336 SETBUILTIN("zip", &PyZip_Type); 2739 SETBUILTIN("zip", &PyZip_Type);
3337 debug = PyBool_FromLong(Py_OptimizeFlag == 0); 2740 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
3338 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { 2741 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
3339 Py_XDECREF(debug); 2742 Py_DECREF(debug);
3340 return NULL; 2743 return NULL;
3341 } 2744 }
3342 Py_XDECREF(debug); 2745 Py_DECREF(debug);
3343 2746
3344 return mod; 2747 return mod;
3345 #undef ADD_TO_ALL 2748 #undef ADD_TO_ALL
3346 #undef SETBUILTIN 2749 #undef SETBUILTIN
3347 } 2750 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+