blob: a23bdc1078c9175d442c1a18cc5ab65ac01fdac7 [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"
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004#include "Python-ast.h"
Victor Stinnerfbca9082018-08-30 00:50:45 +02005#include "internal/pystate.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006
7#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009
Benjamin Petersonea281a52011-08-12 23:10:50 -050010#include "asdl.h"
11#include "ast.h"
12
Guido van Rossum6bf62da1997-04-11 20:37:35 +000013#include <ctype.h>
14
Victor Stinnerbd303c12013-11-07 23:07:29 +010015_Py_IDENTIFIER(__builtins__);
16_Py_IDENTIFIER(__dict__);
17_Py_IDENTIFIER(__prepare__);
18_Py_IDENTIFIER(__round__);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010019_Py_IDENTIFIER(__mro_entries__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010020_Py_IDENTIFIER(encoding);
21_Py_IDENTIFIER(errors);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020022_Py_IDENTIFIER(fileno);
23_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010024_Py_IDENTIFIER(metaclass);
25_Py_IDENTIFIER(sort);
26_Py_IDENTIFIER(stdin);
27_Py_IDENTIFIER(stdout);
28_Py_IDENTIFIER(stderr);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020029
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030030#include "clinic/bltinmodule.c.h"
31
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010032static PyObject*
Victor Stinner05d68a82018-01-18 11:15:25 +010033update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010034{
Victor Stinner05d68a82018-01-18 11:15:25 +010035 Py_ssize_t i, j;
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010036 PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
37 PyObject *stack[1] = {bases};
38 assert(PyTuple_Check(bases));
39
40 for (i = 0; i < nargs; i++) {
41 base = args[i];
42 if (PyType_Check(base)) {
43 if (new_bases) {
44 /* If we already have made a replacement, then we append every normal base,
45 otherwise just skip it. */
46 if (PyList_Append(new_bases, base) < 0) {
47 goto error;
48 }
49 }
50 continue;
51 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +020052 if (_PyObject_LookupAttrId(base, &PyId___mro_entries__, &meth) < 0) {
53 goto error;
54 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010055 if (!meth) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010056 if (new_bases) {
57 if (PyList_Append(new_bases, base) < 0) {
58 goto error;
59 }
60 }
61 continue;
62 }
63 new_base = _PyObject_FastCall(meth, stack, 1);
64 Py_DECREF(meth);
65 if (!new_base) {
66 goto error;
67 }
68 if (!PyTuple_Check(new_base)) {
69 PyErr_SetString(PyExc_TypeError,
70 "__mro_entries__ must return a tuple");
71 Py_DECREF(new_base);
72 goto error;
73 }
74 if (!new_bases) {
75 /* If this is a first successful replacement, create new_bases list and
76 copy previously encountered bases. */
77 if (!(new_bases = PyList_New(i))) {
78 goto error;
79 }
80 for (j = 0; j < i; j++) {
81 base = args[j];
82 PyList_SET_ITEM(new_bases, j, base);
83 Py_INCREF(base);
84 }
85 }
86 j = PyList_GET_SIZE(new_bases);
87 if (PyList_SetSlice(new_bases, j, j, new_base) < 0) {
88 goto error;
89 }
90 Py_DECREF(new_base);
91 }
92 if (!new_bases) {
93 return bases;
94 }
95 result = PyList_AsTuple(new_bases);
96 Py_DECREF(new_bases);
97 return result;
98
99error:
100 Py_XDECREF(new_bases);
101 return NULL;
102}
103
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000104/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000105static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200106builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
Victor Stinner773dc6d2017-01-16 23:46:26 +0100107 PyObject *kwnames)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000108{
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100109 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *orig_bases;
Nick Coghlan19d24672016-12-05 16:47:55 +1000110 PyObject *cls = NULL, *cell = NULL;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100111 int isclass = 0; /* initialize to prevent gcc warning */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 if (nargs < 2) {
114 PyErr_SetString(PyExc_TypeError,
115 "__build_class__: not enough arguments");
116 return NULL;
117 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100118 func = args[0]; /* Better be callable */
Benjamin Petersone8e14592013-05-16 14:37:25 -0500119 if (!PyFunction_Check(func)) {
120 PyErr_SetString(PyExc_TypeError,
Zachary Ware9b338722014-08-05 14:01:10 -0500121 "__build_class__: func must be a function");
Benjamin Petersone8e14592013-05-16 14:37:25 -0500122 return NULL;
123 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100124 name = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (!PyUnicode_Check(name)) {
126 PyErr_SetString(PyExc_TypeError,
127 "__build_class__: name is not a string");
128 return NULL;
129 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100130 orig_bases = _PyStack_AsTupleSlice(args, nargs, 2, nargs);
131 if (orig_bases == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000133
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100134 bases = update_bases(orig_bases, args + 2, nargs - 2);
135 if (bases == NULL) {
136 Py_DECREF(orig_bases);
137 return NULL;
138 }
139
Victor Stinner773dc6d2017-01-16 23:46:26 +0100140 if (kwnames == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 meta = NULL;
142 mkw = NULL;
143 }
144 else {
Victor Stinner773dc6d2017-01-16 23:46:26 +0100145 mkw = _PyStack_AsDict(args + nargs, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (mkw == NULL) {
147 Py_DECREF(bases);
148 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000149 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100150
Victor Stinnerae9f1612013-11-06 22:46:51 +0100151 meta = _PyDict_GetItemId(mkw, &PyId_metaclass);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 if (meta != NULL) {
153 Py_INCREF(meta);
Victor Stinnerae9f1612013-11-06 22:46:51 +0100154 if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 Py_DECREF(meta);
156 Py_DECREF(mkw);
157 Py_DECREF(bases);
158 return NULL;
159 }
Nick Coghlande31b192011-10-23 22:04:16 +1000160 /* metaclass is explicitly given, check if it's indeed a class */
161 isclass = PyType_Check(meta);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 }
163 }
164 if (meta == NULL) {
Nick Coghlande31b192011-10-23 22:04:16 +1000165 /* if there are no bases, use type: */
166 if (PyTuple_GET_SIZE(bases) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 meta = (PyObject *) (&PyType_Type);
Nick Coghlande31b192011-10-23 22:04:16 +1000168 }
169 /* else get the type of the first base */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 else {
171 PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
172 meta = (PyObject *) (base0->ob_type);
173 }
174 Py_INCREF(meta);
Nick Coghlande31b192011-10-23 22:04:16 +1000175 isclass = 1; /* meta is really a class */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 }
Nick Coghlan9715d262011-10-23 22:36:42 +1000177
Nick Coghlande31b192011-10-23 22:04:16 +1000178 if (isclass) {
179 /* meta is really a class, so check for a more derived
180 metaclass, or possible metaclass conflicts: */
181 winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
182 bases);
183 if (winner == NULL) {
184 Py_DECREF(meta);
185 Py_XDECREF(mkw);
186 Py_DECREF(bases);
187 return NULL;
188 }
189 if (winner != meta) {
190 Py_DECREF(meta);
191 meta = winner;
192 Py_INCREF(meta);
193 }
194 }
195 /* else: meta is not a class, so we cannot do the metaclass
196 calculation, so we will use the explicitly given object as it is */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200197 if (_PyObject_LookupAttrId(meta, &PyId___prepare__, &prep) < 0) {
198 ns = NULL;
199 }
200 else if (prep == NULL) {
201 ns = PyDict_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
203 else {
Victor Stinner463b86a2016-08-22 23:33:13 +0200204 PyObject *pargs[2] = {name, bases};
205 ns = _PyObject_FastCallDict(prep, pargs, 2, mkw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 Py_DECREF(prep);
207 }
208 if (ns == NULL) {
209 Py_DECREF(meta);
210 Py_XDECREF(mkw);
211 Py_DECREF(bases);
212 return NULL;
213 }
Oren Milman5837d042017-09-27 17:04:37 +0300214 if (!PyMapping_Check(ns)) {
215 PyErr_Format(PyExc_TypeError,
216 "%.200s.__prepare__() must return a mapping, not %.200s",
217 isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
218 Py_TYPE(ns)->tp_name);
219 goto error;
220 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000221 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500222 NULL, 0, NULL, 0, NULL, 0, NULL,
223 PyFunction_GET_CLOSURE(func));
Nick Coghlan19d24672016-12-05 16:47:55 +1000224 if (cell != NULL) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100225 if (bases != orig_bases) {
226 if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
227 goto error;
228 }
229 }
Victor Stinnerd1c2a8e2016-08-23 01:34:35 +0200230 PyObject *margs[3] = {name, bases, ns};
231 cls = _PyObject_FastCallDict(meta, margs, 3, mkw);
Nick Coghlan19d24672016-12-05 16:47:55 +1000232 if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
233 PyObject *cell_cls = PyCell_GET(cell);
234 if (cell_cls != cls) {
Nick Coghlan19d24672016-12-05 16:47:55 +1000235 if (cell_cls == NULL) {
236 const char *msg =
237 "__class__ not set defining %.200R as %.200R. "
238 "Was __classcell__ propagated to type.__new__?";
Serhiy Storchakaf5e7b192018-05-20 08:48:12 +0300239 PyErr_Format(PyExc_RuntimeError, msg, name, cls);
Nick Coghlan19d24672016-12-05 16:47:55 +1000240 } else {
241 const char *msg =
242 "__class__ set to %.200R defining %.200R as %.200R";
243 PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
Nick Coghlan19d24672016-12-05 16:47:55 +1000244 }
Serhiy Storchakaf5e7b192018-05-20 08:48:12 +0300245 Py_DECREF(cls);
246 cls = NULL;
247 goto error;
Nick Coghlan19d24672016-12-05 16:47:55 +1000248 }
249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000251error:
252 Py_XDECREF(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 Py_DECREF(ns);
254 Py_DECREF(meta);
255 Py_XDECREF(mkw);
256 Py_DECREF(bases);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100257 if (bases != orig_bases) {
258 Py_DECREF(orig_bases);
259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 return cls;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000261}
262
263PyDoc_STRVAR(build_class_doc,
264"__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
265\n\
266Internal helper function used by the class statement.");
267
268static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
272 "level", 0};
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400273 PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -0400274 int level = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000275
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400276 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 kwlist, &name, &globals, &locals, &fromlist, &level))
278 return NULL;
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400279 return PyImport_ImportModuleLevelObject(name, globals, locals,
280 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000281}
282
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000283PyDoc_STRVAR(import_doc,
Brett Cannoncb4996a2012-08-06 16:34:44 -0400284"__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000285\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000286Import a module. Because this function is meant for use by the Python\n\
oldk461d2252018-02-02 12:20:00 +0800287interpreter and not for general use, it is better to use\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000288importlib.import_module() to programmatically import a module.\n\
289\n\
290The globals argument is only used to determine the context;\n\
291they are not modified. The locals argument is unused. The fromlist\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000292should be a list of names to emulate ``from name import ...'', or an\n\
293empty list to emulate ``import name''.\n\
294When importing a module from a package, note that __import__('A.B', ...)\n\
295returns package A when fromlist is empty, but its submodule B when\n\
oldk461d2252018-02-02 12:20:00 +0800296fromlist is not empty. The level argument is used to determine whether to\n\
297perform absolute or relative imports: 0 is absolute, while a positive number\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000299
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000300
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000301/*[clinic input]
302abs as builtin_abs
303
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300304 x: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000305 /
306
307Return the absolute value of the argument.
308[clinic start generated code]*/
309
Guido van Rossum79f25d91997-04-29 20:08:16 +0000310static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300311builtin_abs(PyObject *module, PyObject *x)
312/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000313{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000314 return PyNumber_Absolute(x);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000315}
316
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000317/*[clinic input]
318all as builtin_all
319
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300320 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000321 /
322
323Return True if bool(x) is True for all values x in the iterable.
324
325If the iterable is empty, return True.
326[clinic start generated code]*/
327
Raymond Hettinger96229b12005-03-11 06:49:40 +0000328static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300329builtin_all(PyObject *module, PyObject *iterable)
330/*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyObject *it, *item;
333 PyObject *(*iternext)(PyObject *);
334 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000335
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000336 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 if (it == NULL)
338 return NULL;
339 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 for (;;) {
342 item = iternext(it);
343 if (item == NULL)
344 break;
345 cmp = PyObject_IsTrue(item);
346 Py_DECREF(item);
347 if (cmp < 0) {
348 Py_DECREF(it);
349 return NULL;
350 }
351 if (cmp == 0) {
352 Py_DECREF(it);
353 Py_RETURN_FALSE;
354 }
355 }
356 Py_DECREF(it);
357 if (PyErr_Occurred()) {
358 if (PyErr_ExceptionMatches(PyExc_StopIteration))
359 PyErr_Clear();
360 else
361 return NULL;
362 }
363 Py_RETURN_TRUE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000364}
365
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000366/*[clinic input]
367any as builtin_any
368
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300369 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000370 /
371
372Return True if bool(x) is True for any x in the iterable.
373
374If the iterable is empty, return False.
375[clinic start generated code]*/
376
Raymond Hettinger96229b12005-03-11 06:49:40 +0000377static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300378builtin_any(PyObject *module, PyObject *iterable)
379/*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 PyObject *it, *item;
382 PyObject *(*iternext)(PyObject *);
383 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000384
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000385 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (it == NULL)
387 return NULL;
388 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 for (;;) {
391 item = iternext(it);
392 if (item == NULL)
393 break;
394 cmp = PyObject_IsTrue(item);
395 Py_DECREF(item);
396 if (cmp < 0) {
397 Py_DECREF(it);
398 return NULL;
399 }
Raymond Hettinger5098b582015-10-09 00:42:47 -0400400 if (cmp > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 Py_DECREF(it);
402 Py_RETURN_TRUE;
403 }
404 }
405 Py_DECREF(it);
406 if (PyErr_Occurred()) {
407 if (PyErr_ExceptionMatches(PyExc_StopIteration))
408 PyErr_Clear();
409 else
410 return NULL;
411 }
412 Py_RETURN_FALSE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000413}
414
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000415/*[clinic input]
416ascii as builtin_ascii
417
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300418 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000419 /
420
421Return an ASCII-only representation of an object.
422
423As repr(), return a string containing a printable representation of an
424object, but escape the non-ASCII characters in the string returned by
425repr() using \\x, \\u or \\U escapes. This generates a string similar
426to that returned by repr() in Python 2.
427[clinic start generated code]*/
428
Georg Brandl559e5d72008-06-11 18:37:52 +0000429static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300430builtin_ascii(PyObject *module, PyObject *obj)
431/*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
Georg Brandl559e5d72008-06-11 18:37:52 +0000432{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000433 return PyObject_ASCII(obj);
Georg Brandl559e5d72008-06-11 18:37:52 +0000434}
435
Georg Brandl559e5d72008-06-11 18:37:52 +0000436
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000437/*[clinic input]
438bin as builtin_bin
439
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300440 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000441 /
442
443Return the binary representation of an integer.
444
445 >>> bin(2796202)
446 '0b1010101010101010101010'
447[clinic start generated code]*/
448
Guido van Rossum79f25d91997-04-29 20:08:16 +0000449static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300450builtin_bin(PyObject *module, PyObject *number)
451/*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000452{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000453 return PyNumber_ToBase(number, 2);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000454}
455
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000456
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000457/*[clinic input]
458callable as builtin_callable
459
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300460 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000461 /
462
463Return whether the object is callable (i.e., some kind of function).
464
465Note that classes are callable, as are instances of classes with a
466__call__() method.
467[clinic start generated code]*/
468
Antoine Pitroue71362d2010-11-27 22:00:11 +0000469static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300470builtin_callable(PyObject *module, PyObject *obj)
471/*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
Antoine Pitroue71362d2010-11-27 22:00:11 +0000472{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000473 return PyBool_FromLong((long)PyCallable_Check(obj));
Antoine Pitroue71362d2010-11-27 22:00:11 +0000474}
475
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400476static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200477builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400478{
479 PyObject *hook = PySys_GetObject("breakpointhook");
480
481 if (hook == NULL) {
482 PyErr_SetString(PyExc_RuntimeError, "lost sys.breakpointhook");
483 return NULL;
484 }
485 Py_INCREF(hook);
486 PyObject *retval = _PyObject_FastCallKeywords(hook, args, nargs, keywords);
487 Py_DECREF(hook);
488 return retval;
489}
490
491PyDoc_STRVAR(breakpoint_doc,
492"breakpoint(*args, **kws)\n\
493\n\
494Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept\n\
495whatever arguments are passed.\n\
496\n\
497By default, this drops you into the pdb debugger.");
Antoine Pitroue71362d2010-11-27 22:00:11 +0000498
Raymond Hettinger17301e92008-03-13 00:19:26 +0000499typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyObject_HEAD
501 PyObject *func;
502 PyObject *it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000503} filterobject;
504
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000505static PyObject *
Raymond Hettinger17301e92008-03-13 00:19:26 +0000506filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 PyObject *func, *seq;
509 PyObject *it;
510 filterobject *lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000511
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +0300512 if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
516 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 /* Get iterator. */
519 it = PyObject_GetIter(seq);
520 if (it == NULL)
521 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 /* create filterobject structure */
524 lz = (filterobject *)type->tp_alloc(type, 0);
525 if (lz == NULL) {
526 Py_DECREF(it);
527 return NULL;
528 }
529 Py_INCREF(func);
530 lz->func = func;
531 lz->it = it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return (PyObject *)lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000534}
535
536static void
537filter_dealloc(filterobject *lz)
538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 PyObject_GC_UnTrack(lz);
540 Py_XDECREF(lz->func);
541 Py_XDECREF(lz->it);
542 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger17301e92008-03-13 00:19:26 +0000543}
544
545static int
546filter_traverse(filterobject *lz, visitproc visit, void *arg)
547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 Py_VISIT(lz->it);
549 Py_VISIT(lz->func);
550 return 0;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000551}
552
553static PyObject *
554filter_next(filterobject *lz)
555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 PyObject *item;
557 PyObject *it = lz->it;
558 long ok;
559 PyObject *(*iternext)(PyObject *);
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400560 int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 iternext = *Py_TYPE(it)->tp_iternext;
563 for (;;) {
564 item = iternext(it);
565 if (item == NULL)
566 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000567
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400568 if (checktrue) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 ok = PyObject_IsTrue(item);
570 } else {
571 PyObject *good;
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100572 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (good == NULL) {
574 Py_DECREF(item);
575 return NULL;
576 }
577 ok = PyObject_IsTrue(good);
578 Py_DECREF(good);
579 }
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200580 if (ok > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return item;
582 Py_DECREF(item);
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200583 if (ok < 0)
584 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000586}
587
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000588static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530589filter_reduce(filterobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000590{
591 return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
592}
593
594PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
595
596static PyMethodDef filter_methods[] = {
597 {"__reduce__", (PyCFunction)filter_reduce, METH_NOARGS, reduce_doc},
598 {NULL, NULL} /* sentinel */
599};
600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000601PyDoc_STRVAR(filter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +0000602"filter(function or None, iterable) --> filter object\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000603\n\
Georg Brandld11ae5d2008-05-16 13:27:32 +0000604Return an iterator yielding those items of iterable for which function(item)\n\
Raymond Hettinger17301e92008-03-13 00:19:26 +0000605is true. If function is None, return the items that are true.");
606
607PyTypeObject PyFilter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyVarObject_HEAD_INIT(&PyType_Type, 0)
609 "filter", /* tp_name */
610 sizeof(filterobject), /* tp_basicsize */
611 0, /* tp_itemsize */
612 /* methods */
613 (destructor)filter_dealloc, /* tp_dealloc */
614 0, /* tp_print */
615 0, /* tp_getattr */
616 0, /* tp_setattr */
617 0, /* tp_reserved */
618 0, /* tp_repr */
619 0, /* tp_as_number */
620 0, /* tp_as_sequence */
621 0, /* tp_as_mapping */
622 0, /* tp_hash */
623 0, /* tp_call */
624 0, /* tp_str */
625 PyObject_GenericGetAttr, /* tp_getattro */
626 0, /* tp_setattro */
627 0, /* tp_as_buffer */
628 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
629 Py_TPFLAGS_BASETYPE, /* tp_flags */
630 filter_doc, /* tp_doc */
631 (traverseproc)filter_traverse, /* tp_traverse */
632 0, /* tp_clear */
633 0, /* tp_richcompare */
634 0, /* tp_weaklistoffset */
635 PyObject_SelfIter, /* tp_iter */
636 (iternextfunc)filter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000637 filter_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 0, /* tp_members */
639 0, /* tp_getset */
640 0, /* tp_base */
641 0, /* tp_dict */
642 0, /* tp_descr_get */
643 0, /* tp_descr_set */
644 0, /* tp_dictoffset */
645 0, /* tp_init */
646 PyType_GenericAlloc, /* tp_alloc */
647 filter_new, /* tp_new */
648 PyObject_GC_Del, /* tp_free */
Raymond Hettinger17301e92008-03-13 00:19:26 +0000649};
650
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000651
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000652/*[clinic input]
653format as builtin_format
654
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300655 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000656 format_spec: unicode(c_default="NULL") = ''
657 /
658
659Return value.__format__(format_spec)
660
Amit Kumar2e6bb442017-05-29 06:32:26 +0530661format_spec defaults to the empty string.
662See the Format Specification Mini-Language section of help('FORMATTING') for
663details.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000664[clinic start generated code]*/
665
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000666static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300667builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
Amit Kumar2e6bb442017-05-29 06:32:26 +0530668/*[clinic end generated code: output=2f40bdfa4954b077 input=88339c93ea522b33]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000669{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000670 return PyObject_Format(value, format_spec);
Eric Smith8c663262007-08-25 02:26:07 +0000671}
672
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000673/*[clinic input]
674chr as builtin_chr
675
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300676 i: int
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000677 /
678
679Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
680[clinic start generated code]*/
681
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000682static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300683builtin_chr_impl(PyObject *module, int i)
684/*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000685{
686 return PyUnicode_FromOrdinal(i);
687}
Guido van Rossum09095f32000-03-10 23:00:52 +0000688
689
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200690static const char *
Martin Pantereeb896c2015-11-07 02:32:21 +0000691source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000692{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200693 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 Py_ssize_t size;
Martin Pantereeb896c2015-11-07 02:32:21 +0000695 Py_buffer view;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000696
Martin Pantereeb896c2015-11-07 02:32:21 +0000697 *cmd_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (PyUnicode_Check(cmd)) {
699 cf->cf_flags |= PyCF_IGNORE_COOKIE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200700 str = PyUnicode_AsUTF8AndSize(cmd, &size);
701 if (str == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 return NULL;
703 }
Martin Pantereeb896c2015-11-07 02:32:21 +0000704 else if (PyBytes_Check(cmd)) {
705 str = PyBytes_AS_STRING(cmd);
706 size = PyBytes_GET_SIZE(cmd);
707 }
708 else if (PyByteArray_Check(cmd)) {
709 str = PyByteArray_AS_STRING(cmd);
710 size = PyByteArray_GET_SIZE(cmd);
711 }
712 else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
713 /* Copy to NUL-terminated buffer. */
714 *cmd_copy = PyBytes_FromStringAndSize(
715 (const char *)view.buf, view.len);
716 PyBuffer_Release(&view);
717 if (*cmd_copy == NULL) {
718 return NULL;
719 }
720 str = PyBytes_AS_STRING(*cmd_copy);
721 size = PyBytes_GET_SIZE(*cmd_copy);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200722 }
723 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 PyErr_Format(PyExc_TypeError,
725 "%s() arg 1 must be a %s object",
726 funcname, what);
727 return NULL;
728 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200729
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +0200730 if (strlen(str) != (size_t)size) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300731 PyErr_SetString(PyExc_ValueError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 "source code string cannot contain null bytes");
Martin Pantereeb896c2015-11-07 02:32:21 +0000733 Py_CLEAR(*cmd_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 return NULL;
735 }
736 return str;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000737}
738
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000739/*[clinic input]
740compile as builtin_compile
741
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300742 source: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000743 filename: object(converter="PyUnicode_FSDecoder")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300744 mode: str
745 flags: int = 0
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200746 dont_inherit: bool(accept={int}) = False
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300747 optimize: int = -1
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000748
749Compile source into a code object that can be executed by exec() or eval().
750
751The source code may represent a Python module, statement or expression.
752The filename will be used for run-time error messages.
753The mode must be 'exec' to compile a module, 'single' to compile a
754single (interactive) statement, or 'eval' to compile an expression.
755The flags argument, if present, controls which future statements influence
756the compilation of the code.
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300757The dont_inherit argument, if true, stops the compilation inheriting
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000758the effects of any future statements in effect in the code calling
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300759compile; if absent or false these statements do influence the compilation,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000760in addition to any features explicitly specified.
761[clinic start generated code]*/
762
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000763static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300764builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
765 const char *mode, int flags, int dont_inherit,
766 int optimize)
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200767/*[clinic end generated code: output=1fa176e33452bb63 input=0ff726f595eb9fcd]*/
Guido van Rossum5b722181993-03-30 17:46:03 +0000768{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000769 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200770 const char *str;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000771 int compile_mode = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 int is_ast;
773 PyCompilerFlags cf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000775 PyObject *result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000776
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000777 cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
Tim Peters6cd6a822001-08-17 22:11:27 +0000778
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000779 if (flags &
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
781 {
782 PyErr_SetString(PyExc_ValueError,
783 "compile(): unrecognised flags");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000784 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 }
786 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
Tim Peters6cd6a822001-08-17 22:11:27 +0000787
Georg Brandl8334fd92010-12-04 10:26:46 +0000788 if (optimize < -1 || optimize > 2) {
789 PyErr_SetString(PyExc_ValueError,
790 "compile(): invalid optimize value");
791 goto error;
792 }
793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (!dont_inherit) {
795 PyEval_MergeCompilerFlags(&cf);
796 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000797
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000798 if (strcmp(mode, "exec") == 0)
799 compile_mode = 0;
800 else if (strcmp(mode, "eval") == 0)
801 compile_mode = 1;
802 else if (strcmp(mode, "single") == 0)
803 compile_mode = 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 else {
805 PyErr_SetString(PyExc_ValueError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000806 "compile() mode must be 'exec', 'eval' or 'single'");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000807 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 }
Neal Norwitzdb4115f2008-03-31 04:20:05 +0000809
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000810 is_ast = PyAST_Check(source);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 if (is_ast == -1)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000812 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (is_ast) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000814 if (flags & PyCF_ONLY_AST) {
815 Py_INCREF(source);
816 result = source;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 }
818 else {
819 PyArena *arena;
820 mod_ty mod;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 arena = PyArena_New();
Stefan Krah07795df2012-08-20 17:19:50 +0200823 if (arena == NULL)
824 goto error;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000825 mod = PyAST_obj2mod(source, arena, compile_mode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 if (mod == NULL) {
827 PyArena_Free(arena);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000828 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 }
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500830 if (!PyAST_Validate(mod)) {
831 PyArena_Free(arena);
832 goto error;
833 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200834 result = (PyObject*)PyAST_CompileObject(mod, filename,
835 &cf, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 PyArena_Free(arena);
837 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000838 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000840
Martin Panter61d6e4a2015-11-07 02:56:11 +0000841 str = source_as_string(source, "compile", "string, bytes or AST", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 if (str == NULL)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000843 goto error;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000844
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000845 result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000846 Py_XDECREF(source_copy);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000847 goto finally;
848
849error:
850 result = NULL;
851finally:
Victor Stinner14e461d2013-08-26 22:28:21 +0200852 Py_DECREF(filename);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000853 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000854}
855
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000856/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000858builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
863 return NULL;
864 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000865}
866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000867PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000868"dir([object]) -> list of strings\n"
869"\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000870"If called without an argument, return the names in the current scope.\n"
871"Else, return an alphabetized list of names comprising (some of) the attributes\n"
872"of the given object, and of attributes reachable from it.\n"
873"If the object supplies a method named __dir__, it will be used; otherwise\n"
874"the default dir() logic is used and returns:\n"
875" for a module object: the module's attributes.\n"
876" for a class object: its attributes, and recursively the attributes\n"
877" of its bases.\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000878" for any other object: its attributes, its class's attributes, and\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000879" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000880
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000881/*[clinic input]
882divmod as builtin_divmod
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000883
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300884 x: object
885 y: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000886 /
887
Zachary Ware7f227d92016-04-28 14:39:50 -0500888Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000889[clinic start generated code]*/
890
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000891static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300892builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
893/*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000894{
895 return PyNumber_Divmod(x, y);
896}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000897
898
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000899/*[clinic input]
900eval as builtin_eval
901
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300902 source: object
903 globals: object = None
904 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000905 /
906
907Evaluate the given source in the context of globals and locals.
908
909The source may be a string representing a Python expression
910or a code object as returned by compile().
911The globals must be a dictionary and locals can be any mapping,
912defaulting to the current globals and locals.
913If only globals is given, locals defaults to it.
914[clinic start generated code]*/
915
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000916static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300917builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400918 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300919/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000920{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000921 PyObject *result, *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200922 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (locals != Py_None && !PyMapping_Check(locals)) {
926 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
927 return NULL;
928 }
929 if (globals != Py_None && !PyDict_Check(globals)) {
930 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
931 "globals must be a real dict; try eval(expr, {}, mapping)"
932 : "globals must be a dict");
933 return NULL;
934 }
935 if (globals == Py_None) {
936 globals = PyEval_GetGlobals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100937 if (locals == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100939 if (locals == NULL)
940 return NULL;
941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 }
943 else if (locals == Py_None)
944 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 if (globals == NULL || locals == NULL) {
947 PyErr_SetString(PyExc_TypeError,
948 "eval must be given globals and locals "
949 "when called without a frame");
950 return NULL;
951 }
Georg Brandl77c85e62005-09-15 10:46:13 +0000952
Victor Stinnerb44562b2013-11-06 19:03:11 +0100953 if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
954 if (_PyDict_SetItemId(globals, &PyId___builtins__,
955 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 return NULL;
957 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000958
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000959 if (PyCode_Check(source)) {
960 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 PyErr_SetString(PyExc_TypeError,
962 "code object passed to eval() may not contain free variables");
963 return NULL;
964 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000965 return PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Martin Panter61d6e4a2015-11-07 02:56:11 +0000969 str = source_as_string(source, "eval", "string, bytes or code", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 if (str == NULL)
971 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 while (*str == ' ' || *str == '\t')
974 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 (void)PyEval_MergeCompilerFlags(&cf);
977 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000978 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000980}
981
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000982/*[clinic input]
983exec as builtin_exec
984
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300985 source: object
986 globals: object = None
987 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000988 /
989
990Execute the given source in the context of globals and locals.
991
992The source may be a string representing one or more Python statements
993or a code object as returned by compile().
994The globals must be a dictionary and locals can be any mapping,
995defaulting to the current globals and locals.
996If only globals is given, locals defaults to it.
997[clinic start generated code]*/
998
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000999static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001000builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -04001001 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001002/*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
Georg Brandl7cae87c2006-09-06 06:51:57 +00001003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 PyObject *v;
Georg Brandl2cabc562008-08-28 07:57:16 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if (globals == Py_None) {
1007 globals = PyEval_GetGlobals();
1008 if (locals == Py_None) {
1009 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01001010 if (locals == NULL)
1011 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 }
1013 if (!globals || !locals) {
1014 PyErr_SetString(PyExc_SystemError,
1015 "globals and locals cannot be NULL");
1016 return NULL;
1017 }
1018 }
1019 else if (locals == Py_None)
1020 locals = globals;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 if (!PyDict_Check(globals)) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001023 PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 globals->ob_type->tp_name);
1025 return NULL;
1026 }
1027 if (!PyMapping_Check(locals)) {
1028 PyErr_Format(PyExc_TypeError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001029 "locals must be a mapping or None, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 locals->ob_type->tp_name);
1031 return NULL;
1032 }
Victor Stinnerb44562b2013-11-06 19:03:11 +01001033 if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
1034 if (_PyDict_SetItemId(globals, &PyId___builtins__,
1035 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 return NULL;
1037 }
1038
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001039 if (PyCode_Check(source)) {
1040 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 PyErr_SetString(PyExc_TypeError,
1042 "code object passed to exec() may not "
1043 "contain free variables");
1044 return NULL;
1045 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001046 v = PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 }
1048 else {
Martin Panter61d6e4a2015-11-07 02:56:11 +00001049 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001050 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 PyCompilerFlags cf;
1052 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001053 str = source_as_string(source, "exec",
Martin Panter61d6e4a2015-11-07 02:56:11 +00001054 "string, bytes or code", &cf,
1055 &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if (str == NULL)
1057 return NULL;
1058 if (PyEval_MergeCompilerFlags(&cf))
1059 v = PyRun_StringFlags(str, Py_file_input, globals,
1060 locals, &cf);
1061 else
1062 v = PyRun_String(str, Py_file_input, globals, locals);
Martin Panter61d6e4a2015-11-07 02:56:11 +00001063 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 }
1065 if (v == NULL)
1066 return NULL;
1067 Py_DECREF(v);
1068 Py_RETURN_NONE;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001069}
1070
Georg Brandl7cae87c2006-09-06 06:51:57 +00001071
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001072/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001073static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001074builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Guido van Rossum33894be1992-01-27 16:53:09 +00001075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 PyObject *v, *result, *dflt = NULL;
1077 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001078
Sylvain96c7c062017-06-15 17:05:23 +02001079 if (!_PyArg_UnpackStack(args, nargs, "getattr", 2, 3, &v, &name, &dflt))
1080 return NULL;
1081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 if (!PyUnicode_Check(name)) {
1083 PyErr_SetString(PyExc_TypeError,
1084 "getattr(): attribute name must be string");
1085 return NULL;
1086 }
INADA Naoki378edee2018-01-16 20:52:41 +09001087 if (dflt != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001088 if (_PyObject_LookupAttr(v, name, &result) == 0) {
INADA Naoki378edee2018-01-16 20:52:41 +09001089 Py_INCREF(dflt);
1090 return dflt;
1091 }
1092 }
1093 else {
1094 result = PyObject_GetAttr(v, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 }
1096 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001097}
1098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001099PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +00001100"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001101\n\
Guido van Rossum950ff291998-06-29 13:38:57 +00001102Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
1103When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001104exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001105
1106
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001107/*[clinic input]
1108globals as builtin_globals
1109
1110Return the dictionary containing the current scope's global variables.
1111
1112NOTE: Updates to this dictionary *will* affect name lookups in the current
1113global scope and vice-versa.
1114[clinic start generated code]*/
1115
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001116static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001117builtin_globals_impl(PyObject *module)
1118/*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 d = PyEval_GetGlobals();
1123 Py_XINCREF(d);
1124 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001125}
1126
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001127
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001128/*[clinic input]
1129hasattr as builtin_hasattr
1130
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001131 obj: object
1132 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001133 /
1134
1135Return whether the object has an attribute with the given name.
1136
1137This is done by calling getattr(obj, name) and catching AttributeError.
1138[clinic start generated code]*/
1139
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001140static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001141builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1142/*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001143{
1144 PyObject *v;
1145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (!PyUnicode_Check(name)) {
1147 PyErr_SetString(PyExc_TypeError,
1148 "hasattr(): attribute name must be string");
1149 return NULL;
1150 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001151 if (_PyObject_LookupAttr(obj, name, &v) < 0) {
Benjamin Peterson17689992010-08-24 03:26:23 +00001152 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001154 if (v == NULL) {
1155 Py_RETURN_FALSE;
1156 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 Py_DECREF(v);
Benjamin Peterson17689992010-08-24 03:26:23 +00001158 Py_RETURN_TRUE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001159}
1160
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001161
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001162/* AC: gdb's integration with CPython relies on builtin_id having
1163 * the *exact* parameter names of "self" and "v", so we ensure we
1164 * preserve those name rather than using the AC defaults.
1165 */
1166/*[clinic input]
1167id as builtin_id
1168
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001169 self: self(type="PyModuleDef *")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001170 obj as v: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001171 /
1172
1173Return the identity of an object.
1174
1175This is guaranteed to be unique among simultaneously existing objects.
1176(CPython uses the object's memory address.)
1177[clinic start generated code]*/
1178
Guido van Rossum79f25d91997-04-29 20:08:16 +00001179static PyObject *
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001180builtin_id(PyModuleDef *self, PyObject *v)
1181/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
Guido van Rossum5b722181993-03-30 17:46:03 +00001182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +00001184}
1185
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001186
Raymond Hettingera6c60372008-03-13 01:26:19 +00001187/* map object ************************************************************/
1188
1189typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 PyObject_HEAD
1191 PyObject *iters;
1192 PyObject *func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001193} mapobject;
1194
Guido van Rossum79f25d91997-04-29 20:08:16 +00001195static PyObject *
Raymond Hettingera6c60372008-03-13 01:26:19 +00001196map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 PyObject *it, *iters, *func;
1199 mapobject *lz;
1200 Py_ssize_t numargs, i;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001201
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001202 if (type == &PyMap_Type && !_PyArg_NoKeywords("map", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 numargs = PyTuple_Size(args);
1206 if (numargs < 2) {
1207 PyErr_SetString(PyExc_TypeError,
1208 "map() must have at least two arguments.");
1209 return NULL;
1210 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 iters = PyTuple_New(numargs-1);
1213 if (iters == NULL)
1214 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 for (i=1 ; i<numargs ; i++) {
1217 /* Get iterator. */
1218 it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1219 if (it == NULL) {
1220 Py_DECREF(iters);
1221 return NULL;
1222 }
1223 PyTuple_SET_ITEM(iters, i-1, it);
1224 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 /* create mapobject structure */
1227 lz = (mapobject *)type->tp_alloc(type, 0);
1228 if (lz == NULL) {
1229 Py_DECREF(iters);
1230 return NULL;
1231 }
1232 lz->iters = iters;
1233 func = PyTuple_GET_ITEM(args, 0);
1234 Py_INCREF(func);
1235 lz->func = func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 return (PyObject *)lz;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001238}
1239
1240static void
1241map_dealloc(mapobject *lz)
1242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 PyObject_GC_UnTrack(lz);
1244 Py_XDECREF(lz->iters);
1245 Py_XDECREF(lz->func);
1246 Py_TYPE(lz)->tp_free(lz);
Raymond Hettingera6c60372008-03-13 01:26:19 +00001247}
1248
1249static int
1250map_traverse(mapobject *lz, visitproc visit, void *arg)
1251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 Py_VISIT(lz->iters);
1253 Py_VISIT(lz->func);
1254 return 0;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001255}
1256
1257static PyObject *
1258map_next(mapobject *lz)
1259{
Victor Stinnerbc08ab42016-12-15 12:40:53 +01001260 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001261 PyObject **stack;
1262 Py_ssize_t niters, nargs, i;
1263 PyObject *result = NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001264
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001265 niters = PyTuple_GET_SIZE(lz->iters);
1266 if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1267 stack = small_stack;
1268 }
1269 else {
1270 stack = PyMem_Malloc(niters * sizeof(stack[0]));
1271 if (stack == NULL) {
1272 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 return NULL;
1274 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 }
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001276
1277 nargs = 0;
1278 for (i=0; i < niters; i++) {
1279 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1280 PyObject *val = Py_TYPE(it)->tp_iternext(it);
1281 if (val == NULL) {
1282 goto exit;
1283 }
1284 stack[i] = val;
1285 nargs++;
1286 }
1287
1288 result = _PyObject_FastCall(lz->func, stack, nargs);
1289
1290exit:
1291 for (i=0; i < nargs; i++) {
1292 Py_DECREF(stack[i]);
1293 }
1294 if (stack != small_stack) {
1295 PyMem_Free(stack);
1296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001298}
1299
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001300static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301301map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001302{
1303 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1304 PyObject *args = PyTuple_New(numargs+1);
1305 Py_ssize_t i;
1306 if (args == NULL)
1307 return NULL;
1308 Py_INCREF(lz->func);
1309 PyTuple_SET_ITEM(args, 0, lz->func);
1310 for (i = 0; i<numargs; i++){
1311 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1312 Py_INCREF(it);
1313 PyTuple_SET_ITEM(args, i+1, it);
1314 }
1315
1316 return Py_BuildValue("ON", Py_TYPE(lz), args);
1317}
1318
1319static PyMethodDef map_methods[] = {
1320 {"__reduce__", (PyCFunction)map_reduce, METH_NOARGS, reduce_doc},
1321 {NULL, NULL} /* sentinel */
1322};
1323
1324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001325PyDoc_STRVAR(map_doc,
Raymond Hettingera6c60372008-03-13 01:26:19 +00001326"map(func, *iterables) --> map object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001327\n\
Raymond Hettingera6c60372008-03-13 01:26:19 +00001328Make an iterator that computes the function using arguments from\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329each of the iterables. Stops when the shortest iterable is exhausted.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001330
Raymond Hettingera6c60372008-03-13 01:26:19 +00001331PyTypeObject PyMap_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1333 "map", /* tp_name */
1334 sizeof(mapobject), /* tp_basicsize */
1335 0, /* tp_itemsize */
1336 /* methods */
1337 (destructor)map_dealloc, /* tp_dealloc */
1338 0, /* tp_print */
1339 0, /* tp_getattr */
1340 0, /* tp_setattr */
1341 0, /* tp_reserved */
1342 0, /* tp_repr */
1343 0, /* tp_as_number */
1344 0, /* tp_as_sequence */
1345 0, /* tp_as_mapping */
1346 0, /* tp_hash */
1347 0, /* tp_call */
1348 0, /* tp_str */
1349 PyObject_GenericGetAttr, /* tp_getattro */
1350 0, /* tp_setattro */
1351 0, /* tp_as_buffer */
1352 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1353 Py_TPFLAGS_BASETYPE, /* tp_flags */
1354 map_doc, /* tp_doc */
1355 (traverseproc)map_traverse, /* tp_traverse */
1356 0, /* tp_clear */
1357 0, /* tp_richcompare */
1358 0, /* tp_weaklistoffset */
1359 PyObject_SelfIter, /* tp_iter */
1360 (iternextfunc)map_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001361 map_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 0, /* tp_members */
1363 0, /* tp_getset */
1364 0, /* tp_base */
1365 0, /* tp_dict */
1366 0, /* tp_descr_get */
1367 0, /* tp_descr_set */
1368 0, /* tp_dictoffset */
1369 0, /* tp_init */
1370 PyType_GenericAlloc, /* tp_alloc */
1371 map_new, /* tp_new */
1372 PyObject_GC_Del, /* tp_free */
Raymond Hettingera6c60372008-03-13 01:26:19 +00001373};
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001374
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001375
1376/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001377static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001378builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Georg Brandla18af4e2007-04-21 15:47:16 +00001379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 PyObject *it, *res;
1381 PyObject *def = NULL;
Georg Brandla18af4e2007-04-21 15:47:16 +00001382
Sylvain96c7c062017-06-15 17:05:23 +02001383 if (!_PyArg_UnpackStack(args, nargs, "next", 1, 2, &it, &def))
1384 return NULL;
1385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (!PyIter_Check(it)) {
1387 PyErr_Format(PyExc_TypeError,
Philip Jenvey50add042011-11-06 16:37:52 -08001388 "'%.200s' object is not an iterator",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 it->ob_type->tp_name);
1390 return NULL;
1391 }
1392
1393 res = (*it->ob_type->tp_iternext)(it);
1394 if (res != NULL) {
1395 return res;
1396 } else if (def != NULL) {
1397 if (PyErr_Occurred()) {
1398 if(!PyErr_ExceptionMatches(PyExc_StopIteration))
1399 return NULL;
1400 PyErr_Clear();
1401 }
1402 Py_INCREF(def);
1403 return def;
1404 } else if (PyErr_Occurred()) {
1405 return NULL;
1406 } else {
1407 PyErr_SetNone(PyExc_StopIteration);
1408 return NULL;
1409 }
Georg Brandla18af4e2007-04-21 15:47:16 +00001410}
1411
1412PyDoc_STRVAR(next_doc,
1413"next(iterator[, default])\n\
1414\n\
1415Return the next item from the iterator. If default is given and the iterator\n\
1416is exhausted, it is returned instead of raising StopIteration.");
1417
1418
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001419/*[clinic input]
1420setattr as builtin_setattr
1421
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001422 obj: object
1423 name: object
1424 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001425 /
1426
1427Sets the named attribute on the given object to the specified value.
1428
1429setattr(x, 'y', v) is equivalent to ``x.y = v''
1430[clinic start generated code]*/
1431
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001432static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001433builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
Larry Hastings89964c42015-04-14 18:07:59 -04001434 PyObject *value)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001435/*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001436{
1437 if (PyObject_SetAttr(obj, name, value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001439 Py_RETURN_NONE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001440}
1441
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001442
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001443/*[clinic input]
1444delattr as builtin_delattr
1445
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001446 obj: object
1447 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001448 /
1449
1450Deletes the named attribute from the given object.
1451
1452delattr(x, 'y') is equivalent to ``del x.y''
1453[clinic start generated code]*/
1454
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001455static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001456builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1457/*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001458{
1459 if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001461 Py_RETURN_NONE;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001462}
1463
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001464
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001465/*[clinic input]
1466hash as builtin_hash
1467
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001468 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001469 /
1470
1471Return the hash value for the given object.
1472
1473Two objects that compare equal must also have the same hash value, but the
1474reverse is not necessarily true.
1475[clinic start generated code]*/
1476
Guido van Rossum79f25d91997-04-29 20:08:16 +00001477static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001478builtin_hash(PyObject *module, PyObject *obj)
1479/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001480{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001481 Py_hash_t x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001482
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001483 x = PyObject_Hash(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 if (x == -1)
1485 return NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001486 return PyLong_FromSsize_t(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001487}
1488
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001489
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001490/*[clinic input]
1491hex as builtin_hex
1492
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001493 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001494 /
1495
1496Return the hexadecimal representation of an integer.
1497
1498 >>> hex(12648430)
1499 '0xc0ffee'
1500[clinic start generated code]*/
1501
Guido van Rossum79f25d91997-04-29 20:08:16 +00001502static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001503builtin_hex(PyObject *module, PyObject *number)
1504/*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001505{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001506 return PyNumber_ToBase(number, 16);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001507}
1508
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001509
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001510/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001511static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001512builtin_iter(PyObject *self, PyObject *args)
1513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 PyObject *v, *w = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
1517 return NULL;
1518 if (w == NULL)
1519 return PyObject_GetIter(v);
1520 if (!PyCallable_Check(v)) {
1521 PyErr_SetString(PyExc_TypeError,
1522 "iter(v, w): v must be callable");
1523 return NULL;
1524 }
1525 return PyCallIter_New(v, w);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001526}
1527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528PyDoc_STRVAR(iter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +00001529"iter(iterable) -> iterator\n\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001530iter(callable, sentinel) -> iterator\n\
1531\n\
1532Get an iterator from an object. In the first form, the argument must\n\
1533supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001534In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001535
1536
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001537/*[clinic input]
1538len as builtin_len
1539
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001540 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001541 /
1542
1543Return the number of items in a container.
1544[clinic start generated code]*/
1545
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001546static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001547builtin_len(PyObject *module, PyObject *obj)
1548/*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001551
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001552 res = PyObject_Size(obj);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001553 if (res < 0) {
1554 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 return NULL;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001556 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 return PyLong_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001558}
1559
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001560
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001561/*[clinic input]
1562locals as builtin_locals
1563
1564Return a dictionary containing the current scope's local variables.
1565
1566NOTE: Whether or not updates to this dictionary will affect name lookups in
1567the local scope and vice-versa is *implementation dependent* and not
1568covered by any backwards compatibility guarantees.
1569[clinic start generated code]*/
1570
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001571static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001572builtin_locals_impl(PyObject *module)
1573/*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 d = PyEval_GetLocals();
1578 Py_XINCREF(d);
1579 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001580}
1581
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001582
Guido van Rossum79f25d91997-04-29 20:08:16 +00001583static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001584min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001587 PyObject *emptytuple, *defaultval = NULL;
1588 static char *kwlist[] = {"key", "default", NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 const char *name = op == Py_LT ? "min" : "max";
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001590 const int positional = PyTuple_Size(args) > 1;
1591 int ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001592
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001593 if (positional)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 v = args;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001595 else if (!PyArg_UnpackTuple(args, name, 1, 1, &v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001597
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001598 emptytuple = PyTuple_New(0);
1599 if (emptytuple == NULL)
1600 return NULL;
Oren Milman58cf7482017-08-21 20:19:07 +03001601 ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds,
1602 (op == Py_LT) ? "|$OO:min" : "|$OO:max",
1603 kwlist, &keyfunc, &defaultval);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001604 Py_DECREF(emptytuple);
1605 if (!ret)
1606 return NULL;
1607
1608 if (positional && defaultval != NULL) {
1609 PyErr_Format(PyExc_TypeError,
1610 "Cannot specify a default for %s() with multiple "
1611 "positional arguments", name);
1612 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 it = PyObject_GetIter(v);
1616 if (it == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 return NULL;
1618 }
Tim Petersc3074532001-05-03 07:00:32 +00001619
Alexander Marshalove22072f2018-07-24 10:58:21 +07001620 if (keyfunc == Py_None) {
1621 keyfunc = NULL;
1622 }
1623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 maxitem = NULL; /* the result */
1625 maxval = NULL; /* the value associated with the result */
1626 while (( item = PyIter_Next(it) )) {
1627 /* get the value from the key function */
1628 if (keyfunc != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001629 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (val == NULL)
1631 goto Fail_it_item;
1632 }
1633 /* no key function; the value is the item */
1634 else {
1635 val = item;
1636 Py_INCREF(val);
1637 }
Tim Petersc3074532001-05-03 07:00:32 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 /* maximum value and item are unset; set them */
1640 if (maxval == NULL) {
1641 maxitem = item;
1642 maxval = val;
1643 }
1644 /* maximum value and item are set; update them as necessary */
1645 else {
1646 int cmp = PyObject_RichCompareBool(val, maxval, op);
1647 if (cmp < 0)
1648 goto Fail_it_item_and_val;
1649 else if (cmp > 0) {
1650 Py_DECREF(maxval);
1651 Py_DECREF(maxitem);
1652 maxval = val;
1653 maxitem = item;
1654 }
1655 else {
1656 Py_DECREF(item);
1657 Py_DECREF(val);
1658 }
1659 }
1660 }
1661 if (PyErr_Occurred())
1662 goto Fail_it;
1663 if (maxval == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 assert(maxitem == NULL);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001665 if (defaultval != NULL) {
1666 Py_INCREF(defaultval);
1667 maxitem = defaultval;
1668 } else {
1669 PyErr_Format(PyExc_ValueError,
1670 "%s() arg is an empty sequence", name);
1671 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 }
1673 else
1674 Py_DECREF(maxval);
1675 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 return maxitem;
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001677
1678Fail_it_item_and_val:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 Py_DECREF(val);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001680Fail_it_item:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 Py_DECREF(item);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001682Fail_it:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 Py_XDECREF(maxval);
1684 Py_XDECREF(maxitem);
1685 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001687}
1688
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001689/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001690static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001691builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001694}
1695
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001696PyDoc_STRVAR(min_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001697"min(iterable, *[, default=obj, key=func]) -> value\n\
1698min(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001699\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001700With a single iterable argument, return its smallest item. The\n\
1701default keyword-only argument specifies an object to return if\n\
1702the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001703With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001704
1705
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001706/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001707static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001708builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001711}
1712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001713PyDoc_STRVAR(max_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001714"max(iterable, *[, default=obj, key=func]) -> value\n\
1715max(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001716\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001717With a single iterable argument, return its biggest item. The\n\
1718default keyword-only argument specifies an object to return if\n\
1719the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001720With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001721
1722
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001723/*[clinic input]
1724oct as builtin_oct
1725
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001726 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001727 /
1728
1729Return the octal representation of an integer.
1730
1731 >>> oct(342391)
1732 '0o1234567'
1733[clinic start generated code]*/
1734
Guido van Rossum79f25d91997-04-29 20:08:16 +00001735static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001736builtin_oct(PyObject *module, PyObject *number)
1737/*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001738{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001739 return PyNumber_ToBase(number, 8);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001740}
1741
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001742
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001743/*[clinic input]
1744ord as builtin_ord
1745
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001746 c: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001747 /
1748
1749Return the Unicode code point for a one-character string.
1750[clinic start generated code]*/
1751
Guido van Rossum79f25d91997-04-29 20:08:16 +00001752static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001753builtin_ord(PyObject *module, PyObject *c)
1754/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 long ord;
1757 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001758
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001759 if (PyBytes_Check(c)) {
1760 size = PyBytes_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001762 ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 return PyLong_FromLong(ord);
1764 }
1765 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001766 else if (PyUnicode_Check(c)) {
1767 if (PyUnicode_READY(c) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001768 return NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001769 size = PyUnicode_GET_LENGTH(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001771 ord = (long)PyUnicode_READ_CHAR(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 return PyLong_FromLong(ord);
1773 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001775 else if (PyByteArray_Check(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 /* XXX Hopefully this is temporary */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001777 size = PyByteArray_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001779 ord = (long)((unsigned char)*PyByteArray_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 return PyLong_FromLong(ord);
1781 }
1782 }
1783 else {
1784 PyErr_Format(PyExc_TypeError,
1785 "ord() expected string of length 1, but " \
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001786 "%.200s found", c->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 return NULL;
1788 }
Guido van Rossum09095f32000-03-10 23:00:52 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 PyErr_Format(PyExc_TypeError,
1791 "ord() expected a character, "
1792 "but string of length %zd found",
1793 size);
1794 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001795}
1796
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001797
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001798/*[clinic input]
1799pow as builtin_pow
1800
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001801 x: object
1802 y: object
1803 z: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001804 /
1805
1806Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
1807
1808Some types, such as ints, are able to use a more efficient algorithm when
1809invoked using the three argument form.
1810[clinic start generated code]*/
1811
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001812static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001813builtin_pow_impl(PyObject *module, PyObject *x, PyObject *y, PyObject *z)
1814/*[clinic end generated code: output=50a14d5d130d404b input=653d57d38d41fc07]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001815{
1816 return PyNumber_Power(x, y, z);
1817}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001818
1819
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001820/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum34343512006-11-30 22:13:52 +00001821static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001822builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Guido van Rossum34343512006-11-30 22:13:52 +00001823{
INADA Naokibd584f12017-01-19 12:50:34 +01001824 static const char * const _keywords[] = {"sep", "end", "file", "flush", 0};
1825 static struct _PyArg_Parser _parser = {"|OOOO:print", _keywords, 0};
Georg Brandlbc3b6822012-01-13 19:41:25 +01001826 PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 int i, err;
Guido van Rossum34343512006-11-30 22:13:52 +00001828
INADA Naokibd584f12017-01-19 12:50:34 +01001829 if (kwnames != NULL &&
1830 !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser,
1831 &sep, &end, &file, &flush)) {
Georg Brandlbc3b6822012-01-13 19:41:25 +01001832 return NULL;
INADA Naokibd584f12017-01-19 12:50:34 +01001833 }
1834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (file == NULL || file == Py_None) {
Victor Stinnerbd303c12013-11-07 23:07:29 +01001836 file = _PySys_GetObjectId(&PyId_stdout);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001837 if (file == NULL) {
1838 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1839 return NULL;
1840 }
1841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 /* sys.stdout may be None when FILE* stdout isn't connected */
1843 if (file == Py_None)
1844 Py_RETURN_NONE;
1845 }
Guido van Rossum34343512006-11-30 22:13:52 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 if (sep == Py_None) {
1848 sep = NULL;
1849 }
1850 else if (sep && !PyUnicode_Check(sep)) {
1851 PyErr_Format(PyExc_TypeError,
1852 "sep must be None or a string, not %.200s",
1853 sep->ob_type->tp_name);
1854 return NULL;
1855 }
1856 if (end == Py_None) {
1857 end = NULL;
1858 }
1859 else if (end && !PyUnicode_Check(end)) {
1860 PyErr_Format(PyExc_TypeError,
1861 "end must be None or a string, not %.200s",
1862 end->ob_type->tp_name);
1863 return NULL;
1864 }
Guido van Rossum34343512006-11-30 22:13:52 +00001865
INADA Naokibd584f12017-01-19 12:50:34 +01001866 for (i = 0; i < nargs; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 if (i > 0) {
1868 if (sep == NULL)
1869 err = PyFile_WriteString(" ", file);
1870 else
1871 err = PyFile_WriteObject(sep, file,
1872 Py_PRINT_RAW);
1873 if (err)
1874 return NULL;
1875 }
INADA Naokibd584f12017-01-19 12:50:34 +01001876 err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 if (err)
1878 return NULL;
1879 }
Guido van Rossum34343512006-11-30 22:13:52 +00001880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 if (end == NULL)
1882 err = PyFile_WriteString("\n", file);
1883 else
1884 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1885 if (err)
1886 return NULL;
Guido van Rossum34343512006-11-30 22:13:52 +00001887
Georg Brandlbc3b6822012-01-13 19:41:25 +01001888 if (flush != NULL) {
1889 PyObject *tmp;
1890 int do_flush = PyObject_IsTrue(flush);
1891 if (do_flush == -1)
1892 return NULL;
1893 else if (do_flush) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001894 tmp = _PyObject_CallMethodId(file, &PyId_flush, NULL);
Georg Brandlbc3b6822012-01-13 19:41:25 +01001895 if (tmp == NULL)
1896 return NULL;
1897 else
1898 Py_DECREF(tmp);
1899 }
1900 }
1901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 Py_RETURN_NONE;
Guido van Rossum34343512006-11-30 22:13:52 +00001903}
1904
1905PyDoc_STRVAR(print_doc,
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001906"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\
Guido van Rossum34343512006-11-30 22:13:52 +00001907\n\
1908Prints the values to a stream, or to sys.stdout by default.\n\
1909Optional keyword arguments:\n\
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001910file: a file-like object (stream); defaults to the current sys.stdout.\n\
1911sep: string inserted between values, default a space.\n\
1912end: string appended after the last value, default a newline.\n\
1913flush: whether to forcibly flush the stream.");
Guido van Rossum34343512006-11-30 22:13:52 +00001914
1915
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001916/*[clinic input]
1917input as builtin_input
1918
1919 prompt: object(c_default="NULL") = None
1920 /
1921
1922Read a string from standard input. The trailing newline is stripped.
1923
1924The prompt string, if given, is printed to standard output without a
1925trailing newline before reading input.
1926
1927If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
1928On *nix systems, readline is used if available.
1929[clinic start generated code]*/
1930
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001931static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001932builtin_input_impl(PyObject *module, PyObject *prompt)
1933/*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001934{
Victor Stinnerbd303c12013-11-07 23:07:29 +01001935 PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
1936 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1937 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 PyObject *tmp;
1939 long fd;
1940 int tty;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 /* Check that stdin/out/err are intact */
1943 if (fin == NULL || fin == Py_None) {
1944 PyErr_SetString(PyExc_RuntimeError,
1945 "input(): lost sys.stdin");
1946 return NULL;
1947 }
1948 if (fout == NULL || fout == Py_None) {
1949 PyErr_SetString(PyExc_RuntimeError,
1950 "input(): lost sys.stdout");
1951 return NULL;
1952 }
1953 if (ferr == NULL || ferr == Py_None) {
1954 PyErr_SetString(PyExc_RuntimeError,
1955 "input(): lost sys.stderr");
1956 return NULL;
1957 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 /* First of all, flush stderr */
Victor Stinner3466bde2016-09-05 18:16:01 -07001960 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (tmp == NULL)
1962 PyErr_Clear();
1963 else
1964 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 /* We should only use (GNU) readline if Python's sys.stdin and
1967 sys.stdout are the same as C's stdin and stdout, because we
1968 need to pass it those. */
Victor Stinner3466bde2016-09-05 18:16:01 -07001969 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 if (tmp == NULL) {
1971 PyErr_Clear();
1972 tty = 0;
1973 }
1974 else {
1975 fd = PyLong_AsLong(tmp);
1976 Py_DECREF(tmp);
1977 if (fd < 0 && PyErr_Occurred())
1978 return NULL;
1979 tty = fd == fileno(stdin) && isatty(fd);
1980 }
1981 if (tty) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001982 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
Martin Panterc9a6ab52015-10-10 01:25:38 +00001983 if (tmp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 PyErr_Clear();
Martin Panterc9a6ab52015-10-10 01:25:38 +00001985 tty = 0;
1986 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 else {
1988 fd = PyLong_AsLong(tmp);
1989 Py_DECREF(tmp);
1990 if (fd < 0 && PyErr_Occurred())
1991 return NULL;
1992 tty = fd == fileno(stdout) && isatty(fd);
1993 }
1994 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 /* If we're interactive, use (GNU) readline */
1997 if (tty) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01001998 PyObject *po = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001999 const char *promptstr;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002000 char *s = NULL;
2001 PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
2002 PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002003 const char *stdin_encoding_str, *stdin_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 PyObject *result;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002005 size_t len;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00002006
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002007 /* stdin is a text stream, so it must have an encoding. */
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002008 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002009 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002010 if (!stdin_encoding || !stdin_errors ||
2011 !PyUnicode_Check(stdin_encoding) ||
2012 !PyUnicode_Check(stdin_errors)) {
2013 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002014 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002015 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002016 stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2017 stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002018 if (!stdin_encoding_str || !stdin_errors_str)
2019 goto _readline_errors;
Victor Stinner3466bde2016-09-05 18:16:01 -07002020 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 if (tmp == NULL)
2022 PyErr_Clear();
2023 else
2024 Py_DECREF(tmp);
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002025 if (prompt != NULL) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002026 /* We have a prompt, encode it as stdout would */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002027 const char *stdout_encoding_str, *stdout_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 PyObject *stringpo;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002029 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002030 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002031 if (!stdout_encoding || !stdout_errors ||
2032 !PyUnicode_Check(stdout_encoding) ||
2033 !PyUnicode_Check(stdout_errors)) {
2034 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002035 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002036 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002037 stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2038 stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002039 if (!stdout_encoding_str || !stdout_errors_str)
2040 goto _readline_errors;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002041 stringpo = PyObject_Str(prompt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002042 if (stringpo == NULL)
2043 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 po = PyUnicode_AsEncodedString(stringpo,
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002045 stdout_encoding_str, stdout_errors_str);
2046 Py_CLEAR(stdout_encoding);
2047 Py_CLEAR(stdout_errors);
2048 Py_CLEAR(stringpo);
2049 if (po == NULL)
2050 goto _readline_errors;
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03002051 assert(PyBytes_Check(po));
2052 promptstr = PyBytes_AS_STRING(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 }
2054 else {
2055 po = NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002056 promptstr = "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002058 s = PyOS_Readline(stdin, stdout, promptstr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 if (s == NULL) {
Richard Oudkerk614c5782013-04-03 13:44:50 +01002060 PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 if (!PyErr_Occurred())
2062 PyErr_SetNone(PyExc_KeyboardInterrupt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002063 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002065
2066 len = strlen(s);
2067 if (len == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 PyErr_SetNone(PyExc_EOFError);
2069 result = NULL;
2070 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002071 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 if (len > PY_SSIZE_T_MAX) {
2073 PyErr_SetString(PyExc_OverflowError,
2074 "input: input too long");
2075 result = NULL;
2076 }
2077 else {
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002078 len--; /* strip trailing '\n' */
2079 if (len != 0 && s[len-1] == '\r')
2080 len--; /* strip trailing '\r' */
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002081 result = PyUnicode_Decode(s, len, stdin_encoding_str,
2082 stdin_errors_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 }
2084 }
2085 Py_DECREF(stdin_encoding);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002086 Py_DECREF(stdin_errors);
2087 Py_XDECREF(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 PyMem_FREE(s);
2089 return result;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002090
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002091 _readline_errors:
2092 Py_XDECREF(stdin_encoding);
2093 Py_XDECREF(stdout_encoding);
2094 Py_XDECREF(stdin_errors);
2095 Py_XDECREF(stdout_errors);
2096 Py_XDECREF(po);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002097 if (tty)
2098 return NULL;
2099
2100 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 }
Guido van Rossumeba76962007-05-27 09:13:28 +00002102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 /* Fallback if we're not interactive */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002104 if (prompt != NULL) {
2105 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 return NULL;
2107 }
Victor Stinner3466bde2016-09-05 18:16:01 -07002108 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 if (tmp == NULL)
2110 PyErr_Clear();
2111 else
2112 Py_DECREF(tmp);
2113 return PyFile_GetLine(fin, -1);
Guido van Rossuma88a0332007-02-26 16:59:55 +00002114}
2115
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002116
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002117/*[clinic input]
2118repr as builtin_repr
2119
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002120 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002121 /
2122
2123Return the canonical string representation of the object.
2124
2125For many object types, including most builtins, eval(repr(obj)) == obj.
2126[clinic start generated code]*/
2127
Guido van Rossum79f25d91997-04-29 20:08:16 +00002128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002129builtin_repr(PyObject *module, PyObject *obj)
2130/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
Guido van Rossumc89705d1992-11-26 08:54:07 +00002131{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002132 return PyObject_Repr(obj);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002133}
2134
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002135
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002136/*[clinic input]
2137round as builtin_round
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002138
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002139 number: object
2140 ndigits: object = NULL
2141
2142Round a number to a given precision in decimal digits.
2143
2144The return value is an integer if ndigits is omitted or None. Otherwise
2145the return value has the same type as the number. ndigits may be negative.
2146[clinic start generated code]*/
2147
2148static PyObject *
2149builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
2150/*[clinic end generated code: output=ff0d9dd176c02ede input=854bc3a217530c3d]*/
2151{
2152 PyObject *round, *result;
Alex Martelliae211f92007-08-22 23:21:33 +00002153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 if (Py_TYPE(number)->tp_dict == NULL) {
2155 if (PyType_Ready(Py_TYPE(number)) < 0)
2156 return NULL;
2157 }
Guido van Rossum15d3d042007-08-24 02:02:45 +00002158
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002159 round = _PyObject_LookupSpecial(number, &PyId___round__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 if (round == NULL) {
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002161 if (!PyErr_Occurred())
2162 PyErr_Format(PyExc_TypeError,
2163 "type %.100s doesn't define __round__ method",
2164 Py_TYPE(number)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 return NULL;
2166 }
Alex Martelliae211f92007-08-22 23:21:33 +00002167
Raymond Hettingerf0f1c232016-09-03 01:55:11 -07002168 if (ndigits == NULL || ndigits == Py_None)
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002169 result = _PyObject_CallNoArg(round);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 else
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002171 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002172 Py_DECREF(round);
2173 return result;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002174}
2175
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002176
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002177/*AC: we need to keep the kwds dict intact to easily call into the
2178 * list.sort method, which isn't currently supported in AC. So we just use
2179 * the initially generated signature with a custom implementation.
2180 */
2181/* [disabled clinic input]
2182sorted as builtin_sorted
2183
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002184 iterable as seq: object
2185 key as keyfunc: object = None
2186 reverse: object = False
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002187
2188Return a new list containing all items from the iterable in ascending order.
2189
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002190A custom key function can be supplied to customize the sort order, and the
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002191reverse flag can be set to request the result in descending order.
2192[end disabled clinic input]*/
2193
2194PyDoc_STRVAR(builtin_sorted__doc__,
Serhiy Storchaka3a104252017-01-23 12:29:47 +02002195"sorted($module, iterable, /, *, key=None, reverse=False)\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002196"--\n"
2197"\n"
2198"Return a new list containing all items from the iterable in ascending order.\n"
2199"\n"
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002200"A custom key function can be supplied to customize the sort order, and the\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002201"reverse flag can be set to request the result in descending order.");
2202
2203#define BUILTIN_SORTED_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002204 {"sorted", (PyCFunction)builtin_sorted, METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002205
Raymond Hettinger64958a12003-12-17 20:43:33 +00002206static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002207builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Raymond Hettinger64958a12003-12-17 20:43:33 +00002208{
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002209 PyObject *newlist, *v, *seq, *callable;
Victor Stinner5a60eca2017-01-17 15:17:49 +01002210
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002211 /* Keyword arguments are passed through list.sort() which will check
2212 them. */
2213 if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 newlist = PySequence_List(seq);
2217 if (newlist == NULL)
2218 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002219
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002220 callable = _PyObject_GetAttrId(newlist, &PyId_sort);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 if (callable == NULL) {
2222 Py_DECREF(newlist);
2223 return NULL;
2224 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002225
Serhiy Storchaka299dc232017-01-20 08:35:18 +02002226 assert(nargs >= 1);
Victor Stinner5a60eca2017-01-17 15:17:49 +01002227 v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 Py_DECREF(callable);
2229 if (v == NULL) {
2230 Py_DECREF(newlist);
2231 return NULL;
2232 }
2233 Py_DECREF(v);
2234 return newlist;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002235}
2236
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002237
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002238/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002239static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002240builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 PyObject *v = NULL;
2243 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2246 return NULL;
2247 if (v == NULL) {
2248 d = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01002249 if (d == NULL)
2250 return NULL;
2251 Py_INCREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 }
2253 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002254 d = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 if (d == NULL) {
2256 PyErr_SetString(PyExc_TypeError,
2257 "vars() argument must have __dict__ attribute");
2258 return NULL;
2259 }
2260 }
2261 return d;
Guido van Rossum2d951851994-08-29 12:52:16 +00002262}
2263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002264PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002265"vars([object]) -> dictionary\n\
2266\n\
2267Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002268With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002269
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002270
2271/*[clinic input]
2272sum as builtin_sum
2273
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002274 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002275 start: object(c_default="NULL") = 0
2276 /
2277
2278Return the sum of a 'start' value (default: 0) plus an iterable of numbers
2279
2280When the iterable is empty, return the start value.
2281This function is intended specifically for use with numeric values and may
2282reject non-numeric types.
2283[clinic start generated code]*/
2284
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002285static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002286builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
2287/*[clinic end generated code: output=df758cec7d1d302f input=3b5b7a9d7611c73a]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002288{
2289 PyObject *result = start;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 PyObject *temp, *item, *iter;
Alex Martellia70b1912003-04-22 08:12:33 +00002291
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002292 iter = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (iter == NULL)
2294 return NULL;
Alex Martellia70b1912003-04-22 08:12:33 +00002295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 if (result == NULL) {
2297 result = PyLong_FromLong(0);
2298 if (result == NULL) {
2299 Py_DECREF(iter);
2300 return NULL;
2301 }
2302 } else {
2303 /* reject string values for 'start' parameter */
2304 if (PyUnicode_Check(result)) {
2305 PyErr_SetString(PyExc_TypeError,
2306 "sum() can't sum strings [use ''.join(seq) instead]");
2307 Py_DECREF(iter);
2308 return NULL;
2309 }
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002310 if (PyBytes_Check(result)) {
2311 PyErr_SetString(PyExc_TypeError,
2312 "sum() can't sum bytes [use b''.join(seq) instead]");
Benjamin Peterson405f32c2011-07-29 22:43:45 -05002313 Py_DECREF(iter);
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002314 return NULL;
2315 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 if (PyByteArray_Check(result)) {
2317 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson4f921c22011-07-29 14:24:29 -05002318 "sum() can't sum bytearray [use b''.join(seq) instead]");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 Py_DECREF(iter);
2320 return NULL;
2321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 Py_INCREF(result);
2323 }
Alex Martellia70b1912003-04-22 08:12:33 +00002324
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002325#ifndef SLOW_SUM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2327 Assumes all inputs are the same type. If the assumption fails, default
2328 to the more general routine.
2329 */
2330 if (PyLong_CheckExact(result)) {
2331 int overflow;
2332 long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2333 /* If this already overflowed, don't even enter the loop. */
2334 if (overflow == 0) {
2335 Py_DECREF(result);
2336 result = NULL;
2337 }
2338 while(result == NULL) {
2339 item = PyIter_Next(iter);
2340 if (item == NULL) {
2341 Py_DECREF(iter);
2342 if (PyErr_Occurred())
2343 return NULL;
2344 return PyLong_FromLong(i_result);
2345 }
2346 if (PyLong_CheckExact(item)) {
2347 long b = PyLong_AsLongAndOverflow(item, &overflow);
2348 long x = i_result + b;
2349 if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
2350 i_result = x;
2351 Py_DECREF(item);
2352 continue;
2353 }
2354 }
2355 /* Either overflowed or is not an int. Restore real objects and process normally */
2356 result = PyLong_FromLong(i_result);
Christian Heimes704e2d32013-07-26 22:49:26 +02002357 if (result == NULL) {
2358 Py_DECREF(item);
2359 Py_DECREF(iter);
2360 return NULL;
2361 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 temp = PyNumber_Add(result, item);
2363 Py_DECREF(result);
2364 Py_DECREF(item);
2365 result = temp;
2366 if (result == NULL) {
2367 Py_DECREF(iter);
2368 return NULL;
2369 }
2370 }
2371 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 if (PyFloat_CheckExact(result)) {
2374 double f_result = PyFloat_AS_DOUBLE(result);
2375 Py_DECREF(result);
2376 result = NULL;
2377 while(result == NULL) {
2378 item = PyIter_Next(iter);
2379 if (item == NULL) {
2380 Py_DECREF(iter);
2381 if (PyErr_Occurred())
2382 return NULL;
2383 return PyFloat_FromDouble(f_result);
2384 }
2385 if (PyFloat_CheckExact(item)) {
2386 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2387 f_result += PyFloat_AS_DOUBLE(item);
2388 PyFPE_END_PROTECT(f_result)
2389 Py_DECREF(item);
2390 continue;
2391 }
2392 if (PyLong_CheckExact(item)) {
2393 long value;
2394 int overflow;
2395 value = PyLong_AsLongAndOverflow(item, &overflow);
2396 if (!overflow) {
2397 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2398 f_result += (double)value;
2399 PyFPE_END_PROTECT(f_result)
2400 Py_DECREF(item);
2401 continue;
2402 }
2403 }
2404 result = PyFloat_FromDouble(f_result);
Alexey Izbyshev2b824b22018-08-24 07:27:52 +03002405 if (result == NULL) {
2406 Py_DECREF(item);
2407 Py_DECREF(iter);
2408 return NULL;
2409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 temp = PyNumber_Add(result, item);
2411 Py_DECREF(result);
2412 Py_DECREF(item);
2413 result = temp;
2414 if (result == NULL) {
2415 Py_DECREF(iter);
2416 return NULL;
2417 }
2418 }
2419 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002420#endif
2421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 for(;;) {
2423 item = PyIter_Next(iter);
2424 if (item == NULL) {
2425 /* error, or end-of-sequence */
2426 if (PyErr_Occurred()) {
2427 Py_DECREF(result);
2428 result = NULL;
2429 }
2430 break;
2431 }
2432 /* It's tempting to use PyNumber_InPlaceAdd instead of
2433 PyNumber_Add here, to avoid quadratic running time
2434 when doing 'sum(list_of_lists, [])'. However, this
2435 would produce a change in behaviour: a snippet like
Mark Dickinson9acadc52009-10-26 14:19:42 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 empty = []
2438 sum([[x] for x in range(10)], empty)
Mark Dickinson9acadc52009-10-26 14:19:42 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 would change the value of empty. */
2441 temp = PyNumber_Add(result, item);
2442 Py_DECREF(result);
2443 Py_DECREF(item);
2444 result = temp;
2445 if (result == NULL)
2446 break;
2447 }
2448 Py_DECREF(iter);
2449 return result;
Alex Martellia70b1912003-04-22 08:12:33 +00002450}
2451
Alex Martellia70b1912003-04-22 08:12:33 +00002452
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002453/*[clinic input]
2454isinstance as builtin_isinstance
2455
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002456 obj: object
2457 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002458 /
2459
2460Return whether an object is an instance of a class or of a subclass thereof.
2461
2462A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
2463check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
2464or ...`` etc.
2465[clinic start generated code]*/
2466
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002467static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002468builtin_isinstance_impl(PyObject *module, PyObject *obj,
Larry Hastings89964c42015-04-14 18:07:59 -04002469 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002470/*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002473
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002474 retval = PyObject_IsInstance(obj, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 if (retval < 0)
2476 return NULL;
2477 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002478}
2479
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002480
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002481/*[clinic input]
2482issubclass as builtin_issubclass
2483
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002484 cls: object
2485 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002486 /
2487
2488Return whether 'cls' is a derived from another class or is the same class.
2489
2490A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
2491check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
2492or ...`` etc.
2493[clinic start generated code]*/
2494
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002495static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002496builtin_issubclass_impl(PyObject *module, PyObject *cls,
Larry Hastings89964c42015-04-14 18:07:59 -04002497 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002498/*[clinic end generated code: output=358412410cd7a250 input=af5f35e9ceaddaf6]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002501
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002502 retval = PyObject_IsSubclass(cls, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 if (retval < 0)
2504 return NULL;
2505 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002506}
2507
2508
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002509typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 PyObject_HEAD
2511 Py_ssize_t tuplesize;
2512 PyObject *ittuple; /* tuple of iterators */
2513 PyObject *result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002514} zipobject;
2515
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002516static PyObject *
2517zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Barry Warsawbd599b52000-08-03 15:45:29 +00002518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 zipobject *lz;
2520 Py_ssize_t i;
2521 PyObject *ittuple; /* tuple of iterators */
2522 PyObject *result;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002523 Py_ssize_t tuplesize;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002524
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002525 if (type == &PyZip_Type && !_PyArg_NoKeywords("zip", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 return NULL;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 /* args must be a tuple */
2529 assert(PyTuple_Check(args));
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002530 tuplesize = PyTuple_GET_SIZE(args);
Barry Warsawbd599b52000-08-03 15:45:29 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 /* obtain iterators */
2533 ittuple = PyTuple_New(tuplesize);
2534 if (ittuple == NULL)
2535 return NULL;
2536 for (i=0; i < tuplesize; ++i) {
2537 PyObject *item = PyTuple_GET_ITEM(args, i);
2538 PyObject *it = PyObject_GetIter(item);
2539 if (it == NULL) {
2540 if (PyErr_ExceptionMatches(PyExc_TypeError))
2541 PyErr_Format(PyExc_TypeError,
2542 "zip argument #%zd must support iteration",
2543 i+1);
2544 Py_DECREF(ittuple);
2545 return NULL;
2546 }
2547 PyTuple_SET_ITEM(ittuple, i, it);
2548 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 /* create a result holder */
2551 result = PyTuple_New(tuplesize);
2552 if (result == NULL) {
2553 Py_DECREF(ittuple);
2554 return NULL;
2555 }
2556 for (i=0 ; i < tuplesize ; i++) {
2557 Py_INCREF(Py_None);
2558 PyTuple_SET_ITEM(result, i, Py_None);
2559 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 /* create zipobject structure */
2562 lz = (zipobject *)type->tp_alloc(type, 0);
2563 if (lz == NULL) {
2564 Py_DECREF(ittuple);
2565 Py_DECREF(result);
2566 return NULL;
2567 }
2568 lz->ittuple = ittuple;
2569 lz->tuplesize = tuplesize;
2570 lz->result = result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 return (PyObject *)lz;
Barry Warsawbd599b52000-08-03 15:45:29 +00002573}
2574
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002575static void
2576zip_dealloc(zipobject *lz)
2577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 PyObject_GC_UnTrack(lz);
2579 Py_XDECREF(lz->ittuple);
2580 Py_XDECREF(lz->result);
2581 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002582}
2583
2584static int
2585zip_traverse(zipobject *lz, visitproc visit, void *arg)
2586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 Py_VISIT(lz->ittuple);
2588 Py_VISIT(lz->result);
2589 return 0;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002590}
2591
2592static PyObject *
2593zip_next(zipobject *lz)
2594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 Py_ssize_t i;
2596 Py_ssize_t tuplesize = lz->tuplesize;
2597 PyObject *result = lz->result;
2598 PyObject *it;
2599 PyObject *item;
2600 PyObject *olditem;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 if (tuplesize == 0)
2603 return NULL;
2604 if (Py_REFCNT(result) == 1) {
2605 Py_INCREF(result);
2606 for (i=0 ; i < tuplesize ; i++) {
2607 it = PyTuple_GET_ITEM(lz->ittuple, i);
2608 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002609 if (item == NULL) {
2610 Py_DECREF(result);
2611 return NULL;
2612 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 olditem = PyTuple_GET_ITEM(result, i);
2614 PyTuple_SET_ITEM(result, i, item);
2615 Py_DECREF(olditem);
2616 }
2617 } else {
2618 result = PyTuple_New(tuplesize);
2619 if (result == NULL)
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002620 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 for (i=0 ; i < tuplesize ; i++) {
2622 it = PyTuple_GET_ITEM(lz->ittuple, i);
2623 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002624 if (item == NULL) {
2625 Py_DECREF(result);
2626 return NULL;
2627 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 PyTuple_SET_ITEM(result, i, item);
2629 }
2630 }
2631 return result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002632}
Barry Warsawbd599b52000-08-03 15:45:29 +00002633
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002634static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302635zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002636{
2637 /* Just recreate the zip with the internal iterator tuple */
2638 return Py_BuildValue("OO", Py_TYPE(lz), lz->ittuple);
2639}
2640
2641static PyMethodDef zip_methods[] = {
2642 {"__reduce__", (PyCFunction)zip_reduce, METH_NOARGS, reduce_doc},
2643 {NULL, NULL} /* sentinel */
2644};
2645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002646PyDoc_STRVAR(zip_doc,
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002647"zip(iter1 [,iter2 [...]]) --> zip object\n\
Barry Warsawbd599b52000-08-03 15:45:29 +00002648\n\
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002649Return a zip object whose .__next__() method returns a tuple where\n\
2650the i-th element comes from the i-th iterable argument. The .__next__()\n\
2651method continues until the shortest iterable in the argument sequence\n\
Georg Brandlced51db2008-12-04 18:28:38 +00002652is exhausted and then it raises StopIteration.");
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002653
2654PyTypeObject PyZip_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2656 "zip", /* tp_name */
2657 sizeof(zipobject), /* tp_basicsize */
2658 0, /* tp_itemsize */
2659 /* methods */
2660 (destructor)zip_dealloc, /* tp_dealloc */
2661 0, /* tp_print */
2662 0, /* tp_getattr */
2663 0, /* tp_setattr */
2664 0, /* tp_reserved */
2665 0, /* tp_repr */
2666 0, /* tp_as_number */
2667 0, /* tp_as_sequence */
2668 0, /* tp_as_mapping */
2669 0, /* tp_hash */
2670 0, /* tp_call */
2671 0, /* tp_str */
2672 PyObject_GenericGetAttr, /* tp_getattro */
2673 0, /* tp_setattro */
2674 0, /* tp_as_buffer */
2675 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2676 Py_TPFLAGS_BASETYPE, /* tp_flags */
2677 zip_doc, /* tp_doc */
2678 (traverseproc)zip_traverse, /* tp_traverse */
2679 0, /* tp_clear */
2680 0, /* tp_richcompare */
2681 0, /* tp_weaklistoffset */
2682 PyObject_SelfIter, /* tp_iter */
2683 (iternextfunc)zip_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002684 zip_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 0, /* tp_members */
2686 0, /* tp_getset */
2687 0, /* tp_base */
2688 0, /* tp_dict */
2689 0, /* tp_descr_get */
2690 0, /* tp_descr_set */
2691 0, /* tp_dictoffset */
2692 0, /* tp_init */
2693 PyType_GenericAlloc, /* tp_alloc */
2694 zip_new, /* tp_new */
2695 PyObject_GC_Del, /* tp_free */
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002696};
Barry Warsawbd599b52000-08-03 15:45:29 +00002697
2698
Guido van Rossum79f25d91997-04-29 20:08:16 +00002699static PyMethodDef builtin_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 {"__build_class__", (PyCFunction)builtin___build_class__,
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002701 METH_FASTCALL | METH_KEYWORDS, build_class_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002703 BUILTIN_ABS_METHODDEF
2704 BUILTIN_ALL_METHODDEF
2705 BUILTIN_ANY_METHODDEF
2706 BUILTIN_ASCII_METHODDEF
2707 BUILTIN_BIN_METHODDEF
Barry Warsaw36c1d1f2017-10-05 12:11:18 -04002708 {"breakpoint", (PyCFunction)builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002709 BUILTIN_CALLABLE_METHODDEF
2710 BUILTIN_CHR_METHODDEF
2711 BUILTIN_COMPILE_METHODDEF
2712 BUILTIN_DELATTR_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 {"dir", builtin_dir, METH_VARARGS, dir_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002714 BUILTIN_DIVMOD_METHODDEF
2715 BUILTIN_EVAL_METHODDEF
2716 BUILTIN_EXEC_METHODDEF
2717 BUILTIN_FORMAT_METHODDEF
Victor Stinner84b388b2017-01-17 03:52:27 +01002718 {"getattr", (PyCFunction)builtin_getattr, METH_FASTCALL, getattr_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002719 BUILTIN_GLOBALS_METHODDEF
2720 BUILTIN_HASATTR_METHODDEF
2721 BUILTIN_HASH_METHODDEF
2722 BUILTIN_HEX_METHODDEF
2723 BUILTIN_ID_METHODDEF
2724 BUILTIN_INPUT_METHODDEF
2725 BUILTIN_ISINSTANCE_METHODDEF
2726 BUILTIN_ISSUBCLASS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 {"iter", builtin_iter, METH_VARARGS, iter_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002728 BUILTIN_LEN_METHODDEF
2729 BUILTIN_LOCALS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2731 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Victor Stinnerfda6d0a2017-01-17 04:09:14 +01002732 {"next", (PyCFunction)builtin_next, METH_FASTCALL, next_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002733 BUILTIN_OCT_METHODDEF
2734 BUILTIN_ORD_METHODDEF
2735 BUILTIN_POW_METHODDEF
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002736 {"print", (PyCFunction)builtin_print, METH_FASTCALL | METH_KEYWORDS, print_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002737 BUILTIN_REPR_METHODDEF
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002738 BUILTIN_ROUND_METHODDEF
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002739 BUILTIN_SETATTR_METHODDEF
2740 BUILTIN_SORTED_METHODDEF
2741 BUILTIN_SUM_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 {"vars", builtin_vars, METH_VARARGS, vars_doc},
2743 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002744};
2745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002746PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002747"Built-in functions, exceptions, and other objects.\n\
2748\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002749Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002750
Martin v. Löwis1a214512008-06-11 05:26:20 +00002751static struct PyModuleDef builtinsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 PyModuleDef_HEAD_INIT,
2753 "builtins",
2754 builtin_doc,
2755 -1, /* multiple "initialization" just copies the module dict. */
2756 builtin_methods,
2757 NULL,
2758 NULL,
2759 NULL,
2760 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002761};
2762
2763
Guido van Rossum25ce5661997-08-02 03:10:38 +00002764PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002765_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 PyObject *mod, *dict, *debug;
Benjamin Peterson42124a72012-10-30 23:41:54 -04002768
Victor Stinnerfbca9082018-08-30 00:50:45 +02002769 const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config;
2770
Benjamin Peterson42124a72012-10-30 23:41:54 -04002771 if (PyType_Ready(&PyFilter_Type) < 0 ||
2772 PyType_Ready(&PyMap_Type) < 0 ||
2773 PyType_Ready(&PyZip_Type) < 0)
2774 return NULL;
2775
Eric Snowd393c1b2017-09-14 12:18:12 -06002776 mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 if (mod == NULL)
2778 return NULL;
2779 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002780
Tim Peters7571a0f2003-03-23 17:52:28 +00002781#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 /* "builtins" exposes a number of statically allocated objects
2783 * that, before this code was added in 2.3, never showed up in
2784 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2785 * result, programs leaking references to None and False (etc)
2786 * couldn't be diagnosed by examining sys.getobjects(0).
2787 */
Tim Peters7571a0f2003-03-23 17:52:28 +00002788#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2789#else
2790#define ADD_TO_ALL(OBJECT) (void)0
2791#endif
2792
Tim Peters4b7625e2001-09-13 21:37:17 +00002793#define SETBUILTIN(NAME, OBJECT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2795 return NULL; \
2796 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 SETBUILTIN("None", Py_None);
2799 SETBUILTIN("Ellipsis", Py_Ellipsis);
2800 SETBUILTIN("NotImplemented", Py_NotImplemented);
2801 SETBUILTIN("False", Py_False);
2802 SETBUILTIN("True", Py_True);
2803 SETBUILTIN("bool", &PyBool_Type);
2804 SETBUILTIN("memoryview", &PyMemoryView_Type);
2805 SETBUILTIN("bytearray", &PyByteArray_Type);
2806 SETBUILTIN("bytes", &PyBytes_Type);
2807 SETBUILTIN("classmethod", &PyClassMethod_Type);
2808 SETBUILTIN("complex", &PyComplex_Type);
2809 SETBUILTIN("dict", &PyDict_Type);
2810 SETBUILTIN("enumerate", &PyEnum_Type);
2811 SETBUILTIN("filter", &PyFilter_Type);
2812 SETBUILTIN("float", &PyFloat_Type);
2813 SETBUILTIN("frozenset", &PyFrozenSet_Type);
2814 SETBUILTIN("property", &PyProperty_Type);
2815 SETBUILTIN("int", &PyLong_Type);
2816 SETBUILTIN("list", &PyList_Type);
2817 SETBUILTIN("map", &PyMap_Type);
2818 SETBUILTIN("object", &PyBaseObject_Type);
2819 SETBUILTIN("range", &PyRange_Type);
2820 SETBUILTIN("reversed", &PyReversed_Type);
2821 SETBUILTIN("set", &PySet_Type);
2822 SETBUILTIN("slice", &PySlice_Type);
2823 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2824 SETBUILTIN("str", &PyUnicode_Type);
2825 SETBUILTIN("super", &PySuper_Type);
2826 SETBUILTIN("tuple", &PyTuple_Type);
2827 SETBUILTIN("type", &PyType_Type);
2828 SETBUILTIN("zip", &PyZip_Type);
Victor Stinnerfbca9082018-08-30 00:50:45 +02002829 debug = PyBool_FromLong(config->optimization_level == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002831 Py_DECREF(debug);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 return NULL;
2833 }
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002834 Py_DECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002837#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002838#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002839}