blob: 3e2f2a18928c154c599602d829773d7b26fb91ff [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Built-in functions */
2
Guido van Rossum79f25d91997-04-29 20:08:16 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
5#include "node.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum5b722181993-03-30 17:46:03 +00007#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008
Guido van Rossum6bf62da1997-04-11 20:37:35 +00009#include <ctype.h>
10
Mark Hammond26cffde42001-05-14 12:17:34 +000011/* The default encoding used by the platform file system APIs
12 Can remain NULL for all platforms that don't have such a concept
Guido van Rossum00bc0e02007-10-15 02:52:41 +000013
14 Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the
15 values for Py_FileSystemDefaultEncoding!
Mark Hammond26cffde42001-05-14 12:17:34 +000016*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000017#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde42001-05-14 12:17:34 +000018const char *Py_FileSystemDefaultEncoding = "mbcs";
Christian Heimesc8967002007-11-30 10:18:26 +000019const int Py_HasFileSystemDefaultEncoding = 1;
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Christian Heimesc8967002007-11-30 10:18:26 +000022const int Py_HasFileSystemDefaultEncoding = 1;
Mark Hammond26cffde42001-05-14 12:17:34 +000023#else
24const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
Christian Heimesc8967002007-11-30 10:18:26 +000025const int Py_HasFileSystemDefaultEncoding = 0;
Mark Hammond26cffde42001-05-14 12:17:34 +000026#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000027
Guido van Rossum79f25d91997-04-29 20:08:16 +000028static PyObject *
Guido van Rossum52cc1d82007-03-18 15:41:51 +000029builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
30{
Guido van Rossumcd16bf62007-06-13 18:07:49 +000031 PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell;
32 PyObject *cls = NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +000033 Py_ssize_t nargs, nbases;
34
35 assert(args != NULL);
36 if (!PyTuple_Check(args)) {
37 PyErr_SetString(PyExc_TypeError,
38 "__build_class__: args is not a tuple");
39 return NULL;
40 }
41 nargs = PyTuple_GET_SIZE(args);
42 if (nargs < 2) {
43 PyErr_SetString(PyExc_TypeError,
44 "__build_class__: not enough arguments");
45 return NULL;
46 }
47 func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
48 name = PyTuple_GET_ITEM(args, 1);
Neal Norwitz6ea45d32007-08-26 04:19:43 +000049 if (!PyUnicode_Check(name)) {
Guido van Rossum52cc1d82007-03-18 15:41:51 +000050 PyErr_SetString(PyExc_TypeError,
51 "__build_class__: name is not a string");
52 return NULL;
53 }
54 bases = PyTuple_GetSlice(args, 2, nargs);
55 if (bases == NULL)
56 return NULL;
57 nbases = nargs - 2;
58
59 if (kwds == NULL) {
60 meta = NULL;
61 mkw = NULL;
62 }
63 else {
64 mkw = PyDict_Copy(kwds); /* Don't modify kwds passed in! */
65 if (mkw == NULL) {
66 Py_DECREF(bases);
67 return NULL;
68 }
69 meta = PyDict_GetItemString(mkw, "metaclass");
70 if (meta != NULL) {
71 Py_INCREF(meta);
72 if (PyDict_DelItemString(mkw, "metaclass") < 0) {
73 Py_DECREF(meta);
74 Py_DECREF(mkw);
75 Py_DECREF(bases);
76 return NULL;
77 }
78 }
79 }
80 if (meta == NULL) {
81 if (PyTuple_GET_SIZE(bases) == 0)
82 meta = (PyObject *) (&PyType_Type);
83 else {
84 PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
85 meta = (PyObject *) (base0->ob_type);
86 }
87 Py_INCREF(meta);
88 }
89 prep = PyObject_GetAttrString(meta, "__prepare__");
90 if (prep == NULL) {
91 PyErr_Clear();
92 ns = PyDict_New();
93 }
94 else {
95 PyObject *pargs = Py_BuildValue("OO", name, bases);
96 if (pargs == NULL) {
97 Py_DECREF(prep);
98 Py_DECREF(meta);
99 Py_XDECREF(mkw);
100 Py_DECREF(bases);
101 return NULL;
102 }
103 ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
104 Py_DECREF(pargs);
105 Py_DECREF(prep);
106 if (ns == NULL) {
107 Py_DECREF(meta);
108 Py_XDECREF(mkw);
109 Py_DECREF(bases);
110 return NULL;
111 }
112 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000113 cell = PyObject_CallFunctionObjArgs(func, ns, NULL);
114 if (cell != NULL) {
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000115 PyObject *margs;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000116 margs = Py_BuildValue("OOO", name, bases, ns);
117 if (margs != NULL) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000118 cls = PyEval_CallObjectWithKeywords(meta, margs, mkw);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000119 Py_DECREF(margs);
120 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000121 if (cls != NULL && PyCell_Check(cell)) {
122 Py_INCREF(cls);
123 PyCell_SET(cell, cls);
124 }
125 Py_DECREF(cell);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000126 }
127 Py_DECREF(ns);
128 Py_DECREF(meta);
129 Py_XDECREF(mkw);
130 Py_DECREF(bases);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000131 return cls;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000132}
133
134PyDoc_STRVAR(build_class_doc,
135"__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
136\n\
137Internal helper function used by the class statement.");
138
139static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000140builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000141{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
143 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000144 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000145 PyObject *globals = NULL;
146 PyObject *locals = NULL;
147 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000148 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
151 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000152 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000153 return PyImport_ImportModuleLevel(name, globals, locals,
154 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000155}
156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000157PyDoc_STRVAR(import_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000159\n\
160Import a module. The globals are only used to determine the context;\n\
161they are not modified. The locals are currently unused. The fromlist\n\
162should be a list of names to emulate ``from name import ...'', or an\n\
163empty list to emulate ``import name''.\n\
164When importing a module from a package, note that __import__('A.B', ...)\n\
165returns package A when fromlist is empty, but its submodule B when\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166fromlist is not empty. Level is used to determine whether to perform \n\
167absolute or relative imports. -1 is the original strategy of attempting\n\
168both absolute and relative imports, 0 is absolute, a positive number\n\
169is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000170
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000171
Guido van Rossum79f25d91997-04-29 20:08:16 +0000172static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000173builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000174{
Guido van Rossum09df08a1998-05-22 00:51:39 +0000175 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000176}
177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000179"abs(number) -> number\n\
180\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000181Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000182
Raymond Hettinger96229b12005-03-11 06:49:40 +0000183static PyObject *
184builtin_all(PyObject *self, PyObject *v)
185{
186 PyObject *it, *item;
187
188 it = PyObject_GetIter(v);
189 if (it == NULL)
190 return NULL;
191
192 while ((item = PyIter_Next(it)) != NULL) {
193 int cmp = PyObject_IsTrue(item);
194 Py_DECREF(item);
195 if (cmp < 0) {
196 Py_DECREF(it);
197 return NULL;
198 }
199 if (cmp == 0) {
200 Py_DECREF(it);
201 Py_RETURN_FALSE;
202 }
203 }
204 Py_DECREF(it);
205 if (PyErr_Occurred())
206 return NULL;
207 Py_RETURN_TRUE;
208}
209
210PyDoc_STRVAR(all_doc,
211"all(iterable) -> bool\n\
212\n\
213Return True if bool(x) is True for all values x in the iterable.");
214
215static PyObject *
216builtin_any(PyObject *self, PyObject *v)
217{
218 PyObject *it, *item;
219
220 it = PyObject_GetIter(v);
221 if (it == NULL)
222 return NULL;
223
224 while ((item = PyIter_Next(it)) != NULL) {
225 int cmp = PyObject_IsTrue(item);
226 Py_DECREF(item);
227 if (cmp < 0) {
228 Py_DECREF(it);
229 return NULL;
230 }
231 if (cmp == 1) {
232 Py_DECREF(it);
233 Py_RETURN_TRUE;
234 }
235 }
236 Py_DECREF(it);
237 if (PyErr_Occurred())
238 return NULL;
239 Py_RETURN_FALSE;
240}
241
242PyDoc_STRVAR(any_doc,
243"any(iterable) -> bool\n\
244\n\
245Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000246
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000247
Guido van Rossum79f25d91997-04-29 20:08:16 +0000248static PyObject *
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000249builtin_bin(PyObject *self, PyObject *v)
250{
251 return PyNumber_ToBase(v, 2);
252}
253
254PyDoc_STRVAR(bin_doc,
255"bin(number) -> string\n\
256\n\
257Return the binary representation of an integer or long integer.");
258
259
260static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000261builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000262{
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000263 PyObject *itertools, *ifilter, *result;
264 itertools = PyImport_ImportModule("itertools");
265 if (itertools == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000266 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000267 ifilter = PyObject_GetAttrString(itertools, "ifilter");
268 Py_DECREF(itertools);
269 if (ifilter == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000270 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000271 result = PyObject_Call(ifilter, args, NULL);
272 Py_DECREF(ifilter);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000273 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000274}
275
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000276PyDoc_STRVAR(filter_doc,
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000277"filter(predicate, iterable) -> iterator\n\
278\n\
279Return an iterator yielding only those elements of the input iterable\n\
280for which the predicate (a Boolean function) returns true.\n\
281If the predicate is None, 'lambda x: bool(x)' is assumed.\n\
282(This is identical to itertools.ifilter().)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000283
Eric Smith8c663262007-08-25 02:26:07 +0000284static PyObject *
285builtin_format(PyObject *self, PyObject *args)
286{
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000287 static PyObject * format_str = NULL;
288 PyObject *value;
289 PyObject *spec = NULL;
290 PyObject *meth;
291 PyObject *empty = NULL;
292 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000293
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000294 /* Initialize cached value */
295 if (format_str == NULL) {
296 /* Initialize static variable needed by _PyType_Lookup */
297 format_str = PyUnicode_FromString("__format__");
298 if (format_str == NULL)
299 goto done;
300 }
Eric Smith8c663262007-08-25 02:26:07 +0000301
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000302 if (!PyArg_ParseTuple(args, "O|U:format", &value, &spec))
303 goto done;
Eric Smith81936692007-08-31 01:14:01 +0000304
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000305 /* initialize the default value */
306 if (spec == NULL) {
307 empty = PyUnicode_FromUnicode(NULL, 0);
308 spec = empty;
309 }
Eric Smith8c663262007-08-25 02:26:07 +0000310
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000311 /* Make sure the type is initialized. float gets initialized late */
Christian Heimes90aa7642007-12-19 02:45:37 +0000312 if (Py_TYPE(value)->tp_dict == NULL)
313 if (PyType_Ready(Py_TYPE(value)) < 0)
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000314 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000315
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000316 /* Find the (unbound!) __format__ method (a borrowed reference) */
Christian Heimes90aa7642007-12-19 02:45:37 +0000317 meth = _PyType_Lookup(Py_TYPE(value), format_str);
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000318 if (meth == NULL) {
319 PyErr_Format(PyExc_TypeError,
320 "Type %.100s doesn't define __format__",
Christian Heimes90aa7642007-12-19 02:45:37 +0000321 Py_TYPE(value)->tp_name);
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000322 goto done;
323 }
Eric Smith8c663262007-08-25 02:26:07 +0000324
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000325 /* And call it, binding it to the value */
326 result = PyObject_CallFunctionObjArgs(meth, value, spec, NULL);
Eric Smith8c663262007-08-25 02:26:07 +0000327
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000328 if (result && !PyUnicode_Check(result)) {
329 PyErr_SetString(PyExc_TypeError,
330 "__format__ method did not return string");
331 Py_DECREF(result);
332 result = NULL;
333 goto done;
334 }
Eric Smith8c663262007-08-25 02:26:07 +0000335
Eric Smith81936692007-08-31 01:14:01 +0000336done:
Christian Heimes94b7d3d2007-12-11 20:20:39 +0000337 Py_XDECREF(empty);
338 return result;
Eric Smith8c663262007-08-25 02:26:07 +0000339}
340
Eric Smith8c663262007-08-25 02:26:07 +0000341PyDoc_STRVAR(format_doc,
Eric Smith81936692007-08-31 01:14:01 +0000342"format(value[, format_spec]) -> string\n\
Eric Smith8c663262007-08-25 02:26:07 +0000343\n\
Eric Smith81936692007-08-31 01:14:01 +0000344Returns value.__format__(format_spec)\n\
345format_spec defaults to \"\"");
346
Guido van Rossum7fcf2242007-05-04 17:43:11 +0000347static PyObject *
Walter Dörwalde7efd592007-06-05 20:07:21 +0000348builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000349{
350 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000351
Walter Dörwalde7efd592007-06-05 20:07:21 +0000352 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum09095f32000-03-10 23:00:52 +0000353 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000354
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000355 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000356}
357
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000358PyDoc_VAR(chr_doc) = PyDoc_STR(
Guido van Rossum84fc66d2007-05-03 17:18:26 +0000359"chr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000360\n\
Guido van Rossum8ac004e2007-07-15 13:00:05 +0000361Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000362)
Guido van Rossum8ac004e2007-07-15 13:00:05 +0000363#ifndef Py_UNICODE_WIDE
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000364PyDoc_STR(
Guido van Rossum8ac004e2007-07-15 13:00:05 +0000365"\nIf 0x10000 <= i, a surrogate pair is returned."
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000366)
Guido van Rossum8ac004e2007-07-15 13:00:05 +0000367#endif
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000368;
Guido van Rossum09095f32000-03-10 23:00:52 +0000369
370
371static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000372builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000373{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000375 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000376
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000377 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000378 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000379 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000380 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +0000381 return PyLong_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000382}
383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000384PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000385"cmp(x, y) -> integer\n\
386\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000388
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000389
390static char *
391source_as_string(PyObject *cmd)
392{
393 char *str;
394 Py_ssize_t size;
395
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000396 if (PyUnicode_Check(cmd)) {
397 cmd = _PyUnicode_AsDefaultEncodedString(cmd, NULL);
398 if (cmd == NULL)
399 return NULL;
400 }
Guido van Rossuma4b8d1d2007-08-27 15:02:28 +0000401 else if (!PyObject_CheckReadBuffer(cmd)) {
402 PyErr_SetString(PyExc_TypeError,
403 "eval()/exec() arg 1 must be a string, bytes or code object");
404 return NULL;
405 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000406 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) {
407 return NULL;
408 }
409 if (strlen(str) != size) {
410 PyErr_SetString(PyExc_TypeError,
411 "source code string cannot contain null bytes");
412 return NULL;
413 }
414 return str;
415}
416
Guido van Rossum79f25d91997-04-29 20:08:16 +0000417static PyObject *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000418builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000419{
420 char *str;
421 char *filename;
422 char *startstr;
423 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000424 int dont_inherit = 0;
425 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000426 PyCompilerFlags cf;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000427 PyObject *cmd;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000428 static char *kwlist[] = {"source", "filename", "mode", "flags",
429 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000430
Guido van Rossumd8faa362007-04-27 19:54:29 +0000431 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
432 kwlist, &cmd, &filename, &startstr,
433 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000434 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000435
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000436 cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000437
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000438 str = source_as_string(cmd);
439 if (str == NULL)
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000440 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000441
Guido van Rossum5b722181993-03-30 17:46:03 +0000442 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000443 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000444 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000445 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000446 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000447 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000448 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000449 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000450 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000451 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000452 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000453
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000454 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000455 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000456 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000457 PyErr_SetString(PyExc_ValueError,
458 "compile(): unrecognised flags");
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000459 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000460 }
461 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
462
Tim Peters6cd6a822001-08-17 22:11:27 +0000463 if (!dont_inherit) {
464 PyEval_MergeCompilerFlags(&cf);
465 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000466 return Py_CompileStringFlags(str, filename, start, &cf);
Guido van Rossum5b722181993-03-30 17:46:03 +0000467}
468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000469PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000470"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000471\n\
472Compile the source string (a Python module, statement or expression)\n\
Georg Brandl7cae87c2006-09-06 06:51:57 +0000473into a code object that can be executed by exec() or eval().\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000474The filename will be used for run-time error messages.\n\
475The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000476single (interactive) statement, or 'eval' to compile an expression.\n\
477The flags argument, if present, controls which future statements influence\n\
478the compilation of the code.\n\
479The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
480the effects of any future statements in effect in the code calling\n\
481compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000483
Guido van Rossum79f25d91997-04-29 20:08:16 +0000484static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000485builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000486{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000487 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000488
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000489 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000490 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000491 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000492}
493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000494PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000495"dir([object]) -> list of strings\n"
496"\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000497"If called without an argument, return the names in the current scope.\n"
498"Else, return an alphabetized list of names comprising (some of) the attributes\n"
499"of the given object, and of attributes reachable from it.\n"
500"If the object supplies a method named __dir__, it will be used; otherwise\n"
501"the default dir() logic is used and returns:\n"
502" for a module object: the module's attributes.\n"
503" for a class object: its attributes, and recursively the attributes\n"
504" of its bases.\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000505" for any other object: its attributes, its class's attributes, and\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000506" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000507
Guido van Rossum79f25d91997-04-29 20:08:16 +0000508static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000509builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000510{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000511 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000512
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000513 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000514 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000515 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000516}
517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000518PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000519"divmod(x, y) -> (div, mod)\n\
520\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000521Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000522
523
Guido van Rossum79f25d91997-04-29 20:08:16 +0000524static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000525builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000526{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000527 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000528 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000530 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000531
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000532 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000534 if (locals != Py_None && !PyMapping_Check(locals)) {
535 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000536 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000537 }
538 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000539 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000540 "globals must be a real dict; try eval(expr, {}, mapping)"
541 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000542 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000543 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 if (globals == Py_None) {
545 globals = PyEval_GetGlobals();
546 if (locals == Py_None)
547 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000548 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000550 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000551
Georg Brandl77c85e62005-09-15 10:46:13 +0000552 if (globals == NULL || locals == NULL) {
553 PyErr_SetString(PyExc_TypeError,
554 "eval must be given globals and locals "
555 "when called without a frame");
556 return NULL;
557 }
558
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
560 if (PyDict_SetItemString(globals, "__builtins__",
561 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000562 return NULL;
563 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000564
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000565 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000566 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000567 PyErr_SetString(PyExc_TypeError,
568 "code object passed to eval() may not contain free variables");
569 return NULL;
570 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000572 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000573
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000574 str = source_as_string(cmd);
575 if (str == NULL)
Guido van Rossum94390a41992-08-14 15:14:30 +0000576 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000577
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000578 while (*str == ' ' || *str == '\t')
579 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000580
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000581 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Tim Peters9fa96be2001-08-17 23:04:59 +0000582 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000583 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
584 Py_XDECREF(tmp);
585 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586}
587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000588PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000589"eval(source[, globals[, locals]]) -> value\n\
590\n\
591Evaluate the source in the context of globals and locals.\n\
592The source may be a string representing a Python expression\n\
593or a code object as returned by compile().\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000594The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000595defaulting to the current globals and locals.\n\
596If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000597
Georg Brandl7cae87c2006-09-06 06:51:57 +0000598static PyObject *
599builtin_exec(PyObject *self, PyObject *args)
600{
601 PyObject *v;
602 PyObject *prog, *globals = Py_None, *locals = Py_None;
603 int plain = 0;
604
605 if (!PyArg_ParseTuple(args, "O|OO:exec", &prog, &globals, &locals))
606 return NULL;
607
608 if (globals == Py_None) {
609 globals = PyEval_GetGlobals();
610 if (locals == Py_None) {
611 locals = PyEval_GetLocals();
612 plain = 1;
613 }
614 if (!globals || !locals) {
615 PyErr_SetString(PyExc_SystemError,
616 "globals and locals cannot be NULL");
617 return NULL;
618 }
619 }
620 else if (locals == Py_None)
621 locals = globals;
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000622 if (!PyUnicode_Check(prog) &&
Guido van Rossumda5b8f22007-06-12 23:30:11 +0000623 !PyCode_Check(prog)) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000624 PyErr_Format(PyExc_TypeError,
625 "exec() arg 1 must be a string, file, or code "
626 "object, not %.100s", prog->ob_type->tp_name);
627 return NULL;
628 }
629 if (!PyDict_Check(globals)) {
630 PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
631 globals->ob_type->tp_name);
632 return NULL;
633 }
634 if (!PyMapping_Check(locals)) {
635 PyErr_Format(PyExc_TypeError,
636 "arg 3 must be a mapping or None, not %.100s",
637 locals->ob_type->tp_name);
638 return NULL;
639 }
640 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
641 if (PyDict_SetItemString(globals, "__builtins__",
642 PyEval_GetBuiltins()) != 0)
643 return NULL;
644 }
645
646 if (PyCode_Check(prog)) {
647 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
648 PyErr_SetString(PyExc_TypeError,
649 "code object passed to exec() may not "
650 "contain free variables");
651 return NULL;
652 }
653 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
654 }
Georg Brandl7cae87c2006-09-06 06:51:57 +0000655 else {
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000656 char *str = source_as_string(prog);
Georg Brandl7cae87c2006-09-06 06:51:57 +0000657 PyCompilerFlags cf;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000658 if (str == NULL)
Georg Brandl7cae87c2006-09-06 06:51:57 +0000659 return NULL;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000660 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Georg Brandl7cae87c2006-09-06 06:51:57 +0000661 if (PyEval_MergeCompilerFlags(&cf))
662 v = PyRun_StringFlags(str, Py_file_input, globals,
663 locals, &cf);
664 else
665 v = PyRun_String(str, Py_file_input, globals, locals);
Georg Brandl7cae87c2006-09-06 06:51:57 +0000666 }
667 if (v == NULL)
668 return NULL;
669 Py_DECREF(v);
670 Py_RETURN_NONE;
671}
672
673PyDoc_STRVAR(exec_doc,
674"exec(object[, globals[, locals]])\n\
675\n\
676Read and execute code from a object, which can be a string, a code\n\
677object or a file object.\n\
678The globals and locals are dictionaries, defaulting to the current\n\
679globals and locals. If only globals is given, locals defaults to it.");
680
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000681
Guido van Rossum79f25d91997-04-29 20:08:16 +0000682static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000683builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000684{
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000685 PyObject *v, *result, *dflt = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000687
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000688 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000689 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000690
Martin v. Löwis5b222132007-06-10 09:51:05 +0000691 if (!PyUnicode_Check(name)) {
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000692 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000693 "getattr(): attribute name must be string");
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000694 return NULL;
695 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000696 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000697 if (result == NULL && dflt != NULL &&
698 PyErr_ExceptionMatches(PyExc_AttributeError))
699 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000700 PyErr_Clear();
701 Py_INCREF(dflt);
702 result = dflt;
703 }
704 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000705}
706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000707PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000708"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000709\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000710Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
711When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000712exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000713
714
Guido van Rossum79f25d91997-04-29 20:08:16 +0000715static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000716builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000717{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000718 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000719
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 d = PyEval_GetGlobals();
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000721 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000722 return d;
723}
724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000725PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000726"globals() -> dictionary\n\
727\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000728Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000729
730
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000732builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000733{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000734 PyObject *v;
735 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000736
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000737 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000738 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000739 if (!PyUnicode_Check(name)) {
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000740 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000741 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000742 return NULL;
743 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000744 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000745 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000746 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000747 Py_INCREF(Py_False);
748 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000749 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000751 Py_INCREF(Py_True);
752 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000753}
754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000755PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000756"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000757\n\
758Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000759(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000760
761
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000763builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000764{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000765 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000766}
767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000768PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000769"id(object) -> integer\n\
770\n\
771Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000772simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000773
774
Guido van Rossum79f25d91997-04-29 20:08:16 +0000775static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000776builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000777{
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000778 PyObject *itertools, *imap, *result;
779 itertools = PyImport_ImportModule("itertools");
780 if (itertools == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000781 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000782 imap = PyObject_GetAttrString(itertools, "imap");
783 Py_DECREF(itertools);
784 if (imap == NULL)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000785 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000786 result = PyObject_Call(imap, args, NULL);
787 Py_DECREF(imap);
Tim Peters4e9afdc2001-05-03 23:54:49 +0000788 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000789}
790
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000791PyDoc_STRVAR(map_doc,
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000792"map(function, iterable[, iterable, ...]) -> iterator\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000793\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000794Return an iterator yielding the results of applying the function to the\n\
795items of the argument iterables(s). If more than one iterable is given,\n\
796the function is called with an argument list consisting of the\n\
797corresponding item of each iterable, until an iterable is exhausted.\n\
798If the function is None, 'lambda *a: a' is assumed.\n\
799(This is identical to itertools.imap().)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000800
801
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802static PyObject *
Georg Brandla18af4e2007-04-21 15:47:16 +0000803builtin_next(PyObject *self, PyObject *args)
804{
805 PyObject *it, *res;
806 PyObject *def = NULL;
807
808 if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
809 return NULL;
810 if (!PyIter_Check(it)) {
811 PyErr_Format(PyExc_TypeError,
812 "%.200s object is not an iterator", it->ob_type->tp_name);
813 return NULL;
814 }
815
816 res = (*it->ob_type->tp_iternext)(it);
817 if (res == NULL) {
818 if (def) {
819 if (PyErr_Occurred() &&
820 !PyErr_ExceptionMatches(PyExc_StopIteration))
821 return NULL;
822 PyErr_Clear();
823 Py_INCREF(def);
824 return def;
825 } else if (PyErr_Occurred()) {
826 return NULL;
827 } else {
828 PyErr_SetNone(PyExc_StopIteration);
829 return NULL;
830 }
831 }
832 return res;
833}
834
835PyDoc_STRVAR(next_doc,
836"next(iterator[, default])\n\
837\n\
838Return the next item from the iterator. If default is given and the iterator\n\
839is exhausted, it is returned instead of raising StopIteration.");
840
841
842static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000843builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000844{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 PyObject *v;
846 PyObject *name;
847 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000848
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000849 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000850 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000852 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000853 Py_INCREF(Py_None);
854 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000855}
856
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000857PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000858"setattr(object, name, value)\n\
859\n\
860Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000861``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000862
863
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000865builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000866{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyObject *v;
868 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000869
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000870 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000871 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000872 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000873 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874 Py_INCREF(Py_None);
875 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000876}
877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000878PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +0000879"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000880\n\
881Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000882``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000883
884
Guido van Rossum79f25d91997-04-29 20:08:16 +0000885static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000886builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000887{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000888 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000889
Guido van Rossum79f25d91997-04-29 20:08:16 +0000890 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000891 if (x == -1)
892 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +0000893 return PyLong_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000894}
895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000896PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000897"hash(object) -> integer\n\
898\n\
899Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000900the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000901
902
Guido van Rossum79f25d91997-04-29 20:08:16 +0000903static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000904builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000905{
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000906 return PyNumber_ToBase(v, 16);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000907}
908
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000909PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000910"hex(number) -> string\n\
911\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000912Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000913
914
Guido van Rossum79f25d91997-04-29 20:08:16 +0000915static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000916builtin_iter(PyObject *self, PyObject *args)
917{
918 PyObject *v, *w = NULL;
919
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000920 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000921 return NULL;
922 if (w == NULL)
923 return PyObject_GetIter(v);
924 if (!PyCallable_Check(v)) {
925 PyErr_SetString(PyExc_TypeError,
926 "iter(v, w): v must be callable");
927 return NULL;
928 }
929 return PyCallIter_New(v, w);
930}
931
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000932PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000933"iter(collection) -> iterator\n\
934iter(callable, sentinel) -> iterator\n\
935\n\
936Get an iterator from an object. In the first form, the argument must\n\
937supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000938In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000939
940
941static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000942builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000943{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000944 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000945
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000946 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +0000947 if (res < 0 && PyErr_Occurred())
948 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +0000949 return PyLong_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000950}
951
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000952PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000953"len(object) -> integer\n\
954\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000955Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000956
957
Guido van Rossum79f25d91997-04-29 20:08:16 +0000958static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000959builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000960{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000961 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000962
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963 d = PyEval_GetLocals();
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000964 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000965 return d;
966}
967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000968PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000969"locals() -> dictionary\n\
970\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +0000971Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000972
973
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000975min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000976{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000977 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +0000978 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000979
Guido van Rossum79f25d91997-04-29 20:08:16 +0000980 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000981 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +0000982 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000983 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +0000984
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000985 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
986 keyfunc = PyDict_GetItemString(kwds, "key");
987 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000988 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000989 "%s() got an unexpected keyword argument", name);
990 return NULL;
991 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000992 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000993
Tim Petersc3074532001-05-03 07:00:32 +0000994 it = PyObject_GetIter(v);
995 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000996 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +0000997
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000998 maxitem = NULL; /* the result */
999 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +00001000 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001001 /* get the value from the key function */
1002 if (keyfunc != NULL) {
1003 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1004 if (val == NULL)
1005 goto Fail_it_item;
1006 }
1007 /* no key function; the value is the item */
1008 else {
1009 val = item;
1010 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001011 }
Tim Petersc3074532001-05-03 07:00:32 +00001012
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001013 /* maximum value and item are unset; set them */
1014 if (maxval == NULL) {
1015 maxitem = item;
1016 maxval = val;
1017 }
1018 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +00001019 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001020 int cmp = PyObject_RichCompareBool(val, maxval, op);
1021 if (cmp < 0)
1022 goto Fail_it_item_and_val;
1023 else if (cmp > 0) {
1024 Py_DECREF(maxval);
1025 Py_DECREF(maxitem);
1026 maxval = val;
1027 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +00001028 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001029 else {
1030 Py_DECREF(item);
1031 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001032 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001033 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001034 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001035 if (PyErr_Occurred())
1036 goto Fail_it;
1037 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001038 PyErr_Format(PyExc_ValueError,
1039 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001040 assert(maxitem == NULL);
1041 }
1042 else
1043 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001044 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001045 return maxitem;
1046
1047Fail_it_item_and_val:
1048 Py_DECREF(val);
1049Fail_it_item:
1050 Py_DECREF(item);
1051Fail_it:
1052 Py_XDECREF(maxval);
1053 Py_XDECREF(maxitem);
1054 Py_DECREF(it);
1055 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001056}
1057
Guido van Rossum79f25d91997-04-29 20:08:16 +00001058static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001059builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001060{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001061 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001062}
1063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001064PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001065"min(iterable[, key=func]) -> value\n\
1066min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001067\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001068With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001069With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001070
1071
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001073builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001074{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001075 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001076}
1077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001078PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001079"max(iterable[, key=func]) -> value\n\
1080max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001081\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001082With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001083With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001084
1085
Guido van Rossum79f25d91997-04-29 20:08:16 +00001086static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001087builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001088{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001089 return PyNumber_ToBase(v, 8);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001090}
1091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001092PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001093"oct(number) -> string\n\
1094\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001095Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001096
1097
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001099builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001100{
Guido van Rossum09095f32000-03-10 23:00:52 +00001101 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001102 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001103
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001104 if (PyString_Check(obj)) {
1105 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001106 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001107 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Christian Heimes217cfd12007-12-02 14:31:20 +00001108 return PyLong_FromLong(ord);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001109 }
Guido van Rossum98f97462007-04-13 03:31:13 +00001110 }
1111 else if (PyUnicode_Check(obj)) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001112 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001113 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001114 ord = (long)*PyUnicode_AS_UNICODE(obj);
Christian Heimes217cfd12007-12-02 14:31:20 +00001115 return PyLong_FromLong(ord);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001116 }
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001117#ifndef Py_UNICODE_WIDE
1118 if (size == 2) {
1119 /* Decode a valid surrogate pair */
1120 int c0 = PyUnicode_AS_UNICODE(obj)[0];
1121 int c1 = PyUnicode_AS_UNICODE(obj)[1];
1122 if (0xD800 <= c0 && c0 <= 0xDBFF &&
1123 0xDC00 <= c1 && c1 <= 0xDFFF) {
1124 ord = ((((c0 & 0x03FF) << 10) | (c1 & 0x03FF)) +
1125 0x00010000);
Christian Heimes217cfd12007-12-02 14:31:20 +00001126 return PyLong_FromLong(ord);
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001127 }
1128 }
1129#endif
Guido van Rossum6f376c42007-05-24 14:31:33 +00001130 }
Guido van Rossum98f97462007-04-13 03:31:13 +00001131 else if (PyBytes_Check(obj)) {
1132 /* XXX Hopefully this is temporary */
1133 size = PyBytes_GET_SIZE(obj);
1134 if (size == 1) {
Guido van Rossumf9e91c92007-05-08 21:05:48 +00001135 ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
Christian Heimes217cfd12007-12-02 14:31:20 +00001136 return PyLong_FromLong(ord);
Guido van Rossum98f97462007-04-13 03:31:13 +00001137 }
1138 }
1139 else {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001140 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001141 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001142 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001143 return NULL;
1144 }
1145
Guido van Rossumad991772001-01-12 16:03:05 +00001146 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001147 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001148 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001149 size);
1150 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001151}
1152
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001153PyDoc_VAR(ord_doc) = PyDoc_STR(
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001154"ord(c) -> integer\n\
1155\n\
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001156Return the integer ordinal of a one-character string."
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001157)
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001158#ifndef Py_UNICODE_WIDE
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001159PyDoc_STR(
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001160"\nA valid surrogate pair is also accepted."
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001161)
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001162#endif
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001163;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001164
1165
Guido van Rossum79f25d91997-04-29 20:08:16 +00001166static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001167builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001168{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001169 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001170
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001171 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001172 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001173 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001174}
1175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001176PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001177"pow(x, y[, z]) -> number\n\
1178\n\
1179With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001180equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001181
1182
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001183
Guido van Rossum34343512006-11-30 22:13:52 +00001184static PyObject *
1185builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1186{
1187 static char *kwlist[] = {"sep", "end", "file", 0};
Georg Brandl257d3d92007-02-26 10:35:10 +00001188 static PyObject *dummy_args;
Guido van Rossum34343512006-11-30 22:13:52 +00001189 PyObject *sep = NULL, *end = NULL, *file = NULL;
1190 int i, err;
1191
Georg Brandl257d3d92007-02-26 10:35:10 +00001192 if (dummy_args == NULL) {
1193 if (!(dummy_args = PyTuple_New(0)))
1194 return NULL;
1195 }
Georg Brandl88fc6642007-02-09 21:28:07 +00001196 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
Guido van Rossum34343512006-11-30 22:13:52 +00001197 kwlist, &sep, &end, &file))
Christian Heimesfef9bba2007-11-13 15:24:14 +00001198 return NULL;
1199 if (file == NULL || file == Py_None) {
Guido van Rossum34343512006-11-30 22:13:52 +00001200 file = PySys_GetObject("stdout");
Christian Heimesfef9bba2007-11-13 15:24:14 +00001201 /* sys.stdout may be None when FILE* stdout isn't connected */
1202 if (file == Py_None)
1203 Py_RETURN_NONE;
1204 }
Guido van Rossum34343512006-11-30 22:13:52 +00001205
Neal Norwitz6ea45d32007-08-26 04:19:43 +00001206 if (sep && sep != Py_None && !PyUnicode_Check(sep)) {
Georg Brandl16f3e032006-11-30 22:46:03 +00001207 PyErr_Format(PyExc_TypeError,
Neal Norwitz6ea45d32007-08-26 04:19:43 +00001208 "sep must be None or a string, not %.200s",
Georg Brandl16f3e032006-11-30 22:46:03 +00001209 sep->ob_type->tp_name);
1210 return NULL;
1211 }
Neal Norwitz6ea45d32007-08-26 04:19:43 +00001212 if (end && end != Py_None && !PyUnicode_Check(end)) {
Georg Brandl16f3e032006-11-30 22:46:03 +00001213 PyErr_Format(PyExc_TypeError,
Neal Norwitz6ea45d32007-08-26 04:19:43 +00001214 "end must be None or a string, not %.200s",
Georg Brandl16f3e032006-11-30 22:46:03 +00001215 end->ob_type->tp_name);
1216 return NULL;
1217 }
Guido van Rossum34343512006-11-30 22:13:52 +00001218
1219 for (i = 0; i < PyTuple_Size(args); i++) {
1220 if (i > 0) {
1221 if (sep == NULL || sep == Py_None)
1222 err = PyFile_WriteString(" ", file);
1223 else
1224 err = PyFile_WriteObject(sep, file,
1225 Py_PRINT_RAW);
1226 if (err)
1227 return NULL;
1228 }
1229 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1230 Py_PRINT_RAW);
1231 if (err)
1232 return NULL;
1233 }
1234
1235 if (end == NULL || end == Py_None)
1236 err = PyFile_WriteString("\n", file);
1237 else
1238 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1239 if (err)
1240 return NULL;
1241
1242 Py_RETURN_NONE;
1243}
1244
1245PyDoc_STRVAR(print_doc,
Guido van Rossum452bf512007-02-09 05:32:43 +00001246"print(value, ..., file=None, sep=' ', end='\\n')\n\
Guido van Rossum34343512006-11-30 22:13:52 +00001247\n\
1248Prints the values to a stream, or to sys.stdout by default.\n\
1249Optional keyword arguments:\n\
1250file: a file-like object (stream); defaults to the current sys.stdout.\n\
1251sep: string inserted between values, default a space.\n\
1252end: string appended after the last value, default a newline.");
1253
1254
Guido van Rossuma88a0332007-02-26 16:59:55 +00001255static PyObject *
1256builtin_input(PyObject *self, PyObject *args)
1257{
Guido van Rossumeba76962007-05-27 09:13:28 +00001258 PyObject *promptarg = NULL;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001259 PyObject *fin = PySys_GetObject("stdin");
1260 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossumeba76962007-05-27 09:13:28 +00001261 PyObject *ferr = PySys_GetObject("stderr");
1262 PyObject *tmp;
1263 long fd;
1264 int tty;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001265
Guido van Rossumeba76962007-05-27 09:13:28 +00001266 /* Parse arguments */
1267 if (!PyArg_UnpackTuple(args, "input", 0, 1, &promptarg))
Guido van Rossuma88a0332007-02-26 16:59:55 +00001268 return NULL;
1269
Guido van Rossumeba76962007-05-27 09:13:28 +00001270 /* Check that stdin/out/err are intact */
Christian Heimes2be03732007-11-15 02:26:46 +00001271 if (fin == NULL || fin == Py_None) {
Guido van Rossumeba76962007-05-27 09:13:28 +00001272 PyErr_SetString(PyExc_RuntimeError,
1273 "input(): lost sys.stdin");
Guido van Rossuma88a0332007-02-26 16:59:55 +00001274 return NULL;
1275 }
Christian Heimes2be03732007-11-15 02:26:46 +00001276 if (fout == NULL || fout == Py_None) {
Guido van Rossumeba76962007-05-27 09:13:28 +00001277 PyErr_SetString(PyExc_RuntimeError,
1278 "input(): lost sys.stdout");
Guido van Rossuma88a0332007-02-26 16:59:55 +00001279 return NULL;
1280 }
Christian Heimes2be03732007-11-15 02:26:46 +00001281 if (ferr == NULL || ferr == Py_None) {
Guido van Rossumeba76962007-05-27 09:13:28 +00001282 PyErr_SetString(PyExc_RuntimeError,
1283 "input(): lost sys.stderr");
1284 return NULL;
1285 }
1286
1287 /* First of all, flush stderr */
1288 tmp = PyObject_CallMethod(ferr, "flush", "");
1289 if (tmp == NULL)
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001290 PyErr_Clear();
1291 else
1292 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001293
1294 /* We should only use (GNU) readline if Python's sys.stdin and
1295 sys.stdout are the same as C's stdin and stdout, because we
1296 need to pass it those. */
1297 tmp = PyObject_CallMethod(fin, "fileno", "");
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001298 if (tmp == NULL) {
1299 PyErr_Clear();
1300 tty = 0;
1301 }
1302 else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001303 fd = PyLong_AsLong(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001304 Py_DECREF(tmp);
1305 if (fd < 0 && PyErr_Occurred())
1306 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001307 tty = fd == fileno(stdin) && isatty(fd);
1308 }
1309 if (tty) {
1310 tmp = PyObject_CallMethod(fout, "fileno", "");
1311 if (tmp == NULL)
1312 PyErr_Clear();
1313 else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001314 fd = PyLong_AsLong(tmp);
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001315 Py_DECREF(tmp);
1316 if (fd < 0 && PyErr_Occurred())
1317 return NULL;
1318 tty = fd == fileno(stdout) && isatty(fd);
1319 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001320 }
1321
1322 /* If we're interactive, use (GNU) readline */
1323 if (tty) {
Guido van Rossuma88a0332007-02-26 16:59:55 +00001324 PyObject *po;
1325 char *prompt;
1326 char *s;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00001327 PyObject *stdin_encoding;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001328 PyObject *result;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00001329
1330 stdin_encoding = PyObject_GetAttrString(fin, "encoding");
1331 if (!stdin_encoding)
1332 /* stdin is a text stream, so it must have an
1333 encoding. */
1334 return NULL;
Guido van Rossumeba76962007-05-27 09:13:28 +00001335 tmp = PyObject_CallMethod(fout, "flush", "");
1336 if (tmp == NULL)
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001337 PyErr_Clear();
1338 else
1339 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001340 if (promptarg != NULL) {
1341 po = PyObject_Str(promptarg);
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00001342 if (po == NULL) {
1343 Py_DECREF(stdin_encoding);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001344 return NULL;
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00001345 }
Christian Heimes91c77302007-11-25 09:18:01 +00001346 prompt = PyUnicode_AsString(po);
Guido van Rossumeba76962007-05-27 09:13:28 +00001347 if (prompt == NULL) {
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00001348 Py_DECREF(stdin_encoding);
Guido van Rossumeba76962007-05-27 09:13:28 +00001349 Py_DECREF(po);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001350 return NULL;
Guido van Rossumeba76962007-05-27 09:13:28 +00001351 }
Guido van Rossuma88a0332007-02-26 16:59:55 +00001352 }
1353 else {
1354 po = NULL;
1355 prompt = "";
1356 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001357 s = PyOS_Readline(stdin, stdout, prompt);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001358 Py_XDECREF(po);
1359 if (s == NULL) {
1360 if (!PyErr_Occurred())
1361 PyErr_SetNone(PyExc_KeyboardInterrupt);
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00001362 Py_DECREF(stdin_encoding);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001363 return NULL;
1364 }
1365 if (*s == '\0') {
1366 PyErr_SetNone(PyExc_EOFError);
1367 result = NULL;
1368 }
1369 else { /* strip trailing '\n' */
1370 size_t len = strlen(s);
1371 if (len > PY_SSIZE_T_MAX) {
1372 PyErr_SetString(PyExc_OverflowError,
1373 "input: input too long");
1374 result = NULL;
1375 }
1376 else {
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00001377 result = PyUnicode_Decode
1378 (s, len-1,
1379 PyUnicode_AsString(stdin_encoding),
1380 NULL);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001381 }
1382 }
Martin v. Löwis4a7b5d52007-09-04 05:24:49 +00001383 Py_DECREF(stdin_encoding);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001384 PyMem_FREE(s);
1385 return result;
1386 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001387
1388 /* Fallback if we're not interactive */
1389 if (promptarg != NULL) {
1390 if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0)
Guido van Rossuma88a0332007-02-26 16:59:55 +00001391 return NULL;
1392 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001393 tmp = PyObject_CallMethod(fout, "flush", "");
1394 if (tmp == NULL)
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001395 PyErr_Clear();
1396 else
1397 Py_DECREF(tmp);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001398 return PyFile_GetLine(fin, -1);
1399}
1400
1401PyDoc_STRVAR(input_doc,
1402"input([prompt]) -> string\n\
1403\n\
1404Read a string from standard input. The trailing newline is stripped.\n\
1405If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1406On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1407is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001408
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001409
Guido van Rossum79f25d91997-04-29 20:08:16 +00001410static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001411builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001412{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001413 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001414}
1415
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001416PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001417"repr(object) -> string\n\
1418\n\
1419Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001420For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001421
1422
Guido van Rossum79f25d91997-04-29 20:08:16 +00001423static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001424builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001425{
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001426#define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
1427 static PyObject *round_str = NULL;
1428 int ndigits = UNDEF_NDIGITS;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001429 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001430 PyObject *number, *round;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001431
Alex Martelliae211f92007-08-22 23:21:33 +00001432 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:round",
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001433 kwlist, &number, &ndigits))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001434 return NULL;
Alex Martelliae211f92007-08-22 23:21:33 +00001435
Christian Heimes90aa7642007-12-19 02:45:37 +00001436 if (Py_TYPE(number)->tp_dict == NULL) {
1437 if (PyType_Ready(Py_TYPE(number)) < 0)
Guido van Rossum15d3d042007-08-24 02:02:45 +00001438 return NULL;
1439 }
1440
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001441 if (round_str == NULL) {
1442 round_str = PyUnicode_FromString("__round__");
1443 if (round_str == NULL)
Alex Martelliae211f92007-08-22 23:21:33 +00001444 return NULL;
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001445 }
1446
Christian Heimes90aa7642007-12-19 02:45:37 +00001447 round = _PyType_Lookup(Py_TYPE(number), round_str);
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001448 if (round == NULL) {
1449 PyErr_Format(PyExc_TypeError,
1450 "type %.100s doesn't define __round__ method",
Christian Heimes90aa7642007-12-19 02:45:37 +00001451 Py_TYPE(number)->tp_name);
Alex Martelliae211f92007-08-22 23:21:33 +00001452 return NULL;
1453 }
1454
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001455 if (ndigits == UNDEF_NDIGITS)
1456 return PyObject_CallFunction(round, "O", number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001457 else
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001458 return PyObject_CallFunction(round, "Oi", number, ndigits);
1459#undef UNDEF_NDIGITS
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001460}
1461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001462PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001463"round(number[, ndigits]) -> floating point number\n\
1464\n\
1465Round a number to a given precision in decimal digits (default 0 digits).\n\
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001466This returns an int when called with one argument, otherwise a float.\n\
1467Precision may be negative.");
1468
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001469
Raymond Hettinger64958a12003-12-17 20:43:33 +00001470static PyObject *
1471builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1472{
1473 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1474 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001475 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001476 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001477
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001478 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001479 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1480 kwlist, &seq, &compare, &keyfunc, &reverse))
1481 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001482
1483 newlist = PySequence_List(seq);
1484 if (newlist == NULL)
1485 return NULL;
1486
1487 callable = PyObject_GetAttrString(newlist, "sort");
1488 if (callable == NULL) {
1489 Py_DECREF(newlist);
1490 return NULL;
1491 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001492
Raymond Hettinger64958a12003-12-17 20:43:33 +00001493 newargs = PyTuple_GetSlice(args, 1, 4);
1494 if (newargs == NULL) {
1495 Py_DECREF(newlist);
1496 Py_DECREF(callable);
1497 return NULL;
1498 }
1499
1500 v = PyObject_Call(callable, newargs, kwds);
1501 Py_DECREF(newargs);
1502 Py_DECREF(callable);
1503 if (v == NULL) {
1504 Py_DECREF(newlist);
1505 return NULL;
1506 }
1507 Py_DECREF(v);
1508 return newlist;
1509}
1510
1511PyDoc_STRVAR(sorted_doc,
1512"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001513
Guido van Rossum79f25d91997-04-29 20:08:16 +00001514static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001515builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001516{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517 PyObject *v = NULL;
1518 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001519
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001520 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001521 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001522 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001523 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001524 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001525 if (!PyErr_Occurred())
1526 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001527 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001528 }
1529 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001530 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001531 }
1532 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001534 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001535 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001536 "vars() argument must have __dict__ attribute");
1537 return NULL;
1538 }
1539 }
1540 return d;
1541}
1542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001543PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001544"vars([object]) -> dictionary\n\
1545\n\
1546Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001547With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001548
Alex Martelli86d8b342007-08-22 22:39:42 +00001549static PyObject *
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001550builtin_trunc(PyObject *self, PyObject *number)
Alex Martelli86d8b342007-08-22 22:39:42 +00001551{
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001552 static PyObject *trunc_str = NULL;
1553 PyObject *trunc;
1554
Christian Heimes90aa7642007-12-19 02:45:37 +00001555 if (Py_TYPE(number)->tp_dict == NULL) {
1556 if (PyType_Ready(Py_TYPE(number)) < 0)
Guido van Rossum15d3d042007-08-24 02:02:45 +00001557 return NULL;
1558 }
1559
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001560 if (trunc_str == NULL) {
1561 trunc_str = PyUnicode_FromString("__trunc__");
1562 if (trunc_str == NULL)
1563 return NULL;
1564 }
1565
Christian Heimes90aa7642007-12-19 02:45:37 +00001566 trunc = _PyType_Lookup(Py_TYPE(number), trunc_str);
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001567 if (trunc == NULL) {
1568 PyErr_Format(PyExc_TypeError,
1569 "type %.100s doesn't define __trunc__ method",
Christian Heimes90aa7642007-12-19 02:45:37 +00001570 Py_TYPE(number)->tp_name);
Alex Martelli86d8b342007-08-22 22:39:42 +00001571 return NULL;
1572 }
Guido van Rossum2fa33db2007-08-23 22:07:24 +00001573 return PyObject_CallFunction(trunc, "O", number);
Alex Martelli86d8b342007-08-22 22:39:42 +00001574}
1575
1576PyDoc_STRVAR(trunc_doc,
1577"trunc(Real) -> Integral\n\
1578\n\
1579returns the integral closest to x between 0 and x.");
1580
1581
Alex Martellia70b1912003-04-22 08:12:33 +00001582
1583static PyObject*
1584builtin_sum(PyObject *self, PyObject *args)
1585{
1586 PyObject *seq;
1587 PyObject *result = NULL;
1588 PyObject *temp, *item, *iter;
1589
Raymond Hettinger5cf63942003-10-25 06:41:37 +00001590 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00001591 return NULL;
1592
1593 iter = PyObject_GetIter(seq);
1594 if (iter == NULL)
1595 return NULL;
1596
1597 if (result == NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +00001598 result = PyLong_FromLong(0);
Alex Martellia70b1912003-04-22 08:12:33 +00001599 if (result == NULL) {
1600 Py_DECREF(iter);
1601 return NULL;
1602 }
1603 } else {
1604 /* reject string values for 'start' parameter */
Guido van Rossum3172c5d2007-10-16 18:12:55 +00001605 if (PyUnicode_Check(result)) {
Alex Martellia70b1912003-04-22 08:12:33 +00001606 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001607 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00001608 Py_DECREF(iter);
1609 return NULL;
1610 }
Guido van Rossum3172c5d2007-10-16 18:12:55 +00001611 if (PyBytes_Check(result)) {
1612 PyErr_SetString(PyExc_TypeError,
1613 "sum() can't sum bytes [use b''.join(seq) instead]");
1614 Py_DECREF(iter);
1615 return NULL;
1616 }
1617
Alex Martelli41c9f882003-04-22 09:24:48 +00001618 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00001619 }
1620
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001621#ifndef SLOW_SUM
1622 /* Fast addition by keeping temporary sums in C instead of new Python objects.
1623 Assumes all inputs are the same type. If the assumption fails, default
1624 to the more general routine.
1625 */
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001626 if (PyLong_CheckExact(result)) {
1627 int overflow;
1628 long i_result = PyLong_AsLongAndOverflow(result, &overflow);
1629 /* If this already overflowed, don't even enter the loop. */
1630 if (overflow == 0) {
1631 Py_DECREF(result);
1632 result = NULL;
1633 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001634 while(result == NULL) {
1635 item = PyIter_Next(iter);
1636 if (item == NULL) {
1637 Py_DECREF(iter);
1638 if (PyErr_Occurred())
1639 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +00001640 return PyLong_FromLong(i_result);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001641 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001642 if (PyLong_CheckExact(item)) {
1643 long b = PyLong_AsLongAndOverflow(item, &overflow);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001644 long x = i_result + b;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001645 if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001646 i_result = x;
1647 Py_DECREF(item);
1648 continue;
1649 }
1650 }
1651 /* Either overflowed or is not an int. Restore real objects and process normally */
Christian Heimes217cfd12007-12-02 14:31:20 +00001652 result = PyLong_FromLong(i_result);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001653 temp = PyNumber_Add(result, item);
1654 Py_DECREF(result);
1655 Py_DECREF(item);
1656 result = temp;
1657 if (result == NULL) {
1658 Py_DECREF(iter);
1659 return NULL;
1660 }
1661 }
1662 }
1663
1664 if (PyFloat_CheckExact(result)) {
1665 double f_result = PyFloat_AS_DOUBLE(result);
1666 Py_DECREF(result);
1667 result = NULL;
1668 while(result == NULL) {
1669 item = PyIter_Next(iter);
1670 if (item == NULL) {
1671 Py_DECREF(iter);
1672 if (PyErr_Occurred())
1673 return NULL;
1674 return PyFloat_FromDouble(f_result);
1675 }
1676 if (PyFloat_CheckExact(item)) {
1677 PyFPE_START_PROTECT("add", return 0)
1678 f_result += PyFloat_AS_DOUBLE(item);
1679 PyFPE_END_PROTECT(f_result)
1680 Py_DECREF(item);
1681 continue;
1682 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001683 if (PyLong_CheckExact(item)) {
1684 long value;
1685 int overflow;
1686 value = PyLong_AsLongAndOverflow(item, &overflow);
1687 if (!overflow) {
1688 PyFPE_START_PROTECT("add", return 0)
1689 f_result += (double)value;
1690 PyFPE_END_PROTECT(f_result)
1691 Py_DECREF(item);
1692 continue;
1693 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001694 }
1695 result = PyFloat_FromDouble(f_result);
1696 temp = PyNumber_Add(result, item);
1697 Py_DECREF(result);
1698 Py_DECREF(item);
1699 result = temp;
1700 if (result == NULL) {
1701 Py_DECREF(iter);
1702 return NULL;
1703 }
1704 }
1705 }
1706#endif
1707
Alex Martellia70b1912003-04-22 08:12:33 +00001708 for(;;) {
1709 item = PyIter_Next(iter);
1710 if (item == NULL) {
1711 /* error, or end-of-sequence */
1712 if (PyErr_Occurred()) {
1713 Py_DECREF(result);
1714 result = NULL;
1715 }
1716 break;
1717 }
Alex Martellia253e182003-10-25 23:24:14 +00001718 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00001719 Py_DECREF(result);
1720 Py_DECREF(item);
1721 result = temp;
1722 if (result == NULL)
1723 break;
1724 }
1725 Py_DECREF(iter);
1726 return result;
1727}
1728
1729PyDoc_STRVAR(sum_doc,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001730"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00001731\n\
1732Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +00001733of parameter 'start' (which defaults to 0). When the sequence is\n\
1734empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00001735
1736
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001737static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001738builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001739{
1740 PyObject *inst;
1741 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001742 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001743
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001744 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001745 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001746
Guido van Rossum823649d2001-03-21 18:40:58 +00001747 retval = PyObject_IsInstance(inst, cls);
1748 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001749 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001750 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001751}
1752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001753PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001754"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001755\n\
1756Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00001757With a type as second argument, return whether that is the object's type.\n\
1758The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001759isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001760
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001761
1762static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001763builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001764{
1765 PyObject *derived;
1766 PyObject *cls;
1767 int retval;
1768
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001769 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001770 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001771
Guido van Rossum823649d2001-03-21 18:40:58 +00001772 retval = PyObject_IsSubclass(derived, cls);
1773 if (retval < 0)
1774 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001775 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001776}
1777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001778PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001779"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001780\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00001781Return whether class C is a subclass (i.e., a derived class) of class B.\n\
1782When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
1783is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001784
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001785
Barry Warsawbd599b52000-08-03 15:45:29 +00001786static PyObject*
1787builtin_zip(PyObject *self, PyObject *args)
1788{
Guido van Rossumb65fb332006-08-25 23:26:40 +00001789 /* args must be a tuple */
1790 assert(PyTuple_Check(args));
Barry Warsawbd599b52000-08-03 15:45:29 +00001791
Guido van Rossumb65fb332006-08-25 23:26:40 +00001792 return _PyZip_CreateIter(args);
Barry Warsawbd599b52000-08-03 15:45:29 +00001793}
1794
1795
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001796PyDoc_STRVAR(zip_doc,
Guido van Rossum801f0d72006-08-24 19:48:10 +00001797"zip(it1 [, it2 [...]]) -> iter([(it1[0], it2[0] ...), ...])\n\
Barry Warsawbd599b52000-08-03 15:45:29 +00001798\n\
Guido van Rossum801f0d72006-08-24 19:48:10 +00001799Return an iterator yielding tuples, where each tuple contains the\n\
1800corresponding element from each of the argument iterables.\n\
1801The returned iterator ends when the shortest argument iterable is exhausted.\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001802(This is identical to itertools.izip().)");
Barry Warsawbd599b52000-08-03 15:45:29 +00001803
1804
Guido van Rossum79f25d91997-04-29 20:08:16 +00001805static PyMethodDef builtin_methods[] = {
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001806 {"__build_class__", (PyCFunction)builtin___build_class__,
1807 METH_VARARGS | METH_KEYWORDS, build_class_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001808 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001809 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00001810 {"all", builtin_all, METH_O, all_doc},
1811 {"any", builtin_any, METH_O, any_doc},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001812 {"bin", builtin_bin, METH_O, bin_doc},
Neal Norwitzc2550c72007-08-31 04:17:51 +00001813 {"chr", builtin_chr, METH_VARARGS, chr_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001814 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
Guido van Rossumd8faa362007-04-27 19:54:29 +00001815 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001816 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
1817 {"dir", builtin_dir, METH_VARARGS, dir_doc},
1818 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
1819 {"eval", builtin_eval, METH_VARARGS, eval_doc},
Georg Brandl7cae87c2006-09-06 06:51:57 +00001820 {"exec", builtin_exec, METH_VARARGS, exec_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001821 {"filter", builtin_filter, METH_VARARGS, filter_doc},
Eric Smith8c663262007-08-25 02:26:07 +00001822 {"format", builtin_format, METH_VARARGS, format_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001823 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
1824 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
1825 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
1826 {"hash", builtin_hash, METH_O, hash_doc},
1827 {"hex", builtin_hex, METH_O, hex_doc},
1828 {"id", builtin_id, METH_O, id_doc},
Guido van Rossuma88a0332007-02-26 16:59:55 +00001829 {"input", builtin_input, METH_VARARGS, input_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001830 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
1831 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
1832 {"iter", builtin_iter, METH_VARARGS, iter_doc},
1833 {"len", builtin_len, METH_O, len_doc},
1834 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
1835 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001836 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
1837 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Georg Brandla18af4e2007-04-21 15:47:16 +00001838 {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001839 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001840 {"ord", builtin_ord, METH_O, ord_doc},
1841 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Guido van Rossum452bf512007-02-09 05:32:43 +00001842 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001843 {"repr", builtin_repr, METH_O, repr_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001844 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001845 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00001846 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00001847 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001848 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Alex Martelli86d8b342007-08-22 22:39:42 +00001849 {"trunc", builtin_trunc, METH_O, trunc_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001850 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001851 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001852};
1853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001854PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001855"Built-in functions, exceptions, and other objects.\n\
1856\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001857Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001858
Guido van Rossum25ce5661997-08-02 03:10:38 +00001859PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001860_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001861{
Fred Drake5550de32000-06-20 04:54:19 +00001862 PyObject *mod, *dict, *debug;
Georg Brandl1a3284e2007-12-02 09:40:06 +00001863 mod = Py_InitModule4("builtins", builtin_methods,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001864 builtin_doc, (PyObject *)NULL,
1865 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001866 if (mod == NULL)
1867 return NULL;
1868 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00001869
Tim Peters7571a0f2003-03-23 17:52:28 +00001870#ifdef Py_TRACE_REFS
Georg Brandl1a3284e2007-12-02 09:40:06 +00001871 /* "builtins" exposes a number of statically allocated objects
Tim Peters7571a0f2003-03-23 17:52:28 +00001872 * that, before this code was added in 2.3, never showed up in
1873 * the list of "all objects" maintained by Py_TRACE_REFS. As a
1874 * result, programs leaking references to None and False (etc)
1875 * couldn't be diagnosed by examining sys.getobjects(0).
1876 */
1877#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
1878#else
1879#define ADD_TO_ALL(OBJECT) (void)0
1880#endif
1881
Tim Peters4b7625e2001-09-13 21:37:17 +00001882#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00001883 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
1884 return NULL; \
1885 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00001886
1887 SETBUILTIN("None", Py_None);
1888 SETBUILTIN("Ellipsis", Py_Ellipsis);
1889 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001890 SETBUILTIN("False", Py_False);
1891 SETBUILTIN("True", Py_True);
1892 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbae07c92007-10-08 02:46:15 +00001893 SETBUILTIN("memoryview", &PyMemoryView_Type);
Guido van Rossum254348e2007-11-21 19:29:53 +00001894 SETBUILTIN("bytearray", &PyBytes_Type);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001895 SETBUILTIN("bytes", &PyString_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001896 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001897#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00001898 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001899#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00001900 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00001901 SETBUILTIN("enumerate", &PyEnum_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001902 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00001903 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001904 SETBUILTIN("property", &PyProperty_Type);
Guido van Rossumddefaf32007-01-14 03:31:43 +00001905 SETBUILTIN("int", &PyLong_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001906 SETBUILTIN("list", &PyList_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001907 SETBUILTIN("object", &PyBaseObject_Type);
Guido van Rossum805365e2007-05-07 22:24:25 +00001908 SETBUILTIN("range", &PyRange_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00001909 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00001910 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00001911 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001912 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
Guido van Rossum572dbf82007-04-27 23:53:51 +00001913 SETBUILTIN("str", &PyUnicode_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001914 SETBUILTIN("super", &PySuper_Type);
1915 SETBUILTIN("tuple", &PyTuple_Type);
1916 SETBUILTIN("type", &PyType_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001917 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00001918 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
1919 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001920 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00001921 }
1922 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001923
Guido van Rossum25ce5661997-08-02 03:10:38 +00001924 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00001925#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00001926#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00001927}