LCOV - code coverage report
Current view: top level - Python - bltinmodule.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 1046 1225 85.4 %
Date: 2022-07-07 18:19:46 Functions: 65 65 100.0 %

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

Generated by: LCOV version 1.14