blob: e19bc5604ba10932b5f7687849827ab9668bb54e [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"
Guido van Rossum6bf62da1997-04-11 20:37:35 +00008
Victor Stinnerbd303c12013-11-07 23:07:29 +01009_Py_IDENTIFIER(__builtins__);
10_Py_IDENTIFIER(__dict__);
11_Py_IDENTIFIER(__prepare__);
12_Py_IDENTIFIER(__round__);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010013_Py_IDENTIFIER(__mro_entries__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010014_Py_IDENTIFIER(encoding);
15_Py_IDENTIFIER(errors);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020016_Py_IDENTIFIER(fileno);
17_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010018_Py_IDENTIFIER(metaclass);
19_Py_IDENTIFIER(sort);
20_Py_IDENTIFIER(stdin);
21_Py_IDENTIFIER(stdout);
22_Py_IDENTIFIER(stderr);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020023
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030024#include "clinic/bltinmodule.c.h"
25
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010026static PyObject*
Victor Stinner05d68a82018-01-18 11:15:25 +010027update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010028{
Victor Stinner05d68a82018-01-18 11:15:25 +010029 Py_ssize_t i, j;
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010030 PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
31 PyObject *stack[1] = {bases};
32 assert(PyTuple_Check(bases));
33
34 for (i = 0; i < nargs; i++) {
35 base = args[i];
36 if (PyType_Check(base)) {
37 if (new_bases) {
38 /* If we already have made a replacement, then we append every normal base,
39 otherwise just skip it. */
40 if (PyList_Append(new_bases, base) < 0) {
41 goto error;
42 }
43 }
44 continue;
45 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +020046 if (_PyObject_LookupAttrId(base, &PyId___mro_entries__, &meth) < 0) {
47 goto error;
48 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010049 if (!meth) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010050 if (new_bases) {
51 if (PyList_Append(new_bases, base) < 0) {
52 goto error;
53 }
54 }
55 continue;
56 }
57 new_base = _PyObject_FastCall(meth, stack, 1);
58 Py_DECREF(meth);
59 if (!new_base) {
60 goto error;
61 }
62 if (!PyTuple_Check(new_base)) {
63 PyErr_SetString(PyExc_TypeError,
64 "__mro_entries__ must return a tuple");
65 Py_DECREF(new_base);
66 goto error;
67 }
68 if (!new_bases) {
69 /* If this is a first successful replacement, create new_bases list and
70 copy previously encountered bases. */
71 if (!(new_bases = PyList_New(i))) {
72 goto error;
73 }
74 for (j = 0; j < i; j++) {
75 base = args[j];
76 PyList_SET_ITEM(new_bases, j, base);
77 Py_INCREF(base);
78 }
79 }
80 j = PyList_GET_SIZE(new_bases);
81 if (PyList_SetSlice(new_bases, j, j, new_base) < 0) {
82 goto error;
83 }
84 Py_DECREF(new_base);
85 }
86 if (!new_bases) {
87 return bases;
88 }
89 result = PyList_AsTuple(new_bases);
90 Py_DECREF(new_bases);
91 return result;
92
93error:
94 Py_XDECREF(new_bases);
95 return NULL;
96}
97
Nick Coghlanf9e227e2014-08-17 14:01:19 +100098/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +000099static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200100builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
Victor Stinner773dc6d2017-01-16 23:46:26 +0100101 PyObject *kwnames)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000102{
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100103 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *orig_bases;
Nick Coghlan19d24672016-12-05 16:47:55 +1000104 PyObject *cls = NULL, *cell = NULL;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100105 int isclass = 0; /* initialize to prevent gcc warning */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (nargs < 2) {
108 PyErr_SetString(PyExc_TypeError,
109 "__build_class__: not enough arguments");
110 return NULL;
111 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100112 func = args[0]; /* Better be callable */
Benjamin Petersone8e14592013-05-16 14:37:25 -0500113 if (!PyFunction_Check(func)) {
114 PyErr_SetString(PyExc_TypeError,
Zachary Ware9b338722014-08-05 14:01:10 -0500115 "__build_class__: func must be a function");
Benjamin Petersone8e14592013-05-16 14:37:25 -0500116 return NULL;
117 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100118 name = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 if (!PyUnicode_Check(name)) {
120 PyErr_SetString(PyExc_TypeError,
121 "__build_class__: name is not a string");
122 return NULL;
123 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100124 orig_bases = _PyStack_AsTupleSlice(args, nargs, 2, nargs);
125 if (orig_bases == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000127
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100128 bases = update_bases(orig_bases, args + 2, nargs - 2);
129 if (bases == NULL) {
130 Py_DECREF(orig_bases);
131 return NULL;
132 }
133
Victor Stinner773dc6d2017-01-16 23:46:26 +0100134 if (kwnames == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 meta = NULL;
136 mkw = NULL;
137 }
138 else {
Victor Stinner773dc6d2017-01-16 23:46:26 +0100139 mkw = _PyStack_AsDict(args + nargs, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (mkw == NULL) {
141 Py_DECREF(bases);
142 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000143 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100144
Victor Stinnerae9f1612013-11-06 22:46:51 +0100145 meta = _PyDict_GetItemId(mkw, &PyId_metaclass);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (meta != NULL) {
147 Py_INCREF(meta);
Victor Stinnerae9f1612013-11-06 22:46:51 +0100148 if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 Py_DECREF(meta);
150 Py_DECREF(mkw);
151 Py_DECREF(bases);
152 return NULL;
153 }
Nick Coghlande31b192011-10-23 22:04:16 +1000154 /* metaclass is explicitly given, check if it's indeed a class */
155 isclass = PyType_Check(meta);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 }
157 }
158 if (meta == NULL) {
Nick Coghlande31b192011-10-23 22:04:16 +1000159 /* if there are no bases, use type: */
160 if (PyTuple_GET_SIZE(bases) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 meta = (PyObject *) (&PyType_Type);
Nick Coghlande31b192011-10-23 22:04:16 +1000162 }
163 /* else get the type of the first base */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 else {
165 PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
166 meta = (PyObject *) (base0->ob_type);
167 }
168 Py_INCREF(meta);
Nick Coghlande31b192011-10-23 22:04:16 +1000169 isclass = 1; /* meta is really a class */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 }
Nick Coghlan9715d262011-10-23 22:36:42 +1000171
Nick Coghlande31b192011-10-23 22:04:16 +1000172 if (isclass) {
173 /* meta is really a class, so check for a more derived
174 metaclass, or possible metaclass conflicts: */
175 winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
176 bases);
177 if (winner == NULL) {
178 Py_DECREF(meta);
179 Py_XDECREF(mkw);
180 Py_DECREF(bases);
181 return NULL;
182 }
183 if (winner != meta) {
184 Py_DECREF(meta);
185 meta = winner;
186 Py_INCREF(meta);
187 }
188 }
189 /* else: meta is not a class, so we cannot do the metaclass
190 calculation, so we will use the explicitly given object as it is */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200191 if (_PyObject_LookupAttrId(meta, &PyId___prepare__, &prep) < 0) {
192 ns = NULL;
193 }
194 else if (prep == NULL) {
195 ns = PyDict_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 }
197 else {
Victor Stinner463b86a2016-08-22 23:33:13 +0200198 PyObject *pargs[2] = {name, bases};
199 ns = _PyObject_FastCallDict(prep, pargs, 2, mkw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 Py_DECREF(prep);
201 }
202 if (ns == NULL) {
203 Py_DECREF(meta);
204 Py_XDECREF(mkw);
205 Py_DECREF(bases);
206 return NULL;
207 }
Oren Milman5837d042017-09-27 17:04:37 +0300208 if (!PyMapping_Check(ns)) {
209 PyErr_Format(PyExc_TypeError,
210 "%.200s.__prepare__() must return a mapping, not %.200s",
211 isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
212 Py_TYPE(ns)->tp_name);
213 goto error;
214 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000215 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500216 NULL, 0, NULL, 0, NULL, 0, NULL,
217 PyFunction_GET_CLOSURE(func));
Nick Coghlan19d24672016-12-05 16:47:55 +1000218 if (cell != NULL) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100219 if (bases != orig_bases) {
220 if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
221 goto error;
222 }
223 }
Victor Stinnerd1c2a8e2016-08-23 01:34:35 +0200224 PyObject *margs[3] = {name, bases, ns};
225 cls = _PyObject_FastCallDict(meta, margs, 3, mkw);
Nick Coghlan19d24672016-12-05 16:47:55 +1000226 if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
227 PyObject *cell_cls = PyCell_GET(cell);
228 if (cell_cls != cls) {
Nick Coghlan19d24672016-12-05 16:47:55 +1000229 if (cell_cls == NULL) {
230 const char *msg =
231 "__class__ not set defining %.200R as %.200R. "
232 "Was __classcell__ propagated to type.__new__?";
Serhiy Storchakaf5e7b192018-05-20 08:48:12 +0300233 PyErr_Format(PyExc_RuntimeError, msg, name, cls);
Nick Coghlan19d24672016-12-05 16:47:55 +1000234 } else {
235 const char *msg =
236 "__class__ set to %.200R defining %.200R as %.200R";
237 PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
Nick Coghlan19d24672016-12-05 16:47:55 +1000238 }
Serhiy Storchakaf5e7b192018-05-20 08:48:12 +0300239 Py_DECREF(cls);
240 cls = NULL;
241 goto error;
Nick Coghlan19d24672016-12-05 16:47:55 +1000242 }
243 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000245error:
246 Py_XDECREF(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 Py_DECREF(ns);
248 Py_DECREF(meta);
249 Py_XDECREF(mkw);
250 Py_DECREF(bases);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100251 if (bases != orig_bases) {
252 Py_DECREF(orig_bases);
253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 return cls;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000255}
256
257PyDoc_STRVAR(build_class_doc,
258"__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
259\n\
260Internal helper function used by the class statement.");
261
262static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
266 "level", 0};
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400267 PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -0400268 int level = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000269
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400270 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 kwlist, &name, &globals, &locals, &fromlist, &level))
272 return NULL;
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400273 return PyImport_ImportModuleLevelObject(name, globals, locals,
274 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000275}
276
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000277PyDoc_STRVAR(import_doc,
Brett Cannoncb4996a2012-08-06 16:34:44 -0400278"__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000279\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000280Import a module. Because this function is meant for use by the Python\n\
oldk461d2252018-02-02 12:20:00 +0800281interpreter and not for general use, it is better to use\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000282importlib.import_module() to programmatically import a module.\n\
283\n\
284The globals argument is only used to determine the context;\n\
285they are not modified. The locals argument is unused. The fromlist\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000286should be a list of names to emulate ``from name import ...'', or an\n\
287empty list to emulate ``import name''.\n\
288When importing a module from a package, note that __import__('A.B', ...)\n\
289returns package A when fromlist is empty, but its submodule B when\n\
oldk461d2252018-02-02 12:20:00 +0800290fromlist is not empty. The level argument is used to determine whether to\n\
291perform absolute or relative imports: 0 is absolute, while a positive number\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000293
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000294
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000295/*[clinic input]
296abs as builtin_abs
297
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300298 x: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000299 /
300
301Return the absolute value of the argument.
302[clinic start generated code]*/
303
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300305builtin_abs(PyObject *module, PyObject *x)
306/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000307{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000308 return PyNumber_Absolute(x);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000309}
310
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000311/*[clinic input]
312all as builtin_all
313
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300314 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000315 /
316
317Return True if bool(x) is True for all values x in the iterable.
318
319If the iterable is empty, return True.
320[clinic start generated code]*/
321
Raymond Hettinger96229b12005-03-11 06:49:40 +0000322static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300323builtin_all(PyObject *module, PyObject *iterable)
324/*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 PyObject *it, *item;
327 PyObject *(*iternext)(PyObject *);
328 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000329
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000330 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 if (it == NULL)
332 return NULL;
333 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 for (;;) {
336 item = iternext(it);
337 if (item == NULL)
338 break;
339 cmp = PyObject_IsTrue(item);
340 Py_DECREF(item);
341 if (cmp < 0) {
342 Py_DECREF(it);
343 return NULL;
344 }
345 if (cmp == 0) {
346 Py_DECREF(it);
347 Py_RETURN_FALSE;
348 }
349 }
350 Py_DECREF(it);
351 if (PyErr_Occurred()) {
352 if (PyErr_ExceptionMatches(PyExc_StopIteration))
353 PyErr_Clear();
354 else
355 return NULL;
356 }
357 Py_RETURN_TRUE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000358}
359
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000360/*[clinic input]
361any as builtin_any
362
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300363 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000364 /
365
366Return True if bool(x) is True for any x in the iterable.
367
368If the iterable is empty, return False.
369[clinic start generated code]*/
370
Raymond Hettinger96229b12005-03-11 06:49:40 +0000371static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300372builtin_any(PyObject *module, PyObject *iterable)
373/*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *it, *item;
376 PyObject *(*iternext)(PyObject *);
377 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000378
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000379 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (it == NULL)
381 return NULL;
382 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 for (;;) {
385 item = iternext(it);
386 if (item == NULL)
387 break;
388 cmp = PyObject_IsTrue(item);
389 Py_DECREF(item);
390 if (cmp < 0) {
391 Py_DECREF(it);
392 return NULL;
393 }
Raymond Hettinger5098b582015-10-09 00:42:47 -0400394 if (cmp > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 Py_DECREF(it);
396 Py_RETURN_TRUE;
397 }
398 }
399 Py_DECREF(it);
400 if (PyErr_Occurred()) {
401 if (PyErr_ExceptionMatches(PyExc_StopIteration))
402 PyErr_Clear();
403 else
404 return NULL;
405 }
406 Py_RETURN_FALSE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000407}
408
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000409/*[clinic input]
410ascii as builtin_ascii
411
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300412 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000413 /
414
415Return an ASCII-only representation of an object.
416
417As repr(), return a string containing a printable representation of an
418object, but escape the non-ASCII characters in the string returned by
419repr() using \\x, \\u or \\U escapes. This generates a string similar
420to that returned by repr() in Python 2.
421[clinic start generated code]*/
422
Georg Brandl559e5d72008-06-11 18:37:52 +0000423static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300424builtin_ascii(PyObject *module, PyObject *obj)
425/*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
Georg Brandl559e5d72008-06-11 18:37:52 +0000426{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000427 return PyObject_ASCII(obj);
Georg Brandl559e5d72008-06-11 18:37:52 +0000428}
429
Georg Brandl559e5d72008-06-11 18:37:52 +0000430
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000431/*[clinic input]
432bin as builtin_bin
433
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300434 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000435 /
436
437Return the binary representation of an integer.
438
439 >>> bin(2796202)
440 '0b1010101010101010101010'
441[clinic start generated code]*/
442
Guido van Rossum79f25d91997-04-29 20:08:16 +0000443static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300444builtin_bin(PyObject *module, PyObject *number)
445/*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000446{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000447 return PyNumber_ToBase(number, 2);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000448}
449
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000450
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000451/*[clinic input]
452callable as builtin_callable
453
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300454 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000455 /
456
457Return whether the object is callable (i.e., some kind of function).
458
459Note that classes are callable, as are instances of classes with a
460__call__() method.
461[clinic start generated code]*/
462
Antoine Pitroue71362d2010-11-27 22:00:11 +0000463static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300464builtin_callable(PyObject *module, PyObject *obj)
465/*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
Antoine Pitroue71362d2010-11-27 22:00:11 +0000466{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000467 return PyBool_FromLong((long)PyCallable_Check(obj));
Antoine Pitroue71362d2010-11-27 22:00:11 +0000468}
469
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400470static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200471builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400472{
473 PyObject *hook = PySys_GetObject("breakpointhook");
474
475 if (hook == NULL) {
476 PyErr_SetString(PyExc_RuntimeError, "lost sys.breakpointhook");
477 return NULL;
478 }
479 Py_INCREF(hook);
480 PyObject *retval = _PyObject_FastCallKeywords(hook, args, nargs, keywords);
481 Py_DECREF(hook);
482 return retval;
483}
484
485PyDoc_STRVAR(breakpoint_doc,
486"breakpoint(*args, **kws)\n\
487\n\
488Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept\n\
489whatever arguments are passed.\n\
490\n\
491By default, this drops you into the pdb debugger.");
Antoine Pitroue71362d2010-11-27 22:00:11 +0000492
Raymond Hettinger17301e92008-03-13 00:19:26 +0000493typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PyObject_HEAD
495 PyObject *func;
496 PyObject *it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000497} filterobject;
498
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000499static PyObject *
Raymond Hettinger17301e92008-03-13 00:19:26 +0000500filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyObject *func, *seq;
503 PyObject *it;
504 filterobject *lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000505
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +0300506 if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
510 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 /* Get iterator. */
513 it = PyObject_GetIter(seq);
514 if (it == NULL)
515 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 /* create filterobject structure */
518 lz = (filterobject *)type->tp_alloc(type, 0);
519 if (lz == NULL) {
520 Py_DECREF(it);
521 return NULL;
522 }
523 Py_INCREF(func);
524 lz->func = func;
525 lz->it = it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return (PyObject *)lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000528}
529
530static void
531filter_dealloc(filterobject *lz)
532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyObject_GC_UnTrack(lz);
534 Py_XDECREF(lz->func);
535 Py_XDECREF(lz->it);
536 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger17301e92008-03-13 00:19:26 +0000537}
538
539static int
540filter_traverse(filterobject *lz, visitproc visit, void *arg)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 Py_VISIT(lz->it);
543 Py_VISIT(lz->func);
544 return 0;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000545}
546
547static PyObject *
548filter_next(filterobject *lz)
549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 PyObject *item;
551 PyObject *it = lz->it;
552 long ok;
553 PyObject *(*iternext)(PyObject *);
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400554 int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 iternext = *Py_TYPE(it)->tp_iternext;
557 for (;;) {
558 item = iternext(it);
559 if (item == NULL)
560 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000561
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400562 if (checktrue) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 ok = PyObject_IsTrue(item);
564 } else {
565 PyObject *good;
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100566 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (good == NULL) {
568 Py_DECREF(item);
569 return NULL;
570 }
571 ok = PyObject_IsTrue(good);
572 Py_DECREF(good);
573 }
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200574 if (ok > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 return item;
576 Py_DECREF(item);
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200577 if (ok < 0)
578 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000580}
581
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000582static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530583filter_reduce(filterobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000584{
585 return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
586}
587
588PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
589
590static PyMethodDef filter_methods[] = {
591 {"__reduce__", (PyCFunction)filter_reduce, METH_NOARGS, reduce_doc},
592 {NULL, NULL} /* sentinel */
593};
594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000595PyDoc_STRVAR(filter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +0000596"filter(function or None, iterable) --> filter object\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000597\n\
Georg Brandld11ae5d2008-05-16 13:27:32 +0000598Return an iterator yielding those items of iterable for which function(item)\n\
Raymond Hettinger17301e92008-03-13 00:19:26 +0000599is true. If function is None, return the items that are true.");
600
601PyTypeObject PyFilter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 PyVarObject_HEAD_INIT(&PyType_Type, 0)
603 "filter", /* tp_name */
604 sizeof(filterobject), /* tp_basicsize */
605 0, /* tp_itemsize */
606 /* methods */
607 (destructor)filter_dealloc, /* tp_dealloc */
608 0, /* tp_print */
609 0, /* tp_getattr */
610 0, /* tp_setattr */
611 0, /* tp_reserved */
612 0, /* tp_repr */
613 0, /* tp_as_number */
614 0, /* tp_as_sequence */
615 0, /* tp_as_mapping */
616 0, /* tp_hash */
617 0, /* tp_call */
618 0, /* tp_str */
619 PyObject_GenericGetAttr, /* tp_getattro */
620 0, /* tp_setattro */
621 0, /* tp_as_buffer */
622 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
623 Py_TPFLAGS_BASETYPE, /* tp_flags */
624 filter_doc, /* tp_doc */
625 (traverseproc)filter_traverse, /* tp_traverse */
626 0, /* tp_clear */
627 0, /* tp_richcompare */
628 0, /* tp_weaklistoffset */
629 PyObject_SelfIter, /* tp_iter */
630 (iternextfunc)filter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000631 filter_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 0, /* tp_members */
633 0, /* tp_getset */
634 0, /* tp_base */
635 0, /* tp_dict */
636 0, /* tp_descr_get */
637 0, /* tp_descr_set */
638 0, /* tp_dictoffset */
639 0, /* tp_init */
640 PyType_GenericAlloc, /* tp_alloc */
641 filter_new, /* tp_new */
642 PyObject_GC_Del, /* tp_free */
Raymond Hettinger17301e92008-03-13 00:19:26 +0000643};
644
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000645
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000646/*[clinic input]
647format as builtin_format
648
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300649 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000650 format_spec: unicode(c_default="NULL") = ''
651 /
652
653Return value.__format__(format_spec)
654
Amit Kumar2e6bb442017-05-29 06:32:26 +0530655format_spec defaults to the empty string.
656See the Format Specification Mini-Language section of help('FORMATTING') for
657details.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000658[clinic start generated code]*/
659
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000660static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300661builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
Amit Kumar2e6bb442017-05-29 06:32:26 +0530662/*[clinic end generated code: output=2f40bdfa4954b077 input=88339c93ea522b33]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000663{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000664 return PyObject_Format(value, format_spec);
Eric Smith8c663262007-08-25 02:26:07 +0000665}
666
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000667/*[clinic input]
668chr as builtin_chr
669
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300670 i: int
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000671 /
672
673Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
674[clinic start generated code]*/
675
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000676static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300677builtin_chr_impl(PyObject *module, int i)
678/*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000679{
680 return PyUnicode_FromOrdinal(i);
681}
Guido van Rossum09095f32000-03-10 23:00:52 +0000682
683
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200684static const char *
Martin Pantereeb896c2015-11-07 02:32:21 +0000685source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000686{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200687 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 Py_ssize_t size;
Martin Pantereeb896c2015-11-07 02:32:21 +0000689 Py_buffer view;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000690
Martin Pantereeb896c2015-11-07 02:32:21 +0000691 *cmd_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (PyUnicode_Check(cmd)) {
693 cf->cf_flags |= PyCF_IGNORE_COOKIE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200694 str = PyUnicode_AsUTF8AndSize(cmd, &size);
695 if (str == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 return NULL;
697 }
Martin Pantereeb896c2015-11-07 02:32:21 +0000698 else if (PyBytes_Check(cmd)) {
699 str = PyBytes_AS_STRING(cmd);
700 size = PyBytes_GET_SIZE(cmd);
701 }
702 else if (PyByteArray_Check(cmd)) {
703 str = PyByteArray_AS_STRING(cmd);
704 size = PyByteArray_GET_SIZE(cmd);
705 }
706 else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
707 /* Copy to NUL-terminated buffer. */
708 *cmd_copy = PyBytes_FromStringAndSize(
709 (const char *)view.buf, view.len);
710 PyBuffer_Release(&view);
711 if (*cmd_copy == NULL) {
712 return NULL;
713 }
714 str = PyBytes_AS_STRING(*cmd_copy);
715 size = PyBytes_GET_SIZE(*cmd_copy);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200716 }
717 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 PyErr_Format(PyExc_TypeError,
719 "%s() arg 1 must be a %s object",
720 funcname, what);
721 return NULL;
722 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200723
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +0200724 if (strlen(str) != (size_t)size) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300725 PyErr_SetString(PyExc_ValueError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 "source code string cannot contain null bytes");
Martin Pantereeb896c2015-11-07 02:32:21 +0000727 Py_CLEAR(*cmd_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 return NULL;
729 }
730 return str;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000731}
732
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000733/*[clinic input]
734compile as builtin_compile
735
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300736 source: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000737 filename: object(converter="PyUnicode_FSDecoder")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300738 mode: str
739 flags: int = 0
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200740 dont_inherit: bool(accept={int}) = False
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300741 optimize: int = -1
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000742
743Compile source into a code object that can be executed by exec() or eval().
744
745The source code may represent a Python module, statement or expression.
746The filename will be used for run-time error messages.
747The mode must be 'exec' to compile a module, 'single' to compile a
748single (interactive) statement, or 'eval' to compile an expression.
749The flags argument, if present, controls which future statements influence
750the compilation of the code.
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300751The dont_inherit argument, if true, stops the compilation inheriting
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000752the effects of any future statements in effect in the code calling
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300753compile; if absent or false these statements do influence the compilation,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000754in addition to any features explicitly specified.
755[clinic start generated code]*/
756
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000757static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300758builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
759 const char *mode, int flags, int dont_inherit,
760 int optimize)
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200761/*[clinic end generated code: output=1fa176e33452bb63 input=0ff726f595eb9fcd]*/
Guido van Rossum5b722181993-03-30 17:46:03 +0000762{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000763 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200764 const char *str;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000765 int compile_mode = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 int is_ast;
767 PyCompilerFlags cf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000769 PyObject *result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000770
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000771 cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
Tim Peters6cd6a822001-08-17 22:11:27 +0000772
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000773 if (flags &
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
775 {
776 PyErr_SetString(PyExc_ValueError,
777 "compile(): unrecognised flags");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000778 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 }
780 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
Tim Peters6cd6a822001-08-17 22:11:27 +0000781
Georg Brandl8334fd92010-12-04 10:26:46 +0000782 if (optimize < -1 || optimize > 2) {
783 PyErr_SetString(PyExc_ValueError,
784 "compile(): invalid optimize value");
785 goto error;
786 }
787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (!dont_inherit) {
789 PyEval_MergeCompilerFlags(&cf);
790 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000791
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000792 if (strcmp(mode, "exec") == 0)
793 compile_mode = 0;
794 else if (strcmp(mode, "eval") == 0)
795 compile_mode = 1;
796 else if (strcmp(mode, "single") == 0)
797 compile_mode = 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 else {
799 PyErr_SetString(PyExc_ValueError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000800 "compile() mode must be 'exec', 'eval' or 'single'");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000801 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 }
Neal Norwitzdb4115f2008-03-31 04:20:05 +0000803
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000804 is_ast = PyAST_Check(source);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 if (is_ast == -1)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000806 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 if (is_ast) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000808 if (flags & PyCF_ONLY_AST) {
809 Py_INCREF(source);
810 result = source;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 }
812 else {
813 PyArena *arena;
814 mod_ty mod;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 arena = PyArena_New();
Stefan Krah07795df2012-08-20 17:19:50 +0200817 if (arena == NULL)
818 goto error;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000819 mod = PyAST_obj2mod(source, arena, compile_mode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (mod == NULL) {
821 PyArena_Free(arena);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000822 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 }
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500824 if (!PyAST_Validate(mod)) {
825 PyArena_Free(arena);
826 goto error;
827 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200828 result = (PyObject*)PyAST_CompileObject(mod, filename,
829 &cf, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 PyArena_Free(arena);
831 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000832 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000834
Martin Panter61d6e4a2015-11-07 02:56:11 +0000835 str = source_as_string(source, "compile", "string, bytes or AST", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (str == NULL)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000837 goto error;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000838
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000839 result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000840 Py_XDECREF(source_copy);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000841 goto finally;
842
843error:
844 result = NULL;
845finally:
Victor Stinner14e461d2013-08-26 22:28:21 +0200846 Py_DECREF(filename);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000847 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000848}
849
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000850/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000852builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
857 return NULL;
858 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000859}
860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000861PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000862"dir([object]) -> list of strings\n"
863"\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000864"If called without an argument, return the names in the current scope.\n"
865"Else, return an alphabetized list of names comprising (some of) the attributes\n"
866"of the given object, and of attributes reachable from it.\n"
867"If the object supplies a method named __dir__, it will be used; otherwise\n"
868"the default dir() logic is used and returns:\n"
869" for a module object: the module's attributes.\n"
870" for a class object: its attributes, and recursively the attributes\n"
871" of its bases.\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000872" for any other object: its attributes, its class's attributes, and\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000873" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000874
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000875/*[clinic input]
876divmod as builtin_divmod
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000877
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300878 x: object
879 y: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000880 /
881
Zachary Ware7f227d92016-04-28 14:39:50 -0500882Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000883[clinic start generated code]*/
884
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000885static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300886builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
887/*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000888{
889 return PyNumber_Divmod(x, y);
890}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000891
892
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000893/*[clinic input]
894eval as builtin_eval
895
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300896 source: object
897 globals: object = None
898 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000899 /
900
901Evaluate the given source in the context of globals and locals.
902
903The source may be a string representing a Python expression
904or a code object as returned by compile().
905The globals must be a dictionary and locals can be any mapping,
906defaulting to the current globals and locals.
907If only globals is given, locals defaults to it.
908[clinic start generated code]*/
909
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000910static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300911builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400912 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300913/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000914{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000915 PyObject *result, *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200916 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (locals != Py_None && !PyMapping_Check(locals)) {
920 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
921 return NULL;
922 }
923 if (globals != Py_None && !PyDict_Check(globals)) {
924 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
925 "globals must be a real dict; try eval(expr, {}, mapping)"
926 : "globals must be a dict");
927 return NULL;
928 }
929 if (globals == Py_None) {
930 globals = PyEval_GetGlobals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100931 if (locals == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100933 if (locals == NULL)
934 return NULL;
935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 }
937 else if (locals == Py_None)
938 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 if (globals == NULL || locals == NULL) {
941 PyErr_SetString(PyExc_TypeError,
942 "eval must be given globals and locals "
943 "when called without a frame");
944 return NULL;
945 }
Georg Brandl77c85e62005-09-15 10:46:13 +0000946
Victor Stinnerb44562b2013-11-06 19:03:11 +0100947 if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
948 if (_PyDict_SetItemId(globals, &PyId___builtins__,
949 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 return NULL;
951 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000952
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000953 if (PyCode_Check(source)) {
954 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyErr_SetString(PyExc_TypeError,
956 "code object passed to eval() may not contain free variables");
957 return NULL;
958 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000959 return PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Martin Panter61d6e4a2015-11-07 02:56:11 +0000963 str = source_as_string(source, "eval", "string, bytes or code", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 if (str == NULL)
965 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 while (*str == ' ' || *str == '\t')
968 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 (void)PyEval_MergeCompilerFlags(&cf);
971 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000972 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000974}
975
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000976/*[clinic input]
977exec as builtin_exec
978
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300979 source: object
980 globals: object = None
981 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000982 /
983
984Execute the given source in the context of globals and locals.
985
986The source may be a string representing one or more Python statements
987or a code object as returned by compile().
988The globals must be a dictionary and locals can be any mapping,
989defaulting to the current globals and locals.
990If only globals is given, locals defaults to it.
991[clinic start generated code]*/
992
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000993static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300994builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400995 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300996/*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
Georg Brandl7cae87c2006-09-06 06:51:57 +0000997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 PyObject *v;
Georg Brandl2cabc562008-08-28 07:57:16 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 if (globals == Py_None) {
1001 globals = PyEval_GetGlobals();
1002 if (locals == Py_None) {
1003 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01001004 if (locals == NULL)
1005 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 }
1007 if (!globals || !locals) {
1008 PyErr_SetString(PyExc_SystemError,
1009 "globals and locals cannot be NULL");
1010 return NULL;
1011 }
1012 }
1013 else if (locals == Py_None)
1014 locals = globals;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 if (!PyDict_Check(globals)) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001017 PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 globals->ob_type->tp_name);
1019 return NULL;
1020 }
1021 if (!PyMapping_Check(locals)) {
1022 PyErr_Format(PyExc_TypeError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001023 "locals must be a mapping or None, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 locals->ob_type->tp_name);
1025 return NULL;
1026 }
Victor Stinnerb44562b2013-11-06 19:03:11 +01001027 if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
1028 if (_PyDict_SetItemId(globals, &PyId___builtins__,
1029 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 return NULL;
1031 }
1032
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001033 if (PyCode_Check(source)) {
1034 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyErr_SetString(PyExc_TypeError,
1036 "code object passed to exec() may not "
1037 "contain free variables");
1038 return NULL;
1039 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001040 v = PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 }
1042 else {
Martin Panter61d6e4a2015-11-07 02:56:11 +00001043 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001044 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 PyCompilerFlags cf;
1046 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001047 str = source_as_string(source, "exec",
Martin Panter61d6e4a2015-11-07 02:56:11 +00001048 "string, bytes or code", &cf,
1049 &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (str == NULL)
1051 return NULL;
1052 if (PyEval_MergeCompilerFlags(&cf))
1053 v = PyRun_StringFlags(str, Py_file_input, globals,
1054 locals, &cf);
1055 else
1056 v = PyRun_String(str, Py_file_input, globals, locals);
Martin Panter61d6e4a2015-11-07 02:56:11 +00001057 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 }
1059 if (v == NULL)
1060 return NULL;
1061 Py_DECREF(v);
1062 Py_RETURN_NONE;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001063}
1064
Georg Brandl7cae87c2006-09-06 06:51:57 +00001065
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001066/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001067static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001068builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Guido van Rossum33894be1992-01-27 16:53:09 +00001069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 PyObject *v, *result, *dflt = NULL;
1071 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001072
Sylvain96c7c062017-06-15 17:05:23 +02001073 if (!_PyArg_UnpackStack(args, nargs, "getattr", 2, 3, &v, &name, &dflt))
1074 return NULL;
1075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 if (!PyUnicode_Check(name)) {
1077 PyErr_SetString(PyExc_TypeError,
1078 "getattr(): attribute name must be string");
1079 return NULL;
1080 }
INADA Naoki378edee2018-01-16 20:52:41 +09001081 if (dflt != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001082 if (_PyObject_LookupAttr(v, name, &result) == 0) {
INADA Naoki378edee2018-01-16 20:52:41 +09001083 Py_INCREF(dflt);
1084 return dflt;
1085 }
1086 }
1087 else {
1088 result = PyObject_GetAttr(v, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 }
1090 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001091}
1092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001093PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +00001094"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001095\n\
Guido van Rossum950ff291998-06-29 13:38:57 +00001096Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
1097When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001098exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001099
1100
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001101/*[clinic input]
1102globals as builtin_globals
1103
1104Return the dictionary containing the current scope's global variables.
1105
1106NOTE: Updates to this dictionary *will* affect name lookups in the current
1107global scope and vice-versa.
1108[clinic start generated code]*/
1109
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001110static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001111builtin_globals_impl(PyObject *module)
1112/*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 d = PyEval_GetGlobals();
1117 Py_XINCREF(d);
1118 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001119}
1120
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001121
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001122/*[clinic input]
1123hasattr as builtin_hasattr
1124
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001125 obj: object
1126 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001127 /
1128
1129Return whether the object has an attribute with the given name.
1130
1131This is done by calling getattr(obj, name) and catching AttributeError.
1132[clinic start generated code]*/
1133
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001134static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001135builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1136/*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001137{
1138 PyObject *v;
1139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (!PyUnicode_Check(name)) {
1141 PyErr_SetString(PyExc_TypeError,
1142 "hasattr(): attribute name must be string");
1143 return NULL;
1144 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001145 if (_PyObject_LookupAttr(obj, name, &v) < 0) {
Benjamin Peterson17689992010-08-24 03:26:23 +00001146 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001148 if (v == NULL) {
1149 Py_RETURN_FALSE;
1150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 Py_DECREF(v);
Benjamin Peterson17689992010-08-24 03:26:23 +00001152 Py_RETURN_TRUE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001153}
1154
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001155
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001156/* AC: gdb's integration with CPython relies on builtin_id having
1157 * the *exact* parameter names of "self" and "v", so we ensure we
1158 * preserve those name rather than using the AC defaults.
1159 */
1160/*[clinic input]
1161id as builtin_id
1162
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001163 self: self(type="PyModuleDef *")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001164 obj as v: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001165 /
1166
1167Return the identity of an object.
1168
1169This is guaranteed to be unique among simultaneously existing objects.
1170(CPython uses the object's memory address.)
1171[clinic start generated code]*/
1172
Guido van Rossum79f25d91997-04-29 20:08:16 +00001173static PyObject *
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001174builtin_id(PyModuleDef *self, PyObject *v)
1175/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
Guido van Rossum5b722181993-03-30 17:46:03 +00001176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +00001178}
1179
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001180
Raymond Hettingera6c60372008-03-13 01:26:19 +00001181/* map object ************************************************************/
1182
1183typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 PyObject_HEAD
1185 PyObject *iters;
1186 PyObject *func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001187} mapobject;
1188
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189static PyObject *
Raymond Hettingera6c60372008-03-13 01:26:19 +00001190map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 PyObject *it, *iters, *func;
1193 mapobject *lz;
1194 Py_ssize_t numargs, i;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001195
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001196 if (type == &PyMap_Type && !_PyArg_NoKeywords("map", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 numargs = PyTuple_Size(args);
1200 if (numargs < 2) {
1201 PyErr_SetString(PyExc_TypeError,
1202 "map() must have at least two arguments.");
1203 return NULL;
1204 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 iters = PyTuple_New(numargs-1);
1207 if (iters == NULL)
1208 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 for (i=1 ; i<numargs ; i++) {
1211 /* Get iterator. */
1212 it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1213 if (it == NULL) {
1214 Py_DECREF(iters);
1215 return NULL;
1216 }
1217 PyTuple_SET_ITEM(iters, i-1, it);
1218 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 /* create mapobject structure */
1221 lz = (mapobject *)type->tp_alloc(type, 0);
1222 if (lz == NULL) {
1223 Py_DECREF(iters);
1224 return NULL;
1225 }
1226 lz->iters = iters;
1227 func = PyTuple_GET_ITEM(args, 0);
1228 Py_INCREF(func);
1229 lz->func = func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 return (PyObject *)lz;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001232}
1233
1234static void
1235map_dealloc(mapobject *lz)
1236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 PyObject_GC_UnTrack(lz);
1238 Py_XDECREF(lz->iters);
1239 Py_XDECREF(lz->func);
1240 Py_TYPE(lz)->tp_free(lz);
Raymond Hettingera6c60372008-03-13 01:26:19 +00001241}
1242
1243static int
1244map_traverse(mapobject *lz, visitproc visit, void *arg)
1245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 Py_VISIT(lz->iters);
1247 Py_VISIT(lz->func);
1248 return 0;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001249}
1250
1251static PyObject *
1252map_next(mapobject *lz)
1253{
Victor Stinnerbc08ab42016-12-15 12:40:53 +01001254 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001255 PyObject **stack;
1256 Py_ssize_t niters, nargs, i;
1257 PyObject *result = NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001258
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001259 niters = PyTuple_GET_SIZE(lz->iters);
1260 if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1261 stack = small_stack;
1262 }
1263 else {
1264 stack = PyMem_Malloc(niters * sizeof(stack[0]));
1265 if (stack == NULL) {
1266 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 return NULL;
1268 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 }
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001270
1271 nargs = 0;
1272 for (i=0; i < niters; i++) {
1273 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1274 PyObject *val = Py_TYPE(it)->tp_iternext(it);
1275 if (val == NULL) {
1276 goto exit;
1277 }
1278 stack[i] = val;
1279 nargs++;
1280 }
1281
1282 result = _PyObject_FastCall(lz->func, stack, nargs);
1283
1284exit:
1285 for (i=0; i < nargs; i++) {
1286 Py_DECREF(stack[i]);
1287 }
1288 if (stack != small_stack) {
1289 PyMem_Free(stack);
1290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001292}
1293
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001294static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301295map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001296{
1297 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1298 PyObject *args = PyTuple_New(numargs+1);
1299 Py_ssize_t i;
1300 if (args == NULL)
1301 return NULL;
1302 Py_INCREF(lz->func);
1303 PyTuple_SET_ITEM(args, 0, lz->func);
1304 for (i = 0; i<numargs; i++){
1305 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1306 Py_INCREF(it);
1307 PyTuple_SET_ITEM(args, i+1, it);
1308 }
1309
1310 return Py_BuildValue("ON", Py_TYPE(lz), args);
1311}
1312
1313static PyMethodDef map_methods[] = {
1314 {"__reduce__", (PyCFunction)map_reduce, METH_NOARGS, reduce_doc},
1315 {NULL, NULL} /* sentinel */
1316};
1317
1318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001319PyDoc_STRVAR(map_doc,
Raymond Hettingera6c60372008-03-13 01:26:19 +00001320"map(func, *iterables) --> map object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001321\n\
Raymond Hettingera6c60372008-03-13 01:26:19 +00001322Make an iterator that computes the function using arguments from\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323each of the iterables. Stops when the shortest iterable is exhausted.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001324
Raymond Hettingera6c60372008-03-13 01:26:19 +00001325PyTypeObject PyMap_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1327 "map", /* tp_name */
1328 sizeof(mapobject), /* tp_basicsize */
1329 0, /* tp_itemsize */
1330 /* methods */
1331 (destructor)map_dealloc, /* tp_dealloc */
1332 0, /* tp_print */
1333 0, /* tp_getattr */
1334 0, /* tp_setattr */
1335 0, /* tp_reserved */
1336 0, /* tp_repr */
1337 0, /* tp_as_number */
1338 0, /* tp_as_sequence */
1339 0, /* tp_as_mapping */
1340 0, /* tp_hash */
1341 0, /* tp_call */
1342 0, /* tp_str */
1343 PyObject_GenericGetAttr, /* tp_getattro */
1344 0, /* tp_setattro */
1345 0, /* tp_as_buffer */
1346 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1347 Py_TPFLAGS_BASETYPE, /* tp_flags */
1348 map_doc, /* tp_doc */
1349 (traverseproc)map_traverse, /* tp_traverse */
1350 0, /* tp_clear */
1351 0, /* tp_richcompare */
1352 0, /* tp_weaklistoffset */
1353 PyObject_SelfIter, /* tp_iter */
1354 (iternextfunc)map_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001355 map_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 0, /* tp_members */
1357 0, /* tp_getset */
1358 0, /* tp_base */
1359 0, /* tp_dict */
1360 0, /* tp_descr_get */
1361 0, /* tp_descr_set */
1362 0, /* tp_dictoffset */
1363 0, /* tp_init */
1364 PyType_GenericAlloc, /* tp_alloc */
1365 map_new, /* tp_new */
1366 PyObject_GC_Del, /* tp_free */
Raymond Hettingera6c60372008-03-13 01:26:19 +00001367};
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001368
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001369
1370/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001371static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001372builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Georg Brandla18af4e2007-04-21 15:47:16 +00001373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 PyObject *it, *res;
1375 PyObject *def = NULL;
Georg Brandla18af4e2007-04-21 15:47:16 +00001376
Sylvain96c7c062017-06-15 17:05:23 +02001377 if (!_PyArg_UnpackStack(args, nargs, "next", 1, 2, &it, &def))
1378 return NULL;
1379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (!PyIter_Check(it)) {
1381 PyErr_Format(PyExc_TypeError,
Philip Jenvey50add042011-11-06 16:37:52 -08001382 "'%.200s' object is not an iterator",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 it->ob_type->tp_name);
1384 return NULL;
1385 }
1386
1387 res = (*it->ob_type->tp_iternext)(it);
1388 if (res != NULL) {
1389 return res;
1390 } else if (def != NULL) {
1391 if (PyErr_Occurred()) {
1392 if(!PyErr_ExceptionMatches(PyExc_StopIteration))
1393 return NULL;
1394 PyErr_Clear();
1395 }
1396 Py_INCREF(def);
1397 return def;
1398 } else if (PyErr_Occurred()) {
1399 return NULL;
1400 } else {
1401 PyErr_SetNone(PyExc_StopIteration);
1402 return NULL;
1403 }
Georg Brandla18af4e2007-04-21 15:47:16 +00001404}
1405
1406PyDoc_STRVAR(next_doc,
1407"next(iterator[, default])\n\
1408\n\
1409Return the next item from the iterator. If default is given and the iterator\n\
1410is exhausted, it is returned instead of raising StopIteration.");
1411
1412
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001413/*[clinic input]
1414setattr as builtin_setattr
1415
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001416 obj: object
1417 name: object
1418 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001419 /
1420
1421Sets the named attribute on the given object to the specified value.
1422
1423setattr(x, 'y', v) is equivalent to ``x.y = v''
1424[clinic start generated code]*/
1425
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001426static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001427builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
Larry Hastings89964c42015-04-14 18:07:59 -04001428 PyObject *value)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001429/*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001430{
1431 if (PyObject_SetAttr(obj, name, value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001433 Py_RETURN_NONE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001434}
1435
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001436
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001437/*[clinic input]
1438delattr as builtin_delattr
1439
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001440 obj: object
1441 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001442 /
1443
1444Deletes the named attribute from the given object.
1445
1446delattr(x, 'y') is equivalent to ``del x.y''
1447[clinic start generated code]*/
1448
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001449static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001450builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1451/*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001452{
1453 if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001455 Py_RETURN_NONE;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001456}
1457
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001458
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001459/*[clinic input]
1460hash as builtin_hash
1461
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001462 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001463 /
1464
1465Return the hash value for the given object.
1466
1467Two objects that compare equal must also have the same hash value, but the
1468reverse is not necessarily true.
1469[clinic start generated code]*/
1470
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001472builtin_hash(PyObject *module, PyObject *obj)
1473/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001474{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001475 Py_hash_t x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001476
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001477 x = PyObject_Hash(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (x == -1)
1479 return NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001480 return PyLong_FromSsize_t(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001481}
1482
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001483
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001484/*[clinic input]
1485hex as builtin_hex
1486
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001487 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001488 /
1489
1490Return the hexadecimal representation of an integer.
1491
1492 >>> hex(12648430)
1493 '0xc0ffee'
1494[clinic start generated code]*/
1495
Guido van Rossum79f25d91997-04-29 20:08:16 +00001496static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001497builtin_hex(PyObject *module, PyObject *number)
1498/*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001499{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001500 return PyNumber_ToBase(number, 16);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001501}
1502
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001503
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001504/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001505static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001506builtin_iter(PyObject *self, PyObject *args)
1507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 PyObject *v, *w = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
1511 return NULL;
1512 if (w == NULL)
1513 return PyObject_GetIter(v);
1514 if (!PyCallable_Check(v)) {
1515 PyErr_SetString(PyExc_TypeError,
1516 "iter(v, w): v must be callable");
1517 return NULL;
1518 }
1519 return PyCallIter_New(v, w);
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 /* First of all, flush stderr */
Victor Stinner3466bde2016-09-05 18:16:01 -07001954 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (tmp == NULL)
1956 PyErr_Clear();
1957 else
1958 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 /* We should only use (GNU) readline if Python's sys.stdin and
1961 sys.stdout are the same as C's stdin and stdout, because we
1962 need to pass it those. */
Victor Stinner3466bde2016-09-05 18:16:01 -07001963 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 if (tmp == NULL) {
1965 PyErr_Clear();
1966 tty = 0;
1967 }
1968 else {
1969 fd = PyLong_AsLong(tmp);
1970 Py_DECREF(tmp);
1971 if (fd < 0 && PyErr_Occurred())
1972 return NULL;
1973 tty = fd == fileno(stdin) && isatty(fd);
1974 }
1975 if (tty) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001976 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
Martin Panterc9a6ab52015-10-10 01:25:38 +00001977 if (tmp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 PyErr_Clear();
Martin Panterc9a6ab52015-10-10 01:25:38 +00001979 tty = 0;
1980 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 else {
1982 fd = PyLong_AsLong(tmp);
1983 Py_DECREF(tmp);
1984 if (fd < 0 && PyErr_Occurred())
1985 return NULL;
1986 tty = fd == fileno(stdout) && isatty(fd);
1987 }
1988 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 /* If we're interactive, use (GNU) readline */
1991 if (tty) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01001992 PyObject *po = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001993 const char *promptstr;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01001994 char *s = NULL;
1995 PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
1996 PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001997 const char *stdin_encoding_str, *stdin_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 PyObject *result;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00001999 size_t len;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00002000
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002001 /* stdin is a text stream, so it must have an encoding. */
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002002 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002003 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002004 if (!stdin_encoding || !stdin_errors ||
2005 !PyUnicode_Check(stdin_encoding) ||
2006 !PyUnicode_Check(stdin_errors)) {
2007 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002008 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002009 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002010 stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2011 stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002012 if (!stdin_encoding_str || !stdin_errors_str)
2013 goto _readline_errors;
Victor Stinner3466bde2016-09-05 18:16:01 -07002014 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 if (tmp == NULL)
2016 PyErr_Clear();
2017 else
2018 Py_DECREF(tmp);
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002019 if (prompt != NULL) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002020 /* We have a prompt, encode it as stdout would */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002021 const char *stdout_encoding_str, *stdout_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 PyObject *stringpo;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002023 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002024 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002025 if (!stdout_encoding || !stdout_errors ||
2026 !PyUnicode_Check(stdout_encoding) ||
2027 !PyUnicode_Check(stdout_errors)) {
2028 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002029 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002030 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002031 stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2032 stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002033 if (!stdout_encoding_str || !stdout_errors_str)
2034 goto _readline_errors;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002035 stringpo = PyObject_Str(prompt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002036 if (stringpo == NULL)
2037 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 po = PyUnicode_AsEncodedString(stringpo,
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002039 stdout_encoding_str, stdout_errors_str);
2040 Py_CLEAR(stdout_encoding);
2041 Py_CLEAR(stdout_errors);
2042 Py_CLEAR(stringpo);
2043 if (po == NULL)
2044 goto _readline_errors;
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03002045 assert(PyBytes_Check(po));
2046 promptstr = PyBytes_AS_STRING(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 }
2048 else {
2049 po = NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002050 promptstr = "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002052 s = PyOS_Readline(stdin, stdout, promptstr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 if (s == NULL) {
Richard Oudkerk614c5782013-04-03 13:44:50 +01002054 PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 if (!PyErr_Occurred())
2056 PyErr_SetNone(PyExc_KeyboardInterrupt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002057 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002059
2060 len = strlen(s);
2061 if (len == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 PyErr_SetNone(PyExc_EOFError);
2063 result = NULL;
2064 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002065 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 if (len > PY_SSIZE_T_MAX) {
2067 PyErr_SetString(PyExc_OverflowError,
2068 "input: input too long");
2069 result = NULL;
2070 }
2071 else {
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002072 len--; /* strip trailing '\n' */
2073 if (len != 0 && s[len-1] == '\r')
2074 len--; /* strip trailing '\r' */
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002075 result = PyUnicode_Decode(s, len, stdin_encoding_str,
2076 stdin_errors_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 }
2078 }
2079 Py_DECREF(stdin_encoding);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002080 Py_DECREF(stdin_errors);
2081 Py_XDECREF(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 PyMem_FREE(s);
2083 return result;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002084
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002085 _readline_errors:
2086 Py_XDECREF(stdin_encoding);
2087 Py_XDECREF(stdout_encoding);
2088 Py_XDECREF(stdin_errors);
2089 Py_XDECREF(stdout_errors);
2090 Py_XDECREF(po);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002091 if (tty)
2092 return NULL;
2093
2094 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
Guido van Rossumeba76962007-05-27 09:13:28 +00002096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 /* Fallback if we're not interactive */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002098 if (prompt != NULL) {
2099 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 return NULL;
2101 }
Victor Stinner3466bde2016-09-05 18:16:01 -07002102 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 if (tmp == NULL)
2104 PyErr_Clear();
2105 else
2106 Py_DECREF(tmp);
2107 return PyFile_GetLine(fin, -1);
Guido van Rossuma88a0332007-02-26 16:59:55 +00002108}
2109
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002110
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002111/*[clinic input]
2112repr as builtin_repr
2113
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002114 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002115 /
2116
2117Return the canonical string representation of the object.
2118
2119For many object types, including most builtins, eval(repr(obj)) == obj.
2120[clinic start generated code]*/
2121
Guido van Rossum79f25d91997-04-29 20:08:16 +00002122static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002123builtin_repr(PyObject *module, PyObject *obj)
2124/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
Guido van Rossumc89705d1992-11-26 08:54:07 +00002125{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002126 return PyObject_Repr(obj);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002127}
2128
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002129
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002130/*[clinic input]
2131round as builtin_round
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002132
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002133 number: object
2134 ndigits: object = NULL
2135
2136Round a number to a given precision in decimal digits.
2137
2138The return value is an integer if ndigits is omitted or None. Otherwise
2139the return value has the same type as the number. ndigits may be negative.
2140[clinic start generated code]*/
2141
2142static PyObject *
2143builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
2144/*[clinic end generated code: output=ff0d9dd176c02ede input=854bc3a217530c3d]*/
2145{
2146 PyObject *round, *result;
Alex Martelliae211f92007-08-22 23:21:33 +00002147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 if (Py_TYPE(number)->tp_dict == NULL) {
2149 if (PyType_Ready(Py_TYPE(number)) < 0)
2150 return NULL;
2151 }
Guido van Rossum15d3d042007-08-24 02:02:45 +00002152
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002153 round = _PyObject_LookupSpecial(number, &PyId___round__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 if (round == NULL) {
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002155 if (!PyErr_Occurred())
2156 PyErr_Format(PyExc_TypeError,
2157 "type %.100s doesn't define __round__ method",
2158 Py_TYPE(number)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 return NULL;
2160 }
Alex Martelliae211f92007-08-22 23:21:33 +00002161
Raymond Hettingerf0f1c232016-09-03 01:55:11 -07002162 if (ndigits == NULL || ndigits == Py_None)
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002163 result = _PyObject_CallNoArg(round);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 else
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002165 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002166 Py_DECREF(round);
2167 return result;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002168}
2169
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002170
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002171/*AC: we need to keep the kwds dict intact to easily call into the
2172 * list.sort method, which isn't currently supported in AC. So we just use
2173 * the initially generated signature with a custom implementation.
2174 */
2175/* [disabled clinic input]
2176sorted as builtin_sorted
2177
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002178 iterable as seq: object
2179 key as keyfunc: object = None
2180 reverse: object = False
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002181
2182Return a new list containing all items from the iterable in ascending order.
2183
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002184A custom key function can be supplied to customize the sort order, and the
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002185reverse flag can be set to request the result in descending order.
2186[end disabled clinic input]*/
2187
2188PyDoc_STRVAR(builtin_sorted__doc__,
Serhiy Storchaka3a104252017-01-23 12:29:47 +02002189"sorted($module, iterable, /, *, key=None, reverse=False)\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002190"--\n"
2191"\n"
2192"Return a new list containing all items from the iterable in ascending order.\n"
2193"\n"
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002194"A custom key function can be supplied to customize the sort order, and the\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002195"reverse flag can be set to request the result in descending order.");
2196
2197#define BUILTIN_SORTED_METHODDEF \
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002198 {"sorted", (PyCFunction)(void(*)(void))builtin_sorted, METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002199
Raymond Hettinger64958a12003-12-17 20:43:33 +00002200static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002201builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Raymond Hettinger64958a12003-12-17 20:43:33 +00002202{
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002203 PyObject *newlist, *v, *seq, *callable;
Victor Stinner5a60eca2017-01-17 15:17:49 +01002204
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002205 /* Keyword arguments are passed through list.sort() which will check
2206 them. */
2207 if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 newlist = PySequence_List(seq);
2211 if (newlist == NULL)
2212 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002213
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002214 callable = _PyObject_GetAttrId(newlist, &PyId_sort);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 if (callable == NULL) {
2216 Py_DECREF(newlist);
2217 return NULL;
2218 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002219
Serhiy Storchaka299dc232017-01-20 08:35:18 +02002220 assert(nargs >= 1);
Victor Stinner5a60eca2017-01-17 15:17:49 +01002221 v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 Py_DECREF(callable);
2223 if (v == NULL) {
2224 Py_DECREF(newlist);
2225 return NULL;
2226 }
2227 Py_DECREF(v);
2228 return newlist;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002229}
2230
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002231
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002232/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002233static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002234builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 PyObject *v = NULL;
2237 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2240 return NULL;
2241 if (v == NULL) {
2242 d = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01002243 if (d == NULL)
2244 return NULL;
2245 Py_INCREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 }
2247 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002248 d = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 if (d == NULL) {
2250 PyErr_SetString(PyExc_TypeError,
2251 "vars() argument must have __dict__ attribute");
2252 return NULL;
2253 }
2254 }
2255 return d;
Guido van Rossum2d951851994-08-29 12:52:16 +00002256}
2257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002258PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002259"vars([object]) -> dictionary\n\
2260\n\
2261Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002262With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002263
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002264
2265/*[clinic input]
2266sum as builtin_sum
2267
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002268 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002269 /
Raymond Hettinger9dfa0fe2018-09-12 10:54:06 -07002270 start: object(c_default="NULL") = 0
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002271
2272Return the sum of a 'start' value (default: 0) plus an iterable of numbers
2273
2274When the iterable is empty, return the start value.
2275This function is intended specifically for use with numeric values and may
2276reject non-numeric types.
2277[clinic start generated code]*/
2278
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002279static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002280builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
Raymond Hettinger9dfa0fe2018-09-12 10:54:06 -07002281/*[clinic end generated code: output=df758cec7d1d302f input=162b50765250d222]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002282{
2283 PyObject *result = start;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 PyObject *temp, *item, *iter;
Alex Martellia70b1912003-04-22 08:12:33 +00002285
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002286 iter = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (iter == NULL)
2288 return NULL;
Alex Martellia70b1912003-04-22 08:12:33 +00002289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 if (result == NULL) {
2291 result = PyLong_FromLong(0);
2292 if (result == NULL) {
2293 Py_DECREF(iter);
2294 return NULL;
2295 }
2296 } else {
2297 /* reject string values for 'start' parameter */
2298 if (PyUnicode_Check(result)) {
2299 PyErr_SetString(PyExc_TypeError,
2300 "sum() can't sum strings [use ''.join(seq) instead]");
2301 Py_DECREF(iter);
2302 return NULL;
2303 }
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002304 if (PyBytes_Check(result)) {
2305 PyErr_SetString(PyExc_TypeError,
2306 "sum() can't sum bytes [use b''.join(seq) instead]");
Benjamin Peterson405f32c2011-07-29 22:43:45 -05002307 Py_DECREF(iter);
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002308 return NULL;
2309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (PyByteArray_Check(result)) {
2311 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson4f921c22011-07-29 14:24:29 -05002312 "sum() can't sum bytearray [use b''.join(seq) instead]");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 Py_DECREF(iter);
2314 return NULL;
2315 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 Py_INCREF(result);
2317 }
Alex Martellia70b1912003-04-22 08:12:33 +00002318
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002319#ifndef SLOW_SUM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2321 Assumes all inputs are the same type. If the assumption fails, default
2322 to the more general routine.
2323 */
2324 if (PyLong_CheckExact(result)) {
2325 int overflow;
2326 long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2327 /* If this already overflowed, don't even enter the loop. */
2328 if (overflow == 0) {
2329 Py_DECREF(result);
2330 result = NULL;
2331 }
2332 while(result == NULL) {
2333 item = PyIter_Next(iter);
2334 if (item == NULL) {
2335 Py_DECREF(iter);
2336 if (PyErr_Occurred())
2337 return NULL;
2338 return PyLong_FromLong(i_result);
2339 }
2340 if (PyLong_CheckExact(item)) {
2341 long b = PyLong_AsLongAndOverflow(item, &overflow);
2342 long x = i_result + b;
2343 if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
2344 i_result = x;
2345 Py_DECREF(item);
2346 continue;
2347 }
2348 }
2349 /* Either overflowed or is not an int. Restore real objects and process normally */
2350 result = PyLong_FromLong(i_result);
Christian Heimes704e2d32013-07-26 22:49:26 +02002351 if (result == NULL) {
2352 Py_DECREF(item);
2353 Py_DECREF(iter);
2354 return NULL;
2355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 temp = PyNumber_Add(result, item);
2357 Py_DECREF(result);
2358 Py_DECREF(item);
2359 result = temp;
2360 if (result == NULL) {
2361 Py_DECREF(iter);
2362 return NULL;
2363 }
2364 }
2365 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 if (PyFloat_CheckExact(result)) {
2368 double f_result = PyFloat_AS_DOUBLE(result);
2369 Py_DECREF(result);
2370 result = NULL;
2371 while(result == NULL) {
2372 item = PyIter_Next(iter);
2373 if (item == NULL) {
2374 Py_DECREF(iter);
2375 if (PyErr_Occurred())
2376 return NULL;
2377 return PyFloat_FromDouble(f_result);
2378 }
2379 if (PyFloat_CheckExact(item)) {
2380 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2381 f_result += PyFloat_AS_DOUBLE(item);
2382 PyFPE_END_PROTECT(f_result)
2383 Py_DECREF(item);
2384 continue;
2385 }
2386 if (PyLong_CheckExact(item)) {
2387 long value;
2388 int overflow;
2389 value = PyLong_AsLongAndOverflow(item, &overflow);
2390 if (!overflow) {
2391 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2392 f_result += (double)value;
2393 PyFPE_END_PROTECT(f_result)
2394 Py_DECREF(item);
2395 continue;
2396 }
2397 }
2398 result = PyFloat_FromDouble(f_result);
Alexey Izbyshev2b824b22018-08-24 07:27:52 +03002399 if (result == NULL) {
2400 Py_DECREF(item);
2401 Py_DECREF(iter);
2402 return NULL;
2403 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 temp = PyNumber_Add(result, item);
2405 Py_DECREF(result);
2406 Py_DECREF(item);
2407 result = temp;
2408 if (result == NULL) {
2409 Py_DECREF(iter);
2410 return NULL;
2411 }
2412 }
2413 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002414#endif
2415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 for(;;) {
2417 item = PyIter_Next(iter);
2418 if (item == NULL) {
2419 /* error, or end-of-sequence */
2420 if (PyErr_Occurred()) {
2421 Py_DECREF(result);
2422 result = NULL;
2423 }
2424 break;
2425 }
2426 /* It's tempting to use PyNumber_InPlaceAdd instead of
2427 PyNumber_Add here, to avoid quadratic running time
2428 when doing 'sum(list_of_lists, [])'. However, this
2429 would produce a change in behaviour: a snippet like
Mark Dickinson9acadc52009-10-26 14:19:42 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 empty = []
2432 sum([[x] for x in range(10)], empty)
Mark Dickinson9acadc52009-10-26 14:19:42 +00002433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 would change the value of empty. */
2435 temp = PyNumber_Add(result, item);
2436 Py_DECREF(result);
2437 Py_DECREF(item);
2438 result = temp;
2439 if (result == NULL)
2440 break;
2441 }
2442 Py_DECREF(iter);
2443 return result;
Alex Martellia70b1912003-04-22 08:12:33 +00002444}
2445
Alex Martellia70b1912003-04-22 08:12:33 +00002446
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002447/*[clinic input]
2448isinstance as builtin_isinstance
2449
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002450 obj: object
2451 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002452 /
2453
2454Return whether an object is an instance of a class or of a subclass thereof.
2455
2456A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
2457check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
2458or ...`` etc.
2459[clinic start generated code]*/
2460
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002461static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002462builtin_isinstance_impl(PyObject *module, PyObject *obj,
Larry Hastings89964c42015-04-14 18:07:59 -04002463 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002464/*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002467
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002468 retval = PyObject_IsInstance(obj, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 if (retval < 0)
2470 return NULL;
2471 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002472}
2473
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002474
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002475/*[clinic input]
2476issubclass as builtin_issubclass
2477
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002478 cls: object
2479 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002480 /
2481
2482Return whether 'cls' is a derived from another class or is the same class.
2483
2484A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
2485check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
2486or ...`` etc.
2487[clinic start generated code]*/
2488
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002489static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002490builtin_issubclass_impl(PyObject *module, PyObject *cls,
Larry Hastings89964c42015-04-14 18:07:59 -04002491 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002492/*[clinic end generated code: output=358412410cd7a250 input=af5f35e9ceaddaf6]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002495
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002496 retval = PyObject_IsSubclass(cls, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 if (retval < 0)
2498 return NULL;
2499 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002500}
2501
2502
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002503typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 PyObject_HEAD
2505 Py_ssize_t tuplesize;
2506 PyObject *ittuple; /* tuple of iterators */
2507 PyObject *result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002508} zipobject;
2509
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002510static PyObject *
2511zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Barry Warsawbd599b52000-08-03 15:45:29 +00002512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 zipobject *lz;
2514 Py_ssize_t i;
2515 PyObject *ittuple; /* tuple of iterators */
2516 PyObject *result;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002517 Py_ssize_t tuplesize;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002518
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002519 if (type == &PyZip_Type && !_PyArg_NoKeywords("zip", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 return NULL;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 /* args must be a tuple */
2523 assert(PyTuple_Check(args));
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002524 tuplesize = PyTuple_GET_SIZE(args);
Barry Warsawbd599b52000-08-03 15:45:29 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 /* obtain iterators */
2527 ittuple = PyTuple_New(tuplesize);
2528 if (ittuple == NULL)
2529 return NULL;
2530 for (i=0; i < tuplesize; ++i) {
2531 PyObject *item = PyTuple_GET_ITEM(args, i);
2532 PyObject *it = PyObject_GetIter(item);
2533 if (it == NULL) {
2534 if (PyErr_ExceptionMatches(PyExc_TypeError))
2535 PyErr_Format(PyExc_TypeError,
2536 "zip argument #%zd must support iteration",
2537 i+1);
2538 Py_DECREF(ittuple);
2539 return NULL;
2540 }
2541 PyTuple_SET_ITEM(ittuple, i, it);
2542 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 /* create a result holder */
2545 result = PyTuple_New(tuplesize);
2546 if (result == NULL) {
2547 Py_DECREF(ittuple);
2548 return NULL;
2549 }
2550 for (i=0 ; i < tuplesize ; i++) {
2551 Py_INCREF(Py_None);
2552 PyTuple_SET_ITEM(result, i, Py_None);
2553 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 /* create zipobject structure */
2556 lz = (zipobject *)type->tp_alloc(type, 0);
2557 if (lz == NULL) {
2558 Py_DECREF(ittuple);
2559 Py_DECREF(result);
2560 return NULL;
2561 }
2562 lz->ittuple = ittuple;
2563 lz->tuplesize = tuplesize;
2564 lz->result = result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 return (PyObject *)lz;
Barry Warsawbd599b52000-08-03 15:45:29 +00002567}
2568
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002569static void
2570zip_dealloc(zipobject *lz)
2571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 PyObject_GC_UnTrack(lz);
2573 Py_XDECREF(lz->ittuple);
2574 Py_XDECREF(lz->result);
2575 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002576}
2577
2578static int
2579zip_traverse(zipobject *lz, visitproc visit, void *arg)
2580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 Py_VISIT(lz->ittuple);
2582 Py_VISIT(lz->result);
2583 return 0;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002584}
2585
2586static PyObject *
2587zip_next(zipobject *lz)
2588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 Py_ssize_t i;
2590 Py_ssize_t tuplesize = lz->tuplesize;
2591 PyObject *result = lz->result;
2592 PyObject *it;
2593 PyObject *item;
2594 PyObject *olditem;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 if (tuplesize == 0)
2597 return NULL;
2598 if (Py_REFCNT(result) == 1) {
2599 Py_INCREF(result);
2600 for (i=0 ; i < tuplesize ; i++) {
2601 it = PyTuple_GET_ITEM(lz->ittuple, i);
2602 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002603 if (item == NULL) {
2604 Py_DECREF(result);
2605 return NULL;
2606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 olditem = PyTuple_GET_ITEM(result, i);
2608 PyTuple_SET_ITEM(result, i, item);
2609 Py_DECREF(olditem);
2610 }
2611 } else {
2612 result = PyTuple_New(tuplesize);
2613 if (result == NULL)
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002614 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 for (i=0 ; i < tuplesize ; i++) {
2616 it = PyTuple_GET_ITEM(lz->ittuple, i);
2617 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002618 if (item == NULL) {
2619 Py_DECREF(result);
2620 return NULL;
2621 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 PyTuple_SET_ITEM(result, i, item);
2623 }
2624 }
2625 return result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002626}
Barry Warsawbd599b52000-08-03 15:45:29 +00002627
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002628static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302629zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002630{
2631 /* Just recreate the zip with the internal iterator tuple */
2632 return Py_BuildValue("OO", Py_TYPE(lz), lz->ittuple);
2633}
2634
2635static PyMethodDef zip_methods[] = {
2636 {"__reduce__", (PyCFunction)zip_reduce, METH_NOARGS, reduce_doc},
2637 {NULL, NULL} /* sentinel */
2638};
2639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002640PyDoc_STRVAR(zip_doc,
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002641"zip(iter1 [,iter2 [...]]) --> zip object\n\
Barry Warsawbd599b52000-08-03 15:45:29 +00002642\n\
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002643Return a zip object whose .__next__() method returns a tuple where\n\
2644the i-th element comes from the i-th iterable argument. The .__next__()\n\
2645method continues until the shortest iterable in the argument sequence\n\
Georg Brandlced51db2008-12-04 18:28:38 +00002646is exhausted and then it raises StopIteration.");
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002647
2648PyTypeObject PyZip_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2650 "zip", /* tp_name */
2651 sizeof(zipobject), /* tp_basicsize */
2652 0, /* tp_itemsize */
2653 /* methods */
2654 (destructor)zip_dealloc, /* tp_dealloc */
2655 0, /* tp_print */
2656 0, /* tp_getattr */
2657 0, /* tp_setattr */
2658 0, /* tp_reserved */
2659 0, /* tp_repr */
2660 0, /* tp_as_number */
2661 0, /* tp_as_sequence */
2662 0, /* tp_as_mapping */
2663 0, /* tp_hash */
2664 0, /* tp_call */
2665 0, /* tp_str */
2666 PyObject_GenericGetAttr, /* tp_getattro */
2667 0, /* tp_setattro */
2668 0, /* tp_as_buffer */
2669 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2670 Py_TPFLAGS_BASETYPE, /* tp_flags */
2671 zip_doc, /* tp_doc */
2672 (traverseproc)zip_traverse, /* tp_traverse */
2673 0, /* tp_clear */
2674 0, /* tp_richcompare */
2675 0, /* tp_weaklistoffset */
2676 PyObject_SelfIter, /* tp_iter */
2677 (iternextfunc)zip_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002678 zip_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 0, /* tp_members */
2680 0, /* tp_getset */
2681 0, /* tp_base */
2682 0, /* tp_dict */
2683 0, /* tp_descr_get */
2684 0, /* tp_descr_set */
2685 0, /* tp_dictoffset */
2686 0, /* tp_init */
2687 PyType_GenericAlloc, /* tp_alloc */
2688 zip_new, /* tp_new */
2689 PyObject_GC_Del, /* tp_free */
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002690};
Barry Warsawbd599b52000-08-03 15:45:29 +00002691
2692
Guido van Rossum79f25d91997-04-29 20:08:16 +00002693static PyMethodDef builtin_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002694 {"__build_class__", (PyCFunction)(void(*)(void))builtin___build_class__,
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002695 METH_FASTCALL | METH_KEYWORDS, build_class_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002696 {"__import__", (PyCFunction)(void(*)(void))builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002697 BUILTIN_ABS_METHODDEF
2698 BUILTIN_ALL_METHODDEF
2699 BUILTIN_ANY_METHODDEF
2700 BUILTIN_ASCII_METHODDEF
2701 BUILTIN_BIN_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002702 {"breakpoint", (PyCFunction)(void(*)(void))builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002703 BUILTIN_CALLABLE_METHODDEF
2704 BUILTIN_CHR_METHODDEF
2705 BUILTIN_COMPILE_METHODDEF
2706 BUILTIN_DELATTR_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 {"dir", builtin_dir, METH_VARARGS, dir_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002708 BUILTIN_DIVMOD_METHODDEF
2709 BUILTIN_EVAL_METHODDEF
2710 BUILTIN_EXEC_METHODDEF
2711 BUILTIN_FORMAT_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002712 {"getattr", (PyCFunction)(void(*)(void))builtin_getattr, METH_FASTCALL, getattr_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002713 BUILTIN_GLOBALS_METHODDEF
2714 BUILTIN_HASATTR_METHODDEF
2715 BUILTIN_HASH_METHODDEF
2716 BUILTIN_HEX_METHODDEF
2717 BUILTIN_ID_METHODDEF
2718 BUILTIN_INPUT_METHODDEF
2719 BUILTIN_ISINSTANCE_METHODDEF
2720 BUILTIN_ISSUBCLASS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 {"iter", builtin_iter, METH_VARARGS, iter_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002722 BUILTIN_LEN_METHODDEF
2723 BUILTIN_LOCALS_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002724 {"max", (PyCFunction)(void(*)(void))builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2725 {"min", (PyCFunction)(void(*)(void))builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
2726 {"next", (PyCFunction)(void(*)(void))builtin_next, METH_FASTCALL, next_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002727 BUILTIN_OCT_METHODDEF
2728 BUILTIN_ORD_METHODDEF
2729 BUILTIN_POW_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002730 {"print", (PyCFunction)(void(*)(void))builtin_print, METH_FASTCALL | METH_KEYWORDS, print_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002731 BUILTIN_REPR_METHODDEF
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002732 BUILTIN_ROUND_METHODDEF
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002733 BUILTIN_SETATTR_METHODDEF
2734 BUILTIN_SORTED_METHODDEF
2735 BUILTIN_SUM_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 {"vars", builtin_vars, METH_VARARGS, vars_doc},
2737 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002738};
2739
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002740PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002741"Built-in functions, exceptions, and other objects.\n\
2742\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002743Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002744
Martin v. Löwis1a214512008-06-11 05:26:20 +00002745static struct PyModuleDef builtinsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 PyModuleDef_HEAD_INIT,
2747 "builtins",
2748 builtin_doc,
2749 -1, /* multiple "initialization" just copies the module dict. */
2750 builtin_methods,
2751 NULL,
2752 NULL,
2753 NULL,
2754 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002755};
2756
2757
Guido van Rossum25ce5661997-08-02 03:10:38 +00002758PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002759_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 PyObject *mod, *dict, *debug;
Benjamin Peterson42124a72012-10-30 23:41:54 -04002762
Victor Stinnerfbca9082018-08-30 00:50:45 +02002763 const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
2764
Benjamin Peterson42124a72012-10-30 23:41:54 -04002765 if (PyType_Ready(&PyFilter_Type) < 0 ||
2766 PyType_Ready(&PyMap_Type) < 0 ||
2767 PyType_Ready(&PyZip_Type) < 0)
2768 return NULL;
2769
Eric Snowd393c1b2017-09-14 12:18:12 -06002770 mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 if (mod == NULL)
2772 return NULL;
2773 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002774
Tim Peters7571a0f2003-03-23 17:52:28 +00002775#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 /* "builtins" exposes a number of statically allocated objects
2777 * that, before this code was added in 2.3, never showed up in
2778 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2779 * result, programs leaking references to None and False (etc)
2780 * couldn't be diagnosed by examining sys.getobjects(0).
2781 */
Tim Peters7571a0f2003-03-23 17:52:28 +00002782#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2783#else
2784#define ADD_TO_ALL(OBJECT) (void)0
2785#endif
2786
Tim Peters4b7625e2001-09-13 21:37:17 +00002787#define SETBUILTIN(NAME, OBJECT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2789 return NULL; \
2790 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 SETBUILTIN("None", Py_None);
2793 SETBUILTIN("Ellipsis", Py_Ellipsis);
2794 SETBUILTIN("NotImplemented", Py_NotImplemented);
2795 SETBUILTIN("False", Py_False);
2796 SETBUILTIN("True", Py_True);
2797 SETBUILTIN("bool", &PyBool_Type);
2798 SETBUILTIN("memoryview", &PyMemoryView_Type);
2799 SETBUILTIN("bytearray", &PyByteArray_Type);
2800 SETBUILTIN("bytes", &PyBytes_Type);
2801 SETBUILTIN("classmethod", &PyClassMethod_Type);
2802 SETBUILTIN("complex", &PyComplex_Type);
2803 SETBUILTIN("dict", &PyDict_Type);
2804 SETBUILTIN("enumerate", &PyEnum_Type);
2805 SETBUILTIN("filter", &PyFilter_Type);
2806 SETBUILTIN("float", &PyFloat_Type);
2807 SETBUILTIN("frozenset", &PyFrozenSet_Type);
2808 SETBUILTIN("property", &PyProperty_Type);
2809 SETBUILTIN("int", &PyLong_Type);
2810 SETBUILTIN("list", &PyList_Type);
2811 SETBUILTIN("map", &PyMap_Type);
2812 SETBUILTIN("object", &PyBaseObject_Type);
2813 SETBUILTIN("range", &PyRange_Type);
2814 SETBUILTIN("reversed", &PyReversed_Type);
2815 SETBUILTIN("set", &PySet_Type);
2816 SETBUILTIN("slice", &PySlice_Type);
2817 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2818 SETBUILTIN("str", &PyUnicode_Type);
2819 SETBUILTIN("super", &PySuper_Type);
2820 SETBUILTIN("tuple", &PyTuple_Type);
2821 SETBUILTIN("type", &PyType_Type);
2822 SETBUILTIN("zip", &PyZip_Type);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002823 debug = PyBool_FromLong(config->optimization_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002825 Py_DECREF(debug);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 return NULL;
2827 }
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002828 Py_DECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002831#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002832#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002833}