blob: c3e30593475d9df7ef5e0b4996e653b515c64daf [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;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800726 int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000727 PyObject *result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000728
Miss Islington (bot)92e836c2019-06-12 17:36:03 -0700729 PyCompilerFlags cf = _PyCompilerFlags_INIT;
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 if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) {
732 cf.cf_feature_version = feature_version;
733 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000734
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000735 if (flags &
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800736 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST | PyCF_TYPE_COMMENTS))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 {
738 PyErr_SetString(PyExc_ValueError,
739 "compile(): unrecognised flags");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000740 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 }
742 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
Tim Peters6cd6a822001-08-17 22:11:27 +0000743
Georg Brandl8334fd92010-12-04 10:26:46 +0000744 if (optimize < -1 || optimize > 2) {
745 PyErr_SetString(PyExc_ValueError,
746 "compile(): invalid optimize value");
747 goto error;
748 }
749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 if (!dont_inherit) {
751 PyEval_MergeCompilerFlags(&cf);
752 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000753
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000754 if (strcmp(mode, "exec") == 0)
755 compile_mode = 0;
756 else if (strcmp(mode, "eval") == 0)
757 compile_mode = 1;
758 else if (strcmp(mode, "single") == 0)
759 compile_mode = 2;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800760 else if (strcmp(mode, "func_type") == 0) {
761 if (!(flags & PyCF_ONLY_AST)) {
762 PyErr_SetString(PyExc_ValueError,
763 "compile() mode 'func_type' requires flag PyCF_ONLY_AST");
764 goto error;
765 }
766 compile_mode = 3;
767 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800769 const char *msg;
770 if (flags & PyCF_ONLY_AST)
771 msg = "compile() mode must be 'exec', 'eval', 'single' or 'func_type'";
772 else
773 msg = "compile() mode must be 'exec', 'eval' or 'single'";
774 PyErr_SetString(PyExc_ValueError, msg);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000775 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 }
Neal Norwitzdb4115f2008-03-31 04:20:05 +0000777
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000778 is_ast = PyAST_Check(source);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 if (is_ast == -1)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000780 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 if (is_ast) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000782 if (flags & PyCF_ONLY_AST) {
783 Py_INCREF(source);
784 result = source;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 }
786 else {
787 PyArena *arena;
788 mod_ty mod;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 arena = PyArena_New();
Stefan Krah07795df2012-08-20 17:19:50 +0200791 if (arena == NULL)
792 goto error;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000793 mod = PyAST_obj2mod(source, arena, compile_mode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (mod == NULL) {
795 PyArena_Free(arena);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000796 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 }
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500798 if (!PyAST_Validate(mod)) {
799 PyArena_Free(arena);
800 goto error;
801 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200802 result = (PyObject*)PyAST_CompileObject(mod, filename,
803 &cf, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PyArena_Free(arena);
805 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000806 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000808
Dino Viehland41540692019-05-28 16:21:17 -0700809 str = _Py_SourceAsString(source, "compile", "string, bytes or AST", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 if (str == NULL)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000811 goto error;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000812
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000813 result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000814 Py_XDECREF(source_copy);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000815 goto finally;
816
817error:
818 result = NULL;
819finally:
Victor Stinner14e461d2013-08-26 22:28:21 +0200820 Py_DECREF(filename);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000821 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000822}
823
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000824/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000826builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
831 return NULL;
832 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000833}
834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000835PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000836"dir([object]) -> list of strings\n"
837"\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000838"If called without an argument, return the names in the current scope.\n"
839"Else, return an alphabetized list of names comprising (some of) the attributes\n"
840"of the given object, and of attributes reachable from it.\n"
841"If the object supplies a method named __dir__, it will be used; otherwise\n"
842"the default dir() logic is used and returns:\n"
843" for a module object: the module's attributes.\n"
844" for a class object: its attributes, and recursively the attributes\n"
845" of its bases.\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000846" for any other object: its attributes, its class's attributes, and\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000847" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000848
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000849/*[clinic input]
850divmod as builtin_divmod
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000851
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300852 x: object
853 y: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000854 /
855
Zachary Ware7f227d92016-04-28 14:39:50 -0500856Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000857[clinic start generated code]*/
858
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000859static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300860builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
861/*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000862{
863 return PyNumber_Divmod(x, y);
864}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000865
866
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000867/*[clinic input]
868eval as builtin_eval
869
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300870 source: object
871 globals: object = None
872 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000873 /
874
875Evaluate the given source in the context of globals and locals.
876
877The source may be a string representing a Python expression
878or a code object as returned by compile().
879The globals must be a dictionary and locals can be any mapping,
880defaulting to the current globals and locals.
881If only globals is given, locals defaults to it.
882[clinic start generated code]*/
883
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000884static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300885builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400886 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300887/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000888{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000889 PyObject *result, *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200890 const char *str;
Guido van Rossum590baa41993-11-30 13:40:46 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 if (locals != Py_None && !PyMapping_Check(locals)) {
893 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
894 return NULL;
895 }
896 if (globals != Py_None && !PyDict_Check(globals)) {
897 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
898 "globals must be a real dict; try eval(expr, {}, mapping)"
899 : "globals must be a dict");
900 return NULL;
901 }
902 if (globals == Py_None) {
903 globals = PyEval_GetGlobals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100904 if (locals == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100906 if (locals == NULL)
907 return NULL;
908 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 }
910 else if (locals == Py_None)
911 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 if (globals == NULL || locals == NULL) {
914 PyErr_SetString(PyExc_TypeError,
915 "eval must be given globals and locals "
916 "when called without a frame");
917 return NULL;
918 }
Georg Brandl77c85e62005-09-15 10:46:13 +0000919
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200920 if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
Victor Stinnerb44562b2013-11-06 19:03:11 +0100921 if (_PyDict_SetItemId(globals, &PyId___builtins__,
922 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 return NULL;
924 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200925 else if (PyErr_Occurred()) {
926 return NULL;
927 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000928
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000929 if (PyCode_Check(source)) {
Steve Dowerb82e17e2019-05-23 08:45:22 -0700930 if (PySys_Audit("exec", "O", source) < 0) {
931 return NULL;
932 }
933
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000934 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 PyErr_SetString(PyExc_TypeError,
Steve Dowerb82e17e2019-05-23 08:45:22 -0700936 "code object passed to eval() may not contain free variables");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 return NULL;
938 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000939 return PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000941
Miss Islington (bot)92e836c2019-06-12 17:36:03 -0700942 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Dino Viehland41540692019-05-28 16:21:17 -0700944 str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 if (str == NULL)
946 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 while (*str == ' ' || *str == '\t')
949 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 (void)PyEval_MergeCompilerFlags(&cf);
952 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000953 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000955}
956
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000957/*[clinic input]
958exec as builtin_exec
959
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300960 source: object
961 globals: object = None
962 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000963 /
964
965Execute the given source in the context of globals and locals.
966
967The source may be a string representing one or more Python statements
968or a code object as returned by compile().
969The globals must be a dictionary and locals can be any mapping,
970defaulting to the current globals and locals.
971If only globals is given, locals defaults to it.
972[clinic start generated code]*/
973
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000974static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300975builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400976 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300977/*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
Georg Brandl7cae87c2006-09-06 06:51:57 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyObject *v;
Georg Brandl2cabc562008-08-28 07:57:16 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 if (globals == Py_None) {
982 globals = PyEval_GetGlobals();
983 if (locals == Py_None) {
984 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100985 if (locals == NULL)
986 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 }
988 if (!globals || !locals) {
989 PyErr_SetString(PyExc_SystemError,
990 "globals and locals cannot be NULL");
991 return NULL;
992 }
993 }
994 else if (locals == Py_None)
995 locals = globals;
Georg Brandl7cae87c2006-09-06 06:51:57 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (!PyDict_Check(globals)) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000998 PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 globals->ob_type->tp_name);
1000 return NULL;
1001 }
1002 if (!PyMapping_Check(locals)) {
1003 PyErr_Format(PyExc_TypeError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001004 "locals must be a mapping or None, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 locals->ob_type->tp_name);
1006 return NULL;
1007 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001008 if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
Victor Stinnerb44562b2013-11-06 19:03:11 +01001009 if (_PyDict_SetItemId(globals, &PyId___builtins__,
1010 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return NULL;
1012 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001013 else if (PyErr_Occurred()) {
1014 return NULL;
1015 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001017 if (PyCode_Check(source)) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07001018 if (PySys_Audit("exec", "O", source) < 0) {
1019 return NULL;
1020 }
1021
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001022 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyErr_SetString(PyExc_TypeError,
1024 "code object passed to exec() may not "
1025 "contain free variables");
1026 return NULL;
1027 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001028 v = PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 }
1030 else {
Martin Panter61d6e4a2015-11-07 02:56:11 +00001031 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001032 const char *str;
Miss Islington (bot)92e836c2019-06-12 17:36:03 -07001033 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Dino Viehland41540692019-05-28 16:21:17 -07001035 str = _Py_SourceAsString(source, "exec",
Martin Panter61d6e4a2015-11-07 02:56:11 +00001036 "string, bytes or code", &cf,
1037 &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (str == NULL)
1039 return NULL;
1040 if (PyEval_MergeCompilerFlags(&cf))
1041 v = PyRun_StringFlags(str, Py_file_input, globals,
1042 locals, &cf);
1043 else
1044 v = PyRun_String(str, Py_file_input, globals, locals);
Martin Panter61d6e4a2015-11-07 02:56:11 +00001045 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 }
1047 if (v == NULL)
1048 return NULL;
1049 Py_DECREF(v);
1050 Py_RETURN_NONE;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001051}
1052
Georg Brandl7cae87c2006-09-06 06:51:57 +00001053
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001054/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001055static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001056builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Guido van Rossum33894be1992-01-27 16:53:09 +00001057{
Serhiy Storchaka79342662019-01-12 08:25:41 +02001058 PyObject *v, *name, *result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001059
Serhiy Storchaka79342662019-01-12 08:25:41 +02001060 if (!_PyArg_CheckPositional("getattr", nargs, 2, 3))
Sylvain96c7c062017-06-15 17:05:23 +02001061 return NULL;
1062
Serhiy Storchaka79342662019-01-12 08:25:41 +02001063 v = args[0];
1064 name = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (!PyUnicode_Check(name)) {
1066 PyErr_SetString(PyExc_TypeError,
1067 "getattr(): attribute name must be string");
1068 return NULL;
1069 }
Serhiy Storchaka79342662019-01-12 08:25:41 +02001070 if (nargs > 2) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001071 if (_PyObject_LookupAttr(v, name, &result) == 0) {
Serhiy Storchaka79342662019-01-12 08:25:41 +02001072 PyObject *dflt = args[2];
INADA Naoki378edee2018-01-16 20:52:41 +09001073 Py_INCREF(dflt);
1074 return dflt;
1075 }
1076 }
1077 else {
1078 result = PyObject_GetAttr(v, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 }
1080 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001081}
1082
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001083PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +00001084"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001085\n\
Guido van Rossum950ff291998-06-29 13:38:57 +00001086Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
1087When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001088exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001089
1090
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001091/*[clinic input]
1092globals as builtin_globals
1093
1094Return the dictionary containing the current scope's global variables.
1095
1096NOTE: Updates to this dictionary *will* affect name lookups in the current
1097global scope and vice-versa.
1098[clinic start generated code]*/
1099
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001100static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001101builtin_globals_impl(PyObject *module)
1102/*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 d = PyEval_GetGlobals();
1107 Py_XINCREF(d);
1108 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001109}
1110
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001111
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001112/*[clinic input]
1113hasattr as builtin_hasattr
1114
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001115 obj: object
1116 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001117 /
1118
1119Return whether the object has an attribute with the given name.
1120
1121This is done by calling getattr(obj, name) and catching AttributeError.
1122[clinic start generated code]*/
1123
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001124static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001125builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1126/*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001127{
1128 PyObject *v;
1129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (!PyUnicode_Check(name)) {
1131 PyErr_SetString(PyExc_TypeError,
1132 "hasattr(): attribute name must be string");
1133 return NULL;
1134 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001135 if (_PyObject_LookupAttr(obj, name, &v) < 0) {
Benjamin Peterson17689992010-08-24 03:26:23 +00001136 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001138 if (v == NULL) {
1139 Py_RETURN_FALSE;
1140 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 Py_DECREF(v);
Benjamin Peterson17689992010-08-24 03:26:23 +00001142 Py_RETURN_TRUE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001143}
1144
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001145
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001146/* AC: gdb's integration with CPython relies on builtin_id having
1147 * the *exact* parameter names of "self" and "v", so we ensure we
1148 * preserve those name rather than using the AC defaults.
1149 */
1150/*[clinic input]
1151id as builtin_id
1152
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001153 self: self(type="PyModuleDef *")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001154 obj as v: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001155 /
1156
1157Return the identity of an object.
1158
1159This is guaranteed to be unique among simultaneously existing objects.
1160(CPython uses the object's memory address.)
1161[clinic start generated code]*/
1162
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163static PyObject *
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001164builtin_id(PyModuleDef *self, PyObject *v)
1165/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
Guido van Rossum5b722181993-03-30 17:46:03 +00001166{
Steve Dowerb82e17e2019-05-23 08:45:22 -07001167 PyObject *id = PyLong_FromVoidPtr(v);
1168
1169 if (id && PySys_Audit("builtins.id", "O", id) < 0) {
1170 Py_DECREF(id);
1171 return NULL;
1172 }
1173
1174 return id;
Guido van Rossum5b722181993-03-30 17:46:03 +00001175}
1176
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001177
Raymond Hettingera6c60372008-03-13 01:26:19 +00001178/* map object ************************************************************/
1179
1180typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 PyObject_HEAD
1182 PyObject *iters;
1183 PyObject *func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001184} mapobject;
1185
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186static PyObject *
Raymond Hettingera6c60372008-03-13 01:26:19 +00001187map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 PyObject *it, *iters, *func;
1190 mapobject *lz;
1191 Py_ssize_t numargs, i;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001192
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001193 if (type == &PyMap_Type && !_PyArg_NoKeywords("map", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 numargs = PyTuple_Size(args);
1197 if (numargs < 2) {
1198 PyErr_SetString(PyExc_TypeError,
1199 "map() must have at least two arguments.");
1200 return NULL;
1201 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 iters = PyTuple_New(numargs-1);
1204 if (iters == NULL)
1205 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 for (i=1 ; i<numargs ; i++) {
1208 /* Get iterator. */
1209 it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1210 if (it == NULL) {
1211 Py_DECREF(iters);
1212 return NULL;
1213 }
1214 PyTuple_SET_ITEM(iters, i-1, it);
1215 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 /* create mapobject structure */
1218 lz = (mapobject *)type->tp_alloc(type, 0);
1219 if (lz == NULL) {
1220 Py_DECREF(iters);
1221 return NULL;
1222 }
1223 lz->iters = iters;
1224 func = PyTuple_GET_ITEM(args, 0);
1225 Py_INCREF(func);
1226 lz->func = func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 return (PyObject *)lz;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001229}
1230
1231static void
1232map_dealloc(mapobject *lz)
1233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 PyObject_GC_UnTrack(lz);
1235 Py_XDECREF(lz->iters);
1236 Py_XDECREF(lz->func);
1237 Py_TYPE(lz)->tp_free(lz);
Raymond Hettingera6c60372008-03-13 01:26:19 +00001238}
1239
1240static int
1241map_traverse(mapobject *lz, visitproc visit, void *arg)
1242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 Py_VISIT(lz->iters);
1244 Py_VISIT(lz->func);
1245 return 0;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001246}
1247
1248static PyObject *
1249map_next(mapobject *lz)
1250{
Victor Stinnerbc08ab42016-12-15 12:40:53 +01001251 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001252 PyObject **stack;
1253 Py_ssize_t niters, nargs, i;
1254 PyObject *result = NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001255
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001256 niters = PyTuple_GET_SIZE(lz->iters);
1257 if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1258 stack = small_stack;
1259 }
1260 else {
1261 stack = PyMem_Malloc(niters * sizeof(stack[0]));
1262 if (stack == NULL) {
1263 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 return NULL;
1265 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 }
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001267
1268 nargs = 0;
1269 for (i=0; i < niters; i++) {
1270 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1271 PyObject *val = Py_TYPE(it)->tp_iternext(it);
1272 if (val == NULL) {
1273 goto exit;
1274 }
1275 stack[i] = val;
1276 nargs++;
1277 }
1278
1279 result = _PyObject_FastCall(lz->func, stack, nargs);
1280
1281exit:
1282 for (i=0; i < nargs; i++) {
1283 Py_DECREF(stack[i]);
1284 }
1285 if (stack != small_stack) {
1286 PyMem_Free(stack);
1287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001289}
1290
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001291static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301292map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001293{
1294 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1295 PyObject *args = PyTuple_New(numargs+1);
1296 Py_ssize_t i;
1297 if (args == NULL)
1298 return NULL;
1299 Py_INCREF(lz->func);
1300 PyTuple_SET_ITEM(args, 0, lz->func);
1301 for (i = 0; i<numargs; i++){
1302 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1303 Py_INCREF(it);
1304 PyTuple_SET_ITEM(args, i+1, it);
1305 }
1306
1307 return Py_BuildValue("ON", Py_TYPE(lz), args);
1308}
1309
1310static PyMethodDef map_methods[] = {
1311 {"__reduce__", (PyCFunction)map_reduce, METH_NOARGS, reduce_doc},
1312 {NULL, NULL} /* sentinel */
1313};
1314
1315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001316PyDoc_STRVAR(map_doc,
Raymond Hettingera6c60372008-03-13 01:26:19 +00001317"map(func, *iterables) --> map object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001318\n\
Raymond Hettingera6c60372008-03-13 01:26:19 +00001319Make an iterator that computes the function using arguments from\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320each of the iterables. Stops when the shortest iterable is exhausted.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001321
Raymond Hettingera6c60372008-03-13 01:26:19 +00001322PyTypeObject PyMap_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1324 "map", /* tp_name */
1325 sizeof(mapobject), /* tp_basicsize */
1326 0, /* tp_itemsize */
1327 /* methods */
1328 (destructor)map_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001329 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 0, /* tp_getattr */
1331 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001332 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 0, /* tp_repr */
1334 0, /* tp_as_number */
1335 0, /* tp_as_sequence */
1336 0, /* tp_as_mapping */
1337 0, /* tp_hash */
1338 0, /* tp_call */
1339 0, /* tp_str */
1340 PyObject_GenericGetAttr, /* tp_getattro */
1341 0, /* tp_setattro */
1342 0, /* tp_as_buffer */
1343 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1344 Py_TPFLAGS_BASETYPE, /* tp_flags */
1345 map_doc, /* tp_doc */
1346 (traverseproc)map_traverse, /* tp_traverse */
1347 0, /* tp_clear */
1348 0, /* tp_richcompare */
1349 0, /* tp_weaklistoffset */
1350 PyObject_SelfIter, /* tp_iter */
1351 (iternextfunc)map_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001352 map_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 0, /* tp_members */
1354 0, /* tp_getset */
1355 0, /* tp_base */
1356 0, /* tp_dict */
1357 0, /* tp_descr_get */
1358 0, /* tp_descr_set */
1359 0, /* tp_dictoffset */
1360 0, /* tp_init */
1361 PyType_GenericAlloc, /* tp_alloc */
1362 map_new, /* tp_new */
1363 PyObject_GC_Del, /* tp_free */
Raymond Hettingera6c60372008-03-13 01:26:19 +00001364};
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001365
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001366
1367/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001368static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001369builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Georg Brandla18af4e2007-04-21 15:47:16 +00001370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PyObject *it, *res;
Georg Brandla18af4e2007-04-21 15:47:16 +00001372
Serhiy Storchaka79342662019-01-12 08:25:41 +02001373 if (!_PyArg_CheckPositional("next", nargs, 1, 2))
Sylvain96c7c062017-06-15 17:05:23 +02001374 return NULL;
1375
Serhiy Storchaka79342662019-01-12 08:25:41 +02001376 it = args[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (!PyIter_Check(it)) {
1378 PyErr_Format(PyExc_TypeError,
Philip Jenvey50add042011-11-06 16:37:52 -08001379 "'%.200s' object is not an iterator",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 it->ob_type->tp_name);
1381 return NULL;
1382 }
1383
1384 res = (*it->ob_type->tp_iternext)(it);
1385 if (res != NULL) {
1386 return res;
Serhiy Storchaka79342662019-01-12 08:25:41 +02001387 } else if (nargs > 1) {
1388 PyObject *def = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 if (PyErr_Occurred()) {
1390 if(!PyErr_ExceptionMatches(PyExc_StopIteration))
1391 return NULL;
1392 PyErr_Clear();
1393 }
1394 Py_INCREF(def);
1395 return def;
1396 } else if (PyErr_Occurred()) {
1397 return NULL;
1398 } else {
1399 PyErr_SetNone(PyExc_StopIteration);
1400 return NULL;
1401 }
Georg Brandla18af4e2007-04-21 15:47:16 +00001402}
1403
1404PyDoc_STRVAR(next_doc,
1405"next(iterator[, default])\n\
1406\n\
1407Return the next item from the iterator. If default is given and the iterator\n\
1408is exhausted, it is returned instead of raising StopIteration.");
1409
1410
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001411/*[clinic input]
1412setattr as builtin_setattr
1413
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001414 obj: object
1415 name: object
1416 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001417 /
1418
1419Sets the named attribute on the given object to the specified value.
1420
1421setattr(x, 'y', v) is equivalent to ``x.y = v''
1422[clinic start generated code]*/
1423
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001424static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001425builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
Larry Hastings89964c42015-04-14 18:07:59 -04001426 PyObject *value)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001427/*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001428{
1429 if (PyObject_SetAttr(obj, name, value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001431 Py_RETURN_NONE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001432}
1433
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001434
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001435/*[clinic input]
1436delattr as builtin_delattr
1437
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001438 obj: object
1439 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001440 /
1441
1442Deletes the named attribute from the given object.
1443
1444delattr(x, 'y') is equivalent to ``del x.y''
1445[clinic start generated code]*/
1446
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001447static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001448builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1449/*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001450{
1451 if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001453 Py_RETURN_NONE;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001454}
1455
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001456
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001457/*[clinic input]
1458hash as builtin_hash
1459
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001460 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001461 /
1462
1463Return the hash value for the given object.
1464
1465Two objects that compare equal must also have the same hash value, but the
1466reverse is not necessarily true.
1467[clinic start generated code]*/
1468
Guido van Rossum79f25d91997-04-29 20:08:16 +00001469static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001470builtin_hash(PyObject *module, PyObject *obj)
1471/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001472{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001473 Py_hash_t x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001474
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001475 x = PyObject_Hash(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (x == -1)
1477 return NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001478 return PyLong_FromSsize_t(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001479}
1480
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001481
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001482/*[clinic input]
1483hex as builtin_hex
1484
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001485 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001486 /
1487
1488Return the hexadecimal representation of an integer.
1489
1490 >>> hex(12648430)
1491 '0xc0ffee'
1492[clinic start generated code]*/
1493
Guido van Rossum79f25d91997-04-29 20:08:16 +00001494static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001495builtin_hex(PyObject *module, PyObject *number)
1496/*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001497{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001498 return PyNumber_ToBase(number, 16);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001499}
1500
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001501
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001502/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001503static PyObject *
Serhiy Storchaka79342662019-01-12 08:25:41 +02001504builtin_iter(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001505{
Serhiy Storchaka79342662019-01-12 08:25:41 +02001506 PyObject *v;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001507
Serhiy Storchaka79342662019-01-12 08:25:41 +02001508 if (!_PyArg_CheckPositional("iter", nargs, 1, 2))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 return NULL;
Serhiy Storchaka79342662019-01-12 08:25:41 +02001510 v = args[0];
1511 if (nargs == 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 return PyObject_GetIter(v);
1513 if (!PyCallable_Check(v)) {
1514 PyErr_SetString(PyExc_TypeError,
1515 "iter(v, w): v must be callable");
1516 return NULL;
1517 }
Serhiy Storchaka79342662019-01-12 08:25:41 +02001518 PyObject *sentinel = args[1];
1519 return PyCallIter_New(v, sentinel);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001520}
1521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(iter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +00001523"iter(iterable) -> iterator\n\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001524iter(callable, sentinel) -> iterator\n\
1525\n\
1526Get an iterator from an object. In the first form, the argument must\n\
1527supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001529
1530
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001531/*[clinic input]
1532len as builtin_len
1533
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001534 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001535 /
1536
1537Return the number of items in a container.
1538[clinic start generated code]*/
1539
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001540static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001541builtin_len(PyObject *module, PyObject *obj)
1542/*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001545
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001546 res = PyObject_Size(obj);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001547 if (res < 0) {
1548 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 return NULL;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001550 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return PyLong_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001552}
1553
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001554
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001555/*[clinic input]
1556locals as builtin_locals
1557
1558Return a dictionary containing the current scope's local variables.
1559
1560NOTE: Whether or not updates to this dictionary will affect name lookups in
1561the local scope and vice-versa is *implementation dependent* and not
1562covered by any backwards compatibility guarantees.
1563[clinic start generated code]*/
1564
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001565static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001566builtin_locals_impl(PyObject *module)
1567/*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 d = PyEval_GetLocals();
1572 Py_XINCREF(d);
1573 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001574}
1575
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001576
Guido van Rossum79f25d91997-04-29 20:08:16 +00001577static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001578min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001581 PyObject *emptytuple, *defaultval = NULL;
1582 static char *kwlist[] = {"key", "default", NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 const char *name = op == Py_LT ? "min" : "max";
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001584 const int positional = PyTuple_Size(args) > 1;
1585 int ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001586
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001587 if (positional)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 v = args;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001589 else if (!PyArg_UnpackTuple(args, name, 1, 1, &v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001591
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001592 emptytuple = PyTuple_New(0);
1593 if (emptytuple == NULL)
1594 return NULL;
Oren Milman58cf7482017-08-21 20:19:07 +03001595 ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds,
1596 (op == Py_LT) ? "|$OO:min" : "|$OO:max",
1597 kwlist, &keyfunc, &defaultval);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001598 Py_DECREF(emptytuple);
1599 if (!ret)
1600 return NULL;
1601
1602 if (positional && defaultval != NULL) {
1603 PyErr_Format(PyExc_TypeError,
1604 "Cannot specify a default for %s() with multiple "
1605 "positional arguments", name);
1606 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 it = PyObject_GetIter(v);
1610 if (it == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 return NULL;
1612 }
Tim Petersc3074532001-05-03 07:00:32 +00001613
Alexander Marshalove22072f2018-07-24 10:58:21 +07001614 if (keyfunc == Py_None) {
1615 keyfunc = NULL;
1616 }
1617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 maxitem = NULL; /* the result */
1619 maxval = NULL; /* the value associated with the result */
1620 while (( item = PyIter_Next(it) )) {
1621 /* get the value from the key function */
1622 if (keyfunc != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001623 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (val == NULL)
1625 goto Fail_it_item;
1626 }
1627 /* no key function; the value is the item */
1628 else {
1629 val = item;
1630 Py_INCREF(val);
1631 }
Tim Petersc3074532001-05-03 07:00:32 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 /* maximum value and item are unset; set them */
1634 if (maxval == NULL) {
1635 maxitem = item;
1636 maxval = val;
1637 }
1638 /* maximum value and item are set; update them as necessary */
1639 else {
1640 int cmp = PyObject_RichCompareBool(val, maxval, op);
1641 if (cmp < 0)
1642 goto Fail_it_item_and_val;
1643 else if (cmp > 0) {
1644 Py_DECREF(maxval);
1645 Py_DECREF(maxitem);
1646 maxval = val;
1647 maxitem = item;
1648 }
1649 else {
1650 Py_DECREF(item);
1651 Py_DECREF(val);
1652 }
1653 }
1654 }
1655 if (PyErr_Occurred())
1656 goto Fail_it;
1657 if (maxval == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 assert(maxitem == NULL);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001659 if (defaultval != NULL) {
1660 Py_INCREF(defaultval);
1661 maxitem = defaultval;
1662 } else {
1663 PyErr_Format(PyExc_ValueError,
1664 "%s() arg is an empty sequence", name);
1665 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 }
1667 else
1668 Py_DECREF(maxval);
1669 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 return maxitem;
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001671
1672Fail_it_item_and_val:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 Py_DECREF(val);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001674Fail_it_item:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 Py_DECREF(item);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001676Fail_it:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 Py_XDECREF(maxval);
1678 Py_XDECREF(maxitem);
1679 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001681}
1682
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001683/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001684static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001685builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001688}
1689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001690PyDoc_STRVAR(min_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001691"min(iterable, *[, default=obj, key=func]) -> value\n\
1692min(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001693\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001694With a single iterable argument, return its smallest item. The\n\
1695default keyword-only argument specifies an object to return if\n\
1696the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001697With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001698
1699
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001700/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001701static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001702builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001705}
1706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001707PyDoc_STRVAR(max_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001708"max(iterable, *[, default=obj, key=func]) -> value\n\
1709max(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001710\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001711With a single iterable argument, return its biggest item. The\n\
1712default keyword-only argument specifies an object to return if\n\
1713the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001714With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001715
1716
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001717/*[clinic input]
1718oct as builtin_oct
1719
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001720 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001721 /
1722
1723Return the octal representation of an integer.
1724
1725 >>> oct(342391)
1726 '0o1234567'
1727[clinic start generated code]*/
1728
Guido van Rossum79f25d91997-04-29 20:08:16 +00001729static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001730builtin_oct(PyObject *module, PyObject *number)
1731/*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001732{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001733 return PyNumber_ToBase(number, 8);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001734}
1735
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001736
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001737/*[clinic input]
1738ord as builtin_ord
1739
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001740 c: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001741 /
1742
1743Return the Unicode code point for a one-character string.
1744[clinic start generated code]*/
1745
Guido van Rossum79f25d91997-04-29 20:08:16 +00001746static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001747builtin_ord(PyObject *module, PyObject *c)
1748/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 long ord;
1751 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001752
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001753 if (PyBytes_Check(c)) {
1754 size = PyBytes_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001756 ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 return PyLong_FromLong(ord);
1758 }
1759 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001760 else if (PyUnicode_Check(c)) {
1761 if (PyUnicode_READY(c) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001762 return NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001763 size = PyUnicode_GET_LENGTH(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001765 ord = (long)PyUnicode_READ_CHAR(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 return PyLong_FromLong(ord);
1767 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001769 else if (PyByteArray_Check(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 /* XXX Hopefully this is temporary */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001771 size = PyByteArray_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001773 ord = (long)((unsigned char)*PyByteArray_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 return PyLong_FromLong(ord);
1775 }
1776 }
1777 else {
1778 PyErr_Format(PyExc_TypeError,
1779 "ord() expected string of length 1, but " \
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001780 "%.200s found", c->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 return NULL;
1782 }
Guido van Rossum09095f32000-03-10 23:00:52 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 PyErr_Format(PyExc_TypeError,
1785 "ord() expected a character, "
1786 "but string of length %zd found",
1787 size);
1788 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001789}
1790
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001791
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001792/*[clinic input]
1793pow as builtin_pow
1794
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001795 x: object
1796 y: object
1797 z: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001798 /
1799
1800Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
1801
1802Some types, such as ints, are able to use a more efficient algorithm when
1803invoked using the three argument form.
1804[clinic start generated code]*/
1805
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001806static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001807builtin_pow_impl(PyObject *module, PyObject *x, PyObject *y, PyObject *z)
1808/*[clinic end generated code: output=50a14d5d130d404b input=653d57d38d41fc07]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001809{
1810 return PyNumber_Power(x, y, z);
1811}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001812
1813
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001814/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum34343512006-11-30 22:13:52 +00001815static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001816builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Guido van Rossum34343512006-11-30 22:13:52 +00001817{
INADA Naokibd584f12017-01-19 12:50:34 +01001818 static const char * const _keywords[] = {"sep", "end", "file", "flush", 0};
1819 static struct _PyArg_Parser _parser = {"|OOOO:print", _keywords, 0};
Georg Brandlbc3b6822012-01-13 19:41:25 +01001820 PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 int i, err;
Guido van Rossum34343512006-11-30 22:13:52 +00001822
INADA Naokibd584f12017-01-19 12:50:34 +01001823 if (kwnames != NULL &&
1824 !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser,
1825 &sep, &end, &file, &flush)) {
Georg Brandlbc3b6822012-01-13 19:41:25 +01001826 return NULL;
INADA Naokibd584f12017-01-19 12:50:34 +01001827 }
1828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 if (file == NULL || file == Py_None) {
Victor Stinnerbd303c12013-11-07 23:07:29 +01001830 file = _PySys_GetObjectId(&PyId_stdout);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001831 if (file == NULL) {
1832 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1833 return NULL;
1834 }
1835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 /* sys.stdout may be None when FILE* stdout isn't connected */
1837 if (file == Py_None)
1838 Py_RETURN_NONE;
1839 }
Guido van Rossum34343512006-11-30 22:13:52 +00001840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 if (sep == Py_None) {
1842 sep = NULL;
1843 }
1844 else if (sep && !PyUnicode_Check(sep)) {
1845 PyErr_Format(PyExc_TypeError,
1846 "sep must be None or a string, not %.200s",
1847 sep->ob_type->tp_name);
1848 return NULL;
1849 }
1850 if (end == Py_None) {
1851 end = NULL;
1852 }
1853 else if (end && !PyUnicode_Check(end)) {
1854 PyErr_Format(PyExc_TypeError,
1855 "end must be None or a string, not %.200s",
1856 end->ob_type->tp_name);
1857 return NULL;
1858 }
Guido van Rossum34343512006-11-30 22:13:52 +00001859
INADA Naokibd584f12017-01-19 12:50:34 +01001860 for (i = 0; i < nargs; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 if (i > 0) {
1862 if (sep == NULL)
1863 err = PyFile_WriteString(" ", file);
1864 else
1865 err = PyFile_WriteObject(sep, file,
1866 Py_PRINT_RAW);
1867 if (err)
1868 return NULL;
1869 }
INADA Naokibd584f12017-01-19 12:50:34 +01001870 err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 if (err)
1872 return NULL;
1873 }
Guido van Rossum34343512006-11-30 22:13:52 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 if (end == NULL)
1876 err = PyFile_WriteString("\n", file);
1877 else
1878 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1879 if (err)
1880 return NULL;
Guido van Rossum34343512006-11-30 22:13:52 +00001881
Georg Brandlbc3b6822012-01-13 19:41:25 +01001882 if (flush != NULL) {
1883 PyObject *tmp;
1884 int do_flush = PyObject_IsTrue(flush);
1885 if (do_flush == -1)
1886 return NULL;
1887 else if (do_flush) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001888 tmp = _PyObject_CallMethodId(file, &PyId_flush, NULL);
Georg Brandlbc3b6822012-01-13 19:41:25 +01001889 if (tmp == NULL)
1890 return NULL;
1891 else
1892 Py_DECREF(tmp);
1893 }
1894 }
1895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 Py_RETURN_NONE;
Guido van Rossum34343512006-11-30 22:13:52 +00001897}
1898
1899PyDoc_STRVAR(print_doc,
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001900"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\
Guido van Rossum34343512006-11-30 22:13:52 +00001901\n\
1902Prints the values to a stream, or to sys.stdout by default.\n\
1903Optional keyword arguments:\n\
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001904file: a file-like object (stream); defaults to the current sys.stdout.\n\
1905sep: string inserted between values, default a space.\n\
1906end: string appended after the last value, default a newline.\n\
1907flush: whether to forcibly flush the stream.");
Guido van Rossum34343512006-11-30 22:13:52 +00001908
1909
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001910/*[clinic input]
1911input as builtin_input
1912
1913 prompt: object(c_default="NULL") = None
1914 /
1915
1916Read a string from standard input. The trailing newline is stripped.
1917
1918The prompt string, if given, is printed to standard output without a
1919trailing newline before reading input.
1920
1921If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
1922On *nix systems, readline is used if available.
1923[clinic start generated code]*/
1924
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001925static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001926builtin_input_impl(PyObject *module, PyObject *prompt)
1927/*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001928{
Victor Stinnerbd303c12013-11-07 23:07:29 +01001929 PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
1930 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1931 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 PyObject *tmp;
1933 long fd;
1934 int tty;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 /* Check that stdin/out/err are intact */
1937 if (fin == NULL || fin == Py_None) {
1938 PyErr_SetString(PyExc_RuntimeError,
1939 "input(): lost sys.stdin");
1940 return NULL;
1941 }
1942 if (fout == NULL || fout == Py_None) {
1943 PyErr_SetString(PyExc_RuntimeError,
1944 "input(): lost sys.stdout");
1945 return NULL;
1946 }
1947 if (ferr == NULL || ferr == Py_None) {
1948 PyErr_SetString(PyExc_RuntimeError,
1949 "input(): lost sys.stderr");
1950 return NULL;
1951 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001952
Steve Dowerb82e17e2019-05-23 08:45:22 -07001953 if (PySys_Audit("builtins.input", "O", prompt ? prompt : Py_None) < 0) {
1954 return NULL;
1955 }
1956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 /* First of all, flush stderr */
Victor Stinner3466bde2016-09-05 18:16:01 -07001958 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 if (tmp == NULL)
1960 PyErr_Clear();
1961 else
1962 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 /* We should only use (GNU) readline if Python's sys.stdin and
1965 sys.stdout are the same as C's stdin and stdout, because we
1966 need to pass it those. */
Victor Stinner3466bde2016-09-05 18:16:01 -07001967 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 if (tmp == NULL) {
1969 PyErr_Clear();
1970 tty = 0;
1971 }
1972 else {
1973 fd = PyLong_AsLong(tmp);
1974 Py_DECREF(tmp);
1975 if (fd < 0 && PyErr_Occurred())
1976 return NULL;
1977 tty = fd == fileno(stdin) && isatty(fd);
1978 }
1979 if (tty) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001980 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
Martin Panterc9a6ab52015-10-10 01:25:38 +00001981 if (tmp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 PyErr_Clear();
Martin Panterc9a6ab52015-10-10 01:25:38 +00001983 tty = 0;
1984 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 else {
1986 fd = PyLong_AsLong(tmp);
1987 Py_DECREF(tmp);
1988 if (fd < 0 && PyErr_Occurred())
1989 return NULL;
1990 tty = fd == fileno(stdout) && isatty(fd);
1991 }
1992 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 /* If we're interactive, use (GNU) readline */
1995 if (tty) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01001996 PyObject *po = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001997 const char *promptstr;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01001998 char *s = NULL;
1999 PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
2000 PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002001 const char *stdin_encoding_str, *stdin_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 PyObject *result;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002003 size_t len;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00002004
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002005 /* stdin is a text stream, so it must have an encoding. */
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002006 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002007 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002008 if (!stdin_encoding || !stdin_errors ||
2009 !PyUnicode_Check(stdin_encoding) ||
2010 !PyUnicode_Check(stdin_errors)) {
2011 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002012 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002013 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002014 stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2015 stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002016 if (!stdin_encoding_str || !stdin_errors_str)
2017 goto _readline_errors;
Victor Stinner3466bde2016-09-05 18:16:01 -07002018 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 if (tmp == NULL)
2020 PyErr_Clear();
2021 else
2022 Py_DECREF(tmp);
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002023 if (prompt != NULL) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002024 /* We have a prompt, encode it as stdout would */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002025 const char *stdout_encoding_str, *stdout_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 PyObject *stringpo;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002027 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002028 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002029 if (!stdout_encoding || !stdout_errors ||
2030 !PyUnicode_Check(stdout_encoding) ||
2031 !PyUnicode_Check(stdout_errors)) {
2032 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002033 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002034 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002035 stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2036 stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002037 if (!stdout_encoding_str || !stdout_errors_str)
2038 goto _readline_errors;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002039 stringpo = PyObject_Str(prompt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002040 if (stringpo == NULL)
2041 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 po = PyUnicode_AsEncodedString(stringpo,
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002043 stdout_encoding_str, stdout_errors_str);
2044 Py_CLEAR(stdout_encoding);
2045 Py_CLEAR(stdout_errors);
2046 Py_CLEAR(stringpo);
2047 if (po == NULL)
2048 goto _readline_errors;
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03002049 assert(PyBytes_Check(po));
2050 promptstr = PyBytes_AS_STRING(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 }
2052 else {
2053 po = NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002054 promptstr = "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002056 s = PyOS_Readline(stdin, stdout, promptstr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 if (s == NULL) {
Richard Oudkerk614c5782013-04-03 13:44:50 +01002058 PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 if (!PyErr_Occurred())
2060 PyErr_SetNone(PyExc_KeyboardInterrupt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002061 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002063
2064 len = strlen(s);
2065 if (len == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 PyErr_SetNone(PyExc_EOFError);
2067 result = NULL;
2068 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002069 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 if (len > PY_SSIZE_T_MAX) {
2071 PyErr_SetString(PyExc_OverflowError,
2072 "input: input too long");
2073 result = NULL;
2074 }
2075 else {
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002076 len--; /* strip trailing '\n' */
2077 if (len != 0 && s[len-1] == '\r')
2078 len--; /* strip trailing '\r' */
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002079 result = PyUnicode_Decode(s, len, stdin_encoding_str,
2080 stdin_errors_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 }
2082 }
2083 Py_DECREF(stdin_encoding);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002084 Py_DECREF(stdin_errors);
2085 Py_XDECREF(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 PyMem_FREE(s);
Steve Dowerb82e17e2019-05-23 08:45:22 -07002087
2088 if (result != NULL) {
2089 if (PySys_Audit("builtins.input/result", "O", result) < 0) {
2090 return NULL;
2091 }
2092 }
2093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 return result;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002095
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002096 _readline_errors:
2097 Py_XDECREF(stdin_encoding);
2098 Py_XDECREF(stdout_encoding);
2099 Py_XDECREF(stdin_errors);
2100 Py_XDECREF(stdout_errors);
2101 Py_XDECREF(po);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002102 if (tty)
2103 return NULL;
2104
2105 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 }
Guido van Rossumeba76962007-05-27 09:13:28 +00002107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 /* Fallback if we're not interactive */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002109 if (prompt != NULL) {
2110 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 return NULL;
2112 }
Victor Stinner3466bde2016-09-05 18:16:01 -07002113 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 if (tmp == NULL)
2115 PyErr_Clear();
2116 else
2117 Py_DECREF(tmp);
2118 return PyFile_GetLine(fin, -1);
Guido van Rossuma88a0332007-02-26 16:59:55 +00002119}
2120
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002121
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002122/*[clinic input]
2123repr as builtin_repr
2124
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002125 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002126 /
2127
2128Return the canonical string representation of the object.
2129
2130For many object types, including most builtins, eval(repr(obj)) == obj.
2131[clinic start generated code]*/
2132
Guido van Rossum79f25d91997-04-29 20:08:16 +00002133static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002134builtin_repr(PyObject *module, PyObject *obj)
2135/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
Guido van Rossumc89705d1992-11-26 08:54:07 +00002136{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002137 return PyObject_Repr(obj);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002138}
2139
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002140
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002141/*[clinic input]
2142round as builtin_round
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002143
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002144 number: object
2145 ndigits: object = NULL
2146
2147Round a number to a given precision in decimal digits.
2148
2149The return value is an integer if ndigits is omitted or None. Otherwise
2150the return value has the same type as the number. ndigits may be negative.
2151[clinic start generated code]*/
2152
2153static PyObject *
2154builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
2155/*[clinic end generated code: output=ff0d9dd176c02ede input=854bc3a217530c3d]*/
2156{
2157 PyObject *round, *result;
Alex Martelliae211f92007-08-22 23:21:33 +00002158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 if (Py_TYPE(number)->tp_dict == NULL) {
2160 if (PyType_Ready(Py_TYPE(number)) < 0)
2161 return NULL;
2162 }
Guido van Rossum15d3d042007-08-24 02:02:45 +00002163
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002164 round = _PyObject_LookupSpecial(number, &PyId___round__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 if (round == NULL) {
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002166 if (!PyErr_Occurred())
2167 PyErr_Format(PyExc_TypeError,
2168 "type %.100s doesn't define __round__ method",
2169 Py_TYPE(number)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 return NULL;
2171 }
Alex Martelliae211f92007-08-22 23:21:33 +00002172
Raymond Hettingerf0f1c232016-09-03 01:55:11 -07002173 if (ndigits == NULL || ndigits == Py_None)
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002174 result = _PyObject_CallNoArg(round);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 else
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002176 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002177 Py_DECREF(round);
2178 return result;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002179}
2180
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002181
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002182/*AC: we need to keep the kwds dict intact to easily call into the
2183 * list.sort method, which isn't currently supported in AC. So we just use
2184 * the initially generated signature with a custom implementation.
2185 */
2186/* [disabled clinic input]
2187sorted as builtin_sorted
2188
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002189 iterable as seq: object
2190 key as keyfunc: object = None
2191 reverse: object = False
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002192
2193Return a new list containing all items from the iterable in ascending order.
2194
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002195A custom key function can be supplied to customize the sort order, and the
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002196reverse flag can be set to request the result in descending order.
2197[end disabled clinic input]*/
2198
2199PyDoc_STRVAR(builtin_sorted__doc__,
Serhiy Storchaka3a104252017-01-23 12:29:47 +02002200"sorted($module, iterable, /, *, key=None, reverse=False)\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002201"--\n"
2202"\n"
2203"Return a new list containing all items from the iterable in ascending order.\n"
2204"\n"
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002205"A custom key function can be supplied to customize the sort order, and the\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002206"reverse flag can be set to request the result in descending order.");
2207
2208#define BUILTIN_SORTED_METHODDEF \
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002209 {"sorted", (PyCFunction)(void(*)(void))builtin_sorted, METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002210
Raymond Hettinger64958a12003-12-17 20:43:33 +00002211static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002212builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Raymond Hettinger64958a12003-12-17 20:43:33 +00002213{
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002214 PyObject *newlist, *v, *seq, *callable;
Victor Stinner5a60eca2017-01-17 15:17:49 +01002215
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002216 /* Keyword arguments are passed through list.sort() which will check
2217 them. */
2218 if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 newlist = PySequence_List(seq);
2222 if (newlist == NULL)
2223 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002224
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002225 callable = _PyObject_GetAttrId(newlist, &PyId_sort);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 if (callable == NULL) {
2227 Py_DECREF(newlist);
2228 return NULL;
2229 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002230
Serhiy Storchaka299dc232017-01-20 08:35:18 +02002231 assert(nargs >= 1);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02002232 v = _PyObject_Vectorcall(callable, args + 1, nargs - 1, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 Py_DECREF(callable);
2234 if (v == NULL) {
2235 Py_DECREF(newlist);
2236 return NULL;
2237 }
2238 Py_DECREF(v);
2239 return newlist;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002240}
2241
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002242
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002243/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002244static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002245builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 PyObject *v = NULL;
2248 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2251 return NULL;
2252 if (v == NULL) {
2253 d = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01002254 if (d == NULL)
2255 return NULL;
2256 Py_INCREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 }
2258 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002259 d = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 if (d == NULL) {
2261 PyErr_SetString(PyExc_TypeError,
2262 "vars() argument must have __dict__ attribute");
2263 return NULL;
2264 }
2265 }
2266 return d;
Guido van Rossum2d951851994-08-29 12:52:16 +00002267}
2268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002269PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002270"vars([object]) -> dictionary\n\
2271\n\
2272Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002273With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002274
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002275
2276/*[clinic input]
2277sum as builtin_sum
2278
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002279 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002280 /
Raymond Hettinger9dfa0fe2018-09-12 10:54:06 -07002281 start: object(c_default="NULL") = 0
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002282
2283Return the sum of a 'start' value (default: 0) plus an iterable of numbers
2284
2285When the iterable is empty, return the start value.
2286This function is intended specifically for use with numeric values and may
2287reject non-numeric types.
2288[clinic start generated code]*/
2289
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002290static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002291builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
Raymond Hettinger9dfa0fe2018-09-12 10:54:06 -07002292/*[clinic end generated code: output=df758cec7d1d302f input=162b50765250d222]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002293{
2294 PyObject *result = start;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 PyObject *temp, *item, *iter;
Alex Martellia70b1912003-04-22 08:12:33 +00002296
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002297 iter = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if (iter == NULL)
2299 return NULL;
Alex Martellia70b1912003-04-22 08:12:33 +00002300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (result == NULL) {
2302 result = PyLong_FromLong(0);
2303 if (result == NULL) {
2304 Py_DECREF(iter);
2305 return NULL;
2306 }
2307 } else {
2308 /* reject string values for 'start' parameter */
2309 if (PyUnicode_Check(result)) {
2310 PyErr_SetString(PyExc_TypeError,
2311 "sum() can't sum strings [use ''.join(seq) instead]");
2312 Py_DECREF(iter);
2313 return NULL;
2314 }
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002315 if (PyBytes_Check(result)) {
2316 PyErr_SetString(PyExc_TypeError,
2317 "sum() can't sum bytes [use b''.join(seq) instead]");
Benjamin Peterson405f32c2011-07-29 22:43:45 -05002318 Py_DECREF(iter);
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002319 return NULL;
2320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 if (PyByteArray_Check(result)) {
2322 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson4f921c22011-07-29 14:24:29 -05002323 "sum() can't sum bytearray [use b''.join(seq) instead]");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 Py_DECREF(iter);
2325 return NULL;
2326 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 Py_INCREF(result);
2328 }
Alex Martellia70b1912003-04-22 08:12:33 +00002329
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002330#ifndef SLOW_SUM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2332 Assumes all inputs are the same type. If the assumption fails, default
2333 to the more general routine.
2334 */
2335 if (PyLong_CheckExact(result)) {
2336 int overflow;
2337 long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2338 /* If this already overflowed, don't even enter the loop. */
2339 if (overflow == 0) {
2340 Py_DECREF(result);
2341 result = NULL;
2342 }
2343 while(result == NULL) {
2344 item = PyIter_Next(iter);
2345 if (item == NULL) {
2346 Py_DECREF(iter);
2347 if (PyErr_Occurred())
2348 return NULL;
2349 return PyLong_FromLong(i_result);
2350 }
2351 if (PyLong_CheckExact(item)) {
2352 long b = PyLong_AsLongAndOverflow(item, &overflow);
Serhiy Storchaka29500732019-05-05 14:26:23 +03002353 if (overflow == 0 &&
2354 (i_result >= 0 ? (b <= LONG_MAX - i_result)
2355 : (b >= LONG_MIN - i_result)))
2356 {
2357 i_result += b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 Py_DECREF(item);
2359 continue;
2360 }
2361 }
2362 /* Either overflowed or is not an int. Restore real objects and process normally */
2363 result = PyLong_FromLong(i_result);
Christian Heimes704e2d32013-07-26 22:49:26 +02002364 if (result == NULL) {
2365 Py_DECREF(item);
2366 Py_DECREF(iter);
2367 return NULL;
2368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 temp = PyNumber_Add(result, item);
2370 Py_DECREF(result);
2371 Py_DECREF(item);
2372 result = temp;
2373 if (result == NULL) {
2374 Py_DECREF(iter);
2375 return NULL;
2376 }
2377 }
2378 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 if (PyFloat_CheckExact(result)) {
2381 double f_result = PyFloat_AS_DOUBLE(result);
2382 Py_DECREF(result);
2383 result = NULL;
2384 while(result == NULL) {
2385 item = PyIter_Next(iter);
2386 if (item == NULL) {
2387 Py_DECREF(iter);
2388 if (PyErr_Occurred())
2389 return NULL;
2390 return PyFloat_FromDouble(f_result);
2391 }
2392 if (PyFloat_CheckExact(item)) {
2393 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2394 f_result += PyFloat_AS_DOUBLE(item);
2395 PyFPE_END_PROTECT(f_result)
2396 Py_DECREF(item);
2397 continue;
2398 }
2399 if (PyLong_CheckExact(item)) {
2400 long value;
2401 int overflow;
2402 value = PyLong_AsLongAndOverflow(item, &overflow);
2403 if (!overflow) {
2404 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2405 f_result += (double)value;
2406 PyFPE_END_PROTECT(f_result)
2407 Py_DECREF(item);
2408 continue;
2409 }
2410 }
2411 result = PyFloat_FromDouble(f_result);
Alexey Izbyshev2b824b22018-08-24 07:27:52 +03002412 if (result == NULL) {
2413 Py_DECREF(item);
2414 Py_DECREF(iter);
2415 return NULL;
2416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 temp = PyNumber_Add(result, item);
2418 Py_DECREF(result);
2419 Py_DECREF(item);
2420 result = temp;
2421 if (result == NULL) {
2422 Py_DECREF(iter);
2423 return NULL;
2424 }
2425 }
2426 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002427#endif
2428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 for(;;) {
2430 item = PyIter_Next(iter);
2431 if (item == NULL) {
2432 /* error, or end-of-sequence */
2433 if (PyErr_Occurred()) {
2434 Py_DECREF(result);
2435 result = NULL;
2436 }
2437 break;
2438 }
2439 /* It's tempting to use PyNumber_InPlaceAdd instead of
2440 PyNumber_Add here, to avoid quadratic running time
2441 when doing 'sum(list_of_lists, [])'. However, this
2442 would produce a change in behaviour: a snippet like
Mark Dickinson9acadc52009-10-26 14:19:42 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 empty = []
2445 sum([[x] for x in range(10)], empty)
Mark Dickinson9acadc52009-10-26 14:19:42 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 would change the value of empty. */
2448 temp = PyNumber_Add(result, item);
2449 Py_DECREF(result);
2450 Py_DECREF(item);
2451 result = temp;
2452 if (result == NULL)
2453 break;
2454 }
2455 Py_DECREF(iter);
2456 return result;
Alex Martellia70b1912003-04-22 08:12:33 +00002457}
2458
Alex Martellia70b1912003-04-22 08:12:33 +00002459
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002460/*[clinic input]
2461isinstance as builtin_isinstance
2462
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002463 obj: object
2464 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002465 /
2466
2467Return whether an object is an instance of a class or of a subclass thereof.
2468
2469A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
2470check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
2471or ...`` etc.
2472[clinic start generated code]*/
2473
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002474static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002475builtin_isinstance_impl(PyObject *module, PyObject *obj,
Larry Hastings89964c42015-04-14 18:07:59 -04002476 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002477/*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002480
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002481 retval = PyObject_IsInstance(obj, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 if (retval < 0)
2483 return NULL;
2484 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002485}
2486
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002487
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002488/*[clinic input]
2489issubclass as builtin_issubclass
2490
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002491 cls: object
2492 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002493 /
2494
2495Return whether 'cls' is a derived from another class or is the same class.
2496
2497A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
2498check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
2499or ...`` etc.
2500[clinic start generated code]*/
2501
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002502static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002503builtin_issubclass_impl(PyObject *module, PyObject *cls,
Larry Hastings89964c42015-04-14 18:07:59 -04002504 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002505/*[clinic end generated code: output=358412410cd7a250 input=af5f35e9ceaddaf6]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002508
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002509 retval = PyObject_IsSubclass(cls, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 if (retval < 0)
2511 return NULL;
2512 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002513}
2514
2515
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002516typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 PyObject_HEAD
2518 Py_ssize_t tuplesize;
2519 PyObject *ittuple; /* tuple of iterators */
2520 PyObject *result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002521} zipobject;
2522
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002523static PyObject *
2524zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Barry Warsawbd599b52000-08-03 15:45:29 +00002525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 zipobject *lz;
2527 Py_ssize_t i;
2528 PyObject *ittuple; /* tuple of iterators */
2529 PyObject *result;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002530 Py_ssize_t tuplesize;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002531
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002532 if (type == &PyZip_Type && !_PyArg_NoKeywords("zip", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 return NULL;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 /* args must be a tuple */
2536 assert(PyTuple_Check(args));
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002537 tuplesize = PyTuple_GET_SIZE(args);
Barry Warsawbd599b52000-08-03 15:45:29 +00002538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 /* obtain iterators */
2540 ittuple = PyTuple_New(tuplesize);
2541 if (ittuple == NULL)
2542 return NULL;
2543 for (i=0; i < tuplesize; ++i) {
2544 PyObject *item = PyTuple_GET_ITEM(args, i);
2545 PyObject *it = PyObject_GetIter(item);
2546 if (it == NULL) {
2547 if (PyErr_ExceptionMatches(PyExc_TypeError))
2548 PyErr_Format(PyExc_TypeError,
2549 "zip argument #%zd must support iteration",
2550 i+1);
2551 Py_DECREF(ittuple);
2552 return NULL;
2553 }
2554 PyTuple_SET_ITEM(ittuple, i, it);
2555 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 /* create a result holder */
2558 result = PyTuple_New(tuplesize);
2559 if (result == NULL) {
2560 Py_DECREF(ittuple);
2561 return NULL;
2562 }
2563 for (i=0 ; i < tuplesize ; i++) {
2564 Py_INCREF(Py_None);
2565 PyTuple_SET_ITEM(result, i, Py_None);
2566 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 /* create zipobject structure */
2569 lz = (zipobject *)type->tp_alloc(type, 0);
2570 if (lz == NULL) {
2571 Py_DECREF(ittuple);
2572 Py_DECREF(result);
2573 return NULL;
2574 }
2575 lz->ittuple = ittuple;
2576 lz->tuplesize = tuplesize;
2577 lz->result = result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 return (PyObject *)lz;
Barry Warsawbd599b52000-08-03 15:45:29 +00002580}
2581
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002582static void
2583zip_dealloc(zipobject *lz)
2584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 PyObject_GC_UnTrack(lz);
2586 Py_XDECREF(lz->ittuple);
2587 Py_XDECREF(lz->result);
2588 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002589}
2590
2591static int
2592zip_traverse(zipobject *lz, visitproc visit, void *arg)
2593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 Py_VISIT(lz->ittuple);
2595 Py_VISIT(lz->result);
2596 return 0;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002597}
2598
2599static PyObject *
2600zip_next(zipobject *lz)
2601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 Py_ssize_t i;
2603 Py_ssize_t tuplesize = lz->tuplesize;
2604 PyObject *result = lz->result;
2605 PyObject *it;
2606 PyObject *item;
2607 PyObject *olditem;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 if (tuplesize == 0)
2610 return NULL;
2611 if (Py_REFCNT(result) == 1) {
2612 Py_INCREF(result);
2613 for (i=0 ; i < tuplesize ; i++) {
2614 it = PyTuple_GET_ITEM(lz->ittuple, i);
2615 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002616 if (item == NULL) {
2617 Py_DECREF(result);
2618 return NULL;
2619 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 olditem = PyTuple_GET_ITEM(result, i);
2621 PyTuple_SET_ITEM(result, i, item);
2622 Py_DECREF(olditem);
2623 }
2624 } else {
2625 result = PyTuple_New(tuplesize);
2626 if (result == NULL)
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002627 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 for (i=0 ; i < tuplesize ; i++) {
2629 it = PyTuple_GET_ITEM(lz->ittuple, i);
2630 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002631 if (item == NULL) {
2632 Py_DECREF(result);
2633 return NULL;
2634 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 PyTuple_SET_ITEM(result, i, item);
2636 }
2637 }
2638 return result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002639}
Barry Warsawbd599b52000-08-03 15:45:29 +00002640
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002641static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302642zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002643{
2644 /* Just recreate the zip with the internal iterator tuple */
2645 return Py_BuildValue("OO", Py_TYPE(lz), lz->ittuple);
2646}
2647
2648static PyMethodDef zip_methods[] = {
2649 {"__reduce__", (PyCFunction)zip_reduce, METH_NOARGS, reduce_doc},
2650 {NULL, NULL} /* sentinel */
2651};
2652
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002653PyDoc_STRVAR(zip_doc,
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002654"zip(iter1 [,iter2 [...]]) --> zip object\n\
Barry Warsawbd599b52000-08-03 15:45:29 +00002655\n\
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002656Return a zip object whose .__next__() method returns a tuple where\n\
2657the i-th element comes from the i-th iterable argument. The .__next__()\n\
2658method continues until the shortest iterable in the argument sequence\n\
Georg Brandlced51db2008-12-04 18:28:38 +00002659is exhausted and then it raises StopIteration.");
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002660
2661PyTypeObject PyZip_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2663 "zip", /* tp_name */
2664 sizeof(zipobject), /* tp_basicsize */
2665 0, /* tp_itemsize */
2666 /* methods */
2667 (destructor)zip_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002668 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 0, /* tp_getattr */
2670 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002671 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 0, /* tp_repr */
2673 0, /* tp_as_number */
2674 0, /* tp_as_sequence */
2675 0, /* tp_as_mapping */
2676 0, /* tp_hash */
2677 0, /* tp_call */
2678 0, /* tp_str */
2679 PyObject_GenericGetAttr, /* tp_getattro */
2680 0, /* tp_setattro */
2681 0, /* tp_as_buffer */
2682 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2683 Py_TPFLAGS_BASETYPE, /* tp_flags */
2684 zip_doc, /* tp_doc */
2685 (traverseproc)zip_traverse, /* tp_traverse */
2686 0, /* tp_clear */
2687 0, /* tp_richcompare */
2688 0, /* tp_weaklistoffset */
2689 PyObject_SelfIter, /* tp_iter */
2690 (iternextfunc)zip_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002691 zip_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 0, /* tp_members */
2693 0, /* tp_getset */
2694 0, /* tp_base */
2695 0, /* tp_dict */
2696 0, /* tp_descr_get */
2697 0, /* tp_descr_set */
2698 0, /* tp_dictoffset */
2699 0, /* tp_init */
2700 PyType_GenericAlloc, /* tp_alloc */
2701 zip_new, /* tp_new */
2702 PyObject_GC_Del, /* tp_free */
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002703};
Barry Warsawbd599b52000-08-03 15:45:29 +00002704
2705
Guido van Rossum79f25d91997-04-29 20:08:16 +00002706static PyMethodDef builtin_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002707 {"__build_class__", (PyCFunction)(void(*)(void))builtin___build_class__,
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002708 METH_FASTCALL | METH_KEYWORDS, build_class_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002709 {"__import__", (PyCFunction)(void(*)(void))builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002710 BUILTIN_ABS_METHODDEF
2711 BUILTIN_ALL_METHODDEF
2712 BUILTIN_ANY_METHODDEF
2713 BUILTIN_ASCII_METHODDEF
2714 BUILTIN_BIN_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002715 {"breakpoint", (PyCFunction)(void(*)(void))builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002716 BUILTIN_CALLABLE_METHODDEF
2717 BUILTIN_CHR_METHODDEF
2718 BUILTIN_COMPILE_METHODDEF
2719 BUILTIN_DELATTR_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 {"dir", builtin_dir, METH_VARARGS, dir_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002721 BUILTIN_DIVMOD_METHODDEF
2722 BUILTIN_EVAL_METHODDEF
2723 BUILTIN_EXEC_METHODDEF
2724 BUILTIN_FORMAT_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002725 {"getattr", (PyCFunction)(void(*)(void))builtin_getattr, METH_FASTCALL, getattr_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002726 BUILTIN_GLOBALS_METHODDEF
2727 BUILTIN_HASATTR_METHODDEF
2728 BUILTIN_HASH_METHODDEF
2729 BUILTIN_HEX_METHODDEF
2730 BUILTIN_ID_METHODDEF
2731 BUILTIN_INPUT_METHODDEF
2732 BUILTIN_ISINSTANCE_METHODDEF
2733 BUILTIN_ISSUBCLASS_METHODDEF
Serhiy Storchaka79342662019-01-12 08:25:41 +02002734 {"iter", (PyCFunction)(void(*)(void))builtin_iter, METH_FASTCALL, iter_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002735 BUILTIN_LEN_METHODDEF
2736 BUILTIN_LOCALS_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002737 {"max", (PyCFunction)(void(*)(void))builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2738 {"min", (PyCFunction)(void(*)(void))builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
2739 {"next", (PyCFunction)(void(*)(void))builtin_next, METH_FASTCALL, next_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002740 BUILTIN_OCT_METHODDEF
2741 BUILTIN_ORD_METHODDEF
2742 BUILTIN_POW_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002743 {"print", (PyCFunction)(void(*)(void))builtin_print, METH_FASTCALL | METH_KEYWORDS, print_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002744 BUILTIN_REPR_METHODDEF
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002745 BUILTIN_ROUND_METHODDEF
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002746 BUILTIN_SETATTR_METHODDEF
2747 BUILTIN_SORTED_METHODDEF
2748 BUILTIN_SUM_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 {"vars", builtin_vars, METH_VARARGS, vars_doc},
2750 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002751};
2752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002753PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002754"Built-in functions, exceptions, and other objects.\n\
2755\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002756Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002757
Martin v. Löwis1a214512008-06-11 05:26:20 +00002758static struct PyModuleDef builtinsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 PyModuleDef_HEAD_INIT,
2760 "builtins",
2761 builtin_doc,
2762 -1, /* multiple "initialization" just copies the module dict. */
2763 builtin_methods,
2764 NULL,
2765 NULL,
2766 NULL,
2767 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002768};
2769
2770
Guido van Rossum25ce5661997-08-02 03:10:38 +00002771PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002772_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 PyObject *mod, *dict, *debug;
Benjamin Peterson42124a72012-10-30 23:41:54 -04002775
Victor Stinner331a6a52019-05-27 16:39:22 +02002776 const PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
Victor Stinnerfbca9082018-08-30 00:50:45 +02002777
Benjamin Peterson42124a72012-10-30 23:41:54 -04002778 if (PyType_Ready(&PyFilter_Type) < 0 ||
2779 PyType_Ready(&PyMap_Type) < 0 ||
2780 PyType_Ready(&PyZip_Type) < 0)
2781 return NULL;
2782
Eric Snowd393c1b2017-09-14 12:18:12 -06002783 mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 if (mod == NULL)
2785 return NULL;
2786 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002787
Tim Peters7571a0f2003-03-23 17:52:28 +00002788#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 /* "builtins" exposes a number of statically allocated objects
2790 * that, before this code was added in 2.3, never showed up in
2791 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2792 * result, programs leaking references to None and False (etc)
2793 * couldn't be diagnosed by examining sys.getobjects(0).
2794 */
Tim Peters7571a0f2003-03-23 17:52:28 +00002795#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2796#else
2797#define ADD_TO_ALL(OBJECT) (void)0
2798#endif
2799
Tim Peters4b7625e2001-09-13 21:37:17 +00002800#define SETBUILTIN(NAME, OBJECT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2802 return NULL; \
2803 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 SETBUILTIN("None", Py_None);
2806 SETBUILTIN("Ellipsis", Py_Ellipsis);
2807 SETBUILTIN("NotImplemented", Py_NotImplemented);
2808 SETBUILTIN("False", Py_False);
2809 SETBUILTIN("True", Py_True);
2810 SETBUILTIN("bool", &PyBool_Type);
2811 SETBUILTIN("memoryview", &PyMemoryView_Type);
2812 SETBUILTIN("bytearray", &PyByteArray_Type);
2813 SETBUILTIN("bytes", &PyBytes_Type);
2814 SETBUILTIN("classmethod", &PyClassMethod_Type);
2815 SETBUILTIN("complex", &PyComplex_Type);
2816 SETBUILTIN("dict", &PyDict_Type);
2817 SETBUILTIN("enumerate", &PyEnum_Type);
2818 SETBUILTIN("filter", &PyFilter_Type);
2819 SETBUILTIN("float", &PyFloat_Type);
2820 SETBUILTIN("frozenset", &PyFrozenSet_Type);
2821 SETBUILTIN("property", &PyProperty_Type);
2822 SETBUILTIN("int", &PyLong_Type);
2823 SETBUILTIN("list", &PyList_Type);
2824 SETBUILTIN("map", &PyMap_Type);
2825 SETBUILTIN("object", &PyBaseObject_Type);
2826 SETBUILTIN("range", &PyRange_Type);
2827 SETBUILTIN("reversed", &PyReversed_Type);
2828 SETBUILTIN("set", &PySet_Type);
2829 SETBUILTIN("slice", &PySlice_Type);
2830 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2831 SETBUILTIN("str", &PyUnicode_Type);
2832 SETBUILTIN("super", &PySuper_Type);
2833 SETBUILTIN("tuple", &PyTuple_Type);
2834 SETBUILTIN("type", &PyType_Type);
2835 SETBUILTIN("zip", &PyZip_Type);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002836 debug = PyBool_FromLong(config->optimization_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002838 Py_DECREF(debug);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 return NULL;
2840 }
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002841 Py_DECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002844#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002845#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002846}