blob: b6a7327d615cf993e44d8e196fde48f2b9adfc7e [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
Guido van Rossume2ae77b2001-10-24 20:42:55 +000011#ifdef RISCOS
12#include "unixstuff.h"
13#endif
14
Mark Hammond26cffde2001-05-14 12:17:34 +000015/* The default encoding used by the platform file system APIs
16 Can remain NULL for all platforms that don't have such a concept
17*/
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
Mark Hammond26cffde2001-05-14 12:17:34 +000019const char *Py_FileSystemDefaultEncoding = "mbcs";
Just van Rossumb9b8e9c2003-02-10 09:22:01 +000020#elif defined(__APPLE__)
21const char *Py_FileSystemDefaultEncoding = "utf-8";
Mark Hammond26cffde2001-05-14 12:17:34 +000022#else
23const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
24#endif
Mark Hammondef8b6542001-05-13 08:04:26 +000025
Guido van Rossum79f25d91997-04-29 20:08:16 +000026static PyObject *
Guido van Rossum52cc1d82007-03-18 15:41:51 +000027builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
28{
Guido van Rossumcd16bf62007-06-13 18:07:49 +000029 PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell;
30 PyObject *cls = NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +000031 Py_ssize_t nargs, nbases;
32
33 assert(args != NULL);
34 if (!PyTuple_Check(args)) {
35 PyErr_SetString(PyExc_TypeError,
36 "__build_class__: args is not a tuple");
37 return NULL;
38 }
39 nargs = PyTuple_GET_SIZE(args);
40 if (nargs < 2) {
41 PyErr_SetString(PyExc_TypeError,
42 "__build_class__: not enough arguments");
43 return NULL;
44 }
45 func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
46 name = PyTuple_GET_ITEM(args, 1);
Martin v. Löwis5b222132007-06-10 09:51:05 +000047 if ((!PyString_Check(name) && !PyUnicode_Check(name))) {
Guido van Rossum52cc1d82007-03-18 15:41:51 +000048 PyErr_SetString(PyExc_TypeError,
49 "__build_class__: name is not a string");
50 return NULL;
51 }
52 bases = PyTuple_GetSlice(args, 2, nargs);
53 if (bases == NULL)
54 return NULL;
55 nbases = nargs - 2;
56
57 if (kwds == NULL) {
58 meta = NULL;
59 mkw = NULL;
60 }
61 else {
62 mkw = PyDict_Copy(kwds); /* Don't modify kwds passed in! */
63 if (mkw == NULL) {
64 Py_DECREF(bases);
65 return NULL;
66 }
67 meta = PyDict_GetItemString(mkw, "metaclass");
68 if (meta != NULL) {
69 Py_INCREF(meta);
70 if (PyDict_DelItemString(mkw, "metaclass") < 0) {
71 Py_DECREF(meta);
72 Py_DECREF(mkw);
73 Py_DECREF(bases);
74 return NULL;
75 }
76 }
77 }
78 if (meta == NULL) {
79 if (PyTuple_GET_SIZE(bases) == 0)
80 meta = (PyObject *) (&PyType_Type);
81 else {
82 PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
83 meta = (PyObject *) (base0->ob_type);
84 }
85 Py_INCREF(meta);
86 }
87 prep = PyObject_GetAttrString(meta, "__prepare__");
88 if (prep == NULL) {
89 PyErr_Clear();
90 ns = PyDict_New();
91 }
92 else {
93 PyObject *pargs = Py_BuildValue("OO", name, bases);
94 if (pargs == NULL) {
95 Py_DECREF(prep);
96 Py_DECREF(meta);
97 Py_XDECREF(mkw);
98 Py_DECREF(bases);
99 return NULL;
100 }
101 ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
102 Py_DECREF(pargs);
103 Py_DECREF(prep);
104 if (ns == NULL) {
105 Py_DECREF(meta);
106 Py_XDECREF(mkw);
107 Py_DECREF(bases);
108 return NULL;
109 }
110 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000111 cell = PyObject_CallFunctionObjArgs(func, ns, NULL);
112 if (cell != NULL) {
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000113 PyObject *margs;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000114 margs = Py_BuildValue("OOO", name, bases, ns);
115 if (margs != NULL) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000116 cls = PyEval_CallObjectWithKeywords(meta, margs, mkw);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000117 Py_DECREF(margs);
118 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000119 if (cls != NULL && PyCell_Check(cell)) {
120 Py_INCREF(cls);
121 PyCell_SET(cell, cls);
122 }
123 Py_DECREF(cell);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000124 }
125 Py_DECREF(ns);
126 Py_DECREF(meta);
127 Py_XDECREF(mkw);
128 Py_DECREF(bases);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000129 return cls;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000130}
131
132PyDoc_STRVAR(build_class_doc,
133"__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
134\n\
135Internal helper function used by the class statement.");
136
137static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000139{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000140 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
141 "level", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000142 char *name;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000143 PyObject *globals = NULL;
144 PyObject *locals = NULL;
145 PyObject *fromlist = NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000146 int level = -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000147
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
149 kwlist, &name, &globals, &locals, &fromlist, &level))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000150 return NULL;
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000151 return PyImport_ImportModuleLevel(name, globals, locals,
152 fromlist, level);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000153}
154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000155PyDoc_STRVAR(import_doc,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000157\n\
158Import a module. The globals are only used to determine the context;\n\
159they are not modified. The locals are currently unused. The fromlist\n\
160should be a list of names to emulate ``from name import ...'', or an\n\
161empty list to emulate ``import name''.\n\
162When importing a module from a package, note that __import__('A.B', ...)\n\
163returns package A when fromlist is empty, but its submodule B when\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000164fromlist is not empty. Level is used to determine whether to perform \n\
165absolute or relative imports. -1 is the original strategy of attempting\n\
166both absolute and relative imports, 0 is absolute, a positive number\n\
167is the number of parent directories to search relative to the current module.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000168
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000169
Guido van Rossum79f25d91997-04-29 20:08:16 +0000170static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000171builtin_abs(PyObject *self, PyObject *v)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000172{
Guido van Rossum09df08a1998-05-22 00:51:39 +0000173 return PyNumber_Absolute(v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000174}
175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000176PyDoc_STRVAR(abs_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000177"abs(number) -> number\n\
178\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000179Return the absolute value of the argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000180
Raymond Hettinger96229b12005-03-11 06:49:40 +0000181static PyObject *
182builtin_all(PyObject *self, PyObject *v)
183{
184 PyObject *it, *item;
185
186 it = PyObject_GetIter(v);
187 if (it == NULL)
188 return NULL;
189
190 while ((item = PyIter_Next(it)) != NULL) {
191 int cmp = PyObject_IsTrue(item);
192 Py_DECREF(item);
193 if (cmp < 0) {
194 Py_DECREF(it);
195 return NULL;
196 }
197 if (cmp == 0) {
198 Py_DECREF(it);
199 Py_RETURN_FALSE;
200 }
201 }
202 Py_DECREF(it);
203 if (PyErr_Occurred())
204 return NULL;
205 Py_RETURN_TRUE;
206}
207
208PyDoc_STRVAR(all_doc,
209"all(iterable) -> bool\n\
210\n\
211Return True if bool(x) is True for all values x in the iterable.");
212
213static PyObject *
214builtin_any(PyObject *self, PyObject *v)
215{
216 PyObject *it, *item;
217
218 it = PyObject_GetIter(v);
219 if (it == NULL)
220 return NULL;
221
222 while ((item = PyIter_Next(it)) != NULL) {
223 int cmp = PyObject_IsTrue(item);
224 Py_DECREF(item);
225 if (cmp < 0) {
226 Py_DECREF(it);
227 return NULL;
228 }
229 if (cmp == 1) {
230 Py_DECREF(it);
231 Py_RETURN_TRUE;
232 }
233 }
234 Py_DECREF(it);
235 if (PyErr_Occurred())
236 return NULL;
237 Py_RETURN_FALSE;
238}
239
240PyDoc_STRVAR(any_doc,
241"any(iterable) -> bool\n\
242\n\
243Return True if bool(x) is True for any x in the iterable.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000244
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000245
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246static PyObject *
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000247builtin_bin(PyObject *self, PyObject *v)
248{
249 return PyNumber_ToBase(v, 2);
250}
251
252PyDoc_STRVAR(bin_doc,
253"bin(number) -> string\n\
254\n\
255Return the binary representation of an integer or long integer.");
256
257
258static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259builtin_filter(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000260{
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000261 PyObject *itertools, *ifilter, *result;
262 itertools = PyImport_ImportModule("itertools");
263 if (itertools == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000264 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000265 ifilter = PyObject_GetAttrString(itertools, "ifilter");
266 Py_DECREF(itertools);
267 if (ifilter == NULL)
Georg Brandle35b6572005-07-19 22:20:20 +0000268 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000269 result = PyObject_Call(ifilter, args, NULL);
270 Py_DECREF(ifilter);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000271 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000272}
273
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000274PyDoc_STRVAR(filter_doc,
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000275"filter(predicate, iterable) -> iterator\n\
276\n\
277Return an iterator yielding only those elements of the input iterable\n\
278for which the predicate (a Boolean function) returns true.\n\
279If the predicate is None, 'lambda x: bool(x)' is assumed.\n\
280(This is identical to itertools.ifilter().)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000281
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000282
Guido van Rossum79f25d91997-04-29 20:08:16 +0000283static PyObject *
Walter Dörwalde7efd592007-06-05 20:07:21 +0000284builtin_chr8(PyObject *self, PyObject *args)
Guido van Rossum7fcf2242007-05-04 17:43:11 +0000285{
286 long x;
287 char s[1];
288
289 if (!PyArg_ParseTuple(args, "l:chr8", &x))
290 return NULL;
291 if (x < 0 || x >= 256) {
292 PyErr_SetString(PyExc_ValueError,
293 "chr8() arg not in range(256)");
294 return NULL;
295 }
296 s[0] = (char)x;
297 return PyString_FromStringAndSize(s, 1);
298}
299
Walter Dörwalde7efd592007-06-05 20:07:21 +0000300PyDoc_STRVAR(chr8_doc,
Guido van Rossum7fcf2242007-05-04 17:43:11 +0000301"chr8(i) -> 8-bit character\n\
302\n\
303Return a string of one character with ordinal i; 0 <= i < 256.");
304
305
306static PyObject *
Walter Dörwalde7efd592007-06-05 20:07:21 +0000307builtin_chr(PyObject *self, PyObject *args)
Guido van Rossum09095f32000-03-10 23:00:52 +0000308{
309 long x;
Guido van Rossum09095f32000-03-10 23:00:52 +0000310
Walter Dörwalde7efd592007-06-05 20:07:21 +0000311 if (!PyArg_ParseTuple(args, "l:chr", &x))
Guido van Rossum09095f32000-03-10 23:00:52 +0000312 return NULL;
Fredrik Lundh0dcf67e2001-06-26 20:01:56 +0000313
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000314 return PyUnicode_FromOrdinal(x);
Guido van Rossum09095f32000-03-10 23:00:52 +0000315}
316
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000317PyDoc_VAR(chr_doc) = PyDoc_STR(
Guido van Rossum84fc66d2007-05-03 17:18:26 +0000318"chr(i) -> Unicode character\n\
Guido van Rossum09095f32000-03-10 23:00:52 +0000319\n\
Guido van Rossum8ac004e2007-07-15 13:00:05 +0000320Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000321)
Guido van Rossum8ac004e2007-07-15 13:00:05 +0000322#ifndef Py_UNICODE_WIDE
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000323PyDoc_STR(
Guido van Rossum8ac004e2007-07-15 13:00:05 +0000324"\nIf 0x10000 <= i, a surrogate pair is returned."
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000325)
Guido van Rossum8ac004e2007-07-15 13:00:05 +0000326#endif
Guido van Rossum307fa8c2007-07-16 20:46:27 +0000327;
Guido van Rossum09095f32000-03-10 23:00:52 +0000328
329
330static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000331builtin_cmp(PyObject *self, PyObject *args)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000332{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000333 PyObject *a, *b;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000334 int c;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000335
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000336 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000337 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000338 if (PyObject_Cmp(a, b, &c) < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000339 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000340 return PyInt_FromLong((long)c);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000341}
342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000343PyDoc_STRVAR(cmp_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000344"cmp(x, y) -> integer\n\
345\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000346Return negative if x<y, zero if x==y, positive if x>y.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000347
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000348
349static char *
350source_as_string(PyObject *cmd)
351{
352 char *str;
353 Py_ssize_t size;
354
355 if (!PyObject_CheckReadBuffer(cmd) &&
356 !PyUnicode_Check(cmd)) {
357 PyErr_SetString(PyExc_TypeError,
358 "eval()/exec() arg 1 must be a string, bytes or code object");
359 return NULL;
360 }
361
362 if (PyUnicode_Check(cmd)) {
363 cmd = _PyUnicode_AsDefaultEncodedString(cmd, NULL);
364 if (cmd == NULL)
365 return NULL;
366 }
367 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) {
368 return NULL;
369 }
370 if (strlen(str) != size) {
371 PyErr_SetString(PyExc_TypeError,
372 "source code string cannot contain null bytes");
373 return NULL;
374 }
375 return str;
376}
377
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378static PyObject *
Guido van Rossumd8faa362007-04-27 19:54:29 +0000379builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum5b722181993-03-30 17:46:03 +0000380{
381 char *str;
382 char *filename;
383 char *startstr;
384 int start;
Tim Peters6cd6a822001-08-17 22:11:27 +0000385 int dont_inherit = 0;
386 int supplied_flags = 0;
Tim Peters5ba58662001-07-16 02:29:45 +0000387 PyCompilerFlags cf;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000388 PyObject *cmd;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000389 static char *kwlist[] = {"source", "filename", "mode", "flags",
390 "dont_inherit", NULL};
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000391
Guido van Rossumd8faa362007-04-27 19:54:29 +0000392 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
393 kwlist, &cmd, &filename, &startstr,
394 &supplied_flags, &dont_inherit))
Guido van Rossum5b722181993-03-30 17:46:03 +0000395 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000396
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000397 cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000398
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000399 str = source_as_string(cmd);
400 if (str == NULL)
Just van Rossumb9b8e9c2003-02-10 09:22:01 +0000401 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000402
Guido van Rossum5b722181993-03-30 17:46:03 +0000403 if (strcmp(startstr, "exec") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000404 start = Py_file_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000405 else if (strcmp(startstr, "eval") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000406 start = Py_eval_input;
Guido van Rossum872537c1995-07-07 22:43:42 +0000407 else if (strcmp(startstr, "single") == 0)
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000408 start = Py_single_input;
Guido van Rossum5b722181993-03-30 17:46:03 +0000409 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000411 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000412 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000413 }
Tim Peters6cd6a822001-08-17 22:11:27 +0000414
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000415 if (supplied_flags &
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000416 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000417 {
Tim Peters6cd6a822001-08-17 22:11:27 +0000418 PyErr_SetString(PyExc_ValueError,
419 "compile(): unrecognised flags");
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000420 return NULL;
Tim Peters6cd6a822001-08-17 22:11:27 +0000421 }
422 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
423
Tim Peters6cd6a822001-08-17 22:11:27 +0000424 if (!dont_inherit) {
425 PyEval_MergeCompilerFlags(&cf);
426 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000427 return Py_CompileStringFlags(str, filename, start, &cf);
Guido van Rossum5b722181993-03-30 17:46:03 +0000428}
429
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000430PyDoc_STRVAR(compile_doc,
Tim Peters6cd6a822001-08-17 22:11:27 +0000431"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000432\n\
433Compile the source string (a Python module, statement or expression)\n\
Georg Brandl7cae87c2006-09-06 06:51:57 +0000434into a code object that can be executed by exec() or eval().\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000435The filename will be used for run-time error messages.\n\
436The mode must be 'exec' to compile a module, 'single' to compile a\n\
Tim Peters6cd6a822001-08-17 22:11:27 +0000437single (interactive) statement, or 'eval' to compile an expression.\n\
438The flags argument, if present, controls which future statements influence\n\
439the compilation of the code.\n\
440The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
441the effects of any future statements in effect in the code calling\n\
442compile; if absent or zero these statements do influence the compilation,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000443in addition to any features explicitly specified.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000444
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000446builtin_dir(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000447{
Tim Peters5d2b77c2001-09-03 05:47:38 +0000448 PyObject *arg = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000449
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000450 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451 return NULL;
Tim Peters7eea37e2001-09-04 22:08:56 +0000452 return PyObject_Dir(arg);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000453}
454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000455PyDoc_STRVAR(dir_doc,
Tim Peters5d2b77c2001-09-03 05:47:38 +0000456"dir([object]) -> list of strings\n"
457"\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000458"If called without an argument, return the names in the current scope.\n"
459"Else, return an alphabetized list of names comprising (some of) the attributes\n"
460"of the given object, and of attributes reachable from it.\n"
461"If the object supplies a method named __dir__, it will be used; otherwise\n"
462"the default dir() logic is used and returns:\n"
463" for a module object: the module's attributes.\n"
464" for a class object: its attributes, and recursively the attributes\n"
465" of its bases.\n"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000466" for any other object: its attributes, its class's attributes, and\n"
Georg Brandle32b4222007-03-10 22:13:27 +0000467" recursively the attributes of its class's base classes.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000468
Guido van Rossum79f25d91997-04-29 20:08:16 +0000469static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000470builtin_divmod(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000471{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000472 PyObject *v, *w;
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000473
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000474 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
Guido van Rossum6a00cd81995-01-07 12:39:01 +0000475 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +0000476 return PyNumber_Divmod(v, w);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000477}
478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479PyDoc_STRVAR(divmod_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000480"divmod(x, y) -> (div, mod)\n\
481\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000483
484
Guido van Rossum79f25d91997-04-29 20:08:16 +0000485static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000486builtin_eval(PyObject *self, PyObject *args)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000487{
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000488 PyObject *cmd, *result, *tmp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000489 PyObject *globals = Py_None, *locals = Py_None;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000490 char *str;
Tim Peters9fa96be2001-08-17 23:04:59 +0000491 PyCompilerFlags cf;
Guido van Rossum590baa41993-11-30 13:40:46 +0000492
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000493 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000494 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000495 if (locals != Py_None && !PyMapping_Check(locals)) {
496 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000497 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000498 }
499 if (globals != Py_None && !PyDict_Check(globals)) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000500 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000501 "globals must be a real dict; try eval(expr, {}, mapping)"
502 : "globals must be a dict");
Raymond Hettinger513ffe82004-07-06 13:44:41 +0000503 return NULL;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000504 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000505 if (globals == Py_None) {
506 globals = PyEval_GetGlobals();
507 if (locals == Py_None)
508 locals = PyEval_GetLocals();
Guido van Rossum6135a871995-01-09 17:53:26 +0000509 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000510 else if (locals == Py_None)
Guido van Rossum6135a871995-01-09 17:53:26 +0000511 locals = globals;
Tim Peters9fa96be2001-08-17 23:04:59 +0000512
Georg Brandl77c85e62005-09-15 10:46:13 +0000513 if (globals == NULL || locals == NULL) {
514 PyErr_SetString(PyExc_TypeError,
515 "eval must be given globals and locals "
516 "when called without a frame");
517 return NULL;
518 }
519
Guido van Rossum79f25d91997-04-29 20:08:16 +0000520 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
521 if (PyDict_SetItemString(globals, "__builtins__",
522 PyEval_GetBuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000523 return NULL;
524 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000525
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000526 if (PyCode_Check(cmd)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +0000527 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000528 PyErr_SetString(PyExc_TypeError,
529 "code object passed to eval() may not contain free variables");
530 return NULL;
531 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
Jeremy Hylton15c1c4f2001-07-30 21:50:55 +0000533 }
Tim Peters9fa96be2001-08-17 23:04:59 +0000534
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000535 str = source_as_string(cmd);
536 if (str == NULL)
Guido van Rossum94390a41992-08-14 15:14:30 +0000537 return NULL;
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000538
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539 while (*str == ' ' || *str == '\t')
540 str++;
Tim Peters9fa96be2001-08-17 23:04:59 +0000541
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000542 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Tim Peters9fa96be2001-08-17 23:04:59 +0000543 (void)PyEval_MergeCompilerFlags(&cf);
Just van Rossum3aaf42c2003-02-10 08:21:10 +0000544 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
545 Py_XDECREF(tmp);
546 return result;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000547}
548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000549PyDoc_STRVAR(eval_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000550"eval(source[, globals[, locals]]) -> value\n\
551\n\
552Evaluate the source in the context of globals and locals.\n\
553The source may be a string representing a Python expression\n\
554or a code object as returned by compile().\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000555The globals must be a dictionary and locals can be any mapping,\n\
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000556defaulting to the current globals and locals.\n\
557If only globals is given, locals defaults to it.\n");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000558
Georg Brandl7cae87c2006-09-06 06:51:57 +0000559static PyObject *
560builtin_exec(PyObject *self, PyObject *args)
561{
562 PyObject *v;
563 PyObject *prog, *globals = Py_None, *locals = Py_None;
564 int plain = 0;
565
566 if (!PyArg_ParseTuple(args, "O|OO:exec", &prog, &globals, &locals))
567 return NULL;
568
569 if (globals == Py_None) {
570 globals = PyEval_GetGlobals();
571 if (locals == Py_None) {
572 locals = PyEval_GetLocals();
573 plain = 1;
574 }
575 if (!globals || !locals) {
576 PyErr_SetString(PyExc_SystemError,
577 "globals and locals cannot be NULL");
578 return NULL;
579 }
580 }
581 else if (locals == Py_None)
582 locals = globals;
583 if (!PyString_Check(prog) &&
584 !PyUnicode_Check(prog) &&
Guido van Rossumda5b8f22007-06-12 23:30:11 +0000585 !PyCode_Check(prog)) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000586 PyErr_Format(PyExc_TypeError,
587 "exec() arg 1 must be a string, file, or code "
588 "object, not %.100s", prog->ob_type->tp_name);
589 return NULL;
590 }
591 if (!PyDict_Check(globals)) {
592 PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
593 globals->ob_type->tp_name);
594 return NULL;
595 }
596 if (!PyMapping_Check(locals)) {
597 PyErr_Format(PyExc_TypeError,
598 "arg 3 must be a mapping or None, not %.100s",
599 locals->ob_type->tp_name);
600 return NULL;
601 }
602 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
603 if (PyDict_SetItemString(globals, "__builtins__",
604 PyEval_GetBuiltins()) != 0)
605 return NULL;
606 }
607
608 if (PyCode_Check(prog)) {
609 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
610 PyErr_SetString(PyExc_TypeError,
611 "code object passed to exec() may not "
612 "contain free variables");
613 return NULL;
614 }
615 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
616 }
Georg Brandl7cae87c2006-09-06 06:51:57 +0000617 else {
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000618 char *str = source_as_string(prog);
Georg Brandl7cae87c2006-09-06 06:51:57 +0000619 PyCompilerFlags cf;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000620 if (str == NULL)
Georg Brandl7cae87c2006-09-06 06:51:57 +0000621 return NULL;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000622 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
Georg Brandl7cae87c2006-09-06 06:51:57 +0000623 if (PyEval_MergeCompilerFlags(&cf))
624 v = PyRun_StringFlags(str, Py_file_input, globals,
625 locals, &cf);
626 else
627 v = PyRun_String(str, Py_file_input, globals, locals);
Georg Brandl7cae87c2006-09-06 06:51:57 +0000628 }
629 if (v == NULL)
630 return NULL;
631 Py_DECREF(v);
632 Py_RETURN_NONE;
633}
634
635PyDoc_STRVAR(exec_doc,
636"exec(object[, globals[, locals]])\n\
637\n\
638Read and execute code from a object, which can be a string, a code\n\
639object or a file object.\n\
640The globals and locals are dictionaries, defaulting to the current\n\
641globals and locals. If only globals is given, locals defaults to it.");
642
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000643
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000645builtin_getattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000646{
Martin v. Löwis5b222132007-06-10 09:51:05 +0000647 PyObject *v, *result, *dflt = NULL, *release = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000649
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000650 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
Guido van Rossum33894be1992-01-27 16:53:09 +0000651 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000652
653 if (PyString_Check(name)) {
654 release = PyString_AsDecodedObject(name, NULL, NULL);
655 if (!release)
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000656 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000657 name = release;
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000658 }
659
Martin v. Löwis5b222132007-06-10 09:51:05 +0000660 if (!PyUnicode_Check(name)) {
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000661 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000662 "getattr(): attribute name must be string");
Martin v. Löwis5b222132007-06-10 09:51:05 +0000663 Py_XDECREF(release);
Jeremy Hylton0eb11152001-07-30 22:39:31 +0000664 return NULL;
665 }
Guido van Rossum950ff291998-06-29 13:38:57 +0000666 result = PyObject_GetAttr(v, name);
Guido van Rossumd8923572001-10-16 21:31:32 +0000667 if (result == NULL && dflt != NULL &&
668 PyErr_ExceptionMatches(PyExc_AttributeError))
669 {
Guido van Rossum950ff291998-06-29 13:38:57 +0000670 PyErr_Clear();
671 Py_INCREF(dflt);
672 result = dflt;
673 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000674 Py_XDECREF(release);
Guido van Rossum950ff291998-06-29 13:38:57 +0000675 return result;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000676}
677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000678PyDoc_STRVAR(getattr_doc,
Guido van Rossum950ff291998-06-29 13:38:57 +0000679"getattr(object, name[, default]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000680\n\
Guido van Rossum950ff291998-06-29 13:38:57 +0000681Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
682When a default argument is given, it is returned when the attribute doesn't\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000683exist; without it, an exception is raised in that case.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000684
685
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000687builtin_globals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000688{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000689 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000690
Guido van Rossum79f25d91997-04-29 20:08:16 +0000691 d = PyEval_GetGlobals();
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000692 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000693 return d;
694}
695
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000696PyDoc_STRVAR(globals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000697"globals() -> dictionary\n\
698\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000699Return the dictionary containing the current scope's global variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000700
701
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000703builtin_hasattr(PyObject *self, PyObject *args)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000704{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705 PyObject *v;
706 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000708 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
Guido van Rossum9bfef441993-03-29 10:43:31 +0000709 return NULL;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000710 if (!PyUnicode_Check(name)) {
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000711 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +0000712 "hasattr(): attribute name must be string");
Jeremy Hylton302b54a2001-07-30 22:45:19 +0000713 return NULL;
714 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000715 v = PyObject_GetAttr(v, name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000716 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717 PyErr_Clear();
Guido van Rossum09df08a1998-05-22 00:51:39 +0000718 Py_INCREF(Py_False);
719 return Py_False;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000720 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 Py_DECREF(v);
Guido van Rossum09df08a1998-05-22 00:51:39 +0000722 Py_INCREF(Py_True);
723 return Py_True;
Guido van Rossum33894be1992-01-27 16:53:09 +0000724}
725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726PyDoc_STRVAR(hasattr_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +0000727"hasattr(object, name) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000728\n\
729Return whether the object has an attribute with the given name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730(This is done by calling getattr(object, name) and catching exceptions.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000731
732
Guido van Rossum79f25d91997-04-29 20:08:16 +0000733static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000734builtin_id(PyObject *self, PyObject *v)
Guido van Rossum5b722181993-03-30 17:46:03 +0000735{
Guido van Rossum106f2da2000-06-28 21:12:25 +0000736 return PyLong_FromVoidPtr(v);
Guido van Rossum5b722181993-03-30 17:46:03 +0000737}
738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000739PyDoc_STRVAR(id_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000740"id(object) -> integer\n\
741\n\
742Return the identity of an object. This is guaranteed to be unique among\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000743simultaneously existing objects. (Hint: it's the object's memory address.)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000744
745
Guido van Rossum79f25d91997-04-29 20:08:16 +0000746static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000747builtin_map(PyObject *self, PyObject *args)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000748{
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000749 PyObject *itertools, *imap, *result;
750 itertools = PyImport_ImportModule("itertools");
751 if (itertools == NULL)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000752 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000753 imap = PyObject_GetAttrString(itertools, "imap");
754 Py_DECREF(itertools);
755 if (imap == NULL)
Tim Peters4e9afdc2001-05-03 23:54:49 +0000756 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000757 result = PyObject_Call(imap, args, NULL);
758 Py_DECREF(imap);
Tim Peters4e9afdc2001-05-03 23:54:49 +0000759 return result;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000760}
761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000762PyDoc_STRVAR(map_doc,
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000763"map(function, iterable[, iterable, ...]) -> iterator\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000764\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000765Return an iterator yielding the results of applying the function to the\n\
766items of the argument iterables(s). If more than one iterable is given,\n\
767the function is called with an argument list consisting of the\n\
768corresponding item of each iterable, until an iterable is exhausted.\n\
769If the function is None, 'lambda *a: a' is assumed.\n\
770(This is identical to itertools.imap().)");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000771
772
Guido van Rossum79f25d91997-04-29 20:08:16 +0000773static PyObject *
Georg Brandla18af4e2007-04-21 15:47:16 +0000774builtin_next(PyObject *self, PyObject *args)
775{
776 PyObject *it, *res;
777 PyObject *def = NULL;
778
779 if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
780 return NULL;
781 if (!PyIter_Check(it)) {
782 PyErr_Format(PyExc_TypeError,
783 "%.200s object is not an iterator", it->ob_type->tp_name);
784 return NULL;
785 }
786
787 res = (*it->ob_type->tp_iternext)(it);
788 if (res == NULL) {
789 if (def) {
790 if (PyErr_Occurred() &&
791 !PyErr_ExceptionMatches(PyExc_StopIteration))
792 return NULL;
793 PyErr_Clear();
794 Py_INCREF(def);
795 return def;
796 } else if (PyErr_Occurred()) {
797 return NULL;
798 } else {
799 PyErr_SetNone(PyExc_StopIteration);
800 return NULL;
801 }
802 }
803 return res;
804}
805
806PyDoc_STRVAR(next_doc,
807"next(iterator[, default])\n\
808\n\
809Return the next item from the iterator. If default is given and the iterator\n\
810is exhausted, it is returned instead of raising StopIteration.");
811
812
813static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000814builtin_setattr(PyObject *self, PyObject *args)
Guido van Rossum33894be1992-01-27 16:53:09 +0000815{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816 PyObject *v;
817 PyObject *name;
818 PyObject *value;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000819
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000820 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
Guido van Rossum33894be1992-01-27 16:53:09 +0000821 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000822 if (PyObject_SetAttr(v, name, value) != 0)
Guido van Rossum33894be1992-01-27 16:53:09 +0000823 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000824 Py_INCREF(Py_None);
825 return Py_None;
Guido van Rossum33894be1992-01-27 16:53:09 +0000826}
827
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000828PyDoc_STRVAR(setattr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000829"setattr(object, name, value)\n\
830\n\
831Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000832``x.y = v''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000833
834
Guido van Rossum79f25d91997-04-29 20:08:16 +0000835static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000836builtin_delattr(PyObject *self, PyObject *args)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000837{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 PyObject *v;
839 PyObject *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000840
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000841 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
Guido van Rossum14144fc1994-08-29 12:53:40 +0000842 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000843 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
Guido van Rossum14144fc1994-08-29 12:53:40 +0000844 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 Py_INCREF(Py_None);
846 return Py_None;
Guido van Rossum14144fc1994-08-29 12:53:40 +0000847}
848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000849PyDoc_STRVAR(delattr_doc,
Guido van Rossumdf12a591998-11-23 22:13:04 +0000850"delattr(object, name)\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000851\n\
852Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000853``del x.y''.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000854
855
Guido van Rossum79f25d91997-04-29 20:08:16 +0000856static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000857builtin_hash(PyObject *self, PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000858{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000859 long x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000860
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 x = PyObject_Hash(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000862 if (x == -1)
863 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000864 return PyInt_FromLong(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000865}
866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000867PyDoc_STRVAR(hash_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000868"hash(object) -> integer\n\
869\n\
870Return a hash value for the object. Two objects with the same value have\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000871the same hash value. The reverse is not necessarily true, but likely.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000872
873
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000875builtin_hex(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +0000876{
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000877 return PyNumber_ToBase(v, 16);
Guido van Rossum006bcd41991-10-24 14:54:44 +0000878}
879
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000880PyDoc_STRVAR(hex_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000881"hex(number) -> string\n\
882\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000883Return the hexadecimal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000884
885
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000887builtin_iter(PyObject *self, PyObject *args)
888{
889 PyObject *v, *w = NULL;
890
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000891 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000892 return NULL;
893 if (w == NULL)
894 return PyObject_GetIter(v);
895 if (!PyCallable_Check(v)) {
896 PyErr_SetString(PyExc_TypeError,
897 "iter(v, w): v must be callable");
898 return NULL;
899 }
900 return PyCallIter_New(v, w);
901}
902
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000903PyDoc_STRVAR(iter_doc,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000904"iter(collection) -> iterator\n\
905iter(callable, sentinel) -> iterator\n\
906\n\
907Get an iterator from an object. In the first form, the argument must\n\
908supply its own iterator, or be a sequence.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000909In the second form, the callable is called until it returns the sentinel.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000910
911
912static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000913builtin_len(PyObject *self, PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000914{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000915 Py_ssize_t res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000916
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000917 res = PyObject_Size(v);
Guido van Rossum8ea9f4d1998-06-29 22:26:50 +0000918 if (res < 0 && PyErr_Occurred())
919 return NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000920 return PyInt_FromSsize_t(res);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000921}
922
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000923PyDoc_STRVAR(len_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000924"len(object) -> integer\n\
925\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000926Return the number of items of a sequence or mapping.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000927
928
Guido van Rossum79f25d91997-04-29 20:08:16 +0000929static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000930builtin_locals(PyObject *self)
Guido van Rossum872537c1995-07-07 22:43:42 +0000931{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000932 PyObject *d;
Guido van Rossum872537c1995-07-07 22:43:42 +0000933
Guido van Rossum79f25d91997-04-29 20:08:16 +0000934 d = PyEval_GetLocals();
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000935 Py_XINCREF(d);
Guido van Rossum872537c1995-07-07 22:43:42 +0000936 return d;
937}
938
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000939PyDoc_STRVAR(locals_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000940"locals() -> dictionary\n\
941\n\
Raymond Hettinger69bf8f32003-01-04 02:16:22 +0000942Update and return a dictionary containing the current scope's local variables.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +0000943
944
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000946min_max(PyObject *args, PyObject *kwds, int op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000947{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000948 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +0000949 const char *name = op == Py_LT ? "min" : "max";
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000950
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951 if (PyTuple_Size(args) > 1)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000952 v = args;
Martin v. Löwisfd39ad42004-08-12 14:42:37 +0000953 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
Guido van Rossum3f5da241990-12-20 15:06:42 +0000954 return NULL;
Tim Peters67d687a2002-04-29 21:27:32 +0000955
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000956 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
957 keyfunc = PyDict_GetItemString(kwds, "key");
958 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000959 PyErr_Format(PyExc_TypeError,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000960 "%s() got an unexpected keyword argument", name);
961 return NULL;
962 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +0000963 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000964
Tim Petersc3074532001-05-03 07:00:32 +0000965 it = PyObject_GetIter(v);
966 if (it == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000967 return NULL;
Tim Petersc3074532001-05-03 07:00:32 +0000968
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000969 maxitem = NULL; /* the result */
970 maxval = NULL; /* the value associated with the result */
Brett Cannon9e635cf2004-12-07 00:25:35 +0000971 while (( item = PyIter_Next(it) )) {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000972 /* get the value from the key function */
973 if (keyfunc != NULL) {
974 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
975 if (val == NULL)
976 goto Fail_it_item;
977 }
978 /* no key function; the value is the item */
979 else {
980 val = item;
981 Py_INCREF(val);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000982 }
Tim Petersc3074532001-05-03 07:00:32 +0000983
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000984 /* maximum value and item are unset; set them */
985 if (maxval == NULL) {
986 maxitem = item;
987 maxval = val;
988 }
989 /* maximum value and item are set; update them as necessary */
Guido van Rossum2d951851994-08-29 12:52:16 +0000990 else {
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +0000991 int cmp = PyObject_RichCompareBool(val, maxval, op);
992 if (cmp < 0)
993 goto Fail_it_item_and_val;
994 else if (cmp > 0) {
995 Py_DECREF(maxval);
996 Py_DECREF(maxitem);
997 maxval = val;
998 maxitem = item;
Guido van Rossum53451b32001-01-17 15:47:24 +0000999 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001000 else {
1001 Py_DECREF(item);
1002 Py_DECREF(val);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001003 }
Guido van Rossum2d951851994-08-29 12:52:16 +00001004 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001005 }
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001006 if (PyErr_Occurred())
1007 goto Fail_it;
1008 if (maxval == NULL) {
Martin v. Löwisfd39ad42004-08-12 14:42:37 +00001009 PyErr_Format(PyExc_ValueError,
1010 "%s() arg is an empty sequence", name);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001011 assert(maxitem == NULL);
1012 }
1013 else
1014 Py_DECREF(maxval);
Tim Petersc3074532001-05-03 07:00:32 +00001015 Py_DECREF(it);
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001016 return maxitem;
1017
1018Fail_it_item_and_val:
1019 Py_DECREF(val);
1020Fail_it_item:
1021 Py_DECREF(item);
1022Fail_it:
1023 Py_XDECREF(maxval);
1024 Py_XDECREF(maxitem);
1025 Py_DECREF(it);
1026 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001027}
1028
Guido van Rossum79f25d91997-04-29 20:08:16 +00001029static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001030builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001031{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001032 return min_max(args, kwds, Py_LT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001033}
1034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001035PyDoc_STRVAR(min_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001036"min(iterable[, key=func]) -> value\n\
1037min(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001038\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001039With a single iterable argument, return its smallest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001040With two or more arguments, return the smallest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001041
1042
Guido van Rossum79f25d91997-04-29 20:08:16 +00001043static PyObject *
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001044builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001045{
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001046 return min_max(args, kwds, Py_GT);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001047}
1048
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001049PyDoc_STRVAR(max_doc,
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001050"max(iterable[, key=func]) -> value\n\
1051max(a, b, c, ...[, key=func]) -> value\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001052\n\
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001053With a single iterable argument, return its largest item.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001054With two or more arguments, return the largest argument.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001055
1056
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001058builtin_oct(PyObject *self, PyObject *v)
Guido van Rossum006bcd41991-10-24 14:54:44 +00001059{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001060 return PyNumber_ToBase(v, 8);
Guido van Rossum006bcd41991-10-24 14:54:44 +00001061}
1062
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001063PyDoc_STRVAR(oct_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001064"oct(number) -> string\n\
1065\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066Return the octal representation of an integer or long integer.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001067
1068
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001070builtin_ord(PyObject *self, PyObject* obj)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001071{
Guido van Rossum09095f32000-03-10 23:00:52 +00001072 long ord;
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001073 Py_ssize_t size;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001074
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001075 if (PyString_Check(obj)) {
1076 size = PyString_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001077 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001078 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001079 return PyInt_FromLong(ord);
1080 }
Guido van Rossum98f97462007-04-13 03:31:13 +00001081 }
1082 else if (PyUnicode_Check(obj)) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001083 size = PyUnicode_GET_SIZE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001084 if (size == 1) {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001085 ord = (long)*PyUnicode_AS_UNICODE(obj);
Andrew M. Kuchling34c20cf2000-12-20 14:36:56 +00001086 return PyInt_FromLong(ord);
1087 }
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001088#ifndef Py_UNICODE_WIDE
1089 if (size == 2) {
1090 /* Decode a valid surrogate pair */
1091 int c0 = PyUnicode_AS_UNICODE(obj)[0];
1092 int c1 = PyUnicode_AS_UNICODE(obj)[1];
1093 if (0xD800 <= c0 && c0 <= 0xDBFF &&
1094 0xDC00 <= c1 && c1 <= 0xDFFF) {
1095 ord = ((((c0 & 0x03FF) << 10) | (c1 & 0x03FF)) +
1096 0x00010000);
1097 return PyInt_FromLong(ord);
1098 }
1099 }
1100#endif
Guido van Rossum6f376c42007-05-24 14:31:33 +00001101 }
Guido van Rossum98f97462007-04-13 03:31:13 +00001102 else if (PyBytes_Check(obj)) {
1103 /* XXX Hopefully this is temporary */
1104 size = PyBytes_GET_SIZE(obj);
1105 if (size == 1) {
Guido van Rossumf9e91c92007-05-08 21:05:48 +00001106 ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
Guido van Rossum98f97462007-04-13 03:31:13 +00001107 return PyInt_FromLong(ord);
1108 }
1109 }
1110 else {
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001111 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001112 "ord() expected string of length 1, but " \
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001113 "%.200s found", obj->ob_type->tp_name);
Guido van Rossum09095f32000-03-10 23:00:52 +00001114 return NULL;
1115 }
1116
Guido van Rossumad991772001-01-12 16:03:05 +00001117 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingf07aad12000-12-23 14:11:28 +00001118 "ord() expected a character, "
Martin v. Löwisd96ee902006-02-16 14:37:16 +00001119 "but string of length %zd found",
Jeremy Hylton394b54d2000-04-12 21:19:47 +00001120 size);
1121 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001122}
1123
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001124PyDoc_VAR(ord_doc) = PyDoc_STR(
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001125"ord(c) -> integer\n\
1126\n\
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001127Return the integer ordinal of a one-character string."
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001128)
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001129#ifndef Py_UNICODE_WIDE
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001130PyDoc_STR(
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001131"\nA valid surrogate pair is also accepted."
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001132)
Guido van Rossum8ac004e2007-07-15 13:00:05 +00001133#endif
Guido van Rossum307fa8c2007-07-16 20:46:27 +00001134;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001135
1136
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001138builtin_pow(PyObject *self, PyObject *args)
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001139{
Guido van Rossum09df08a1998-05-22 00:51:39 +00001140 PyObject *v, *w, *z = Py_None;
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001141
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001142 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
Guido van Rossum6a00cd81995-01-07 12:39:01 +00001143 return NULL;
Guido van Rossum09df08a1998-05-22 00:51:39 +00001144 return PyNumber_Power(v, w, z);
Guido van Rossumd4905451991-05-05 20:00:36 +00001145}
1146
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001147PyDoc_STRVAR(pow_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001148"pow(x, y[, z]) -> number\n\
1149\n\
1150With two arguments, equivalent to x**y. With three arguments,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001151equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001152
1153
Guido van Rossumefbbb1c2003-04-11 18:43:06 +00001154
Guido van Rossum34343512006-11-30 22:13:52 +00001155static PyObject *
1156builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1157{
1158 static char *kwlist[] = {"sep", "end", "file", 0};
Georg Brandl257d3d92007-02-26 10:35:10 +00001159 static PyObject *dummy_args;
Guido van Rossum34343512006-11-30 22:13:52 +00001160 PyObject *sep = NULL, *end = NULL, *file = NULL;
1161 int i, err;
1162
Georg Brandl257d3d92007-02-26 10:35:10 +00001163 if (dummy_args == NULL) {
1164 if (!(dummy_args = PyTuple_New(0)))
1165 return NULL;
1166 }
Georg Brandl88fc6642007-02-09 21:28:07 +00001167 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
Guido van Rossum34343512006-11-30 22:13:52 +00001168 kwlist, &sep, &end, &file))
1169 return NULL;
1170 if (file == NULL || file == Py_None)
1171 file = PySys_GetObject("stdout");
1172
Georg Brandl16f3e032006-11-30 22:46:03 +00001173 if (sep && sep != Py_None && !PyString_Check(sep) &&
1174 !PyUnicode_Check(sep)) {
1175 PyErr_Format(PyExc_TypeError,
1176 "sep must be None, str or unicode, not %.200s",
1177 sep->ob_type->tp_name);
1178 return NULL;
1179 }
1180 if (end && end != Py_None && !PyString_Check(end) &&
1181 !PyUnicode_Check(end)) {
1182 PyErr_Format(PyExc_TypeError,
1183 "end must be None, str or unicode, not %.200s",
1184 end->ob_type->tp_name);
1185 return NULL;
1186 }
Guido van Rossum34343512006-11-30 22:13:52 +00001187
1188 for (i = 0; i < PyTuple_Size(args); i++) {
1189 if (i > 0) {
1190 if (sep == NULL || sep == Py_None)
1191 err = PyFile_WriteString(" ", file);
1192 else
1193 err = PyFile_WriteObject(sep, file,
1194 Py_PRINT_RAW);
1195 if (err)
1196 return NULL;
1197 }
1198 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1199 Py_PRINT_RAW);
1200 if (err)
1201 return NULL;
1202 }
1203
1204 if (end == NULL || end == Py_None)
1205 err = PyFile_WriteString("\n", file);
1206 else
1207 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1208 if (err)
1209 return NULL;
1210
1211 Py_RETURN_NONE;
1212}
1213
1214PyDoc_STRVAR(print_doc,
Guido van Rossum452bf512007-02-09 05:32:43 +00001215"print(value, ..., file=None, sep=' ', end='\\n')\n\
Guido van Rossum34343512006-11-30 22:13:52 +00001216\n\
1217Prints the values to a stream, or to sys.stdout by default.\n\
1218Optional keyword arguments:\n\
1219file: a file-like object (stream); defaults to the current sys.stdout.\n\
1220sep: string inserted between values, default a space.\n\
1221end: string appended after the last value, default a newline.");
1222
1223
Guido van Rossuma88a0332007-02-26 16:59:55 +00001224static PyObject *
1225builtin_input(PyObject *self, PyObject *args)
1226{
Guido van Rossumeba76962007-05-27 09:13:28 +00001227 PyObject *promptarg = NULL;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001228 PyObject *fin = PySys_GetObject("stdin");
1229 PyObject *fout = PySys_GetObject("stdout");
Guido van Rossumeba76962007-05-27 09:13:28 +00001230 PyObject *ferr = PySys_GetObject("stderr");
1231 PyObject *tmp;
1232 long fd;
1233 int tty;
Guido van Rossuma88a0332007-02-26 16:59:55 +00001234
Guido van Rossumeba76962007-05-27 09:13:28 +00001235 /* Parse arguments */
1236 if (!PyArg_UnpackTuple(args, "input", 0, 1, &promptarg))
Guido van Rossuma88a0332007-02-26 16:59:55 +00001237 return NULL;
1238
Guido van Rossumeba76962007-05-27 09:13:28 +00001239 /* Check that stdin/out/err are intact */
Guido van Rossuma88a0332007-02-26 16:59:55 +00001240 if (fin == NULL) {
Guido van Rossumeba76962007-05-27 09:13:28 +00001241 PyErr_SetString(PyExc_RuntimeError,
1242 "input(): lost sys.stdin");
Guido van Rossuma88a0332007-02-26 16:59:55 +00001243 return NULL;
1244 }
1245 if (fout == NULL) {
Guido van Rossumeba76962007-05-27 09:13:28 +00001246 PyErr_SetString(PyExc_RuntimeError,
1247 "input(): lost sys.stdout");
Guido van Rossuma88a0332007-02-26 16:59:55 +00001248 return NULL;
1249 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001250 if (ferr == NULL) {
1251 PyErr_SetString(PyExc_RuntimeError,
1252 "input(): lost sys.stderr");
1253 return NULL;
1254 }
1255
1256 /* First of all, flush stderr */
1257 tmp = PyObject_CallMethod(ferr, "flush", "");
1258 if (tmp == NULL)
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001259 PyErr_Clear();
1260 else
1261 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001262
1263 /* We should only use (GNU) readline if Python's sys.stdin and
1264 sys.stdout are the same as C's stdin and stdout, because we
1265 need to pass it those. */
1266 tmp = PyObject_CallMethod(fin, "fileno", "");
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001267 if (tmp == NULL) {
1268 PyErr_Clear();
1269 tty = 0;
1270 }
1271 else {
Guido van Rossumeba76962007-05-27 09:13:28 +00001272 fd = PyInt_AsLong(tmp);
1273 Py_DECREF(tmp);
1274 if (fd < 0 && PyErr_Occurred())
1275 return NULL;
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001276 tty = fd == fileno(stdin) && isatty(fd);
1277 }
1278 if (tty) {
1279 tmp = PyObject_CallMethod(fout, "fileno", "");
1280 if (tmp == NULL)
1281 PyErr_Clear();
1282 else {
1283 fd = PyInt_AsLong(tmp);
1284 Py_DECREF(tmp);
1285 if (fd < 0 && PyErr_Occurred())
1286 return NULL;
1287 tty = fd == fileno(stdout) && isatty(fd);
1288 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001289 }
1290
1291 /* If we're interactive, use (GNU) readline */
1292 if (tty) {
Guido van Rossuma88a0332007-02-26 16:59:55 +00001293 PyObject *po;
1294 char *prompt;
1295 char *s;
1296 PyObject *result;
Guido van Rossumeba76962007-05-27 09:13:28 +00001297 tmp = PyObject_CallMethod(fout, "flush", "");
1298 if (tmp == NULL)
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001299 PyErr_Clear();
1300 else
1301 Py_DECREF(tmp);
Guido van Rossumeba76962007-05-27 09:13:28 +00001302 if (promptarg != NULL) {
1303 po = PyObject_Str(promptarg);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001304 if (po == NULL)
1305 return NULL;
1306 prompt = PyString_AsString(po);
Guido van Rossumeba76962007-05-27 09:13:28 +00001307 if (prompt == NULL) {
1308 Py_DECREF(po);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001309 return NULL;
Guido van Rossumeba76962007-05-27 09:13:28 +00001310 }
Guido van Rossuma88a0332007-02-26 16:59:55 +00001311 }
1312 else {
1313 po = NULL;
1314 prompt = "";
1315 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001316 s = PyOS_Readline(stdin, stdout, prompt);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001317 Py_XDECREF(po);
1318 if (s == NULL) {
1319 if (!PyErr_Occurred())
1320 PyErr_SetNone(PyExc_KeyboardInterrupt);
1321 return NULL;
1322 }
1323 if (*s == '\0') {
1324 PyErr_SetNone(PyExc_EOFError);
1325 result = NULL;
1326 }
1327 else { /* strip trailing '\n' */
1328 size_t len = strlen(s);
1329 if (len > PY_SSIZE_T_MAX) {
1330 PyErr_SetString(PyExc_OverflowError,
1331 "input: input too long");
1332 result = NULL;
1333 }
1334 else {
1335 result = PyString_FromStringAndSize(s, len-1);
1336 }
1337 }
1338 PyMem_FREE(s);
1339 return result;
1340 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001341
1342 /* Fallback if we're not interactive */
1343 if (promptarg != NULL) {
1344 if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0)
Guido van Rossuma88a0332007-02-26 16:59:55 +00001345 return NULL;
1346 }
Guido van Rossumeba76962007-05-27 09:13:28 +00001347 tmp = PyObject_CallMethod(fout, "flush", "");
1348 if (tmp == NULL)
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001349 PyErr_Clear();
1350 else
1351 Py_DECREF(tmp);
Guido van Rossuma88a0332007-02-26 16:59:55 +00001352 return PyFile_GetLine(fin, -1);
1353}
1354
1355PyDoc_STRVAR(input_doc,
1356"input([prompt]) -> string\n\
1357\n\
1358Read a string from standard input. The trailing newline is stripped.\n\
1359If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1360On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1361is printed without a trailing newline before reading.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001362
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001363
Guido van Rossum79f25d91997-04-29 20:08:16 +00001364static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001365builtin_repr(PyObject *self, PyObject *v)
Guido van Rossumc89705d1992-11-26 08:54:07 +00001366{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001367 return PyObject_Repr(v);
Guido van Rossumc89705d1992-11-26 08:54:07 +00001368}
1369
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001370PyDoc_STRVAR(repr_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001371"repr(object) -> string\n\
1372\n\
1373Return the canonical string representation of the object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001374For most object types, eval(repr(object)) == object.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001375
1376
Guido van Rossum79f25d91997-04-29 20:08:16 +00001377static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001378builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001379{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001380 double number;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001381 double f;
1382 int ndigits = 0;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001383 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001384 static char *kwlist[] = {"number", "ndigits", 0};
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001385
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001386 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
1387 kwlist, &number, &ndigits))
1388 return NULL;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001389 f = 1.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001390 i = abs(ndigits);
1391 while (--i >= 0)
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001392 f = f*10.0;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001393 if (ndigits < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001394 number /= f;
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001395 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001396 number *= f;
1397 if (number >= 0.0)
1398 number = floor(number + 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001399 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001400 number = ceil(number - 0.5);
Guido van Rossum1e162d31998-05-09 14:42:25 +00001401 if (ndigits < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402 number *= f;
Guido van Rossum1e162d31998-05-09 14:42:25 +00001403 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001404 number /= f;
1405 return PyFloat_FromDouble(number);
Guido van Rossum9e51f9b1993-02-12 16:29:05 +00001406}
1407
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001408PyDoc_STRVAR(round_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001409"round(number[, ndigits]) -> floating point number\n\
1410\n\
1411Round a number to a given precision in decimal digits (default 0 digits).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001412This always returns a floating point number. Precision may be negative.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001413
Raymond Hettinger64958a12003-12-17 20:43:33 +00001414static PyObject *
1415builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1416{
1417 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
1418 PyObject *callable;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001419 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001420 int reverse;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001421
Neal Norwitz6f0d4792005-12-15 06:40:36 +00001422 /* args 1-4 should match listsort in Objects/listobject.c */
Armin Rigo71d7e702005-09-20 18:13:03 +00001423 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
1424 kwlist, &seq, &compare, &keyfunc, &reverse))
1425 return NULL;
Raymond Hettinger64958a12003-12-17 20:43:33 +00001426
1427 newlist = PySequence_List(seq);
1428 if (newlist == NULL)
1429 return NULL;
1430
1431 callable = PyObject_GetAttrString(newlist, "sort");
1432 if (callable == NULL) {
1433 Py_DECREF(newlist);
1434 return NULL;
1435 }
Georg Brandl99d7e4e2005-08-31 22:21:15 +00001436
Raymond Hettinger64958a12003-12-17 20:43:33 +00001437 newargs = PyTuple_GetSlice(args, 1, 4);
1438 if (newargs == NULL) {
1439 Py_DECREF(newlist);
1440 Py_DECREF(callable);
1441 return NULL;
1442 }
1443
1444 v = PyObject_Call(callable, newargs, kwds);
1445 Py_DECREF(newargs);
1446 Py_DECREF(callable);
1447 if (v == NULL) {
1448 Py_DECREF(newlist);
1449 return NULL;
1450 }
1451 Py_DECREF(v);
1452 return newlist;
1453}
1454
1455PyDoc_STRVAR(sorted_doc,
1456"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001457
Guido van Rossum79f25d91997-04-29 20:08:16 +00001458static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001459builtin_vars(PyObject *self, PyObject *args)
Guido van Rossum2d951851994-08-29 12:52:16 +00001460{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001461 PyObject *v = NULL;
1462 PyObject *d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001463
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001464 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001465 return NULL;
Guido van Rossum2d951851994-08-29 12:52:16 +00001466 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001467 d = PyEval_GetLocals();
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001468 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001469 if (!PyErr_Occurred())
1470 PyErr_SetString(PyExc_SystemError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001471 "vars(): no locals!?");
Guido van Rossum53bb7ff1995-07-26 16:26:31 +00001472 }
1473 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001474 Py_INCREF(d);
Guido van Rossum2d951851994-08-29 12:52:16 +00001475 }
1476 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001477 d = PyObject_GetAttrString(v, "__dict__");
Guido van Rossum2d951851994-08-29 12:52:16 +00001478 if (d == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001479 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2d951851994-08-29 12:52:16 +00001480 "vars() argument must have __dict__ attribute");
1481 return NULL;
1482 }
1483 }
1484 return d;
1485}
1486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001487PyDoc_STRVAR(vars_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001488"vars([object]) -> dictionary\n\
1489\n\
1490Without arguments, equivalent to locals().\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001491With an argument, equivalent to object.__dict__.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001492
Alex Martellia70b1912003-04-22 08:12:33 +00001493
1494static PyObject*
1495builtin_sum(PyObject *self, PyObject *args)
1496{
1497 PyObject *seq;
1498 PyObject *result = NULL;
1499 PyObject *temp, *item, *iter;
1500
Raymond Hettinger5cf63942003-10-25 06:41:37 +00001501 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
Alex Martellia70b1912003-04-22 08:12:33 +00001502 return NULL;
1503
1504 iter = PyObject_GetIter(seq);
1505 if (iter == NULL)
1506 return NULL;
1507
1508 if (result == NULL) {
1509 result = PyInt_FromLong(0);
1510 if (result == NULL) {
1511 Py_DECREF(iter);
1512 return NULL;
1513 }
1514 } else {
1515 /* reject string values for 'start' parameter */
1516 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
1517 PyErr_SetString(PyExc_TypeError,
Alex Martellia9b9c9f2003-04-23 13:34:35 +00001518 "sum() can't sum strings [use ''.join(seq) instead]");
Alex Martellia70b1912003-04-22 08:12:33 +00001519 Py_DECREF(iter);
1520 return NULL;
1521 }
Alex Martelli41c9f882003-04-22 09:24:48 +00001522 Py_INCREF(result);
Alex Martellia70b1912003-04-22 08:12:33 +00001523 }
1524
1525 for(;;) {
1526 item = PyIter_Next(iter);
1527 if (item == NULL) {
1528 /* error, or end-of-sequence */
1529 if (PyErr_Occurred()) {
1530 Py_DECREF(result);
1531 result = NULL;
1532 }
1533 break;
1534 }
Alex Martellia253e182003-10-25 23:24:14 +00001535 temp = PyNumber_Add(result, item);
Alex Martellia70b1912003-04-22 08:12:33 +00001536 Py_DECREF(result);
1537 Py_DECREF(item);
1538 result = temp;
1539 if (result == NULL)
1540 break;
1541 }
1542 Py_DECREF(iter);
1543 return result;
1544}
1545
1546PyDoc_STRVAR(sum_doc,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001547"sum(sequence[, start]) -> value\n\
Alex Martellia70b1912003-04-22 08:12:33 +00001548\n\
1549Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +00001550of parameter 'start' (which defaults to 0). When the sequence is\n\
1551empty, returns start.");
Alex Martellia70b1912003-04-22 08:12:33 +00001552
1553
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001554static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001555builtin_isinstance(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001556{
1557 PyObject *inst;
1558 PyObject *cls;
Guido van Rossum823649d2001-03-21 18:40:58 +00001559 int retval;
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001560
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001561 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001562 return NULL;
Guido van Rossumf5dd9141997-12-02 19:11:45 +00001563
Guido van Rossum823649d2001-03-21 18:40:58 +00001564 retval = PyObject_IsInstance(inst, cls);
1565 if (retval < 0)
Guido van Rossumad991772001-01-12 16:03:05 +00001566 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001567 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001568}
1569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001570PyDoc_STRVAR(isinstance_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001571"isinstance(object, class-or-type-or-tuple) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001572\n\
1573Return whether an object is an instance of a class or of a subclass thereof.\n\
Guido van Rossum03290ec2001-10-07 20:54:12 +00001574With a type as second argument, return whether that is the object's type.\n\
1575The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001576isinstance(x, A) or isinstance(x, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001577
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001578
1579static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001580builtin_issubclass(PyObject *self, PyObject *args)
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001581{
1582 PyObject *derived;
1583 PyObject *cls;
1584 int retval;
1585
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001586 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001587 return NULL;
Guido van Rossum668213d1999-06-16 17:28:37 +00001588
Guido van Rossum823649d2001-03-21 18:40:58 +00001589 retval = PyObject_IsSubclass(derived, cls);
1590 if (retval < 0)
1591 return NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001592 return PyBool_FromLong(retval);
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001593}
1594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595PyDoc_STRVAR(issubclass_doc,
Guido van Rossum77f6a652002-04-03 22:41:51 +00001596"issubclass(C, B) -> bool\n\
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001597\n\
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +00001598Return whether class C is a subclass (i.e., a derived class) of class B.\n\
1599When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
1600is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001601
Barry Warsawcde8b1b1997-08-22 21:14:38 +00001602
Barry Warsawbd599b52000-08-03 15:45:29 +00001603static PyObject*
1604builtin_zip(PyObject *self, PyObject *args)
1605{
Guido van Rossumb65fb332006-08-25 23:26:40 +00001606 /* args must be a tuple */
1607 assert(PyTuple_Check(args));
Barry Warsawbd599b52000-08-03 15:45:29 +00001608
Guido van Rossumb65fb332006-08-25 23:26:40 +00001609 return _PyZip_CreateIter(args);
Barry Warsawbd599b52000-08-03 15:45:29 +00001610}
1611
1612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001613PyDoc_STRVAR(zip_doc,
Guido van Rossum801f0d72006-08-24 19:48:10 +00001614"zip(it1 [, it2 [...]]) -> iter([(it1[0], it2[0] ...), ...])\n\
Barry Warsawbd599b52000-08-03 15:45:29 +00001615\n\
Guido van Rossum801f0d72006-08-24 19:48:10 +00001616Return an iterator yielding tuples, where each tuple contains the\n\
1617corresponding element from each of the argument iterables.\n\
1618The returned iterator ends when the shortest argument iterable is exhausted.\n\
Guido van Rossumc1f779c2007-07-03 08:25:58 +00001619(This is identical to itertools.izip().)");
Barry Warsawbd599b52000-08-03 15:45:29 +00001620
1621
Guido van Rossum79f25d91997-04-29 20:08:16 +00001622static PyMethodDef builtin_methods[] = {
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001623 {"__build_class__", (PyCFunction)builtin___build_class__,
1624 METH_VARARGS | METH_KEYWORDS, build_class_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001625 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001626 {"abs", builtin_abs, METH_O, abs_doc},
Raymond Hettinger96229b12005-03-11 06:49:40 +00001627 {"all", builtin_all, METH_O, all_doc},
1628 {"any", builtin_any, METH_O, any_doc},
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001629 {"bin", builtin_bin, METH_O, bin_doc},
Walter Dörwalde7efd592007-06-05 20:07:21 +00001630 {"chr", builtin_chr, METH_VARARGS, chr_doc},
1631 {"chr8", builtin_chr8, METH_VARARGS, chr8_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001632 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
Guido van Rossumd8faa362007-04-27 19:54:29 +00001633 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001634 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
1635 {"dir", builtin_dir, METH_VARARGS, dir_doc},
1636 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
1637 {"eval", builtin_eval, METH_VARARGS, eval_doc},
Georg Brandl7cae87c2006-09-06 06:51:57 +00001638 {"exec", builtin_exec, METH_VARARGS, exec_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001639 {"filter", builtin_filter, METH_VARARGS, filter_doc},
1640 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
1641 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
1642 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
1643 {"hash", builtin_hash, METH_O, hash_doc},
1644 {"hex", builtin_hex, METH_O, hex_doc},
1645 {"id", builtin_id, METH_O, id_doc},
Guido van Rossuma88a0332007-02-26 16:59:55 +00001646 {"input", builtin_input, METH_VARARGS, input_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001647 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
1648 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
1649 {"iter", builtin_iter, METH_VARARGS, iter_doc},
1650 {"len", builtin_len, METH_O, len_doc},
1651 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
1652 {"map", builtin_map, METH_VARARGS, map_doc},
Raymond Hettinger3b0c7c22004-12-03 08:30:39 +00001653 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
1654 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
Georg Brandla18af4e2007-04-21 15:47:16 +00001655 {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001656 {"oct", builtin_oct, METH_O, oct_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001657 {"ord", builtin_ord, METH_O, ord_doc},
1658 {"pow", builtin_pow, METH_VARARGS, pow_doc},
Guido van Rossum452bf512007-02-09 05:32:43 +00001659 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001660 {"repr", builtin_repr, METH_O, repr_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001661 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001662 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
Raymond Hettinger64958a12003-12-17 20:43:33 +00001663 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
Alex Martellia70b1912003-04-22 08:12:33 +00001664 {"sum", builtin_sum, METH_VARARGS, sum_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001665 {"vars", builtin_vars, METH_VARARGS, vars_doc},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001666 {"zip", builtin_zip, METH_VARARGS, zip_doc},
Guido van Rossumc02e15c1991-12-16 13:03:00 +00001667 {NULL, NULL},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001668};
1669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001670PyDoc_STRVAR(builtin_doc,
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001671"Built-in functions, exceptions, and other objects.\n\
1672\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001673Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001674
Guido van Rossum25ce5661997-08-02 03:10:38 +00001675PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001676_PyBuiltin_Init(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +00001677{
Fred Drake5550de32000-06-20 04:54:19 +00001678 PyObject *mod, *dict, *debug;
Guido van Rossumf9d9c6c1998-06-26 21:23:49 +00001679 mod = Py_InitModule4("__builtin__", builtin_methods,
1680 builtin_doc, (PyObject *)NULL,
1681 PYTHON_API_VERSION);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001682 if (mod == NULL)
1683 return NULL;
1684 dict = PyModule_GetDict(mod);
Tim Peters4b7625e2001-09-13 21:37:17 +00001685
Tim Peters7571a0f2003-03-23 17:52:28 +00001686#ifdef Py_TRACE_REFS
1687 /* __builtin__ exposes a number of statically allocated objects
1688 * that, before this code was added in 2.3, never showed up in
1689 * the list of "all objects" maintained by Py_TRACE_REFS. As a
1690 * result, programs leaking references to None and False (etc)
1691 * couldn't be diagnosed by examining sys.getobjects(0).
1692 */
1693#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
1694#else
1695#define ADD_TO_ALL(OBJECT) (void)0
1696#endif
1697
Tim Peters4b7625e2001-09-13 21:37:17 +00001698#define SETBUILTIN(NAME, OBJECT) \
Tim Peters7571a0f2003-03-23 17:52:28 +00001699 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
1700 return NULL; \
1701 ADD_TO_ALL(OBJECT)
Tim Peters4b7625e2001-09-13 21:37:17 +00001702
1703 SETBUILTIN("None", Py_None);
1704 SETBUILTIN("Ellipsis", Py_Ellipsis);
1705 SETBUILTIN("NotImplemented", Py_NotImplemented);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001706 SETBUILTIN("False", Py_False);
1707 SETBUILTIN("True", Py_True);
Neal Norwitz32a7e7f2002-05-31 19:58:02 +00001708 SETBUILTIN("basestring", &PyBaseString_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001709 SETBUILTIN("bool", &PyBool_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00001710 SETBUILTIN("buffer", &PyBuffer_Type);
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001711 SETBUILTIN("bytes", &PyBytes_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001712 SETBUILTIN("classmethod", &PyClassMethod_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001713#ifndef WITHOUT_COMPLEX
Tim Peters4b7625e2001-09-13 21:37:17 +00001714 SETBUILTIN("complex", &PyComplex_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001715#endif
Tim Petersa427a2b2001-10-29 22:25:45 +00001716 SETBUILTIN("dict", &PyDict_Type);
Tim Peters67d687a2002-04-29 21:27:32 +00001717 SETBUILTIN("enumerate", &PyEnum_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001718 SETBUILTIN("float", &PyFloat_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00001719 SETBUILTIN("frozenset", &PyFrozenSet_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001720 SETBUILTIN("property", &PyProperty_Type);
Guido van Rossumddefaf32007-01-14 03:31:43 +00001721 SETBUILTIN("int", &PyLong_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001722 SETBUILTIN("list", &PyList_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001723 SETBUILTIN("object", &PyBaseObject_Type);
Guido van Rossum805365e2007-05-07 22:24:25 +00001724 SETBUILTIN("range", &PyRange_Type);
Raymond Hettinger85c20a42003-11-06 14:06:48 +00001725 SETBUILTIN("reversed", &PyReversed_Type);
Raymond Hettingera690a992003-11-16 16:17:49 +00001726 SETBUILTIN("set", &PySet_Type);
Guido van Rossumbea18cc2002-06-14 20:41:17 +00001727 SETBUILTIN("slice", &PySlice_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001728 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
Guido van Rossum572dbf82007-04-27 23:53:51 +00001729 SETBUILTIN("str", &PyUnicode_Type);
Guido van Rossum84fc66d2007-05-03 17:18:26 +00001730 SETBUILTIN("str8", &PyString_Type);
Tim Peters4b7625e2001-09-13 21:37:17 +00001731 SETBUILTIN("super", &PySuper_Type);
1732 SETBUILTIN("tuple", &PyTuple_Type);
1733 SETBUILTIN("type", &PyType_Type);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001734 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
Fred Drake5550de32000-06-20 04:54:19 +00001735 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
1736 Py_XDECREF(debug);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001737 return NULL;
Fred Drake5550de32000-06-20 04:54:19 +00001738 }
1739 Py_XDECREF(debug);
Barry Warsaw757af0e1997-08-29 22:13:51 +00001740
Guido van Rossum25ce5661997-08-02 03:10:38 +00001741 return mod;
Tim Peters7571a0f2003-03-23 17:52:28 +00001742#undef ADD_TO_ALL
Tim Peters4b7625e2001-09-13 21:37:17 +00001743#undef SETBUILTIN
Guido van Rossum3f5da241990-12-20 15:06:42 +00001744}