1 /* Built-in functions */
2 
3 #include "Python.h"
4 #include <ctype.h>
5 #include "pycore_ast.h"           // _PyAST_Validate()
6 #include "pycore_call.h"          // _PyObject_CallNoArgs()
7 #include "pycore_compile.h"       // _PyAST_Compile()
8 #include "pycore_object.h"        // _Py_AddToAllObjects()
9 #include "pycore_pyerrors.h"      // _PyErr_NoMemory()
10 #include "pycore_pystate.h"       // _PyThreadState_GET()
11 #include "pycore_tuple.h"         // _PyTuple_FromArray()
12 #include "pycore_ceval.h"         // _PyEval_Vector()
13 
14 _Py_IDENTIFIER(__builtins__);
15 _Py_IDENTIFIER(__dict__);
16 _Py_IDENTIFIER(__prepare__);
17 _Py_IDENTIFIER(__round__);
18 _Py_IDENTIFIER(__mro_entries__);
19 _Py_IDENTIFIER(encoding);
20 _Py_IDENTIFIER(errors);
21 _Py_IDENTIFIER(fileno);
22 _Py_IDENTIFIER(flush);
23 _Py_IDENTIFIER(metaclass);
24 _Py_IDENTIFIER(sort);
25 _Py_IDENTIFIER(stdin);
26 _Py_IDENTIFIER(stdout);
27 _Py_IDENTIFIER(stderr);
28 
29 #include "clinic/bltinmodule.c.h"
30 
31 static PyObject*
update_bases(PyObject * bases,PyObject * const * args,Py_ssize_t nargs)32 update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
33 {
34     Py_ssize_t i, j;
35     PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
36     assert(PyTuple_Check(bases));
37 
38     for (i = 0; i < nargs; i++) {
39         base  = args[i];
40         if (PyType_Check(base)) {
41             if (new_bases) {
42                 /* If we already have made a replacement, then we append every normal base,
43                    otherwise just skip it. */
44                 if (PyList_Append(new_bases, base) < 0) {
45                     goto error;
46                 }
47             }
48             continue;
49         }
50         if (_PyObject_LookupAttrId(base, &PyId___mro_entries__, &meth) < 0) {
51             goto error;
52         }
53         if (!meth) {
54             if (new_bases) {
55                 if (PyList_Append(new_bases, base) < 0) {
56                     goto error;
57                 }
58             }
59             continue;
60         }
61         new_base = PyObject_CallOneArg(meth, bases);
62         Py_DECREF(meth);
63         if (!new_base) {
64             goto error;
65         }
66         if (!PyTuple_Check(new_base)) {
67             PyErr_SetString(PyExc_TypeError,
68                             "__mro_entries__ must return a tuple");
69             Py_DECREF(new_base);
70             goto error;
71         }
72         if (!new_bases) {
73             /* If this is a first successful replacement, create new_bases list and
74                copy previously encountered bases. */
75             if (!(new_bases = PyList_New(i))) {
76                 Py_DECREF(new_base);
77                 goto error;
78             }
79             for (j = 0; j < i; j++) {
80                 base = args[j];
81                 PyList_SET_ITEM(new_bases, j, base);
82                 Py_INCREF(base);
83             }
84         }
85         j = PyList_GET_SIZE(new_bases);
86         if (PyList_SetSlice(new_bases, j, j, new_base) < 0) {
87             Py_DECREF(new_base);
88             goto error;
89         }
90         Py_DECREF(new_base);
91     }
92     if (!new_bases) {
93         return bases;
94     }
95     result = PyList_AsTuple(new_bases);
96     Py_DECREF(new_bases);
97     return result;
98 
99 error:
100     Py_XDECREF(new_bases);
101     return NULL;
102 }
103 
104 /* AC: cannot convert yet, waiting for *args support */
105 static PyObject *
builtin___build_class__(PyObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)106 builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
107                         PyObject *kwnames)
108 {
109     PyObject *func, *name, *winner, *prep;
110     PyObject *cls = NULL, *cell = NULL, *ns = NULL, *meta = NULL, *orig_bases = NULL;
111     PyObject *mkw = NULL, *bases = NULL;
112     int isclass = 0;   /* initialize to prevent gcc warning */
113 
114     if (nargs < 2) {
115         PyErr_SetString(PyExc_TypeError,
116                         "__build_class__: not enough arguments");
117         return NULL;
118     }
119     func = args[0];   /* Better be callable */
120     if (!PyFunction_Check(func)) {
121         PyErr_SetString(PyExc_TypeError,
122                         "__build_class__: func must be a function");
123         return NULL;
124     }
125     name = args[1];
126     if (!PyUnicode_Check(name)) {
127         PyErr_SetString(PyExc_TypeError,
128                         "__build_class__: name is not a string");
129         return NULL;
130     }
131     orig_bases = _PyTuple_FromArray(args + 2, nargs - 2);
132     if (orig_bases == NULL)
133         return NULL;
134 
135     bases = update_bases(orig_bases, args + 2, nargs - 2);
136     if (bases == NULL) {
137         Py_DECREF(orig_bases);
138         return NULL;
139     }
140 
141     if (kwnames == NULL) {
142         meta = NULL;
143         mkw = NULL;
144     }
145     else {
146         mkw = _PyStack_AsDict(args + nargs, kwnames);
147         if (mkw == NULL) {
148             goto error;
149         }
150 
151         meta = _PyDict_GetItemIdWithError(mkw, &PyId_metaclass);
152         if (meta != NULL) {
153             Py_INCREF(meta);
154             if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) {
155                 goto error;
156             }
157             /* metaclass is explicitly given, check if it's indeed a class */
158             isclass = PyType_Check(meta);
159         }
160         else if (PyErr_Occurred()) {
161             goto error;
162         }
163     }
164     if (meta == NULL) {
165         /* if there are no bases, use type: */
166         if (PyTuple_GET_SIZE(bases) == 0) {
167             meta = (PyObject *) (&PyType_Type);
168         }
169         /* else get the type of the first base */
170         else {
171             PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
172             meta = (PyObject *)Py_TYPE(base0);
173         }
174         Py_INCREF(meta);
175         isclass = 1;  /* meta is really a class */
176     }
177 
178     if (isclass) {
179         /* meta is really a class, so check for a more derived
180            metaclass, or possible metaclass conflicts: */
181         winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
182                                                         bases);
183         if (winner == NULL) {
184             goto error;
185         }
186         if (winner != meta) {
187             Py_DECREF(meta);
188             meta = winner;
189             Py_INCREF(meta);
190         }
191     }
192     /* else: meta is not a class, so we cannot do the metaclass
193        calculation, so we will use the explicitly given object as it is */
194     if (_PyObject_LookupAttrId(meta, &PyId___prepare__, &prep) < 0) {
195         ns = NULL;
196     }
197     else if (prep == NULL) {
198         ns = PyDict_New();
199     }
200     else {
201         PyObject *pargs[2] = {name, bases};
202         ns = PyObject_VectorcallDict(prep, pargs, 2, mkw);
203         Py_DECREF(prep);
204     }
205     if (ns == NULL) {
206         goto error;
207     }
208     if (!PyMapping_Check(ns)) {
209         PyErr_Format(PyExc_TypeError,
210                      "%.200s.__prepare__() must return a mapping, not %.200s",
211                      isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
212                      Py_TYPE(ns)->tp_name);
213         goto error;
214     }
215     PyThreadState *tstate = _PyThreadState_GET();
216     cell = _PyEval_Vector(tstate, (PyFunctionObject *)func, ns, NULL, 0, NULL);
217     if (cell != NULL) {
218         if (bases != orig_bases) {
219             if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
220                 goto error;
221             }
222         }
223         PyObject *margs[3] = {name, bases, ns};
224         cls = PyObject_VectorcallDict(meta, margs, 3, mkw);
225         if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
226             PyObject *cell_cls = PyCell_GET(cell);
227             if (cell_cls != cls) {
228                 if (cell_cls == NULL) {
229                     const char *msg =
230                         "__class__ not set defining %.200R as %.200R. "
231                         "Was __classcell__ propagated to type.__new__?";
232                     PyErr_Format(PyExc_RuntimeError, msg, name, cls);
233                 } else {
234                     const char *msg =
235                         "__class__ set to %.200R defining %.200R as %.200R";
236                     PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
237                 }
238                 Py_DECREF(cls);
239                 cls = NULL;
240                 goto error;
241             }
242         }
243     }
244 error:
245     Py_XDECREF(cell);
246     Py_XDECREF(ns);
247     Py_XDECREF(meta);
248     Py_XDECREF(mkw);
249     if (bases != orig_bases) {
250         Py_DECREF(orig_bases);
251     }
252     Py_DECREF(bases);
253     return cls;
254 }
255 
256 PyDoc_STRVAR(build_class_doc,
257 "__build_class__(func, name, /, *bases, [metaclass], **kwds) -> class\n\
258 \n\
259 Internal helper function used by the class statement.");
260 
261 static PyObject *
builtin___import__(PyObject * self,PyObject * args,PyObject * kwds)262 builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
263 {
264     static char *kwlist[] = {"name", "globals", "locals", "fromlist",
265                              "level", 0};
266     PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
267     int level = 0;
268 
269     if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
270                     kwlist, &name, &globals, &locals, &fromlist, &level))
271         return NULL;
272     return PyImport_ImportModuleLevelObject(name, globals, locals,
273                                             fromlist, level);
274 }
275 
276 PyDoc_STRVAR(import_doc,
277 "__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module\n\
278 \n\
279 Import a module. Because this function is meant for use by the Python\n\
280 interpreter and not for general use, it is better to use\n\
281 importlib.import_module() to programmatically import a module.\n\
282 \n\
283 The globals argument is only used to determine the context;\n\
284 they are not modified.  The locals argument is unused.  The fromlist\n\
285 should be a list of names to emulate ``from name import ...'', or an\n\
286 empty list to emulate ``import name''.\n\
287 When importing a module from a package, note that __import__('A.B', ...)\n\
288 returns package A when fromlist is empty, but its submodule B when\n\
289 fromlist is not empty.  The level argument is used to determine whether to\n\
290 perform absolute or relative imports: 0 is absolute, while a positive number\n\
291 is the number of parent directories to search relative to the current module.");
292 
293 
294 /*[clinic input]
295 abs as builtin_abs
296 
297     x: object
298     /
299 
300 Return the absolute value of the argument.
301 [clinic start generated code]*/
302 
303 static PyObject *
builtin_abs(PyObject * module,PyObject * x)304 builtin_abs(PyObject *module, PyObject *x)
305 /*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
306 {
307     return PyNumber_Absolute(x);
308 }
309 
310 /*[clinic input]
311 all as builtin_all
312 
313     iterable: object
314     /
315 
316 Return True if bool(x) is True for all values x in the iterable.
317 
318 If the iterable is empty, return True.
319 [clinic start generated code]*/
320 
321 static PyObject *
builtin_all(PyObject * module,PyObject * iterable)322 builtin_all(PyObject *module, PyObject *iterable)
323 /*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
324 {
325     PyObject *it, *item;
326     PyObject *(*iternext)(PyObject *);
327     int cmp;
328 
329     it = PyObject_GetIter(iterable);
330     if (it == NULL)
331         return NULL;
332     iternext = *Py_TYPE(it)->tp_iternext;
333 
334     for (;;) {
335         item = iternext(it);
336         if (item == NULL)
337             break;
338         cmp = PyObject_IsTrue(item);
339         Py_DECREF(item);
340         if (cmp < 0) {
341             Py_DECREF(it);
342             return NULL;
343         }
344         if (cmp == 0) {
345             Py_DECREF(it);
346             Py_RETURN_FALSE;
347         }
348     }
349     Py_DECREF(it);
350     if (PyErr_Occurred()) {
351         if (PyErr_ExceptionMatches(PyExc_StopIteration))
352             PyErr_Clear();
353         else
354             return NULL;
355     }
356     Py_RETURN_TRUE;
357 }
358 
359 /*[clinic input]
360 any as builtin_any
361 
362     iterable: object
363     /
364 
365 Return True if bool(x) is True for any x in the iterable.
366 
367 If the iterable is empty, return False.
368 [clinic start generated code]*/
369 
370 static PyObject *
builtin_any(PyObject * module,PyObject * iterable)371 builtin_any(PyObject *module, PyObject *iterable)
372 /*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/
373 {
374     PyObject *it, *item;
375     PyObject *(*iternext)(PyObject *);
376     int cmp;
377 
378     it = PyObject_GetIter(iterable);
379     if (it == NULL)
380         return NULL;
381     iternext = *Py_TYPE(it)->tp_iternext;
382 
383     for (;;) {
384         item = iternext(it);
385         if (item == NULL)
386             break;
387         cmp = PyObject_IsTrue(item);
388         Py_DECREF(item);
389         if (cmp < 0) {
390             Py_DECREF(it);
391             return NULL;
392         }
393         if (cmp > 0) {
394             Py_DECREF(it);
395             Py_RETURN_TRUE;
396         }
397     }
398     Py_DECREF(it);
399     if (PyErr_Occurred()) {
400         if (PyErr_ExceptionMatches(PyExc_StopIteration))
401             PyErr_Clear();
402         else
403             return NULL;
404     }
405     Py_RETURN_FALSE;
406 }
407 
408 /*[clinic input]
409 ascii as builtin_ascii
410 
411     obj: object
412     /
413 
414 Return an ASCII-only representation of an object.
415 
416 As repr(), return a string containing a printable representation of an
417 object, but escape the non-ASCII characters in the string returned by
418 repr() using \\x, \\u or \\U escapes. This generates a string similar
419 to that returned by repr() in Python 2.
420 [clinic start generated code]*/
421 
422 static PyObject *
builtin_ascii(PyObject * module,PyObject * obj)423 builtin_ascii(PyObject *module, PyObject *obj)
424 /*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
425 {
426     return PyObject_ASCII(obj);
427 }
428 
429 
430 /*[clinic input]
431 bin as builtin_bin
432 
433     number: object
434     /
435 
436 Return the binary representation of an integer.
437 
438    >>> bin(2796202)
439    '0b1010101010101010101010'
440 [clinic start generated code]*/
441 
442 static PyObject *
builtin_bin(PyObject * module,PyObject * number)443 builtin_bin(PyObject *module, PyObject *number)
444 /*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
445 {
446     return PyNumber_ToBase(number, 2);
447 }
448 
449 
450 /*[clinic input]
451 callable as builtin_callable
452 
453     obj: object
454     /
455 
456 Return whether the object is callable (i.e., some kind of function).
457 
458 Note that classes are callable, as are instances of classes with a
459 __call__() method.
460 [clinic start generated code]*/
461 
462 static PyObject *
builtin_callable(PyObject * module,PyObject * obj)463 builtin_callable(PyObject *module, PyObject *obj)
464 /*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
465 {
466     return PyBool_FromLong((long)PyCallable_Check(obj));
467 }
468 
469 static PyObject *
builtin_breakpoint(PyObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * keywords)470 builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
471 {
472     PyObject *hook = PySys_GetObject("breakpointhook");
473 
474     if (hook == NULL) {
475         PyErr_SetString(PyExc_RuntimeError, "lost sys.breakpointhook");
476         return NULL;
477     }
478 
479     if (PySys_Audit("builtins.breakpoint", "O", hook) < 0) {
480         return NULL;
481     }
482 
483     Py_INCREF(hook);
484     PyObject *retval = PyObject_Vectorcall(hook, args, nargs, keywords);
485     Py_DECREF(hook);
486     return retval;
487 }
488 
489 PyDoc_STRVAR(breakpoint_doc,
490 "breakpoint(*args, **kws)\n\
491 \n\
492 Call sys.breakpointhook(*args, **kws).  sys.breakpointhook() must accept\n\
493 whatever arguments are passed.\n\
494 \n\
495 By default, this drops you into the pdb debugger.");
496 
497 typedef struct {
498     PyObject_HEAD
499     PyObject *func;
500     PyObject *it;
501 } filterobject;
502 
503 static PyObject *
filter_new(PyTypeObject * type,PyObject * args,PyObject * kwds)504 filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
505 {
506     PyObject *func, *seq;
507     PyObject *it;
508     filterobject *lz;
509 
510     if ((type == &PyFilter_Type || type->tp_init == PyFilter_Type.tp_init) &&
511         !_PyArg_NoKeywords("filter", kwds))
512         return NULL;
513 
514     if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
515         return NULL;
516 
517     /* Get iterator. */
518     it = PyObject_GetIter(seq);
519     if (it == NULL)
520         return NULL;
521 
522     /* create filterobject structure */
523     lz = (filterobject *)type->tp_alloc(type, 0);
524     if (lz == NULL) {
525         Py_DECREF(it);
526         return NULL;
527     }
528 
529     lz->func = Py_NewRef(func);
530     lz->it = it;
531 
532     return (PyObject *)lz;
533 }
534 
535 static PyObject *
filter_vectorcall(PyObject * type,PyObject * const * args,size_t nargsf,PyObject * kwnames)536 filter_vectorcall(PyObject *type, PyObject * const*args,
537                 size_t nargsf, PyObject *kwnames)
538 {
539     PyTypeObject *tp = (PyTypeObject *)type;
540     if (tp == &PyFilter_Type && !_PyArg_NoKwnames("filter", kwnames)) {
541         return NULL;
542     }
543 
544     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
545     if (!_PyArg_CheckPositional("filter", nargs, 2, 2)) {
546         return NULL;
547     }
548 
549     PyObject *it = PyObject_GetIter(args[1]);
550     if (it == NULL) {
551         return NULL;
552     }
553 
554     filterobject *lz = (filterobject *)tp->tp_alloc(tp, 0);
555 
556     if (lz == NULL) {
557         Py_DECREF(it);
558         return NULL;
559     }
560 
561     lz->func = Py_NewRef(args[0]);
562     lz->it = it;
563 
564     return (PyObject *)lz;
565 }
566 
567 static void
filter_dealloc(filterobject * lz)568 filter_dealloc(filterobject *lz)
569 {
570     PyObject_GC_UnTrack(lz);
571     Py_XDECREF(lz->func);
572     Py_XDECREF(lz->it);
573     Py_TYPE(lz)->tp_free(lz);
574 }
575 
576 static int
filter_traverse(filterobject * lz,visitproc visit,void * arg)577 filter_traverse(filterobject *lz, visitproc visit, void *arg)
578 {
579     Py_VISIT(lz->it);
580     Py_VISIT(lz->func);
581     return 0;
582 }
583 
584 static PyObject *
filter_next(filterobject * lz)585 filter_next(filterobject *lz)
586 {
587     PyObject *item;
588     PyObject *it = lz->it;
589     long ok;
590     PyObject *(*iternext)(PyObject *);
591     int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
592 
593     iternext = *Py_TYPE(it)->tp_iternext;
594     for (;;) {
595         item = iternext(it);
596         if (item == NULL)
597             return NULL;
598 
599         if (checktrue) {
600             ok = PyObject_IsTrue(item);
601         } else {
602             PyObject *good;
603             good = PyObject_CallOneArg(lz->func, item);
604             if (good == NULL) {
605                 Py_DECREF(item);
606                 return NULL;
607             }
608             ok = PyObject_IsTrue(good);
609             Py_DECREF(good);
610         }
611         if (ok > 0)
612             return item;
613         Py_DECREF(item);
614         if (ok < 0)
615             return NULL;
616     }
617 }
618 
619 static PyObject *
filter_reduce(filterobject * lz,PyObject * Py_UNUSED (ignored))620 filter_reduce(filterobject *lz, PyObject *Py_UNUSED(ignored))
621 {
622     return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
623 }
624 
625 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
626 
627 static PyMethodDef filter_methods[] = {
628     {"__reduce__",   (PyCFunction)filter_reduce,   METH_NOARGS, reduce_doc},
629     {NULL,           NULL}           /* sentinel */
630 };
631 
632 PyDoc_STRVAR(filter_doc,
633 "filter(function or None, iterable) --> filter object\n\
634 \n\
635 Return an iterator yielding those items of iterable for which function(item)\n\
636 is true. If function is None, return the items that are true.");
637 
638 PyTypeObject PyFilter_Type = {
639     PyVarObject_HEAD_INIT(&PyType_Type, 0)
640     "filter",                           /* tp_name */
641     sizeof(filterobject),               /* tp_basicsize */
642     0,                                  /* tp_itemsize */
643     /* methods */
644     (destructor)filter_dealloc,         /* tp_dealloc */
645     0,                                  /* tp_vectorcall_offset */
646     0,                                  /* tp_getattr */
647     0,                                  /* tp_setattr */
648     0,                                  /* tp_as_async */
649     0,                                  /* tp_repr */
650     0,                                  /* tp_as_number */
651     0,                                  /* tp_as_sequence */
652     0,                                  /* tp_as_mapping */
653     0,                                  /* tp_hash */
654     0,                                  /* tp_call */
655     0,                                  /* tp_str */
656     PyObject_GenericGetAttr,            /* tp_getattro */
657     0,                                  /* tp_setattro */
658     0,                                  /* tp_as_buffer */
659     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
660         Py_TPFLAGS_BASETYPE,            /* tp_flags */
661     filter_doc,                         /* tp_doc */
662     (traverseproc)filter_traverse,      /* tp_traverse */
663     0,                                  /* tp_clear */
664     0,                                  /* tp_richcompare */
665     0,                                  /* tp_weaklistoffset */
666     PyObject_SelfIter,                  /* tp_iter */
667     (iternextfunc)filter_next,          /* tp_iternext */
668     filter_methods,                     /* tp_methods */
669     0,                                  /* tp_members */
670     0,                                  /* tp_getset */
671     0,                                  /* tp_base */
672     0,                                  /* tp_dict */
673     0,                                  /* tp_descr_get */
674     0,                                  /* tp_descr_set */
675     0,                                  /* tp_dictoffset */
676     0,                                  /* tp_init */
677     PyType_GenericAlloc,                /* tp_alloc */
678     filter_new,                         /* tp_new */
679     PyObject_GC_Del,                    /* tp_free */
680     .tp_vectorcall = (vectorcallfunc)filter_vectorcall
681 };
682 
683 
684 /*[clinic input]
685 format as builtin_format
686 
687     value: object
688     format_spec: unicode(c_default="NULL") = ''
689     /
690 
691 Return value.__format__(format_spec)
692 
693 format_spec defaults to the empty string.
694 See the Format Specification Mini-Language section of help('FORMATTING') for
695 details.
696 [clinic start generated code]*/
697 
698 static PyObject *
builtin_format_impl(PyObject * module,PyObject * value,PyObject * format_spec)699 builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
700 /*[clinic end generated code: output=2f40bdfa4954b077 input=88339c93ea522b33]*/
701 {
702     return PyObject_Format(value, format_spec);
703 }
704 
705 /*[clinic input]
706 chr as builtin_chr
707 
708     i: int
709     /
710 
711 Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
712 [clinic start generated code]*/
713 
714 static PyObject *
builtin_chr_impl(PyObject * module,int i)715 builtin_chr_impl(PyObject *module, int i)
716 /*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
717 {
718     return PyUnicode_FromOrdinal(i);
719 }
720 
721 
722 /*[clinic input]
723 compile as builtin_compile
724 
725     source: object
726     filename: object(converter="PyUnicode_FSDecoder")
727     mode: str
728     flags: int = 0
729     dont_inherit: bool(accept={int}) = False
730     optimize: int = -1
731     *
732     _feature_version as feature_version: int = -1
733 
734 Compile source into a code object that can be executed by exec() or eval().
735 
736 The source code may represent a Python module, statement or expression.
737 The filename will be used for run-time error messages.
738 The mode must be 'exec' to compile a module, 'single' to compile a
739 single (interactive) statement, or 'eval' to compile an expression.
740 The flags argument, if present, controls which future statements influence
741 the compilation of the code.
742 The dont_inherit argument, if true, stops the compilation inheriting
743 the effects of any future statements in effect in the code calling
744 compile; if absent or false these statements do influence the compilation,
745 in addition to any features explicitly specified.
746 [clinic start generated code]*/
747 
748 static PyObject *
builtin_compile_impl(PyObject * module,PyObject * source,PyObject * filename,const char * mode,int flags,int dont_inherit,int optimize,int feature_version)749 builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
750                      const char *mode, int flags, int dont_inherit,
751                      int optimize, int feature_version)
752 /*[clinic end generated code: output=b0c09c84f116d3d7 input=40171fb92c1d580d]*/
753 {
754     PyObject *source_copy;
755     const char *str;
756     int compile_mode = -1;
757     int is_ast;
758     int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
759     PyObject *result;
760 
761     PyCompilerFlags cf = _PyCompilerFlags_INIT;
762     cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
763     if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) {
764         cf.cf_feature_version = feature_version;
765     }
766 
767     if (flags &
768         ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_COMPILE_MASK))
769     {
770         PyErr_SetString(PyExc_ValueError,
771                         "compile(): unrecognised flags");
772         goto error;
773     }
774     /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
775 
776     if (optimize < -1 || optimize > 2) {
777         PyErr_SetString(PyExc_ValueError,
778                         "compile(): invalid optimize value");
779         goto error;
780     }
781 
782     if (!dont_inherit) {
783         PyEval_MergeCompilerFlags(&cf);
784     }
785 
786     if (strcmp(mode, "exec") == 0)
787         compile_mode = 0;
788     else if (strcmp(mode, "eval") == 0)
789         compile_mode = 1;
790     else if (strcmp(mode, "single") == 0)
791         compile_mode = 2;
792     else if (strcmp(mode, "func_type") == 0) {
793         if (!(flags & PyCF_ONLY_AST)) {
794             PyErr_SetString(PyExc_ValueError,
795                             "compile() mode 'func_type' requires flag PyCF_ONLY_AST");
796             goto error;
797         }
798         compile_mode = 3;
799     }
800     else {
801         const char *msg;
802         if (flags & PyCF_ONLY_AST)
803             msg = "compile() mode must be 'exec', 'eval', 'single' or 'func_type'";
804         else
805             msg = "compile() mode must be 'exec', 'eval' or 'single'";
806         PyErr_SetString(PyExc_ValueError, msg);
807         goto error;
808     }
809 
810     is_ast = PyAST_Check(source);
811     if (is_ast == -1)
812         goto error;
813     if (is_ast) {
814         if (flags & PyCF_ONLY_AST) {
815             Py_INCREF(source);
816             result = source;
817         }
818         else {
819             PyArena *arena;
820             mod_ty mod;
821 
822             arena = _PyArena_New();
823             if (arena == NULL)
824                 goto error;
825             mod = PyAST_obj2mod(source, arena, compile_mode);
826             if (mod == NULL || !_PyAST_Validate(mod)) {
827                 _PyArena_Free(arena);
828                 goto error;
829             }
830             result = (PyObject*)_PyAST_Compile(mod, filename,
831                                                &cf, optimize, arena);
832             _PyArena_Free(arena);
833         }
834         goto finally;
835     }
836 
837     str = _Py_SourceAsString(source, "compile", "string, bytes or AST", &cf, &source_copy);
838     if (str == NULL)
839         goto error;
840 
841     result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
842 
843     Py_XDECREF(source_copy);
844     goto finally;
845 
846 error:
847     result = NULL;
848 finally:
849     Py_DECREF(filename);
850     return result;
851 }
852 
853 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */
854 static PyObject *
builtin_dir(PyObject * self,PyObject * args)855 builtin_dir(PyObject *self, PyObject *args)
856 {
857     PyObject *arg = NULL;
858 
859     if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
860         return NULL;
861     return PyObject_Dir(arg);
862 }
863 
864 PyDoc_STRVAR(dir_doc,
865 "dir([object]) -> list of strings\n"
866 "\n"
867 "If called without an argument, return the names in the current scope.\n"
868 "Else, return an alphabetized list of names comprising (some of) the attributes\n"
869 "of the given object, and of attributes reachable from it.\n"
870 "If the object supplies a method named __dir__, it will be used; otherwise\n"
871 "the default dir() logic is used and returns:\n"
872 "  for a module object: the module's attributes.\n"
873 "  for a class object:  its attributes, and recursively the attributes\n"
874 "    of its bases.\n"
875 "  for any other object: its attributes, its class's attributes, and\n"
876 "    recursively the attributes of its class's base classes.");
877 
878 /*[clinic input]
879 divmod as builtin_divmod
880 
881     x: object
882     y: object
883     /
884 
885 Return the tuple (x//y, x%y).  Invariant: div*y + mod == x.
886 [clinic start generated code]*/
887 
888 static PyObject *
builtin_divmod_impl(PyObject * module,PyObject * x,PyObject * y)889 builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
890 /*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
891 {
892     return PyNumber_Divmod(x, y);
893 }
894 
895 
896 /*[clinic input]
897 eval as builtin_eval
898 
899     source: object
900     globals: object = None
901     locals: object = None
902     /
903 
904 Evaluate the given source in the context of globals and locals.
905 
906 The source may be a string representing a Python expression
907 or a code object as returned by compile().
908 The globals must be a dictionary and locals can be any mapping,
909 defaulting to the current globals and locals.
910 If only globals is given, locals defaults to it.
911 [clinic start generated code]*/
912 
913 static PyObject *
builtin_eval_impl(PyObject * module,PyObject * source,PyObject * globals,PyObject * locals)914 builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
915                   PyObject *locals)
916 /*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
917 {
918     PyObject *result, *source_copy;
919     const char *str;
920 
921     if (locals != Py_None && !PyMapping_Check(locals)) {
922         PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
923         return NULL;
924     }
925     if (globals != Py_None && !PyDict_Check(globals)) {
926         PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
927             "globals must be a real dict; try eval(expr, {}, mapping)"
928             : "globals must be a dict");
929         return NULL;
930     }
931     if (globals == Py_None) {
932         globals = PyEval_GetGlobals();
933         if (locals == Py_None) {
934             locals = PyEval_GetLocals();
935             if (locals == NULL)
936                 return NULL;
937         }
938     }
939     else if (locals == Py_None)
940         locals = globals;
941 
942     if (globals == NULL || locals == NULL) {
943         PyErr_SetString(PyExc_TypeError,
944             "eval must be given globals and locals "
945             "when called without a frame");
946         return NULL;
947     }
948 
949     int r = _PyDict_ContainsId(globals, &PyId___builtins__);
950     if (r == 0) {
951         r = _PyDict_SetItemId(globals, &PyId___builtins__,
952                               PyEval_GetBuiltins());
953     }
954     if (r < 0) {
955         return NULL;
956     }
957 
958     if (PyCode_Check(source)) {
959         if (PySys_Audit("exec", "O", source) < 0) {
960             return NULL;
961         }
962 
963         if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
964             PyErr_SetString(PyExc_TypeError,
965                 "code object passed to eval() may not contain free variables");
966             return NULL;
967         }
968         return PyEval_EvalCode(source, globals, locals);
969     }
970 
971     PyCompilerFlags cf = _PyCompilerFlags_INIT;
972     cf.cf_flags = PyCF_SOURCE_IS_UTF8;
973     str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
974     if (str == NULL)
975         return NULL;
976 
977     while (*str == ' ' || *str == '\t')
978         str++;
979 
980     (void)PyEval_MergeCompilerFlags(&cf);
981     result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
982     Py_XDECREF(source_copy);
983     return result;
984 }
985 
986 /*[clinic input]
987 exec as builtin_exec
988 
989     source: object
990     globals: object = None
991     locals: object = None
992     /
993 
994 Execute the given source in the context of globals and locals.
995 
996 The source may be a string representing one or more Python statements
997 or a code object as returned by compile().
998 The globals must be a dictionary and locals can be any mapping,
999 defaulting to the current globals and locals.
1000 If only globals is given, locals defaults to it.
1001 [clinic start generated code]*/
1002 
1003 static PyObject *
builtin_exec_impl(PyObject * module,PyObject * source,PyObject * globals,PyObject * locals)1004 builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
1005                   PyObject *locals)
1006 /*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
1007 {
1008     PyObject *v;
1009 
1010     if (globals == Py_None) {
1011         globals = PyEval_GetGlobals();
1012         if (locals == Py_None) {
1013             locals = PyEval_GetLocals();
1014             if (locals == NULL)
1015                 return NULL;
1016         }
1017         if (!globals || !locals) {
1018             PyErr_SetString(PyExc_SystemError,
1019                             "globals and locals cannot be NULL");
1020             return NULL;
1021         }
1022     }
1023     else if (locals == Py_None)
1024         locals = globals;
1025 
1026     if (!PyDict_Check(globals)) {
1027         PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
1028                      Py_TYPE(globals)->tp_name);
1029         return NULL;
1030     }
1031     if (!PyMapping_Check(locals)) {
1032         PyErr_Format(PyExc_TypeError,
1033             "locals must be a mapping or None, not %.100s",
1034             Py_TYPE(locals)->tp_name);
1035         return NULL;
1036     }
1037     int r = _PyDict_ContainsId(globals, &PyId___builtins__);
1038     if (r == 0) {
1039         r = _PyDict_SetItemId(globals, &PyId___builtins__,
1040                               PyEval_GetBuiltins());
1041     }
1042     if (r < 0) {
1043         return NULL;
1044     }
1045 
1046     if (PyCode_Check(source)) {
1047         if (PySys_Audit("exec", "O", source) < 0) {
1048             return NULL;
1049         }
1050 
1051         if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
1052             PyErr_SetString(PyExc_TypeError,
1053                 "code object passed to exec() may not "
1054                 "contain free variables");
1055             return NULL;
1056         }
1057         v = PyEval_EvalCode(source, globals, locals);
1058     }
1059     else {
1060         PyObject *source_copy;
1061         const char *str;
1062         PyCompilerFlags cf = _PyCompilerFlags_INIT;
1063         cf.cf_flags = PyCF_SOURCE_IS_UTF8;
1064         str = _Py_SourceAsString(source, "exec",
1065                                        "string, bytes or code", &cf,
1066                                        &source_copy);
1067         if (str == NULL)
1068             return NULL;
1069         if (PyEval_MergeCompilerFlags(&cf))
1070             v = PyRun_StringFlags(str, Py_file_input, globals,
1071                                   locals, &cf);
1072         else
1073             v = PyRun_String(str, Py_file_input, globals, locals);
1074         Py_XDECREF(source_copy);
1075     }
1076     if (v == NULL)
1077         return NULL;
1078     Py_DECREF(v);
1079     Py_RETURN_NONE;
1080 }
1081 
1082 
1083 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */
1084 static PyObject *
builtin_getattr(PyObject * self,PyObject * const * args,Py_ssize_t nargs)1085 builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1086 {
1087     PyObject *v, *name, *result;
1088 
1089     if (!_PyArg_CheckPositional("getattr", nargs, 2, 3))
1090         return NULL;
1091 
1092     v = args[0];
1093     name = args[1];
1094     if (!PyUnicode_Check(name)) {
1095         PyErr_SetString(PyExc_TypeError,
1096                         "getattr(): attribute name must be string");
1097         return NULL;
1098     }
1099     if (nargs > 2) {
1100         if (_PyObject_LookupAttr(v, name, &result) == 0) {
1101             PyObject *dflt = args[2];
1102             Py_INCREF(dflt);
1103             return dflt;
1104         }
1105     }
1106     else {
1107         result = PyObject_GetAttr(v, name);
1108     }
1109     return result;
1110 }
1111 
1112 PyDoc_STRVAR(getattr_doc,
1113 "getattr(object, name[, default]) -> value\n\
1114 \n\
1115 Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
1116 When a default argument is given, it is returned when the attribute doesn't\n\
1117 exist; without it, an exception is raised in that case.");
1118 
1119 
1120 /*[clinic input]
1121 globals as builtin_globals
1122 
1123 Return the dictionary containing the current scope's global variables.
1124 
1125 NOTE: Updates to this dictionary *will* affect name lookups in the current
1126 global scope and vice-versa.
1127 [clinic start generated code]*/
1128 
1129 static PyObject *
builtin_globals_impl(PyObject * module)1130 builtin_globals_impl(PyObject *module)
1131 /*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
1132 {
1133     PyObject *d;
1134 
1135     d = PyEval_GetGlobals();
1136     Py_XINCREF(d);
1137     return d;
1138 }
1139 
1140 
1141 /*[clinic input]
1142 hasattr as builtin_hasattr
1143 
1144     obj: object
1145     name: object
1146     /
1147 
1148 Return whether the object has an attribute with the given name.
1149 
1150 This is done by calling getattr(obj, name) and catching AttributeError.
1151 [clinic start generated code]*/
1152 
1153 static PyObject *
builtin_hasattr_impl(PyObject * module,PyObject * obj,PyObject * name)1154 builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1155 /*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
1156 {
1157     PyObject *v;
1158 
1159     if (!PyUnicode_Check(name)) {
1160         PyErr_SetString(PyExc_TypeError,
1161                         "hasattr(): attribute name must be string");
1162         return NULL;
1163     }
1164     if (_PyObject_LookupAttr(obj, name, &v) < 0) {
1165         return NULL;
1166     }
1167     if (v == NULL) {
1168         Py_RETURN_FALSE;
1169     }
1170     Py_DECREF(v);
1171     Py_RETURN_TRUE;
1172 }
1173 
1174 
1175 /* AC: gdb's integration with CPython relies on builtin_id having
1176  * the *exact* parameter names of "self" and "v", so we ensure we
1177  * preserve those name rather than using the AC defaults.
1178  */
1179 /*[clinic input]
1180 id as builtin_id
1181 
1182     self: self(type="PyModuleDef *")
1183     obj as v: object
1184     /
1185 
1186 Return the identity of an object.
1187 
1188 This is guaranteed to be unique among simultaneously existing objects.
1189 (CPython uses the object's memory address.)
1190 [clinic start generated code]*/
1191 
1192 static PyObject *
builtin_id(PyModuleDef * self,PyObject * v)1193 builtin_id(PyModuleDef *self, PyObject *v)
1194 /*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
1195 {
1196     PyObject *id = PyLong_FromVoidPtr(v);
1197 
1198     if (id && PySys_Audit("builtins.id", "O", id) < 0) {
1199         Py_DECREF(id);
1200         return NULL;
1201     }
1202 
1203     return id;
1204 }
1205 
1206 
1207 /* map object ************************************************************/
1208 
1209 typedef struct {
1210     PyObject_HEAD
1211     PyObject *iters;
1212     PyObject *func;
1213 } mapobject;
1214 
1215 static PyObject *
map_new(PyTypeObject * type,PyObject * args,PyObject * kwds)1216 map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1217 {
1218     PyObject *it, *iters, *func;
1219     mapobject *lz;
1220     Py_ssize_t numargs, i;
1221 
1222     if ((type == &PyMap_Type || type->tp_init == PyMap_Type.tp_init) &&
1223         !_PyArg_NoKeywords("map", kwds))
1224         return NULL;
1225 
1226     numargs = PyTuple_Size(args);
1227     if (numargs < 2) {
1228         PyErr_SetString(PyExc_TypeError,
1229            "map() must have at least two arguments.");
1230         return NULL;
1231     }
1232 
1233     iters = PyTuple_New(numargs-1);
1234     if (iters == NULL)
1235         return NULL;
1236 
1237     for (i=1 ; i<numargs ; i++) {
1238         /* Get iterator. */
1239         it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1240         if (it == NULL) {
1241             Py_DECREF(iters);
1242             return NULL;
1243         }
1244         PyTuple_SET_ITEM(iters, i-1, it);
1245     }
1246 
1247     /* create mapobject structure */
1248     lz = (mapobject *)type->tp_alloc(type, 0);
1249     if (lz == NULL) {
1250         Py_DECREF(iters);
1251         return NULL;
1252     }
1253     lz->iters = iters;
1254     func = PyTuple_GET_ITEM(args, 0);
1255     lz->func = Py_NewRef(func);
1256 
1257     return (PyObject *)lz;
1258 }
1259 
1260 static PyObject *
map_vectorcall(PyObject * type,PyObject * const * args,size_t nargsf,PyObject * kwnames)1261 map_vectorcall(PyObject *type, PyObject * const*args,
1262                 size_t nargsf, PyObject *kwnames)
1263 {
1264     PyTypeObject *tp = (PyTypeObject *)type;
1265     if (tp == &PyMap_Type && !_PyArg_NoKwnames("map", kwnames)) {
1266         return NULL;
1267     }
1268 
1269     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1270     if (nargs < 2) {
1271         PyErr_SetString(PyExc_TypeError,
1272            "map() must have at least two arguments.");
1273         return NULL;
1274     }
1275 
1276     PyObject *iters = PyTuple_New(nargs-1);
1277     if (iters == NULL) {
1278         return NULL;
1279     }
1280 
1281     for (int i=1; i<nargs; i++) {
1282         PyObject *it = PyObject_GetIter(args[i]);
1283         if (it == NULL) {
1284             Py_DECREF(iters);
1285             return NULL;
1286         }
1287         PyTuple_SET_ITEM(iters, i-1, it);
1288     }
1289 
1290     mapobject *lz = (mapobject *)tp->tp_alloc(tp, 0);
1291     if (lz == NULL) {
1292         Py_DECREF(iters);
1293         return NULL;
1294     }
1295     lz->iters = iters;
1296     lz->func = Py_NewRef(args[0]);
1297 
1298     return (PyObject *)lz;
1299 }
1300 
1301 static void
map_dealloc(mapobject * lz)1302 map_dealloc(mapobject *lz)
1303 {
1304     PyObject_GC_UnTrack(lz);
1305     Py_XDECREF(lz->iters);
1306     Py_XDECREF(lz->func);
1307     Py_TYPE(lz)->tp_free(lz);
1308 }
1309 
1310 static int
map_traverse(mapobject * lz,visitproc visit,void * arg)1311 map_traverse(mapobject *lz, visitproc visit, void *arg)
1312 {
1313     Py_VISIT(lz->iters);
1314     Py_VISIT(lz->func);
1315     return 0;
1316 }
1317 
1318 static PyObject *
map_next(mapobject * lz)1319 map_next(mapobject *lz)
1320 {
1321     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
1322     PyObject **stack;
1323     PyObject *result = NULL;
1324     PyThreadState *tstate = _PyThreadState_GET();
1325 
1326     const Py_ssize_t niters = PyTuple_GET_SIZE(lz->iters);
1327     if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1328         stack = small_stack;
1329     }
1330     else {
1331         stack = PyMem_Malloc(niters * sizeof(stack[0]));
1332         if (stack == NULL) {
1333             _PyErr_NoMemory(tstate);
1334             return NULL;
1335         }
1336     }
1337 
1338     Py_ssize_t nargs = 0;
1339     for (Py_ssize_t i=0; i < niters; i++) {
1340         PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1341         PyObject *val = Py_TYPE(it)->tp_iternext(it);
1342         if (val == NULL) {
1343             goto exit;
1344         }
1345         stack[i] = val;
1346         nargs++;
1347     }
1348 
1349     result = _PyObject_VectorcallTstate(tstate, lz->func, stack, nargs, NULL);
1350 
1351 exit:
1352     for (Py_ssize_t i=0; i < nargs; i++) {
1353         Py_DECREF(stack[i]);
1354     }
1355     if (stack != small_stack) {
1356         PyMem_Free(stack);
1357     }
1358     return result;
1359 }
1360 
1361 static PyObject *
map_reduce(mapobject * lz,PyObject * Py_UNUSED (ignored))1362 map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
1363 {
1364     Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1365     PyObject *args = PyTuple_New(numargs+1);
1366     Py_ssize_t i;
1367     if (args == NULL)
1368         return NULL;
1369     Py_INCREF(lz->func);
1370     PyTuple_SET_ITEM(args, 0, lz->func);
1371     for (i = 0; i<numargs; i++){
1372         PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1373         Py_INCREF(it);
1374         PyTuple_SET_ITEM(args, i+1, it);
1375     }
1376 
1377     return Py_BuildValue("ON", Py_TYPE(lz), args);
1378 }
1379 
1380 static PyMethodDef map_methods[] = {
1381     {"__reduce__",   (PyCFunction)map_reduce,   METH_NOARGS, reduce_doc},
1382     {NULL,           NULL}           /* sentinel */
1383 };
1384 
1385 
1386 PyDoc_STRVAR(map_doc,
1387 "map(func, *iterables) --> map object\n\
1388 \n\
1389 Make an iterator that computes the function using arguments from\n\
1390 each of the iterables.  Stops when the shortest iterable is exhausted.");
1391 
1392 PyTypeObject PyMap_Type = {
1393     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1394     "map",                              /* tp_name */
1395     sizeof(mapobject),                  /* tp_basicsize */
1396     0,                                  /* tp_itemsize */
1397     /* methods */
1398     (destructor)map_dealloc,            /* tp_dealloc */
1399     0,                                  /* tp_vectorcall_offset */
1400     0,                                  /* tp_getattr */
1401     0,                                  /* tp_setattr */
1402     0,                                  /* tp_as_async */
1403     0,                                  /* tp_repr */
1404     0,                                  /* tp_as_number */
1405     0,                                  /* tp_as_sequence */
1406     0,                                  /* tp_as_mapping */
1407     0,                                  /* tp_hash */
1408     0,                                  /* tp_call */
1409     0,                                  /* tp_str */
1410     PyObject_GenericGetAttr,            /* tp_getattro */
1411     0,                                  /* tp_setattro */
1412     0,                                  /* tp_as_buffer */
1413     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1414         Py_TPFLAGS_BASETYPE,            /* tp_flags */
1415     map_doc,                            /* tp_doc */
1416     (traverseproc)map_traverse,         /* tp_traverse */
1417     0,                                  /* tp_clear */
1418     0,                                  /* tp_richcompare */
1419     0,                                  /* tp_weaklistoffset */
1420     PyObject_SelfIter,                  /* tp_iter */
1421     (iternextfunc)map_next,     /* tp_iternext */
1422     map_methods,                        /* tp_methods */
1423     0,                                  /* tp_members */
1424     0,                                  /* tp_getset */
1425     0,                                  /* tp_base */
1426     0,                                  /* tp_dict */
1427     0,                                  /* tp_descr_get */
1428     0,                                  /* tp_descr_set */
1429     0,                                  /* tp_dictoffset */
1430     0,                                  /* tp_init */
1431     PyType_GenericAlloc,                /* tp_alloc */
1432     map_new,                            /* tp_new */
1433     PyObject_GC_Del,                    /* tp_free */
1434     .tp_vectorcall = (vectorcallfunc)map_vectorcall
1435 };
1436 
1437 
1438 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */
1439 static PyObject *
builtin_next(PyObject * self,PyObject * const * args,Py_ssize_t nargs)1440 builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1441 {
1442     PyObject *it, *res;
1443 
1444     if (!_PyArg_CheckPositional("next", nargs, 1, 2))
1445         return NULL;
1446 
1447     it = args[0];
1448     if (!PyIter_Check(it)) {
1449         PyErr_Format(PyExc_TypeError,
1450             "'%.200s' object is not an iterator",
1451             Py_TYPE(it)->tp_name);
1452         return NULL;
1453     }
1454 
1455     res = (*Py_TYPE(it)->tp_iternext)(it);
1456     if (res != NULL) {
1457         return res;
1458     } else if (nargs > 1) {
1459         PyObject *def = args[1];
1460         if (PyErr_Occurred()) {
1461             if(!PyErr_ExceptionMatches(PyExc_StopIteration))
1462                 return NULL;
1463             PyErr_Clear();
1464         }
1465         Py_INCREF(def);
1466         return def;
1467     } else if (PyErr_Occurred()) {
1468         return NULL;
1469     } else {
1470         PyErr_SetNone(PyExc_StopIteration);
1471         return NULL;
1472     }
1473 }
1474 
1475 PyDoc_STRVAR(next_doc,
1476 "next(iterator[, default])\n\
1477 \n\
1478 Return the next item from the iterator. If default is given and the iterator\n\
1479 is exhausted, it is returned instead of raising StopIteration.");
1480 
1481 
1482 /*[clinic input]
1483 setattr as builtin_setattr
1484 
1485     obj: object
1486     name: object
1487     value: object
1488     /
1489 
1490 Sets the named attribute on the given object to the specified value.
1491 
1492 setattr(x, 'y', v) is equivalent to ``x.y = v''
1493 [clinic start generated code]*/
1494 
1495 static PyObject *
builtin_setattr_impl(PyObject * module,PyObject * obj,PyObject * name,PyObject * value)1496 builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
1497                      PyObject *value)
1498 /*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
1499 {
1500     if (PyObject_SetAttr(obj, name, value) != 0)
1501         return NULL;
1502     Py_RETURN_NONE;
1503 }
1504 
1505 
1506 /*[clinic input]
1507 delattr as builtin_delattr
1508 
1509     obj: object
1510     name: object
1511     /
1512 
1513 Deletes the named attribute from the given object.
1514 
1515 delattr(x, 'y') is equivalent to ``del x.y''
1516 [clinic start generated code]*/
1517 
1518 static PyObject *
builtin_delattr_impl(PyObject * module,PyObject * obj,PyObject * name)1519 builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1520 /*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
1521 {
1522     if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
1523         return NULL;
1524     Py_RETURN_NONE;
1525 }
1526 
1527 
1528 /*[clinic input]
1529 hash as builtin_hash
1530 
1531     obj: object
1532     /
1533 
1534 Return the hash value for the given object.
1535 
1536 Two objects that compare equal must also have the same hash value, but the
1537 reverse is not necessarily true.
1538 [clinic start generated code]*/
1539 
1540 static PyObject *
builtin_hash(PyObject * module,PyObject * obj)1541 builtin_hash(PyObject *module, PyObject *obj)
1542 /*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
1543 {
1544     Py_hash_t x;
1545 
1546     x = PyObject_Hash(obj);
1547     if (x == -1)
1548         return NULL;
1549     return PyLong_FromSsize_t(x);
1550 }
1551 
1552 
1553 /*[clinic input]
1554 hex as builtin_hex
1555 
1556     number: object
1557     /
1558 
1559 Return the hexadecimal representation of an integer.
1560 
1561    >>> hex(12648430)
1562    '0xc0ffee'
1563 [clinic start generated code]*/
1564 
1565 static PyObject *
builtin_hex(PyObject * module,PyObject * number)1566 builtin_hex(PyObject *module, PyObject *number)
1567 /*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
1568 {
1569     return PyNumber_ToBase(number, 16);
1570 }
1571 
1572 
1573 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */
1574 static PyObject *
builtin_iter(PyObject * self,PyObject * const * args,Py_ssize_t nargs)1575 builtin_iter(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1576 {
1577     PyObject *v;
1578 
1579     if (!_PyArg_CheckPositional("iter", nargs, 1, 2))
1580         return NULL;
1581     v = args[0];
1582     if (nargs == 1)
1583         return PyObject_GetIter(v);
1584     if (!PyCallable_Check(v)) {
1585         PyErr_SetString(PyExc_TypeError,
1586                         "iter(v, w): v must be callable");
1587         return NULL;
1588     }
1589     PyObject *sentinel = args[1];
1590     return PyCallIter_New(v, sentinel);
1591 }
1592 
1593 PyDoc_STRVAR(iter_doc,
1594 "iter(iterable) -> iterator\n\
1595 iter(callable, sentinel) -> iterator\n\
1596 \n\
1597 Get an iterator from an object.  In the first form, the argument must\n\
1598 supply its own iterator, or be a sequence.\n\
1599 In the second form, the callable is called until it returns the sentinel.");
1600 
1601 
1602 /*[clinic input]
1603 aiter as builtin_aiter
1604 
1605     async_iterable: object
1606     /
1607 
1608 Return an AsyncIterator for an AsyncIterable object.
1609 [clinic start generated code]*/
1610 
1611 static PyObject *
builtin_aiter(PyObject * module,PyObject * async_iterable)1612 builtin_aiter(PyObject *module, PyObject *async_iterable)
1613 /*[clinic end generated code: output=1bae108d86f7960e input=473993d0cacc7d23]*/
1614 {
1615     return PyObject_GetAIter(async_iterable);
1616 }
1617 
1618 PyObject *PyAnextAwaitable_New(PyObject *, PyObject *);
1619 
1620 /*[clinic input]
1621 anext as builtin_anext
1622 
1623     aiterator: object
1624     default: object = NULL
1625     /
1626 
1627 async anext(aiterator[, default])
1628 
1629 Return the next item from the async iterator.  If default is given and the async
1630 iterator is exhausted, it is returned instead of raising StopAsyncIteration.
1631 [clinic start generated code]*/
1632 
1633 static PyObject *
builtin_anext_impl(PyObject * module,PyObject * aiterator,PyObject * default_value)1634 builtin_anext_impl(PyObject *module, PyObject *aiterator,
1635                    PyObject *default_value)
1636 /*[clinic end generated code: output=f02c060c163a81fa input=8f63f4f78590bb4c]*/
1637 {
1638     PyTypeObject *t;
1639     PyObject *awaitable;
1640 
1641     t = Py_TYPE(aiterator);
1642     if (t->tp_as_async == NULL || t->tp_as_async->am_anext == NULL) {
1643         PyErr_Format(PyExc_TypeError,
1644             "'%.200s' object is not an async iterator",
1645             t->tp_name);
1646         return NULL;
1647     }
1648 
1649     awaitable = (*t->tp_as_async->am_anext)(aiterator);
1650     if (default_value == NULL) {
1651         return awaitable;
1652     }
1653 
1654     PyObject* new_awaitable = PyAnextAwaitable_New(
1655             awaitable, default_value);
1656     Py_DECREF(awaitable);
1657     return new_awaitable;
1658 }
1659 
1660 
1661 /*[clinic input]
1662 len as builtin_len
1663 
1664     obj: object
1665     /
1666 
1667 Return the number of items in a container.
1668 [clinic start generated code]*/
1669 
1670 static PyObject *
builtin_len(PyObject * module,PyObject * obj)1671 builtin_len(PyObject *module, PyObject *obj)
1672 /*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
1673 {
1674     Py_ssize_t res;
1675 
1676     res = PyObject_Size(obj);
1677     if (res < 0) {
1678         assert(PyErr_Occurred());
1679         return NULL;
1680     }
1681     return PyLong_FromSsize_t(res);
1682 }
1683 
1684 
1685 /*[clinic input]
1686 locals as builtin_locals
1687 
1688 Return a dictionary containing the current scope's local variables.
1689 
1690 NOTE: Whether or not updates to this dictionary will affect name lookups in
1691 the local scope and vice-versa is *implementation dependent* and not
1692 covered by any backwards compatibility guarantees.
1693 [clinic start generated code]*/
1694 
1695 static PyObject *
builtin_locals_impl(PyObject * module)1696 builtin_locals_impl(PyObject *module)
1697 /*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
1698 {
1699     PyObject *d;
1700 
1701     d = PyEval_GetLocals();
1702     Py_XINCREF(d);
1703     return d;
1704 }
1705 
1706 
1707 static PyObject *
min_max(PyObject * args,PyObject * kwds,int op)1708 min_max(PyObject *args, PyObject *kwds, int op)
1709 {
1710     PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
1711     PyObject *emptytuple, *defaultval = NULL;
1712     static char *kwlist[] = {"key", "default", NULL};
1713     const char *name = op == Py_LT ? "min" : "max";
1714     const int positional = PyTuple_Size(args) > 1;
1715     int ret;
1716 
1717     if (positional) {
1718         v = args;
1719     }
1720     else if (!PyArg_UnpackTuple(args, name, 1, 1, &v)) {
1721         if (PyExceptionClass_Check(PyExc_TypeError)) {
1722             PyErr_Format(PyExc_TypeError, "%s expected at least 1 argument, got 0", name);
1723         }
1724         return NULL;
1725     }
1726 
1727     emptytuple = PyTuple_New(0);
1728     if (emptytuple == NULL)
1729         return NULL;
1730     ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds,
1731                                       (op == Py_LT) ? "|$OO:min" : "|$OO:max",
1732                                       kwlist, &keyfunc, &defaultval);
1733     Py_DECREF(emptytuple);
1734     if (!ret)
1735         return NULL;
1736 
1737     if (positional && defaultval != NULL) {
1738         PyErr_Format(PyExc_TypeError,
1739                         "Cannot specify a default for %s() with multiple "
1740                         "positional arguments", name);
1741         return NULL;
1742     }
1743 
1744     it = PyObject_GetIter(v);
1745     if (it == NULL) {
1746         return NULL;
1747     }
1748 
1749     if (keyfunc == Py_None) {
1750         keyfunc = NULL;
1751     }
1752 
1753     maxitem = NULL; /* the result */
1754     maxval = NULL;  /* the value associated with the result */
1755     while (( item = PyIter_Next(it) )) {
1756         /* get the value from the key function */
1757         if (keyfunc != NULL) {
1758             val = PyObject_CallOneArg(keyfunc, item);
1759             if (val == NULL)
1760                 goto Fail_it_item;
1761         }
1762         /* no key function; the value is the item */
1763         else {
1764             val = item;
1765             Py_INCREF(val);
1766         }
1767 
1768         /* maximum value and item are unset; set them */
1769         if (maxval == NULL) {
1770             maxitem = item;
1771             maxval = val;
1772         }
1773         /* maximum value and item are set; update them as necessary */
1774         else {
1775             int cmp = PyObject_RichCompareBool(val, maxval, op);
1776             if (cmp < 0)
1777                 goto Fail_it_item_and_val;
1778             else if (cmp > 0) {
1779                 Py_DECREF(maxval);
1780                 Py_DECREF(maxitem);
1781                 maxval = val;
1782                 maxitem = item;
1783             }
1784             else {
1785                 Py_DECREF(item);
1786                 Py_DECREF(val);
1787             }
1788         }
1789     }
1790     if (PyErr_Occurred())
1791         goto Fail_it;
1792     if (maxval == NULL) {
1793         assert(maxitem == NULL);
1794         if (defaultval != NULL) {
1795             Py_INCREF(defaultval);
1796             maxitem = defaultval;
1797         } else {
1798             PyErr_Format(PyExc_ValueError,
1799                          "%s() arg is an empty sequence", name);
1800         }
1801     }
1802     else
1803         Py_DECREF(maxval);
1804     Py_DECREF(it);
1805     return maxitem;
1806 
1807 Fail_it_item_and_val:
1808     Py_DECREF(val);
1809 Fail_it_item:
1810     Py_DECREF(item);
1811 Fail_it:
1812     Py_XDECREF(maxval);
1813     Py_XDECREF(maxitem);
1814     Py_DECREF(it);
1815     return NULL;
1816 }
1817 
1818 /* AC: cannot convert yet, waiting for *args support */
1819 static PyObject *
builtin_min(PyObject * self,PyObject * args,PyObject * kwds)1820 builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
1821 {
1822     return min_max(args, kwds, Py_LT);
1823 }
1824 
1825 PyDoc_STRVAR(min_doc,
1826 "min(iterable, *[, default=obj, key=func]) -> value\n\
1827 min(arg1, arg2, *args, *[, key=func]) -> value\n\
1828 \n\
1829 With a single iterable argument, return its smallest item. The\n\
1830 default keyword-only argument specifies an object to return if\n\
1831 the provided iterable is empty.\n\
1832 With two or more arguments, return the smallest argument.");
1833 
1834 
1835 /* AC: cannot convert yet, waiting for *args support */
1836 static PyObject *
builtin_max(PyObject * self,PyObject * args,PyObject * kwds)1837 builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
1838 {
1839     return min_max(args, kwds, Py_GT);
1840 }
1841 
1842 PyDoc_STRVAR(max_doc,
1843 "max(iterable, *[, default=obj, key=func]) -> value\n\
1844 max(arg1, arg2, *args, *[, key=func]) -> value\n\
1845 \n\
1846 With a single iterable argument, return its biggest item. The\n\
1847 default keyword-only argument specifies an object to return if\n\
1848 the provided iterable is empty.\n\
1849 With two or more arguments, return the largest argument.");
1850 
1851 
1852 /*[clinic input]
1853 oct as builtin_oct
1854 
1855     number: object
1856     /
1857 
1858 Return the octal representation of an integer.
1859 
1860    >>> oct(342391)
1861    '0o1234567'
1862 [clinic start generated code]*/
1863 
1864 static PyObject *
builtin_oct(PyObject * module,PyObject * number)1865 builtin_oct(PyObject *module, PyObject *number)
1866 /*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
1867 {
1868     return PyNumber_ToBase(number, 8);
1869 }
1870 
1871 
1872 /*[clinic input]
1873 ord as builtin_ord
1874 
1875     c: object
1876     /
1877 
1878 Return the Unicode code point for a one-character string.
1879 [clinic start generated code]*/
1880 
1881 static PyObject *
builtin_ord(PyObject * module,PyObject * c)1882 builtin_ord(PyObject *module, PyObject *c)
1883 /*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
1884 {
1885     long ord;
1886     Py_ssize_t size;
1887 
1888     if (PyBytes_Check(c)) {
1889         size = PyBytes_GET_SIZE(c);
1890         if (size == 1) {
1891             ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
1892             return PyLong_FromLong(ord);
1893         }
1894     }
1895     else if (PyUnicode_Check(c)) {
1896         if (PyUnicode_READY(c) == -1)
1897             return NULL;
1898         size = PyUnicode_GET_LENGTH(c);
1899         if (size == 1) {
1900             ord = (long)PyUnicode_READ_CHAR(c, 0);
1901             return PyLong_FromLong(ord);
1902         }
1903     }
1904     else if (PyByteArray_Check(c)) {
1905         /* XXX Hopefully this is temporary */
1906         size = PyByteArray_GET_SIZE(c);
1907         if (size == 1) {
1908             ord = (long)((unsigned char)*PyByteArray_AS_STRING(c));
1909             return PyLong_FromLong(ord);
1910         }
1911     }
1912     else {
1913         PyErr_Format(PyExc_TypeError,
1914                      "ord() expected string of length 1, but " \
1915                      "%.200s found", Py_TYPE(c)->tp_name);
1916         return NULL;
1917     }
1918 
1919     PyErr_Format(PyExc_TypeError,
1920                  "ord() expected a character, "
1921                  "but string of length %zd found",
1922                  size);
1923     return NULL;
1924 }
1925 
1926 
1927 /*[clinic input]
1928 pow as builtin_pow
1929 
1930     base: object
1931     exp: object
1932     mod: object = None
1933 
1934 Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments
1935 
1936 Some types, such as ints, are able to use a more efficient algorithm when
1937 invoked using the three argument form.
1938 [clinic start generated code]*/
1939 
1940 static PyObject *
builtin_pow_impl(PyObject * module,PyObject * base,PyObject * exp,PyObject * mod)1941 builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp,
1942                  PyObject *mod)
1943 /*[clinic end generated code: output=3ca1538221bbf15f input=435dbd48a12efb23]*/
1944 {
1945     return PyNumber_Power(base, exp, mod);
1946 }
1947 
1948 /*[clinic input]
1949 print as builtin_print
1950 
1951     *args: object
1952     sep: object(c_default="Py_None") = ' '
1953         string inserted between values, default a space.
1954     end: object(c_default="Py_None") = '\n'
1955         string appended after the last value, default a newline.
1956     file: object = None
1957         a file-like object (stream); defaults to the current sys.stdout.
1958     flush: bool = False
1959         whether to forcibly flush the stream.
1960 
1961 Prints the values to a stream, or to sys.stdout by default.
1962 
1963 [clinic start generated code]*/
1964 
1965 static PyObject *
builtin_print_impl(PyObject * module,PyObject * args,PyObject * sep,PyObject * end,PyObject * file,int flush)1966 builtin_print_impl(PyObject *module, PyObject *args, PyObject *sep,
1967                    PyObject *end, PyObject *file, int flush)
1968 /*[clinic end generated code: output=3cfc0940f5bc237b input=c143c575d24fe665]*/
1969 {
1970     int i, err;
1971 
1972     if (file == Py_None) {
1973         file = _PySys_GetObjectId(&PyId_stdout);
1974         if (file == NULL) {
1975             PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1976             return NULL;
1977         }
1978 
1979         /* sys.stdout may be None when FILE* stdout isn't connected */
1980         if (file == Py_None) {
1981             Py_RETURN_NONE;
1982         }
1983     }
1984 
1985     if (sep == Py_None) {
1986         sep = NULL;
1987     }
1988     else if (sep && !PyUnicode_Check(sep)) {
1989         PyErr_Format(PyExc_TypeError,
1990                      "sep must be None or a string, not %.200s",
1991                      Py_TYPE(sep)->tp_name);
1992         return NULL;
1993     }
1994     if (end == Py_None) {
1995         end = NULL;
1996     }
1997     else if (end && !PyUnicode_Check(end)) {
1998         PyErr_Format(PyExc_TypeError,
1999                      "end must be None or a string, not %.200s",
2000                      Py_TYPE(end)->tp_name);
2001         return NULL;
2002     }
2003 
2004     for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
2005         if (i > 0) {
2006             if (sep == NULL) {
2007                 err = PyFile_WriteString(" ", file);
2008             }
2009             else {
2010                 err = PyFile_WriteObject(sep, file, Py_PRINT_RAW);
2011             }
2012             if (err) {
2013                 return NULL;
2014             }
2015         }
2016         err = PyFile_WriteObject(PyTuple_GET_ITEM(args, i), file, Py_PRINT_RAW);
2017         if (err) {
2018             return NULL;
2019         }
2020     }
2021 
2022     if (end == NULL) {
2023         err = PyFile_WriteString("\n", file);
2024     }
2025     else {
2026         err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
2027     }
2028     if (err) {
2029         return NULL;
2030     }
2031 
2032     if (flush) {
2033         PyObject *tmp = _PyObject_CallMethodIdNoArgs(file, &PyId_flush);
2034         if (tmp == NULL) {
2035             return NULL;
2036         }
2037         Py_DECREF(tmp);
2038     }
2039 
2040     Py_RETURN_NONE;
2041 }
2042 
2043 
2044 /*[clinic input]
2045 input as builtin_input
2046 
2047     prompt: object(c_default="NULL") = None
2048     /
2049 
2050 Read a string from standard input.  The trailing newline is stripped.
2051 
2052 The prompt string, if given, is printed to standard output without a
2053 trailing newline before reading input.
2054 
2055 If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
2056 On *nix systems, readline is used if available.
2057 [clinic start generated code]*/
2058 
2059 static PyObject *
builtin_input_impl(PyObject * module,PyObject * prompt)2060 builtin_input_impl(PyObject *module, PyObject *prompt)
2061 /*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
2062 {
2063     PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
2064     PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
2065     PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
2066     PyObject *tmp;
2067     long fd;
2068     int tty;
2069 
2070     /* Check that stdin/out/err are intact */
2071     if (fin == NULL || fin == Py_None) {
2072         PyErr_SetString(PyExc_RuntimeError,
2073                         "input(): lost sys.stdin");
2074         return NULL;
2075     }
2076     if (fout == NULL || fout == Py_None) {
2077         PyErr_SetString(PyExc_RuntimeError,
2078                         "input(): lost sys.stdout");
2079         return NULL;
2080     }
2081     if (ferr == NULL || ferr == Py_None) {
2082         PyErr_SetString(PyExc_RuntimeError,
2083                         "input(): lost sys.stderr");
2084         return NULL;
2085     }
2086 
2087     if (PySys_Audit("builtins.input", "O", prompt ? prompt : Py_None) < 0) {
2088         return NULL;
2089     }
2090 
2091     /* First of all, flush stderr */
2092     tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
2093     if (tmp == NULL)
2094         PyErr_Clear();
2095     else
2096         Py_DECREF(tmp);
2097 
2098     /* We should only use (GNU) readline if Python's sys.stdin and
2099        sys.stdout are the same as C's stdin and stdout, because we
2100        need to pass it those. */
2101     tmp = _PyObject_CallMethodIdNoArgs(fin, &PyId_fileno);
2102     if (tmp == NULL) {
2103         PyErr_Clear();
2104         tty = 0;
2105     }
2106     else {
2107         fd = PyLong_AsLong(tmp);
2108         Py_DECREF(tmp);
2109         if (fd < 0 && PyErr_Occurred())
2110             return NULL;
2111         tty = fd == fileno(stdin) && isatty(fd);
2112     }
2113     if (tty) {
2114         tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_fileno);
2115         if (tmp == NULL) {
2116             PyErr_Clear();
2117             tty = 0;
2118         }
2119         else {
2120             fd = PyLong_AsLong(tmp);
2121             Py_DECREF(tmp);
2122             if (fd < 0 && PyErr_Occurred())
2123                 return NULL;
2124             tty = fd == fileno(stdout) && isatty(fd);
2125         }
2126     }
2127 
2128     /* If we're interactive, use (GNU) readline */
2129     if (tty) {
2130         PyObject *po = NULL;
2131         const char *promptstr;
2132         char *s = NULL;
2133         PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
2134         PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
2135         const char *stdin_encoding_str, *stdin_errors_str;
2136         PyObject *result;
2137         size_t len;
2138 
2139         /* stdin is a text stream, so it must have an encoding. */
2140         stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
2141         stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
2142         if (!stdin_encoding || !stdin_errors ||
2143                 !PyUnicode_Check(stdin_encoding) ||
2144                 !PyUnicode_Check(stdin_errors)) {
2145             tty = 0;
2146             goto _readline_errors;
2147         }
2148         stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2149         stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
2150         if (!stdin_encoding_str || !stdin_errors_str)
2151             goto _readline_errors;
2152         tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
2153         if (tmp == NULL)
2154             PyErr_Clear();
2155         else
2156             Py_DECREF(tmp);
2157         if (prompt != NULL) {
2158             /* We have a prompt, encode it as stdout would */
2159             const char *stdout_encoding_str, *stdout_errors_str;
2160             PyObject *stringpo;
2161             stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
2162             stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
2163             if (!stdout_encoding || !stdout_errors ||
2164                     !PyUnicode_Check(stdout_encoding) ||
2165                     !PyUnicode_Check(stdout_errors)) {
2166                 tty = 0;
2167                 goto _readline_errors;
2168             }
2169             stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2170             stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
2171             if (!stdout_encoding_str || !stdout_errors_str)
2172                 goto _readline_errors;
2173             stringpo = PyObject_Str(prompt);
2174             if (stringpo == NULL)
2175                 goto _readline_errors;
2176             po = PyUnicode_AsEncodedString(stringpo,
2177                 stdout_encoding_str, stdout_errors_str);
2178             Py_CLEAR(stdout_encoding);
2179             Py_CLEAR(stdout_errors);
2180             Py_CLEAR(stringpo);
2181             if (po == NULL)
2182                 goto _readline_errors;
2183             assert(PyBytes_Check(po));
2184             promptstr = PyBytes_AS_STRING(po);
2185         }
2186         else {
2187             po = NULL;
2188             promptstr = "";
2189         }
2190         s = PyOS_Readline(stdin, stdout, promptstr);
2191         if (s == NULL) {
2192             PyErr_CheckSignals();
2193             if (!PyErr_Occurred())
2194                 PyErr_SetNone(PyExc_KeyboardInterrupt);
2195             goto _readline_errors;
2196         }
2197 
2198         len = strlen(s);
2199         if (len == 0) {
2200             PyErr_SetNone(PyExc_EOFError);
2201             result = NULL;
2202         }
2203         else {
2204             if (len > PY_SSIZE_T_MAX) {
2205                 PyErr_SetString(PyExc_OverflowError,
2206                                 "input: input too long");
2207                 result = NULL;
2208             }
2209             else {
2210                 len--;   /* strip trailing '\n' */
2211                 if (len != 0 && s[len-1] == '\r')
2212                     len--;   /* strip trailing '\r' */
2213                 result = PyUnicode_Decode(s, len, stdin_encoding_str,
2214                                                   stdin_errors_str);
2215             }
2216         }
2217         Py_DECREF(stdin_encoding);
2218         Py_DECREF(stdin_errors);
2219         Py_XDECREF(po);
2220         PyMem_Free(s);
2221 
2222         if (result != NULL) {
2223             if (PySys_Audit("builtins.input/result", "O", result) < 0) {
2224                 return NULL;
2225             }
2226         }
2227 
2228         return result;
2229 
2230     _readline_errors:
2231         Py_XDECREF(stdin_encoding);
2232         Py_XDECREF(stdout_encoding);
2233         Py_XDECREF(stdin_errors);
2234         Py_XDECREF(stdout_errors);
2235         Py_XDECREF(po);
2236         if (tty)
2237             return NULL;
2238 
2239         PyErr_Clear();
2240     }
2241 
2242     /* Fallback if we're not interactive */
2243     if (prompt != NULL) {
2244         if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
2245             return NULL;
2246     }
2247     tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
2248     if (tmp == NULL)
2249         PyErr_Clear();
2250     else
2251         Py_DECREF(tmp);
2252     return PyFile_GetLine(fin, -1);
2253 }
2254 
2255 
2256 /*[clinic input]
2257 repr as builtin_repr
2258 
2259     obj: object
2260     /
2261 
2262 Return the canonical string representation of the object.
2263 
2264 For many object types, including most builtins, eval(repr(obj)) == obj.
2265 [clinic start generated code]*/
2266 
2267 static PyObject *
builtin_repr(PyObject * module,PyObject * obj)2268 builtin_repr(PyObject *module, PyObject *obj)
2269 /*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
2270 {
2271     return PyObject_Repr(obj);
2272 }
2273 
2274 
2275 /*[clinic input]
2276 round as builtin_round
2277 
2278     number: object
2279     ndigits: object = None
2280 
2281 Round a number to a given precision in decimal digits.
2282 
2283 The return value is an integer if ndigits is omitted or None.  Otherwise
2284 the return value has the same type as the number.  ndigits may be negative.
2285 [clinic start generated code]*/
2286 
2287 static PyObject *
builtin_round_impl(PyObject * module,PyObject * number,PyObject * ndigits)2288 builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
2289 /*[clinic end generated code: output=ff0d9dd176c02ede input=275678471d7aca15]*/
2290 {
2291     PyObject *round, *result;
2292 
2293     if (Py_TYPE(number)->tp_dict == NULL) {
2294         if (PyType_Ready(Py_TYPE(number)) < 0)
2295             return NULL;
2296     }
2297 
2298     round = _PyObject_LookupSpecial(number, &PyId___round__);
2299     if (round == NULL) {
2300         if (!PyErr_Occurred())
2301             PyErr_Format(PyExc_TypeError,
2302                          "type %.100s doesn't define __round__ method",
2303                          Py_TYPE(number)->tp_name);
2304         return NULL;
2305     }
2306 
2307     if (ndigits == Py_None)
2308         result = _PyObject_CallNoArgs(round);
2309     else
2310         result = PyObject_CallOneArg(round, ndigits);
2311     Py_DECREF(round);
2312     return result;
2313 }
2314 
2315 
2316 /*AC: we need to keep the kwds dict intact to easily call into the
2317  * list.sort method, which isn't currently supported in AC. So we just use
2318  * the initially generated signature with a custom implementation.
2319  */
2320 /* [disabled clinic input]
2321 sorted as builtin_sorted
2322 
2323     iterable as seq: object
2324     key as keyfunc: object = None
2325     reverse: object = False
2326 
2327 Return a new list containing all items from the iterable in ascending order.
2328 
2329 A custom key function can be supplied to customize the sort order, and the
2330 reverse flag can be set to request the result in descending order.
2331 [end disabled clinic input]*/
2332 
2333 PyDoc_STRVAR(builtin_sorted__doc__,
2334 "sorted($module, iterable, /, *, key=None, reverse=False)\n"
2335 "--\n"
2336 "\n"
2337 "Return a new list containing all items from the iterable in ascending order.\n"
2338 "\n"
2339 "A custom key function can be supplied to customize the sort order, and the\n"
2340 "reverse flag can be set to request the result in descending order.");
2341 
2342 #define BUILTIN_SORTED_METHODDEF    \
2343     {"sorted", (PyCFunction)(void(*)(void))builtin_sorted, METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__},
2344 
2345 static PyObject *
builtin_sorted(PyObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)2346 builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2347 {
2348     PyObject *newlist, *v, *seq, *callable;
2349 
2350     /* Keyword arguments are passed through list.sort() which will check
2351        them. */
2352     if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
2353         return NULL;
2354 
2355     newlist = PySequence_List(seq);
2356     if (newlist == NULL)
2357         return NULL;
2358 
2359     callable = _PyObject_GetAttrId(newlist, &PyId_sort);
2360     if (callable == NULL) {
2361         Py_DECREF(newlist);
2362         return NULL;
2363     }
2364 
2365     assert(nargs >= 1);
2366     v = PyObject_Vectorcall(callable, args + 1, nargs - 1, kwnames);
2367     Py_DECREF(callable);
2368     if (v == NULL) {
2369         Py_DECREF(newlist);
2370         return NULL;
2371     }
2372     Py_DECREF(v);
2373     return newlist;
2374 }
2375 
2376 
2377 /* AC: cannot convert yet, as needs PEP 457 group support in inspect */
2378 static PyObject *
builtin_vars(PyObject * self,PyObject * args)2379 builtin_vars(PyObject *self, PyObject *args)
2380 {
2381     PyObject *v = NULL;
2382     PyObject *d;
2383 
2384     if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2385         return NULL;
2386     if (v == NULL) {
2387         d = PyEval_GetLocals();
2388         Py_XINCREF(d);
2389     }
2390     else {
2391         if (_PyObject_LookupAttrId(v, &PyId___dict__, &d) == 0) {
2392             PyErr_SetString(PyExc_TypeError,
2393                 "vars() argument must have __dict__ attribute");
2394         }
2395     }
2396     return d;
2397 }
2398 
2399 PyDoc_STRVAR(vars_doc,
2400 "vars([object]) -> dictionary\n\
2401 \n\
2402 Without arguments, equivalent to locals().\n\
2403 With an argument, equivalent to object.__dict__.");
2404 
2405 
2406 /*[clinic input]
2407 sum as builtin_sum
2408 
2409     iterable: object
2410     /
2411     start: object(c_default="NULL") = 0
2412 
2413 Return the sum of a 'start' value (default: 0) plus an iterable of numbers
2414 
2415 When the iterable is empty, return the start value.
2416 This function is intended specifically for use with numeric values and may
2417 reject non-numeric types.
2418 [clinic start generated code]*/
2419 
2420 static PyObject *
builtin_sum_impl(PyObject * module,PyObject * iterable,PyObject * start)2421 builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
2422 /*[clinic end generated code: output=df758cec7d1d302f input=162b50765250d222]*/
2423 {
2424     PyObject *result = start;
2425     PyObject *temp, *item, *iter;
2426 
2427     iter = PyObject_GetIter(iterable);
2428     if (iter == NULL)
2429         return NULL;
2430 
2431     if (result == NULL) {
2432         result = PyLong_FromLong(0);
2433         if (result == NULL) {
2434             Py_DECREF(iter);
2435             return NULL;
2436         }
2437     } else {
2438         /* reject string values for 'start' parameter */
2439         if (PyUnicode_Check(result)) {
2440             PyErr_SetString(PyExc_TypeError,
2441                 "sum() can't sum strings [use ''.join(seq) instead]");
2442             Py_DECREF(iter);
2443             return NULL;
2444         }
2445         if (PyBytes_Check(result)) {
2446             PyErr_SetString(PyExc_TypeError,
2447                 "sum() can't sum bytes [use b''.join(seq) instead]");
2448             Py_DECREF(iter);
2449             return NULL;
2450         }
2451         if (PyByteArray_Check(result)) {
2452             PyErr_SetString(PyExc_TypeError,
2453                 "sum() can't sum bytearray [use b''.join(seq) instead]");
2454             Py_DECREF(iter);
2455             return NULL;
2456         }
2457         Py_INCREF(result);
2458     }
2459 
2460 #ifndef SLOW_SUM
2461     /* Fast addition by keeping temporary sums in C instead of new Python objects.
2462        Assumes all inputs are the same type.  If the assumption fails, default
2463        to the more general routine.
2464     */
2465     if (PyLong_CheckExact(result)) {
2466         int overflow;
2467         long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2468         /* If this already overflowed, don't even enter the loop. */
2469         if (overflow == 0) {
2470             Py_DECREF(result);
2471             result = NULL;
2472         }
2473         while(result == NULL) {
2474             item = PyIter_Next(iter);
2475             if (item == NULL) {
2476                 Py_DECREF(iter);
2477                 if (PyErr_Occurred())
2478                     return NULL;
2479                 return PyLong_FromLong(i_result);
2480             }
2481             if (PyLong_CheckExact(item) || PyBool_Check(item)) {
2482                 long b;
2483                 overflow = 0;
2484                 /* Single digits are common, fast, and cannot overflow on unpacking. */
2485                 switch (Py_SIZE(item)) {
2486                     case -1: b = -(sdigit) ((PyLongObject*)item)->ob_digit[0]; break;
2487                     // Note: the continue goes to the top of the "while" loop that iterates over the elements
2488                     case  0: Py_DECREF(item); continue;
2489                     case  1: b = ((PyLongObject*)item)->ob_digit[0]; break;
2490                     default: b = PyLong_AsLongAndOverflow(item, &overflow); break;
2491                 }
2492                 if (overflow == 0 &&
2493                     (i_result >= 0 ? (b <= LONG_MAX - i_result)
2494                                    : (b >= LONG_MIN - i_result)))
2495                 {
2496                     i_result += b;
2497                     Py_DECREF(item);
2498                     continue;
2499                 }
2500             }
2501             /* Either overflowed or is not an int. Restore real objects and process normally */
2502             result = PyLong_FromLong(i_result);
2503             if (result == NULL) {
2504                 Py_DECREF(item);
2505                 Py_DECREF(iter);
2506                 return NULL;
2507             }
2508             temp = PyNumber_Add(result, item);
2509             Py_DECREF(result);
2510             Py_DECREF(item);
2511             result = temp;
2512             if (result == NULL) {
2513                 Py_DECREF(iter);
2514                 return NULL;
2515             }
2516         }
2517     }
2518 
2519     if (PyFloat_CheckExact(result)) {
2520         double f_result = PyFloat_AS_DOUBLE(result);
2521         Py_DECREF(result);
2522         result = NULL;
2523         while(result == NULL) {
2524             item = PyIter_Next(iter);
2525             if (item == NULL) {
2526                 Py_DECREF(iter);
2527                 if (PyErr_Occurred())
2528                     return NULL;
2529                 return PyFloat_FromDouble(f_result);
2530             }
2531             if (PyFloat_CheckExact(item)) {
2532                 f_result += PyFloat_AS_DOUBLE(item);
2533                 Py_DECREF(item);
2534                 continue;
2535             }
2536             if (PyLong_Check(item)) {
2537                 long value;
2538                 int overflow;
2539                 value = PyLong_AsLongAndOverflow(item, &overflow);
2540                 if (!overflow) {
2541                     f_result += (double)value;
2542                     Py_DECREF(item);
2543                     continue;
2544                 }
2545             }
2546             result = PyFloat_FromDouble(f_result);
2547             if (result == NULL) {
2548                 Py_DECREF(item);
2549                 Py_DECREF(iter);
2550                 return NULL;
2551             }
2552             temp = PyNumber_Add(result, item);
2553             Py_DECREF(result);
2554             Py_DECREF(item);
2555             result = temp;
2556             if (result == NULL) {
2557                 Py_DECREF(iter);
2558                 return NULL;
2559             }
2560         }
2561     }
2562 #endif
2563 
2564     for(;;) {
2565         item = PyIter_Next(iter);
2566         if (item == NULL) {
2567             /* error, or end-of-sequence */
2568             if (PyErr_Occurred()) {
2569                 Py_DECREF(result);
2570                 result = NULL;
2571             }
2572             break;
2573         }
2574         /* It's tempting to use PyNumber_InPlaceAdd instead of
2575            PyNumber_Add here, to avoid quadratic running time
2576            when doing 'sum(list_of_lists, [])'.  However, this
2577            would produce a change in behaviour: a snippet like
2578 
2579              empty = []
2580              sum([[x] for x in range(10)], empty)
2581 
2582            would change the value of empty. In fact, using
2583            in-place addition rather that binary addition for
2584            any of the steps introduces subtle behavior changes:
2585 
2586            https://bugs.python.org/issue18305 */
2587         temp = PyNumber_Add(result, item);
2588         Py_DECREF(result);
2589         Py_DECREF(item);
2590         result = temp;
2591         if (result == NULL)
2592             break;
2593     }
2594     Py_DECREF(iter);
2595     return result;
2596 }
2597 
2598 
2599 /*[clinic input]
2600 isinstance as builtin_isinstance
2601 
2602     obj: object
2603     class_or_tuple: object
2604     /
2605 
2606 Return whether an object is an instance of a class or of a subclass thereof.
2607 
2608 A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
2609 check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
2610 or ...`` etc.
2611 [clinic start generated code]*/
2612 
2613 static PyObject *
builtin_isinstance_impl(PyObject * module,PyObject * obj,PyObject * class_or_tuple)2614 builtin_isinstance_impl(PyObject *module, PyObject *obj,
2615                         PyObject *class_or_tuple)
2616 /*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
2617 {
2618     int retval;
2619 
2620     retval = PyObject_IsInstance(obj, class_or_tuple);
2621     if (retval < 0)
2622         return NULL;
2623     return PyBool_FromLong(retval);
2624 }
2625 
2626 
2627 /*[clinic input]
2628 issubclass as builtin_issubclass
2629 
2630     cls: object
2631     class_or_tuple: object
2632     /
2633 
2634 Return whether 'cls' is derived from another class or is the same class.
2635 
2636 A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
2637 check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
2638 or ...``.
2639 [clinic start generated code]*/
2640 
2641 static PyObject *
builtin_issubclass_impl(PyObject * module,PyObject * cls,PyObject * class_or_tuple)2642 builtin_issubclass_impl(PyObject *module, PyObject *cls,
2643                         PyObject *class_or_tuple)
2644 /*[clinic end generated code: output=358412410cd7a250 input=a24b9f3d58c370d6]*/
2645 {
2646     int retval;
2647 
2648     retval = PyObject_IsSubclass(cls, class_or_tuple);
2649     if (retval < 0)
2650         return NULL;
2651     return PyBool_FromLong(retval);
2652 }
2653 
2654 typedef struct {
2655     PyObject_HEAD
2656     Py_ssize_t tuplesize;
2657     PyObject *ittuple;     /* tuple of iterators */
2658     PyObject *result;
2659     int strict;
2660 } zipobject;
2661 
2662 static PyObject *
zip_new(PyTypeObject * type,PyObject * args,PyObject * kwds)2663 zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2664 {
2665     zipobject *lz;
2666     Py_ssize_t i;
2667     PyObject *ittuple;  /* tuple of iterators */
2668     PyObject *result;
2669     Py_ssize_t tuplesize;
2670     int strict = 0;
2671 
2672     if (kwds) {
2673         PyObject *empty = PyTuple_New(0);
2674         if (empty == NULL) {
2675             return NULL;
2676         }
2677         static char *kwlist[] = {"strict", NULL};
2678         int parsed = PyArg_ParseTupleAndKeywords(
2679                 empty, kwds, "|$p:zip", kwlist, &strict);
2680         Py_DECREF(empty);
2681         if (!parsed) {
2682             return NULL;
2683         }
2684     }
2685 
2686     /* args must be a tuple */
2687     assert(PyTuple_Check(args));
2688     tuplesize = PyTuple_GET_SIZE(args);
2689 
2690     /* obtain iterators */
2691     ittuple = PyTuple_New(tuplesize);
2692     if (ittuple == NULL)
2693         return NULL;
2694     for (i=0; i < tuplesize; ++i) {
2695         PyObject *item = PyTuple_GET_ITEM(args, i);
2696         PyObject *it = PyObject_GetIter(item);
2697         if (it == NULL) {
2698             Py_DECREF(ittuple);
2699             return NULL;
2700         }
2701         PyTuple_SET_ITEM(ittuple, i, it);
2702     }
2703 
2704     /* create a result holder */
2705     result = PyTuple_New(tuplesize);
2706     if (result == NULL) {
2707         Py_DECREF(ittuple);
2708         return NULL;
2709     }
2710     for (i=0 ; i < tuplesize ; i++) {
2711         Py_INCREF(Py_None);
2712         PyTuple_SET_ITEM(result, i, Py_None);
2713     }
2714 
2715     /* create zipobject structure */
2716     lz = (zipobject *)type->tp_alloc(type, 0);
2717     if (lz == NULL) {
2718         Py_DECREF(ittuple);
2719         Py_DECREF(result);
2720         return NULL;
2721     }
2722     lz->ittuple = ittuple;
2723     lz->tuplesize = tuplesize;
2724     lz->result = result;
2725     lz->strict = strict;
2726 
2727     return (PyObject *)lz;
2728 }
2729 
2730 static void
zip_dealloc(zipobject * lz)2731 zip_dealloc(zipobject *lz)
2732 {
2733     PyObject_GC_UnTrack(lz);
2734     Py_XDECREF(lz->ittuple);
2735     Py_XDECREF(lz->result);
2736     Py_TYPE(lz)->tp_free(lz);
2737 }
2738 
2739 static int
zip_traverse(zipobject * lz,visitproc visit,void * arg)2740 zip_traverse(zipobject *lz, visitproc visit, void *arg)
2741 {
2742     Py_VISIT(lz->ittuple);
2743     Py_VISIT(lz->result);
2744     return 0;
2745 }
2746 
2747 static PyObject *
zip_next(zipobject * lz)2748 zip_next(zipobject *lz)
2749 {
2750     Py_ssize_t i;
2751     Py_ssize_t tuplesize = lz->tuplesize;
2752     PyObject *result = lz->result;
2753     PyObject *it;
2754     PyObject *item;
2755     PyObject *olditem;
2756 
2757     if (tuplesize == 0)
2758         return NULL;
2759     if (Py_REFCNT(result) == 1) {
2760         Py_INCREF(result);
2761         for (i=0 ; i < tuplesize ; i++) {
2762             it = PyTuple_GET_ITEM(lz->ittuple, i);
2763             item = (*Py_TYPE(it)->tp_iternext)(it);
2764             if (item == NULL) {
2765                 Py_DECREF(result);
2766                 if (lz->strict) {
2767                     goto check;
2768                 }
2769                 return NULL;
2770             }
2771             olditem = PyTuple_GET_ITEM(result, i);
2772             PyTuple_SET_ITEM(result, i, item);
2773             Py_DECREF(olditem);
2774         }
2775         // bpo-42536: The GC may have untracked this result tuple. Since we're
2776         // recycling it, make sure it's tracked again:
2777         if (!_PyObject_GC_IS_TRACKED(result)) {
2778             _PyObject_GC_TRACK(result);
2779         }
2780     } else {
2781         result = PyTuple_New(tuplesize);
2782         if (result == NULL)
2783             return NULL;
2784         for (i=0 ; i < tuplesize ; i++) {
2785             it = PyTuple_GET_ITEM(lz->ittuple, i);
2786             item = (*Py_TYPE(it)->tp_iternext)(it);
2787             if (item == NULL) {
2788                 Py_DECREF(result);
2789                 if (lz->strict) {
2790                     goto check;
2791                 }
2792                 return NULL;
2793             }
2794             PyTuple_SET_ITEM(result, i, item);
2795         }
2796     }
2797     return result;
2798 check:
2799     if (PyErr_Occurred()) {
2800         if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
2801             // next() on argument i raised an exception (not StopIteration)
2802             return NULL;
2803         }
2804         PyErr_Clear();
2805     }
2806     if (i) {
2807         // ValueError: zip() argument 2 is shorter than argument 1
2808         // ValueError: zip() argument 3 is shorter than arguments 1-2
2809         const char* plural = i == 1 ? " " : "s 1-";
2810         return PyErr_Format(PyExc_ValueError,
2811                             "zip() argument %d is shorter than argument%s%d",
2812                             i + 1, plural, i);
2813     }
2814     for (i = 1; i < tuplesize; i++) {
2815         it = PyTuple_GET_ITEM(lz->ittuple, i);
2816         item = (*Py_TYPE(it)->tp_iternext)(it);
2817         if (item) {
2818             Py_DECREF(item);
2819             const char* plural = i == 1 ? " " : "s 1-";
2820             return PyErr_Format(PyExc_ValueError,
2821                                 "zip() argument %d is longer than argument%s%d",
2822                                 i + 1, plural, i);
2823         }
2824         if (PyErr_Occurred()) {
2825             if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
2826                 // next() on argument i raised an exception (not StopIteration)
2827                 return NULL;
2828             }
2829             PyErr_Clear();
2830         }
2831         // Argument i is exhausted. So far so good...
2832     }
2833     // All arguments are exhausted. Success!
2834     return NULL;
2835 }
2836 
2837 static PyObject *
zip_reduce(zipobject * lz,PyObject * Py_UNUSED (ignored))2838 zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
2839 {
2840     /* Just recreate the zip with the internal iterator tuple */
2841     if (lz->strict) {
2842         return PyTuple_Pack(3, Py_TYPE(lz), lz->ittuple, Py_True);
2843     }
2844     return PyTuple_Pack(2, Py_TYPE(lz), lz->ittuple);
2845 }
2846 
2847 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2848 
2849 static PyObject *
zip_setstate(zipobject * lz,PyObject * state)2850 zip_setstate(zipobject *lz, PyObject *state)
2851 {
2852     int strict = PyObject_IsTrue(state);
2853     if (strict < 0) {
2854         return NULL;
2855     }
2856     lz->strict = strict;
2857     Py_RETURN_NONE;
2858 }
2859 
2860 static PyMethodDef zip_methods[] = {
2861     {"__reduce__",   (PyCFunction)zip_reduce,   METH_NOARGS, reduce_doc},
2862     {"__setstate__", (PyCFunction)zip_setstate, METH_O,      setstate_doc},
2863     {NULL}  /* sentinel */
2864 };
2865 
2866 PyDoc_STRVAR(zip_doc,
2867 "zip(*iterables, strict=False) --> Yield tuples until an input is exhausted.\n\
2868 \n\
2869    >>> list(zip('abcdefg', range(3), range(4)))\n\
2870    [('a', 0, 0), ('b', 1, 1), ('c', 2, 2)]\n\
2871 \n\
2872 The zip object yields n-length tuples, where n is the number of iterables\n\
2873 passed as positional arguments to zip().  The i-th element in every tuple\n\
2874 comes from the i-th iterable argument to zip().  This continues until the\n\
2875 shortest argument is exhausted.\n\
2876 \n\
2877 If strict is true and one of the arguments is exhausted before the others,\n\
2878 raise a ValueError.");
2879 
2880 PyTypeObject PyZip_Type = {
2881     PyVarObject_HEAD_INIT(&PyType_Type, 0)
2882     "zip",                              /* tp_name */
2883     sizeof(zipobject),                  /* tp_basicsize */
2884     0,                                  /* tp_itemsize */
2885     /* methods */
2886     (destructor)zip_dealloc,            /* tp_dealloc */
2887     0,                                  /* tp_vectorcall_offset */
2888     0,                                  /* tp_getattr */
2889     0,                                  /* tp_setattr */
2890     0,                                  /* tp_as_async */
2891     0,                                  /* tp_repr */
2892     0,                                  /* tp_as_number */
2893     0,                                  /* tp_as_sequence */
2894     0,                                  /* tp_as_mapping */
2895     0,                                  /* tp_hash */
2896     0,                                  /* tp_call */
2897     0,                                  /* tp_str */
2898     PyObject_GenericGetAttr,            /* tp_getattro */
2899     0,                                  /* tp_setattro */
2900     0,                                  /* tp_as_buffer */
2901     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2902         Py_TPFLAGS_BASETYPE,            /* tp_flags */
2903     zip_doc,                            /* tp_doc */
2904     (traverseproc)zip_traverse,    /* tp_traverse */
2905     0,                                  /* tp_clear */
2906     0,                                  /* tp_richcompare */
2907     0,                                  /* tp_weaklistoffset */
2908     PyObject_SelfIter,                  /* tp_iter */
2909     (iternextfunc)zip_next,     /* tp_iternext */
2910     zip_methods,                        /* tp_methods */
2911     0,                                  /* tp_members */
2912     0,                                  /* tp_getset */
2913     0,                                  /* tp_base */
2914     0,                                  /* tp_dict */
2915     0,                                  /* tp_descr_get */
2916     0,                                  /* tp_descr_set */
2917     0,                                  /* tp_dictoffset */
2918     0,                                  /* tp_init */
2919     PyType_GenericAlloc,                /* tp_alloc */
2920     zip_new,                            /* tp_new */
2921     PyObject_GC_Del,                    /* tp_free */
2922 };
2923 
2924 
2925 static PyMethodDef builtin_methods[] = {
2926     {"__build_class__", (PyCFunction)(void(*)(void))builtin___build_class__,
2927      METH_FASTCALL | METH_KEYWORDS, build_class_doc},
2928     {"__import__",      (PyCFunction)(void(*)(void))builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
2929     BUILTIN_ABS_METHODDEF
2930     BUILTIN_ALL_METHODDEF
2931     BUILTIN_ANY_METHODDEF
2932     BUILTIN_ASCII_METHODDEF
2933     BUILTIN_BIN_METHODDEF
2934     {"breakpoint",      (PyCFunction)(void(*)(void))builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
2935     BUILTIN_CALLABLE_METHODDEF
2936     BUILTIN_CHR_METHODDEF
2937     BUILTIN_COMPILE_METHODDEF
2938     BUILTIN_DELATTR_METHODDEF
2939     {"dir",             builtin_dir,        METH_VARARGS, dir_doc},
2940     BUILTIN_DIVMOD_METHODDEF
2941     BUILTIN_EVAL_METHODDEF
2942     BUILTIN_EXEC_METHODDEF
2943     BUILTIN_FORMAT_METHODDEF
2944     {"getattr",         (PyCFunction)(void(*)(void))builtin_getattr, METH_FASTCALL, getattr_doc},
2945     BUILTIN_GLOBALS_METHODDEF
2946     BUILTIN_HASATTR_METHODDEF
2947     BUILTIN_HASH_METHODDEF
2948     BUILTIN_HEX_METHODDEF
2949     BUILTIN_ID_METHODDEF
2950     BUILTIN_INPUT_METHODDEF
2951     BUILTIN_ISINSTANCE_METHODDEF
2952     BUILTIN_ISSUBCLASS_METHODDEF
2953     {"iter",            (PyCFunction)(void(*)(void))builtin_iter,       METH_FASTCALL, iter_doc},
2954     BUILTIN_AITER_METHODDEF
2955     BUILTIN_LEN_METHODDEF
2956     BUILTIN_LOCALS_METHODDEF
2957     {"max",             (PyCFunction)(void(*)(void))builtin_max,        METH_VARARGS | METH_KEYWORDS, max_doc},
2958     {"min",             (PyCFunction)(void(*)(void))builtin_min,        METH_VARARGS | METH_KEYWORDS, min_doc},
2959     {"next",            (PyCFunction)(void(*)(void))builtin_next,       METH_FASTCALL, next_doc},
2960     BUILTIN_ANEXT_METHODDEF
2961     BUILTIN_OCT_METHODDEF
2962     BUILTIN_ORD_METHODDEF
2963     BUILTIN_POW_METHODDEF
2964     BUILTIN_PRINT_METHODDEF
2965     BUILTIN_REPR_METHODDEF
2966     BUILTIN_ROUND_METHODDEF
2967     BUILTIN_SETATTR_METHODDEF
2968     BUILTIN_SORTED_METHODDEF
2969     BUILTIN_SUM_METHODDEF
2970     {"vars",            builtin_vars,       METH_VARARGS, vars_doc},
2971     {NULL,              NULL},
2972 };
2973 
2974 PyDoc_STRVAR(builtin_doc,
2975 "Built-in functions, exceptions, and other objects.\n\
2976 \n\
2977 Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
2978 
2979 static struct PyModuleDef builtinsmodule = {
2980     PyModuleDef_HEAD_INIT,
2981     "builtins",
2982     builtin_doc,
2983     -1, /* multiple "initialization" just copies the module dict. */
2984     builtin_methods,
2985     NULL,
2986     NULL,
2987     NULL,
2988     NULL
2989 };
2990 
2991 
2992 PyObject *
_PyBuiltin_Init(PyInterpreterState * interp)2993 _PyBuiltin_Init(PyInterpreterState *interp)
2994 {
2995     PyObject *mod, *dict, *debug;
2996 
2997     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
2998 
2999     if (PyType_Ready(&PyFilter_Type) < 0 ||
3000         PyType_Ready(&PyMap_Type) < 0 ||
3001         PyType_Ready(&PyZip_Type) < 0)
3002         return NULL;
3003 
3004     mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
3005     if (mod == NULL)
3006         return NULL;
3007     dict = PyModule_GetDict(mod);
3008 
3009 #ifdef Py_TRACE_REFS
3010     /* "builtins" exposes a number of statically allocated objects
3011      * that, before this code was added in 2.3, never showed up in
3012      * the list of "all objects" maintained by Py_TRACE_REFS.  As a
3013      * result, programs leaking references to None and False (etc)
3014      * couldn't be diagnosed by examining sys.getobjects(0).
3015      */
3016 #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
3017 #else
3018 #define ADD_TO_ALL(OBJECT) (void)0
3019 #endif
3020 
3021 #define SETBUILTIN(NAME, OBJECT) \
3022     if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0)       \
3023         return NULL;                                                    \
3024     ADD_TO_ALL(OBJECT)
3025 
3026     SETBUILTIN("None",                  Py_None);
3027     SETBUILTIN("Ellipsis",              Py_Ellipsis);
3028     SETBUILTIN("NotImplemented",        Py_NotImplemented);
3029     SETBUILTIN("False",                 Py_False);
3030     SETBUILTIN("True",                  Py_True);
3031     SETBUILTIN("bool",                  &PyBool_Type);
3032     SETBUILTIN("memoryview",        &PyMemoryView_Type);
3033     SETBUILTIN("bytearray",             &PyByteArray_Type);
3034     SETBUILTIN("bytes",                 &PyBytes_Type);
3035     SETBUILTIN("classmethod",           &PyClassMethod_Type);
3036     SETBUILTIN("complex",               &PyComplex_Type);
3037     SETBUILTIN("dict",                  &PyDict_Type);
3038     SETBUILTIN("enumerate",             &PyEnum_Type);
3039     SETBUILTIN("filter",                &PyFilter_Type);
3040     SETBUILTIN("float",                 &PyFloat_Type);
3041     SETBUILTIN("frozenset",             &PyFrozenSet_Type);
3042     SETBUILTIN("property",              &PyProperty_Type);
3043     SETBUILTIN("int",                   &PyLong_Type);
3044     SETBUILTIN("list",                  &PyList_Type);
3045     SETBUILTIN("map",                   &PyMap_Type);
3046     SETBUILTIN("object",                &PyBaseObject_Type);
3047     SETBUILTIN("range",                 &PyRange_Type);
3048     SETBUILTIN("reversed",              &PyReversed_Type);
3049     SETBUILTIN("set",                   &PySet_Type);
3050     SETBUILTIN("slice",                 &PySlice_Type);
3051     SETBUILTIN("staticmethod",          &PyStaticMethod_Type);
3052     SETBUILTIN("str",                   &PyUnicode_Type);
3053     SETBUILTIN("super",                 &PySuper_Type);
3054     SETBUILTIN("tuple",                 &PyTuple_Type);
3055     SETBUILTIN("type",                  &PyType_Type);
3056     SETBUILTIN("zip",                   &PyZip_Type);
3057     debug = PyBool_FromLong(config->optimization_level == 0);
3058     if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
3059         Py_DECREF(debug);
3060         return NULL;
3061     }
3062     Py_DECREF(debug);
3063 
3064     return mod;
3065 #undef ADD_TO_ALL
3066 #undef SETBUILTIN
3067 }
3068