Coverage Report

Created: 2022-07-08 09:39

/home/mdboom/Work/builds/cpython/Python/bltinmodule.c
Line
Count
Source (jump to first uncovered line)
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
update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
18
{
19
    Py_ssize_t i, j;
20
    PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
21
    assert(PyTuple_Check(bases));
22
23
    for (i = 0; i < nargs; 
i++44.4k
) {
  Branch (23:17): [True: 44.4k, False: 65.0k]
24
        base  = args[i];
25
        if (PyType_Check(base)) {
26
            if (new_bases) {
  Branch (26:17): [True: 15, False: 44.1k]
27
                /* If we already have made a replacement, then we append every normal base,
28
                   otherwise just skip it. */
29
                if (PyList_Append(new_bases, base) < 0) {
  Branch (29:21): [True: 0, False: 15]
30
                    goto error;
31
                }
32
            }
33
            continue;
34
        }
35
        if (_PyObject_LookupAttr(base, &_Py_ID(__mro_entries__), &meth) < 0) {
  Branch (35:13): [True: 0, False: 341]
36
            goto error;
37
        }
38
        if (!meth) {
  Branch (38:13): [True: 31, False: 310]
39
            if (new_bases) {
  Branch (39:17): [True: 0, False: 31]
40
                if (PyList_Append(new_bases, base) < 0) {
  Branch (40:21): [True: 0, False: 0]
41
                    goto error;
42
                }
43
            }
44
            continue;
45
        }
46
        new_base = PyObject_CallOneArg(meth, bases);
47
        Py_DECREF(meth);
48
        if (!new_base) {
  Branch (48:13): [True: 30, False: 280]
49
            goto error;
50
        }
51
        if (!PyTuple_Check(new_base)) {
  Branch (51:13): [True: 0, False: 280]
52
            PyErr_SetString(PyExc_TypeError,
53
                            "__mro_entries__ must return a tuple");
54
            Py_DECREF(new_base);
55
            goto error;
56
        }
57
        if (!new_bases) {
  Branch (57:13): [True: 260, False: 20]
58
            /* If this is a first successful replacement, create new_bases list and
59
               copy previously encountered bases. */
60
            if (!(new_bases = PyList_New(i))) {
  Branch (60:17): [True: 0, False: 260]
61
                Py_DECREF(new_base);
62
                goto error;
63
            }
64
            
for (j = 0; 260
j < i;
j++10
) {
  Branch (64:25): [True: 10, False: 260]
65
                base = args[j];
66
                PyList_SET_ITEM(new_bases, j, base);
67
                Py_INCREF(base);
68
            }
69
        }
70
        j = PyList_GET_SIZE(new_bases);
71
        if (PyList_SetSlice(new_bases, j, j, new_base) < 0) {
  Branch (71:13): [True: 0, False: 280]
72
            Py_DECREF(new_base);
73
            goto error;
74
        }
75
        Py_DECREF(new_base);
76
    }
77
    if (!new_bases) {
  Branch (77:9): [True: 64.7k, False: 260]
78
        return bases;
79
    }
80
    result = PyList_AsTuple(new_bases);
81
    Py_DECREF(new_bases);
82
    return result;
83
84
error:
85
    Py_XDECREF(new_bases);
86
    return NULL;
87
}
88
89
/* AC: cannot convert yet, waiting for *args support */
90
static PyObject *
91
builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
92
                        PyObject *kwnames)
93
{
94
    PyObject *func, *name, *winner, *prep;
95
    PyObject *cls = NULL, *cell = NULL, *ns = NULL, *meta = NULL, *orig_bases = NULL;
96
    PyObject *mkw = NULL, *bases = NULL;
97
    int isclass = 0;   /* initialize to prevent gcc warning */
98
99
    if (nargs < 2) {
  Branch (99:9): [True: 0, False: 65.0k]
100
        PyErr_SetString(PyExc_TypeError,
101
                        "__build_class__: not enough arguments");
102
        return NULL;
103
    }
104
    func = args[0];   /* Better be callable */
105
    if (!PyFunction_Check(func)) {
  Branch (105:9): [True: 0, False: 65.0k]
106
        PyErr_SetString(PyExc_TypeError,
107
                        "__build_class__: func must be a function");
108
        return NULL;
109
    }
110
    name = args[1];
111
    if (!PyUnicode_Check(name)) {
  Branch (111:9): [True: 0, False: 65.0k]
112
        PyErr_SetString(PyExc_TypeError,
113
                        "__build_class__: name is not a string");
114
        return NULL;
115
    }
116
    orig_bases = _PyTuple_FromArray(args + 2, nargs - 2);
117
    if (orig_bases == NULL)
  Branch (117:9): [True: 0, False: 65.0k]
118
        return NULL;
119
120
    bases = update_bases(orig_bases, args + 2, nargs - 2);
121
    if (bases == NULL) {
  Branch (121:9): [True: 30, False: 65.0k]
122
        Py_DECREF(orig_bases);
123
        return NULL;
124
    }
125
126
    if (kwnames == NULL) {
  Branch (126:9): [True: 61.8k, False: 3.18k]
127
        meta = NULL;
128
        mkw = NULL;
129
    }
130
    else {
131
        mkw = _PyStack_AsDict(args + nargs, kwnames);
132
        if (mkw == NULL) {
  Branch (132:13): [True: 0, False: 3.18k]
133
            goto error;
134
        }
135
136
        meta = _PyDict_GetItemWithError(mkw, &_Py_ID(metaclass));
137
        if (meta != NULL) {
  Branch (137:13): [True: 2.93k, False: 248]
138
            Py_INCREF(meta);
139
            if (PyDict_DelItem(mkw, &_Py_ID(metaclass)) < 0) {
  Branch (139:17): [True: 0, False: 2.93k]
140
                goto error;
141
            }
142
            /* metaclass is explicitly given, check if it's indeed a class */
143
            isclass = PyType_Check(meta);
144
        }
145
        else if (PyErr_Occurred()) {
  Branch (145:18): [True: 0, False: 248]
146
            goto error;
147
        }
148
    }
149
    if (meta == NULL) {
  Branch (149:9): [True: 62.0k, False: 2.93k]
150
        /* if there are no bases, use type: */
151
        if (PyTuple_GET_SIZE(bases) == 0) {
  Branch (151:13): [True: 25.1k, False: 36.9k]
152
            meta = (PyObject *) (&PyType_Type);
153
        }
154
        /* else get the type of the first base */
155
        else {
156
            PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
157
            meta = (PyObject *)Py_TYPE(base0);
158
        }
159
        Py_INCREF(meta);
160
        isclass = 1;  /* meta is really a class */
161
    }
162
163
    if (isclass) {
  Branch (163:9): [True: 65.0k, False: 6]
164
        /* meta is really a class, so check for a more derived
165
           metaclass, or possible metaclass conflicts: */
166
        winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
167
                                                        bases);
168
        if (winner == NULL) {
  Branch (168:13): [True: 5, False: 65.0k]
169
            goto error;
170
        }
171
        if (winner != meta) {
  Branch (171:13): [True: 1.51k, False: 63.4k]
172
            Py_DECREF(meta);
173
            meta = winner;
174
            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
    if (_PyObject_LookupAttr(meta, &_Py_ID(__prepare__), &prep) < 0) {
  Branch (179:9): [True: 1, False: 65.0k]
180
        ns = NULL;
181
    }
182
    else if (prep == NULL) {
  Branch (182:14): [True: 4, False: 65.0k]
183
        ns = PyDict_New();
184
    }
185
    else {
186
        PyObject *pargs[2] = {name, bases};
187
        ns = PyObject_VectorcallDict(prep, pargs, 2, mkw);
188
        Py_DECREF(prep);
189
    }
190
    if (ns == NULL) {
  Branch (190:9): [True: 7, False: 65.0k]
191
        goto error;
192
    }
193
    if (!PyMapping_Check(ns)) {
  Branch (193:9): [True: 2, False: 65.0k]
194
        PyErr_Format(PyExc_TypeError,
195
                     "%.200s.__prepare__() must return a mapping, not %.200s",
196
                     isclass ? 
((PyTypeObject *)meta)->tp_name1
:
"<metaclass>"1
,
  Branch (196:22): [True: 1, False: 1]
197
                     Py_TYPE(ns)->tp_name);
198
        goto error;
199
    }
200
    PyThreadState *tstate = _PyThreadState_GET();
201
    EVAL_CALL_STAT_INC(EVAL_CALL_BUILD_CLASS);
202
    cell = _PyEval_Vector(tstate, (PyFunctionObject *)func, ns, NULL, 0, NULL);
203
    if (cell != NULL) {
  Branch (203:9): [True: 64.9k, False: 65]
204
        if (bases != orig_bases) {
  Branch (204:13): [True: 260, False: 64.6k]
205
            if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
  Branch (205:17): [True: 0, False: 260]
206
                goto error;
207
            }
208
        }
209
        PyObject *margs[3] = {name, bases, ns};
210
        cls = PyObject_VectorcallDict(meta, margs, 3, mkw);
211
        if (cls != NULL && 
PyType_Check64.7k
(cls) &&
PyCell_Check64.7k
(cell)) {
  Branch (211:13): [True: 64.7k, False: 143]
212
            PyObject *cell_cls = PyCell_GET(cell);
213
            if (cell_cls != cls) {
  Branch (213:17): [True: 2, False: 3.16k]
214
                if (cell_cls == NULL) {
  Branch (214:21): [True: 1, False: 1]
215
                    const char *msg =
216
                        "__class__ not set defining %.200R as %.200R. "
217
                        "Was __classcell__ propagated to type.__new__?";
218
                    PyErr_Format(PyExc_RuntimeError, msg, name, cls);
219
                } else {
220
                    const char *msg =
221
                        "__class__ set to %.200R defining %.200R as %.200R";
222
                    PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
223
                }
224
                Py_DECREF(cls);
225
                cls = NULL;
226
                goto error;
227
            }
228
        }
229
    }
230
error:
231
    Py_XDECREF(cell);
232
    Py_XDECREF(ns);
233
    Py_XDECREF(meta);
234
    Py_XDECREF(mkw);
235
    if (bases != orig_bases) {
  Branch (235:9): [True: 260, False: 64.7k]
236
        Py_DECREF(orig_bases);
237
    }
238
    Py_DECREF(bases);
239
    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
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
    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
builtin_abs(PyObject *module, PyObject *x)
294
/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
295
{
296
    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
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
    it = PyObject_GetIter(iterable);
319
    if (it == NULL)
  Branch (319:9): [True: 2, False: 104k]
320
        return NULL;
321
    iternext = *Py_TYPE(it)->tp_iternext;
322
323
    for (;;) {
324
        item = iternext(it);
325
        if (item == NULL)
  Branch (325:13): [True: 84.9k, False: 510k]
326
            break;
327
        cmp = PyObject_IsTrue(item);
328
        Py_DECREF(item);
329
        if (cmp < 0) {
  Branch (329:13): [True: 1, False: 510k]
330
            Py_DECREF(it);
331
            return NULL;
332
        }
333
        if (cmp == 0) {
  Branch (333:13): [True: 20.0k, False: 490k]
334
            Py_DECREF(it);
335
            Py_RETURN_FALSE;
336
        }
337
    }
338
    Py_DECREF(it);
339
    if (PyErr_Occurred()) {
  Branch (339:9): [True: 0, False: 84.9k]
340
        if (PyErr_ExceptionMatches(PyExc_StopIteration))
  Branch (340:13): [True: 0, False: 0]
341
            PyErr_Clear();
342
        else
343
            return NULL;
344
    }
345
    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
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
    it = PyObject_GetIter(iterable);
368
    if (it == NULL)
  Branch (368:9): [True: 2, False: 189k]
369
        return NULL;
370
    iternext = *Py_TYPE(it)->tp_iternext;
371
372
    for (;;) {
373
        item = iternext(it);
374
        if (item == NULL)
  Branch (374:13): [True: 66.2k, False: 20.8M]
375
            break;
376
        cmp = PyObject_IsTrue(item);
377
        Py_DECREF(item);
378
        if (cmp < 0) {
  Branch (378:13): [True: 1, False: 20.8M]
379
            Py_DECREF(it);
380
            return NULL;
381
        }
382
        if (cmp > 0) {
  Branch (382:13): [True: 123k, False: 20.7M]
383
            Py_DECREF(it);
384
            Py_RETURN_TRUE;
385
        }
386
    }
387
    Py_DECREF(it);
388
    if (PyErr_Occurred()) {
  Branch (388:9): [True: 0, False: 66.2k]
389
        if (PyErr_ExceptionMatches(PyExc_StopIteration))
  Branch (389:13): [True: 0, False: 0]
390
            PyErr_Clear();
391
        else
392
            return NULL;
393
    }
394
    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
builtin_ascii(PyObject *module, PyObject *obj)
413
/*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
414
{
415
    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
builtin_bin(PyObject *module, PyObject *number)
433
/*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
434
{
435
    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
builtin_callable(PyObject *module, PyObject *obj)
453
/*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
454
{
455
    return PyBool_FromLong((long)PyCallable_Check(obj));
456
}
457
458
static PyObject *
459
builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
460
{
461
    PyObject *hook = PySys_GetObject("breakpointhook");
462
463
    if (hook == NULL) {
  Branch (463:9): [True: 0, False: 22]
464
        PyErr_SetString(PyExc_RuntimeError, "lost sys.breakpointhook");
465
        return NULL;
466
    }
467
468
    if (PySys_Audit("builtins.breakpoint", "O", hook) < 0) {
  Branch (468:9): [True: 0, False: 22]
469
        return NULL;
470
    }
471
472
    Py_INCREF(hook);
473
    PyObject *retval = PyObject_Vectorcall(hook, args, nargs, keywords);
474
    Py_DECREF(hook);
475
    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
filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
494
{
495
    PyObject *func, *seq;
496
    PyObject *it;
497
    filterobject *lz;
498
499
    if ((type == &PyFilter_Type || type->tp_init == PyFilter_Type.tp_init) &&
  Branch (499:10): [True: 0, False: 4]
  Branch (499:36): [True: 3, False: 1]
500
        
!3
_PyArg_NoKeywords3
("filter", kwds))
501
        return NULL;
502
503
    if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
  Branch (503:9): [True: 0, False: 3]
504
        return NULL;
505
506
    /* Get iterator. */
507
    it = PyObject_GetIter(seq);
508
    if (it == NULL)
  Branch (508:9): [True: 0, False: 3]
509
        return NULL;
510
511
    /* create filterobject structure */
512
    lz = (filterobject *)type->tp_alloc(type, 0);
513
    if (lz == NULL) {
  Branch (513:9): [True: 0, False: 3]
514
        Py_DECREF(it);
515
        return NULL;
516
    }
517
518
    lz->func = Py_NewRef(func);
519
    lz->it = it;
520
521
    return (PyObject *)lz;
522
}
523
524
static PyObject *
525
filter_vectorcall(PyObject *type, PyObject * const*args,
526
                size_t nargsf, PyObject *kwnames)
527
{
528
    PyTypeObject *tp = _PyType_CAST(type);
529
    if (tp == &PyFilter_Type && !_PyArg_NoKwnames("filter", kwnames)) {
  Branch (529:9): [True: 7.92k, False: 0]
530
        return NULL;
531
    }
532
533
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
534
    if (!_PyArg_CheckPositional("filter", nargs, 2, 2)) {
535
        return NULL;
536
    }
537
538
    PyObject *it = PyObject_GetIter(args[1]);
539
    if (it == NULL) {
  Branch (539:9): [True: 13, False: 7.90k]
540
        return NULL;
541
    }
542
543
    filterobject *lz = (filterobject *)tp->tp_alloc(tp, 0);
544
545
    if (lz == NULL) {
  Branch (545:9): [True: 0, False: 7.90k]
546
        Py_DECREF(it);
547
        return NULL;
548
    }
549
550
    lz->func = Py_NewRef(args[0]);
551
    lz->it = it;
552
553
    return (PyObject *)lz;
554
}
555
556
static void
557
filter_dealloc(filterobject *lz)
558
{
559
    PyObject_GC_UnTrack(lz);
560
    Py_XDECREF(lz->func);
561
    Py_XDECREF(lz->it);
562
    Py_TYPE(lz)->tp_free(lz);
563
}
564
565
static int
566
filter_traverse(filterobject *lz, visitproc visit, void *arg)
567
{
568
    Py_VISIT(lz->it);
569
    Py_VISIT(lz->func);
570
    return 0;
571
}
572
573
static PyObject *
574
filter_next(filterobject *lz)
575
{
576
    PyObject *item;
577
    PyObject *it = lz->it;
578
    long ok;
579
    PyObject *(*iternext)(PyObject *);
580
    int checktrue = lz->func == Py_None || 
lz->func == (PyObject *)&PyBool_Type59.3k
;
  Branch (580:21): [True: 9.35k, False: 59.3k]
  Branch (580:44): [True: 3, False: 59.3k]
581
582
    iternext = *Py_TYPE(it)->tp_iternext;
583
    for (;;) {
584
        item = iternext(it);
585
        if (item == NULL)
  Branch (585:13): [True: 7.46k, False: 473k]
586
            return NULL;
587
588
        if (checktrue) {
  Branch (588:13): [True: 9.54k, False: 463k]
589
            ok = PyObject_IsTrue(item);
590
        } else {
591
            PyObject *good;
592
            good = PyObject_CallOneArg(lz->func, item);
593
            if (good == NULL) {
  Branch (593:17): [True: 3, False: 463k]
594
                Py_DECREF(item);
595
                return NULL;
596
            }
597
            ok = PyObject_IsTrue(good);
598
            Py_DECREF(good);
599
        }
600
        if (ok > 0)
  Branch (600:13): [True: 61.2k, False: 412k]
601
            return item;
602
        Py_DECREF(item);
603
        if (ok < 0)
  Branch (603:13): [True: 0, False: 412k]
604
            return NULL;
605
    }
606
}
607
608
static PyObject *
609
filter_reduce(filterobject *lz, PyObject *Py_UNUSED(ignored))
610
{
611
    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
builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
689
/*[clinic end generated code: output=2f40bdfa4954b077 input=88339c93ea522b33]*/
690
{
691
    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
builtin_chr_impl(PyObject *module, int i)
705
/*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
706
{
707
    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
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
    int compile_mode = -1;
746
    int is_ast;
747
    int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
748
    PyObject *result;
749
750
    PyCompilerFlags cf = _PyCompilerFlags_INIT;
751
    cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
752
    if (feature_version >= 0 && 
(flags & 209
PyCF_ONLY_AST209
)) {
  Branch (752:9): [True: 209, False: 17.6k]
  Branch (752:33): [True: 209, False: 0]
753
        cf.cf_feature_version = feature_version;
754
    }
755
756
    if (flags &
  Branch (756:9): [True: 1, False: 17.8k]
757
        ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_COMPILE_MASK))
758
    {
759
        PyErr_SetString(PyExc_ValueError,
760
                        "compile(): unrecognised flags");
761
        goto error;
762
    }
763
    /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
764
765
    if (optimize < -1 || optimize > 2) {
  Branch (765:9): [True: 0, False: 17.8k]
  Branch (765:26): [True: 0, False: 17.8k]
766
        PyErr_SetString(PyExc_ValueError,
767
                        "compile(): invalid optimize value");
768
        goto error;
769
    }
770
771
    if (!dont_inherit) {
  Branch (771:9): [True: 11.9k, False: 5.91k]
772
        PyEval_MergeCompilerFlags(&cf);
773
    }
774
775
    if (strcmp(mode, "exec") == 0)
  Branch (775:9): [True: 12.0k, False: 5.73k]
776
        compile_mode = 0;
777
    else if (strcmp(mode, "eval") == 0)
  Branch (777:14): [True: 1.05k, False: 4.68k]
778
        compile_mode = 1;
779
    else if (strcmp(mode, "single") == 0)
  Branch (779:14): [True: 4.66k, False: 17]
780
        compile_mode = 2;
781
    else if (strcmp(mode, "func_type") == 0) {
  Branch (781:14): [True: 15, False: 2]
782
        if (!(flags & PyCF_ONLY_AST)) {
  Branch (782:13): [True: 0, False: 15]
783
            PyErr_SetString(PyExc_ValueError,
784
                            "compile() mode 'func_type' requires flag PyCF_ONLY_AST");
785
            goto error;
786
        }
787
        compile_mode = 3;
788
    }
789
    else {
790
        const char *msg;
791
        if (flags & PyCF_ONLY_AST)
  Branch (791:13): [True: 0, False: 2]
792
            msg = "compile() mode must be 'exec', 'eval', 'single' or 'func_type'";
793
        else
794
            msg = "compile() mode must be 'exec', 'eval' or 'single'";
795
        PyErr_SetString(PyExc_ValueError, msg);
796
        goto error;
797
    }
798
799
    is_ast = PyAST_Check(source);
800
    if (is_ast == -1)
  Branch (800:9): [True: 0, False: 17.8k]
801
        goto error;
802
    if (is_ast) {
  Branch (802:9): [True: 690, False: 17.1k]
803
        if (flags & PyCF_ONLY_AST) {
  Branch (803:13): [True: 2, False: 688]
804
            Py_INCREF(source);
805
            result = source;
806
        }
807
        else {
808
            PyArena *arena;
809
            mod_ty mod;
810
811
            arena = _PyArena_New();
812
            if (arena == NULL)
  Branch (812:17): [True: 0, False: 688]
813
                goto error;
814
            mod = PyAST_obj2mod(source, arena, compile_mode);
815
            if (mod == NULL || 
!_PyAST_Validate(mod)679
) {
  Branch (815:17): [True: 9, False: 679]
  Branch (815:32): [True: 177, False: 502]
816
                _PyArena_Free(arena);
817
                goto error;
818
            }
819
            result = (PyObject*)_PyAST_Compile(mod, filename,
820
                                               &cf, optimize, arena);
821
            _PyArena_Free(arena);
822
        }
823
        goto finally;
824
    }
825
826
    str = _Py_SourceAsString(source, "compile", "string, bytes or AST", &cf, &source_copy);
827
    if (str == NULL)
  Branch (827:9): [True: 4, False: 17.1k]
828
        goto error;
829
830
    result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
831
832
    Py_XDECREF(source_copy);
833
    goto finally;
834
835
error:
836
    result = NULL;
837
finally:
838
    Py_DECREF(filename);
839
    return result;
840
}
841
842
/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
843
static PyObject *
844
builtin_dir(PyObject *self, PyObject *args)
845
{
846
    PyObject *arg = NULL;
847
848
    if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
  Branch (848:9): [True: 1, False: 19.7k]
849
        return NULL;
850
    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
builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
879
/*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
880
{
881
    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
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
    if (locals != Py_None && 
!PyMapping_Check(locals)778
) {
  Branch (910:9): [True: 778, False: 35.4k]
  Branch (910:30): [True: 1, False: 777]
911
        PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
912
        return NULL;
913
    }
914
    if (globals != Py_None && 
!24.2k
PyDict_Check24.2k
(globals)) {
  Branch (914:9): [True: 24.2k, False: 11.8k]
  Branch (914:31): [True: 1, False: 24.2k]
915
        PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
  Branch (915:42): [True: 1, False: 0]
916
            "globals must be a real dict; try eval(expr, {}, mapping)"
917
            : 
"globals must be a dict"0
);
918
        return NULL;
919
    }
920
    if (globals == Py_None) {
  Branch (920:9): [True: 11.8k, False: 24.2k]
921
        globals = PyEval_GetGlobals();
922
        if (locals == Py_None) {
  Branch (922:13): [True: 11.3k, False: 588]
923
            locals = PyEval_GetLocals();
924
            if (locals == NULL)
  Branch (924:17): [True: 0, False: 11.3k]
925
                return NULL;
926
        }
927
    }
928
    else if (locals == Py_None)
  Branch (928:14): [True: 24.0k, False: 189]
929
        locals = globals;
930
931
    if (globals == NULL || locals == NULL) {
  Branch (931:9): [True: 0, False: 36.1k]
  Branch (931:28): [True: 0, False: 36.1k]
932
        PyErr_SetString(PyExc_TypeError,
933
            "eval must be given globals and locals "
934
            "when called without a frame");
935
        return NULL;
936
    }
937
938
    int r = PyDict_Contains(globals, &_Py_ID(__builtins__));
939
    if (r == 0) {
  Branch (939:9): [True: 23.3k, False: 12.8k]
940
        r = PyDict_SetItem(globals, &_Py_ID(__builtins__), PyEval_GetBuiltins());
941
    }
942
    if (r < 0) {
  Branch (942:9): [True: 0, False: 36.1k]
943
        return NULL;
944
    }
945
946
    if (PyCode_Check(source)) {
947
        if (PySys_Audit("exec", "O", source) < 0) {
  Branch (947:13): [True: 0, False: 171]
948
            return NULL;
949
        }
950
951
        if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
  Branch (951:13): [True: 1, False: 170]
952
            PyErr_SetString(PyExc_TypeError,
953
                "code object passed to eval() may not contain free variables");
954
            return NULL;
955
        }
956
        return PyEval_EvalCode(source, globals, locals);
957
    }
958
959
    PyCompilerFlags cf = _PyCompilerFlags_INIT;
960
    cf.cf_flags = PyCF_SOURCE_IS_UTF8;
961
    str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
962
    if (str == NULL)
  Branch (962:9): [True: 2, False: 36.0k]
963
        return NULL;
964
965
    
while (36.0k
*str == ' ' ||
*str == '\t'36.0k
)
  Branch (965:12): [True: 75, False: 36.0k]
  Branch (965:27): [True: 0, False: 36.0k]
966
        str++;
967
968
    (void)PyEval_MergeCompilerFlags(&cf);
969
    result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
970
    Py_XDECREF(source_copy);
971
    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
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
    if (globals == Py_None) {
  Branch (1002:9): [True: 144, False: 18.6k]
1003
        globals = PyEval_GetGlobals();
1004
        if (locals == Py_None) {
  Branch (1004:13): [True: 143, False: 1]
1005
            locals = PyEval_GetLocals();
1006
            if (locals == NULL)
  Branch (1006:17): [True: 0, False: 143]
1007
                return NULL;
1008
        }
1009
        if (!globals || !locals) {
  Branch (1009:13): [True: 0, False: 144]
  Branch (1009:25): [True: 0, False: 144]
1010
            PyErr_SetString(PyExc_SystemError,
1011
                            "globals and locals cannot be NULL");
1012
            return NULL;
1013
        }
1014
    }
1015
    else if (locals == Py_None)
  Branch (1015:14): [True: 16.0k, False: 2.62k]
1016
        locals = globals;
1017
1018
    if (!PyDict_Check(globals)) {
  Branch (1018:9): [True: 1, False: 18.7k]
1019
        PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
1020
                     Py_TYPE(globals)->tp_name);
1021
        return NULL;
1022
    }
1023
    if (!PyMapping_Check(locals)) {
  Branch (1023:9): [True: 1, False: 18.7k]
1024
        PyErr_Format(PyExc_TypeError,
1025
            "locals must be a mapping or None, not %.100s",
1026
            Py_TYPE(locals)->tp_name);
1027
        return NULL;
1028
    }
1029
    int r = PyDict_Contains(globals, &_Py_ID(__builtins__));
1030
    if (r == 0) {
  Branch (1030:9): [True: 12.9k, False: 5.81k]
1031
        r = PyDict_SetItem(globals, &_Py_ID(__builtins__), PyEval_GetBuiltins());
1032
    }
1033
    if (r < 0) {
  Branch (1033:9): [True: 0, False: 18.7k]
1034
        return NULL;
1035
    }
1036
1037
    if (closure == Py_None) {
  Branch (1037:9): [True: 1, False: 18.7k]
1038
        closure = NULL;
1039
    }
1040
1041
    if (PyCode_Check(source)) {
1042
        Py_ssize_t num_free = PyCode_GetNumFree((PyCodeObject *)source);
1043
        if (num_free == 0) {
  Branch (1043:13): [True: 13.1k, False: 7]
1044
            if (closure) {
  Branch (1044:17): [True: 1, False: 13.1k]
1045
                PyErr_SetString(PyExc_TypeError,
1046
                    "cannot use a closure with this code object");
1047
                return NULL;
1048
            }
1049
        } else {
1050
            int closure_is_ok =
1051
                closure
  Branch (1051:17): [True: 5, False: 2]
1052
                && PyTuple_CheckExact(closure)
1053
                && 
(4
PyTuple_GET_SIZE4
(closure) == num_free);
  Branch (1053:20): [True: 3, False: 1]
1054
            if (closure_is_ok) {
  Branch (1054:17): [True: 3, False: 4]
1055
                for (Py_ssize_t i = 0; i < num_free; 
i++6
) {
  Branch (1055:40): [True: 7, False: 2]
1056
                    PyObject *cell = PyTuple_GET_ITEM(closure, i);
1057
                    if (!PyCell_Check(cell)) {
  Branch (1057:25): [True: 1, False: 6]
1058
                        closure_is_ok = 0;
1059
                        break;
1060
                    }
1061
                }
1062
            }
1063
            if (!closure_is_ok) {
  Branch (1063:17): [True: 5, False: 2]
1064
                PyErr_Format(PyExc_TypeError,
1065
                    "code object requires a closure of exactly length %zd",
1066
                    num_free);
1067
                return NULL;
1068
            }
1069
        }
1070
1071
        if (PySys_Audit("exec", "O", source) < 0) {
  Branch (1071:13): [True: 0, False: 13.1k]
1072
            return NULL;
1073
        }
1074
1075
        if (!closure) {
  Branch (1075:13): [True: 13.1k, False: 2]
1076
            v = PyEval_EvalCode(source, globals, locals);
1077
        } else {
1078
            v = PyEval_EvalCodeEx(source, globals, locals,
1079
                NULL, 0,
1080
                NULL, 0,
1081
                NULL, 0,
1082
                NULL,
1083
                closure);
1084
        }
1085
    }
1086
    else {
1087
        if (closure != NULL) {
  Branch (1087:13): [True: 0, False: 5.61k]
1088
            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
        PyCompilerFlags cf = _PyCompilerFlags_INIT;
1094
        cf.cf_flags = PyCF_SOURCE_IS_UTF8;
1095
        str = _Py_SourceAsString(source, "exec",
1096
                                       "string, bytes or code", &cf,
1097
                                       &source_copy);
1098
        if (str == NULL)
  Branch (1098:13): [True: 1, False: 5.61k]
1099
            return NULL;
1100
        if (PyEval_MergeCompilerFlags(&cf))
  Branch (1100:13): [True: 5.61k, False: 0]
1101
            v = PyRun_StringFlags(str, Py_file_input, globals,
1102
                                  locals, &cf);
1103
        else
1104
            v = PyRun_String(str, Py_file_input, globals, locals);
1105
        Py_XDECREF(source_copy);
1106
    }
1107
    if (v == NULL)
  Branch (1107:9): [True: 1.02k, False: 17.7k]
1108
        return NULL;
1109
    Py_DECREF(v);
1110
    Py_RETURN_NONE;
1111
}
1112
1113
1114
/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
1115
static PyObject *
1116
builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1117
{
1118
    PyObject *v, *name, *result;
1119
1120
    if (!_PyArg_CheckPositional("getattr", nargs, 2, 3))
1121
        return NULL;
1122
1123
    v = args[0];
1124
    name = args[1];
1125
    if (nargs > 2) {
  Branch (1125:9): [True: 7.56M, False: 2.77M]
1126
        if (_PyObject_LookupAttr(v, name, &result) == 0) {
  Branch (1126:13): [True: 5.21M, False: 2.34M]
1127
            PyObject *dflt = args[2];
1128
            Py_INCREF(dflt);
1129
            return dflt;
1130
        }
1131
    }
1132
    else {
1133
        result = PyObject_GetAttr(v, name);
1134
    }
1135
    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
builtin_globals_impl(PyObject *module)
1157
/*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
1158
{
1159
    PyObject *d;
1160
1161
    d = PyEval_GetGlobals();
1162
    Py_XINCREF(d);
1163
    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
builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1181
/*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
1182
{
1183
    PyObject *v;
1184
1185
    if (_PyObject_LookupAttr(obj, name, &v) < 0) {
  Branch (1185:9): [True: 8, False: 1.79M]
1186
        return NULL;
1187
    }
1188
    if (v == NULL) {
  Branch (1188:9): [True: 866k, False: 924k]
1189
        Py_RETURN_FALSE;
1190
    }
1191
    Py_DECREF(v);
1192
    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
builtin_id(PyModuleDef *self, PyObject *v)
1215
/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
1216
{
1217
    PyObject *id = PyLong_FromVoidPtr(v);
1218
1219
    if (id && PySys_Audit("builtins.id", "O", id) < 0) {
  Branch (1219:9): [True: 2.41M, False: 0]
  Branch (1219:15): [True: 3, False: 2.41M]
1220
        Py_DECREF(id);
1221
        return NULL;
1222
    }
1223
1224
    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
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
    if ((type == &PyMap_Type || type->tp_init == PyMap_Type.tp_init) &&
  Branch (1243:10): [True: 0, False: 4]
  Branch (1243:33): [True: 3, False: 1]
1244
        
!3
_PyArg_NoKeywords3
("map", kwds))
1245
        return NULL;
1246
1247
    numargs = PyTuple_Size(args);
1248
    if (numargs < 2) {
  Branch (1248:9): [True: 0, False: 3]
1249
        PyErr_SetString(PyExc_TypeError,
1250
           "map() must have at least two arguments.");
1251
        return NULL;
1252
    }
1253
1254
    iters = PyTuple_New(numargs-1);
1255
    if (iters == NULL)
  Branch (1255:9): [True: 0, False: 3]
1256
        return NULL;
1257
1258
    
for (i=1 ; 3
i<numargs ;
i++3
) {
  Branch (1258:16): [True: 3, False: 3]
1259
        /* Get iterator. */
1260
        it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1261
        if (it == NULL) {
  Branch (1261:13): [True: 0, False: 3]
1262
            Py_DECREF(iters);
1263
            return NULL;
1264
        }
1265
        PyTuple_SET_ITEM(iters, i-1, it);
1266
    }
1267
1268
    /* create mapobject structure */
1269
    lz = (mapobject *)type->tp_alloc(type, 0);
1270
    if (lz == NULL) {
  Branch (1270:9): [True: 0, False: 3]
1271
        Py_DECREF(iters);
1272
        return NULL;
1273
    }
1274
    lz->iters = iters;
1275
    func = PyTuple_GET_ITEM(args, 0);
1276
    lz->func = Py_NewRef(func);
1277
1278
    return (PyObject *)lz;
1279
}
1280
1281
static PyObject *
1282
map_vectorcall(PyObject *type, PyObject * const*args,
1283
                size_t nargsf, PyObject *kwnames)
1284
{
1285
    PyTypeObject *tp = _PyType_CAST(type);
1286
    if (tp == &PyMap_Type && !_PyArg_NoKwnames("map", kwnames)) {
  Branch (1286:9): [True: 755k, False: 0]
1287
        return NULL;
1288
    }
1289
1290
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1291
    if (nargs < 2) {
  Branch (1291:9): [True: 3, False: 755k]
1292
        PyErr_SetString(PyExc_TypeError,
1293
           "map() must have at least two arguments.");
1294
        return NULL;
1295
    }
1296
1297
    PyObject *iters = PyTuple_New(nargs-1);
1298
    if (iters == NULL) {
  Branch (1298:9): [True: 0, False: 755k]
1299
        return NULL;
1300
    }
1301
1302
    
for (int i=1; 755k
i<nargs;
i++756k
) {
  Branch (1302:19): [True: 756k, False: 755k]
1303
        PyObject *it = PyObject_GetIter(args[i]);
1304
        if (it == NULL) {
  Branch (1304:13): [True: 14, False: 756k]
1305
            Py_DECREF(iters);
1306
            return NULL;
1307
        }
1308
        PyTuple_SET_ITEM(iters, i-1, it);
1309
    }
1310
1311
    mapobject *lz = (mapobject *)tp->tp_alloc(tp, 0);
1312
    if (lz == NULL) {
  Branch (1312:9): [True: 0, False: 755k]
1313
        Py_DECREF(iters);
1314
        return NULL;
1315
    }
1316
    lz->iters = iters;
1317
    lz->func = Py_NewRef(args[0]);
1318
1319
    return (PyObject *)lz;
1320
}
1321
1322
static void
1323
map_dealloc(mapobject *lz)
1324
{
1325
    PyObject_GC_UnTrack(lz);
1326
    Py_XDECREF(lz->iters);
1327
    Py_XDECREF(lz->func);
1328
    Py_TYPE(lz)->tp_free(lz);
1329
}
1330
1331
static int
1332
map_traverse(mapobject *lz, visitproc visit, void *arg)
1333
{
1334
    Py_VISIT(lz->iters);
1335
    Py_VISIT(lz->func);
1336
    return 0;
1337
}
1338
1339
static PyObject *
1340
map_next(mapobject *lz)
1341
{
1342
    PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
1343
    PyObject **stack;
1344
    PyObject *result = NULL;
1345
    PyThreadState *tstate = _PyThreadState_GET();
1346
1347
    const Py_ssize_t niters = PyTuple_GET_SIZE(lz->iters);
1348
    if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
  Branch (1348:9): [True: 8.02M, False: 0]
1349
        stack = small_stack;
1350
    }
1351
    else {
1352
        stack = PyMem_Malloc(niters * sizeof(stack[0]));
1353
        if (stack == NULL) {
  Branch (1353:13): [True: 0, False: 0]
1354
            _PyErr_NoMemory(tstate);
1355
            return NULL;
1356
        }
1357
    }
1358
1359
    Py_ssize_t nargs = 0;
1360
    for (Py_ssize_t i=0; i < niters; 
i++7.85M
) {
  Branch (1360:26): [True: 8.49M, False: 7.38M]
1361
        PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1362
        PyObject *val = Py_TYPE(it)->tp_iternext(it);
1363
        if (val == NULL) {
  Branch (1363:13): [True: 641k, False: 7.85M]
1364
            goto exit;
1365
        }
1366
        stack[i] = val;
1367
        nargs++;
1368
    }
1369
1370
    result = _PyObject_VectorcallTstate(tstate, lz->func, stack, nargs, NULL);
1371
1372
exit:
1373
    for (Py_ssize_t i=0; i < nargs; 
i++7.85M
) {
  Branch (1373:26): [True: 7.85M, False: 8.02M]
1374
        Py_DECREF(stack[i]);
1375
    }
1376
    if (stack != small_stack) {
  Branch (1376:9): [True: 0, False: 8.02M]
1377
        PyMem_Free(stack);
1378
    }
1379
    return result;
1380
}
1381
1382
static PyObject *
1383
map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
1384
{
1385
    Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1386
    PyObject *args = PyTuple_New(numargs+1);
1387
    Py_ssize_t i;
1388
    if (args == NULL)
  Branch (1388:9): [True: 0, False: 44]
1389
        return NULL;
1390
    Py_INCREF(lz->func);
1391
    PyTuple_SET_ITEM(args, 0, lz->func);
1392
    for (i = 0; i<numargs; 
i++58
){
  Branch (1392:17): [True: 58, False: 44]
1393
        PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1394
        Py_INCREF(it);
1395
        PyTuple_SET_ITEM(args, i+1, it);
1396
    }
1397
1398
    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
builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1462
{
1463
    PyObject *it, *res;
1464
1465
    if (!_PyArg_CheckPositional("next", nargs, 1, 2))
1466
        return NULL;
1467
1468
    it = args[0];
1469
    if (!PyIter_Check(it)) {
  Branch (1469:9): [True: 0, False: 2.82M]
1470
        PyErr_Format(PyExc_TypeError,
1471
            "'%.200s' object is not an iterator",
1472
            Py_TYPE(it)->tp_name);
1473
        return NULL;
1474
    }
1475
1476
    res = (*Py_TYPE(it)->tp_iternext)(it);
1477
    if (res != NULL) {
  Branch (1477:9): [True: 2.17M, False: 654k]
1478
        return res;
1479
    } else 
if (654k
nargs > 1654k
) {
  Branch (1479:16): [True: 1.67k, False: 653k]
1480
        PyObject *def = args[1];
1481
        if (PyErr_Occurred()) {
  Branch (1481:13): [True: 1, False: 1.67k]
1482
            if(!PyErr_ExceptionMatches(PyExc_StopIteration))
  Branch (1482:16): [True: 0, False: 1]
1483
                return NULL;
1484
            PyErr_Clear();
1485
        }
1486
        Py_INCREF(def);
1487
        return def;
1488
    } else if (PyErr_Occurred()) {
  Branch (1488:16): [True: 186, False: 652k]
1489
        return NULL;
1490
    } else {
1491
        PyErr_SetNone(PyExc_StopIteration);
1492
        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
builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
1518
                     PyObject *value)
1519
/*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
1520
{
1521
    if (PyObject_SetAttr(obj, name, value) != 0)
  Branch (1521:9): [True: 438, False: 499k]
1522
        return NULL;
1523
    
Py_RETURN_NONE499k
;
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
builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1541
/*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
1542
{
1543
    if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
  Branch (1543:9): [True: 132, False: 133k]
1544
        return NULL;
1545
    
Py_RETURN_NONE133k
;
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
builtin_hash(PyObject *module, PyObject *obj)
1563
/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
1564
{
1565
    Py_hash_t x;
1566
1567
    x = PyObject_Hash(obj);
1568
    if (x == -1)
  Branch (1568:9): [True: 58, False: 1.13M]
1569
        return NULL;
1570
    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
builtin_hex(PyObject *module, PyObject *number)
1588
/*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
1589
{
1590
    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
builtin_iter(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1597
{
1598
    PyObject *v;
1599
1600
    if (!_PyArg_CheckPositional("iter", nargs, 1, 2))
1601
        return NULL;
1602
    v = args[0];
1603
    if (nargs == 1)
  Branch (1603:9): [True: 211k, False: 4.58k]
1604
        return PyObject_GetIter(v);
1605
    if (!PyCallable_Check(v)) {
  Branch (1605:9): [True: 1, False: 4.57k]
1606
        PyErr_SetString(PyExc_TypeError,
1607
                        "iter(v, w): v must be callable");
1608
        return NULL;
1609
    }
1610
    PyObject *sentinel = args[1];
1611
    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
builtin_aiter(PyObject *module, PyObject *async_iterable)
1634
/*[clinic end generated code: output=1bae108d86f7960e input=473993d0cacc7d23]*/
1635
{
1636
    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
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
    t = Py_TYPE(aiterator);
1663
    if (t->tp_as_async == NULL || 
t->tp_as_async->am_anext == NULL113
) {
  Branch (1663:9): [True: 1, False: 113]
  Branch (1663:35): [True: 0, False: 113]
1664
        PyErr_Format(PyExc_TypeError,
1665
            "'%.200s' object is not an async iterator",
1666
            t->tp_name);
1667
        return NULL;
1668
    }
1669
1670
    awaitable = (*t->tp_as_async->am_anext)(aiterator);
1671
    if (default_value == NULL) {
  Branch (1671:9): [True: 82, False: 31]
1672
        return awaitable;
1673
    }
1674
1675
    PyObject* new_awaitable = PyAnextAwaitable_New(
1676
            awaitable, default_value);
1677
    Py_DECREF(awaitable);
1678
    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
builtin_len(PyObject *module, PyObject *obj)
1693
/*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
1694
{
1695
    Py_ssize_t res;
1696
1697
    res = PyObject_Size(obj);
1698
    if (res < 0) {
  Branch (1698:9): [True: 69, False: 472k]
1699
        assert(PyErr_Occurred());
1700
        return NULL;
1701
    }
1702
    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
builtin_locals_impl(PyObject *module)
1718
/*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
1719
{
1720
    PyObject *d;
1721
1722
    d = PyEval_GetLocals();
1723
    Py_XINCREF(d);
1724
    return d;
1725
}
1726
1727
1728
static PyObject *
1729
min_max(PyObject *args, PyObject *kwds, int op)
1730
{
1731
    PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
1732
    PyObject *emptytuple, *defaultval = NULL;
1733
    static char *kwlist[] = {"key", "default", NULL};
1734
    const char *name = op == Py_LT ? 
"min"962k
:
"max"585k
;
  Branch (1734:24): [True: 962k, False: 585k]
1735
    const int positional = PyTuple_Size(args) > 1;
1736
    int ret;
1737
1738
    if (positional) {
  Branch (1738:9): [True: 1.50M, False: 41.5k]
1739
        v = args;
1740
    }
1741
    else if (!PyArg_UnpackTuple(args, name, 1, 1, &v)) {
  Branch (1741:14): [True: 8, False: 41.5k]
1742
        if (PyExceptionClass_Check(PyExc_TypeError)) {
1743
            PyErr_Format(PyExc_TypeError, "%s expected at least 1 argument, got 0", name);
1744
        }
1745
        return NULL;
1746
    }
1747
1748
    emptytuple = PyTuple_New(0);
1749
    if (emptytuple == NULL)
  Branch (1749:9): [True: 0, False: 1.54M]
1750
        return NULL;
1751
    ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds,
1752
                                      (op == Py_LT) ? 
"|$OO:min"962k
:
"|$OO:max"585k
,
  Branch (1752:39): [True: 962k, False: 585k]
1753
                                      kwlist, &keyfunc, &defaultval);
1754
    Py_DECREF(emptytuple);
1755
    if (!ret)
  Branch (1755:9): [True: 5, False: 1.54M]
1756
        return NULL;
1757
1758
    if (positional && 
defaultval != NULL1.50M
) {
  Branch (1758:9): [True: 1.50M, False: 41.5k]
  Branch (1758:23): [True: 2, False: 1.50M]
1759
        PyErr_Format(PyExc_TypeError,
1760
                        "Cannot specify a default for %s() with multiple "
1761
                        "positional arguments", name);
1762
        return NULL;
1763
    }
1764
1765
    it = PyObject_GetIter(v);
1766
    if (it == NULL) {
  Branch (1766:9): [True: 4, False: 1.54M]
1767
        return NULL;
1768
    }
1769
1770
    if (keyfunc == Py_None) {
  Branch (1770:9): [True: 14, False: 1.54M]
1771
        keyfunc = NULL;
1772
    }
1773
1774
    maxitem = NULL; /* the result */
1775
    maxval = NULL;  /* the value associated with the result */
1776
    while (( item = PyIter_Next(it) )) {
  Branch (1776:12): [True: 3.12M, False: 1.54M]
1777
        /* get the value from the key function */
1778
        if (keyfunc != NULL) {
  Branch (1778:13): [True: 13.5k, False: 3.10M]
1779
            val = PyObject_CallOneArg(keyfunc, item);
1780
            if (val == NULL)
  Branch (1780:17): [True: 2, False: 13.5k]
1781
                goto Fail_it_item;
1782
        }
1783
        /* no key function; the value is the item */
1784
        else {
1785
            val = item;
1786
            Py_INCREF(val);
1787
        }
1788
1789
        /* maximum value and item are unset; set them */
1790
        if (maxval == NULL) {
  Branch (1790:13): [True: 1.54M, False: 1.57M]
1791
            maxitem = item;
1792
            maxval = val;
1793
        }
1794
        /* maximum value and item are set; update them as necessary */
1795
        else {
1796
            int cmp = PyObject_RichCompareBool(val, maxval, op);
1797
            if (cmp < 0)
  Branch (1797:17): [True: 0, False: 1.57M]
1798
                goto Fail_it_item_and_val;
1799
            else if (cmp > 0) {
  Branch (1799:22): [True: 523k, False: 1.05M]
1800
                Py_DECREF(maxval);
1801
                Py_DECREF(maxitem);
1802
                maxval = val;
1803
                maxitem = item;
1804
            }
1805
            else {
1806
                Py_DECREF(item);
1807
                Py_DECREF(val);
1808
            }
1809
        }
1810
    }
1811
    if (PyErr_Occurred())
  Branch (1811:9): [True: 2, False: 1.54M]
1812
        goto Fail_it;
1813
    if (maxval == NULL) {
  Branch (1813:9): [True: 362, False: 1.54M]
1814
        assert(maxitem == NULL);
1815
        if (defaultval != NULL) {
  Branch (1815:13): [True: 12, False: 350]
1816
            Py_INCREF(defaultval);
1817
            maxitem = defaultval;
1818
        } else {
1819
            PyErr_Format(PyExc_ValueError,
1820
                         "%s() arg is an empty sequence", name);
1821
        }
1822
    }
1823
    else
1824
        Py_DECREF(maxval);
1825
    Py_DECREF(it);
1826
    return maxitem;
1827
1828
Fail_it_item_and_val:
1829
    Py_DECREF(val);
1830
Fail_it_item:
1831
    Py_DECREF(item);
1832
Fail_it:
1833
    Py_XDECREF(maxval);
1834
    Py_XDECREF(maxitem);
1835
    Py_DECREF(it);
1836
    return NULL;
1837
}
1838
1839
/* AC: cannot convert yet, waiting for *args support */
1840
static PyObject *
1841
builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
1842
{
1843
    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
builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
1859
{
1860
    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
builtin_oct(PyObject *module, PyObject *number)
1887
/*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
1888
{
1889
    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
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
    if (PyBytes_Check(c)) {
1910
        size = PyBytes_GET_SIZE(c);
1911
        if (size == 1) {
  Branch (1911:13): [True: 4.30k, False: 0]
1912
            ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
1913
            return PyLong_FromLong(ord);
1914
        }
1915
    }
1916
    else if (PyUnicode_Check(c)) {
1917
        if (PyUnicode_READY(c) == -1)
  Branch (1917:13): [True: 0, False: 1.64M]
1918
            return NULL;
1919
        size = PyUnicode_GET_LENGTH(c);
1920
        if (size == 1) {
  Branch (1920:13): [True: 1.64M, False: 2]
1921
            ord = (long)PyUnicode_READ_CHAR(c, 0);
1922
            return PyLong_FromLong(ord);
1923
        }
1924
    }
1925
    else if (PyByteArray_Check(c)) {
1926
        /* XXX Hopefully this is temporary */
1927
        size = PyByteArray_GET_SIZE(c);
1928
        if (size == 1) {
  Branch (1928:13): [True: 5, False: 0]
1929
            ord = (long)((unsigned char)*PyByteArray_AS_STRING(c));
1930
            return PyLong_FromLong(ord);
1931
        }
1932
    }
1933
    else {
1934
        PyErr_Format(PyExc_TypeError,
1935
                     "ord() expected string of length 1, but " \
1936
                     "%.200s found", Py_TYPE(c)->tp_name);
1937
        return NULL;
1938
    }
1939
1940
    PyErr_Format(PyExc_TypeError,
1941
                 "ord() expected a character, "
1942
                 "but string of length %zd found",
1943
                 size);
1944
    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
builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp,
1963
                 PyObject *mod)
1964
/*[clinic end generated code: output=3ca1538221bbf15f input=435dbd48a12efb23]*/
1965
{
1966
    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
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
    if (file == Py_None) {
  Branch (1993:9): [True: 20.9k, False: 39.6k]
1994
        PyThreadState *tstate = _PyThreadState_GET();
1995
        file = _PySys_GetAttr(tstate, &_Py_ID(stdout));
1996
        if (file == NULL) {
  Branch (1996:13): [True: 0, False: 20.9k]
1997
            PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1998
            return NULL;
1999
        }
2000
2001
        /* sys.stdout may be None when FILE* stdout isn't connected */
2002
        if (file == Py_None) {
  Branch (2002:13): [True: 0, False: 20.9k]
2003
            Py_RETURN_NONE;
2004
        }
2005
    }
2006
2007
    if (sep == Py_None) {
  Branch (2007:9): [True: 60.5k, False: 74]
2008
        sep = NULL;
2009
    }
2010
    else if (sep && !PyUnicode_Check(sep)) {
  Branch (2010:14): [True: 74, False: 0]
  Branch (2010:21): [True: 1, False: 73]
2011
        PyErr_Format(PyExc_TypeError,
2012
                     "sep must be None or a string, not %.200s",
2013
                     Py_TYPE(sep)->tp_name);
2014
        return NULL;
2015
    }
2016
    if (end == Py_None) {
  Branch (2016:9): [True: 43.9k, False: 16.7k]
2017
        end = NULL;
2018
    }
2019
    else if (end && !PyUnicode_Check(end)) {
  Branch (2019:14): [True: 16.7k, False: 0]
  Branch (2019:21): [True: 1, False: 16.7k]
2020
        PyErr_Format(PyExc_TypeError,
2021
                     "end must be None or a string, not %.200s",
2022
                     Py_TYPE(end)->tp_name);
2023
        return NULL;
2024
    }
2025
2026
    
for (i = 0; 60.6k
i < PyTuple_GET_SIZE(args);
i++59.4k
) {
  Branch (2026:17): [True: 59.4k, False: 60.6k]
2027
        if (i > 0) {
  Branch (2027:13): [True: 1.90k, False: 57.5k]
2028
            if (sep == NULL) {
  Branch (2028:17): [True: 1.83k, False: 77]
2029
                err = PyFile_WriteString(" ", file);
2030
            }
2031
            else {
2032
                err = PyFile_WriteObject(sep, file, Py_PRINT_RAW);
2033
            }
2034
            if (err) {
  Branch (2034:17): [True: 0, False: 1.90k]
2035
                return NULL;
2036
            }
2037
        }
2038
        err = PyFile_WriteObject(PyTuple_GET_ITEM(args, i), file, Py_PRINT_RAW);
2039
        if (err) {
  Branch (2039:13): [True: 3, False: 59.4k]
2040
            return NULL;
2041
        }
2042
    }
2043
2044
    if (end == NULL) {
  Branch (2044:9): [True: 43.9k, False: 16.7k]
2045
        err = PyFile_WriteString("\n", file);
2046
    }
2047
    else {
2048
        err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
2049
    }
2050
    if (err) {
  Branch (2050:9): [True: 0, False: 60.6k]
2051
        return NULL;
2052
    }
2053
2054
    if (flush) {
  Branch (2054:9): [True: 463, False: 60.1k]
2055
        PyObject *tmp = PyObject_CallMethodNoArgs(file, &_Py_ID(flush));
2056
        if (tmp == NULL) {
  Branch (2056:13): [True: 1, False: 462]
2057
            return NULL;
2058
        }
2059
        Py_DECREF(tmp);
2060
    }
2061
2062
    
Py_RETURN_NONE60.6k
;
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
builtin_input_impl(PyObject *module, PyObject *prompt)
2083
/*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
2084
{
2085
    PyThreadState *tstate = _PyThreadState_GET();
2086
    PyObject *fin = _PySys_GetAttr(
2087
        tstate, &_Py_ID(stdin));
2088
    PyObject *fout = _PySys_GetAttr(
2089
        tstate, &_Py_ID(stdout));
2090
    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
    if (fin == NULL || 
fin == 239
Py_None239
) {
  Branch (2097:9): [True: 1, False: 239]
  Branch (2097:24): [True: 0, False: 239]
2098
        PyErr_SetString(PyExc_RuntimeError,
2099
                        "input(): lost sys.stdin");
2100
        return NULL;
2101
    }
2102
    if (fout == NULL || 
fout == 238
Py_None238
) {
  Branch (2102:9): [True: 1, False: 238]
  Branch (2102:25): [True: 0, False: 238]
2103
        PyErr_SetString(PyExc_RuntimeError,
2104
                        "input(): lost sys.stdout");
2105
        return NULL;
2106
    }
2107
    if (ferr == NULL || ferr == Py_None) {
  Branch (2107:9): [True: 0, False: 238]
  Branch (2107:25): [True: 0, False: 238]
2108
        PyErr_SetString(PyExc_RuntimeError,
2109
                        "input(): lost sys.stderr");
2110
        return NULL;
2111
    }
2112
2113
    if (PySys_Audit("builtins.input", "O", prompt ? 
prompt232
:
Py_None6
) < 0) {
  Branch (2113:9): [True: 0, False: 238]
  Branch (2113:44): [True: 232, False: 6]
2114
        return NULL;
2115
    }
2116
2117
    /* First of all, flush stderr */
2118
    tmp = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
2119
    if (tmp == NULL)
  Branch (2119:9): [True: 0, False: 238]
2120
        PyErr_Clear();
2121
    else
2122
        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
    tmp = PyObject_CallMethodNoArgs(fin, &_Py_ID(fileno));
2128
    if (tmp == NULL) {
  Branch (2128:9): [True: 235, False: 3]
2129
        PyErr_Clear();
2130
        tty = 0;
2131
    }
2132
    else {
2133
        fd = PyLong_AsLong(tmp);
2134
        Py_DECREF(tmp);
2135
        if (fd < 0 && 
PyErr_Occurred()0
)
  Branch (2135:13): [True: 0, False: 3]
  Branch (2135:23): [True: 0, False: 0]
2136
            return NULL;
2137
        tty = fd == fileno(stdin) && 
isatty(fd)0
;
  Branch (2137:15): [True: 0, False: 3]
  Branch (2137:38): [True: 0, False: 0]
2138
    }
2139
    if (tty) {
  Branch (2139:9): [True: 0, False: 238]
2140
        tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(fileno));
2141
        if (tmp == NULL) {
  Branch (2141:13): [True: 0, False: 0]
2142
            PyErr_Clear();
2143
            tty = 0;
2144
        }
2145
        else {
2146
            fd = PyLong_AsLong(tmp);
2147
            Py_DECREF(tmp);
2148
            if (fd < 0 && PyErr_Occurred())
  Branch (2148:17): [True: 0, False: 0]
  Branch (2148:27): [True: 0, False: 0]
2149
                return NULL;
2150
            tty = fd == fileno(stdout) && isatty(fd);
  Branch (2150:19): [True: 0, False: 0]
  Branch (2150:43): [True: 0, False: 0]
2151
        }
2152
    }
2153
2154
    /* If we're interactive, use (GNU) readline */
2155
    if (tty) {
  Branch (2155:9): [True: 0, False: 238]
2156
        PyObject *po = NULL;
2157
        const char *promptstr;
2158
        char *s = NULL;
2159
        PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
2160
        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
        stdin_encoding = PyObject_GetAttr(fin, &_Py_ID(encoding));
2167
        stdin_errors = PyObject_GetAttr(fin, &_Py_ID(errors));
2168
        if (!stdin_encoding || !stdin_errors ||
  Branch (2168:13): [True: 0, False: 0]
  Branch (2168:32): [True: 0, False: 0]
2169
                !PyUnicode_Check(stdin_encoding) ||
  Branch (2169:17): [True: 0, False: 0]
2170
                !PyUnicode_Check(stdin_errors)) {
  Branch (2170:17): [True: 0, False: 0]
2171
            tty = 0;
2172
            goto _readline_errors;
2173
        }
2174
        stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2175
        stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
2176
        if (!stdin_encoding_str || !stdin_errors_str)
  Branch (2176:13): [True: 0, False: 0]
  Branch (2176:36): [True: 0, False: 0]
2177
            goto _readline_errors;
2178
        tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
2179
        if (tmp == NULL)
  Branch (2179:13): [True: 0, False: 0]
2180
            PyErr_Clear();
2181
        else
2182
            Py_DECREF(tmp);
2183
        if (prompt != NULL) {
  Branch (2183:13): [True: 0, False: 0]
2184
            /* We have a prompt, encode it as stdout would */
2185
            const char *stdout_encoding_str, *stdout_errors_str;
2186
            PyObject *stringpo;
2187
            stdout_encoding = PyObject_GetAttr(fout, &_Py_ID(encoding));
2188
            stdout_errors = PyObject_GetAttr(fout, &_Py_ID(errors));
2189
            if (!stdout_encoding || !stdout_errors ||
  Branch (2189:17): [True: 0, False: 0]
  Branch (2189:37): [True: 0, False: 0]
2190
                    !PyUnicode_Check(stdout_encoding) ||
  Branch (2190:21): [True: 0, False: 0]
2191
                    !PyUnicode_Check(stdout_errors)) {
  Branch (2191:21): [True: 0, False: 0]
2192
                tty = 0;
2193
                goto _readline_errors;
2194
            }
2195
            stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2196
            stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
2197
            if (!stdout_encoding_str || !stdout_errors_str)
  Branch (2197:17): [True: 0, False: 0]
  Branch (2197:41): [True: 0, False: 0]
2198
                goto _readline_errors;
2199
            stringpo = PyObject_Str(prompt);
2200
            if (stringpo == NULL)
  Branch (2200:17): [True: 0, False: 0]
2201
                goto _readline_errors;
2202
            po = PyUnicode_AsEncodedString(stringpo,
2203
                stdout_encoding_str, stdout_errors_str);
2204
            Py_CLEAR(stdout_encoding);
2205
            Py_CLEAR(stdout_errors);
2206
            Py_CLEAR(stringpo);
2207
            if (po == NULL)
  Branch (2207:17): [True: 0, False: 0]
2208
                goto _readline_errors;
2209
            assert(PyBytes_Check(po));
2210
            promptstr = PyBytes_AS_STRING(po);
2211
        }
2212
        else {
2213
            po = NULL;
2214
            promptstr = "";
2215
        }
2216
        s = PyOS_Readline(stdin, stdout, promptstr);
2217
        if (s == NULL) {
  Branch (2217:13): [True: 0, False: 0]
2218
            PyErr_CheckSignals();
2219
            if (!PyErr_Occurred())
  Branch (2219:17): [True: 0, False: 0]
2220
                PyErr_SetNone(PyExc_KeyboardInterrupt);
2221
            goto _readline_errors;
2222
        }
2223
2224
        len = strlen(s);
2225
        if (len == 0) {
  Branch (2225:13): [True: 0, False: 0]
2226
            PyErr_SetNone(PyExc_EOFError);
2227
            result = NULL;
2228
        }
2229
        else {
2230
            if (len > PY_SSIZE_T_MAX) {
  Branch (2230:17): [True: 0, False: 0]
2231
                PyErr_SetString(PyExc_OverflowError,
2232
                                "input: input too long");
2233
                result = NULL;
2234
            }
2235
            else {
2236
                len--;   /* strip trailing '\n' */
2237
                if (len != 0 && s[len-1] == '\r')
  Branch (2237:21): [True: 0, False: 0]
  Branch (2237:33): [True: 0, False: 0]
2238
                    len--;   /* strip trailing '\r' */
2239
                result = PyUnicode_Decode(s, len, stdin_encoding_str,
2240
                                                  stdin_errors_str);
2241
            }
2242
        }
2243
        Py_DECREF(stdin_encoding);
2244
        Py_DECREF(stdin_errors);
2245
        Py_XDECREF(po);
2246
        PyMem_Free(s);
2247
2248
        if (result != NULL) {
  Branch (2248:13): [True: 0, False: 0]
2249
            if (PySys_Audit("builtins.input/result", "O", result) < 0) {
  Branch (2249:17): [True: 0, False: 0]
2250
                return NULL;
2251
            }
2252
        }
2253
2254
        return result;
2255
2256
    _readline_errors:
2257
        Py_XDECREF(stdin_encoding);
2258
        Py_XDECREF(stdout_encoding);
2259
        Py_XDECREF(stdin_errors);
2260
        Py_XDECREF(stdout_errors);
2261
        Py_XDECREF(po);
2262
        if (tty)
  Branch (2262:13): [True: 0, False: 0]
2263
            return NULL;
2264
2265
        PyErr_Clear();
2266
    }
2267
2268
    /* Fallback if we're not interactive */
2269
    if (prompt != NULL) {
  Branch (2269:9): [True: 232, False: 6]
2270
        if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
  Branch (2270:13): [True: 0, False: 232]
2271
            return NULL;
2272
    }
2273
    tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
2274
    if (tmp == NULL)
  Branch (2274:9): [True: 5, False: 233]
2275
        PyErr_Clear();
2276
    else
2277
        Py_DECREF(tmp);
2278
    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
builtin_repr(PyObject *module, PyObject *obj)
2295
/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
2296
{
2297
    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
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
    if (Py_TYPE(number)->tp_dict == NULL) {
  Branch (2319:9): [True: 0, False: 1.30M]
2320
        if (PyType_Ready(Py_TYPE(number)) < 0)
  Branch (2320:13): [True: 0, False: 0]
2321
            return NULL;
2322
    }
2323
2324
    round = _PyObject_LookupSpecial(number, &_Py_ID(__round__));
2325
    if (round == NULL) {
  Branch (2325:9): [True: 8, False: 1.30M]
2326
        if (!PyErr_Occurred())
  Branch (2326:13): [True: 7, False: 1]
2327
            PyErr_Format(PyExc_TypeError,
2328
                         "type %.100s doesn't define __round__ method",
2329
                         Py_TYPE(number)->tp_name);
2330
        return NULL;
2331
    }
2332
2333
    if (ndigits == Py_None)
  Branch (2333:9): [True: 83.1k, False: 1.21M]
2334
        result = _PyObject_CallNoArgs(round);
2335
    else
2336
        result = PyObject_CallOneArg(round, ndigits);
2337
    Py_DECREF(round);
2338
    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
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
    if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
  Branch (2378:9): [True: 3, False: 514k]
2379
        return NULL;
2380
2381
    newlist = PySequence_List(seq);
2382
    if (newlist == NULL)
  Branch (2382:9): [True: 16, False: 514k]
2383
        return NULL;
2384
2385
    callable = PyObject_GetAttr(newlist, &_Py_ID(sort));
2386
    if (callable == NULL) {
  Branch (2386:9): [True: 0, False: 514k]
2387
        Py_DECREF(newlist);
2388
        return NULL;
2389
    }
2390
2391
    assert(nargs >= 1);
2392
    v = PyObject_Vectorcall(callable, args + 1, nargs - 1, kwnames);
2393
    Py_DECREF(callable);
2394
    if (v == NULL) {
  Branch (2394:9): [True: 8, False: 514k]
2395
        Py_DECREF(newlist);
2396
        return NULL;
2397
    }
2398
    Py_DECREF(v);
2399
    return newlist;
2400
}
2401
2402
2403
/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
2404
static PyObject *
2405
builtin_vars(PyObject *self, PyObject *args)
2406
{
2407
    PyObject *v = NULL;
2408
    PyObject *d;
2409
2410
    if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
  Branch (2410:9): [True: 1, False: 9.23k]
2411
        return NULL;
2412
    if (v == NULL) {
  Branch (2412:9): [True: 22, False: 9.21k]
2413
        d = PyEval_GetLocals();
2414
        Py_XINCREF(d);
2415
    }
2416
    else {
2417
        if (_PyObject_LookupAttr(v, &_Py_ID(__dict__), &d) == 0) {
  Branch (2417:13): [True: 3, False: 9.20k]
2418
            PyErr_SetString(PyExc_TypeError,
2419
                "vars() argument must have __dict__ attribute");
2420
        }
2421
    }
2422
    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
builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
2448
/*[clinic end generated code: output=df758cec7d1d302f input=162b50765250d222]*/
2449
{
2450
    PyObject *result = start;
2451
    PyObject *temp, *item, *iter;
2452
2453
    iter = PyObject_GetIter(iterable);
2454
    if (iter == NULL)
  Branch (2454:9): [True: 2, False: 96.6k]
2455
        return NULL;
2456
2457
    if (result == NULL) {
  Branch (2457:9): [True: 38.5k, False: 58.0k]
2458
        result = PyLong_FromLong(0);
2459
        if (result == NULL) {
  Branch (2459:13): [True: 0, False: 38.5k]
2460
            Py_DECREF(iter);
2461
            return NULL;
2462
        }
2463
    } else {
2464
        /* reject string values for 'start' parameter */
2465
        if (PyUnicode_Check(result)) {
2466
            PyErr_SetString(PyExc_TypeError,
2467
                "sum() can't sum strings [use ''.join(seq) instead]");
2468
            Py_DECREF(iter);
2469
            return NULL;
2470
        }
2471
        if (PyBytes_Check(result)) {
2472
            PyErr_SetString(PyExc_TypeError,
2473
                "sum() can't sum bytes [use b''.join(seq) instead]");
2474
            Py_DECREF(iter);
2475
            return NULL;
2476
        }
2477
        if (PyByteArray_Check(result)) {
2478
            PyErr_SetString(PyExc_TypeError,
2479
                "sum() can't sum bytearray [use b''.join(seq) instead]");
2480
            Py_DECREF(iter);
2481
            return NULL;
2482
        }
2483
        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
    if (PyLong_CheckExact(result)) {
2492
        int overflow;
2493
        long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2494
        /* If this already overflowed, don't even enter the loop. */
2495
        if (overflow == 0) {
  Branch (2495:13): [True: 54.1k, False: 0]
2496
            Py_DECREF(result);
2497
            result = NULL;
2498
        }
2499
        while(result == NULL) {
  Branch (2499:15): [True: 12.2M, False: 694]
2500
            item = PyIter_Next(iter);
2501
            if (item == NULL) {
  Branch (2501:17): [True: 53.4k, False: 12.1M]
2502
                Py_DECREF(iter);
2503
                if (PyErr_Occurred())
  Branch (2503:21): [True: 1, False: 53.4k]
2504
                    return NULL;
2505
                return PyLong_FromLong(i_result);
2506
            }
2507
            if (PyLong_CheckExact(item) || 
PyBool_Check215k
(item)) {
2508
                long b;
2509
                overflow = 0;
2510
                /* Single digits are common, fast, and cannot overflow on unpacking. */
2511
                switch (Py_SIZE(item)) {
2512
                    case -1: b = -(sdigit) ((PyLongObject*)item)->ob_digit[0]; break;
  Branch (2512:21): [True: 10.4k, False: 12.1M]
2513
                    // Note: the continue goes to the top of the "while" loop that iterates over the elements
2514
                    case  0: Py_DECREF(item); continue;
  Branch (2514:21): [True: 9.47M, False: 2.70M]
2515
                    case  1: b = ((PyLongObject*)item)->ob_digit[0]; break;
  Branch (2515:21): [True: 2.69M, False: 9.48M]
2516
                    default: b = PyLong_AsLongAndOverflow(item, &overflow); break;
  Branch (2516:21): [True: 0, False: 12.1M]
2517
                }
2518
                if (overflow == 0 &&
  Branch (2518:21): [True: 2.70M, False: 0]
2519
                    (i_result >= 0 ? 
(b <= LONG_MAX - i_result)2.70M
  Branch (2519:21): [True: 2.70M, False: 2]
  Branch (2519:22): [True: 2.70M, False: 321]
2520
                                   : 
(b >= LONG_MIN - i_result)321
))
2521
                {
2522
                    i_result += b;
2523
                    Py_DECREF(item);
2524
                    continue;
2525
                }
2526
            }
2527
            /* Either overflowed or is not an int. Restore real objects and process normally */
2528
            result = PyLong_FromLong(i_result);
2529
            if (result == NULL) {
  Branch (2529:17): [True: 0, False: 697]
2530
                Py_DECREF(item);
2531
                Py_DECREF(iter);
2532
                return NULL;
2533
            }
2534
            temp = PyNumber_Add(result, item);
2535
            Py_DECREF(result);
2536
            Py_DECREF(item);
2537
            result = temp;
2538
            if (result == NULL) {
  Branch (2538:17): [True: 3, False: 694]
2539
                Py_DECREF(iter);
2540
                return NULL;
2541
            }
2542
        }
2543
    }
2544
2545
    if (PyFloat_CheckExact(result)) {
2546
        double f_result = PyFloat_AS_DOUBLE(result);
2547
        Py_DECREF(result);
2548
        result = NULL;
2549
        while(result == NULL) {
  Branch (2549:15): [True: 255, False: 0]
2550
            item = PyIter_Next(iter);
2551
            if (item == NULL) {
  Branch (2551:17): [True: 55, False: 200]
2552
                Py_DECREF(iter);
2553
                if (PyErr_Occurred())
  Branch (2553:21): [True: 0, False: 55]
2554
                    return NULL;
2555
                return PyFloat_FromDouble(f_result);
2556
            }
2557
            if (PyFloat_CheckExact(item)) {
2558
                f_result += PyFloat_AS_DOUBLE(item);
2559
                _Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc);
2560
                continue;
2561
            }
2562
            if (PyLong_Check(item)) {
2563
                long value;
2564
                int overflow;
2565
                value = PyLong_AsLongAndOverflow(item, &overflow);
2566
                if (!overflow) {
  Branch (2566:21): [True: 15, False: 0]
2567
                    f_result += (double)value;
2568
                    Py_DECREF(item);
2569
                    continue;
2570
                }
2571
            }
2572
            result = PyFloat_FromDouble(f_result);
2573
            if (result == NULL) {
  Branch (2573:17): [True: 0, False: 0]
2574
                Py_DECREF(item);
2575
                Py_DECREF(iter);
2576
                return NULL;
2577
            }
2578
            temp = PyNumber_Add(result, item);
2579
            Py_DECREF(result);
2580
            Py_DECREF(item);
2581
            result = temp;
2582
            if (result == NULL) {
  Branch (2582:17): [True: 0, False: 0]
2583
                Py_DECREF(iter);
2584
                return NULL;
2585
            }
2586
        }
2587
    }
2588
#endif
2589
2590
    
for(;;)43.0k
{
2591
        item = PyIter_Next(iter);
2592
        if (item == NULL) {
  Branch (2592:13): [True: 43.0k, False: 74.4k]
2593
            /* error, or end-of-sequence */
2594
            if (PyErr_Occurred()) {
  Branch (2594:17): [True: 0, False: 43.0k]
2595
                Py_DECREF(result);
2596
                result = NULL;
2597
            }
2598
            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
        temp = PyNumber_Add(result, item);
2614
        Py_DECREF(result);
2615
        Py_DECREF(item);
2616
        result = temp;
2617
        if (result == NULL)
  Branch (2617:13): [True: 1, False: 74.4k]
2618
            break;
2619
    }
2620
    Py_DECREF(iter);
2621
    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
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
    retval = PyObject_IsInstance(obj, class_or_tuple);
2647
    if (retval < 0)
  Branch (2647:9): [True: 44, False: 351k]
2648
        return NULL;
2649
    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
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
    retval = PyObject_IsSubclass(cls, class_or_tuple);
2675
    if (retval < 0)
  Branch (2675:9): [True: 168, False: 771k]
2676
        return NULL;
2677
    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
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
    int strict = 0;
2697
2698
    if (kwds) {
  Branch (2698:9): [True: 42.4k, False: 164k]
2699
        PyObject *empty = PyTuple_New(0);
2700
        if (empty == NULL) {
  Branch (2700:13): [True: 0, False: 42.4k]
2701
            return NULL;
2702
        }
2703
        static char *kwlist[] = {"strict", NULL};
2704
        int parsed = PyArg_ParseTupleAndKeywords(
2705
                empty, kwds, "|$p:zip", kwlist, &strict);
2706
        Py_DECREF(empty);
2707
        if (!parsed) {
  Branch (2707:13): [True: 1, False: 42.4k]
2708
            return NULL;
2709
        }
2710
    }
2711
2712
    /* args must be a tuple */
2713
    assert(PyTuple_Check(args));
2714
    tuplesize = PyTuple_GET_SIZE(args);
2715
2716
    /* obtain iterators */
2717
    ittuple = PyTuple_New(tuplesize);
2718
    if (ittuple == NULL)
  Branch (2718:9): [True: 0, False: 206k]
2719
        return NULL;
2720
    
for (i=0; 206k
i < tuplesize;
++i415k
) {
  Branch (2720:15): [True: 415k, False: 206k]
2721
        PyObject *item = PyTuple_GET_ITEM(args, i);
2722
        PyObject *it = PyObject_GetIter(item);
2723
        if (it == NULL) {
  Branch (2723:13): [True: 19, False: 415k]
2724
            Py_DECREF(ittuple);
2725
            return NULL;
2726
        }
2727
        PyTuple_SET_ITEM(ittuple, i, it);
2728
    }
2729
2730
    /* create a result holder */
2731
    result = PyTuple_New(tuplesize);
2732
    if (result == NULL) {
  Branch (2732:9): [True: 0, False: 206k]
2733
        Py_DECREF(ittuple);
2734
        return NULL;
2735
    }
2736
    
for (i=0 ; 206k
i < tuplesize ;
i++415k
) {
  Branch (2736:16): [True: 415k, False: 206k]
2737
        Py_INCREF(Py_None);
2738
        PyTuple_SET_ITEM(result, i, Py_None);
2739
    }
2740
2741
    /* create zipobject structure */
2742
    lz = (zipobject *)type->tp_alloc(type, 0);
2743
    if (lz == NULL) {
  Branch (2743:9): [True: 0, False: 206k]
2744
        Py_DECREF(ittuple);
2745
        Py_DECREF(result);
2746
        return NULL;
2747
    }
2748
    lz->ittuple = ittuple;
2749
    lz->tuplesize = tuplesize;
2750
    lz->result = result;
2751
    lz->strict = strict;
2752
2753
    return (PyObject *)lz;
2754
}
2755
2756
static void
2757
zip_dealloc(zipobject *lz)
2758
{
2759
    PyObject_GC_UnTrack(lz);
2760
    Py_XDECREF(lz->ittuple);
2761
    Py_XDECREF(lz->result);
2762
    Py_TYPE(lz)->tp_free(lz);
2763
}
2764
2765
static int
2766
zip_traverse(zipobject *lz, visitproc visit, void *arg)
2767
{
2768
    Py_VISIT(lz->ittuple);
2769
    Py_VISIT(lz->result);
2770
    return 0;
2771
}
2772
2773
static PyObject *
2774
zip_next(zipobject *lz)
2775
{
2776
    Py_ssize_t i;
2777
    Py_ssize_t tuplesize = lz->tuplesize;
2778
    PyObject *result = lz->result;
2779
    PyObject *it;
2780
    PyObject *item;
2781
    PyObject *olditem;
2782
2783
    if (tuplesize == 0)
  Branch (2783:9): [True: 8, False: 1.00M]
2784
        return NULL;
2785
    if (Py_REFCNT(result) == 1) {
  Branch (2785:9): [True: 920k, False: 81.5k]
2786
        Py_INCREF(result);
2787
        for (i=0 ; i < tuplesize ; 
i++1.44M
) {
  Branch (2787:20): [True: 1.64M, False: 721k]
2788
            it = PyTuple_GET_ITEM(lz->ittuple, i);
2789
            item = (*Py_TYPE(it)->tp_iternext)(it);
2790
            if (item == NULL) {
  Branch (2790:17): [True: 198k, False: 1.44M]
2791
                Py_DECREF(result);
2792
                if (lz->strict) {
  Branch (2792:21): [True: 42.3k, False: 156k]
2793
                    goto check;
2794
                }
2795
                return NULL;
2796
            }
2797
            olditem = PyTuple_GET_ITEM(result, i);
2798
            PyTuple_SET_ITEM(result, i, item);
2799
            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
        if (!_PyObject_GC_IS_TRACKED(result)) {
  Branch (2803:13): [True: 33, False: 721k]
2804
            _PyObject_GC_TRACK(result);
2805
        }
2806
    } else {
2807
        result = PyTuple_New(tuplesize);
2808
        if (result == NULL)
  Branch (2808:13): [True: 0, False: 81.5k]
2809
            return NULL;
2810
        
for (i=0 ; 81.5k
i < tuplesize ;
i++137k
) {
  Branch (2810:20): [True: 144k, False: 74.4k]
2811
            it = PyTuple_GET_ITEM(lz->ittuple, i);
2812
            item = (*Py_TYPE(it)->tp_iternext)(it);
2813
            if (item == NULL) {
  Branch (2813:17): [True: 7.04k, False: 137k]
2814
                Py_DECREF(result);
2815
                if (lz->strict) {
  Branch (2815:21): [True: 59, False: 6.98k]
2816
                    goto check;
2817
                }
2818
                return NULL;
2819
            }
2820
            PyTuple_SET_ITEM(result, i, item);
2821
        }
2822
    }
2823
    return result;
2824
check:
2825
    if (PyErr_Occurred()) {
  Branch (2825:9): [True: 6, False: 42.4k]
2826
        if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
  Branch (2826:13): [True: 3, False: 3]
2827
            // next() on argument i raised an exception (not StopIteration)
2828
            return NULL;
2829
        }
2830
        PyErr_Clear();
2831
    }
2832
    if (i) {
  Branch (2832:9): [True: 9, False: 42.4k]
2833
        // ValueError: zip() argument 2 is shorter than argument 1
2834
        // ValueError: zip() argument 3 is shorter than arguments 1-2
2835
        const char* plural = i == 1 ? 
" "7
:
"s 1-"2
;
  Branch (2835:30): [True: 7, False: 2]
2836
        return PyErr_Format(PyExc_ValueError,
2837
                            "zip() argument %d is shorter than argument%s%d",
2838
                            i + 1, plural, i);
2839
    }
2840
    
for (i = 1; 42.4k
i < tuplesize;
i++42.4k
) {
  Branch (2840:17): [True: 42.4k, False: 42.3k]
2841
        it = PyTuple_GET_ITEM(lz->ittuple, i);
2842
        item = (*Py_TYPE(it)->tp_iternext)(it);
2843
        if (item) {
  Branch (2843:13): [True: 19, False: 42.4k]
2844
            Py_DECREF(item);
2845
            const char* plural = i == 1 ? 
" "17
:
"s 1-"2
;
  Branch (2845:34): [True: 17, False: 2]
2846
            return PyErr_Format(PyExc_ValueError,
2847
                                "zip() argument %d is longer than argument%s%d",
2848
                                i + 1, plural, i);
2849
        }
2850
        if (PyErr_Occurred()) {
  Branch (2850:13): [True: 2, False: 42.4k]
2851
            if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
  Branch (2851:17): [True: 1, False: 1]
2852
                // next() on argument i raised an exception (not StopIteration)
2853
                return NULL;
2854
            }
2855
            PyErr_Clear();
2856
        }
2857
        // Argument i is exhausted. So far so good...
2858
    }
2859
    // All arguments are exhausted. Success!
2860
    return NULL;
2861
}
2862
2863
static PyObject *
2864
zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
2865
{
2866
    /* Just recreate the zip with the internal iterator tuple */
2867
    if (lz->strict) {
  Branch (2867:9): [True: 18, False: 51]
2868
        return PyTuple_Pack(3, Py_TYPE(lz), lz->ittuple, Py_True);
2869
    }
2870
    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
zip_setstate(zipobject *lz, PyObject *state)
2877
{
2878
    int strict = PyObject_IsTrue(state);
2879
    if (strict < 0) {
  Branch (2879:9): [True: 0, False: 24]
2880
        return NULL;
2881
    }
2882
    lz->strict = strict;
2883
    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
_PyBuiltin_Init(PyInterpreterState *interp)
3020
{
3021
    PyObject *mod, *dict, *debug;
3022
3023
    const PyConfig *config = _PyInterpreterState_GetConfig(interp);
3024
3025
    mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
3026
    if (mod == NULL)
  Branch (3026:9): [True: 0, False: 278]
3027
        return NULL;
3028
    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 NULL0
; \
3045
    ADD_TO_ALL(OBJECT)
3046
3047
    SETBUILTIN("None",                  Py_None);
3048
    SETBUILTIN("Ellipsis",              Py_Ellipsis);
3049
    SETBUILTIN("NotImplemented",        Py_NotImplemented);
3050
    SETBUILTIN("False",                 Py_False);
3051
    SETBUILTIN("True",                  Py_True);
3052
    SETBUILTIN("bool",                  &PyBool_Type);
3053
    SETBUILTIN("memoryview",        &PyMemoryView_Type);
3054
    SETBUILTIN("bytearray",             &PyByteArray_Type);
3055
    SETBUILTIN("bytes",                 &PyBytes_Type);
3056
    SETBUILTIN("classmethod",           &PyClassMethod_Type);
3057
    SETBUILTIN("complex",               &PyComplex_Type);
3058
    SETBUILTIN("dict",                  &PyDict_Type);
3059
    SETBUILTIN("enumerate",             &PyEnum_Type);
3060
    SETBUILTIN("filter",                &PyFilter_Type);
3061
    SETBUILTIN("float",                 &PyFloat_Type);
3062
    SETBUILTIN("frozenset",             &PyFrozenSet_Type);
3063
    SETBUILTIN("property",              &PyProperty_Type);
3064
    SETBUILTIN("int",                   &PyLong_Type);
3065
    SETBUILTIN("list",                  &PyList_Type);
3066
    SETBUILTIN("map",                   &PyMap_Type);
3067
    SETBUILTIN("object",                &PyBaseObject_Type);
3068
    SETBUILTIN("range",                 &PyRange_Type);
3069
    SETBUILTIN("reversed",              &PyReversed_Type);
3070
    SETBUILTIN("set",                   &PySet_Type);
3071
    SETBUILTIN("slice",                 &PySlice_Type);
3072
    SETBUILTIN("staticmethod",          &PyStaticMethod_Type);
3073
    SETBUILTIN("str",                   &PyUnicode_Type);
3074
    SETBUILTIN("super",                 &PySuper_Type);
3075
    SETBUILTIN("tuple",                 &PyTuple_Type);
3076
    SETBUILTIN("type",                  &PyType_Type);
3077
    SETBUILTIN("zip",                   &PyZip_Type);
3078
    debug = PyBool_FromLong(config->optimization_level == 0);
3079
    if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
  Branch (3079:9): [True: 0, False: 278]
3080
        Py_DECREF(debug);
3081
        return NULL;
3082
    }
3083
    Py_DECREF(debug);
3084
3085
    return mod;
3086
#undef ADD_TO_ALL
3087
#undef SETBUILTIN
3088
}