blob: abf807a408f781fa1c6b6cd71af8134742c5fcb5 [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum6bf62da1997-04-11 20:37:35 +00004#include <ctype.h>
Victor Stinner5f2df882018-11-12 00:56:19 +01005#include "ast.h"
Victor Stinner3bb183d2018-11-22 18:38:38 +01006#undef Yield /* undefine macro conflicting with <winbase.h> */
Victor Stinner621cebe2018-11-12 16:53:38 +01007#include "pycore_pystate.h"
Sergey Fedoseevf1b9abe2019-02-26 02:37:26 +05008#include "pycore_tupleobject.h"
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009
Victor Stinnerbd303c12013-11-07 23:07:29 +010010_Py_IDENTIFIER(__builtins__);
11_Py_IDENTIFIER(__dict__);
12_Py_IDENTIFIER(__prepare__);
13_Py_IDENTIFIER(__round__);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010014_Py_IDENTIFIER(__mro_entries__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010015_Py_IDENTIFIER(encoding);
16_Py_IDENTIFIER(errors);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020017_Py_IDENTIFIER(fileno);
18_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010019_Py_IDENTIFIER(metaclass);
20_Py_IDENTIFIER(sort);
21_Py_IDENTIFIER(stdin);
22_Py_IDENTIFIER(stdout);
23_Py_IDENTIFIER(stderr);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020024
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030025#include "clinic/bltinmodule.c.h"
26
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010027static PyObject*
Victor Stinner05d68a82018-01-18 11:15:25 +010028update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010029{
Victor Stinner05d68a82018-01-18 11:15:25 +010030 Py_ssize_t i, j;
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010031 PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
32 PyObject *stack[1] = {bases};
33 assert(PyTuple_Check(bases));
34
35 for (i = 0; i < nargs; i++) {
36 base = args[i];
37 if (PyType_Check(base)) {
38 if (new_bases) {
39 /* If we already have made a replacement, then we append every normal base,
40 otherwise just skip it. */
41 if (PyList_Append(new_bases, base) < 0) {
42 goto error;
43 }
44 }
45 continue;
46 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +020047 if (_PyObject_LookupAttrId(base, &PyId___mro_entries__, &meth) < 0) {
48 goto error;
49 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010050 if (!meth) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010051 if (new_bases) {
52 if (PyList_Append(new_bases, base) < 0) {
53 goto error;
54 }
55 }
56 continue;
57 }
58 new_base = _PyObject_FastCall(meth, stack, 1);
59 Py_DECREF(meth);
60 if (!new_base) {
61 goto error;
62 }
63 if (!PyTuple_Check(new_base)) {
64 PyErr_SetString(PyExc_TypeError,
65 "__mro_entries__ must return a tuple");
66 Py_DECREF(new_base);
67 goto error;
68 }
69 if (!new_bases) {
70 /* If this is a first successful replacement, create new_bases list and
71 copy previously encountered bases. */
72 if (!(new_bases = PyList_New(i))) {
73 goto error;
74 }
75 for (j = 0; j < i; j++) {
76 base = args[j];
77 PyList_SET_ITEM(new_bases, j, base);
78 Py_INCREF(base);
79 }
80 }
81 j = PyList_GET_SIZE(new_bases);
82 if (PyList_SetSlice(new_bases, j, j, new_base) < 0) {
83 goto error;
84 }
85 Py_DECREF(new_base);
86 }
87 if (!new_bases) {
88 return bases;
89 }
90 result = PyList_AsTuple(new_bases);
91 Py_DECREF(new_bases);
92 return result;
93
94error:
95 Py_XDECREF(new_bases);
96 return NULL;
97}
98
Nick Coghlanf9e227e2014-08-17 14:01:19 +100099/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000100static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200101builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
Victor Stinner773dc6d2017-01-16 23:46:26 +0100102 PyObject *kwnames)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000103{
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100104 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *orig_bases;
Nick Coghlan19d24672016-12-05 16:47:55 +1000105 PyObject *cls = NULL, *cell = NULL;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100106 int isclass = 0; /* initialize to prevent gcc warning */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (nargs < 2) {
109 PyErr_SetString(PyExc_TypeError,
110 "__build_class__: not enough arguments");
111 return NULL;
112 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100113 func = args[0]; /* Better be callable */
Benjamin Petersone8e14592013-05-16 14:37:25 -0500114 if (!PyFunction_Check(func)) {
115 PyErr_SetString(PyExc_TypeError,
Zachary Ware9b338722014-08-05 14:01:10 -0500116 "__build_class__: func must be a function");
Benjamin Petersone8e14592013-05-16 14:37:25 -0500117 return NULL;
118 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100119 name = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 if (!PyUnicode_Check(name)) {
121 PyErr_SetString(PyExc_TypeError,
122 "__build_class__: name is not a string");
123 return NULL;
124 }
Sergey Fedoseevf1b9abe2019-02-26 02:37:26 +0500125 orig_bases = _PyTuple_FromArray(args + 2, nargs - 2);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100126 if (orig_bases == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000128
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100129 bases = update_bases(orig_bases, args + 2, nargs - 2);
130 if (bases == NULL) {
131 Py_DECREF(orig_bases);
132 return NULL;
133 }
134
Victor Stinner773dc6d2017-01-16 23:46:26 +0100135 if (kwnames == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 meta = NULL;
137 mkw = NULL;
138 }
139 else {
Victor Stinner773dc6d2017-01-16 23:46:26 +0100140 mkw = _PyStack_AsDict(args + nargs, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 if (mkw == NULL) {
142 Py_DECREF(bases);
143 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000144 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100145
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200146 meta = _PyDict_GetItemIdWithError(mkw, &PyId_metaclass);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (meta != NULL) {
148 Py_INCREF(meta);
Victor Stinnerae9f1612013-11-06 22:46:51 +0100149 if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 Py_DECREF(meta);
151 Py_DECREF(mkw);
152 Py_DECREF(bases);
153 return NULL;
154 }
Nick Coghlande31b192011-10-23 22:04:16 +1000155 /* metaclass is explicitly given, check if it's indeed a class */
156 isclass = PyType_Check(meta);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200158 else if (PyErr_Occurred()) {
159 Py_DECREF(mkw);
160 Py_DECREF(bases);
161 return NULL;
162 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 }
164 if (meta == NULL) {
Nick Coghlande31b192011-10-23 22:04:16 +1000165 /* if there are no bases, use type: */
166 if (PyTuple_GET_SIZE(bases) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 meta = (PyObject *) (&PyType_Type);
Nick Coghlande31b192011-10-23 22:04:16 +1000168 }
169 /* else get the type of the first base */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 else {
171 PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
172 meta = (PyObject *) (base0->ob_type);
173 }
174 Py_INCREF(meta);
Nick Coghlande31b192011-10-23 22:04:16 +1000175 isclass = 1; /* meta is really a class */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 }
Nick Coghlan9715d262011-10-23 22:36:42 +1000177
Nick Coghlande31b192011-10-23 22:04:16 +1000178 if (isclass) {
179 /* meta is really a class, so check for a more derived
180 metaclass, or possible metaclass conflicts: */
181 winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
182 bases);
183 if (winner == NULL) {
184 Py_DECREF(meta);
185 Py_XDECREF(mkw);
186 Py_DECREF(bases);
187 return NULL;
188 }
189 if (winner != meta) {
190 Py_DECREF(meta);
191 meta = winner;
192 Py_INCREF(meta);
193 }
194 }
195 /* else: meta is not a class, so we cannot do the metaclass
196 calculation, so we will use the explicitly given object as it is */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200197 if (_PyObject_LookupAttrId(meta, &PyId___prepare__, &prep) < 0) {
198 ns = NULL;
199 }
200 else if (prep == NULL) {
201 ns = PyDict_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
203 else {
Victor Stinner463b86a2016-08-22 23:33:13 +0200204 PyObject *pargs[2] = {name, bases};
205 ns = _PyObject_FastCallDict(prep, pargs, 2, mkw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 Py_DECREF(prep);
207 }
208 if (ns == NULL) {
209 Py_DECREF(meta);
210 Py_XDECREF(mkw);
211 Py_DECREF(bases);
212 return NULL;
213 }
Oren Milman5837d042017-09-27 17:04:37 +0300214 if (!PyMapping_Check(ns)) {
215 PyErr_Format(PyExc_TypeError,
216 "%.200s.__prepare__() must return a mapping, not %.200s",
217 isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
218 Py_TYPE(ns)->tp_name);
219 goto error;
220 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000221 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500222 NULL, 0, NULL, 0, NULL, 0, NULL,
223 PyFunction_GET_CLOSURE(func));
Nick Coghlan19d24672016-12-05 16:47:55 +1000224 if (cell != NULL) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100225 if (bases != orig_bases) {
226 if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
227 goto error;
228 }
229 }
Victor Stinnerd1c2a8e2016-08-23 01:34:35 +0200230 PyObject *margs[3] = {name, bases, ns};
231 cls = _PyObject_FastCallDict(meta, margs, 3, mkw);
Nick Coghlan19d24672016-12-05 16:47:55 +1000232 if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
233 PyObject *cell_cls = PyCell_GET(cell);
234 if (cell_cls != cls) {
Nick Coghlan19d24672016-12-05 16:47:55 +1000235 if (cell_cls == NULL) {
236 const char *msg =
237 "__class__ not set defining %.200R as %.200R. "
238 "Was __classcell__ propagated to type.__new__?";
Serhiy Storchakaf5e7b192018-05-20 08:48:12 +0300239 PyErr_Format(PyExc_RuntimeError, msg, name, cls);
Nick Coghlan19d24672016-12-05 16:47:55 +1000240 } else {
241 const char *msg =
242 "__class__ set to %.200R defining %.200R as %.200R";
243 PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
Nick Coghlan19d24672016-12-05 16:47:55 +1000244 }
Serhiy Storchakaf5e7b192018-05-20 08:48:12 +0300245 Py_DECREF(cls);
246 cls = NULL;
247 goto error;
Nick Coghlan19d24672016-12-05 16:47:55 +1000248 }
249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000251error:
252 Py_XDECREF(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 Py_DECREF(ns);
254 Py_DECREF(meta);
255 Py_XDECREF(mkw);
256 Py_DECREF(bases);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100257 if (bases != orig_bases) {
258 Py_DECREF(orig_bases);
259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 return cls;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000261}
262
263PyDoc_STRVAR(build_class_doc,
264"__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
265\n\
266Internal helper function used by the class statement.");
267
268static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
272 "level", 0};
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400273 PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -0400274 int level = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000275
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400276 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 kwlist, &name, &globals, &locals, &fromlist, &level))
278 return NULL;
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400279 return PyImport_ImportModuleLevelObject(name, globals, locals,
280 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000281}
282
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000283PyDoc_STRVAR(import_doc,
Brett Cannoncb4996a2012-08-06 16:34:44 -0400284"__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000285\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000286Import a module. Because this function is meant for use by the Python\n\
oldk461d2252018-02-02 12:20:00 +0800287interpreter and not for general use, it is better to use\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000288importlib.import_module() to programmatically import a module.\n\
289\n\
290The globals argument is only used to determine the context;\n\
291they are not modified. The locals argument is unused. The fromlist\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000292should be a list of names to emulate ``from name import ...'', or an\n\
293empty list to emulate ``import name''.\n\
294When importing a module from a package, note that __import__('A.B', ...)\n\
295returns package A when fromlist is empty, but its submodule B when\n\
oldk461d2252018-02-02 12:20:00 +0800296fromlist is not empty. The level argument is used to determine whether to\n\
297perform absolute or relative imports: 0 is absolute, while a positive number\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000299
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000300
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000301/*[clinic input]
302abs as builtin_abs
303
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300304 x: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000305 /
306
307Return the absolute value of the argument.
308[clinic start generated code]*/
309
Guido van Rossum79f25d91997-04-29 20:08:16 +0000310static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300311builtin_abs(PyObject *module, PyObject *x)
312/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000313{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000314 return PyNumber_Absolute(x);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000315}
316
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000317/*[clinic input]
318all as builtin_all
319
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300320 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000321 /
322
323Return True if bool(x) is True for all values x in the iterable.
324
325If the iterable is empty, return True.
326[clinic start generated code]*/
327
Raymond Hettinger96229b12005-03-11 06:49:40 +0000328static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300329builtin_all(PyObject *module, PyObject *iterable)
330/*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyObject *it, *item;
333 PyObject *(*iternext)(PyObject *);
334 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000335
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000336 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 if (it == NULL)
338 return NULL;
339 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 for (;;) {
342 item = iternext(it);
343 if (item == NULL)
344 break;
345 cmp = PyObject_IsTrue(item);
346 Py_DECREF(item);
347 if (cmp < 0) {
348 Py_DECREF(it);
349 return NULL;
350 }
351 if (cmp == 0) {
352 Py_DECREF(it);
353 Py_RETURN_FALSE;
354 }
355 }
356 Py_DECREF(it);
357 if (PyErr_Occurred()) {
358 if (PyErr_ExceptionMatches(PyExc_StopIteration))
359 PyErr_Clear();
360 else
361 return NULL;
362 }
363 Py_RETURN_TRUE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000364}
365
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000366/*[clinic input]
367any as builtin_any
368
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300369 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000370 /
371
372Return True if bool(x) is True for any x in the iterable.
373
374If the iterable is empty, return False.
375[clinic start generated code]*/
376
Raymond Hettinger96229b12005-03-11 06:49:40 +0000377static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300378builtin_any(PyObject *module, PyObject *iterable)
379/*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 PyObject *it, *item;
382 PyObject *(*iternext)(PyObject *);
383 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000384
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000385 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (it == NULL)
387 return NULL;
388 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 for (;;) {
391 item = iternext(it);
392 if (item == NULL)
393 break;
394 cmp = PyObject_IsTrue(item);
395 Py_DECREF(item);
396 if (cmp < 0) {
397 Py_DECREF(it);
398 return NULL;
399 }
Raymond Hettinger5098b582015-10-09 00:42:47 -0400400 if (cmp > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 Py_DECREF(it);
402 Py_RETURN_TRUE;
403 }
404 }
405 Py_DECREF(it);
406 if (PyErr_Occurred()) {
407 if (PyErr_ExceptionMatches(PyExc_StopIteration))
408 PyErr_Clear();
409 else
410 return NULL;
411 }
412 Py_RETURN_FALSE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000413}
414
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000415/*[clinic input]
416ascii as builtin_ascii
417
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300418 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000419 /
420
421Return an ASCII-only representation of an object.
422
423As repr(), return a string containing a printable representation of an
424object, but escape the non-ASCII characters in the string returned by
425repr() using \\x, \\u or \\U escapes. This generates a string similar
426to that returned by repr() in Python 2.
427[clinic start generated code]*/
428
Georg Brandl559e5d72008-06-11 18:37:52 +0000429static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300430builtin_ascii(PyObject *module, PyObject *obj)
431/*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
Georg Brandl559e5d72008-06-11 18:37:52 +0000432{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000433 return PyObject_ASCII(obj);
Georg Brandl559e5d72008-06-11 18:37:52 +0000434}
435
Georg Brandl559e5d72008-06-11 18:37:52 +0000436
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000437/*[clinic input]
438bin as builtin_bin
439
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300440 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000441 /
442
443Return the binary representation of an integer.
444
445 >>> bin(2796202)
446 '0b1010101010101010101010'
447[clinic start generated code]*/
448
Guido van Rossum79f25d91997-04-29 20:08:16 +0000449static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300450builtin_bin(PyObject *module, PyObject *number)
451/*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000452{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000453 return PyNumber_ToBase(number, 2);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000454}
455
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000456
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000457/*[clinic input]
458callable as builtin_callable
459
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300460 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000461 /
462
463Return whether the object is callable (i.e., some kind of function).
464
465Note that classes are callable, as are instances of classes with a
466__call__() method.
467[clinic start generated code]*/
468
Antoine Pitroue71362d2010-11-27 22:00:11 +0000469static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300470builtin_callable(PyObject *module, PyObject *obj)
471/*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
Antoine Pitroue71362d2010-11-27 22:00:11 +0000472{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000473 return PyBool_FromLong((long)PyCallable_Check(obj));
Antoine Pitroue71362d2010-11-27 22:00:11 +0000474}
475
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400476static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200477builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400478{
479 PyObject *hook = PySys_GetObject("breakpointhook");
480
481 if (hook == NULL) {
482 PyErr_SetString(PyExc_RuntimeError, "lost sys.breakpointhook");
483 return NULL;
484 }
485 Py_INCREF(hook);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200486 PyObject *retval = _PyObject_Vectorcall(hook, args, nargs, keywords);
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400487 Py_DECREF(hook);
488 return retval;
489}
490
491PyDoc_STRVAR(breakpoint_doc,
492"breakpoint(*args, **kws)\n\
493\n\
494Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept\n\
495whatever arguments are passed.\n\
496\n\
497By default, this drops you into the pdb debugger.");
Antoine Pitroue71362d2010-11-27 22:00:11 +0000498
Raymond Hettinger17301e92008-03-13 00:19:26 +0000499typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyObject_HEAD
501 PyObject *func;
502 PyObject *it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000503} filterobject;
504
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000505static PyObject *
Raymond Hettinger17301e92008-03-13 00:19:26 +0000506filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 PyObject *func, *seq;
509 PyObject *it;
510 filterobject *lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000511
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +0300512 if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
516 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 /* Get iterator. */
519 it = PyObject_GetIter(seq);
520 if (it == NULL)
521 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 /* create filterobject structure */
524 lz = (filterobject *)type->tp_alloc(type, 0);
525 if (lz == NULL) {
526 Py_DECREF(it);
527 return NULL;
528 }
529 Py_INCREF(func);
530 lz->func = func;
531 lz->it = it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return (PyObject *)lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000534}
535
536static void
537filter_dealloc(filterobject *lz)
538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 PyObject_GC_UnTrack(lz);
540 Py_XDECREF(lz->func);
541 Py_XDECREF(lz->it);
542 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger17301e92008-03-13 00:19:26 +0000543}
544
545static int
546filter_traverse(filterobject *lz, visitproc visit, void *arg)
547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 Py_VISIT(lz->it);
549 Py_VISIT(lz->func);
550 return 0;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000551}
552
553static PyObject *
554filter_next(filterobject *lz)
555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 PyObject *item;
557 PyObject *it = lz->it;
558 long ok;
559 PyObject *(*iternext)(PyObject *);
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400560 int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 iternext = *Py_TYPE(it)->tp_iternext;
563 for (;;) {
564 item = iternext(it);
565 if (item == NULL)
566 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000567
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400568 if (checktrue) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 ok = PyObject_IsTrue(item);
570 } else {
571 PyObject *good;
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100572 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (good == NULL) {
574 Py_DECREF(item);
575 return NULL;
576 }
577 ok = PyObject_IsTrue(good);
578 Py_DECREF(good);
579 }
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200580 if (ok > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return item;
582 Py_DECREF(item);
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200583 if (ok < 0)
584 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000586}
587
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000588static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530589filter_reduce(filterobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000590{
591 return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
592}
593
594PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
595
596static PyMethodDef filter_methods[] = {
597 {"__reduce__", (PyCFunction)filter_reduce, METH_NOARGS, reduce_doc},
598 {NULL, NULL} /* sentinel */
599};
600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000601PyDoc_STRVAR(filter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +0000602"filter(function or None, iterable) --> filter object\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000603\n\
Georg Brandld11ae5d2008-05-16 13:27:32 +0000604Return an iterator yielding those items of iterable for which function(item)\n\
Raymond Hettinger17301e92008-03-13 00:19:26 +0000605is true. If function is None, return the items that are true.");
606
607PyTypeObject PyFilter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyVarObject_HEAD_INIT(&PyType_Type, 0)
609 "filter", /* tp_name */
610 sizeof(filterobject), /* tp_basicsize */
611 0, /* tp_itemsize */
612 /* methods */
613 (destructor)filter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200614 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 0, /* tp_getattr */
616 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200617 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 0, /* tp_repr */
619 0, /* tp_as_number */
620 0, /* tp_as_sequence */
621 0, /* tp_as_mapping */
622 0, /* tp_hash */
623 0, /* tp_call */
624 0, /* tp_str */
625 PyObject_GenericGetAttr, /* tp_getattro */
626 0, /* tp_setattro */
627 0, /* tp_as_buffer */
628 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
629 Py_TPFLAGS_BASETYPE, /* tp_flags */
630 filter_doc, /* tp_doc */
631 (traverseproc)filter_traverse, /* tp_traverse */
632 0, /* tp_clear */
633 0, /* tp_richcompare */
634 0, /* tp_weaklistoffset */
635 PyObject_SelfIter, /* tp_iter */
636 (iternextfunc)filter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000637 filter_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 0, /* tp_members */
639 0, /* tp_getset */
640 0, /* tp_base */
641 0, /* tp_dict */
642 0, /* tp_descr_get */
643 0, /* tp_descr_set */
644 0, /* tp_dictoffset */
645 0, /* tp_init */
646 PyType_GenericAlloc, /* tp_alloc */
647 filter_new, /* tp_new */
648 PyObject_GC_Del, /* tp_free */
Raymond Hettinger17301e92008-03-13 00:19:26 +0000649};
650
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000651
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000652/*[clinic input]
653format as builtin_format
654
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300655 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000656 format_spec: unicode(c_default="NULL") = ''
657 /
658
659Return value.__format__(format_spec)
660
Amit Kumar2e6bb442017-05-29 06:32:26 +0530661format_spec defaults to the empty string.
662See the Format Specification Mini-Language section of help('FORMATTING') for
663details.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000664[clinic start generated code]*/
665
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000666static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300667builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
Amit Kumar2e6bb442017-05-29 06:32:26 +0530668/*[clinic end generated code: output=2f40bdfa4954b077 input=88339c93ea522b33]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000669{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000670 return PyObject_Format(value, format_spec);
Eric Smith8c663262007-08-25 02:26:07 +0000671}
672
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000673/*[clinic input]
674chr as builtin_chr
675
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300676 i: int
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000677 /
678
679Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
680[clinic start generated code]*/
681
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000682static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300683builtin_chr_impl(PyObject *module, int i)
684/*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000685{
686 return PyUnicode_FromOrdinal(i);
687}
Guido van Rossum09095f32000-03-10 23:00:52 +0000688
689
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000690/*[clinic input]
691compile as builtin_compile
692
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300693 source: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000694 filename: object(converter="PyUnicode_FSDecoder")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300695 mode: str
696 flags: int = 0
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200697 dont_inherit: bool(accept={int}) = False
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300698 optimize: int = -1
Victor Stinnerb2fd32b2019-06-12 16:17:05 +0200699 *
700 _feature_version as feature_version: int = -1
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000701
702Compile source into a code object that can be executed by exec() or eval().
703
704The source code may represent a Python module, statement or expression.
705The filename will be used for run-time error messages.
706The mode must be 'exec' to compile a module, 'single' to compile a
707single (interactive) statement, or 'eval' to compile an expression.
708The flags argument, if present, controls which future statements influence
709the compilation of the code.
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300710The dont_inherit argument, if true, stops the compilation inheriting
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000711the effects of any future statements in effect in the code calling
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300712compile; if absent or false these statements do influence the compilation,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000713in addition to any features explicitly specified.
714[clinic start generated code]*/
715
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000716static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300717builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
718 const char *mode, int flags, int dont_inherit,
Guido van Rossum495da292019-03-07 12:38:08 -0800719 int optimize, int feature_version)
Victor Stinnerb2fd32b2019-06-12 16:17:05 +0200720/*[clinic end generated code: output=b0c09c84f116d3d7 input=40171fb92c1d580d]*/
Guido van Rossum5b722181993-03-30 17:46:03 +0000721{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000722 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200723 const char *str;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000724 int compile_mode = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 int is_ast;
726 PyCompilerFlags cf;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800727 int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000728 PyObject *result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000729
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000730 cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
Guido van Rossum495da292019-03-07 12:38:08 -0800731 cf.cf_feature_version = PY_MINOR_VERSION;
732 if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) {
733 cf.cf_feature_version = feature_version;
734 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000735
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000736 if (flags &
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800737 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST | PyCF_TYPE_COMMENTS))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 {
739 PyErr_SetString(PyExc_ValueError,
740 "compile(): unrecognised flags");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000741 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 }
743 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
Tim Peters6cd6a822001-08-17 22:11:27 +0000744
Georg Brandl8334fd92010-12-04 10:26:46 +0000745 if (optimize < -1 || optimize > 2) {
746 PyErr_SetString(PyExc_ValueError,
747 "compile(): invalid optimize value");
748 goto error;
749 }
750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 if (!dont_inherit) {
752 PyEval_MergeCompilerFlags(&cf);
753 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000754
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000755 if (strcmp(mode, "exec") == 0)
756 compile_mode = 0;
757 else if (strcmp(mode, "eval") == 0)
758 compile_mode = 1;
759 else if (strcmp(mode, "single") == 0)
760 compile_mode = 2;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800761 else if (strcmp(mode, "func_type") == 0) {
762 if (!(flags & PyCF_ONLY_AST)) {
763 PyErr_SetString(PyExc_ValueError,
764 "compile() mode 'func_type' requires flag PyCF_ONLY_AST");
765 goto error;
766 }
767 compile_mode = 3;
768 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800770 const char *msg;
771 if (flags & PyCF_ONLY_AST)
772 msg = "compile() mode must be 'exec', 'eval', 'single' or 'func_type'";
773 else
774 msg = "compile() mode must be 'exec', 'eval' or 'single'";
775 PyErr_SetString(PyExc_ValueError, msg);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000776 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 }
Neal Norwitzdb4115f2008-03-31 04:20:05 +0000778
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000779 is_ast = PyAST_Check(source);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 if (is_ast == -1)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000781 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (is_ast) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000783 if (flags & PyCF_ONLY_AST) {
784 Py_INCREF(source);
785 result = source;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 }
787 else {
788 PyArena *arena;
789 mod_ty mod;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 arena = PyArena_New();
Stefan Krah07795df2012-08-20 17:19:50 +0200792 if (arena == NULL)
793 goto error;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000794 mod = PyAST_obj2mod(source, arena, compile_mode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 if (mod == NULL) {
796 PyArena_Free(arena);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000797 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 }
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500799 if (!PyAST_Validate(mod)) {
800 PyArena_Free(arena);
801 goto error;
802 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200803 result = (PyObject*)PyAST_CompileObject(mod, filename,
804 &cf, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PyArena_Free(arena);
806 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000807 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000809
Dino Viehland41540692019-05-28 16:21:17 -0700810 str = _Py_SourceAsString(source, "compile", "string, bytes or AST", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 if (str == NULL)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000812 goto error;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000813
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000814 result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000815 Py_XDECREF(source_copy);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000816 goto finally;
817
818error:
819 result = NULL;
820finally:
Victor Stinner14e461d2013-08-26 22:28:21 +0200821 Py_DECREF(filename);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000822 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000823}
824
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000825/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000827builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
832 return NULL;
833 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000834}
835
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000836PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000837"dir([object]) -> list of strings\n"
838"\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000839"If called without an argument, return the names in the current scope.\n"
840"Else, return an alphabetized list of names comprising (some of) the attributes\n"
841"of the given object, and of attributes reachable from it.\n"
842"If the object supplies a method named __dir__, it will be used; otherwise\n"
843"the default dir() logic is used and returns:\n"
844" for a module object: the module's attributes.\n"
845" for a class object: its attributes, and recursively the attributes\n"
846" of its bases.\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000847" for any other object: its attributes, its class's attributes, and\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000848" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000849
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000850/*[clinic input]
851divmod as builtin_divmod
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000852
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300853 x: object
854 y: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000855 /
856
Zachary Ware7f227d92016-04-28 14:39:50 -0500857Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000858[clinic start generated code]*/
859
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000860static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300861builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
862/*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000863{
864 return PyNumber_Divmod(x, y);
865}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000866
867
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000868/*[clinic input]
869eval as builtin_eval
870
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300871 source: object
872 globals: object = None
873 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000874 /
875
876Evaluate the given source in the context of globals and locals.
877
878The source may be a string representing a Python expression
879or a code object as returned by compile().
880The globals must be a dictionary and locals can be any mapping,
881defaulting to the current globals and locals.
882If only globals is given, locals defaults to it.
883[clinic start generated code]*/
884
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000885static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300886builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400887 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300888/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000889{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000890 PyObject *result, *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200891 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (locals != Py_None && !PyMapping_Check(locals)) {
895 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
896 return NULL;
897 }
898 if (globals != Py_None && !PyDict_Check(globals)) {
899 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
900 "globals must be a real dict; try eval(expr, {}, mapping)"
901 : "globals must be a dict");
902 return NULL;
903 }
904 if (globals == Py_None) {
905 globals = PyEval_GetGlobals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100906 if (locals == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100908 if (locals == NULL)
909 return NULL;
910 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 }
912 else if (locals == Py_None)
913 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 if (globals == NULL || locals == NULL) {
916 PyErr_SetString(PyExc_TypeError,
917 "eval must be given globals and locals "
918 "when called without a frame");
919 return NULL;
920 }
Georg Brandl77c85e62005-09-15 10:46:13 +0000921
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200922 if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
Victor Stinnerb44562b2013-11-06 19:03:11 +0100923 if (_PyDict_SetItemId(globals, &PyId___builtins__,
924 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 return NULL;
926 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200927 else if (PyErr_Occurred()) {
928 return NULL;
929 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000930
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000931 if (PyCode_Check(source)) {
Steve Dowerb82e17e2019-05-23 08:45:22 -0700932 if (PySys_Audit("exec", "O", source) < 0) {
933 return NULL;
934 }
935
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000936 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PyErr_SetString(PyExc_TypeError,
Steve Dowerb82e17e2019-05-23 08:45:22 -0700938 "code object passed to eval() may not contain free variables");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return NULL;
940 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000941 return PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Guido van Rossum495da292019-03-07 12:38:08 -0800945 cf.cf_feature_version = PY_MINOR_VERSION;
Dino Viehland41540692019-05-28 16:21:17 -0700946 str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 if (str == NULL)
948 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 while (*str == ' ' || *str == '\t')
951 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 (void)PyEval_MergeCompilerFlags(&cf);
954 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000955 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000957}
958
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000959/*[clinic input]
960exec as builtin_exec
961
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300962 source: object
963 globals: object = None
964 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000965 /
966
967Execute the given source in the context of globals and locals.
968
969The source may be a string representing one or more Python statements
970or a code object as returned by compile().
971The globals must be a dictionary and locals can be any mapping,
972defaulting to the current globals and locals.
973If only globals is given, locals defaults to it.
974[clinic start generated code]*/
975
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000976static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300977builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400978 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300979/*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
Georg Brandl7cae87c2006-09-06 06:51:57 +0000980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyObject *v;
Georg Brandl2cabc562008-08-28 07:57:16 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 if (globals == Py_None) {
984 globals = PyEval_GetGlobals();
985 if (locals == Py_None) {
986 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100987 if (locals == NULL)
988 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 }
990 if (!globals || !locals) {
991 PyErr_SetString(PyExc_SystemError,
992 "globals and locals cannot be NULL");
993 return NULL;
994 }
995 }
996 else if (locals == Py_None)
997 locals = globals;
Georg Brandl7cae87c2006-09-06 06:51:57 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (!PyDict_Check(globals)) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001000 PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 globals->ob_type->tp_name);
1002 return NULL;
1003 }
1004 if (!PyMapping_Check(locals)) {
1005 PyErr_Format(PyExc_TypeError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001006 "locals must be a mapping or None, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 locals->ob_type->tp_name);
1008 return NULL;
1009 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001010 if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
Victor Stinnerb44562b2013-11-06 19:03:11 +01001011 if (_PyDict_SetItemId(globals, &PyId___builtins__,
1012 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 return NULL;
1014 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001015 else if (PyErr_Occurred()) {
1016 return NULL;
1017 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001019 if (PyCode_Check(source)) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07001020 if (PySys_Audit("exec", "O", source) < 0) {
1021 return NULL;
1022 }
1023
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001024 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 PyErr_SetString(PyExc_TypeError,
1026 "code object passed to exec() may not "
1027 "contain free variables");
1028 return NULL;
1029 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001030 v = PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 }
1032 else {
Martin Panter61d6e4a2015-11-07 02:56:11 +00001033 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001034 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyCompilerFlags cf;
1036 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Guido van Rossum495da292019-03-07 12:38:08 -08001037 cf.cf_feature_version = PY_MINOR_VERSION;
Dino Viehland41540692019-05-28 16:21:17 -07001038 str = _Py_SourceAsString(source, "exec",
Martin Panter61d6e4a2015-11-07 02:56:11 +00001039 "string, bytes or code", &cf,
1040 &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (str == NULL)
1042 return NULL;
1043 if (PyEval_MergeCompilerFlags(&cf))
1044 v = PyRun_StringFlags(str, Py_file_input, globals,
1045 locals, &cf);
1046 else
1047 v = PyRun_String(str, Py_file_input, globals, locals);
Martin Panter61d6e4a2015-11-07 02:56:11 +00001048 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 }
1050 if (v == NULL)
1051 return NULL;
1052 Py_DECREF(v);
1053 Py_RETURN_NONE;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001054}
1055
Georg Brandl7cae87c2006-09-06 06:51:57 +00001056
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001057/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001058static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001059builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Guido van Rossum33894be1992-01-27 16:53:09 +00001060{
Serhiy Storchaka79342662019-01-12 08:25:41 +02001061 PyObject *v, *name, *result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001062
Serhiy Storchaka79342662019-01-12 08:25:41 +02001063 if (!_PyArg_CheckPositional("getattr", nargs, 2, 3))
Sylvain96c7c062017-06-15 17:05:23 +02001064 return NULL;
1065
Serhiy Storchaka79342662019-01-12 08:25:41 +02001066 v = args[0];
1067 name = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!PyUnicode_Check(name)) {
1069 PyErr_SetString(PyExc_TypeError,
1070 "getattr(): attribute name must be string");
1071 return NULL;
1072 }
Serhiy Storchaka79342662019-01-12 08:25:41 +02001073 if (nargs > 2) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001074 if (_PyObject_LookupAttr(v, name, &result) == 0) {
Serhiy Storchaka79342662019-01-12 08:25:41 +02001075 PyObject *dflt = args[2];
INADA Naoki378edee2018-01-16 20:52:41 +09001076 Py_INCREF(dflt);
1077 return dflt;
1078 }
1079 }
1080 else {
1081 result = PyObject_GetAttr(v, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 }
1083 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001084}
1085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001086PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +00001087"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001088\n\
Guido van Rossum950ff291998-06-29 13:38:57 +00001089Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
1090When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001091exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001092
1093
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001094/*[clinic input]
1095globals as builtin_globals
1096
1097Return the dictionary containing the current scope's global variables.
1098
1099NOTE: Updates to this dictionary *will* affect name lookups in the current
1100global scope and vice-versa.
1101[clinic start generated code]*/
1102
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001103static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001104builtin_globals_impl(PyObject *module)
1105/*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 d = PyEval_GetGlobals();
1110 Py_XINCREF(d);
1111 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001112}
1113
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001114
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001115/*[clinic input]
1116hasattr as builtin_hasattr
1117
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001118 obj: object
1119 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001120 /
1121
1122Return whether the object has an attribute with the given name.
1123
1124This is done by calling getattr(obj, name) and catching AttributeError.
1125[clinic start generated code]*/
1126
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001127static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001128builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1129/*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001130{
1131 PyObject *v;
1132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 if (!PyUnicode_Check(name)) {
1134 PyErr_SetString(PyExc_TypeError,
1135 "hasattr(): attribute name must be string");
1136 return NULL;
1137 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001138 if (_PyObject_LookupAttr(obj, name, &v) < 0) {
Benjamin Peterson17689992010-08-24 03:26:23 +00001139 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001141 if (v == NULL) {
1142 Py_RETURN_FALSE;
1143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 Py_DECREF(v);
Benjamin Peterson17689992010-08-24 03:26:23 +00001145 Py_RETURN_TRUE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001146}
1147
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001148
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001149/* AC: gdb's integration with CPython relies on builtin_id having
1150 * the *exact* parameter names of "self" and "v", so we ensure we
1151 * preserve those name rather than using the AC defaults.
1152 */
1153/*[clinic input]
1154id as builtin_id
1155
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001156 self: self(type="PyModuleDef *")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001157 obj as v: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001158 /
1159
1160Return the identity of an object.
1161
1162This is guaranteed to be unique among simultaneously existing objects.
1163(CPython uses the object's memory address.)
1164[clinic start generated code]*/
1165
Guido van Rossum79f25d91997-04-29 20:08:16 +00001166static PyObject *
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001167builtin_id(PyModuleDef *self, PyObject *v)
1168/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
Guido van Rossum5b722181993-03-30 17:46:03 +00001169{
Steve Dowerb82e17e2019-05-23 08:45:22 -07001170 PyObject *id = PyLong_FromVoidPtr(v);
1171
1172 if (id && PySys_Audit("builtins.id", "O", id) < 0) {
1173 Py_DECREF(id);
1174 return NULL;
1175 }
1176
1177 return id;
Guido van Rossum5b722181993-03-30 17:46:03 +00001178}
1179
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001180
Raymond Hettingera6c60372008-03-13 01:26:19 +00001181/* map object ************************************************************/
1182
1183typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 PyObject_HEAD
1185 PyObject *iters;
1186 PyObject *func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001187} mapobject;
1188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189static PyObject *
Raymond Hettingera6c60372008-03-13 01:26:19 +00001190map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 PyObject *it, *iters, *func;
1193 mapobject *lz;
1194 Py_ssize_t numargs, i;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001195
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001196 if (type == &PyMap_Type && !_PyArg_NoKeywords("map", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 numargs = PyTuple_Size(args);
1200 if (numargs < 2) {
1201 PyErr_SetString(PyExc_TypeError,
1202 "map() must have at least two arguments.");
1203 return NULL;
1204 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 iters = PyTuple_New(numargs-1);
1207 if (iters == NULL)
1208 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 for (i=1 ; i<numargs ; i++) {
1211 /* Get iterator. */
1212 it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1213 if (it == NULL) {
1214 Py_DECREF(iters);
1215 return NULL;
1216 }
1217 PyTuple_SET_ITEM(iters, i-1, it);
1218 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 /* create mapobject structure */
1221 lz = (mapobject *)type->tp_alloc(type, 0);
1222 if (lz == NULL) {
1223 Py_DECREF(iters);
1224 return NULL;
1225 }
1226 lz->iters = iters;
1227 func = PyTuple_GET_ITEM(args, 0);
1228 Py_INCREF(func);
1229 lz->func = func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 return (PyObject *)lz;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001232}
1233
1234static void
1235map_dealloc(mapobject *lz)
1236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 PyObject_GC_UnTrack(lz);
1238 Py_XDECREF(lz->iters);
1239 Py_XDECREF(lz->func);
1240 Py_TYPE(lz)->tp_free(lz);
Raymond Hettingera6c60372008-03-13 01:26:19 +00001241}
1242
1243static int
1244map_traverse(mapobject *lz, visitproc visit, void *arg)
1245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 Py_VISIT(lz->iters);
1247 Py_VISIT(lz->func);
1248 return 0;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001249}
1250
1251static PyObject *
1252map_next(mapobject *lz)
1253{
Victor Stinnerbc08ab42016-12-15 12:40:53 +01001254 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001255 PyObject **stack;
1256 Py_ssize_t niters, nargs, i;
1257 PyObject *result = NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001258
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001259 niters = PyTuple_GET_SIZE(lz->iters);
1260 if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1261 stack = small_stack;
1262 }
1263 else {
1264 stack = PyMem_Malloc(niters * sizeof(stack[0]));
1265 if (stack == NULL) {
1266 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 return NULL;
1268 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 }
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001270
1271 nargs = 0;
1272 for (i=0; i < niters; i++) {
1273 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1274 PyObject *val = Py_TYPE(it)->tp_iternext(it);
1275 if (val == NULL) {
1276 goto exit;
1277 }
1278 stack[i] = val;
1279 nargs++;
1280 }
1281
1282 result = _PyObject_FastCall(lz->func, stack, nargs);
1283
1284exit:
1285 for (i=0; i < nargs; i++) {
1286 Py_DECREF(stack[i]);
1287 }
1288 if (stack != small_stack) {
1289 PyMem_Free(stack);
1290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001292}
1293
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001294static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301295map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001296{
1297 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1298 PyObject *args = PyTuple_New(numargs+1);
1299 Py_ssize_t i;
1300 if (args == NULL)
1301 return NULL;
1302 Py_INCREF(lz->func);
1303 PyTuple_SET_ITEM(args, 0, lz->func);
1304 for (i = 0; i<numargs; i++){
1305 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1306 Py_INCREF(it);
1307 PyTuple_SET_ITEM(args, i+1, it);
1308 }
1309
1310 return Py_BuildValue("ON", Py_TYPE(lz), args);
1311}
1312
1313static PyMethodDef map_methods[] = {
1314 {"__reduce__", (PyCFunction)map_reduce, METH_NOARGS, reduce_doc},
1315 {NULL, NULL} /* sentinel */
1316};
1317
1318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001319PyDoc_STRVAR(map_doc,
Raymond Hettingera6c60372008-03-13 01:26:19 +00001320"map(func, *iterables) --> map object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001321\n\
Raymond Hettingera6c60372008-03-13 01:26:19 +00001322Make an iterator that computes the function using arguments from\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323each of the iterables. Stops when the shortest iterable is exhausted.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001324
Raymond Hettingera6c60372008-03-13 01:26:19 +00001325PyTypeObject PyMap_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1327 "map", /* tp_name */
1328 sizeof(mapobject), /* tp_basicsize */
1329 0, /* tp_itemsize */
1330 /* methods */
1331 (destructor)map_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001332 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 0, /* tp_getattr */
1334 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001335 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 0, /* tp_repr */
1337 0, /* tp_as_number */
1338 0, /* tp_as_sequence */
1339 0, /* tp_as_mapping */
1340 0, /* tp_hash */
1341 0, /* tp_call */
1342 0, /* tp_str */
1343 PyObject_GenericGetAttr, /* tp_getattro */
1344 0, /* tp_setattro */
1345 0, /* tp_as_buffer */
1346 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1347 Py_TPFLAGS_BASETYPE, /* tp_flags */
1348 map_doc, /* tp_doc */
1349 (traverseproc)map_traverse, /* tp_traverse */
1350 0, /* tp_clear */
1351 0, /* tp_richcompare */
1352 0, /* tp_weaklistoffset */
1353 PyObject_SelfIter, /* tp_iter */
1354 (iternextfunc)map_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001355 map_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 0, /* tp_members */
1357 0, /* tp_getset */
1358 0, /* tp_base */
1359 0, /* tp_dict */
1360 0, /* tp_descr_get */
1361 0, /* tp_descr_set */
1362 0, /* tp_dictoffset */
1363 0, /* tp_init */
1364 PyType_GenericAlloc, /* tp_alloc */
1365 map_new, /* tp_new */
1366 PyObject_GC_Del, /* tp_free */
Raymond Hettingera6c60372008-03-13 01:26:19 +00001367};
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001368
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001369
1370/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001371static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001372builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Georg Brandla18af4e2007-04-21 15:47:16 +00001373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 PyObject *it, *res;
Georg Brandla18af4e2007-04-21 15:47:16 +00001375
Serhiy Storchaka79342662019-01-12 08:25:41 +02001376 if (!_PyArg_CheckPositional("next", nargs, 1, 2))
Sylvain96c7c062017-06-15 17:05:23 +02001377 return NULL;
1378
Serhiy Storchaka79342662019-01-12 08:25:41 +02001379 it = args[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (!PyIter_Check(it)) {
1381 PyErr_Format(PyExc_TypeError,
Philip Jenvey50add042011-11-06 16:37:52 -08001382 "'%.200s' object is not an iterator",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 it->ob_type->tp_name);
1384 return NULL;
1385 }
1386
1387 res = (*it->ob_type->tp_iternext)(it);
1388 if (res != NULL) {
1389 return res;
Serhiy Storchaka79342662019-01-12 08:25:41 +02001390 } else if (nargs > 1) {
1391 PyObject *def = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if (PyErr_Occurred()) {
1393 if(!PyErr_ExceptionMatches(PyExc_StopIteration))
1394 return NULL;
1395 PyErr_Clear();
1396 }
1397 Py_INCREF(def);
1398 return def;
1399 } else if (PyErr_Occurred()) {
1400 return NULL;
1401 } else {
1402 PyErr_SetNone(PyExc_StopIteration);
1403 return NULL;
1404 }
Georg Brandla18af4e2007-04-21 15:47:16 +00001405}
1406
1407PyDoc_STRVAR(next_doc,
1408"next(iterator[, default])\n\
1409\n\
1410Return the next item from the iterator. If default is given and the iterator\n\
1411is exhausted, it is returned instead of raising StopIteration.");
1412
1413
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001414/*[clinic input]
1415setattr as builtin_setattr
1416
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001417 obj: object
1418 name: object
1419 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001420 /
1421
1422Sets the named attribute on the given object to the specified value.
1423
1424setattr(x, 'y', v) is equivalent to ``x.y = v''
1425[clinic start generated code]*/
1426
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001427static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001428builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
Larry Hastings89964c42015-04-14 18:07:59 -04001429 PyObject *value)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001430/*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001431{
1432 if (PyObject_SetAttr(obj, name, value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001434 Py_RETURN_NONE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001435}
1436
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001437
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001438/*[clinic input]
1439delattr as builtin_delattr
1440
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001441 obj: object
1442 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001443 /
1444
1445Deletes the named attribute from the given object.
1446
1447delattr(x, 'y') is equivalent to ``del x.y''
1448[clinic start generated code]*/
1449
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001450static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001451builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1452/*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001453{
1454 if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001456 Py_RETURN_NONE;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001457}
1458
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001459
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001460/*[clinic input]
1461hash as builtin_hash
1462
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001463 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001464 /
1465
1466Return the hash value for the given object.
1467
1468Two objects that compare equal must also have the same hash value, but the
1469reverse is not necessarily true.
1470[clinic start generated code]*/
1471
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001473builtin_hash(PyObject *module, PyObject *obj)
1474/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001475{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001476 Py_hash_t x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001477
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001478 x = PyObject_Hash(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (x == -1)
1480 return NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001481 return PyLong_FromSsize_t(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001482}
1483
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001484
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001485/*[clinic input]
1486hex as builtin_hex
1487
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001488 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001489 /
1490
1491Return the hexadecimal representation of an integer.
1492
1493 >>> hex(12648430)
1494 '0xc0ffee'
1495[clinic start generated code]*/
1496
Guido van Rossum79f25d91997-04-29 20:08:16 +00001497static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001498builtin_hex(PyObject *module, PyObject *number)
1499/*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001500{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001501 return PyNumber_ToBase(number, 16);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001502}
1503
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001504
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001505/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001506static PyObject *
Serhiy Storchaka79342662019-01-12 08:25:41 +02001507builtin_iter(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001508{
Serhiy Storchaka79342662019-01-12 08:25:41 +02001509 PyObject *v;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001510
Serhiy Storchaka79342662019-01-12 08:25:41 +02001511 if (!_PyArg_CheckPositional("iter", nargs, 1, 2))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 return NULL;
Serhiy Storchaka79342662019-01-12 08:25:41 +02001513 v = args[0];
1514 if (nargs == 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 return PyObject_GetIter(v);
1516 if (!PyCallable_Check(v)) {
1517 PyErr_SetString(PyExc_TypeError,
1518 "iter(v, w): v must be callable");
1519 return NULL;
1520 }
Serhiy Storchaka79342662019-01-12 08:25:41 +02001521 PyObject *sentinel = args[1];
1522 return PyCallIter_New(v, sentinel);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001523}
1524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525PyDoc_STRVAR(iter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +00001526"iter(iterable) -> iterator\n\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001527iter(callable, sentinel) -> iterator\n\
1528\n\
1529Get an iterator from an object. In the first form, the argument must\n\
1530supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001531In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001532
1533
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001534/*[clinic input]
1535len as builtin_len
1536
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001537 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001538 /
1539
1540Return the number of items in a container.
1541[clinic start generated code]*/
1542
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001543static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001544builtin_len(PyObject *module, PyObject *obj)
1545/*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001548
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001549 res = PyObject_Size(obj);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001550 if (res < 0) {
1551 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 return NULL;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001553 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 return PyLong_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001555}
1556
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001557
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001558/*[clinic input]
1559locals as builtin_locals
1560
1561Return a dictionary containing the current scope's local variables.
1562
1563NOTE: Whether or not updates to this dictionary will affect name lookups in
1564the local scope and vice-versa is *implementation dependent* and not
1565covered by any backwards compatibility guarantees.
1566[clinic start generated code]*/
1567
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001568static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001569builtin_locals_impl(PyObject *module)
1570/*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 d = PyEval_GetLocals();
1575 Py_XINCREF(d);
1576 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001577}
1578
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001579
Guido van Rossum79f25d91997-04-29 20:08:16 +00001580static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001581min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001584 PyObject *emptytuple, *defaultval = NULL;
1585 static char *kwlist[] = {"key", "default", NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 const char *name = op == Py_LT ? "min" : "max";
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001587 const int positional = PyTuple_Size(args) > 1;
1588 int ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001589
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001590 if (positional)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 v = args;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001592 else if (!PyArg_UnpackTuple(args, name, 1, 1, &v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001594
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001595 emptytuple = PyTuple_New(0);
1596 if (emptytuple == NULL)
1597 return NULL;
Oren Milman58cf7482017-08-21 20:19:07 +03001598 ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds,
1599 (op == Py_LT) ? "|$OO:min" : "|$OO:max",
1600 kwlist, &keyfunc, &defaultval);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001601 Py_DECREF(emptytuple);
1602 if (!ret)
1603 return NULL;
1604
1605 if (positional && defaultval != NULL) {
1606 PyErr_Format(PyExc_TypeError,
1607 "Cannot specify a default for %s() with multiple "
1608 "positional arguments", name);
1609 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 it = PyObject_GetIter(v);
1613 if (it == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 return NULL;
1615 }
Tim Petersc3074532001-05-03 07:00:32 +00001616
Alexander Marshalove22072f2018-07-24 10:58:21 +07001617 if (keyfunc == Py_None) {
1618 keyfunc = NULL;
1619 }
1620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 maxitem = NULL; /* the result */
1622 maxval = NULL; /* the value associated with the result */
1623 while (( item = PyIter_Next(it) )) {
1624 /* get the value from the key function */
1625 if (keyfunc != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001626 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 if (val == NULL)
1628 goto Fail_it_item;
1629 }
1630 /* no key function; the value is the item */
1631 else {
1632 val = item;
1633 Py_INCREF(val);
1634 }
Tim Petersc3074532001-05-03 07:00:32 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 /* maximum value and item are unset; set them */
1637 if (maxval == NULL) {
1638 maxitem = item;
1639 maxval = val;
1640 }
1641 /* maximum value and item are set; update them as necessary */
1642 else {
1643 int cmp = PyObject_RichCompareBool(val, maxval, op);
1644 if (cmp < 0)
1645 goto Fail_it_item_and_val;
1646 else if (cmp > 0) {
1647 Py_DECREF(maxval);
1648 Py_DECREF(maxitem);
1649 maxval = val;
1650 maxitem = item;
1651 }
1652 else {
1653 Py_DECREF(item);
1654 Py_DECREF(val);
1655 }
1656 }
1657 }
1658 if (PyErr_Occurred())
1659 goto Fail_it;
1660 if (maxval == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 assert(maxitem == NULL);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001662 if (defaultval != NULL) {
1663 Py_INCREF(defaultval);
1664 maxitem = defaultval;
1665 } else {
1666 PyErr_Format(PyExc_ValueError,
1667 "%s() arg is an empty sequence", name);
1668 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 }
1670 else
1671 Py_DECREF(maxval);
1672 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 return maxitem;
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001674
1675Fail_it_item_and_val:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 Py_DECREF(val);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001677Fail_it_item:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 Py_DECREF(item);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001679Fail_it:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 Py_XDECREF(maxval);
1681 Py_XDECREF(maxitem);
1682 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001684}
1685
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001686/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001687static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001688builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001691}
1692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001693PyDoc_STRVAR(min_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001694"min(iterable, *[, default=obj, key=func]) -> value\n\
1695min(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001696\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001697With a single iterable argument, return its smallest item. The\n\
1698default keyword-only argument specifies an object to return if\n\
1699the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001700With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001701
1702
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001703/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001704static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001705builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001708}
1709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001710PyDoc_STRVAR(max_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001711"max(iterable, *[, default=obj, key=func]) -> value\n\
1712max(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001713\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001714With a single iterable argument, return its biggest item. The\n\
1715default keyword-only argument specifies an object to return if\n\
1716the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001718
1719
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001720/*[clinic input]
1721oct as builtin_oct
1722
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001723 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001724 /
1725
1726Return the octal representation of an integer.
1727
1728 >>> oct(342391)
1729 '0o1234567'
1730[clinic start generated code]*/
1731
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001733builtin_oct(PyObject *module, PyObject *number)
1734/*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001735{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001736 return PyNumber_ToBase(number, 8);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001737}
1738
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001739
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001740/*[clinic input]
1741ord as builtin_ord
1742
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001743 c: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001744 /
1745
1746Return the Unicode code point for a one-character string.
1747[clinic start generated code]*/
1748
Guido van Rossum79f25d91997-04-29 20:08:16 +00001749static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001750builtin_ord(PyObject *module, PyObject *c)
1751/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 long ord;
1754 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001755
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001756 if (PyBytes_Check(c)) {
1757 size = PyBytes_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001759 ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 return PyLong_FromLong(ord);
1761 }
1762 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001763 else if (PyUnicode_Check(c)) {
1764 if (PyUnicode_READY(c) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001765 return NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001766 size = PyUnicode_GET_LENGTH(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001768 ord = (long)PyUnicode_READ_CHAR(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 return PyLong_FromLong(ord);
1770 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001772 else if (PyByteArray_Check(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 /* XXX Hopefully this is temporary */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001774 size = PyByteArray_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001776 ord = (long)((unsigned char)*PyByteArray_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 return PyLong_FromLong(ord);
1778 }
1779 }
1780 else {
1781 PyErr_Format(PyExc_TypeError,
1782 "ord() expected string of length 1, but " \
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001783 "%.200s found", c->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 return NULL;
1785 }
Guido van Rossum09095f32000-03-10 23:00:52 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 PyErr_Format(PyExc_TypeError,
1788 "ord() expected a character, "
1789 "but string of length %zd found",
1790 size);
1791 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001792}
1793
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001794
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001795/*[clinic input]
1796pow as builtin_pow
1797
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001798 x: object
1799 y: object
1800 z: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001801 /
1802
1803Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
1804
1805Some types, such as ints, are able to use a more efficient algorithm when
1806invoked using the three argument form.
1807[clinic start generated code]*/
1808
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001809static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001810builtin_pow_impl(PyObject *module, PyObject *x, PyObject *y, PyObject *z)
1811/*[clinic end generated code: output=50a14d5d130d404b input=653d57d38d41fc07]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001812{
1813 return PyNumber_Power(x, y, z);
1814}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001815
1816
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001817/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum34343512006-11-30 22:13:52 +00001818static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001819builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Guido van Rossum34343512006-11-30 22:13:52 +00001820{
INADA Naokibd584f12017-01-19 12:50:34 +01001821 static const char * const _keywords[] = {"sep", "end", "file", "flush", 0};
1822 static struct _PyArg_Parser _parser = {"|OOOO:print", _keywords, 0};
Georg Brandlbc3b6822012-01-13 19:41:25 +01001823 PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 int i, err;
Guido van Rossum34343512006-11-30 22:13:52 +00001825
INADA Naokibd584f12017-01-19 12:50:34 +01001826 if (kwnames != NULL &&
1827 !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser,
1828 &sep, &end, &file, &flush)) {
Georg Brandlbc3b6822012-01-13 19:41:25 +01001829 return NULL;
INADA Naokibd584f12017-01-19 12:50:34 +01001830 }
1831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 if (file == NULL || file == Py_None) {
Victor Stinnerbd303c12013-11-07 23:07:29 +01001833 file = _PySys_GetObjectId(&PyId_stdout);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001834 if (file == NULL) {
1835 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1836 return NULL;
1837 }
1838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 /* sys.stdout may be None when FILE* stdout isn't connected */
1840 if (file == Py_None)
1841 Py_RETURN_NONE;
1842 }
Guido van Rossum34343512006-11-30 22:13:52 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 if (sep == Py_None) {
1845 sep = NULL;
1846 }
1847 else if (sep && !PyUnicode_Check(sep)) {
1848 PyErr_Format(PyExc_TypeError,
1849 "sep must be None or a string, not %.200s",
1850 sep->ob_type->tp_name);
1851 return NULL;
1852 }
1853 if (end == Py_None) {
1854 end = NULL;
1855 }
1856 else if (end && !PyUnicode_Check(end)) {
1857 PyErr_Format(PyExc_TypeError,
1858 "end must be None or a string, not %.200s",
1859 end->ob_type->tp_name);
1860 return NULL;
1861 }
Guido van Rossum34343512006-11-30 22:13:52 +00001862
INADA Naokibd584f12017-01-19 12:50:34 +01001863 for (i = 0; i < nargs; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 if (i > 0) {
1865 if (sep == NULL)
1866 err = PyFile_WriteString(" ", file);
1867 else
1868 err = PyFile_WriteObject(sep, file,
1869 Py_PRINT_RAW);
1870 if (err)
1871 return NULL;
1872 }
INADA Naokibd584f12017-01-19 12:50:34 +01001873 err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (err)
1875 return NULL;
1876 }
Guido van Rossum34343512006-11-30 22:13:52 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 if (end == NULL)
1879 err = PyFile_WriteString("\n", file);
1880 else
1881 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1882 if (err)
1883 return NULL;
Guido van Rossum34343512006-11-30 22:13:52 +00001884
Georg Brandlbc3b6822012-01-13 19:41:25 +01001885 if (flush != NULL) {
1886 PyObject *tmp;
1887 int do_flush = PyObject_IsTrue(flush);
1888 if (do_flush == -1)
1889 return NULL;
1890 else if (do_flush) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001891 tmp = _PyObject_CallMethodId(file, &PyId_flush, NULL);
Georg Brandlbc3b6822012-01-13 19:41:25 +01001892 if (tmp == NULL)
1893 return NULL;
1894 else
1895 Py_DECREF(tmp);
1896 }
1897 }
1898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 Py_RETURN_NONE;
Guido van Rossum34343512006-11-30 22:13:52 +00001900}
1901
1902PyDoc_STRVAR(print_doc,
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001903"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\
Guido van Rossum34343512006-11-30 22:13:52 +00001904\n\
1905Prints the values to a stream, or to sys.stdout by default.\n\
1906Optional keyword arguments:\n\
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001907file: a file-like object (stream); defaults to the current sys.stdout.\n\
1908sep: string inserted between values, default a space.\n\
1909end: string appended after the last value, default a newline.\n\
1910flush: whether to forcibly flush the stream.");
Guido van Rossum34343512006-11-30 22:13:52 +00001911
1912
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001913/*[clinic input]
1914input as builtin_input
1915
1916 prompt: object(c_default="NULL") = None
1917 /
1918
1919Read a string from standard input. The trailing newline is stripped.
1920
1921The prompt string, if given, is printed to standard output without a
1922trailing newline before reading input.
1923
1924If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
1925On *nix systems, readline is used if available.
1926[clinic start generated code]*/
1927
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001928static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001929builtin_input_impl(PyObject *module, PyObject *prompt)
1930/*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001931{
Victor Stinnerbd303c12013-11-07 23:07:29 +01001932 PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
1933 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1934 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 PyObject *tmp;
1936 long fd;
1937 int tty;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 /* Check that stdin/out/err are intact */
1940 if (fin == NULL || fin == Py_None) {
1941 PyErr_SetString(PyExc_RuntimeError,
1942 "input(): lost sys.stdin");
1943 return NULL;
1944 }
1945 if (fout == NULL || fout == Py_None) {
1946 PyErr_SetString(PyExc_RuntimeError,
1947 "input(): lost sys.stdout");
1948 return NULL;
1949 }
1950 if (ferr == NULL || ferr == Py_None) {
1951 PyErr_SetString(PyExc_RuntimeError,
1952 "input(): lost sys.stderr");
1953 return NULL;
1954 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001955
Steve Dowerb82e17e2019-05-23 08:45:22 -07001956 if (PySys_Audit("builtins.input", "O", prompt ? prompt : Py_None) < 0) {
1957 return NULL;
1958 }
1959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 /* First of all, flush stderr */
Victor Stinner3466bde2016-09-05 18:16:01 -07001961 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 if (tmp == NULL)
1963 PyErr_Clear();
1964 else
1965 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 /* We should only use (GNU) readline if Python's sys.stdin and
1968 sys.stdout are the same as C's stdin and stdout, because we
1969 need to pass it those. */
Victor Stinner3466bde2016-09-05 18:16:01 -07001970 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 if (tmp == NULL) {
1972 PyErr_Clear();
1973 tty = 0;
1974 }
1975 else {
1976 fd = PyLong_AsLong(tmp);
1977 Py_DECREF(tmp);
1978 if (fd < 0 && PyErr_Occurred())
1979 return NULL;
1980 tty = fd == fileno(stdin) && isatty(fd);
1981 }
1982 if (tty) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001983 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
Martin Panterc9a6ab52015-10-10 01:25:38 +00001984 if (tmp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 PyErr_Clear();
Martin Panterc9a6ab52015-10-10 01:25:38 +00001986 tty = 0;
1987 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 else {
1989 fd = PyLong_AsLong(tmp);
1990 Py_DECREF(tmp);
1991 if (fd < 0 && PyErr_Occurred())
1992 return NULL;
1993 tty = fd == fileno(stdout) && isatty(fd);
1994 }
1995 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 /* If we're interactive, use (GNU) readline */
1998 if (tty) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01001999 PyObject *po = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002000 const char *promptstr;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002001 char *s = NULL;
2002 PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
2003 PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002004 const char *stdin_encoding_str, *stdin_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 PyObject *result;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002006 size_t len;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00002007
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002008 /* stdin is a text stream, so it must have an encoding. */
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002009 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002010 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002011 if (!stdin_encoding || !stdin_errors ||
2012 !PyUnicode_Check(stdin_encoding) ||
2013 !PyUnicode_Check(stdin_errors)) {
2014 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002015 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002016 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002017 stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2018 stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002019 if (!stdin_encoding_str || !stdin_errors_str)
2020 goto _readline_errors;
Victor Stinner3466bde2016-09-05 18:16:01 -07002021 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 if (tmp == NULL)
2023 PyErr_Clear();
2024 else
2025 Py_DECREF(tmp);
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002026 if (prompt != NULL) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002027 /* We have a prompt, encode it as stdout would */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002028 const char *stdout_encoding_str, *stdout_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 PyObject *stringpo;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002030 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002031 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002032 if (!stdout_encoding || !stdout_errors ||
2033 !PyUnicode_Check(stdout_encoding) ||
2034 !PyUnicode_Check(stdout_errors)) {
2035 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002036 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002037 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002038 stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2039 stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002040 if (!stdout_encoding_str || !stdout_errors_str)
2041 goto _readline_errors;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002042 stringpo = PyObject_Str(prompt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002043 if (stringpo == NULL)
2044 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 po = PyUnicode_AsEncodedString(stringpo,
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002046 stdout_encoding_str, stdout_errors_str);
2047 Py_CLEAR(stdout_encoding);
2048 Py_CLEAR(stdout_errors);
2049 Py_CLEAR(stringpo);
2050 if (po == NULL)
2051 goto _readline_errors;
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03002052 assert(PyBytes_Check(po));
2053 promptstr = PyBytes_AS_STRING(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 }
2055 else {
2056 po = NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002057 promptstr = "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002059 s = PyOS_Readline(stdin, stdout, promptstr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 if (s == NULL) {
Richard Oudkerk614c5782013-04-03 13:44:50 +01002061 PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 if (!PyErr_Occurred())
2063 PyErr_SetNone(PyExc_KeyboardInterrupt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002064 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002066
2067 len = strlen(s);
2068 if (len == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 PyErr_SetNone(PyExc_EOFError);
2070 result = NULL;
2071 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002072 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 if (len > PY_SSIZE_T_MAX) {
2074 PyErr_SetString(PyExc_OverflowError,
2075 "input: input too long");
2076 result = NULL;
2077 }
2078 else {
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002079 len--; /* strip trailing '\n' */
2080 if (len != 0 && s[len-1] == '\r')
2081 len--; /* strip trailing '\r' */
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002082 result = PyUnicode_Decode(s, len, stdin_encoding_str,
2083 stdin_errors_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 }
2085 }
2086 Py_DECREF(stdin_encoding);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002087 Py_DECREF(stdin_errors);
2088 Py_XDECREF(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 PyMem_FREE(s);
Steve Dowerb82e17e2019-05-23 08:45:22 -07002090
2091 if (result != NULL) {
2092 if (PySys_Audit("builtins.input/result", "O", result) < 0) {
2093 return NULL;
2094 }
2095 }
2096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 return result;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002098
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002099 _readline_errors:
2100 Py_XDECREF(stdin_encoding);
2101 Py_XDECREF(stdout_encoding);
2102 Py_XDECREF(stdin_errors);
2103 Py_XDECREF(stdout_errors);
2104 Py_XDECREF(po);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002105 if (tty)
2106 return NULL;
2107
2108 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 }
Guido van Rossumeba76962007-05-27 09:13:28 +00002110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 /* Fallback if we're not interactive */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002112 if (prompt != NULL) {
2113 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 return NULL;
2115 }
Victor Stinner3466bde2016-09-05 18:16:01 -07002116 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 if (tmp == NULL)
2118 PyErr_Clear();
2119 else
2120 Py_DECREF(tmp);
2121 return PyFile_GetLine(fin, -1);
Guido van Rossuma88a0332007-02-26 16:59:55 +00002122}
2123
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002124
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002125/*[clinic input]
2126repr as builtin_repr
2127
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002128 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002129 /
2130
2131Return the canonical string representation of the object.
2132
2133For many object types, including most builtins, eval(repr(obj)) == obj.
2134[clinic start generated code]*/
2135
Guido van Rossum79f25d91997-04-29 20:08:16 +00002136static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002137builtin_repr(PyObject *module, PyObject *obj)
2138/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
Guido van Rossumc89705d1992-11-26 08:54:07 +00002139{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002140 return PyObject_Repr(obj);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002141}
2142
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002143
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002144/*[clinic input]
2145round as builtin_round
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002146
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002147 number: object
2148 ndigits: object = NULL
2149
2150Round a number to a given precision in decimal digits.
2151
2152The return value is an integer if ndigits is omitted or None. Otherwise
2153the return value has the same type as the number. ndigits may be negative.
2154[clinic start generated code]*/
2155
2156static PyObject *
2157builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
2158/*[clinic end generated code: output=ff0d9dd176c02ede input=854bc3a217530c3d]*/
2159{
2160 PyObject *round, *result;
Alex Martelliae211f92007-08-22 23:21:33 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (Py_TYPE(number)->tp_dict == NULL) {
2163 if (PyType_Ready(Py_TYPE(number)) < 0)
2164 return NULL;
2165 }
Guido van Rossum15d3d042007-08-24 02:02:45 +00002166
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002167 round = _PyObject_LookupSpecial(number, &PyId___round__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 if (round == NULL) {
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002169 if (!PyErr_Occurred())
2170 PyErr_Format(PyExc_TypeError,
2171 "type %.100s doesn't define __round__ method",
2172 Py_TYPE(number)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 return NULL;
2174 }
Alex Martelliae211f92007-08-22 23:21:33 +00002175
Raymond Hettingerf0f1c232016-09-03 01:55:11 -07002176 if (ndigits == NULL || ndigits == Py_None)
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002177 result = _PyObject_CallNoArg(round);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 else
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002179 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002180 Py_DECREF(round);
2181 return result;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002182}
2183
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002184
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002185/*AC: we need to keep the kwds dict intact to easily call into the
2186 * list.sort method, which isn't currently supported in AC. So we just use
2187 * the initially generated signature with a custom implementation.
2188 */
2189/* [disabled clinic input]
2190sorted as builtin_sorted
2191
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002192 iterable as seq: object
2193 key as keyfunc: object = None
2194 reverse: object = False
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002195
2196Return a new list containing all items from the iterable in ascending order.
2197
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002198A custom key function can be supplied to customize the sort order, and the
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002199reverse flag can be set to request the result in descending order.
2200[end disabled clinic input]*/
2201
2202PyDoc_STRVAR(builtin_sorted__doc__,
Serhiy Storchaka3a104252017-01-23 12:29:47 +02002203"sorted($module, iterable, /, *, key=None, reverse=False)\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002204"--\n"
2205"\n"
2206"Return a new list containing all items from the iterable in ascending order.\n"
2207"\n"
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002208"A custom key function can be supplied to customize the sort order, and the\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002209"reverse flag can be set to request the result in descending order.");
2210
2211#define BUILTIN_SORTED_METHODDEF \
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002212 {"sorted", (PyCFunction)(void(*)(void))builtin_sorted, METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002213
Raymond Hettinger64958a12003-12-17 20:43:33 +00002214static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002215builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Raymond Hettinger64958a12003-12-17 20:43:33 +00002216{
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002217 PyObject *newlist, *v, *seq, *callable;
Victor Stinner5a60eca2017-01-17 15:17:49 +01002218
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002219 /* Keyword arguments are passed through list.sort() which will check
2220 them. */
2221 if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 newlist = PySequence_List(seq);
2225 if (newlist == NULL)
2226 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002227
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002228 callable = _PyObject_GetAttrId(newlist, &PyId_sort);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 if (callable == NULL) {
2230 Py_DECREF(newlist);
2231 return NULL;
2232 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002233
Serhiy Storchaka299dc232017-01-20 08:35:18 +02002234 assert(nargs >= 1);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02002235 v = _PyObject_Vectorcall(callable, args + 1, nargs - 1, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 Py_DECREF(callable);
2237 if (v == NULL) {
2238 Py_DECREF(newlist);
2239 return NULL;
2240 }
2241 Py_DECREF(v);
2242 return newlist;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002243}
2244
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002245
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002246/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002247static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002248builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 PyObject *v = NULL;
2251 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2254 return NULL;
2255 if (v == NULL) {
2256 d = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01002257 if (d == NULL)
2258 return NULL;
2259 Py_INCREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 }
2261 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002262 d = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 if (d == NULL) {
2264 PyErr_SetString(PyExc_TypeError,
2265 "vars() argument must have __dict__ attribute");
2266 return NULL;
2267 }
2268 }
2269 return d;
Guido van Rossum2d951851994-08-29 12:52:16 +00002270}
2271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002272PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002273"vars([object]) -> dictionary\n\
2274\n\
2275Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002276With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002277
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002278
2279/*[clinic input]
2280sum as builtin_sum
2281
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002282 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002283 /
Raymond Hettinger9dfa0fe2018-09-12 10:54:06 -07002284 start: object(c_default="NULL") = 0
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002285
2286Return the sum of a 'start' value (default: 0) plus an iterable of numbers
2287
2288When the iterable is empty, return the start value.
2289This function is intended specifically for use with numeric values and may
2290reject non-numeric types.
2291[clinic start generated code]*/
2292
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002293static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002294builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
Raymond Hettinger9dfa0fe2018-09-12 10:54:06 -07002295/*[clinic end generated code: output=df758cec7d1d302f input=162b50765250d222]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002296{
2297 PyObject *result = start;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 PyObject *temp, *item, *iter;
Alex Martellia70b1912003-04-22 08:12:33 +00002299
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002300 iter = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (iter == NULL)
2302 return NULL;
Alex Martellia70b1912003-04-22 08:12:33 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 if (result == NULL) {
2305 result = PyLong_FromLong(0);
2306 if (result == NULL) {
2307 Py_DECREF(iter);
2308 return NULL;
2309 }
2310 } else {
2311 /* reject string values for 'start' parameter */
2312 if (PyUnicode_Check(result)) {
2313 PyErr_SetString(PyExc_TypeError,
2314 "sum() can't sum strings [use ''.join(seq) instead]");
2315 Py_DECREF(iter);
2316 return NULL;
2317 }
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002318 if (PyBytes_Check(result)) {
2319 PyErr_SetString(PyExc_TypeError,
2320 "sum() can't sum bytes [use b''.join(seq) instead]");
Benjamin Peterson405f32c2011-07-29 22:43:45 -05002321 Py_DECREF(iter);
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002322 return NULL;
2323 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 if (PyByteArray_Check(result)) {
2325 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson4f921c22011-07-29 14:24:29 -05002326 "sum() can't sum bytearray [use b''.join(seq) instead]");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 Py_DECREF(iter);
2328 return NULL;
2329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 Py_INCREF(result);
2331 }
Alex Martellia70b1912003-04-22 08:12:33 +00002332
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002333#ifndef SLOW_SUM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2335 Assumes all inputs are the same type. If the assumption fails, default
2336 to the more general routine.
2337 */
2338 if (PyLong_CheckExact(result)) {
2339 int overflow;
2340 long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2341 /* If this already overflowed, don't even enter the loop. */
2342 if (overflow == 0) {
2343 Py_DECREF(result);
2344 result = NULL;
2345 }
2346 while(result == NULL) {
2347 item = PyIter_Next(iter);
2348 if (item == NULL) {
2349 Py_DECREF(iter);
2350 if (PyErr_Occurred())
2351 return NULL;
2352 return PyLong_FromLong(i_result);
2353 }
2354 if (PyLong_CheckExact(item)) {
2355 long b = PyLong_AsLongAndOverflow(item, &overflow);
Serhiy Storchaka29500732019-05-05 14:26:23 +03002356 if (overflow == 0 &&
2357 (i_result >= 0 ? (b <= LONG_MAX - i_result)
2358 : (b >= LONG_MIN - i_result)))
2359 {
2360 i_result += b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 Py_DECREF(item);
2362 continue;
2363 }
2364 }
2365 /* Either overflowed or is not an int. Restore real objects and process normally */
2366 result = PyLong_FromLong(i_result);
Christian Heimes704e2d32013-07-26 22:49:26 +02002367 if (result == NULL) {
2368 Py_DECREF(item);
2369 Py_DECREF(iter);
2370 return NULL;
2371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 temp = PyNumber_Add(result, item);
2373 Py_DECREF(result);
2374 Py_DECREF(item);
2375 result = temp;
2376 if (result == NULL) {
2377 Py_DECREF(iter);
2378 return NULL;
2379 }
2380 }
2381 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 if (PyFloat_CheckExact(result)) {
2384 double f_result = PyFloat_AS_DOUBLE(result);
2385 Py_DECREF(result);
2386 result = NULL;
2387 while(result == NULL) {
2388 item = PyIter_Next(iter);
2389 if (item == NULL) {
2390 Py_DECREF(iter);
2391 if (PyErr_Occurred())
2392 return NULL;
2393 return PyFloat_FromDouble(f_result);
2394 }
2395 if (PyFloat_CheckExact(item)) {
2396 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2397 f_result += PyFloat_AS_DOUBLE(item);
2398 PyFPE_END_PROTECT(f_result)
2399 Py_DECREF(item);
2400 continue;
2401 }
2402 if (PyLong_CheckExact(item)) {
2403 long value;
2404 int overflow;
2405 value = PyLong_AsLongAndOverflow(item, &overflow);
2406 if (!overflow) {
2407 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2408 f_result += (double)value;
2409 PyFPE_END_PROTECT(f_result)
2410 Py_DECREF(item);
2411 continue;
2412 }
2413 }
2414 result = PyFloat_FromDouble(f_result);
Alexey Izbyshev2b824b22018-08-24 07:27:52 +03002415 if (result == NULL) {
2416 Py_DECREF(item);
2417 Py_DECREF(iter);
2418 return NULL;
2419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 temp = PyNumber_Add(result, item);
2421 Py_DECREF(result);
2422 Py_DECREF(item);
2423 result = temp;
2424 if (result == NULL) {
2425 Py_DECREF(iter);
2426 return NULL;
2427 }
2428 }
2429 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002430#endif
2431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 for(;;) {
2433 item = PyIter_Next(iter);
2434 if (item == NULL) {
2435 /* error, or end-of-sequence */
2436 if (PyErr_Occurred()) {
2437 Py_DECREF(result);
2438 result = NULL;
2439 }
2440 break;
2441 }
2442 /* It's tempting to use PyNumber_InPlaceAdd instead of
2443 PyNumber_Add here, to avoid quadratic running time
2444 when doing 'sum(list_of_lists, [])'. However, this
2445 would produce a change in behaviour: a snippet like
Mark Dickinson9acadc52009-10-26 14:19:42 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 empty = []
2448 sum([[x] for x in range(10)], empty)
Mark Dickinson9acadc52009-10-26 14:19:42 +00002449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 would change the value of empty. */
2451 temp = PyNumber_Add(result, item);
2452 Py_DECREF(result);
2453 Py_DECREF(item);
2454 result = temp;
2455 if (result == NULL)
2456 break;
2457 }
2458 Py_DECREF(iter);
2459 return result;
Alex Martellia70b1912003-04-22 08:12:33 +00002460}
2461
Alex Martellia70b1912003-04-22 08:12:33 +00002462
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002463/*[clinic input]
2464isinstance as builtin_isinstance
2465
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002466 obj: object
2467 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002468 /
2469
2470Return whether an object is an instance of a class or of a subclass thereof.
2471
2472A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
2473check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
2474or ...`` etc.
2475[clinic start generated code]*/
2476
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002477static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002478builtin_isinstance_impl(PyObject *module, PyObject *obj,
Larry Hastings89964c42015-04-14 18:07:59 -04002479 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002480/*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002483
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002484 retval = PyObject_IsInstance(obj, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 if (retval < 0)
2486 return NULL;
2487 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002488}
2489
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002490
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002491/*[clinic input]
2492issubclass as builtin_issubclass
2493
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002494 cls: object
2495 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002496 /
2497
2498Return whether 'cls' is a derived from another class or is the same class.
2499
2500A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
2501check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
2502or ...`` etc.
2503[clinic start generated code]*/
2504
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002505static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002506builtin_issubclass_impl(PyObject *module, PyObject *cls,
Larry Hastings89964c42015-04-14 18:07:59 -04002507 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002508/*[clinic end generated code: output=358412410cd7a250 input=af5f35e9ceaddaf6]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002511
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002512 retval = PyObject_IsSubclass(cls, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 if (retval < 0)
2514 return NULL;
2515 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002516}
2517
2518
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002519typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 PyObject_HEAD
2521 Py_ssize_t tuplesize;
2522 PyObject *ittuple; /* tuple of iterators */
2523 PyObject *result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002524} zipobject;
2525
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002526static PyObject *
2527zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Barry Warsawbd599b52000-08-03 15:45:29 +00002528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 zipobject *lz;
2530 Py_ssize_t i;
2531 PyObject *ittuple; /* tuple of iterators */
2532 PyObject *result;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002533 Py_ssize_t tuplesize;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002534
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002535 if (type == &PyZip_Type && !_PyArg_NoKeywords("zip", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 return NULL;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 /* args must be a tuple */
2539 assert(PyTuple_Check(args));
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002540 tuplesize = PyTuple_GET_SIZE(args);
Barry Warsawbd599b52000-08-03 15:45:29 +00002541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 /* obtain iterators */
2543 ittuple = PyTuple_New(tuplesize);
2544 if (ittuple == NULL)
2545 return NULL;
2546 for (i=0; i < tuplesize; ++i) {
2547 PyObject *item = PyTuple_GET_ITEM(args, i);
2548 PyObject *it = PyObject_GetIter(item);
2549 if (it == NULL) {
2550 if (PyErr_ExceptionMatches(PyExc_TypeError))
2551 PyErr_Format(PyExc_TypeError,
2552 "zip argument #%zd must support iteration",
2553 i+1);
2554 Py_DECREF(ittuple);
2555 return NULL;
2556 }
2557 PyTuple_SET_ITEM(ittuple, i, it);
2558 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 /* create a result holder */
2561 result = PyTuple_New(tuplesize);
2562 if (result == NULL) {
2563 Py_DECREF(ittuple);
2564 return NULL;
2565 }
2566 for (i=0 ; i < tuplesize ; i++) {
2567 Py_INCREF(Py_None);
2568 PyTuple_SET_ITEM(result, i, Py_None);
2569 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 /* create zipobject structure */
2572 lz = (zipobject *)type->tp_alloc(type, 0);
2573 if (lz == NULL) {
2574 Py_DECREF(ittuple);
2575 Py_DECREF(result);
2576 return NULL;
2577 }
2578 lz->ittuple = ittuple;
2579 lz->tuplesize = tuplesize;
2580 lz->result = result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 return (PyObject *)lz;
Barry Warsawbd599b52000-08-03 15:45:29 +00002583}
2584
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002585static void
2586zip_dealloc(zipobject *lz)
2587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 PyObject_GC_UnTrack(lz);
2589 Py_XDECREF(lz->ittuple);
2590 Py_XDECREF(lz->result);
2591 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002592}
2593
2594static int
2595zip_traverse(zipobject *lz, visitproc visit, void *arg)
2596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 Py_VISIT(lz->ittuple);
2598 Py_VISIT(lz->result);
2599 return 0;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002600}
2601
2602static PyObject *
2603zip_next(zipobject *lz)
2604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 Py_ssize_t i;
2606 Py_ssize_t tuplesize = lz->tuplesize;
2607 PyObject *result = lz->result;
2608 PyObject *it;
2609 PyObject *item;
2610 PyObject *olditem;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 if (tuplesize == 0)
2613 return NULL;
2614 if (Py_REFCNT(result) == 1) {
2615 Py_INCREF(result);
2616 for (i=0 ; i < tuplesize ; i++) {
2617 it = PyTuple_GET_ITEM(lz->ittuple, i);
2618 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002619 if (item == NULL) {
2620 Py_DECREF(result);
2621 return NULL;
2622 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 olditem = PyTuple_GET_ITEM(result, i);
2624 PyTuple_SET_ITEM(result, i, item);
2625 Py_DECREF(olditem);
2626 }
2627 } else {
2628 result = PyTuple_New(tuplesize);
2629 if (result == NULL)
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002630 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 for (i=0 ; i < tuplesize ; i++) {
2632 it = PyTuple_GET_ITEM(lz->ittuple, i);
2633 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002634 if (item == NULL) {
2635 Py_DECREF(result);
2636 return NULL;
2637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 PyTuple_SET_ITEM(result, i, item);
2639 }
2640 }
2641 return result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002642}
Barry Warsawbd599b52000-08-03 15:45:29 +00002643
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002644static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302645zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002646{
2647 /* Just recreate the zip with the internal iterator tuple */
2648 return Py_BuildValue("OO", Py_TYPE(lz), lz->ittuple);
2649}
2650
2651static PyMethodDef zip_methods[] = {
2652 {"__reduce__", (PyCFunction)zip_reduce, METH_NOARGS, reduce_doc},
2653 {NULL, NULL} /* sentinel */
2654};
2655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002656PyDoc_STRVAR(zip_doc,
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002657"zip(iter1 [,iter2 [...]]) --> zip object\n\
Barry Warsawbd599b52000-08-03 15:45:29 +00002658\n\
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002659Return a zip object whose .__next__() method returns a tuple where\n\
2660the i-th element comes from the i-th iterable argument. The .__next__()\n\
2661method continues until the shortest iterable in the argument sequence\n\
Georg Brandlced51db2008-12-04 18:28:38 +00002662is exhausted and then it raises StopIteration.");
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002663
2664PyTypeObject PyZip_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2666 "zip", /* tp_name */
2667 sizeof(zipobject), /* tp_basicsize */
2668 0, /* tp_itemsize */
2669 /* methods */
2670 (destructor)zip_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002671 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 0, /* tp_getattr */
2673 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002674 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 0, /* tp_repr */
2676 0, /* tp_as_number */
2677 0, /* tp_as_sequence */
2678 0, /* tp_as_mapping */
2679 0, /* tp_hash */
2680 0, /* tp_call */
2681 0, /* tp_str */
2682 PyObject_GenericGetAttr, /* tp_getattro */
2683 0, /* tp_setattro */
2684 0, /* tp_as_buffer */
2685 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2686 Py_TPFLAGS_BASETYPE, /* tp_flags */
2687 zip_doc, /* tp_doc */
2688 (traverseproc)zip_traverse, /* tp_traverse */
2689 0, /* tp_clear */
2690 0, /* tp_richcompare */
2691 0, /* tp_weaklistoffset */
2692 PyObject_SelfIter, /* tp_iter */
2693 (iternextfunc)zip_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002694 zip_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 0, /* tp_members */
2696 0, /* tp_getset */
2697 0, /* tp_base */
2698 0, /* tp_dict */
2699 0, /* tp_descr_get */
2700 0, /* tp_descr_set */
2701 0, /* tp_dictoffset */
2702 0, /* tp_init */
2703 PyType_GenericAlloc, /* tp_alloc */
2704 zip_new, /* tp_new */
2705 PyObject_GC_Del, /* tp_free */
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002706};
Barry Warsawbd599b52000-08-03 15:45:29 +00002707
2708
Guido van Rossum79f25d91997-04-29 20:08:16 +00002709static PyMethodDef builtin_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002710 {"__build_class__", (PyCFunction)(void(*)(void))builtin___build_class__,
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002711 METH_FASTCALL | METH_KEYWORDS, build_class_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002712 {"__import__", (PyCFunction)(void(*)(void))builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002713 BUILTIN_ABS_METHODDEF
2714 BUILTIN_ALL_METHODDEF
2715 BUILTIN_ANY_METHODDEF
2716 BUILTIN_ASCII_METHODDEF
2717 BUILTIN_BIN_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002718 {"breakpoint", (PyCFunction)(void(*)(void))builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002719 BUILTIN_CALLABLE_METHODDEF
2720 BUILTIN_CHR_METHODDEF
2721 BUILTIN_COMPILE_METHODDEF
2722 BUILTIN_DELATTR_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 {"dir", builtin_dir, METH_VARARGS, dir_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002724 BUILTIN_DIVMOD_METHODDEF
2725 BUILTIN_EVAL_METHODDEF
2726 BUILTIN_EXEC_METHODDEF
2727 BUILTIN_FORMAT_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002728 {"getattr", (PyCFunction)(void(*)(void))builtin_getattr, METH_FASTCALL, getattr_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002729 BUILTIN_GLOBALS_METHODDEF
2730 BUILTIN_HASATTR_METHODDEF
2731 BUILTIN_HASH_METHODDEF
2732 BUILTIN_HEX_METHODDEF
2733 BUILTIN_ID_METHODDEF
2734 BUILTIN_INPUT_METHODDEF
2735 BUILTIN_ISINSTANCE_METHODDEF
2736 BUILTIN_ISSUBCLASS_METHODDEF
Serhiy Storchaka79342662019-01-12 08:25:41 +02002737 {"iter", (PyCFunction)(void(*)(void))builtin_iter, METH_FASTCALL, iter_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002738 BUILTIN_LEN_METHODDEF
2739 BUILTIN_LOCALS_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002740 {"max", (PyCFunction)(void(*)(void))builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2741 {"min", (PyCFunction)(void(*)(void))builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
2742 {"next", (PyCFunction)(void(*)(void))builtin_next, METH_FASTCALL, next_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002743 BUILTIN_OCT_METHODDEF
2744 BUILTIN_ORD_METHODDEF
2745 BUILTIN_POW_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002746 {"print", (PyCFunction)(void(*)(void))builtin_print, METH_FASTCALL | METH_KEYWORDS, print_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002747 BUILTIN_REPR_METHODDEF
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002748 BUILTIN_ROUND_METHODDEF
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002749 BUILTIN_SETATTR_METHODDEF
2750 BUILTIN_SORTED_METHODDEF
2751 BUILTIN_SUM_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 {"vars", builtin_vars, METH_VARARGS, vars_doc},
2753 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002754};
2755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002756PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002757"Built-in functions, exceptions, and other objects.\n\
2758\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002759Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002760
Martin v. Löwis1a214512008-06-11 05:26:20 +00002761static struct PyModuleDef builtinsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 PyModuleDef_HEAD_INIT,
2763 "builtins",
2764 builtin_doc,
2765 -1, /* multiple "initialization" just copies the module dict. */
2766 builtin_methods,
2767 NULL,
2768 NULL,
2769 NULL,
2770 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002771};
2772
2773
Guido van Rossum25ce5661997-08-02 03:10:38 +00002774PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002775_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 PyObject *mod, *dict, *debug;
Benjamin Peterson42124a72012-10-30 23:41:54 -04002778
Victor Stinner331a6a52019-05-27 16:39:22 +02002779 const PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
Victor Stinnerfbca9082018-08-30 00:50:45 +02002780
Benjamin Peterson42124a72012-10-30 23:41:54 -04002781 if (PyType_Ready(&PyFilter_Type) < 0 ||
2782 PyType_Ready(&PyMap_Type) < 0 ||
2783 PyType_Ready(&PyZip_Type) < 0)
2784 return NULL;
2785
Eric Snowd393c1b2017-09-14 12:18:12 -06002786 mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 if (mod == NULL)
2788 return NULL;
2789 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002790
Tim Peters7571a0f2003-03-23 17:52:28 +00002791#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 /* "builtins" exposes a number of statically allocated objects
2793 * that, before this code was added in 2.3, never showed up in
2794 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2795 * result, programs leaking references to None and False (etc)
2796 * couldn't be diagnosed by examining sys.getobjects(0).
2797 */
Tim Peters7571a0f2003-03-23 17:52:28 +00002798#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2799#else
2800#define ADD_TO_ALL(OBJECT) (void)0
2801#endif
2802
Tim Peters4b7625e2001-09-13 21:37:17 +00002803#define SETBUILTIN(NAME, OBJECT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2805 return NULL; \
2806 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 SETBUILTIN("None", Py_None);
2809 SETBUILTIN("Ellipsis", Py_Ellipsis);
2810 SETBUILTIN("NotImplemented", Py_NotImplemented);
2811 SETBUILTIN("False", Py_False);
2812 SETBUILTIN("True", Py_True);
2813 SETBUILTIN("bool", &PyBool_Type);
2814 SETBUILTIN("memoryview", &PyMemoryView_Type);
2815 SETBUILTIN("bytearray", &PyByteArray_Type);
2816 SETBUILTIN("bytes", &PyBytes_Type);
2817 SETBUILTIN("classmethod", &PyClassMethod_Type);
2818 SETBUILTIN("complex", &PyComplex_Type);
2819 SETBUILTIN("dict", &PyDict_Type);
2820 SETBUILTIN("enumerate", &PyEnum_Type);
2821 SETBUILTIN("filter", &PyFilter_Type);
2822 SETBUILTIN("float", &PyFloat_Type);
2823 SETBUILTIN("frozenset", &PyFrozenSet_Type);
2824 SETBUILTIN("property", &PyProperty_Type);
2825 SETBUILTIN("int", &PyLong_Type);
2826 SETBUILTIN("list", &PyList_Type);
2827 SETBUILTIN("map", &PyMap_Type);
2828 SETBUILTIN("object", &PyBaseObject_Type);
2829 SETBUILTIN("range", &PyRange_Type);
2830 SETBUILTIN("reversed", &PyReversed_Type);
2831 SETBUILTIN("set", &PySet_Type);
2832 SETBUILTIN("slice", &PySlice_Type);
2833 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2834 SETBUILTIN("str", &PyUnicode_Type);
2835 SETBUILTIN("super", &PySuper_Type);
2836 SETBUILTIN("tuple", &PyTuple_Type);
2837 SETBUILTIN("type", &PyType_Type);
2838 SETBUILTIN("zip", &PyZip_Type);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002839 debug = PyBool_FromLong(config->optimization_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002841 Py_DECREF(debug);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 return NULL;
2843 }
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002844 Py_DECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002847#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002848#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002849}