blob: 604493dc5e294a35f60000edbe4871d4fac1db85 [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Benjamin Petersonea281a52011-08-12 23:10:50 -05009#include "asdl.h"
10#include "ast.h"
11
Guido van Rossum6bf62da1997-04-11 20:37:35 +000012#include <ctype.h>
13
Mark Hammond26cffde42001-05-14 12:17:34 +000014/* The default encoding used by the platform file system APIs
15 Can remain NULL for all platforms that don't have such a concept
Guido van Rossum00bc0e02007-10-15 02:52:41 +000016
17 Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the
18 values for Py_FileSystemDefaultEncoding!
Mark Hammond26cffde42001-05-14 12:17:34 +000019*/
Steve Dowercc16be82016-09-08 10:35:16 -070020#if defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Martin v. Löwis04dc25c2008-10-03 16:09:28 +000022int Py_HasFileSystemDefaultEncoding = 1;
Steve Dowercc16be82016-09-08 10:35:16 -070023#elif defined(MS_WINDOWS)
24/* may be changed by initfsencoding(), but should never be free()d */
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000025const char *Py_FileSystemDefaultEncoding = "utf-8";
Martin v. Löwis04dc25c2008-10-03 16:09:28 +000026int Py_HasFileSystemDefaultEncoding = 1;
Victor Stinnerd64e8a72011-07-04 13:48:30 +020027#else
Victor Stinnerb744ba12010-05-15 12:27:16 +000028const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */
Martin v. Löwis04dc25c2008-10-03 16:09:28 +000029int Py_HasFileSystemDefaultEncoding = 0;
Mark Hammond26cffde42001-05-14 12:17:34 +000030#endif
Steve Dowercc16be82016-09-08 10:35:16 -070031const char *Py_FileSystemDefaultEncodeErrors = "surrogateescape";
Victor Stinner91106cd2017-12-13 12:29:09 +010032/* UTF-8 mode (PEP 540): if non-zero, use the UTF-8 encoding, and change stdin
33 and stdout error handler to "surrogateescape". */
34int Py_UTF8Mode = 0;
Mark Hammondef8b6542001-05-13 08:04:26 +000035
Victor Stinnerbd303c12013-11-07 23:07:29 +010036_Py_IDENTIFIER(__builtins__);
37_Py_IDENTIFIER(__dict__);
38_Py_IDENTIFIER(__prepare__);
39_Py_IDENTIFIER(__round__);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010040_Py_IDENTIFIER(__mro_entries__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010041_Py_IDENTIFIER(encoding);
42_Py_IDENTIFIER(errors);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020043_Py_IDENTIFIER(fileno);
44_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010045_Py_IDENTIFIER(metaclass);
46_Py_IDENTIFIER(sort);
47_Py_IDENTIFIER(stdin);
48_Py_IDENTIFIER(stdout);
49_Py_IDENTIFIER(stderr);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020050
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030051#include "clinic/bltinmodule.c.h"
52
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010053static PyObject*
54update_bases(PyObject *bases, PyObject *const *args, int nargs)
55{
56 int i, j;
57 PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
58 PyObject *stack[1] = {bases};
59 assert(PyTuple_Check(bases));
60
61 for (i = 0; i < nargs; i++) {
62 base = args[i];
63 if (PyType_Check(base)) {
64 if (new_bases) {
65 /* If we already have made a replacement, then we append every normal base,
66 otherwise just skip it. */
67 if (PyList_Append(new_bases, base) < 0) {
68 goto error;
69 }
70 }
71 continue;
72 }
73 meth = _PyObject_GetAttrId(base, &PyId___mro_entries__);
74 if (!meth) {
75 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
76 goto error;
77 }
78 PyErr_Clear();
79 if (new_bases) {
80 if (PyList_Append(new_bases, base) < 0) {
81 goto error;
82 }
83 }
84 continue;
85 }
86 new_base = _PyObject_FastCall(meth, stack, 1);
87 Py_DECREF(meth);
88 if (!new_base) {
89 goto error;
90 }
91 if (!PyTuple_Check(new_base)) {
92 PyErr_SetString(PyExc_TypeError,
93 "__mro_entries__ must return a tuple");
94 Py_DECREF(new_base);
95 goto error;
96 }
97 if (!new_bases) {
98 /* If this is a first successful replacement, create new_bases list and
99 copy previously encountered bases. */
100 if (!(new_bases = PyList_New(i))) {
101 goto error;
102 }
103 for (j = 0; j < i; j++) {
104 base = args[j];
105 PyList_SET_ITEM(new_bases, j, base);
106 Py_INCREF(base);
107 }
108 }
109 j = PyList_GET_SIZE(new_bases);
110 if (PyList_SetSlice(new_bases, j, j, new_base) < 0) {
111 goto error;
112 }
113 Py_DECREF(new_base);
114 }
115 if (!new_bases) {
116 return bases;
117 }
118 result = PyList_AsTuple(new_bases);
119 Py_DECREF(new_bases);
120 return result;
121
122error:
123 Py_XDECREF(new_bases);
124 return NULL;
125}
126
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000127/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000128static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200129builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
Victor Stinner773dc6d2017-01-16 23:46:26 +0100130 PyObject *kwnames)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000131{
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100132 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *orig_bases;
Nick Coghlan19d24672016-12-05 16:47:55 +1000133 PyObject *cls = NULL, *cell = NULL;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100134 int isclass = 0; /* initialize to prevent gcc warning */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 if (nargs < 2) {
137 PyErr_SetString(PyExc_TypeError,
138 "__build_class__: not enough arguments");
139 return NULL;
140 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100141 func = args[0]; /* Better be callable */
Benjamin Petersone8e14592013-05-16 14:37:25 -0500142 if (!PyFunction_Check(func)) {
143 PyErr_SetString(PyExc_TypeError,
Zachary Ware9b338722014-08-05 14:01:10 -0500144 "__build_class__: func must be a function");
Benjamin Petersone8e14592013-05-16 14:37:25 -0500145 return NULL;
146 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100147 name = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (!PyUnicode_Check(name)) {
149 PyErr_SetString(PyExc_TypeError,
150 "__build_class__: name is not a string");
151 return NULL;
152 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100153 orig_bases = _PyStack_AsTupleSlice(args, nargs, 2, nargs);
154 if (orig_bases == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000156
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100157 bases = update_bases(orig_bases, args + 2, nargs - 2);
158 if (bases == NULL) {
159 Py_DECREF(orig_bases);
160 return NULL;
161 }
162
Victor Stinner773dc6d2017-01-16 23:46:26 +0100163 if (kwnames == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 meta = NULL;
165 mkw = NULL;
166 }
167 else {
Victor Stinner773dc6d2017-01-16 23:46:26 +0100168 mkw = _PyStack_AsDict(args + nargs, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 if (mkw == NULL) {
170 Py_DECREF(bases);
171 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000172 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100173
Victor Stinnerae9f1612013-11-06 22:46:51 +0100174 meta = _PyDict_GetItemId(mkw, &PyId_metaclass);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (meta != NULL) {
176 Py_INCREF(meta);
Victor Stinnerae9f1612013-11-06 22:46:51 +0100177 if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 Py_DECREF(meta);
179 Py_DECREF(mkw);
180 Py_DECREF(bases);
181 return NULL;
182 }
Nick Coghlande31b192011-10-23 22:04:16 +1000183 /* metaclass is explicitly given, check if it's indeed a class */
184 isclass = PyType_Check(meta);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 }
186 }
187 if (meta == NULL) {
Nick Coghlande31b192011-10-23 22:04:16 +1000188 /* if there are no bases, use type: */
189 if (PyTuple_GET_SIZE(bases) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 meta = (PyObject *) (&PyType_Type);
Nick Coghlande31b192011-10-23 22:04:16 +1000191 }
192 /* else get the type of the first base */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 else {
194 PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
195 meta = (PyObject *) (base0->ob_type);
196 }
197 Py_INCREF(meta);
Nick Coghlande31b192011-10-23 22:04:16 +1000198 isclass = 1; /* meta is really a class */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 }
Nick Coghlan9715d262011-10-23 22:36:42 +1000200
Nick Coghlande31b192011-10-23 22:04:16 +1000201 if (isclass) {
202 /* meta is really a class, so check for a more derived
203 metaclass, or possible metaclass conflicts: */
204 winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
205 bases);
206 if (winner == NULL) {
207 Py_DECREF(meta);
208 Py_XDECREF(mkw);
209 Py_DECREF(bases);
210 return NULL;
211 }
212 if (winner != meta) {
213 Py_DECREF(meta);
214 meta = winner;
215 Py_INCREF(meta);
216 }
217 }
218 /* else: meta is not a class, so we cannot do the metaclass
219 calculation, so we will use the explicitly given object as it is */
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200220 prep = _PyObject_GetAttrId(meta, &PyId___prepare__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (prep == NULL) {
222 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
223 PyErr_Clear();
Eric Snow4f29e752016-09-08 15:11:11 -0700224 ns = PyDict_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 }
226 else {
227 Py_DECREF(meta);
228 Py_XDECREF(mkw);
229 Py_DECREF(bases);
230 return NULL;
231 }
232 }
233 else {
Victor Stinner463b86a2016-08-22 23:33:13 +0200234 PyObject *pargs[2] = {name, bases};
235 ns = _PyObject_FastCallDict(prep, pargs, 2, mkw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 Py_DECREF(prep);
237 }
238 if (ns == NULL) {
239 Py_DECREF(meta);
240 Py_XDECREF(mkw);
241 Py_DECREF(bases);
242 return NULL;
243 }
Oren Milman5837d042017-09-27 17:04:37 +0300244 if (!PyMapping_Check(ns)) {
245 PyErr_Format(PyExc_TypeError,
246 "%.200s.__prepare__() must return a mapping, not %.200s",
247 isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
248 Py_TYPE(ns)->tp_name);
249 goto error;
250 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000251 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500252 NULL, 0, NULL, 0, NULL, 0, NULL,
253 PyFunction_GET_CLOSURE(func));
Nick Coghlan19d24672016-12-05 16:47:55 +1000254 if (cell != NULL) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100255 if (bases != orig_bases) {
256 if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
257 goto error;
258 }
259 }
Victor Stinnerd1c2a8e2016-08-23 01:34:35 +0200260 PyObject *margs[3] = {name, bases, ns};
261 cls = _PyObject_FastCallDict(meta, margs, 3, mkw);
Nick Coghlan19d24672016-12-05 16:47:55 +1000262 if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
263 PyObject *cell_cls = PyCell_GET(cell);
264 if (cell_cls != cls) {
265 /* TODO: In 3.7, DeprecationWarning will become RuntimeError.
266 * At that point, cell_error won't be needed.
267 */
268 int cell_error;
269 if (cell_cls == NULL) {
270 const char *msg =
271 "__class__ not set defining %.200R as %.200R. "
272 "Was __classcell__ propagated to type.__new__?";
273 cell_error = PyErr_WarnFormat(
274 PyExc_DeprecationWarning, 1, msg, name, cls);
275 } else {
276 const char *msg =
277 "__class__ set to %.200R defining %.200R as %.200R";
278 PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
279 cell_error = 1;
280 }
281 if (cell_error) {
282 Py_DECREF(cls);
283 cls = NULL;
284 goto error;
285 } else {
286 /* Fill in the cell, since type.__new__ didn't do it */
287 PyCell_Set(cell, cls);
288 }
289 }
290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000292error:
293 Py_XDECREF(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_DECREF(ns);
295 Py_DECREF(meta);
296 Py_XDECREF(mkw);
297 Py_DECREF(bases);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100298 if (bases != orig_bases) {
299 Py_DECREF(orig_bases);
300 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return cls;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000302}
303
304PyDoc_STRVAR(build_class_doc,
305"__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
306\n\
307Internal helper function used by the class statement.");
308
309static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000310builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
313 "level", 0};
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400314 PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -0400315 int level = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000316
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400317 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 kwlist, &name, &globals, &locals, &fromlist, &level))
319 return NULL;
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400320 return PyImport_ImportModuleLevelObject(name, globals, locals,
321 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000322}
323
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000324PyDoc_STRVAR(import_doc,
Brett Cannoncb4996a2012-08-06 16:34:44 -0400325"__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000326\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000327Import a module. Because this function is meant for use by the Python\n\
328interpreter and not for general use it is better to use\n\
329importlib.import_module() to programmatically import a module.\n\
330\n\
331The globals argument is only used to determine the context;\n\
332they are not modified. The locals argument is unused. The fromlist\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000333should be a list of names to emulate ``from name import ...'', or an\n\
334empty list to emulate ``import name''.\n\
335When importing a module from a package, note that __import__('A.B', ...)\n\
336returns package A when fromlist is empty, but its submodule B when\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337fromlist is not empty. Level is used to determine whether to perform \n\
Brett Cannon722d3ae2012-07-30 17:45:54 -0400338absolute or relative imports. 0 is absolute while a positive number\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000340
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000341
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000342/*[clinic input]
343abs as builtin_abs
344
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300345 x: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000346 /
347
348Return the absolute value of the argument.
349[clinic start generated code]*/
350
Guido van Rossum79f25d91997-04-29 20:08:16 +0000351static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300352builtin_abs(PyObject *module, PyObject *x)
353/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000354{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000355 return PyNumber_Absolute(x);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000356}
357
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000358/*[clinic input]
359all as builtin_all
360
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300361 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000362 /
363
364Return True if bool(x) is True for all values x in the iterable.
365
366If the iterable is empty, return True.
367[clinic start generated code]*/
368
Raymond Hettinger96229b12005-03-11 06:49:40 +0000369static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300370builtin_all(PyObject *module, PyObject *iterable)
371/*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 PyObject *it, *item;
374 PyObject *(*iternext)(PyObject *);
375 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000376
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000377 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (it == NULL)
379 return NULL;
380 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 for (;;) {
383 item = iternext(it);
384 if (item == NULL)
385 break;
386 cmp = PyObject_IsTrue(item);
387 Py_DECREF(item);
388 if (cmp < 0) {
389 Py_DECREF(it);
390 return NULL;
391 }
392 if (cmp == 0) {
393 Py_DECREF(it);
394 Py_RETURN_FALSE;
395 }
396 }
397 Py_DECREF(it);
398 if (PyErr_Occurred()) {
399 if (PyErr_ExceptionMatches(PyExc_StopIteration))
400 PyErr_Clear();
401 else
402 return NULL;
403 }
404 Py_RETURN_TRUE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000405}
406
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000407/*[clinic input]
408any as builtin_any
409
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300410 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000411 /
412
413Return True if bool(x) is True for any x in the iterable.
414
415If the iterable is empty, return False.
416[clinic start generated code]*/
417
Raymond Hettinger96229b12005-03-11 06:49:40 +0000418static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300419builtin_any(PyObject *module, PyObject *iterable)
420/*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 PyObject *it, *item;
423 PyObject *(*iternext)(PyObject *);
424 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000425
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000426 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (it == NULL)
428 return NULL;
429 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 for (;;) {
432 item = iternext(it);
433 if (item == NULL)
434 break;
435 cmp = PyObject_IsTrue(item);
436 Py_DECREF(item);
437 if (cmp < 0) {
438 Py_DECREF(it);
439 return NULL;
440 }
Raymond Hettinger5098b582015-10-09 00:42:47 -0400441 if (cmp > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 Py_DECREF(it);
443 Py_RETURN_TRUE;
444 }
445 }
446 Py_DECREF(it);
447 if (PyErr_Occurred()) {
448 if (PyErr_ExceptionMatches(PyExc_StopIteration))
449 PyErr_Clear();
450 else
451 return NULL;
452 }
453 Py_RETURN_FALSE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000454}
455
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000456/*[clinic input]
457ascii as builtin_ascii
458
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300459 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000460 /
461
462Return an ASCII-only representation of an object.
463
464As repr(), return a string containing a printable representation of an
465object, but escape the non-ASCII characters in the string returned by
466repr() using \\x, \\u or \\U escapes. This generates a string similar
467to that returned by repr() in Python 2.
468[clinic start generated code]*/
469
Georg Brandl559e5d72008-06-11 18:37:52 +0000470static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300471builtin_ascii(PyObject *module, PyObject *obj)
472/*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
Georg Brandl559e5d72008-06-11 18:37:52 +0000473{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000474 return PyObject_ASCII(obj);
Georg Brandl559e5d72008-06-11 18:37:52 +0000475}
476
Georg Brandl559e5d72008-06-11 18:37:52 +0000477
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000478/*[clinic input]
479bin as builtin_bin
480
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300481 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000482 /
483
484Return the binary representation of an integer.
485
486 >>> bin(2796202)
487 '0b1010101010101010101010'
488[clinic start generated code]*/
489
Guido van Rossum79f25d91997-04-29 20:08:16 +0000490static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300491builtin_bin(PyObject *module, PyObject *number)
492/*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000493{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000494 return PyNumber_ToBase(number, 2);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000495}
496
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000497
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000498/*[clinic input]
499callable as builtin_callable
500
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300501 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000502 /
503
504Return whether the object is callable (i.e., some kind of function).
505
506Note that classes are callable, as are instances of classes with a
507__call__() method.
508[clinic start generated code]*/
509
Antoine Pitroue71362d2010-11-27 22:00:11 +0000510static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300511builtin_callable(PyObject *module, PyObject *obj)
512/*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
Antoine Pitroue71362d2010-11-27 22:00:11 +0000513{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000514 return PyBool_FromLong((long)PyCallable_Check(obj));
Antoine Pitroue71362d2010-11-27 22:00:11 +0000515}
516
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400517static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200518builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400519{
520 PyObject *hook = PySys_GetObject("breakpointhook");
521
522 if (hook == NULL) {
523 PyErr_SetString(PyExc_RuntimeError, "lost sys.breakpointhook");
524 return NULL;
525 }
526 Py_INCREF(hook);
527 PyObject *retval = _PyObject_FastCallKeywords(hook, args, nargs, keywords);
528 Py_DECREF(hook);
529 return retval;
530}
531
532PyDoc_STRVAR(breakpoint_doc,
533"breakpoint(*args, **kws)\n\
534\n\
535Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept\n\
536whatever arguments are passed.\n\
537\n\
538By default, this drops you into the pdb debugger.");
Antoine Pitroue71362d2010-11-27 22:00:11 +0000539
Raymond Hettinger17301e92008-03-13 00:19:26 +0000540typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyObject_HEAD
542 PyObject *func;
543 PyObject *it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000544} filterobject;
545
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000546static PyObject *
Raymond Hettinger17301e92008-03-13 00:19:26 +0000547filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyObject *func, *seq;
550 PyObject *it;
551 filterobject *lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000552
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +0300553 if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
557 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* Get iterator. */
560 it = PyObject_GetIter(seq);
561 if (it == NULL)
562 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 /* create filterobject structure */
565 lz = (filterobject *)type->tp_alloc(type, 0);
566 if (lz == NULL) {
567 Py_DECREF(it);
568 return NULL;
569 }
570 Py_INCREF(func);
571 lz->func = func;
572 lz->it = it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 return (PyObject *)lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000575}
576
577static void
578filter_dealloc(filterobject *lz)
579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 PyObject_GC_UnTrack(lz);
581 Py_XDECREF(lz->func);
582 Py_XDECREF(lz->it);
583 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger17301e92008-03-13 00:19:26 +0000584}
585
586static int
587filter_traverse(filterobject *lz, visitproc visit, void *arg)
588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 Py_VISIT(lz->it);
590 Py_VISIT(lz->func);
591 return 0;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000592}
593
594static PyObject *
595filter_next(filterobject *lz)
596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyObject *item;
598 PyObject *it = lz->it;
599 long ok;
600 PyObject *(*iternext)(PyObject *);
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400601 int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 iternext = *Py_TYPE(it)->tp_iternext;
604 for (;;) {
605 item = iternext(it);
606 if (item == NULL)
607 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000608
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400609 if (checktrue) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 ok = PyObject_IsTrue(item);
611 } else {
612 PyObject *good;
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100613 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (good == NULL) {
615 Py_DECREF(item);
616 return NULL;
617 }
618 ok = PyObject_IsTrue(good);
619 Py_DECREF(good);
620 }
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200621 if (ok > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 return item;
623 Py_DECREF(item);
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200624 if (ok < 0)
625 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000627}
628
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000629static PyObject *
630filter_reduce(filterobject *lz)
631{
632 return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
633}
634
635PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
636
637static PyMethodDef filter_methods[] = {
638 {"__reduce__", (PyCFunction)filter_reduce, METH_NOARGS, reduce_doc},
639 {NULL, NULL} /* sentinel */
640};
641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000642PyDoc_STRVAR(filter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +0000643"filter(function or None, iterable) --> filter object\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000644\n\
Georg Brandld11ae5d2008-05-16 13:27:32 +0000645Return an iterator yielding those items of iterable for which function(item)\n\
Raymond Hettinger17301e92008-03-13 00:19:26 +0000646is true. If function is None, return the items that are true.");
647
648PyTypeObject PyFilter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyVarObject_HEAD_INIT(&PyType_Type, 0)
650 "filter", /* tp_name */
651 sizeof(filterobject), /* tp_basicsize */
652 0, /* tp_itemsize */
653 /* methods */
654 (destructor)filter_dealloc, /* tp_dealloc */
655 0, /* tp_print */
656 0, /* tp_getattr */
657 0, /* tp_setattr */
658 0, /* tp_reserved */
659 0, /* tp_repr */
660 0, /* tp_as_number */
661 0, /* tp_as_sequence */
662 0, /* tp_as_mapping */
663 0, /* tp_hash */
664 0, /* tp_call */
665 0, /* tp_str */
666 PyObject_GenericGetAttr, /* tp_getattro */
667 0, /* tp_setattro */
668 0, /* tp_as_buffer */
669 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
670 Py_TPFLAGS_BASETYPE, /* tp_flags */
671 filter_doc, /* tp_doc */
672 (traverseproc)filter_traverse, /* tp_traverse */
673 0, /* tp_clear */
674 0, /* tp_richcompare */
675 0, /* tp_weaklistoffset */
676 PyObject_SelfIter, /* tp_iter */
677 (iternextfunc)filter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000678 filter_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 0, /* tp_members */
680 0, /* tp_getset */
681 0, /* tp_base */
682 0, /* tp_dict */
683 0, /* tp_descr_get */
684 0, /* tp_descr_set */
685 0, /* tp_dictoffset */
686 0, /* tp_init */
687 PyType_GenericAlloc, /* tp_alloc */
688 filter_new, /* tp_new */
689 PyObject_GC_Del, /* tp_free */
Raymond Hettinger17301e92008-03-13 00:19:26 +0000690};
691
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000692
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000693/*[clinic input]
694format as builtin_format
695
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300696 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000697 format_spec: unicode(c_default="NULL") = ''
698 /
699
700Return value.__format__(format_spec)
701
Amit Kumar2e6bb442017-05-29 06:32:26 +0530702format_spec defaults to the empty string.
703See the Format Specification Mini-Language section of help('FORMATTING') for
704details.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000705[clinic start generated code]*/
706
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000707static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300708builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
Amit Kumar2e6bb442017-05-29 06:32:26 +0530709/*[clinic end generated code: output=2f40bdfa4954b077 input=88339c93ea522b33]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000710{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000711 return PyObject_Format(value, format_spec);
Eric Smith8c663262007-08-25 02:26:07 +0000712}
713
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000714/*[clinic input]
715chr as builtin_chr
716
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300717 i: int
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000718 /
719
720Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
721[clinic start generated code]*/
722
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000723static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300724builtin_chr_impl(PyObject *module, int i)
725/*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000726{
727 return PyUnicode_FromOrdinal(i);
728}
Guido van Rossum09095f32000-03-10 23:00:52 +0000729
730
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200731static const char *
Martin Pantereeb896c2015-11-07 02:32:21 +0000732source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000733{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200734 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 Py_ssize_t size;
Martin Pantereeb896c2015-11-07 02:32:21 +0000736 Py_buffer view;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000737
Martin Pantereeb896c2015-11-07 02:32:21 +0000738 *cmd_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 if (PyUnicode_Check(cmd)) {
740 cf->cf_flags |= PyCF_IGNORE_COOKIE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200741 str = PyUnicode_AsUTF8AndSize(cmd, &size);
742 if (str == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 return NULL;
744 }
Martin Pantereeb896c2015-11-07 02:32:21 +0000745 else if (PyBytes_Check(cmd)) {
746 str = PyBytes_AS_STRING(cmd);
747 size = PyBytes_GET_SIZE(cmd);
748 }
749 else if (PyByteArray_Check(cmd)) {
750 str = PyByteArray_AS_STRING(cmd);
751 size = PyByteArray_GET_SIZE(cmd);
752 }
753 else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
754 /* Copy to NUL-terminated buffer. */
755 *cmd_copy = PyBytes_FromStringAndSize(
756 (const char *)view.buf, view.len);
757 PyBuffer_Release(&view);
758 if (*cmd_copy == NULL) {
759 return NULL;
760 }
761 str = PyBytes_AS_STRING(*cmd_copy);
762 size = PyBytes_GET_SIZE(*cmd_copy);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200763 }
764 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 PyErr_Format(PyExc_TypeError,
766 "%s() arg 1 must be a %s object",
767 funcname, what);
768 return NULL;
769 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200770
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +0200771 if (strlen(str) != (size_t)size) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300772 PyErr_SetString(PyExc_ValueError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 "source code string cannot contain null bytes");
Martin Pantereeb896c2015-11-07 02:32:21 +0000774 Py_CLEAR(*cmd_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 return NULL;
776 }
777 return str;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000778}
779
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000780/*[clinic input]
781compile as builtin_compile
782
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300783 source: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000784 filename: object(converter="PyUnicode_FSDecoder")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300785 mode: str
786 flags: int = 0
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200787 dont_inherit: bool(accept={int}) = False
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300788 optimize: int = -1
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000789
790Compile source into a code object that can be executed by exec() or eval().
791
792The source code may represent a Python module, statement or expression.
793The filename will be used for run-time error messages.
794The mode must be 'exec' to compile a module, 'single' to compile a
795single (interactive) statement, or 'eval' to compile an expression.
796The flags argument, if present, controls which future statements influence
797the compilation of the code.
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300798The dont_inherit argument, if true, stops the compilation inheriting
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000799the effects of any future statements in effect in the code calling
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300800compile; if absent or false these statements do influence the compilation,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000801in addition to any features explicitly specified.
802[clinic start generated code]*/
803
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000804static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300805builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
806 const char *mode, int flags, int dont_inherit,
807 int optimize)
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200808/*[clinic end generated code: output=1fa176e33452bb63 input=0ff726f595eb9fcd]*/
Guido van Rossum5b722181993-03-30 17:46:03 +0000809{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000810 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200811 const char *str;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000812 int compile_mode = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 int is_ast;
814 PyCompilerFlags cf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000816 PyObject *result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000817
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000818 cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
Tim Peters6cd6a822001-08-17 22:11:27 +0000819
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000820 if (flags &
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
822 {
823 PyErr_SetString(PyExc_ValueError,
824 "compile(): unrecognised flags");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000825 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 }
827 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
Tim Peters6cd6a822001-08-17 22:11:27 +0000828
Georg Brandl8334fd92010-12-04 10:26:46 +0000829 if (optimize < -1 || optimize > 2) {
830 PyErr_SetString(PyExc_ValueError,
831 "compile(): invalid optimize value");
832 goto error;
833 }
834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 if (!dont_inherit) {
836 PyEval_MergeCompilerFlags(&cf);
837 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000838
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000839 if (strcmp(mode, "exec") == 0)
840 compile_mode = 0;
841 else if (strcmp(mode, "eval") == 0)
842 compile_mode = 1;
843 else if (strcmp(mode, "single") == 0)
844 compile_mode = 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 else {
846 PyErr_SetString(PyExc_ValueError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000847 "compile() mode must be 'exec', 'eval' or 'single'");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000848 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 }
Neal Norwitzdb4115f2008-03-31 04:20:05 +0000850
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000851 is_ast = PyAST_Check(source);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (is_ast == -1)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000853 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 if (is_ast) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000855 if (flags & PyCF_ONLY_AST) {
856 Py_INCREF(source);
857 result = source;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 }
859 else {
860 PyArena *arena;
861 mod_ty mod;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 arena = PyArena_New();
Stefan Krah07795df2012-08-20 17:19:50 +0200864 if (arena == NULL)
865 goto error;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000866 mod = PyAST_obj2mod(source, arena, compile_mode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (mod == NULL) {
868 PyArena_Free(arena);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000869 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 }
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500871 if (!PyAST_Validate(mod)) {
872 PyArena_Free(arena);
873 goto error;
874 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200875 result = (PyObject*)PyAST_CompileObject(mod, filename,
876 &cf, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 PyArena_Free(arena);
878 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000879 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000881
Martin Panter61d6e4a2015-11-07 02:56:11 +0000882 str = source_as_string(source, "compile", "string, bytes or AST", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (str == NULL)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000884 goto error;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000885
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000886 result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000887 Py_XDECREF(source_copy);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000888 goto finally;
889
890error:
891 result = NULL;
892finally:
Victor Stinner14e461d2013-08-26 22:28:21 +0200893 Py_DECREF(filename);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000894 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000895}
896
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000897/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000899builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
904 return NULL;
905 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000906}
907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000908PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000909"dir([object]) -> list of strings\n"
910"\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000911"If called without an argument, return the names in the current scope.\n"
912"Else, return an alphabetized list of names comprising (some of) the attributes\n"
913"of the given object, and of attributes reachable from it.\n"
914"If the object supplies a method named __dir__, it will be used; otherwise\n"
915"the default dir() logic is used and returns:\n"
916" for a module object: the module's attributes.\n"
917" for a class object: its attributes, and recursively the attributes\n"
918" of its bases.\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000919" for any other object: its attributes, its class's attributes, and\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000920" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000921
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000922/*[clinic input]
923divmod as builtin_divmod
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000924
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300925 x: object
926 y: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000927 /
928
Zachary Ware7f227d92016-04-28 14:39:50 -0500929Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000930[clinic start generated code]*/
931
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000932static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300933builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
934/*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000935{
936 return PyNumber_Divmod(x, y);
937}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000938
939
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000940/*[clinic input]
941eval as builtin_eval
942
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300943 source: object
944 globals: object = None
945 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000946 /
947
948Evaluate the given source in the context of globals and locals.
949
950The source may be a string representing a Python expression
951or a code object as returned by compile().
952The globals must be a dictionary and locals can be any mapping,
953defaulting to the current globals and locals.
954If only globals is given, locals defaults to it.
955[clinic start generated code]*/
956
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000957static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300958builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400959 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300960/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000961{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000962 PyObject *result, *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200963 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 if (locals != Py_None && !PyMapping_Check(locals)) {
967 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
968 return NULL;
969 }
970 if (globals != Py_None && !PyDict_Check(globals)) {
971 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
972 "globals must be a real dict; try eval(expr, {}, mapping)"
973 : "globals must be a dict");
974 return NULL;
975 }
976 if (globals == Py_None) {
977 globals = PyEval_GetGlobals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100978 if (locals == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100980 if (locals == NULL)
981 return NULL;
982 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 }
984 else if (locals == Py_None)
985 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 if (globals == NULL || locals == NULL) {
988 PyErr_SetString(PyExc_TypeError,
989 "eval must be given globals and locals "
990 "when called without a frame");
991 return NULL;
992 }
Georg Brandl77c85e62005-09-15 10:46:13 +0000993
Victor Stinnerb44562b2013-11-06 19:03:11 +0100994 if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
995 if (_PyDict_SetItemId(globals, &PyId___builtins__,
996 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 return NULL;
998 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000999
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001000 if (PyCode_Check(source)) {
1001 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyErr_SetString(PyExc_TypeError,
1003 "code object passed to eval() may not contain free variables");
1004 return NULL;
1005 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001006 return PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 }
Tim Peters9fa96be2001-08-17 23:04:59 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Martin Panter61d6e4a2015-11-07 02:56:11 +00001010 str = source_as_string(source, "eval", "string, bytes or code", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 if (str == NULL)
1012 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 while (*str == ' ' || *str == '\t')
1015 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +00001016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 (void)PyEval_MergeCompilerFlags(&cf);
1018 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Martin Panter61d6e4a2015-11-07 02:56:11 +00001019 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001021}
1022
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001023/*[clinic input]
1024exec as builtin_exec
1025
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001026 source: object
1027 globals: object = None
1028 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001029 /
1030
1031Execute the given source in the context of globals and locals.
1032
1033The source may be a string representing one or more Python statements
1034or a code object as returned by compile().
1035The globals must be a dictionary and locals can be any mapping,
1036defaulting to the current globals and locals.
1037If only globals is given, locals defaults to it.
1038[clinic start generated code]*/
1039
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001040static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001041builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -04001042 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001043/*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
Georg Brandl7cae87c2006-09-06 06:51:57 +00001044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 PyObject *v;
Georg Brandl2cabc562008-08-28 07:57:16 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (globals == Py_None) {
1048 globals = PyEval_GetGlobals();
1049 if (locals == Py_None) {
1050 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01001051 if (locals == NULL)
1052 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 }
1054 if (!globals || !locals) {
1055 PyErr_SetString(PyExc_SystemError,
1056 "globals and locals cannot be NULL");
1057 return NULL;
1058 }
1059 }
1060 else if (locals == Py_None)
1061 locals = globals;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (!PyDict_Check(globals)) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001064 PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 globals->ob_type->tp_name);
1066 return NULL;
1067 }
1068 if (!PyMapping_Check(locals)) {
1069 PyErr_Format(PyExc_TypeError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001070 "locals must be a mapping or None, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 locals->ob_type->tp_name);
1072 return NULL;
1073 }
Victor Stinnerb44562b2013-11-06 19:03:11 +01001074 if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
1075 if (_PyDict_SetItemId(globals, &PyId___builtins__,
1076 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 return NULL;
1078 }
1079
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001080 if (PyCode_Check(source)) {
1081 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 PyErr_SetString(PyExc_TypeError,
1083 "code object passed to exec() may not "
1084 "contain free variables");
1085 return NULL;
1086 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001087 v = PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 }
1089 else {
Martin Panter61d6e4a2015-11-07 02:56:11 +00001090 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001091 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 PyCompilerFlags cf;
1093 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001094 str = source_as_string(source, "exec",
Martin Panter61d6e4a2015-11-07 02:56:11 +00001095 "string, bytes or code", &cf,
1096 &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (str == NULL)
1098 return NULL;
1099 if (PyEval_MergeCompilerFlags(&cf))
1100 v = PyRun_StringFlags(str, Py_file_input, globals,
1101 locals, &cf);
1102 else
1103 v = PyRun_String(str, Py_file_input, globals, locals);
Martin Panter61d6e4a2015-11-07 02:56:11 +00001104 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 }
1106 if (v == NULL)
1107 return NULL;
1108 Py_DECREF(v);
1109 Py_RETURN_NONE;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001110}
1111
Georg Brandl7cae87c2006-09-06 06:51:57 +00001112
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001113/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001114static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001115builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Guido van Rossum33894be1992-01-27 16:53:09 +00001116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 PyObject *v, *result, *dflt = NULL;
1118 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001119
Sylvain96c7c062017-06-15 17:05:23 +02001120 if (!_PyArg_UnpackStack(args, nargs, "getattr", 2, 3, &v, &name, &dflt))
1121 return NULL;
1122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (!PyUnicode_Check(name)) {
1124 PyErr_SetString(PyExc_TypeError,
1125 "getattr(): attribute name must be string");
1126 return NULL;
1127 }
1128 result = PyObject_GetAttr(v, name);
1129 if (result == NULL && dflt != NULL &&
1130 PyErr_ExceptionMatches(PyExc_AttributeError))
1131 {
1132 PyErr_Clear();
1133 Py_INCREF(dflt);
1134 result = dflt;
1135 }
1136 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001137}
1138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001139PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +00001140"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001141\n\
Guido van Rossum950ff291998-06-29 13:38:57 +00001142Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
1143When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001144exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001145
1146
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001147/*[clinic input]
1148globals as builtin_globals
1149
1150Return the dictionary containing the current scope's global variables.
1151
1152NOTE: Updates to this dictionary *will* affect name lookups in the current
1153global scope and vice-versa.
1154[clinic start generated code]*/
1155
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001156static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001157builtin_globals_impl(PyObject *module)
1158/*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 d = PyEval_GetGlobals();
1163 Py_XINCREF(d);
1164 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001165}
1166
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001167
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001168/*[clinic input]
1169hasattr as builtin_hasattr
1170
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001171 obj: object
1172 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001173 /
1174
1175Return whether the object has an attribute with the given name.
1176
1177This is done by calling getattr(obj, name) and catching AttributeError.
1178[clinic start generated code]*/
1179
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001180static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001181builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1182/*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001183{
1184 PyObject *v;
1185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (!PyUnicode_Check(name)) {
1187 PyErr_SetString(PyExc_TypeError,
1188 "hasattr(): attribute name must be string");
1189 return NULL;
1190 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001191 v = PyObject_GetAttr(obj, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 if (v == NULL) {
Benjamin Peterson17689992010-08-24 03:26:23 +00001193 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 PyErr_Clear();
Benjamin Peterson17689992010-08-24 03:26:23 +00001195 Py_RETURN_FALSE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
Benjamin Peterson17689992010-08-24 03:26:23 +00001197 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 }
1199 Py_DECREF(v);
Benjamin Peterson17689992010-08-24 03:26:23 +00001200 Py_RETURN_TRUE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001201}
1202
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001203
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001204/* AC: gdb's integration with CPython relies on builtin_id having
1205 * the *exact* parameter names of "self" and "v", so we ensure we
1206 * preserve those name rather than using the AC defaults.
1207 */
1208/*[clinic input]
1209id as builtin_id
1210
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001211 self: self(type="PyModuleDef *")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001212 obj as v: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001213 /
1214
1215Return the identity of an object.
1216
1217This is guaranteed to be unique among simultaneously existing objects.
1218(CPython uses the object's memory address.)
1219[clinic start generated code]*/
1220
Guido van Rossum79f25d91997-04-29 20:08:16 +00001221static PyObject *
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001222builtin_id(PyModuleDef *self, PyObject *v)
1223/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
Guido van Rossum5b722181993-03-30 17:46:03 +00001224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +00001226}
1227
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001228
Raymond Hettingera6c60372008-03-13 01:26:19 +00001229/* map object ************************************************************/
1230
1231typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 PyObject_HEAD
1233 PyObject *iters;
1234 PyObject *func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001235} mapobject;
1236
Guido van Rossum79f25d91997-04-29 20:08:16 +00001237static PyObject *
Raymond Hettingera6c60372008-03-13 01:26:19 +00001238map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 PyObject *it, *iters, *func;
1241 mapobject *lz;
1242 Py_ssize_t numargs, i;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001243
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001244 if (type == &PyMap_Type && !_PyArg_NoKeywords("map", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 numargs = PyTuple_Size(args);
1248 if (numargs < 2) {
1249 PyErr_SetString(PyExc_TypeError,
1250 "map() must have at least two arguments.");
1251 return NULL;
1252 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 iters = PyTuple_New(numargs-1);
1255 if (iters == NULL)
1256 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 for (i=1 ; i<numargs ; i++) {
1259 /* Get iterator. */
1260 it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1261 if (it == NULL) {
1262 Py_DECREF(iters);
1263 return NULL;
1264 }
1265 PyTuple_SET_ITEM(iters, i-1, it);
1266 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 /* create mapobject structure */
1269 lz = (mapobject *)type->tp_alloc(type, 0);
1270 if (lz == NULL) {
1271 Py_DECREF(iters);
1272 return NULL;
1273 }
1274 lz->iters = iters;
1275 func = PyTuple_GET_ITEM(args, 0);
1276 Py_INCREF(func);
1277 lz->func = func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 return (PyObject *)lz;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001280}
1281
1282static void
1283map_dealloc(mapobject *lz)
1284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyObject_GC_UnTrack(lz);
1286 Py_XDECREF(lz->iters);
1287 Py_XDECREF(lz->func);
1288 Py_TYPE(lz)->tp_free(lz);
Raymond Hettingera6c60372008-03-13 01:26:19 +00001289}
1290
1291static int
1292map_traverse(mapobject *lz, visitproc visit, void *arg)
1293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 Py_VISIT(lz->iters);
1295 Py_VISIT(lz->func);
1296 return 0;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001297}
1298
1299static PyObject *
1300map_next(mapobject *lz)
1301{
Victor Stinnerbc08ab42016-12-15 12:40:53 +01001302 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001303 PyObject **stack;
1304 Py_ssize_t niters, nargs, i;
1305 PyObject *result = NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001306
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001307 niters = PyTuple_GET_SIZE(lz->iters);
1308 if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1309 stack = small_stack;
1310 }
1311 else {
1312 stack = PyMem_Malloc(niters * sizeof(stack[0]));
1313 if (stack == NULL) {
1314 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 return NULL;
1316 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 }
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001318
1319 nargs = 0;
1320 for (i=0; i < niters; i++) {
1321 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1322 PyObject *val = Py_TYPE(it)->tp_iternext(it);
1323 if (val == NULL) {
1324 goto exit;
1325 }
1326 stack[i] = val;
1327 nargs++;
1328 }
1329
1330 result = _PyObject_FastCall(lz->func, stack, nargs);
1331
1332exit:
1333 for (i=0; i < nargs; i++) {
1334 Py_DECREF(stack[i]);
1335 }
1336 if (stack != small_stack) {
1337 PyMem_Free(stack);
1338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001340}
1341
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001342static PyObject *
1343map_reduce(mapobject *lz)
1344{
1345 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1346 PyObject *args = PyTuple_New(numargs+1);
1347 Py_ssize_t i;
1348 if (args == NULL)
1349 return NULL;
1350 Py_INCREF(lz->func);
1351 PyTuple_SET_ITEM(args, 0, lz->func);
1352 for (i = 0; i<numargs; i++){
1353 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1354 Py_INCREF(it);
1355 PyTuple_SET_ITEM(args, i+1, it);
1356 }
1357
1358 return Py_BuildValue("ON", Py_TYPE(lz), args);
1359}
1360
1361static PyMethodDef map_methods[] = {
1362 {"__reduce__", (PyCFunction)map_reduce, METH_NOARGS, reduce_doc},
1363 {NULL, NULL} /* sentinel */
1364};
1365
1366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001367PyDoc_STRVAR(map_doc,
Raymond Hettingera6c60372008-03-13 01:26:19 +00001368"map(func, *iterables) --> map object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001369\n\
Raymond Hettingera6c60372008-03-13 01:26:19 +00001370Make an iterator that computes the function using arguments from\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371each of the iterables. Stops when the shortest iterable is exhausted.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001372
Raymond Hettingera6c60372008-03-13 01:26:19 +00001373PyTypeObject PyMap_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1375 "map", /* tp_name */
1376 sizeof(mapobject), /* tp_basicsize */
1377 0, /* tp_itemsize */
1378 /* methods */
1379 (destructor)map_dealloc, /* tp_dealloc */
1380 0, /* tp_print */
1381 0, /* tp_getattr */
1382 0, /* tp_setattr */
1383 0, /* tp_reserved */
1384 0, /* tp_repr */
1385 0, /* tp_as_number */
1386 0, /* tp_as_sequence */
1387 0, /* tp_as_mapping */
1388 0, /* tp_hash */
1389 0, /* tp_call */
1390 0, /* tp_str */
1391 PyObject_GenericGetAttr, /* tp_getattro */
1392 0, /* tp_setattro */
1393 0, /* tp_as_buffer */
1394 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1395 Py_TPFLAGS_BASETYPE, /* tp_flags */
1396 map_doc, /* tp_doc */
1397 (traverseproc)map_traverse, /* tp_traverse */
1398 0, /* tp_clear */
1399 0, /* tp_richcompare */
1400 0, /* tp_weaklistoffset */
1401 PyObject_SelfIter, /* tp_iter */
1402 (iternextfunc)map_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001403 map_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 0, /* tp_members */
1405 0, /* tp_getset */
1406 0, /* tp_base */
1407 0, /* tp_dict */
1408 0, /* tp_descr_get */
1409 0, /* tp_descr_set */
1410 0, /* tp_dictoffset */
1411 0, /* tp_init */
1412 PyType_GenericAlloc, /* tp_alloc */
1413 map_new, /* tp_new */
1414 PyObject_GC_Del, /* tp_free */
Raymond Hettingera6c60372008-03-13 01:26:19 +00001415};
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001416
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001417
1418/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001420builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Georg Brandla18af4e2007-04-21 15:47:16 +00001421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 PyObject *it, *res;
1423 PyObject *def = NULL;
Georg Brandla18af4e2007-04-21 15:47:16 +00001424
Sylvain96c7c062017-06-15 17:05:23 +02001425 if (!_PyArg_UnpackStack(args, nargs, "next", 1, 2, &it, &def))
1426 return NULL;
1427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if (!PyIter_Check(it)) {
1429 PyErr_Format(PyExc_TypeError,
Philip Jenvey50add042011-11-06 16:37:52 -08001430 "'%.200s' object is not an iterator",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 it->ob_type->tp_name);
1432 return NULL;
1433 }
1434
1435 res = (*it->ob_type->tp_iternext)(it);
1436 if (res != NULL) {
1437 return res;
1438 } else if (def != NULL) {
1439 if (PyErr_Occurred()) {
1440 if(!PyErr_ExceptionMatches(PyExc_StopIteration))
1441 return NULL;
1442 PyErr_Clear();
1443 }
1444 Py_INCREF(def);
1445 return def;
1446 } else if (PyErr_Occurred()) {
1447 return NULL;
1448 } else {
1449 PyErr_SetNone(PyExc_StopIteration);
1450 return NULL;
1451 }
Georg Brandla18af4e2007-04-21 15:47:16 +00001452}
1453
1454PyDoc_STRVAR(next_doc,
1455"next(iterator[, default])\n\
1456\n\
1457Return the next item from the iterator. If default is given and the iterator\n\
1458is exhausted, it is returned instead of raising StopIteration.");
1459
1460
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001461/*[clinic input]
1462setattr as builtin_setattr
1463
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001464 obj: object
1465 name: object
1466 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001467 /
1468
1469Sets the named attribute on the given object to the specified value.
1470
1471setattr(x, 'y', v) is equivalent to ``x.y = v''
1472[clinic start generated code]*/
1473
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001474static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001475builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
Larry Hastings89964c42015-04-14 18:07:59 -04001476 PyObject *value)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001477/*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001478{
1479 if (PyObject_SetAttr(obj, name, value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001481 Py_RETURN_NONE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001482}
1483
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001484
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001485/*[clinic input]
1486delattr as builtin_delattr
1487
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001488 obj: object
1489 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001490 /
1491
1492Deletes the named attribute from the given object.
1493
1494delattr(x, 'y') is equivalent to ``del x.y''
1495[clinic start generated code]*/
1496
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001497static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001498builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1499/*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001500{
1501 if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001503 Py_RETURN_NONE;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001504}
1505
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001506
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001507/*[clinic input]
1508hash as builtin_hash
1509
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001510 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001511 /
1512
1513Return the hash value for the given object.
1514
1515Two objects that compare equal must also have the same hash value, but the
1516reverse is not necessarily true.
1517[clinic start generated code]*/
1518
Guido van Rossum79f25d91997-04-29 20:08:16 +00001519static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001520builtin_hash(PyObject *module, PyObject *obj)
1521/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001522{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001523 Py_hash_t x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001524
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001525 x = PyObject_Hash(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 if (x == -1)
1527 return NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001528 return PyLong_FromSsize_t(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001529}
1530
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001531
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001532/*[clinic input]
1533hex as builtin_hex
1534
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001535 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001536 /
1537
1538Return the hexadecimal representation of an integer.
1539
1540 >>> hex(12648430)
1541 '0xc0ffee'
1542[clinic start generated code]*/
1543
Guido van Rossum79f25d91997-04-29 20:08:16 +00001544static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001545builtin_hex(PyObject *module, PyObject *number)
1546/*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001547{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001548 return PyNumber_ToBase(number, 16);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001549}
1550
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001551
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001552/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001553static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001554builtin_iter(PyObject *self, PyObject *args)
1555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 PyObject *v, *w = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
1559 return NULL;
1560 if (w == NULL)
1561 return PyObject_GetIter(v);
1562 if (!PyCallable_Check(v)) {
1563 PyErr_SetString(PyExc_TypeError,
1564 "iter(v, w): v must be callable");
1565 return NULL;
1566 }
1567 return PyCallIter_New(v, w);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001568}
1569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001570PyDoc_STRVAR(iter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +00001571"iter(iterable) -> iterator\n\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001572iter(callable, sentinel) -> iterator\n\
1573\n\
1574Get an iterator from an object. In the first form, the argument must\n\
1575supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001576In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001577
1578
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001579/*[clinic input]
1580len as builtin_len
1581
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001582 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001583 /
1584
1585Return the number of items in a container.
1586[clinic start generated code]*/
1587
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001588static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001589builtin_len(PyObject *module, PyObject *obj)
1590/*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001593
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001594 res = PyObject_Size(obj);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001595 if (res < 0) {
1596 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 return NULL;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001598 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 return PyLong_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001600}
1601
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001602
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001603/*[clinic input]
1604locals as builtin_locals
1605
1606Return a dictionary containing the current scope's local variables.
1607
1608NOTE: Whether or not updates to this dictionary will affect name lookups in
1609the local scope and vice-versa is *implementation dependent* and not
1610covered by any backwards compatibility guarantees.
1611[clinic start generated code]*/
1612
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001613static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001614builtin_locals_impl(PyObject *module)
1615/*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 d = PyEval_GetLocals();
1620 Py_XINCREF(d);
1621 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001622}
1623
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001624
Guido van Rossum79f25d91997-04-29 20:08:16 +00001625static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001626min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001629 PyObject *emptytuple, *defaultval = NULL;
1630 static char *kwlist[] = {"key", "default", NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 const char *name = op == Py_LT ? "min" : "max";
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001632 const int positional = PyTuple_Size(args) > 1;
1633 int ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001634
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001635 if (positional)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 v = args;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001637 else if (!PyArg_UnpackTuple(args, name, 1, 1, &v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001639
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001640 emptytuple = PyTuple_New(0);
1641 if (emptytuple == NULL)
1642 return NULL;
Oren Milman58cf7482017-08-21 20:19:07 +03001643 ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds,
1644 (op == Py_LT) ? "|$OO:min" : "|$OO:max",
1645 kwlist, &keyfunc, &defaultval);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001646 Py_DECREF(emptytuple);
1647 if (!ret)
1648 return NULL;
1649
1650 if (positional && defaultval != NULL) {
1651 PyErr_Format(PyExc_TypeError,
1652 "Cannot specify a default for %s() with multiple "
1653 "positional arguments", name);
1654 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 it = PyObject_GetIter(v);
1658 if (it == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 return NULL;
1660 }
Tim Petersc3074532001-05-03 07:00:32 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 maxitem = NULL; /* the result */
1663 maxval = NULL; /* the value associated with the result */
1664 while (( item = PyIter_Next(it) )) {
1665 /* get the value from the key function */
1666 if (keyfunc != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001667 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (val == NULL)
1669 goto Fail_it_item;
1670 }
1671 /* no key function; the value is the item */
1672 else {
1673 val = item;
1674 Py_INCREF(val);
1675 }
Tim Petersc3074532001-05-03 07:00:32 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 /* maximum value and item are unset; set them */
1678 if (maxval == NULL) {
1679 maxitem = item;
1680 maxval = val;
1681 }
1682 /* maximum value and item are set; update them as necessary */
1683 else {
1684 int cmp = PyObject_RichCompareBool(val, maxval, op);
1685 if (cmp < 0)
1686 goto Fail_it_item_and_val;
1687 else if (cmp > 0) {
1688 Py_DECREF(maxval);
1689 Py_DECREF(maxitem);
1690 maxval = val;
1691 maxitem = item;
1692 }
1693 else {
1694 Py_DECREF(item);
1695 Py_DECREF(val);
1696 }
1697 }
1698 }
1699 if (PyErr_Occurred())
1700 goto Fail_it;
1701 if (maxval == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 assert(maxitem == NULL);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001703 if (defaultval != NULL) {
1704 Py_INCREF(defaultval);
1705 maxitem = defaultval;
1706 } else {
1707 PyErr_Format(PyExc_ValueError,
1708 "%s() arg is an empty sequence", name);
1709 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 }
1711 else
1712 Py_DECREF(maxval);
1713 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 return maxitem;
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001715
1716Fail_it_item_and_val:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 Py_DECREF(val);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001718Fail_it_item:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 Py_DECREF(item);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001720Fail_it:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 Py_XDECREF(maxval);
1722 Py_XDECREF(maxitem);
1723 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001725}
1726
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001727/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001728static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001729builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001732}
1733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001734PyDoc_STRVAR(min_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001735"min(iterable, *[, default=obj, key=func]) -> value\n\
1736min(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001737\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001738With a single iterable argument, return its smallest item. The\n\
1739default keyword-only argument specifies an object to return if\n\
1740the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001741With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001742
1743
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001744/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001745static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001746builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001749}
1750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001751PyDoc_STRVAR(max_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001752"max(iterable, *[, default=obj, key=func]) -> value\n\
1753max(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001754\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001755With a single iterable argument, return its biggest item. The\n\
1756default keyword-only argument specifies an object to return if\n\
1757the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001758With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001759
1760
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001761/*[clinic input]
1762oct as builtin_oct
1763
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001764 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001765 /
1766
1767Return the octal representation of an integer.
1768
1769 >>> oct(342391)
1770 '0o1234567'
1771[clinic start generated code]*/
1772
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001774builtin_oct(PyObject *module, PyObject *number)
1775/*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001776{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001777 return PyNumber_ToBase(number, 8);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001778}
1779
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001780
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001781/*[clinic input]
1782ord as builtin_ord
1783
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001784 c: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001785 /
1786
1787Return the Unicode code point for a one-character string.
1788[clinic start generated code]*/
1789
Guido van Rossum79f25d91997-04-29 20:08:16 +00001790static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001791builtin_ord(PyObject *module, PyObject *c)
1792/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 long ord;
1795 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001796
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001797 if (PyBytes_Check(c)) {
1798 size = PyBytes_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001800 ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 return PyLong_FromLong(ord);
1802 }
1803 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001804 else if (PyUnicode_Check(c)) {
1805 if (PyUnicode_READY(c) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001806 return NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001807 size = PyUnicode_GET_LENGTH(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001809 ord = (long)PyUnicode_READ_CHAR(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 return PyLong_FromLong(ord);
1811 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001813 else if (PyByteArray_Check(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 /* XXX Hopefully this is temporary */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001815 size = PyByteArray_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001817 ord = (long)((unsigned char)*PyByteArray_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 return PyLong_FromLong(ord);
1819 }
1820 }
1821 else {
1822 PyErr_Format(PyExc_TypeError,
1823 "ord() expected string of length 1, but " \
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001824 "%.200s found", c->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 return NULL;
1826 }
Guido van Rossum09095f32000-03-10 23:00:52 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 PyErr_Format(PyExc_TypeError,
1829 "ord() expected a character, "
1830 "but string of length %zd found",
1831 size);
1832 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001833}
1834
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001835
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001836/*[clinic input]
1837pow as builtin_pow
1838
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001839 x: object
1840 y: object
1841 z: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001842 /
1843
1844Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
1845
1846Some types, such as ints, are able to use a more efficient algorithm when
1847invoked using the three argument form.
1848[clinic start generated code]*/
1849
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001850static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001851builtin_pow_impl(PyObject *module, PyObject *x, PyObject *y, PyObject *z)
1852/*[clinic end generated code: output=50a14d5d130d404b input=653d57d38d41fc07]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001853{
1854 return PyNumber_Power(x, y, z);
1855}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001856
1857
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001858/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum34343512006-11-30 22:13:52 +00001859static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001860builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Guido van Rossum34343512006-11-30 22:13:52 +00001861{
INADA Naokibd584f12017-01-19 12:50:34 +01001862 static const char * const _keywords[] = {"sep", "end", "file", "flush", 0};
1863 static struct _PyArg_Parser _parser = {"|OOOO:print", _keywords, 0};
Georg Brandlbc3b6822012-01-13 19:41:25 +01001864 PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 int i, err;
Guido van Rossum34343512006-11-30 22:13:52 +00001866
INADA Naokibd584f12017-01-19 12:50:34 +01001867 if (kwnames != NULL &&
1868 !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser,
1869 &sep, &end, &file, &flush)) {
Georg Brandlbc3b6822012-01-13 19:41:25 +01001870 return NULL;
INADA Naokibd584f12017-01-19 12:50:34 +01001871 }
1872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 if (file == NULL || file == Py_None) {
Victor Stinnerbd303c12013-11-07 23:07:29 +01001874 file = _PySys_GetObjectId(&PyId_stdout);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001875 if (file == NULL) {
1876 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1877 return NULL;
1878 }
1879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 /* sys.stdout may be None when FILE* stdout isn't connected */
1881 if (file == Py_None)
1882 Py_RETURN_NONE;
1883 }
Guido van Rossum34343512006-11-30 22:13:52 +00001884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 if (sep == Py_None) {
1886 sep = NULL;
1887 }
1888 else if (sep && !PyUnicode_Check(sep)) {
1889 PyErr_Format(PyExc_TypeError,
1890 "sep must be None or a string, not %.200s",
1891 sep->ob_type->tp_name);
1892 return NULL;
1893 }
1894 if (end == Py_None) {
1895 end = NULL;
1896 }
1897 else if (end && !PyUnicode_Check(end)) {
1898 PyErr_Format(PyExc_TypeError,
1899 "end must be None or a string, not %.200s",
1900 end->ob_type->tp_name);
1901 return NULL;
1902 }
Guido van Rossum34343512006-11-30 22:13:52 +00001903
INADA Naokibd584f12017-01-19 12:50:34 +01001904 for (i = 0; i < nargs; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 if (i > 0) {
1906 if (sep == NULL)
1907 err = PyFile_WriteString(" ", file);
1908 else
1909 err = PyFile_WriteObject(sep, file,
1910 Py_PRINT_RAW);
1911 if (err)
1912 return NULL;
1913 }
INADA Naokibd584f12017-01-19 12:50:34 +01001914 err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (err)
1916 return NULL;
1917 }
Guido van Rossum34343512006-11-30 22:13:52 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 if (end == NULL)
1920 err = PyFile_WriteString("\n", file);
1921 else
1922 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1923 if (err)
1924 return NULL;
Guido van Rossum34343512006-11-30 22:13:52 +00001925
Georg Brandlbc3b6822012-01-13 19:41:25 +01001926 if (flush != NULL) {
1927 PyObject *tmp;
1928 int do_flush = PyObject_IsTrue(flush);
1929 if (do_flush == -1)
1930 return NULL;
1931 else if (do_flush) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001932 tmp = _PyObject_CallMethodId(file, &PyId_flush, NULL);
Georg Brandlbc3b6822012-01-13 19:41:25 +01001933 if (tmp == NULL)
1934 return NULL;
1935 else
1936 Py_DECREF(tmp);
1937 }
1938 }
1939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 Py_RETURN_NONE;
Guido van Rossum34343512006-11-30 22:13:52 +00001941}
1942
1943PyDoc_STRVAR(print_doc,
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001944"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\
Guido van Rossum34343512006-11-30 22:13:52 +00001945\n\
1946Prints the values to a stream, or to sys.stdout by default.\n\
1947Optional keyword arguments:\n\
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001948file: a file-like object (stream); defaults to the current sys.stdout.\n\
1949sep: string inserted between values, default a space.\n\
1950end: string appended after the last value, default a newline.\n\
1951flush: whether to forcibly flush the stream.");
Guido van Rossum34343512006-11-30 22:13:52 +00001952
1953
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001954/*[clinic input]
1955input as builtin_input
1956
1957 prompt: object(c_default="NULL") = None
1958 /
1959
1960Read a string from standard input. The trailing newline is stripped.
1961
1962The prompt string, if given, is printed to standard output without a
1963trailing newline before reading input.
1964
1965If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
1966On *nix systems, readline is used if available.
1967[clinic start generated code]*/
1968
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001969static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001970builtin_input_impl(PyObject *module, PyObject *prompt)
1971/*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001972{
Victor Stinnerbd303c12013-11-07 23:07:29 +01001973 PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
1974 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1975 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 PyObject *tmp;
1977 long fd;
1978 int tty;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 /* Check that stdin/out/err are intact */
1981 if (fin == NULL || fin == Py_None) {
1982 PyErr_SetString(PyExc_RuntimeError,
1983 "input(): lost sys.stdin");
1984 return NULL;
1985 }
1986 if (fout == NULL || fout == Py_None) {
1987 PyErr_SetString(PyExc_RuntimeError,
1988 "input(): lost sys.stdout");
1989 return NULL;
1990 }
1991 if (ferr == NULL || ferr == Py_None) {
1992 PyErr_SetString(PyExc_RuntimeError,
1993 "input(): lost sys.stderr");
1994 return NULL;
1995 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 /* First of all, flush stderr */
Victor Stinner3466bde2016-09-05 18:16:01 -07001998 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 if (tmp == NULL)
2000 PyErr_Clear();
2001 else
2002 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 /* We should only use (GNU) readline if Python's sys.stdin and
2005 sys.stdout are the same as C's stdin and stdout, because we
2006 need to pass it those. */
Victor Stinner3466bde2016-09-05 18:16:01 -07002007 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (tmp == NULL) {
2009 PyErr_Clear();
2010 tty = 0;
2011 }
2012 else {
2013 fd = PyLong_AsLong(tmp);
2014 Py_DECREF(tmp);
2015 if (fd < 0 && PyErr_Occurred())
2016 return NULL;
2017 tty = fd == fileno(stdin) && isatty(fd);
2018 }
2019 if (tty) {
Victor Stinner3466bde2016-09-05 18:16:01 -07002020 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
Martin Panterc9a6ab52015-10-10 01:25:38 +00002021 if (tmp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 PyErr_Clear();
Martin Panterc9a6ab52015-10-10 01:25:38 +00002023 tty = 0;
2024 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 else {
2026 fd = PyLong_AsLong(tmp);
2027 Py_DECREF(tmp);
2028 if (fd < 0 && PyErr_Occurred())
2029 return NULL;
2030 tty = fd == fileno(stdout) && isatty(fd);
2031 }
2032 }
Guido van Rossumeba76962007-05-27 09:13:28 +00002033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 /* If we're interactive, use (GNU) readline */
2035 if (tty) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002036 PyObject *po = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002037 const char *promptstr;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002038 char *s = NULL;
2039 PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
2040 PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002041 const char *stdin_encoding_str, *stdin_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 PyObject *result;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002043 size_t len;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00002044
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002045 /* stdin is a text stream, so it must have an encoding. */
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002046 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002047 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002048 if (!stdin_encoding || !stdin_errors ||
2049 !PyUnicode_Check(stdin_encoding) ||
2050 !PyUnicode_Check(stdin_errors)) {
2051 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002052 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002053 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002054 stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2055 stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002056 if (!stdin_encoding_str || !stdin_errors_str)
2057 goto _readline_errors;
Victor Stinner3466bde2016-09-05 18:16:01 -07002058 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 if (tmp == NULL)
2060 PyErr_Clear();
2061 else
2062 Py_DECREF(tmp);
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002063 if (prompt != NULL) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002064 /* We have a prompt, encode it as stdout would */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002065 const char *stdout_encoding_str, *stdout_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 PyObject *stringpo;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002067 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002068 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002069 if (!stdout_encoding || !stdout_errors ||
2070 !PyUnicode_Check(stdout_encoding) ||
2071 !PyUnicode_Check(stdout_errors)) {
2072 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002073 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002074 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002075 stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2076 stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002077 if (!stdout_encoding_str || !stdout_errors_str)
2078 goto _readline_errors;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002079 stringpo = PyObject_Str(prompt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002080 if (stringpo == NULL)
2081 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 po = PyUnicode_AsEncodedString(stringpo,
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002083 stdout_encoding_str, stdout_errors_str);
2084 Py_CLEAR(stdout_encoding);
2085 Py_CLEAR(stdout_errors);
2086 Py_CLEAR(stringpo);
2087 if (po == NULL)
2088 goto _readline_errors;
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03002089 assert(PyBytes_Check(po));
2090 promptstr = PyBytes_AS_STRING(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 }
2092 else {
2093 po = NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002094 promptstr = "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002096 s = PyOS_Readline(stdin, stdout, promptstr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 if (s == NULL) {
Richard Oudkerk614c5782013-04-03 13:44:50 +01002098 PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 if (!PyErr_Occurred())
2100 PyErr_SetNone(PyExc_KeyboardInterrupt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002101 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002103
2104 len = strlen(s);
2105 if (len == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 PyErr_SetNone(PyExc_EOFError);
2107 result = NULL;
2108 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002109 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 if (len > PY_SSIZE_T_MAX) {
2111 PyErr_SetString(PyExc_OverflowError,
2112 "input: input too long");
2113 result = NULL;
2114 }
2115 else {
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002116 len--; /* strip trailing '\n' */
2117 if (len != 0 && s[len-1] == '\r')
2118 len--; /* strip trailing '\r' */
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002119 result = PyUnicode_Decode(s, len, stdin_encoding_str,
2120 stdin_errors_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 }
2122 }
2123 Py_DECREF(stdin_encoding);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002124 Py_DECREF(stdin_errors);
2125 Py_XDECREF(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 PyMem_FREE(s);
2127 return result;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002128
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002129 _readline_errors:
2130 Py_XDECREF(stdin_encoding);
2131 Py_XDECREF(stdout_encoding);
2132 Py_XDECREF(stdin_errors);
2133 Py_XDECREF(stdout_errors);
2134 Py_XDECREF(po);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002135 if (tty)
2136 return NULL;
2137
2138 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 }
Guido van Rossumeba76962007-05-27 09:13:28 +00002140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 /* Fallback if we're not interactive */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002142 if (prompt != NULL) {
2143 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 return NULL;
2145 }
Victor Stinner3466bde2016-09-05 18:16:01 -07002146 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 if (tmp == NULL)
2148 PyErr_Clear();
2149 else
2150 Py_DECREF(tmp);
2151 return PyFile_GetLine(fin, -1);
Guido van Rossuma88a0332007-02-26 16:59:55 +00002152}
2153
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002154
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002155/*[clinic input]
2156repr as builtin_repr
2157
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002158 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002159 /
2160
2161Return the canonical string representation of the object.
2162
2163For many object types, including most builtins, eval(repr(obj)) == obj.
2164[clinic start generated code]*/
2165
Guido van Rossum79f25d91997-04-29 20:08:16 +00002166static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002167builtin_repr(PyObject *module, PyObject *obj)
2168/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
Guido van Rossumc89705d1992-11-26 08:54:07 +00002169{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002170 return PyObject_Repr(obj);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002171}
2172
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002173
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002174/*[clinic input]
2175round as builtin_round
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002176
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002177 number: object
2178 ndigits: object = NULL
2179
2180Round a number to a given precision in decimal digits.
2181
2182The return value is an integer if ndigits is omitted or None. Otherwise
2183the return value has the same type as the number. ndigits may be negative.
2184[clinic start generated code]*/
2185
2186static PyObject *
2187builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
2188/*[clinic end generated code: output=ff0d9dd176c02ede input=854bc3a217530c3d]*/
2189{
2190 PyObject *round, *result;
Alex Martelliae211f92007-08-22 23:21:33 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 if (Py_TYPE(number)->tp_dict == NULL) {
2193 if (PyType_Ready(Py_TYPE(number)) < 0)
2194 return NULL;
2195 }
Guido van Rossum15d3d042007-08-24 02:02:45 +00002196
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002197 round = _PyObject_LookupSpecial(number, &PyId___round__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 if (round == NULL) {
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002199 if (!PyErr_Occurred())
2200 PyErr_Format(PyExc_TypeError,
2201 "type %.100s doesn't define __round__ method",
2202 Py_TYPE(number)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 return NULL;
2204 }
Alex Martelliae211f92007-08-22 23:21:33 +00002205
Raymond Hettingerf0f1c232016-09-03 01:55:11 -07002206 if (ndigits == NULL || ndigits == Py_None)
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002207 result = _PyObject_CallNoArg(round);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 else
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002209 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002210 Py_DECREF(round);
2211 return result;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002212}
2213
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002214
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002215/*AC: we need to keep the kwds dict intact to easily call into the
2216 * list.sort method, which isn't currently supported in AC. So we just use
2217 * the initially generated signature with a custom implementation.
2218 */
2219/* [disabled clinic input]
2220sorted as builtin_sorted
2221
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002222 iterable as seq: object
2223 key as keyfunc: object = None
2224 reverse: object = False
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002225
2226Return a new list containing all items from the iterable in ascending order.
2227
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002228A custom key function can be supplied to customize the sort order, and the
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002229reverse flag can be set to request the result in descending order.
2230[end disabled clinic input]*/
2231
2232PyDoc_STRVAR(builtin_sorted__doc__,
Serhiy Storchaka3a104252017-01-23 12:29:47 +02002233"sorted($module, iterable, /, *, key=None, reverse=False)\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002234"--\n"
2235"\n"
2236"Return a new list containing all items from the iterable in ascending order.\n"
2237"\n"
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002238"A custom key function can be supplied to customize the sort order, and the\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002239"reverse flag can be set to request the result in descending order.");
2240
2241#define BUILTIN_SORTED_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002242 {"sorted", (PyCFunction)builtin_sorted, METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002243
Raymond Hettinger64958a12003-12-17 20:43:33 +00002244static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002245builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Raymond Hettinger64958a12003-12-17 20:43:33 +00002246{
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002247 PyObject *newlist, *v, *seq, *callable;
Victor Stinner5a60eca2017-01-17 15:17:49 +01002248
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002249 /* Keyword arguments are passed through list.sort() which will check
2250 them. */
2251 if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 newlist = PySequence_List(seq);
2255 if (newlist == NULL)
2256 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002257
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002258 callable = _PyObject_GetAttrId(newlist, &PyId_sort);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 if (callable == NULL) {
2260 Py_DECREF(newlist);
2261 return NULL;
2262 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002263
Serhiy Storchaka299dc232017-01-20 08:35:18 +02002264 assert(nargs >= 1);
Victor Stinner5a60eca2017-01-17 15:17:49 +01002265 v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 Py_DECREF(callable);
2267 if (v == NULL) {
2268 Py_DECREF(newlist);
2269 return NULL;
2270 }
2271 Py_DECREF(v);
2272 return newlist;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002273}
2274
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002275
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002276/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002277static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002278builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 PyObject *v = NULL;
2281 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2284 return NULL;
2285 if (v == NULL) {
2286 d = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01002287 if (d == NULL)
2288 return NULL;
2289 Py_INCREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 }
2291 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002292 d = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (d == NULL) {
2294 PyErr_SetString(PyExc_TypeError,
2295 "vars() argument must have __dict__ attribute");
2296 return NULL;
2297 }
2298 }
2299 return d;
Guido van Rossum2d951851994-08-29 12:52:16 +00002300}
2301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002302PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002303"vars([object]) -> dictionary\n\
2304\n\
2305Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002306With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002307
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002308
2309/*[clinic input]
2310sum as builtin_sum
2311
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002312 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002313 start: object(c_default="NULL") = 0
2314 /
2315
2316Return the sum of a 'start' value (default: 0) plus an iterable of numbers
2317
2318When the iterable is empty, return the start value.
2319This function is intended specifically for use with numeric values and may
2320reject non-numeric types.
2321[clinic start generated code]*/
2322
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002323static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002324builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
2325/*[clinic end generated code: output=df758cec7d1d302f input=3b5b7a9d7611c73a]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002326{
2327 PyObject *result = start;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 PyObject *temp, *item, *iter;
Alex Martellia70b1912003-04-22 08:12:33 +00002329
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002330 iter = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 if (iter == NULL)
2332 return NULL;
Alex Martellia70b1912003-04-22 08:12:33 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 if (result == NULL) {
2335 result = PyLong_FromLong(0);
2336 if (result == NULL) {
2337 Py_DECREF(iter);
2338 return NULL;
2339 }
2340 } else {
2341 /* reject string values for 'start' parameter */
2342 if (PyUnicode_Check(result)) {
2343 PyErr_SetString(PyExc_TypeError,
2344 "sum() can't sum strings [use ''.join(seq) instead]");
2345 Py_DECREF(iter);
2346 return NULL;
2347 }
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002348 if (PyBytes_Check(result)) {
2349 PyErr_SetString(PyExc_TypeError,
2350 "sum() can't sum bytes [use b''.join(seq) instead]");
Benjamin Peterson405f32c2011-07-29 22:43:45 -05002351 Py_DECREF(iter);
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002352 return NULL;
2353 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (PyByteArray_Check(result)) {
2355 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson4f921c22011-07-29 14:24:29 -05002356 "sum() can't sum bytearray [use b''.join(seq) instead]");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 Py_DECREF(iter);
2358 return NULL;
2359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 Py_INCREF(result);
2361 }
Alex Martellia70b1912003-04-22 08:12:33 +00002362
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002363#ifndef SLOW_SUM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2365 Assumes all inputs are the same type. If the assumption fails, default
2366 to the more general routine.
2367 */
2368 if (PyLong_CheckExact(result)) {
2369 int overflow;
2370 long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2371 /* If this already overflowed, don't even enter the loop. */
2372 if (overflow == 0) {
2373 Py_DECREF(result);
2374 result = NULL;
2375 }
2376 while(result == NULL) {
2377 item = PyIter_Next(iter);
2378 if (item == NULL) {
2379 Py_DECREF(iter);
2380 if (PyErr_Occurred())
2381 return NULL;
2382 return PyLong_FromLong(i_result);
2383 }
2384 if (PyLong_CheckExact(item)) {
2385 long b = PyLong_AsLongAndOverflow(item, &overflow);
2386 long x = i_result + b;
2387 if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
2388 i_result = x;
2389 Py_DECREF(item);
2390 continue;
2391 }
2392 }
2393 /* Either overflowed or is not an int. Restore real objects and process normally */
2394 result = PyLong_FromLong(i_result);
Christian Heimes704e2d32013-07-26 22:49:26 +02002395 if (result == NULL) {
2396 Py_DECREF(item);
2397 Py_DECREF(iter);
2398 return NULL;
2399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 temp = PyNumber_Add(result, item);
2401 Py_DECREF(result);
2402 Py_DECREF(item);
2403 result = temp;
2404 if (result == NULL) {
2405 Py_DECREF(iter);
2406 return NULL;
2407 }
2408 }
2409 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 if (PyFloat_CheckExact(result)) {
2412 double f_result = PyFloat_AS_DOUBLE(result);
2413 Py_DECREF(result);
2414 result = NULL;
2415 while(result == NULL) {
2416 item = PyIter_Next(iter);
2417 if (item == NULL) {
2418 Py_DECREF(iter);
2419 if (PyErr_Occurred())
2420 return NULL;
2421 return PyFloat_FromDouble(f_result);
2422 }
2423 if (PyFloat_CheckExact(item)) {
2424 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2425 f_result += PyFloat_AS_DOUBLE(item);
2426 PyFPE_END_PROTECT(f_result)
2427 Py_DECREF(item);
2428 continue;
2429 }
2430 if (PyLong_CheckExact(item)) {
2431 long value;
2432 int overflow;
2433 value = PyLong_AsLongAndOverflow(item, &overflow);
2434 if (!overflow) {
2435 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2436 f_result += (double)value;
2437 PyFPE_END_PROTECT(f_result)
2438 Py_DECREF(item);
2439 continue;
2440 }
2441 }
2442 result = PyFloat_FromDouble(f_result);
2443 temp = PyNumber_Add(result, item);
2444 Py_DECREF(result);
2445 Py_DECREF(item);
2446 result = temp;
2447 if (result == NULL) {
2448 Py_DECREF(iter);
2449 return NULL;
2450 }
2451 }
2452 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002453#endif
2454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 for(;;) {
2456 item = PyIter_Next(iter);
2457 if (item == NULL) {
2458 /* error, or end-of-sequence */
2459 if (PyErr_Occurred()) {
2460 Py_DECREF(result);
2461 result = NULL;
2462 }
2463 break;
2464 }
2465 /* It's tempting to use PyNumber_InPlaceAdd instead of
2466 PyNumber_Add here, to avoid quadratic running time
2467 when doing 'sum(list_of_lists, [])'. However, this
2468 would produce a change in behaviour: a snippet like
Mark Dickinson9acadc52009-10-26 14:19:42 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 empty = []
2471 sum([[x] for x in range(10)], empty)
Mark Dickinson9acadc52009-10-26 14:19:42 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 would change the value of empty. */
2474 temp = PyNumber_Add(result, item);
2475 Py_DECREF(result);
2476 Py_DECREF(item);
2477 result = temp;
2478 if (result == NULL)
2479 break;
2480 }
2481 Py_DECREF(iter);
2482 return result;
Alex Martellia70b1912003-04-22 08:12:33 +00002483}
2484
Alex Martellia70b1912003-04-22 08:12:33 +00002485
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002486/*[clinic input]
2487isinstance as builtin_isinstance
2488
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002489 obj: object
2490 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002491 /
2492
2493Return whether an object is an instance of a class or of a subclass thereof.
2494
2495A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
2496check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
2497or ...`` etc.
2498[clinic start generated code]*/
2499
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002500static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002501builtin_isinstance_impl(PyObject *module, PyObject *obj,
Larry Hastings89964c42015-04-14 18:07:59 -04002502 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002503/*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002506
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002507 retval = PyObject_IsInstance(obj, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 if (retval < 0)
2509 return NULL;
2510 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002511}
2512
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002513
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002514/*[clinic input]
2515issubclass as builtin_issubclass
2516
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002517 cls: object
2518 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002519 /
2520
2521Return whether 'cls' is a derived from another class or is the same class.
2522
2523A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
2524check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
2525or ...`` etc.
2526[clinic start generated code]*/
2527
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002528static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002529builtin_issubclass_impl(PyObject *module, PyObject *cls,
Larry Hastings89964c42015-04-14 18:07:59 -04002530 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002531/*[clinic end generated code: output=358412410cd7a250 input=af5f35e9ceaddaf6]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002534
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002535 retval = PyObject_IsSubclass(cls, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 if (retval < 0)
2537 return NULL;
2538 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002539}
2540
2541
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002542typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 PyObject_HEAD
2544 Py_ssize_t tuplesize;
2545 PyObject *ittuple; /* tuple of iterators */
2546 PyObject *result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002547} zipobject;
2548
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002549static PyObject *
2550zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Barry Warsawbd599b52000-08-03 15:45:29 +00002551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 zipobject *lz;
2553 Py_ssize_t i;
2554 PyObject *ittuple; /* tuple of iterators */
2555 PyObject *result;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002556 Py_ssize_t tuplesize;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002557
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002558 if (type == &PyZip_Type && !_PyArg_NoKeywords("zip", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 return NULL;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 /* args must be a tuple */
2562 assert(PyTuple_Check(args));
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002563 tuplesize = PyTuple_GET_SIZE(args);
Barry Warsawbd599b52000-08-03 15:45:29 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 /* obtain iterators */
2566 ittuple = PyTuple_New(tuplesize);
2567 if (ittuple == NULL)
2568 return NULL;
2569 for (i=0; i < tuplesize; ++i) {
2570 PyObject *item = PyTuple_GET_ITEM(args, i);
2571 PyObject *it = PyObject_GetIter(item);
2572 if (it == NULL) {
2573 if (PyErr_ExceptionMatches(PyExc_TypeError))
2574 PyErr_Format(PyExc_TypeError,
2575 "zip argument #%zd must support iteration",
2576 i+1);
2577 Py_DECREF(ittuple);
2578 return NULL;
2579 }
2580 PyTuple_SET_ITEM(ittuple, i, it);
2581 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 /* create a result holder */
2584 result = PyTuple_New(tuplesize);
2585 if (result == NULL) {
2586 Py_DECREF(ittuple);
2587 return NULL;
2588 }
2589 for (i=0 ; i < tuplesize ; i++) {
2590 Py_INCREF(Py_None);
2591 PyTuple_SET_ITEM(result, i, Py_None);
2592 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 /* create zipobject structure */
2595 lz = (zipobject *)type->tp_alloc(type, 0);
2596 if (lz == NULL) {
2597 Py_DECREF(ittuple);
2598 Py_DECREF(result);
2599 return NULL;
2600 }
2601 lz->ittuple = ittuple;
2602 lz->tuplesize = tuplesize;
2603 lz->result = result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 return (PyObject *)lz;
Barry Warsawbd599b52000-08-03 15:45:29 +00002606}
2607
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002608static void
2609zip_dealloc(zipobject *lz)
2610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 PyObject_GC_UnTrack(lz);
2612 Py_XDECREF(lz->ittuple);
2613 Py_XDECREF(lz->result);
2614 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002615}
2616
2617static int
2618zip_traverse(zipobject *lz, visitproc visit, void *arg)
2619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 Py_VISIT(lz->ittuple);
2621 Py_VISIT(lz->result);
2622 return 0;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002623}
2624
2625static PyObject *
2626zip_next(zipobject *lz)
2627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 Py_ssize_t i;
2629 Py_ssize_t tuplesize = lz->tuplesize;
2630 PyObject *result = lz->result;
2631 PyObject *it;
2632 PyObject *item;
2633 PyObject *olditem;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 if (tuplesize == 0)
2636 return NULL;
2637 if (Py_REFCNT(result) == 1) {
2638 Py_INCREF(result);
2639 for (i=0 ; i < tuplesize ; i++) {
2640 it = PyTuple_GET_ITEM(lz->ittuple, i);
2641 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002642 if (item == NULL) {
2643 Py_DECREF(result);
2644 return NULL;
2645 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 olditem = PyTuple_GET_ITEM(result, i);
2647 PyTuple_SET_ITEM(result, i, item);
2648 Py_DECREF(olditem);
2649 }
2650 } else {
2651 result = PyTuple_New(tuplesize);
2652 if (result == NULL)
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002653 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 for (i=0 ; i < tuplesize ; i++) {
2655 it = PyTuple_GET_ITEM(lz->ittuple, i);
2656 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002657 if (item == NULL) {
2658 Py_DECREF(result);
2659 return NULL;
2660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 PyTuple_SET_ITEM(result, i, item);
2662 }
2663 }
2664 return result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002665}
Barry Warsawbd599b52000-08-03 15:45:29 +00002666
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002667static PyObject *
2668zip_reduce(zipobject *lz)
2669{
2670 /* Just recreate the zip with the internal iterator tuple */
2671 return Py_BuildValue("OO", Py_TYPE(lz), lz->ittuple);
2672}
2673
2674static PyMethodDef zip_methods[] = {
2675 {"__reduce__", (PyCFunction)zip_reduce, METH_NOARGS, reduce_doc},
2676 {NULL, NULL} /* sentinel */
2677};
2678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002679PyDoc_STRVAR(zip_doc,
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002680"zip(iter1 [,iter2 [...]]) --> zip object\n\
Barry Warsawbd599b52000-08-03 15:45:29 +00002681\n\
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002682Return a zip object whose .__next__() method returns a tuple where\n\
2683the i-th element comes from the i-th iterable argument. The .__next__()\n\
2684method continues until the shortest iterable in the argument sequence\n\
Georg Brandlced51db2008-12-04 18:28:38 +00002685is exhausted and then it raises StopIteration.");
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002686
2687PyTypeObject PyZip_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2689 "zip", /* tp_name */
2690 sizeof(zipobject), /* tp_basicsize */
2691 0, /* tp_itemsize */
2692 /* methods */
2693 (destructor)zip_dealloc, /* tp_dealloc */
2694 0, /* tp_print */
2695 0, /* tp_getattr */
2696 0, /* tp_setattr */
2697 0, /* tp_reserved */
2698 0, /* tp_repr */
2699 0, /* tp_as_number */
2700 0, /* tp_as_sequence */
2701 0, /* tp_as_mapping */
2702 0, /* tp_hash */
2703 0, /* tp_call */
2704 0, /* tp_str */
2705 PyObject_GenericGetAttr, /* tp_getattro */
2706 0, /* tp_setattro */
2707 0, /* tp_as_buffer */
2708 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2709 Py_TPFLAGS_BASETYPE, /* tp_flags */
2710 zip_doc, /* tp_doc */
2711 (traverseproc)zip_traverse, /* tp_traverse */
2712 0, /* tp_clear */
2713 0, /* tp_richcompare */
2714 0, /* tp_weaklistoffset */
2715 PyObject_SelfIter, /* tp_iter */
2716 (iternextfunc)zip_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002717 zip_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 0, /* tp_members */
2719 0, /* tp_getset */
2720 0, /* tp_base */
2721 0, /* tp_dict */
2722 0, /* tp_descr_get */
2723 0, /* tp_descr_set */
2724 0, /* tp_dictoffset */
2725 0, /* tp_init */
2726 PyType_GenericAlloc, /* tp_alloc */
2727 zip_new, /* tp_new */
2728 PyObject_GC_Del, /* tp_free */
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002729};
Barry Warsawbd599b52000-08-03 15:45:29 +00002730
2731
Guido van Rossum79f25d91997-04-29 20:08:16 +00002732static PyMethodDef builtin_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 {"__build_class__", (PyCFunction)builtin___build_class__,
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002734 METH_FASTCALL | METH_KEYWORDS, build_class_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002736 BUILTIN_ABS_METHODDEF
2737 BUILTIN_ALL_METHODDEF
2738 BUILTIN_ANY_METHODDEF
2739 BUILTIN_ASCII_METHODDEF
2740 BUILTIN_BIN_METHODDEF
Barry Warsaw36c1d1f2017-10-05 12:11:18 -04002741 {"breakpoint", (PyCFunction)builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002742 BUILTIN_CALLABLE_METHODDEF
2743 BUILTIN_CHR_METHODDEF
2744 BUILTIN_COMPILE_METHODDEF
2745 BUILTIN_DELATTR_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 {"dir", builtin_dir, METH_VARARGS, dir_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002747 BUILTIN_DIVMOD_METHODDEF
2748 BUILTIN_EVAL_METHODDEF
2749 BUILTIN_EXEC_METHODDEF
2750 BUILTIN_FORMAT_METHODDEF
Victor Stinner84b388b2017-01-17 03:52:27 +01002751 {"getattr", (PyCFunction)builtin_getattr, METH_FASTCALL, getattr_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002752 BUILTIN_GLOBALS_METHODDEF
2753 BUILTIN_HASATTR_METHODDEF
2754 BUILTIN_HASH_METHODDEF
2755 BUILTIN_HEX_METHODDEF
2756 BUILTIN_ID_METHODDEF
2757 BUILTIN_INPUT_METHODDEF
2758 BUILTIN_ISINSTANCE_METHODDEF
2759 BUILTIN_ISSUBCLASS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 {"iter", builtin_iter, METH_VARARGS, iter_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002761 BUILTIN_LEN_METHODDEF
2762 BUILTIN_LOCALS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2764 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Victor Stinnerfda6d0a2017-01-17 04:09:14 +01002765 {"next", (PyCFunction)builtin_next, METH_FASTCALL, next_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002766 BUILTIN_OCT_METHODDEF
2767 BUILTIN_ORD_METHODDEF
2768 BUILTIN_POW_METHODDEF
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002769 {"print", (PyCFunction)builtin_print, METH_FASTCALL | METH_KEYWORDS, print_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002770 BUILTIN_REPR_METHODDEF
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002771 BUILTIN_ROUND_METHODDEF
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002772 BUILTIN_SETATTR_METHODDEF
2773 BUILTIN_SORTED_METHODDEF
2774 BUILTIN_SUM_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 {"vars", builtin_vars, METH_VARARGS, vars_doc},
2776 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002777};
2778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002779PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002780"Built-in functions, exceptions, and other objects.\n\
2781\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002782Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002783
Martin v. Löwis1a214512008-06-11 05:26:20 +00002784static struct PyModuleDef builtinsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 PyModuleDef_HEAD_INIT,
2786 "builtins",
2787 builtin_doc,
2788 -1, /* multiple "initialization" just copies the module dict. */
2789 builtin_methods,
2790 NULL,
2791 NULL,
2792 NULL,
2793 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002794};
2795
2796
Guido van Rossum25ce5661997-08-02 03:10:38 +00002797PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002798_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 PyObject *mod, *dict, *debug;
Benjamin Peterson42124a72012-10-30 23:41:54 -04002801
2802 if (PyType_Ready(&PyFilter_Type) < 0 ||
2803 PyType_Ready(&PyMap_Type) < 0 ||
2804 PyType_Ready(&PyZip_Type) < 0)
2805 return NULL;
2806
Eric Snowd393c1b2017-09-14 12:18:12 -06002807 mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 if (mod == NULL)
2809 return NULL;
2810 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002811
Tim Peters7571a0f2003-03-23 17:52:28 +00002812#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 /* "builtins" exposes a number of statically allocated objects
2814 * that, before this code was added in 2.3, never showed up in
2815 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2816 * result, programs leaking references to None and False (etc)
2817 * couldn't be diagnosed by examining sys.getobjects(0).
2818 */
Tim Peters7571a0f2003-03-23 17:52:28 +00002819#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2820#else
2821#define ADD_TO_ALL(OBJECT) (void)0
2822#endif
2823
Tim Peters4b7625e2001-09-13 21:37:17 +00002824#define SETBUILTIN(NAME, OBJECT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2826 return NULL; \
2827 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 SETBUILTIN("None", Py_None);
2830 SETBUILTIN("Ellipsis", Py_Ellipsis);
2831 SETBUILTIN("NotImplemented", Py_NotImplemented);
2832 SETBUILTIN("False", Py_False);
2833 SETBUILTIN("True", Py_True);
2834 SETBUILTIN("bool", &PyBool_Type);
2835 SETBUILTIN("memoryview", &PyMemoryView_Type);
2836 SETBUILTIN("bytearray", &PyByteArray_Type);
2837 SETBUILTIN("bytes", &PyBytes_Type);
2838 SETBUILTIN("classmethod", &PyClassMethod_Type);
2839 SETBUILTIN("complex", &PyComplex_Type);
2840 SETBUILTIN("dict", &PyDict_Type);
2841 SETBUILTIN("enumerate", &PyEnum_Type);
2842 SETBUILTIN("filter", &PyFilter_Type);
2843 SETBUILTIN("float", &PyFloat_Type);
2844 SETBUILTIN("frozenset", &PyFrozenSet_Type);
2845 SETBUILTIN("property", &PyProperty_Type);
2846 SETBUILTIN("int", &PyLong_Type);
2847 SETBUILTIN("list", &PyList_Type);
2848 SETBUILTIN("map", &PyMap_Type);
2849 SETBUILTIN("object", &PyBaseObject_Type);
2850 SETBUILTIN("range", &PyRange_Type);
2851 SETBUILTIN("reversed", &PyReversed_Type);
2852 SETBUILTIN("set", &PySet_Type);
2853 SETBUILTIN("slice", &PySlice_Type);
2854 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2855 SETBUILTIN("str", &PyUnicode_Type);
2856 SETBUILTIN("super", &PySuper_Type);
2857 SETBUILTIN("tuple", &PyTuple_Type);
2858 SETBUILTIN("type", &PyType_Type);
2859 SETBUILTIN("zip", &PyZip_Type);
2860 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
2861 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002862 Py_DECREF(debug);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 return NULL;
2864 }
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002865 Py_DECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002868#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002869#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002870}