blob: 94f290853ccf9f937742420e2404eef1070a3d0a [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 Stinner94540602017-12-16 04:54:22 +010032/* UTF-8 mode (PEP 540): if equals to 1, use the UTF-8 encoding, and change
33 stdin and stdout error handler to "surrogateescape". It is equal to
34 -1 by default: unknown, will be set by Py_Main() */
35int Py_UTF8Mode = -1;
Mark Hammondef8b6542001-05-13 08:04:26 +000036
Victor Stinnerbd303c12013-11-07 23:07:29 +010037_Py_IDENTIFIER(__builtins__);
38_Py_IDENTIFIER(__dict__);
39_Py_IDENTIFIER(__prepare__);
40_Py_IDENTIFIER(__round__);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010041_Py_IDENTIFIER(__mro_entries__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010042_Py_IDENTIFIER(encoding);
43_Py_IDENTIFIER(errors);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020044_Py_IDENTIFIER(fileno);
45_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010046_Py_IDENTIFIER(metaclass);
47_Py_IDENTIFIER(sort);
48_Py_IDENTIFIER(stdin);
49_Py_IDENTIFIER(stdout);
50_Py_IDENTIFIER(stderr);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020051
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030052#include "clinic/bltinmodule.c.h"
53
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010054static PyObject*
Victor Stinner05d68a82018-01-18 11:15:25 +010055update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010056{
Victor Stinner05d68a82018-01-18 11:15:25 +010057 Py_ssize_t i, j;
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010058 PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
59 PyObject *stack[1] = {bases};
60 assert(PyTuple_Check(bases));
61
62 for (i = 0; i < nargs; i++) {
63 base = args[i];
64 if (PyType_Check(base)) {
65 if (new_bases) {
66 /* If we already have made a replacement, then we append every normal base,
67 otherwise just skip it. */
68 if (PyList_Append(new_bases, base) < 0) {
69 goto error;
70 }
71 }
72 continue;
73 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +020074 if (_PyObject_LookupAttrId(base, &PyId___mro_entries__, &meth) < 0) {
75 goto error;
76 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010077 if (!meth) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +010078 if (new_bases) {
79 if (PyList_Append(new_bases, base) < 0) {
80 goto error;
81 }
82 }
83 continue;
84 }
85 new_base = _PyObject_FastCall(meth, stack, 1);
86 Py_DECREF(meth);
87 if (!new_base) {
88 goto error;
89 }
90 if (!PyTuple_Check(new_base)) {
91 PyErr_SetString(PyExc_TypeError,
92 "__mro_entries__ must return a tuple");
93 Py_DECREF(new_base);
94 goto error;
95 }
96 if (!new_bases) {
97 /* If this is a first successful replacement, create new_bases list and
98 copy previously encountered bases. */
99 if (!(new_bases = PyList_New(i))) {
100 goto error;
101 }
102 for (j = 0; j < i; j++) {
103 base = args[j];
104 PyList_SET_ITEM(new_bases, j, base);
105 Py_INCREF(base);
106 }
107 }
108 j = PyList_GET_SIZE(new_bases);
109 if (PyList_SetSlice(new_bases, j, j, new_base) < 0) {
110 goto error;
111 }
112 Py_DECREF(new_base);
113 }
114 if (!new_bases) {
115 return bases;
116 }
117 result = PyList_AsTuple(new_bases);
118 Py_DECREF(new_bases);
119 return result;
120
121error:
122 Py_XDECREF(new_bases);
123 return NULL;
124}
125
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000126/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000127static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200128builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
Victor Stinner773dc6d2017-01-16 23:46:26 +0100129 PyObject *kwnames)
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000130{
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100131 PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *orig_bases;
Nick Coghlan19d24672016-12-05 16:47:55 +1000132 PyObject *cls = NULL, *cell = NULL;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100133 int isclass = 0; /* initialize to prevent gcc warning */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 if (nargs < 2) {
136 PyErr_SetString(PyExc_TypeError,
137 "__build_class__: not enough arguments");
138 return NULL;
139 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100140 func = args[0]; /* Better be callable */
Benjamin Petersone8e14592013-05-16 14:37:25 -0500141 if (!PyFunction_Check(func)) {
142 PyErr_SetString(PyExc_TypeError,
Zachary Ware9b338722014-08-05 14:01:10 -0500143 "__build_class__: func must be a function");
Benjamin Petersone8e14592013-05-16 14:37:25 -0500144 return NULL;
145 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100146 name = args[1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (!PyUnicode_Check(name)) {
148 PyErr_SetString(PyExc_TypeError,
149 "__build_class__: name is not a string");
150 return NULL;
151 }
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100152 orig_bases = _PyStack_AsTupleSlice(args, nargs, 2, nargs);
153 if (orig_bases == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000155
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100156 bases = update_bases(orig_bases, args + 2, nargs - 2);
157 if (bases == NULL) {
158 Py_DECREF(orig_bases);
159 return NULL;
160 }
161
Victor Stinner773dc6d2017-01-16 23:46:26 +0100162 if (kwnames == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 meta = NULL;
164 mkw = NULL;
165 }
166 else {
Victor Stinner773dc6d2017-01-16 23:46:26 +0100167 mkw = _PyStack_AsDict(args + nargs, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (mkw == NULL) {
169 Py_DECREF(bases);
170 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000171 }
Victor Stinner773dc6d2017-01-16 23:46:26 +0100172
Victor Stinnerae9f1612013-11-06 22:46:51 +0100173 meta = _PyDict_GetItemId(mkw, &PyId_metaclass);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (meta != NULL) {
175 Py_INCREF(meta);
Victor Stinnerae9f1612013-11-06 22:46:51 +0100176 if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 Py_DECREF(meta);
178 Py_DECREF(mkw);
179 Py_DECREF(bases);
180 return NULL;
181 }
Nick Coghlande31b192011-10-23 22:04:16 +1000182 /* metaclass is explicitly given, check if it's indeed a class */
183 isclass = PyType_Check(meta);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 }
185 }
186 if (meta == NULL) {
Nick Coghlande31b192011-10-23 22:04:16 +1000187 /* if there are no bases, use type: */
188 if (PyTuple_GET_SIZE(bases) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 meta = (PyObject *) (&PyType_Type);
Nick Coghlande31b192011-10-23 22:04:16 +1000190 }
191 /* else get the type of the first base */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 else {
193 PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
194 meta = (PyObject *) (base0->ob_type);
195 }
196 Py_INCREF(meta);
Nick Coghlande31b192011-10-23 22:04:16 +1000197 isclass = 1; /* meta is really a class */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 }
Nick Coghlan9715d262011-10-23 22:36:42 +1000199
Nick Coghlande31b192011-10-23 22:04:16 +1000200 if (isclass) {
201 /* meta is really a class, so check for a more derived
202 metaclass, or possible metaclass conflicts: */
203 winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
204 bases);
205 if (winner == NULL) {
206 Py_DECREF(meta);
207 Py_XDECREF(mkw);
208 Py_DECREF(bases);
209 return NULL;
210 }
211 if (winner != meta) {
212 Py_DECREF(meta);
213 meta = winner;
214 Py_INCREF(meta);
215 }
216 }
217 /* else: meta is not a class, so we cannot do the metaclass
218 calculation, so we will use the explicitly given object as it is */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200219 if (_PyObject_LookupAttrId(meta, &PyId___prepare__, &prep) < 0) {
220 ns = NULL;
221 }
222 else if (prep == NULL) {
223 ns = PyDict_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 }
225 else {
Victor Stinner463b86a2016-08-22 23:33:13 +0200226 PyObject *pargs[2] = {name, bases};
227 ns = _PyObject_FastCallDict(prep, pargs, 2, mkw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 Py_DECREF(prep);
229 }
230 if (ns == NULL) {
231 Py_DECREF(meta);
232 Py_XDECREF(mkw);
233 Py_DECREF(bases);
234 return NULL;
235 }
Oren Milman5837d042017-09-27 17:04:37 +0300236 if (!PyMapping_Check(ns)) {
237 PyErr_Format(PyExc_TypeError,
238 "%.200s.__prepare__() must return a mapping, not %.200s",
239 isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
240 Py_TYPE(ns)->tp_name);
241 goto error;
242 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000243 cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500244 NULL, 0, NULL, 0, NULL, 0, NULL,
245 PyFunction_GET_CLOSURE(func));
Nick Coghlan19d24672016-12-05 16:47:55 +1000246 if (cell != NULL) {
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100247 if (bases != orig_bases) {
248 if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
249 goto error;
250 }
251 }
Victor Stinnerd1c2a8e2016-08-23 01:34:35 +0200252 PyObject *margs[3] = {name, bases, ns};
253 cls = _PyObject_FastCallDict(meta, margs, 3, mkw);
Nick Coghlan19d24672016-12-05 16:47:55 +1000254 if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
255 PyObject *cell_cls = PyCell_GET(cell);
256 if (cell_cls != cls) {
257 /* TODO: In 3.7, DeprecationWarning will become RuntimeError.
258 * At that point, cell_error won't be needed.
259 */
260 int cell_error;
261 if (cell_cls == NULL) {
262 const char *msg =
263 "__class__ not set defining %.200R as %.200R. "
264 "Was __classcell__ propagated to type.__new__?";
265 cell_error = PyErr_WarnFormat(
266 PyExc_DeprecationWarning, 1, msg, name, cls);
267 } else {
268 const char *msg =
269 "__class__ set to %.200R defining %.200R as %.200R";
270 PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
271 cell_error = 1;
272 }
273 if (cell_error) {
274 Py_DECREF(cls);
275 cls = NULL;
276 goto error;
277 } else {
278 /* Fill in the cell, since type.__new__ didn't do it */
279 PyCell_Set(cell, cls);
280 }
281 }
282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 }
Nick Coghlan19d24672016-12-05 16:47:55 +1000284error:
285 Py_XDECREF(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_DECREF(ns);
287 Py_DECREF(meta);
288 Py_XDECREF(mkw);
289 Py_DECREF(bases);
Ivan Levkivskyi2b5fd1e2017-12-14 23:32:56 +0100290 if (bases != orig_bases) {
291 Py_DECREF(orig_bases);
292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 return cls;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000294}
295
296PyDoc_STRVAR(build_class_doc,
297"__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
298\n\
299Internal helper function used by the class statement.");
300
301static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000302builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
305 "level", 0};
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400306 PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
Brett Cannonfd074152012-04-14 14:10:13 -0400307 int level = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000308
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400309 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 kwlist, &name, &globals, &locals, &fromlist, &level))
311 return NULL;
Victor Stinnerfe93faf2011-03-14 15:54:52 -0400312 return PyImport_ImportModuleLevelObject(name, globals, locals,
313 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000314}
315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000316PyDoc_STRVAR(import_doc,
Brett Cannoncb4996a2012-08-06 16:34:44 -0400317"__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000318\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000319Import a module. Because this function is meant for use by the Python\n\
oldk461d2252018-02-02 12:20:00 +0800320interpreter and not for general use, it is better to use\n\
Brett Cannon5305a992010-09-27 21:08:38 +0000321importlib.import_module() to programmatically import a module.\n\
322\n\
323The globals argument is only used to determine the context;\n\
324they are not modified. The locals argument is unused. The fromlist\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000325should be a list of names to emulate ``from name import ...'', or an\n\
326empty list to emulate ``import name''.\n\
327When importing a module from a package, note that __import__('A.B', ...)\n\
328returns package A when fromlist is empty, but its submodule B when\n\
oldk461d2252018-02-02 12:20:00 +0800329fromlist is not empty. The level argument is used to determine whether to\n\
330perform absolute or relative imports: 0 is absolute, while a positive number\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000332
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000333
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000334/*[clinic input]
335abs as builtin_abs
336
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300337 x: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000338 /
339
340Return the absolute value of the argument.
341[clinic start generated code]*/
342
Guido van Rossum79f25d91997-04-29 20:08:16 +0000343static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300344builtin_abs(PyObject *module, PyObject *x)
345/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000346{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000347 return PyNumber_Absolute(x);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000348}
349
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000350/*[clinic input]
351all as builtin_all
352
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300353 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000354 /
355
356Return True if bool(x) is True for all values x in the iterable.
357
358If the iterable is empty, return True.
359[clinic start generated code]*/
360
Raymond Hettinger96229b12005-03-11 06:49:40 +0000361static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300362builtin_all(PyObject *module, PyObject *iterable)
363/*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 PyObject *it, *item;
366 PyObject *(*iternext)(PyObject *);
367 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000368
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000369 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 if (it == NULL)
371 return NULL;
372 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 for (;;) {
375 item = iternext(it);
376 if (item == NULL)
377 break;
378 cmp = PyObject_IsTrue(item);
379 Py_DECREF(item);
380 if (cmp < 0) {
381 Py_DECREF(it);
382 return NULL;
383 }
384 if (cmp == 0) {
385 Py_DECREF(it);
386 Py_RETURN_FALSE;
387 }
388 }
389 Py_DECREF(it);
390 if (PyErr_Occurred()) {
391 if (PyErr_ExceptionMatches(PyExc_StopIteration))
392 PyErr_Clear();
393 else
394 return NULL;
395 }
396 Py_RETURN_TRUE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000397}
398
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000399/*[clinic input]
400any as builtin_any
401
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300402 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000403 /
404
405Return True if bool(x) is True for any x in the iterable.
406
407If the iterable is empty, return False.
408[clinic start generated code]*/
409
Raymond Hettinger96229b12005-03-11 06:49:40 +0000410static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300411builtin_any(PyObject *module, PyObject *iterable)
412/*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/
Raymond Hettinger96229b12005-03-11 06:49:40 +0000413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 PyObject *it, *item;
415 PyObject *(*iternext)(PyObject *);
416 int cmp;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000417
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000418 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (it == NULL)
420 return NULL;
421 iternext = *Py_TYPE(it)->tp_iternext;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 for (;;) {
424 item = iternext(it);
425 if (item == NULL)
426 break;
427 cmp = PyObject_IsTrue(item);
428 Py_DECREF(item);
429 if (cmp < 0) {
430 Py_DECREF(it);
431 return NULL;
432 }
Raymond Hettinger5098b582015-10-09 00:42:47 -0400433 if (cmp > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 Py_DECREF(it);
435 Py_RETURN_TRUE;
436 }
437 }
438 Py_DECREF(it);
439 if (PyErr_Occurred()) {
440 if (PyErr_ExceptionMatches(PyExc_StopIteration))
441 PyErr_Clear();
442 else
443 return NULL;
444 }
445 Py_RETURN_FALSE;
Raymond Hettinger96229b12005-03-11 06:49:40 +0000446}
447
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000448/*[clinic input]
449ascii as builtin_ascii
450
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300451 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000452 /
453
454Return an ASCII-only representation of an object.
455
456As repr(), return a string containing a printable representation of an
457object, but escape the non-ASCII characters in the string returned by
458repr() using \\x, \\u or \\U escapes. This generates a string similar
459to that returned by repr() in Python 2.
460[clinic start generated code]*/
461
Georg Brandl559e5d72008-06-11 18:37:52 +0000462static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300463builtin_ascii(PyObject *module, PyObject *obj)
464/*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
Georg Brandl559e5d72008-06-11 18:37:52 +0000465{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000466 return PyObject_ASCII(obj);
Georg Brandl559e5d72008-06-11 18:37:52 +0000467}
468
Georg Brandl559e5d72008-06-11 18:37:52 +0000469
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000470/*[clinic input]
471bin as builtin_bin
472
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300473 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000474 /
475
476Return the binary representation of an integer.
477
478 >>> bin(2796202)
479 '0b1010101010101010101010'
480[clinic start generated code]*/
481
Guido van Rossum79f25d91997-04-29 20:08:16 +0000482static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300483builtin_bin(PyObject *module, PyObject *number)
484/*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000485{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000486 return PyNumber_ToBase(number, 2);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000487}
488
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000489
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000490/*[clinic input]
491callable as builtin_callable
492
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300493 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000494 /
495
496Return whether the object is callable (i.e., some kind of function).
497
498Note that classes are callable, as are instances of classes with a
499__call__() method.
500[clinic start generated code]*/
501
Antoine Pitroue71362d2010-11-27 22:00:11 +0000502static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300503builtin_callable(PyObject *module, PyObject *obj)
504/*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
Antoine Pitroue71362d2010-11-27 22:00:11 +0000505{
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000506 return PyBool_FromLong((long)PyCallable_Check(obj));
Antoine Pitroue71362d2010-11-27 22:00:11 +0000507}
508
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400509static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200510builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
Barry Warsaw36c1d1f2017-10-05 12:11:18 -0400511{
512 PyObject *hook = PySys_GetObject("breakpointhook");
513
514 if (hook == NULL) {
515 PyErr_SetString(PyExc_RuntimeError, "lost sys.breakpointhook");
516 return NULL;
517 }
518 Py_INCREF(hook);
519 PyObject *retval = _PyObject_FastCallKeywords(hook, args, nargs, keywords);
520 Py_DECREF(hook);
521 return retval;
522}
523
524PyDoc_STRVAR(breakpoint_doc,
525"breakpoint(*args, **kws)\n\
526\n\
527Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept\n\
528whatever arguments are passed.\n\
529\n\
530By default, this drops you into the pdb debugger.");
Antoine Pitroue71362d2010-11-27 22:00:11 +0000531
Raymond Hettinger17301e92008-03-13 00:19:26 +0000532typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyObject_HEAD
534 PyObject *func;
535 PyObject *it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000536} filterobject;
537
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000538static PyObject *
Raymond Hettinger17301e92008-03-13 00:19:26 +0000539filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyObject *func, *seq;
542 PyObject *it;
543 filterobject *lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000544
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +0300545 if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
549 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* Get iterator. */
552 it = PyObject_GetIter(seq);
553 if (it == NULL)
554 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* create filterobject structure */
557 lz = (filterobject *)type->tp_alloc(type, 0);
558 if (lz == NULL) {
559 Py_DECREF(it);
560 return NULL;
561 }
562 Py_INCREF(func);
563 lz->func = func;
564 lz->it = it;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 return (PyObject *)lz;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000567}
568
569static void
570filter_dealloc(filterobject *lz)
571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 PyObject_GC_UnTrack(lz);
573 Py_XDECREF(lz->func);
574 Py_XDECREF(lz->it);
575 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger17301e92008-03-13 00:19:26 +0000576}
577
578static int
579filter_traverse(filterobject *lz, visitproc visit, void *arg)
580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 Py_VISIT(lz->it);
582 Py_VISIT(lz->func);
583 return 0;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000584}
585
586static PyObject *
587filter_next(filterobject *lz)
588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 PyObject *item;
590 PyObject *it = lz->it;
591 long ok;
592 PyObject *(*iternext)(PyObject *);
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400593 int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 iternext = *Py_TYPE(it)->tp_iternext;
596 for (;;) {
597 item = iternext(it);
598 if (item == NULL)
599 return NULL;
Raymond Hettinger17301e92008-03-13 00:19:26 +0000600
Raymond Hettingerbd5f0e82015-10-09 01:34:08 -0400601 if (checktrue) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 ok = PyObject_IsTrue(item);
603 } else {
604 PyObject *good;
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100605 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (good == NULL) {
607 Py_DECREF(item);
608 return NULL;
609 }
610 ok = PyObject_IsTrue(good);
611 Py_DECREF(good);
612 }
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200613 if (ok > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 return item;
615 Py_DECREF(item);
Antoine Pitrou6f430e42012-08-15 23:18:25 +0200616 if (ok < 0)
617 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 }
Guido van Rossum12d12c51993-10-26 17:58:25 +0000619}
620
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000621static PyObject *
622filter_reduce(filterobject *lz)
623{
624 return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
625}
626
627PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
628
629static PyMethodDef filter_methods[] = {
630 {"__reduce__", (PyCFunction)filter_reduce, METH_NOARGS, reduce_doc},
631 {NULL, NULL} /* sentinel */
632};
633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000634PyDoc_STRVAR(filter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +0000635"filter(function or None, iterable) --> filter object\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000636\n\
Georg Brandld11ae5d2008-05-16 13:27:32 +0000637Return an iterator yielding those items of iterable for which function(item)\n\
Raymond Hettinger17301e92008-03-13 00:19:26 +0000638is true. If function is None, return the items that are true.");
639
640PyTypeObject PyFilter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 PyVarObject_HEAD_INIT(&PyType_Type, 0)
642 "filter", /* tp_name */
643 sizeof(filterobject), /* tp_basicsize */
644 0, /* tp_itemsize */
645 /* methods */
646 (destructor)filter_dealloc, /* tp_dealloc */
647 0, /* tp_print */
648 0, /* tp_getattr */
649 0, /* tp_setattr */
650 0, /* tp_reserved */
651 0, /* tp_repr */
652 0, /* tp_as_number */
653 0, /* tp_as_sequence */
654 0, /* tp_as_mapping */
655 0, /* tp_hash */
656 0, /* tp_call */
657 0, /* tp_str */
658 PyObject_GenericGetAttr, /* tp_getattro */
659 0, /* tp_setattro */
660 0, /* tp_as_buffer */
661 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
662 Py_TPFLAGS_BASETYPE, /* tp_flags */
663 filter_doc, /* tp_doc */
664 (traverseproc)filter_traverse, /* tp_traverse */
665 0, /* tp_clear */
666 0, /* tp_richcompare */
667 0, /* tp_weaklistoffset */
668 PyObject_SelfIter, /* tp_iter */
669 (iternextfunc)filter_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000670 filter_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 0, /* tp_members */
672 0, /* tp_getset */
673 0, /* tp_base */
674 0, /* tp_dict */
675 0, /* tp_descr_get */
676 0, /* tp_descr_set */
677 0, /* tp_dictoffset */
678 0, /* tp_init */
679 PyType_GenericAlloc, /* tp_alloc */
680 filter_new, /* tp_new */
681 PyObject_GC_Del, /* tp_free */
Raymond Hettinger17301e92008-03-13 00:19:26 +0000682};
683
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000684
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000685/*[clinic input]
686format as builtin_format
687
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300688 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000689 format_spec: unicode(c_default="NULL") = ''
690 /
691
692Return value.__format__(format_spec)
693
Amit Kumar2e6bb442017-05-29 06:32:26 +0530694format_spec defaults to the empty string.
695See the Format Specification Mini-Language section of help('FORMATTING') for
696details.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000697[clinic start generated code]*/
698
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000699static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300700builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
Amit Kumar2e6bb442017-05-29 06:32:26 +0530701/*[clinic end generated code: output=2f40bdfa4954b077 input=88339c93ea522b33]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000702{
Eric Smith8fd3eba2008-02-17 19:48:00 +0000703 return PyObject_Format(value, format_spec);
Eric Smith8c663262007-08-25 02:26:07 +0000704}
705
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000706/*[clinic input]
707chr as builtin_chr
708
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300709 i: int
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000710 /
711
712Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
713[clinic start generated code]*/
714
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300716builtin_chr_impl(PyObject *module, int i)
717/*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000718{
719 return PyUnicode_FromOrdinal(i);
720}
Guido van Rossum09095f32000-03-10 23:00:52 +0000721
722
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200723static const char *
Martin Pantereeb896c2015-11-07 02:32:21 +0000724source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000725{
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200726 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 Py_ssize_t size;
Martin Pantereeb896c2015-11-07 02:32:21 +0000728 Py_buffer view;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000729
Martin Pantereeb896c2015-11-07 02:32:21 +0000730 *cmd_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 if (PyUnicode_Check(cmd)) {
732 cf->cf_flags |= PyCF_IGNORE_COOKIE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200733 str = PyUnicode_AsUTF8AndSize(cmd, &size);
734 if (str == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 return NULL;
736 }
Martin Pantereeb896c2015-11-07 02:32:21 +0000737 else if (PyBytes_Check(cmd)) {
738 str = PyBytes_AS_STRING(cmd);
739 size = PyBytes_GET_SIZE(cmd);
740 }
741 else if (PyByteArray_Check(cmd)) {
742 str = PyByteArray_AS_STRING(cmd);
743 size = PyByteArray_GET_SIZE(cmd);
744 }
745 else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
746 /* Copy to NUL-terminated buffer. */
747 *cmd_copy = PyBytes_FromStringAndSize(
748 (const char *)view.buf, view.len);
749 PyBuffer_Release(&view);
750 if (*cmd_copy == NULL) {
751 return NULL;
752 }
753 str = PyBytes_AS_STRING(*cmd_copy);
754 size = PyBytes_GET_SIZE(*cmd_copy);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200755 }
756 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PyErr_Format(PyExc_TypeError,
758 "%s() arg 1 must be a %s object",
759 funcname, what);
760 return NULL;
761 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200762
Serhiy Storchaka3dd3e262015-02-03 01:25:42 +0200763 if (strlen(str) != (size_t)size) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300764 PyErr_SetString(PyExc_ValueError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 "source code string cannot contain null bytes");
Martin Pantereeb896c2015-11-07 02:32:21 +0000766 Py_CLEAR(*cmd_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 return NULL;
768 }
769 return str;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000770}
771
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000772/*[clinic input]
773compile as builtin_compile
774
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300775 source: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000776 filename: object(converter="PyUnicode_FSDecoder")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300777 mode: str
778 flags: int = 0
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200779 dont_inherit: bool(accept={int}) = False
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300780 optimize: int = -1
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000781
782Compile source into a code object that can be executed by exec() or eval().
783
784The source code may represent a Python module, statement or expression.
785The filename will be used for run-time error messages.
786The mode must be 'exec' to compile a module, 'single' to compile a
787single (interactive) statement, or 'eval' to compile an expression.
788The flags argument, if present, controls which future statements influence
789the compilation of the code.
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300790The dont_inherit argument, if true, stops the compilation inheriting
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000791the effects of any future statements in effect in the code calling
Serhiy Storchaka8b2e8b62015-05-30 11:30:39 +0300792compile; if absent or false these statements do influence the compilation,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000793in addition to any features explicitly specified.
794[clinic start generated code]*/
795
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000796static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300797builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
798 const char *mode, int flags, int dont_inherit,
799 int optimize)
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200800/*[clinic end generated code: output=1fa176e33452bb63 input=0ff726f595eb9fcd]*/
Guido van Rossum5b722181993-03-30 17:46:03 +0000801{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000802 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200803 const char *str;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000804 int compile_mode = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 int is_ast;
806 PyCompilerFlags cf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000808 PyObject *result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000809
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000810 cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
Tim Peters6cd6a822001-08-17 22:11:27 +0000811
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000812 if (flags &
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
814 {
815 PyErr_SetString(PyExc_ValueError,
816 "compile(): unrecognised flags");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000817 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 }
819 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
Tim Peters6cd6a822001-08-17 22:11:27 +0000820
Georg Brandl8334fd92010-12-04 10:26:46 +0000821 if (optimize < -1 || optimize > 2) {
822 PyErr_SetString(PyExc_ValueError,
823 "compile(): invalid optimize value");
824 goto error;
825 }
826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 if (!dont_inherit) {
828 PyEval_MergeCompilerFlags(&cf);
829 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000830
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000831 if (strcmp(mode, "exec") == 0)
832 compile_mode = 0;
833 else if (strcmp(mode, "eval") == 0)
834 compile_mode = 1;
835 else if (strcmp(mode, "single") == 0)
836 compile_mode = 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 else {
838 PyErr_SetString(PyExc_ValueError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000839 "compile() mode must be 'exec', 'eval' or 'single'");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000840 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 }
Neal Norwitzdb4115f2008-03-31 04:20:05 +0000842
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000843 is_ast = PyAST_Check(source);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 if (is_ast == -1)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000845 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 if (is_ast) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000847 if (flags & PyCF_ONLY_AST) {
848 Py_INCREF(source);
849 result = source;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 }
851 else {
852 PyArena *arena;
853 mod_ty mod;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 arena = PyArena_New();
Stefan Krah07795df2012-08-20 17:19:50 +0200856 if (arena == NULL)
857 goto error;
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000858 mod = PyAST_obj2mod(source, arena, compile_mode);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (mod == NULL) {
860 PyArena_Free(arena);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000861 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 }
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500863 if (!PyAST_Validate(mod)) {
864 PyArena_Free(arena);
865 goto error;
866 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200867 result = (PyObject*)PyAST_CompileObject(mod, filename,
868 &cf, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 PyArena_Free(arena);
870 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000871 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000873
Martin Panter61d6e4a2015-11-07 02:56:11 +0000874 str = source_as_string(source, "compile", "string, bytes or AST", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (str == NULL)
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000876 goto error;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000877
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000878 result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
Martin Panter61d6e4a2015-11-07 02:56:11 +0000879 Py_XDECREF(source_copy);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000880 goto finally;
881
882error:
883 result = NULL;
884finally:
Victor Stinner14e461d2013-08-26 22:28:21 +0200885 Py_DECREF(filename);
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000886 return result;
Guido van Rossum5b722181993-03-30 17:46:03 +0000887}
888
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000889/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000891builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
896 return NULL;
897 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000898}
899
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000900PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000901"dir([object]) -> list of strings\n"
902"\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000903"If called without an argument, return the names in the current scope.\n"
904"Else, return an alphabetized list of names comprising (some of) the attributes\n"
905"of the given object, and of attributes reachable from it.\n"
906"If the object supplies a method named __dir__, it will be used; otherwise\n"
907"the default dir() logic is used and returns:\n"
908" for a module object: the module's attributes.\n"
909" for a class object: its attributes, and recursively the attributes\n"
910" of its bases.\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000911" for any other object: its attributes, its class's attributes, and\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000912" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000913
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000914/*[clinic input]
915divmod as builtin_divmod
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000916
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300917 x: object
918 y: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000919 /
920
Zachary Ware7f227d92016-04-28 14:39:50 -0500921Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000922[clinic start generated code]*/
923
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000924static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300925builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
926/*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000927{
928 return PyNumber_Divmod(x, y);
929}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000930
931
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000932/*[clinic input]
933eval as builtin_eval
934
Serhiy Storchaka7e810a62015-05-30 11:09:35 +0300935 source: object
936 globals: object = None
937 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000938 /
939
940Evaluate the given source in the context of globals and locals.
941
942The source may be a string representing a Python expression
943or a code object as returned by compile().
944The globals must be a dictionary and locals can be any mapping,
945defaulting to the current globals and locals.
946If only globals is given, locals defaults to it.
947[clinic start generated code]*/
948
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000949static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300950builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -0400951 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300952/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000953{
Martin Panter61d6e4a2015-11-07 02:56:11 +0000954 PyObject *result, *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200955 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (locals != Py_None && !PyMapping_Check(locals)) {
959 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
960 return NULL;
961 }
962 if (globals != Py_None && !PyDict_Check(globals)) {
963 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
964 "globals must be a real dict; try eval(expr, {}, mapping)"
965 : "globals must be a dict");
966 return NULL;
967 }
968 if (globals == Py_None) {
969 globals = PyEval_GetGlobals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100970 if (locals == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100972 if (locals == NULL)
973 return NULL;
974 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 }
976 else if (locals == Py_None)
977 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 if (globals == NULL || locals == NULL) {
980 PyErr_SetString(PyExc_TypeError,
981 "eval must be given globals and locals "
982 "when called without a frame");
983 return NULL;
984 }
Georg Brandl77c85e62005-09-15 10:46:13 +0000985
Victor Stinnerb44562b2013-11-06 19:03:11 +0100986 if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
987 if (_PyDict_SetItemId(globals, &PyId___builtins__,
988 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 return NULL;
990 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000991
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000992 if (PyCode_Check(source)) {
993 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyErr_SetString(PyExc_TypeError,
995 "code object passed to eval() may not contain free variables");
996 return NULL;
997 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +1000998 return PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 }
Tim Peters9fa96be2001-08-17 23:04:59 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Martin Panter61d6e4a2015-11-07 02:56:11 +00001002 str = source_as_string(source, "eval", "string, bytes or code", &cf, &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 if (str == NULL)
1004 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 while (*str == ' ' || *str == '\t')
1007 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 (void)PyEval_MergeCompilerFlags(&cf);
1010 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Martin Panter61d6e4a2015-11-07 02:56:11 +00001011 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001013}
1014
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001015/*[clinic input]
1016exec as builtin_exec
1017
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001018 source: object
1019 globals: object = None
1020 locals: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001021 /
1022
1023Execute the given source in the context of globals and locals.
1024
1025The source may be a string representing one or more Python statements
1026or a code object as returned by compile().
1027The globals must be a dictionary and locals can be any mapping,
1028defaulting to the current globals and locals.
1029If only globals is given, locals defaults to it.
1030[clinic start generated code]*/
1031
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001032static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001033builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
Larry Hastings89964c42015-04-14 18:07:59 -04001034 PyObject *locals)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001035/*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
Georg Brandl7cae87c2006-09-06 06:51:57 +00001036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 PyObject *v;
Georg Brandl2cabc562008-08-28 07:57:16 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 if (globals == Py_None) {
1040 globals = PyEval_GetGlobals();
1041 if (locals == Py_None) {
1042 locals = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01001043 if (locals == NULL)
1044 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 }
1046 if (!globals || !locals) {
1047 PyErr_SetString(PyExc_SystemError,
1048 "globals and locals cannot be NULL");
1049 return NULL;
1050 }
1051 }
1052 else if (locals == Py_None)
1053 locals = globals;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (!PyDict_Check(globals)) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001056 PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 globals->ob_type->tp_name);
1058 return NULL;
1059 }
1060 if (!PyMapping_Check(locals)) {
1061 PyErr_Format(PyExc_TypeError,
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001062 "locals must be a mapping or None, not %.100s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 locals->ob_type->tp_name);
1064 return NULL;
1065 }
Victor Stinnerb44562b2013-11-06 19:03:11 +01001066 if (_PyDict_GetItemId(globals, &PyId___builtins__) == NULL) {
1067 if (_PyDict_SetItemId(globals, &PyId___builtins__,
1068 PyEval_GetBuiltins()) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 return NULL;
1070 }
1071
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001072 if (PyCode_Check(source)) {
1073 if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 PyErr_SetString(PyExc_TypeError,
1075 "code object passed to exec() may not "
1076 "contain free variables");
1077 return NULL;
1078 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001079 v = PyEval_EvalCode(source, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 }
1081 else {
Martin Panter61d6e4a2015-11-07 02:56:11 +00001082 PyObject *source_copy;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001083 const char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 PyCompilerFlags cf;
1085 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001086 str = source_as_string(source, "exec",
Martin Panter61d6e4a2015-11-07 02:56:11 +00001087 "string, bytes or code", &cf,
1088 &source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 if (str == NULL)
1090 return NULL;
1091 if (PyEval_MergeCompilerFlags(&cf))
1092 v = PyRun_StringFlags(str, Py_file_input, globals,
1093 locals, &cf);
1094 else
1095 v = PyRun_String(str, Py_file_input, globals, locals);
Martin Panter61d6e4a2015-11-07 02:56:11 +00001096 Py_XDECREF(source_copy);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 }
1098 if (v == NULL)
1099 return NULL;
1100 Py_DECREF(v);
1101 Py_RETURN_NONE;
Georg Brandl7cae87c2006-09-06 06:51:57 +00001102}
1103
Georg Brandl7cae87c2006-09-06 06:51:57 +00001104
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001105/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001107builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Guido van Rossum33894be1992-01-27 16:53:09 +00001108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 PyObject *v, *result, *dflt = NULL;
1110 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001111
Sylvain96c7c062017-06-15 17:05:23 +02001112 if (!_PyArg_UnpackStack(args, nargs, "getattr", 2, 3, &v, &name, &dflt))
1113 return NULL;
1114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (!PyUnicode_Check(name)) {
1116 PyErr_SetString(PyExc_TypeError,
1117 "getattr(): attribute name must be string");
1118 return NULL;
1119 }
INADA Naoki378edee2018-01-16 20:52:41 +09001120 if (dflt != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001121 if (_PyObject_LookupAttr(v, name, &result) == 0) {
INADA Naoki378edee2018-01-16 20:52:41 +09001122 Py_INCREF(dflt);
1123 return dflt;
1124 }
1125 }
1126 else {
1127 result = PyObject_GetAttr(v, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 }
1129 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001130}
1131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001132PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +00001133"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001134\n\
Guido van Rossum950ff291998-06-29 13:38:57 +00001135Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
1136When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001137exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001138
1139
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001140/*[clinic input]
1141globals as builtin_globals
1142
1143Return the dictionary containing the current scope's global variables.
1144
1145NOTE: Updates to this dictionary *will* affect name lookups in the current
1146global scope and vice-versa.
1147[clinic start generated code]*/
1148
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001150builtin_globals_impl(PyObject *module)
1151/*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 d = PyEval_GetGlobals();
1156 Py_XINCREF(d);
1157 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001158}
1159
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001160
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001161/*[clinic input]
1162hasattr as builtin_hasattr
1163
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001164 obj: object
1165 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001166 /
1167
1168Return whether the object has an attribute with the given name.
1169
1170This is done by calling getattr(obj, name) and catching AttributeError.
1171[clinic start generated code]*/
1172
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001173static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001174builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1175/*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001176{
1177 PyObject *v;
1178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (!PyUnicode_Check(name)) {
1180 PyErr_SetString(PyExc_TypeError,
1181 "hasattr(): attribute name must be string");
1182 return NULL;
1183 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001184 if (_PyObject_LookupAttr(obj, name, &v) < 0) {
Benjamin Peterson17689992010-08-24 03:26:23 +00001185 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001187 if (v == NULL) {
1188 Py_RETURN_FALSE;
1189 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 Py_DECREF(v);
Benjamin Peterson17689992010-08-24 03:26:23 +00001191 Py_RETURN_TRUE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001192}
1193
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001194
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001195/* AC: gdb's integration with CPython relies on builtin_id having
1196 * the *exact* parameter names of "self" and "v", so we ensure we
1197 * preserve those name rather than using the AC defaults.
1198 */
1199/*[clinic input]
1200id as builtin_id
1201
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001202 self: self(type="PyModuleDef *")
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001203 obj as v: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001204 /
1205
1206Return the identity of an object.
1207
1208This is guaranteed to be unique among simultaneously existing objects.
1209(CPython uses the object's memory address.)
1210[clinic start generated code]*/
1211
Guido van Rossum79f25d91997-04-29 20:08:16 +00001212static PyObject *
Serhiy Storchaka24182a32016-05-05 16:21:35 +03001213builtin_id(PyModuleDef *self, PyObject *v)
1214/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
Guido van Rossum5b722181993-03-30 17:46:03 +00001215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +00001217}
1218
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001219
Raymond Hettingera6c60372008-03-13 01:26:19 +00001220/* map object ************************************************************/
1221
1222typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 PyObject_HEAD
1224 PyObject *iters;
1225 PyObject *func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001226} mapobject;
1227
Guido van Rossum79f25d91997-04-29 20:08:16 +00001228static PyObject *
Raymond Hettingera6c60372008-03-13 01:26:19 +00001229map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Guido van Rossum12d12c51993-10-26 17:58:25 +00001230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 PyObject *it, *iters, *func;
1232 mapobject *lz;
1233 Py_ssize_t numargs, i;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001234
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001235 if (type == &PyMap_Type && !_PyArg_NoKeywords("map", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 numargs = PyTuple_Size(args);
1239 if (numargs < 2) {
1240 PyErr_SetString(PyExc_TypeError,
1241 "map() must have at least two arguments.");
1242 return NULL;
1243 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 iters = PyTuple_New(numargs-1);
1246 if (iters == NULL)
1247 return NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 for (i=1 ; i<numargs ; i++) {
1250 /* Get iterator. */
1251 it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1252 if (it == NULL) {
1253 Py_DECREF(iters);
1254 return NULL;
1255 }
1256 PyTuple_SET_ITEM(iters, i-1, it);
1257 }
Raymond Hettingera6c60372008-03-13 01:26:19 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 /* create mapobject structure */
1260 lz = (mapobject *)type->tp_alloc(type, 0);
1261 if (lz == NULL) {
1262 Py_DECREF(iters);
1263 return NULL;
1264 }
1265 lz->iters = iters;
1266 func = PyTuple_GET_ITEM(args, 0);
1267 Py_INCREF(func);
1268 lz->func = func;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 return (PyObject *)lz;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001271}
1272
1273static void
1274map_dealloc(mapobject *lz)
1275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 PyObject_GC_UnTrack(lz);
1277 Py_XDECREF(lz->iters);
1278 Py_XDECREF(lz->func);
1279 Py_TYPE(lz)->tp_free(lz);
Raymond Hettingera6c60372008-03-13 01:26:19 +00001280}
1281
1282static int
1283map_traverse(mapobject *lz, visitproc visit, void *arg)
1284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 Py_VISIT(lz->iters);
1286 Py_VISIT(lz->func);
1287 return 0;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001288}
1289
1290static PyObject *
1291map_next(mapobject *lz)
1292{
Victor Stinnerbc08ab42016-12-15 12:40:53 +01001293 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001294 PyObject **stack;
1295 Py_ssize_t niters, nargs, i;
1296 PyObject *result = NULL;
Raymond Hettingera6c60372008-03-13 01:26:19 +00001297
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001298 niters = PyTuple_GET_SIZE(lz->iters);
1299 if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1300 stack = small_stack;
1301 }
1302 else {
1303 stack = PyMem_Malloc(niters * sizeof(stack[0]));
1304 if (stack == NULL) {
1305 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 return NULL;
1307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 }
Victor Stinnercdb5cee2016-08-24 01:45:13 +02001309
1310 nargs = 0;
1311 for (i=0; i < niters; i++) {
1312 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1313 PyObject *val = Py_TYPE(it)->tp_iternext(it);
1314 if (val == NULL) {
1315 goto exit;
1316 }
1317 stack[i] = val;
1318 nargs++;
1319 }
1320
1321 result = _PyObject_FastCall(lz->func, stack, nargs);
1322
1323exit:
1324 for (i=0; i < nargs; i++) {
1325 Py_DECREF(stack[i]);
1326 }
1327 if (stack != small_stack) {
1328 PyMem_Free(stack);
1329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +00001331}
1332
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001333static PyObject *
1334map_reduce(mapobject *lz)
1335{
1336 Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1337 PyObject *args = PyTuple_New(numargs+1);
1338 Py_ssize_t i;
1339 if (args == NULL)
1340 return NULL;
1341 Py_INCREF(lz->func);
1342 PyTuple_SET_ITEM(args, 0, lz->func);
1343 for (i = 0; i<numargs; i++){
1344 PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1345 Py_INCREF(it);
1346 PyTuple_SET_ITEM(args, i+1, it);
1347 }
1348
1349 return Py_BuildValue("ON", Py_TYPE(lz), args);
1350}
1351
1352static PyMethodDef map_methods[] = {
1353 {"__reduce__", (PyCFunction)map_reduce, METH_NOARGS, reduce_doc},
1354 {NULL, NULL} /* sentinel */
1355};
1356
1357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001358PyDoc_STRVAR(map_doc,
Raymond Hettingera6c60372008-03-13 01:26:19 +00001359"map(func, *iterables) --> map object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001360\n\
Raymond Hettingera6c60372008-03-13 01:26:19 +00001361Make an iterator that computes the function using arguments from\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362each of the iterables. Stops when the shortest iterable is exhausted.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001363
Raymond Hettingera6c60372008-03-13 01:26:19 +00001364PyTypeObject PyMap_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1366 "map", /* tp_name */
1367 sizeof(mapobject), /* tp_basicsize */
1368 0, /* tp_itemsize */
1369 /* methods */
1370 (destructor)map_dealloc, /* tp_dealloc */
1371 0, /* tp_print */
1372 0, /* tp_getattr */
1373 0, /* tp_setattr */
1374 0, /* tp_reserved */
1375 0, /* tp_repr */
1376 0, /* tp_as_number */
1377 0, /* tp_as_sequence */
1378 0, /* tp_as_mapping */
1379 0, /* tp_hash */
1380 0, /* tp_call */
1381 0, /* tp_str */
1382 PyObject_GenericGetAttr, /* tp_getattro */
1383 0, /* tp_setattro */
1384 0, /* tp_as_buffer */
1385 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1386 Py_TPFLAGS_BASETYPE, /* tp_flags */
1387 map_doc, /* tp_doc */
1388 (traverseproc)map_traverse, /* tp_traverse */
1389 0, /* tp_clear */
1390 0, /* tp_richcompare */
1391 0, /* tp_weaklistoffset */
1392 PyObject_SelfIter, /* tp_iter */
1393 (iternextfunc)map_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00001394 map_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 0, /* tp_members */
1396 0, /* tp_getset */
1397 0, /* tp_base */
1398 0, /* tp_dict */
1399 0, /* tp_descr_get */
1400 0, /* tp_descr_set */
1401 0, /* tp_dictoffset */
1402 0, /* tp_init */
1403 PyType_GenericAlloc, /* tp_alloc */
1404 map_new, /* tp_new */
1405 PyObject_GC_Del, /* tp_free */
Raymond Hettingera6c60372008-03-13 01:26:19 +00001406};
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001407
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001408
1409/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001410static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001411builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Georg Brandla18af4e2007-04-21 15:47:16 +00001412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 PyObject *it, *res;
1414 PyObject *def = NULL;
Georg Brandla18af4e2007-04-21 15:47:16 +00001415
Sylvain96c7c062017-06-15 17:05:23 +02001416 if (!_PyArg_UnpackStack(args, nargs, "next", 1, 2, &it, &def))
1417 return NULL;
1418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (!PyIter_Check(it)) {
1420 PyErr_Format(PyExc_TypeError,
Philip Jenvey50add042011-11-06 16:37:52 -08001421 "'%.200s' object is not an iterator",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 it->ob_type->tp_name);
1423 return NULL;
1424 }
1425
1426 res = (*it->ob_type->tp_iternext)(it);
1427 if (res != NULL) {
1428 return res;
1429 } else if (def != NULL) {
1430 if (PyErr_Occurred()) {
1431 if(!PyErr_ExceptionMatches(PyExc_StopIteration))
1432 return NULL;
1433 PyErr_Clear();
1434 }
1435 Py_INCREF(def);
1436 return def;
1437 } else if (PyErr_Occurred()) {
1438 return NULL;
1439 } else {
1440 PyErr_SetNone(PyExc_StopIteration);
1441 return NULL;
1442 }
Georg Brandla18af4e2007-04-21 15:47:16 +00001443}
1444
1445PyDoc_STRVAR(next_doc,
1446"next(iterator[, default])\n\
1447\n\
1448Return the next item from the iterator. If default is given and the iterator\n\
1449is exhausted, it is returned instead of raising StopIteration.");
1450
1451
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001452/*[clinic input]
1453setattr as builtin_setattr
1454
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001455 obj: object
1456 name: object
1457 value: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001458 /
1459
1460Sets the named attribute on the given object to the specified value.
1461
1462setattr(x, 'y', v) is equivalent to ``x.y = v''
1463[clinic start generated code]*/
1464
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001465static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001466builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
Larry Hastings89964c42015-04-14 18:07:59 -04001467 PyObject *value)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001468/*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001469{
1470 if (PyObject_SetAttr(obj, name, value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001472 Py_RETURN_NONE;
Guido van Rossum33894be1992-01-27 16:53:09 +00001473}
1474
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001475
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001476/*[clinic input]
1477delattr as builtin_delattr
1478
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001479 obj: object
1480 name: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001481 /
1482
1483Deletes the named attribute from the given object.
1484
1485delattr(x, 'y') is equivalent to ``del x.y''
1486[clinic start generated code]*/
1487
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001488static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001489builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
1490/*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001491{
1492 if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001494 Py_RETURN_NONE;
Guido van Rossum14144fc1994-08-29 12:53:40 +00001495}
1496
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001497
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001498/*[clinic input]
1499hash as builtin_hash
1500
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001501 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001502 /
1503
1504Return the hash value for the given object.
1505
1506Two objects that compare equal must also have the same hash value, but the
1507reverse is not necessarily true.
1508[clinic start generated code]*/
1509
Guido van Rossum79f25d91997-04-29 20:08:16 +00001510static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001511builtin_hash(PyObject *module, PyObject *obj)
1512/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001513{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001514 Py_hash_t x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001515
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001516 x = PyObject_Hash(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (x == -1)
1518 return NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001519 return PyLong_FromSsize_t(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001520}
1521
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001522
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001523/*[clinic input]
1524hex as builtin_hex
1525
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001526 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001527 /
1528
1529Return the hexadecimal representation of an integer.
1530
1531 >>> hex(12648430)
1532 '0xc0ffee'
1533[clinic start generated code]*/
1534
Guido van Rossum79f25d91997-04-29 20:08:16 +00001535static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001536builtin_hex(PyObject *module, PyObject *number)
1537/*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001538{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001539 return PyNumber_ToBase(number, 16);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001540}
1541
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001542
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001543/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001544static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001545builtin_iter(PyObject *self, PyObject *args)
1546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 PyObject *v, *w = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
1550 return NULL;
1551 if (w == NULL)
1552 return PyObject_GetIter(v);
1553 if (!PyCallable_Check(v)) {
1554 PyErr_SetString(PyExc_TypeError,
1555 "iter(v, w): v must be callable");
1556 return NULL;
1557 }
1558 return PyCallIter_New(v, w);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001559}
1560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561PyDoc_STRVAR(iter_doc,
Georg Brandld11ae5d2008-05-16 13:27:32 +00001562"iter(iterable) -> iterator\n\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001563iter(callable, sentinel) -> iterator\n\
1564\n\
1565Get an iterator from an object. In the first form, the argument must\n\
1566supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001567In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001568
1569
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001570/*[clinic input]
1571len as builtin_len
1572
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001573 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001574 /
1575
1576Return the number of items in a container.
1577[clinic start generated code]*/
1578
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001579static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001580builtin_len(PyObject *module, PyObject *obj)
1581/*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001584
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001585 res = PyObject_Size(obj);
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001586 if (res < 0) {
1587 assert(PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 return NULL;
Serhiy Storchaka813f9432017-04-16 09:21:44 +03001589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 return PyLong_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001591}
1592
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001593
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001594/*[clinic input]
1595locals as builtin_locals
1596
1597Return a dictionary containing the current scope's local variables.
1598
1599NOTE: Whether or not updates to this dictionary will affect name lookups in
1600the local scope and vice-versa is *implementation dependent* and not
1601covered by any backwards compatibility guarantees.
1602[clinic start generated code]*/
1603
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001604static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001605builtin_locals_impl(PyObject *module)
1606/*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
Guido van Rossum872537c1995-07-07 22:43:42 +00001607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 d = PyEval_GetLocals();
1611 Py_XINCREF(d);
1612 return d;
Guido van Rossum872537c1995-07-07 22:43:42 +00001613}
1614
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001615
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001617min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001620 PyObject *emptytuple, *defaultval = NULL;
1621 static char *kwlist[] = {"key", "default", NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 const char *name = op == Py_LT ? "min" : "max";
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001623 const int positional = PyTuple_Size(args) > 1;
1624 int ret;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001625
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001626 if (positional)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 v = args;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001628 else if (!PyArg_UnpackTuple(args, name, 1, 1, &v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +00001630
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001631 emptytuple = PyTuple_New(0);
1632 if (emptytuple == NULL)
1633 return NULL;
Oren Milman58cf7482017-08-21 20:19:07 +03001634 ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds,
1635 (op == Py_LT) ? "|$OO:min" : "|$OO:max",
1636 kwlist, &keyfunc, &defaultval);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001637 Py_DECREF(emptytuple);
1638 if (!ret)
1639 return NULL;
1640
1641 if (positional && defaultval != NULL) {
1642 PyErr_Format(PyExc_TypeError,
1643 "Cannot specify a default for %s() with multiple "
1644 "positional arguments", name);
1645 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 it = PyObject_GetIter(v);
1649 if (it == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 return NULL;
1651 }
Tim Petersc3074532001-05-03 07:00:32 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 maxitem = NULL; /* the result */
1654 maxval = NULL; /* the value associated with the result */
1655 while (( item = PyIter_Next(it) )) {
1656 /* get the value from the key function */
1657 if (keyfunc != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001658 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (val == NULL)
1660 goto Fail_it_item;
1661 }
1662 /* no key function; the value is the item */
1663 else {
1664 val = item;
1665 Py_INCREF(val);
1666 }
Tim Petersc3074532001-05-03 07:00:32 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 /* maximum value and item are unset; set them */
1669 if (maxval == NULL) {
1670 maxitem = item;
1671 maxval = val;
1672 }
1673 /* maximum value and item are set; update them as necessary */
1674 else {
1675 int cmp = PyObject_RichCompareBool(val, maxval, op);
1676 if (cmp < 0)
1677 goto Fail_it_item_and_val;
1678 else if (cmp > 0) {
1679 Py_DECREF(maxval);
1680 Py_DECREF(maxitem);
1681 maxval = val;
1682 maxitem = item;
1683 }
1684 else {
1685 Py_DECREF(item);
1686 Py_DECREF(val);
1687 }
1688 }
1689 }
1690 if (PyErr_Occurred())
1691 goto Fail_it;
1692 if (maxval == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 assert(maxitem == NULL);
Raymond Hettinger4d6018f2013-06-24 22:43:02 -07001694 if (defaultval != NULL) {
1695 Py_INCREF(defaultval);
1696 maxitem = defaultval;
1697 } else {
1698 PyErr_Format(PyExc_ValueError,
1699 "%s() arg is an empty sequence", name);
1700 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 }
1702 else
1703 Py_DECREF(maxval);
1704 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 return maxitem;
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001706
1707Fail_it_item_and_val:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 Py_DECREF(val);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001709Fail_it_item:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 Py_DECREF(item);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001711Fail_it:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 Py_XDECREF(maxval);
1713 Py_XDECREF(maxitem);
1714 Py_DECREF(it);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001716}
1717
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001718/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001719static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001720builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001723}
1724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001725PyDoc_STRVAR(min_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001726"min(iterable, *[, default=obj, key=func]) -> value\n\
1727min(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001728\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001729With a single iterable argument, return its smallest item. The\n\
1730default keyword-only argument specifies an object to return if\n\
1731the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001732With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001733
1734
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001735/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001736static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001737builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001740}
1741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001742PyDoc_STRVAR(max_doc,
Raymond Hettinger2a545822014-05-19 22:20:52 +01001743"max(iterable, *[, default=obj, key=func]) -> value\n\
1744max(arg1, arg2, *args, *[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001745\n\
Raymond Hettinger2a545822014-05-19 22:20:52 +01001746With a single iterable argument, return its biggest item. The\n\
1747default keyword-only argument specifies an object to return if\n\
1748the provided iterable is empty.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001750
1751
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001752/*[clinic input]
1753oct as builtin_oct
1754
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001755 number: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001756 /
1757
1758Return the octal representation of an integer.
1759
1760 >>> oct(342391)
1761 '0o1234567'
1762[clinic start generated code]*/
1763
Guido van Rossum79f25d91997-04-29 20:08:16 +00001764static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001765builtin_oct(PyObject *module, PyObject *number)
1766/*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
Guido van Rossum006bcd41991-10-24 14:54:44 +00001767{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001768 return PyNumber_ToBase(number, 8);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001769}
1770
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001771
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001772/*[clinic input]
1773ord as builtin_ord
1774
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001775 c: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001776 /
1777
1778Return the Unicode code point for a one-character string.
1779[clinic start generated code]*/
1780
Guido van Rossum79f25d91997-04-29 20:08:16 +00001781static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001782builtin_ord(PyObject *module, PyObject *c)
1783/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 long ord;
1786 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001787
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001788 if (PyBytes_Check(c)) {
1789 size = PyBytes_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001791 ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 return PyLong_FromLong(ord);
1793 }
1794 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001795 else if (PyUnicode_Check(c)) {
1796 if (PyUnicode_READY(c) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001797 return NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001798 size = PyUnicode_GET_LENGTH(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001800 ord = (long)PyUnicode_READ_CHAR(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 return PyLong_FromLong(ord);
1802 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001804 else if (PyByteArray_Check(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 /* XXX Hopefully this is temporary */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001806 size = PyByteArray_GET_SIZE(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 if (size == 1) {
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001808 ord = (long)((unsigned char)*PyByteArray_AS_STRING(c));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 return PyLong_FromLong(ord);
1810 }
1811 }
1812 else {
1813 PyErr_Format(PyExc_TypeError,
1814 "ord() expected string of length 1, but " \
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001815 "%.200s found", c->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 return NULL;
1817 }
Guido van Rossum09095f32000-03-10 23:00:52 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 PyErr_Format(PyExc_TypeError,
1820 "ord() expected a character, "
1821 "but string of length %zd found",
1822 size);
1823 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001824}
1825
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001826
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001827/*[clinic input]
1828pow as builtin_pow
1829
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03001830 x: object
1831 y: object
1832 z: object = None
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001833 /
1834
1835Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
1836
1837Some types, such as ints, are able to use a more efficient algorithm when
1838invoked using the three argument form.
1839[clinic start generated code]*/
1840
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001841static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001842builtin_pow_impl(PyObject *module, PyObject *x, PyObject *y, PyObject *z)
1843/*[clinic end generated code: output=50a14d5d130d404b input=653d57d38d41fc07]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001844{
1845 return PyNumber_Power(x, y, z);
1846}
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001847
1848
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001849/* AC: cannot convert yet, waiting for *args support */
Guido van Rossum34343512006-11-30 22:13:52 +00001850static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001851builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Guido van Rossum34343512006-11-30 22:13:52 +00001852{
INADA Naokibd584f12017-01-19 12:50:34 +01001853 static const char * const _keywords[] = {"sep", "end", "file", "flush", 0};
1854 static struct _PyArg_Parser _parser = {"|OOOO:print", _keywords, 0};
Georg Brandlbc3b6822012-01-13 19:41:25 +01001855 PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 int i, err;
Guido van Rossum34343512006-11-30 22:13:52 +00001857
INADA Naokibd584f12017-01-19 12:50:34 +01001858 if (kwnames != NULL &&
1859 !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser,
1860 &sep, &end, &file, &flush)) {
Georg Brandlbc3b6822012-01-13 19:41:25 +01001861 return NULL;
INADA Naokibd584f12017-01-19 12:50:34 +01001862 }
1863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 if (file == NULL || file == Py_None) {
Victor Stinnerbd303c12013-11-07 23:07:29 +01001865 file = _PySys_GetObjectId(&PyId_stdout);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001866 if (file == NULL) {
1867 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
1868 return NULL;
1869 }
1870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 /* sys.stdout may be None when FILE* stdout isn't connected */
1872 if (file == Py_None)
1873 Py_RETURN_NONE;
1874 }
Guido van Rossum34343512006-11-30 22:13:52 +00001875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 if (sep == Py_None) {
1877 sep = NULL;
1878 }
1879 else if (sep && !PyUnicode_Check(sep)) {
1880 PyErr_Format(PyExc_TypeError,
1881 "sep must be None or a string, not %.200s",
1882 sep->ob_type->tp_name);
1883 return NULL;
1884 }
1885 if (end == Py_None) {
1886 end = NULL;
1887 }
1888 else if (end && !PyUnicode_Check(end)) {
1889 PyErr_Format(PyExc_TypeError,
1890 "end must be None or a string, not %.200s",
1891 end->ob_type->tp_name);
1892 return NULL;
1893 }
Guido van Rossum34343512006-11-30 22:13:52 +00001894
INADA Naokibd584f12017-01-19 12:50:34 +01001895 for (i = 0; i < nargs; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 if (i > 0) {
1897 if (sep == NULL)
1898 err = PyFile_WriteString(" ", file);
1899 else
1900 err = PyFile_WriteObject(sep, file,
1901 Py_PRINT_RAW);
1902 if (err)
1903 return NULL;
1904 }
INADA Naokibd584f12017-01-19 12:50:34 +01001905 err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (err)
1907 return NULL;
1908 }
Guido van Rossum34343512006-11-30 22:13:52 +00001909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 if (end == NULL)
1911 err = PyFile_WriteString("\n", file);
1912 else
1913 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1914 if (err)
1915 return NULL;
Guido van Rossum34343512006-11-30 22:13:52 +00001916
Georg Brandlbc3b6822012-01-13 19:41:25 +01001917 if (flush != NULL) {
1918 PyObject *tmp;
1919 int do_flush = PyObject_IsTrue(flush);
1920 if (do_flush == -1)
1921 return NULL;
1922 else if (do_flush) {
Victor Stinner3466bde2016-09-05 18:16:01 -07001923 tmp = _PyObject_CallMethodId(file, &PyId_flush, NULL);
Georg Brandlbc3b6822012-01-13 19:41:25 +01001924 if (tmp == NULL)
1925 return NULL;
1926 else
1927 Py_DECREF(tmp);
1928 }
1929 }
1930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 Py_RETURN_NONE;
Guido van Rossum34343512006-11-30 22:13:52 +00001932}
1933
1934PyDoc_STRVAR(print_doc,
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001935"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\
Guido van Rossum34343512006-11-30 22:13:52 +00001936\n\
1937Prints the values to a stream, or to sys.stdout by default.\n\
1938Optional keyword arguments:\n\
Senthil Kumarane9175bd2012-08-10 13:53:45 -07001939file: a file-like object (stream); defaults to the current sys.stdout.\n\
1940sep: string inserted between values, default a space.\n\
1941end: string appended after the last value, default a newline.\n\
1942flush: whether to forcibly flush the stream.");
Guido van Rossum34343512006-11-30 22:13:52 +00001943
1944
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001945/*[clinic input]
1946input as builtin_input
1947
1948 prompt: object(c_default="NULL") = None
1949 /
1950
1951Read a string from standard input. The trailing newline is stripped.
1952
1953The prompt string, if given, is printed to standard output without a
1954trailing newline before reading input.
1955
1956If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
1957On *nix systems, readline is used if available.
1958[clinic start generated code]*/
1959
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001960static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001961builtin_input_impl(PyObject *module, PyObject *prompt)
1962/*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10001963{
Victor Stinnerbd303c12013-11-07 23:07:29 +01001964 PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
1965 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
1966 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 PyObject *tmp;
1968 long fd;
1969 int tty;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 /* Check that stdin/out/err are intact */
1972 if (fin == NULL || fin == Py_None) {
1973 PyErr_SetString(PyExc_RuntimeError,
1974 "input(): lost sys.stdin");
1975 return NULL;
1976 }
1977 if (fout == NULL || fout == Py_None) {
1978 PyErr_SetString(PyExc_RuntimeError,
1979 "input(): lost sys.stdout");
1980 return NULL;
1981 }
1982 if (ferr == NULL || ferr == Py_None) {
1983 PyErr_SetString(PyExc_RuntimeError,
1984 "input(): lost sys.stderr");
1985 return NULL;
1986 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 /* First of all, flush stderr */
Victor Stinner3466bde2016-09-05 18:16:01 -07001989 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (tmp == NULL)
1991 PyErr_Clear();
1992 else
1993 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 /* We should only use (GNU) readline if Python's sys.stdin and
1996 sys.stdout are the same as C's stdin and stdout, because we
1997 need to pass it those. */
Victor Stinner3466bde2016-09-05 18:16:01 -07001998 tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 if (tmp == NULL) {
2000 PyErr_Clear();
2001 tty = 0;
2002 }
2003 else {
2004 fd = PyLong_AsLong(tmp);
2005 Py_DECREF(tmp);
2006 if (fd < 0 && PyErr_Occurred())
2007 return NULL;
2008 tty = fd == fileno(stdin) && isatty(fd);
2009 }
2010 if (tty) {
Victor Stinner3466bde2016-09-05 18:16:01 -07002011 tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
Martin Panterc9a6ab52015-10-10 01:25:38 +00002012 if (tmp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 PyErr_Clear();
Martin Panterc9a6ab52015-10-10 01:25:38 +00002014 tty = 0;
2015 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 else {
2017 fd = PyLong_AsLong(tmp);
2018 Py_DECREF(tmp);
2019 if (fd < 0 && PyErr_Occurred())
2020 return NULL;
2021 tty = fd == fileno(stdout) && isatty(fd);
2022 }
2023 }
Guido van Rossumeba76962007-05-27 09:13:28 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 /* If we're interactive, use (GNU) readline */
2026 if (tty) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002027 PyObject *po = NULL;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002028 const char *promptstr;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002029 char *s = NULL;
2030 PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
2031 PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002032 const char *stdin_encoding_str, *stdin_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 PyObject *result;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002034 size_t len;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00002035
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002036 /* stdin is a text stream, so it must have an encoding. */
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002037 stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002038 stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002039 if (!stdin_encoding || !stdin_errors ||
2040 !PyUnicode_Check(stdin_encoding) ||
2041 !PyUnicode_Check(stdin_errors)) {
2042 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002043 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002044 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002045 stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2046 stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002047 if (!stdin_encoding_str || !stdin_errors_str)
2048 goto _readline_errors;
Victor Stinner3466bde2016-09-05 18:16:01 -07002049 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 if (tmp == NULL)
2051 PyErr_Clear();
2052 else
2053 Py_DECREF(tmp);
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002054 if (prompt != NULL) {
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002055 /* We have a prompt, encode it as stdout would */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002056 const char *stdout_encoding_str, *stdout_errors_str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 PyObject *stringpo;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002058 stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
Antoine Pitrou5ee9d8a2011-11-06 00:38:45 +01002059 stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002060 if (!stdout_encoding || !stdout_errors ||
2061 !PyUnicode_Check(stdout_encoding) ||
2062 !PyUnicode_Check(stdout_errors)) {
2063 tty = 0;
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002064 goto _readline_errors;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002065 }
Serhiy Storchaka06515832016-11-20 09:13:07 +02002066 stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2067 stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002068 if (!stdout_encoding_str || !stdout_errors_str)
2069 goto _readline_errors;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002070 stringpo = PyObject_Str(prompt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002071 if (stringpo == NULL)
2072 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 po = PyUnicode_AsEncodedString(stringpo,
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002074 stdout_encoding_str, stdout_errors_str);
2075 Py_CLEAR(stdout_encoding);
2076 Py_CLEAR(stdout_errors);
2077 Py_CLEAR(stringpo);
2078 if (po == NULL)
2079 goto _readline_errors;
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03002080 assert(PyBytes_Check(po));
2081 promptstr = PyBytes_AS_STRING(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 }
2083 else {
2084 po = NULL;
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002085 promptstr = "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 }
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002087 s = PyOS_Readline(stdin, stdout, promptstr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 if (s == NULL) {
Richard Oudkerk614c5782013-04-03 13:44:50 +01002089 PyErr_CheckSignals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (!PyErr_Occurred())
2091 PyErr_SetNone(PyExc_KeyboardInterrupt);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002092 goto _readline_errors;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002094
2095 len = strlen(s);
2096 if (len == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 PyErr_SetNone(PyExc_EOFError);
2098 result = NULL;
2099 }
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002100 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 if (len > PY_SSIZE_T_MAX) {
2102 PyErr_SetString(PyExc_OverflowError,
2103 "input: input too long");
2104 result = NULL;
2105 }
2106 else {
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00002107 len--; /* strip trailing '\n' */
2108 if (len != 0 && s[len-1] == '\r')
2109 len--; /* strip trailing '\r' */
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002110 result = PyUnicode_Decode(s, len, stdin_encoding_str,
2111 stdin_errors_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 }
2113 }
2114 Py_DECREF(stdin_encoding);
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002115 Py_DECREF(stdin_errors);
2116 Py_XDECREF(po);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 PyMem_FREE(s);
2118 return result;
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002119
Antoine Pitrou0d776b12011-11-06 00:34:26 +01002120 _readline_errors:
2121 Py_XDECREF(stdin_encoding);
2122 Py_XDECREF(stdout_encoding);
2123 Py_XDECREF(stdin_errors);
2124 Py_XDECREF(stdout_errors);
2125 Py_XDECREF(po);
Serhiy Storchakac2cf1282017-03-12 13:50:36 +02002126 if (tty)
2127 return NULL;
2128
2129 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 }
Guido van Rossumeba76962007-05-27 09:13:28 +00002131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 /* Fallback if we're not interactive */
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002133 if (prompt != NULL) {
2134 if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 return NULL;
2136 }
Victor Stinner3466bde2016-09-05 18:16:01 -07002137 tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 if (tmp == NULL)
2139 PyErr_Clear();
2140 else
2141 Py_DECREF(tmp);
2142 return PyFile_GetLine(fin, -1);
Guido van Rossuma88a0332007-02-26 16:59:55 +00002143}
2144
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002145
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002146/*[clinic input]
2147repr as builtin_repr
2148
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002149 obj: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002150 /
2151
2152Return the canonical string representation of the object.
2153
2154For many object types, including most builtins, eval(repr(obj)) == obj.
2155[clinic start generated code]*/
2156
Guido van Rossum79f25d91997-04-29 20:08:16 +00002157static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002158builtin_repr(PyObject *module, PyObject *obj)
2159/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
Guido van Rossumc89705d1992-11-26 08:54:07 +00002160{
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002161 return PyObject_Repr(obj);
Guido van Rossumc89705d1992-11-26 08:54:07 +00002162}
2163
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002164
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002165/*[clinic input]
2166round as builtin_round
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002167
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002168 number: object
2169 ndigits: object = NULL
2170
2171Round a number to a given precision in decimal digits.
2172
2173The return value is an integer if ndigits is omitted or None. Otherwise
2174the return value has the same type as the number. ndigits may be negative.
2175[clinic start generated code]*/
2176
2177static PyObject *
2178builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
2179/*[clinic end generated code: output=ff0d9dd176c02ede input=854bc3a217530c3d]*/
2180{
2181 PyObject *round, *result;
Alex Martelliae211f92007-08-22 23:21:33 +00002182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 if (Py_TYPE(number)->tp_dict == NULL) {
2184 if (PyType_Ready(Py_TYPE(number)) < 0)
2185 return NULL;
2186 }
Guido van Rossum15d3d042007-08-24 02:02:45 +00002187
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002188 round = _PyObject_LookupSpecial(number, &PyId___round__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (round == NULL) {
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002190 if (!PyErr_Occurred())
2191 PyErr_Format(PyExc_TypeError,
2192 "type %.100s doesn't define __round__ method",
2193 Py_TYPE(number)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 return NULL;
2195 }
Alex Martelliae211f92007-08-22 23:21:33 +00002196
Raymond Hettingerf0f1c232016-09-03 01:55:11 -07002197 if (ndigits == NULL || ndigits == Py_None)
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002198 result = _PyObject_CallNoArg(round);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 else
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002200 result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002201 Py_DECREF(round);
2202 return result;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00002203}
2204
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002205
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002206/*AC: we need to keep the kwds dict intact to easily call into the
2207 * list.sort method, which isn't currently supported in AC. So we just use
2208 * the initially generated signature with a custom implementation.
2209 */
2210/* [disabled clinic input]
2211sorted as builtin_sorted
2212
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002213 iterable as seq: object
2214 key as keyfunc: object = None
2215 reverse: object = False
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002216
2217Return a new list containing all items from the iterable in ascending order.
2218
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002219A custom key function can be supplied to customize the sort order, and the
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002220reverse flag can be set to request the result in descending order.
2221[end disabled clinic input]*/
2222
2223PyDoc_STRVAR(builtin_sorted__doc__,
Serhiy Storchaka3a104252017-01-23 12:29:47 +02002224"sorted($module, iterable, /, *, key=None, reverse=False)\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002225"--\n"
2226"\n"
2227"Return a new list containing all items from the iterable in ascending order.\n"
2228"\n"
Raymond Hettinger7ea386e2016-08-25 21:11:50 -07002229"A custom key function can be supplied to customize the sort order, and the\n"
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002230"reverse flag can be set to request the result in descending order.");
2231
2232#define BUILTIN_SORTED_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002233 {"sorted", (PyCFunction)builtin_sorted, METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002234
Raymond Hettinger64958a12003-12-17 20:43:33 +00002235static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002236builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Raymond Hettinger64958a12003-12-17 20:43:33 +00002237{
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002238 PyObject *newlist, *v, *seq, *callable;
Victor Stinner5a60eca2017-01-17 15:17:49 +01002239
Serhiy Storchaka7cf8beb2017-01-21 23:05:00 +02002240 /* Keyword arguments are passed through list.sort() which will check
2241 them. */
2242 if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 newlist = PySequence_List(seq);
2246 if (newlist == NULL)
2247 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002248
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002249 callable = _PyObject_GetAttrId(newlist, &PyId_sort);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 if (callable == NULL) {
2251 Py_DECREF(newlist);
2252 return NULL;
2253 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00002254
Serhiy Storchaka299dc232017-01-20 08:35:18 +02002255 assert(nargs >= 1);
Victor Stinner5a60eca2017-01-17 15:17:49 +01002256 v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 Py_DECREF(callable);
2258 if (v == NULL) {
2259 Py_DECREF(newlist);
2260 return NULL;
2261 }
2262 Py_DECREF(v);
2263 return newlist;
Raymond Hettinger64958a12003-12-17 20:43:33 +00002264}
2265
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002266
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002267/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002268static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002269builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00002270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 PyObject *v = NULL;
2272 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2275 return NULL;
2276 if (v == NULL) {
2277 d = PyEval_GetLocals();
Victor Stinner41bb43a2013-10-29 01:19:37 +01002278 if (d == NULL)
2279 return NULL;
2280 Py_INCREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
2282 else {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02002283 d = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if (d == NULL) {
2285 PyErr_SetString(PyExc_TypeError,
2286 "vars() argument must have __dict__ attribute");
2287 return NULL;
2288 }
2289 }
2290 return d;
Guido van Rossum2d951851994-08-29 12:52:16 +00002291}
2292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002293PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002294"vars([object]) -> dictionary\n\
2295\n\
2296Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002297With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002298
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002299
2300/*[clinic input]
2301sum as builtin_sum
2302
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002303 iterable: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002304 start: object(c_default="NULL") = 0
2305 /
2306
2307Return the sum of a 'start' value (default: 0) plus an iterable of numbers
2308
2309When the iterable is empty, return the start value.
2310This function is intended specifically for use with numeric values and may
2311reject non-numeric types.
2312[clinic start generated code]*/
2313
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002314static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002315builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
2316/*[clinic end generated code: output=df758cec7d1d302f input=3b5b7a9d7611c73a]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002317{
2318 PyObject *result = start;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 PyObject *temp, *item, *iter;
Alex Martellia70b1912003-04-22 08:12:33 +00002320
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002321 iter = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 if (iter == NULL)
2323 return NULL;
Alex Martellia70b1912003-04-22 08:12:33 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 if (result == NULL) {
2326 result = PyLong_FromLong(0);
2327 if (result == NULL) {
2328 Py_DECREF(iter);
2329 return NULL;
2330 }
2331 } else {
2332 /* reject string values for 'start' parameter */
2333 if (PyUnicode_Check(result)) {
2334 PyErr_SetString(PyExc_TypeError,
2335 "sum() can't sum strings [use ''.join(seq) instead]");
2336 Py_DECREF(iter);
2337 return NULL;
2338 }
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002339 if (PyBytes_Check(result)) {
2340 PyErr_SetString(PyExc_TypeError,
2341 "sum() can't sum bytes [use b''.join(seq) instead]");
Benjamin Peterson405f32c2011-07-29 22:43:45 -05002342 Py_DECREF(iter);
Benjamin Petersonce071ca2011-07-29 14:23:47 -05002343 return NULL;
2344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 if (PyByteArray_Check(result)) {
2346 PyErr_SetString(PyExc_TypeError,
Benjamin Peterson4f921c22011-07-29 14:24:29 -05002347 "sum() can't sum bytearray [use b''.join(seq) instead]");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 Py_DECREF(iter);
2349 return NULL;
2350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 Py_INCREF(result);
2352 }
Alex Martellia70b1912003-04-22 08:12:33 +00002353
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002354#ifndef SLOW_SUM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 /* Fast addition by keeping temporary sums in C instead of new Python objects.
2356 Assumes all inputs are the same type. If the assumption fails, default
2357 to the more general routine.
2358 */
2359 if (PyLong_CheckExact(result)) {
2360 int overflow;
2361 long i_result = PyLong_AsLongAndOverflow(result, &overflow);
2362 /* If this already overflowed, don't even enter the loop. */
2363 if (overflow == 0) {
2364 Py_DECREF(result);
2365 result = NULL;
2366 }
2367 while(result == NULL) {
2368 item = PyIter_Next(iter);
2369 if (item == NULL) {
2370 Py_DECREF(iter);
2371 if (PyErr_Occurred())
2372 return NULL;
2373 return PyLong_FromLong(i_result);
2374 }
2375 if (PyLong_CheckExact(item)) {
2376 long b = PyLong_AsLongAndOverflow(item, &overflow);
2377 long x = i_result + b;
2378 if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
2379 i_result = x;
2380 Py_DECREF(item);
2381 continue;
2382 }
2383 }
2384 /* Either overflowed or is not an int. Restore real objects and process normally */
2385 result = PyLong_FromLong(i_result);
Christian Heimes704e2d32013-07-26 22:49:26 +02002386 if (result == NULL) {
2387 Py_DECREF(item);
2388 Py_DECREF(iter);
2389 return NULL;
2390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 temp = PyNumber_Add(result, item);
2392 Py_DECREF(result);
2393 Py_DECREF(item);
2394 result = temp;
2395 if (result == NULL) {
2396 Py_DECREF(iter);
2397 return NULL;
2398 }
2399 }
2400 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 if (PyFloat_CheckExact(result)) {
2403 double f_result = PyFloat_AS_DOUBLE(result);
2404 Py_DECREF(result);
2405 result = NULL;
2406 while(result == NULL) {
2407 item = PyIter_Next(iter);
2408 if (item == NULL) {
2409 Py_DECREF(iter);
2410 if (PyErr_Occurred())
2411 return NULL;
2412 return PyFloat_FromDouble(f_result);
2413 }
2414 if (PyFloat_CheckExact(item)) {
2415 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2416 f_result += PyFloat_AS_DOUBLE(item);
2417 PyFPE_END_PROTECT(f_result)
2418 Py_DECREF(item);
2419 continue;
2420 }
2421 if (PyLong_CheckExact(item)) {
2422 long value;
2423 int overflow;
2424 value = PyLong_AsLongAndOverflow(item, &overflow);
2425 if (!overflow) {
2426 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2427 f_result += (double)value;
2428 PyFPE_END_PROTECT(f_result)
2429 Py_DECREF(item);
2430 continue;
2431 }
2432 }
2433 result = PyFloat_FromDouble(f_result);
2434 temp = PyNumber_Add(result, item);
2435 Py_DECREF(result);
2436 Py_DECREF(item);
2437 result = temp;
2438 if (result == NULL) {
2439 Py_DECREF(iter);
2440 return NULL;
2441 }
2442 }
2443 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002444#endif
2445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 for(;;) {
2447 item = PyIter_Next(iter);
2448 if (item == NULL) {
2449 /* error, or end-of-sequence */
2450 if (PyErr_Occurred()) {
2451 Py_DECREF(result);
2452 result = NULL;
2453 }
2454 break;
2455 }
2456 /* It's tempting to use PyNumber_InPlaceAdd instead of
2457 PyNumber_Add here, to avoid quadratic running time
2458 when doing 'sum(list_of_lists, [])'. However, this
2459 would produce a change in behaviour: a snippet like
Mark Dickinson9acadc52009-10-26 14:19:42 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 empty = []
2462 sum([[x] for x in range(10)], empty)
Mark Dickinson9acadc52009-10-26 14:19:42 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 would change the value of empty. */
2465 temp = PyNumber_Add(result, item);
2466 Py_DECREF(result);
2467 Py_DECREF(item);
2468 result = temp;
2469 if (result == NULL)
2470 break;
2471 }
2472 Py_DECREF(iter);
2473 return result;
Alex Martellia70b1912003-04-22 08:12:33 +00002474}
2475
Alex Martellia70b1912003-04-22 08:12:33 +00002476
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002477/*[clinic input]
2478isinstance as builtin_isinstance
2479
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002480 obj: object
2481 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002482 /
2483
2484Return whether an object is an instance of a class or of a subclass thereof.
2485
2486A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
2487check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
2488or ...`` etc.
2489[clinic start generated code]*/
2490
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002491static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002492builtin_isinstance_impl(PyObject *module, PyObject *obj,
Larry Hastings89964c42015-04-14 18:07:59 -04002493 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002494/*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002497
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002498 retval = PyObject_IsInstance(obj, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 if (retval < 0)
2500 return NULL;
2501 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002502}
2503
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002504
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002505/*[clinic input]
2506issubclass as builtin_issubclass
2507
Serhiy Storchaka7e810a62015-05-30 11:09:35 +03002508 cls: object
2509 class_or_tuple: object
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002510 /
2511
2512Return whether 'cls' is a derived from another class or is the same class.
2513
2514A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
2515check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
2516or ...`` etc.
2517[clinic start generated code]*/
2518
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002519static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002520builtin_issubclass_impl(PyObject *module, PyObject *cls,
Larry Hastings89964c42015-04-14 18:07:59 -04002521 PyObject *class_or_tuple)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002522/*[clinic end generated code: output=358412410cd7a250 input=af5f35e9ceaddaf6]*/
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002525
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002526 retval = PyObject_IsSubclass(cls, class_or_tuple);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 if (retval < 0)
2528 return NULL;
2529 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00002530}
2531
2532
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002533typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 PyObject_HEAD
2535 Py_ssize_t tuplesize;
2536 PyObject *ittuple; /* tuple of iterators */
2537 PyObject *result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002538} zipobject;
2539
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002540static PyObject *
2541zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Barry Warsawbd599b52000-08-03 15:45:29 +00002542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 zipobject *lz;
2544 Py_ssize_t i;
2545 PyObject *ittuple; /* tuple of iterators */
2546 PyObject *result;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002547 Py_ssize_t tuplesize;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002548
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03002549 if (type == &PyZip_Type && !_PyArg_NoKeywords("zip", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 return NULL;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 /* args must be a tuple */
2553 assert(PyTuple_Check(args));
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03002554 tuplesize = PyTuple_GET_SIZE(args);
Barry Warsawbd599b52000-08-03 15:45:29 +00002555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 /* obtain iterators */
2557 ittuple = PyTuple_New(tuplesize);
2558 if (ittuple == NULL)
2559 return NULL;
2560 for (i=0; i < tuplesize; ++i) {
2561 PyObject *item = PyTuple_GET_ITEM(args, i);
2562 PyObject *it = PyObject_GetIter(item);
2563 if (it == NULL) {
2564 if (PyErr_ExceptionMatches(PyExc_TypeError))
2565 PyErr_Format(PyExc_TypeError,
2566 "zip argument #%zd must support iteration",
2567 i+1);
2568 Py_DECREF(ittuple);
2569 return NULL;
2570 }
2571 PyTuple_SET_ITEM(ittuple, i, it);
2572 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 /* create a result holder */
2575 result = PyTuple_New(tuplesize);
2576 if (result == NULL) {
2577 Py_DECREF(ittuple);
2578 return NULL;
2579 }
2580 for (i=0 ; i < tuplesize ; i++) {
2581 Py_INCREF(Py_None);
2582 PyTuple_SET_ITEM(result, i, Py_None);
2583 }
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 /* create zipobject structure */
2586 lz = (zipobject *)type->tp_alloc(type, 0);
2587 if (lz == NULL) {
2588 Py_DECREF(ittuple);
2589 Py_DECREF(result);
2590 return NULL;
2591 }
2592 lz->ittuple = ittuple;
2593 lz->tuplesize = tuplesize;
2594 lz->result = result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 return (PyObject *)lz;
Barry Warsawbd599b52000-08-03 15:45:29 +00002597}
2598
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002599static void
2600zip_dealloc(zipobject *lz)
2601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 PyObject_GC_UnTrack(lz);
2603 Py_XDECREF(lz->ittuple);
2604 Py_XDECREF(lz->result);
2605 Py_TYPE(lz)->tp_free(lz);
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002606}
2607
2608static int
2609zip_traverse(zipobject *lz, visitproc visit, void *arg)
2610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 Py_VISIT(lz->ittuple);
2612 Py_VISIT(lz->result);
2613 return 0;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002614}
2615
2616static PyObject *
2617zip_next(zipobject *lz)
2618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 Py_ssize_t i;
2620 Py_ssize_t tuplesize = lz->tuplesize;
2621 PyObject *result = lz->result;
2622 PyObject *it;
2623 PyObject *item;
2624 PyObject *olditem;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 if (tuplesize == 0)
2627 return NULL;
2628 if (Py_REFCNT(result) == 1) {
2629 Py_INCREF(result);
2630 for (i=0 ; i < tuplesize ; i++) {
2631 it = PyTuple_GET_ITEM(lz->ittuple, i);
2632 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002633 if (item == NULL) {
2634 Py_DECREF(result);
2635 return NULL;
2636 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 olditem = PyTuple_GET_ITEM(result, i);
2638 PyTuple_SET_ITEM(result, i, item);
2639 Py_DECREF(olditem);
2640 }
2641 } else {
2642 result = PyTuple_New(tuplesize);
2643 if (result == NULL)
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002644 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 for (i=0 ; i < tuplesize ; i++) {
2646 it = PyTuple_GET_ITEM(lz->ittuple, i);
2647 item = (*Py_TYPE(it)->tp_iternext)(it);
Serhiy Storchaka278d03b2013-04-06 22:52:34 +03002648 if (item == NULL) {
2649 Py_DECREF(result);
2650 return NULL;
2651 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 PyTuple_SET_ITEM(result, i, item);
2653 }
2654 }
2655 return result;
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002656}
Barry Warsawbd599b52000-08-03 15:45:29 +00002657
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002658static PyObject *
2659zip_reduce(zipobject *lz)
2660{
2661 /* Just recreate the zip with the internal iterator tuple */
2662 return Py_BuildValue("OO", Py_TYPE(lz), lz->ittuple);
2663}
2664
2665static PyMethodDef zip_methods[] = {
2666 {"__reduce__", (PyCFunction)zip_reduce, METH_NOARGS, reduce_doc},
2667 {NULL, NULL} /* sentinel */
2668};
2669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002670PyDoc_STRVAR(zip_doc,
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002671"zip(iter1 [,iter2 [...]]) --> zip object\n\
Barry Warsawbd599b52000-08-03 15:45:29 +00002672\n\
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002673Return a zip object whose .__next__() method returns a tuple where\n\
2674the i-th element comes from the i-th iterable argument. The .__next__()\n\
2675method continues until the shortest iterable in the argument sequence\n\
Georg Brandlced51db2008-12-04 18:28:38 +00002676is exhausted and then it raises StopIteration.");
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002677
2678PyTypeObject PyZip_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2680 "zip", /* tp_name */
2681 sizeof(zipobject), /* tp_basicsize */
2682 0, /* tp_itemsize */
2683 /* methods */
2684 (destructor)zip_dealloc, /* tp_dealloc */
2685 0, /* tp_print */
2686 0, /* tp_getattr */
2687 0, /* tp_setattr */
2688 0, /* tp_reserved */
2689 0, /* tp_repr */
2690 0, /* tp_as_number */
2691 0, /* tp_as_sequence */
2692 0, /* tp_as_mapping */
2693 0, /* tp_hash */
2694 0, /* tp_call */
2695 0, /* tp_str */
2696 PyObject_GenericGetAttr, /* tp_getattro */
2697 0, /* tp_setattro */
2698 0, /* tp_as_buffer */
2699 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2700 Py_TPFLAGS_BASETYPE, /* tp_flags */
2701 zip_doc, /* tp_doc */
2702 (traverseproc)zip_traverse, /* tp_traverse */
2703 0, /* tp_clear */
2704 0, /* tp_richcompare */
2705 0, /* tp_weaklistoffset */
2706 PyObject_SelfIter, /* tp_iter */
2707 (iternextfunc)zip_next, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002708 zip_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 0, /* tp_members */
2710 0, /* tp_getset */
2711 0, /* tp_base */
2712 0, /* tp_dict */
2713 0, /* tp_descr_get */
2714 0, /* tp_descr_set */
2715 0, /* tp_dictoffset */
2716 0, /* tp_init */
2717 PyType_GenericAlloc, /* tp_alloc */
2718 zip_new, /* tp_new */
2719 PyObject_GC_Del, /* tp_free */
Raymond Hettinger736c0ab2008-03-13 02:09:15 +00002720};
Barry Warsawbd599b52000-08-03 15:45:29 +00002721
2722
Guido van Rossum79f25d91997-04-29 20:08:16 +00002723static PyMethodDef builtin_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 {"__build_class__", (PyCFunction)builtin___build_class__,
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002725 METH_FASTCALL | METH_KEYWORDS, build_class_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002727 BUILTIN_ABS_METHODDEF
2728 BUILTIN_ALL_METHODDEF
2729 BUILTIN_ANY_METHODDEF
2730 BUILTIN_ASCII_METHODDEF
2731 BUILTIN_BIN_METHODDEF
Barry Warsaw36c1d1f2017-10-05 12:11:18 -04002732 {"breakpoint", (PyCFunction)builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002733 BUILTIN_CALLABLE_METHODDEF
2734 BUILTIN_CHR_METHODDEF
2735 BUILTIN_COMPILE_METHODDEF
2736 BUILTIN_DELATTR_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 {"dir", builtin_dir, METH_VARARGS, dir_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002738 BUILTIN_DIVMOD_METHODDEF
2739 BUILTIN_EVAL_METHODDEF
2740 BUILTIN_EXEC_METHODDEF
2741 BUILTIN_FORMAT_METHODDEF
Victor Stinner84b388b2017-01-17 03:52:27 +01002742 {"getattr", (PyCFunction)builtin_getattr, METH_FASTCALL, getattr_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002743 BUILTIN_GLOBALS_METHODDEF
2744 BUILTIN_HASATTR_METHODDEF
2745 BUILTIN_HASH_METHODDEF
2746 BUILTIN_HEX_METHODDEF
2747 BUILTIN_ID_METHODDEF
2748 BUILTIN_INPUT_METHODDEF
2749 BUILTIN_ISINSTANCE_METHODDEF
2750 BUILTIN_ISSUBCLASS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 {"iter", builtin_iter, METH_VARARGS, iter_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002752 BUILTIN_LEN_METHODDEF
2753 BUILTIN_LOCALS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2755 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Victor Stinnerfda6d0a2017-01-17 04:09:14 +01002756 {"next", (PyCFunction)builtin_next, METH_FASTCALL, next_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002757 BUILTIN_OCT_METHODDEF
2758 BUILTIN_ORD_METHODDEF
2759 BUILTIN_POW_METHODDEF
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002760 {"print", (PyCFunction)builtin_print, METH_FASTCALL | METH_KEYWORDS, print_doc},
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002761 BUILTIN_REPR_METHODDEF
Serhiy Storchakaaca7f572017-11-15 17:51:14 +02002762 BUILTIN_ROUND_METHODDEF
Nick Coghlanf9e227e2014-08-17 14:01:19 +10002763 BUILTIN_SETATTR_METHODDEF
2764 BUILTIN_SORTED_METHODDEF
2765 BUILTIN_SUM_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 {"vars", builtin_vars, METH_VARARGS, vars_doc},
2767 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002768};
2769
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002770PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002771"Built-in functions, exceptions, and other objects.\n\
2772\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002773Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00002774
Martin v. Löwis1a214512008-06-11 05:26:20 +00002775static struct PyModuleDef builtinsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 PyModuleDef_HEAD_INIT,
2777 "builtins",
2778 builtin_doc,
2779 -1, /* multiple "initialization" just copies the module dict. */
2780 builtin_methods,
2781 NULL,
2782 NULL,
2783 NULL,
2784 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002785};
2786
2787
Guido van Rossum25ce5661997-08-02 03:10:38 +00002788PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002789_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 PyObject *mod, *dict, *debug;
Benjamin Peterson42124a72012-10-30 23:41:54 -04002792
2793 if (PyType_Ready(&PyFilter_Type) < 0 ||
2794 PyType_Ready(&PyMap_Type) < 0 ||
2795 PyType_Ready(&PyZip_Type) < 0)
2796 return NULL;
2797
Eric Snowd393c1b2017-09-14 12:18:12 -06002798 mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 if (mod == NULL)
2800 return NULL;
2801 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00002802
Tim Peters7571a0f2003-03-23 17:52:28 +00002803#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 /* "builtins" exposes a number of statically allocated objects
2805 * that, before this code was added in 2.3, never showed up in
2806 * the list of "all objects" maintained by Py_TRACE_REFS. As a
2807 * result, programs leaking references to None and False (etc)
2808 * couldn't be diagnosed by examining sys.getobjects(0).
2809 */
Tim Peters7571a0f2003-03-23 17:52:28 +00002810#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2811#else
2812#define ADD_TO_ALL(OBJECT) (void)0
2813#endif
2814
Tim Peters4b7625e2001-09-13 21:37:17 +00002815#define SETBUILTIN(NAME, OBJECT) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2817 return NULL; \
2818 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00002819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 SETBUILTIN("None", Py_None);
2821 SETBUILTIN("Ellipsis", Py_Ellipsis);
2822 SETBUILTIN("NotImplemented", Py_NotImplemented);
2823 SETBUILTIN("False", Py_False);
2824 SETBUILTIN("True", Py_True);
2825 SETBUILTIN("bool", &PyBool_Type);
2826 SETBUILTIN("memoryview", &PyMemoryView_Type);
2827 SETBUILTIN("bytearray", &PyByteArray_Type);
2828 SETBUILTIN("bytes", &PyBytes_Type);
2829 SETBUILTIN("classmethod", &PyClassMethod_Type);
2830 SETBUILTIN("complex", &PyComplex_Type);
2831 SETBUILTIN("dict", &PyDict_Type);
2832 SETBUILTIN("enumerate", &PyEnum_Type);
2833 SETBUILTIN("filter", &PyFilter_Type);
2834 SETBUILTIN("float", &PyFloat_Type);
2835 SETBUILTIN("frozenset", &PyFrozenSet_Type);
2836 SETBUILTIN("property", &PyProperty_Type);
2837 SETBUILTIN("int", &PyLong_Type);
2838 SETBUILTIN("list", &PyList_Type);
2839 SETBUILTIN("map", &PyMap_Type);
2840 SETBUILTIN("object", &PyBaseObject_Type);
2841 SETBUILTIN("range", &PyRange_Type);
2842 SETBUILTIN("reversed", &PyReversed_Type);
2843 SETBUILTIN("set", &PySet_Type);
2844 SETBUILTIN("slice", &PySlice_Type);
2845 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2846 SETBUILTIN("str", &PyUnicode_Type);
2847 SETBUILTIN("super", &PySuper_Type);
2848 SETBUILTIN("tuple", &PyTuple_Type);
2849 SETBUILTIN("type", &PyType_Type);
2850 SETBUILTIN("zip", &PyZip_Type);
2851 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
2852 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002853 Py_DECREF(debug);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 return NULL;
2855 }
Serhiy Storchakacfdfbb42016-06-18 09:44:03 +03002856 Py_DECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00002857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00002859#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00002860#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00002861}