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