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