Line data Source code
1 : /* Built-in functions */
2 :
3 : #include "Python.h"
4 : #include "Python-ast.h"
5 :
6 : #include "node.h"
7 : #include "code.h"
8 :
9 : #include "asdl.h"
10 : #include "ast.h"
11 :
12 : #include <ctype.h>
13 :
14 : #ifdef HAVE_LANGINFO_H
15 : #include <langinfo.h> /* CODESET */
16 : #endif
17 :
18 : /* The default encoding used by the platform file system APIs
19 : Can remain NULL for all platforms that don't have such a concept
20 :
21 : Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the
22 : values for Py_FileSystemDefaultEncoding!
23 : */
24 : #ifdef HAVE_MBCS
25 : const char *Py_FileSystemDefaultEncoding = "mbcs";
26 : int Py_HasFileSystemDefaultEncoding = 1;
27 : #elif defined(__APPLE__)
28 : const char *Py_FileSystemDefaultEncoding = "utf-8";
29 : int Py_HasFileSystemDefaultEncoding = 1;
30 : #else
31 : const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */
32 : int Py_HasFileSystemDefaultEncoding = 0;
33 : #endif
34 :
35 : _Py_IDENTIFIER(fileno);
36 : _Py_IDENTIFIER(flush);
37 :
38 : static PyObject *
39 117 : builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
40 : {
41 : PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *cell;
42 117 : PyObject *cls = NULL;
43 : Py_ssize_t nargs;
44 : int isclass;
45 : _Py_IDENTIFIER(__prepare__);
46 :
47 : assert(args != NULL);
48 117 : if (!PyTuple_Check(args)) {
49 0 : PyErr_SetString(PyExc_TypeError,
50 : "__build_class__: args is not a tuple");
51 0 : return NULL;
52 : }
53 117 : nargs = PyTuple_GET_SIZE(args);
54 117 : if (nargs < 2) {
55 0 : PyErr_SetString(PyExc_TypeError,
56 : "__build_class__: not enough arguments");
57 0 : return NULL;
58 : }
59 117 : func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
60 117 : name = PyTuple_GET_ITEM(args, 1);
61 117 : if (!PyUnicode_Check(name)) {
62 0 : PyErr_SetString(PyExc_TypeError,
63 : "__build_class__: name is not a string");
64 0 : return NULL;
65 : }
66 117 : bases = PyTuple_GetSlice(args, 2, nargs);
67 117 : if (bases == NULL)
68 0 : return NULL;
69 :
70 117 : if (kwds == NULL) {
71 111 : meta = NULL;
72 111 : mkw = NULL;
73 : }
74 : else {
75 6 : mkw = PyDict_Copy(kwds); /* Don't modify kwds passed in! */
76 6 : if (mkw == NULL) {
77 0 : Py_DECREF(bases);
78 0 : return NULL;
79 : }
80 6 : meta = PyDict_GetItemString(mkw, "metaclass");
81 6 : if (meta != NULL) {
82 6 : Py_INCREF(meta);
83 6 : if (PyDict_DelItemString(mkw, "metaclass") < 0) {
84 0 : Py_DECREF(meta);
85 0 : Py_DECREF(mkw);
86 0 : Py_DECREF(bases);
87 0 : return NULL;
88 : }
89 : /* metaclass is explicitly given, check if it's indeed a class */
90 6 : isclass = PyType_Check(meta);
91 : }
92 : }
93 117 : if (meta == NULL) {
94 : /* if there are no bases, use type: */
95 111 : if (PyTuple_GET_SIZE(bases) == 0) {
96 35 : meta = (PyObject *) (&PyType_Type);
97 : }
98 : /* else get the type of the first base */
99 : else {
100 76 : PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
101 76 : meta = (PyObject *) (base0->ob_type);
102 : }
103 111 : Py_INCREF(meta);
104 111 : isclass = 1; /* meta is really a class */
105 : }
106 :
107 117 : if (isclass) {
108 : /* meta is really a class, so check for a more derived
109 : metaclass, or possible metaclass conflicts: */
110 117 : winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
111 : bases);
112 117 : if (winner == NULL) {
113 0 : Py_DECREF(meta);
114 0 : Py_XDECREF(mkw);
115 0 : Py_DECREF(bases);
116 0 : return NULL;
117 : }
118 117 : if (winner != meta) {
119 3 : Py_DECREF(meta);
120 3 : meta = winner;
121 3 : Py_INCREF(meta);
122 : }
123 : }
124 : /* else: meta is not a class, so we cannot do the metaclass
125 : calculation, so we will use the explicitly given object as it is */
126 117 : prep = _PyObject_GetAttrId(meta, &PyId___prepare__);
127 117 : if (prep == NULL) {
128 0 : if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
129 0 : PyErr_Clear();
130 0 : ns = PyDict_New();
131 : }
132 : else {
133 0 : Py_DECREF(meta);
134 0 : Py_XDECREF(mkw);
135 0 : Py_DECREF(bases);
136 0 : return NULL;
137 : }
138 : }
139 : else {
140 117 : PyObject *pargs = PyTuple_Pack(2, name, bases);
141 117 : if (pargs == NULL) {
142 0 : Py_DECREF(prep);
143 0 : Py_DECREF(meta);
144 0 : Py_XDECREF(mkw);
145 0 : Py_DECREF(bases);
146 0 : return NULL;
147 : }
148 117 : ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
149 117 : Py_DECREF(pargs);
150 117 : Py_DECREF(prep);
151 : }
152 117 : if (ns == NULL) {
153 0 : Py_DECREF(meta);
154 0 : Py_XDECREF(mkw);
155 0 : Py_DECREF(bases);
156 0 : return NULL;
157 : }
158 117 : cell = PyObject_CallFunctionObjArgs(func, ns, NULL);
159 117 : if (cell != NULL) {
160 : PyObject *margs;
161 117 : margs = PyTuple_Pack(3, name, bases, ns);
162 117 : if (margs != NULL) {
163 117 : cls = PyEval_CallObjectWithKeywords(meta, margs, mkw);
164 117 : Py_DECREF(margs);
165 : }
166 117 : if (cls != NULL && PyCell_Check(cell))
167 8 : PyCell_Set(cell, cls);
168 117 : Py_DECREF(cell);
169 : }
170 117 : Py_DECREF(ns);
171 117 : Py_DECREF(meta);
172 117 : Py_XDECREF(mkw);
173 117 : Py_DECREF(bases);
174 117 : return cls;
175 : }
176 :
177 : PyDoc_STRVAR(build_class_doc,
178 : "__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
179 : \n\
180 : Internal helper function used by the class statement.");
181 :
182 : static PyObject *
183 322 : builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
184 : {
185 : static char *kwlist[] = {"name", "globals", "locals", "fromlist",
186 : "level", 0};
187 322 : PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
188 322 : int level = 0;
189 :
190 322 : if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
191 : kwlist, &name, &globals, &locals, &fromlist, &level))
192 0 : return NULL;
193 322 : return PyImport_ImportModuleLevelObject(name, globals, locals,
194 : fromlist, level);
195 : }
196 :
197 : PyDoc_STRVAR(import_doc,
198 : "__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module\n\
199 : \n\
200 : Import a module. Because this function is meant for use by the Python\n\
201 : interpreter and not for general use it is better to use\n\
202 : importlib.import_module() to programmatically import a module.\n\
203 : \n\
204 : The globals argument is only used to determine the context;\n\
205 : they are not modified. The locals argument is unused. The fromlist\n\
206 : should be a list of names to emulate ``from name import ...'', or an\n\
207 : empty list to emulate ``import name''.\n\
208 : When importing a module from a package, note that __import__('A.B', ...)\n\
209 : returns package A when fromlist is empty, but its submodule B when\n\
210 : fromlist is not empty. Level is used to determine whether to perform \n\
211 : absolute or relative imports. 0 is absolute while a positive number\n\
212 : is the number of parent directories to search relative to the current module.");
213 :
214 :
215 : static PyObject *
216 0 : builtin_abs(PyObject *self, PyObject *v)
217 : {
218 0 : return PyNumber_Absolute(v);
219 : }
220 :
221 : PyDoc_STRVAR(abs_doc,
222 : "abs(number) -> number\n\
223 : \n\
224 : Return the absolute value of the argument.");
225 :
226 : static PyObject *
227 12 : builtin_all(PyObject *self, PyObject *v)
228 : {
229 : PyObject *it, *item;
230 : PyObject *(*iternext)(PyObject *);
231 : int cmp;
232 :
233 12 : it = PyObject_GetIter(v);
234 12 : if (it == NULL)
235 0 : return NULL;
236 12 : iternext = *Py_TYPE(it)->tp_iternext;
237 :
238 : for (;;) {
239 78 : item = iternext(it);
240 78 : if (item == NULL)
241 12 : break;
242 66 : cmp = PyObject_IsTrue(item);
243 66 : Py_DECREF(item);
244 66 : if (cmp < 0) {
245 0 : Py_DECREF(it);
246 0 : return NULL;
247 : }
248 66 : if (cmp == 0) {
249 0 : Py_DECREF(it);
250 0 : Py_RETURN_FALSE;
251 : }
252 66 : }
253 12 : Py_DECREF(it);
254 12 : if (PyErr_Occurred()) {
255 12 : if (PyErr_ExceptionMatches(PyExc_StopIteration))
256 12 : PyErr_Clear();
257 : else
258 0 : return NULL;
259 : }
260 12 : Py_RETURN_TRUE;
261 : }
262 :
263 : PyDoc_STRVAR(all_doc,
264 : "all(iterable) -> bool\n\
265 : \n\
266 : Return True if bool(x) is True for all values x in the iterable.");
267 :
268 : static PyObject *
269 28 : builtin_any(PyObject *self, PyObject *v)
270 : {
271 : PyObject *it, *item;
272 : PyObject *(*iternext)(PyObject *);
273 : int cmp;
274 :
275 28 : it = PyObject_GetIter(v);
276 28 : if (it == NULL)
277 0 : return NULL;
278 28 : iternext = *Py_TYPE(it)->tp_iternext;
279 :
280 : for (;;) {
281 40 : item = iternext(it);
282 40 : if (item == NULL)
283 4 : break;
284 36 : cmp = PyObject_IsTrue(item);
285 36 : Py_DECREF(item);
286 36 : if (cmp < 0) {
287 0 : Py_DECREF(it);
288 0 : return NULL;
289 : }
290 36 : if (cmp == 1) {
291 24 : Py_DECREF(it);
292 24 : Py_RETURN_TRUE;
293 : }
294 12 : }
295 4 : Py_DECREF(it);
296 4 : if (PyErr_Occurred()) {
297 4 : if (PyErr_ExceptionMatches(PyExc_StopIteration))
298 4 : PyErr_Clear();
299 : else
300 0 : return NULL;
301 : }
302 4 : Py_RETURN_FALSE;
303 : }
304 :
305 : PyDoc_STRVAR(any_doc,
306 : "any(iterable) -> bool\n\
307 : \n\
308 : Return True if bool(x) is True for any x in the iterable.");
309 :
310 : static PyObject *
311 0 : builtin_ascii(PyObject *self, PyObject *v)
312 : {
313 0 : return PyObject_ASCII(v);
314 : }
315 :
316 : PyDoc_STRVAR(ascii_doc,
317 : "ascii(object) -> string\n\
318 : \n\
319 : As repr(), return a string containing a printable representation of an\n\
320 : object, but escape the non-ASCII characters in the string returned by\n\
321 : repr() using \\x, \\u or \\U escapes. This generates a string similar\n\
322 : to that returned by repr() in Python 2.");
323 :
324 :
325 : static PyObject *
326 0 : builtin_bin(PyObject *self, PyObject *v)
327 : {
328 0 : return PyNumber_ToBase(v, 2);
329 : }
330 :
331 : PyDoc_STRVAR(bin_doc,
332 : "bin(number) -> string\n\
333 : \n\
334 : Return the binary representation of an integer.");
335 :
336 :
337 : static PyObject *
338 8 : builtin_callable(PyObject *self, PyObject *v)
339 : {
340 8 : return PyBool_FromLong((long)PyCallable_Check(v));
341 : }
342 :
343 : PyDoc_STRVAR(callable_doc,
344 : "callable(object) -> bool\n\
345 : \n\
346 : Return whether the object is callable (i.e., some kind of function).\n\
347 : Note that classes are callable, as are instances of classes with a\n\
348 : __call__() method.");
349 :
350 :
351 : typedef struct {
352 : PyObject_HEAD
353 : PyObject *func;
354 : PyObject *it;
355 : } filterobject;
356 :
357 : static PyObject *
358 0 : filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
359 : {
360 : PyObject *func, *seq;
361 : PyObject *it;
362 : filterobject *lz;
363 :
364 0 : if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter()", kwds))
365 0 : return NULL;
366 :
367 0 : if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
368 0 : return NULL;
369 :
370 : /* Get iterator. */
371 0 : it = PyObject_GetIter(seq);
372 0 : if (it == NULL)
373 0 : return NULL;
374 :
375 : /* create filterobject structure */
376 0 : lz = (filterobject *)type->tp_alloc(type, 0);
377 0 : if (lz == NULL) {
378 0 : Py_DECREF(it);
379 0 : return NULL;
380 : }
381 0 : Py_INCREF(func);
382 0 : lz->func = func;
383 0 : lz->it = it;
384 :
385 0 : return (PyObject *)lz;
386 : }
387 :
388 : static void
389 0 : filter_dealloc(filterobject *lz)
390 : {
391 0 : PyObject_GC_UnTrack(lz);
392 0 : Py_XDECREF(lz->func);
393 0 : Py_XDECREF(lz->it);
394 0 : Py_TYPE(lz)->tp_free(lz);
395 0 : }
396 :
397 : static int
398 0 : filter_traverse(filterobject *lz, visitproc visit, void *arg)
399 : {
400 0 : Py_VISIT(lz->it);
401 0 : Py_VISIT(lz->func);
402 0 : return 0;
403 : }
404 :
405 : static PyObject *
406 0 : filter_next(filterobject *lz)
407 : {
408 : PyObject *item;
409 0 : PyObject *it = lz->it;
410 : long ok;
411 : PyObject *(*iternext)(PyObject *);
412 :
413 0 : iternext = *Py_TYPE(it)->tp_iternext;
414 : for (;;) {
415 0 : item = iternext(it);
416 0 : if (item == NULL)
417 0 : return NULL;
418 :
419 0 : if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) {
420 0 : ok = PyObject_IsTrue(item);
421 : } else {
422 : PyObject *good;
423 0 : good = PyObject_CallFunctionObjArgs(lz->func,
424 : item, NULL);
425 0 : if (good == NULL) {
426 0 : Py_DECREF(item);
427 0 : return NULL;
428 : }
429 0 : ok = PyObject_IsTrue(good);
430 0 : Py_DECREF(good);
431 : }
432 0 : if (ok > 0)
433 0 : return item;
434 0 : Py_DECREF(item);
435 0 : if (ok < 0)
436 0 : return NULL;
437 0 : }
438 : }
439 :
440 : static PyObject *
441 0 : filter_reduce(filterobject *lz)
442 : {
443 0 : return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
444 : }
445 :
446 : PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
447 :
448 : static PyMethodDef filter_methods[] = {
449 : {"__reduce__", (PyCFunction)filter_reduce, METH_NOARGS, reduce_doc},
450 : {NULL, NULL} /* sentinel */
451 : };
452 :
453 : PyDoc_STRVAR(filter_doc,
454 : "filter(function or None, iterable) --> filter object\n\
455 : \n\
456 : Return an iterator yielding those items of iterable for which function(item)\n\
457 : is true. If function is None, return the items that are true.");
458 :
459 : PyTypeObject PyFilter_Type = {
460 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
461 : "filter", /* tp_name */
462 : sizeof(filterobject), /* tp_basicsize */
463 : 0, /* tp_itemsize */
464 : /* methods */
465 : (destructor)filter_dealloc, /* tp_dealloc */
466 : 0, /* tp_print */
467 : 0, /* tp_getattr */
468 : 0, /* tp_setattr */
469 : 0, /* tp_reserved */
470 : 0, /* tp_repr */
471 : 0, /* tp_as_number */
472 : 0, /* tp_as_sequence */
473 : 0, /* tp_as_mapping */
474 : 0, /* tp_hash */
475 : 0, /* tp_call */
476 : 0, /* tp_str */
477 : PyObject_GenericGetAttr, /* tp_getattro */
478 : 0, /* tp_setattro */
479 : 0, /* tp_as_buffer */
480 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
481 : Py_TPFLAGS_BASETYPE, /* tp_flags */
482 : filter_doc, /* tp_doc */
483 : (traverseproc)filter_traverse, /* tp_traverse */
484 : 0, /* tp_clear */
485 : 0, /* tp_richcompare */
486 : 0, /* tp_weaklistoffset */
487 : PyObject_SelfIter, /* tp_iter */
488 : (iternextfunc)filter_next, /* tp_iternext */
489 : filter_methods, /* tp_methods */
490 : 0, /* tp_members */
491 : 0, /* tp_getset */
492 : 0, /* tp_base */
493 : 0, /* tp_dict */
494 : 0, /* tp_descr_get */
495 : 0, /* tp_descr_set */
496 : 0, /* tp_dictoffset */
497 : 0, /* tp_init */
498 : PyType_GenericAlloc, /* tp_alloc */
499 : filter_new, /* tp_new */
500 : PyObject_GC_Del, /* tp_free */
501 : };
502 :
503 :
504 : static PyObject *
505 0 : builtin_format(PyObject *self, PyObject *args)
506 : {
507 : PyObject *value;
508 0 : PyObject *format_spec = NULL;
509 :
510 0 : if (!PyArg_ParseTuple(args, "O|U:format", &value, &format_spec))
511 0 : return NULL;
512 :
513 0 : return PyObject_Format(value, format_spec);
514 : }
515 :
516 : PyDoc_STRVAR(format_doc,
517 : "format(value[, format_spec]) -> string\n\
518 : \n\
519 : Returns value.__format__(format_spec)\n\
520 : format_spec defaults to \"\"");
521 :
522 : static PyObject *
523 0 : builtin_chr(PyObject *self, PyObject *args)
524 : {
525 : int x;
526 :
527 0 : if (!PyArg_ParseTuple(args, "i:chr", &x))
528 0 : return NULL;
529 :
530 0 : return PyUnicode_FromOrdinal(x);
531 : }
532 :
533 : PyDoc_STRVAR(chr_doc,
534 : "chr(i) -> Unicode character\n\
535 : \n\
536 : Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
537 :
538 :
539 : static char *
540 3 : source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf)
541 : {
542 : char *str;
543 : Py_ssize_t size;
544 :
545 3 : if (PyUnicode_Check(cmd)) {
546 3 : cf->cf_flags |= PyCF_IGNORE_COOKIE;
547 3 : str = PyUnicode_AsUTF8AndSize(cmd, &size);
548 3 : if (str == NULL)
549 0 : return NULL;
550 : }
551 0 : else if (!PyObject_CheckReadBuffer(cmd)) {
552 0 : PyErr_Format(PyExc_TypeError,
553 : "%s() arg 1 must be a %s object",
554 : funcname, what);
555 0 : return NULL;
556 : }
557 0 : else if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) {
558 0 : return NULL;
559 : }
560 :
561 3 : if (strlen(str) != size) {
562 0 : PyErr_SetString(PyExc_TypeError,
563 : "source code string cannot contain null bytes");
564 0 : return NULL;
565 : }
566 3 : return str;
567 : }
568 :
569 : static PyObject *
570 1 : builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
571 : {
572 : char *str;
573 : PyObject *filename_obj;
574 : char *filename;
575 : char *startstr;
576 1 : int mode = -1;
577 1 : int dont_inherit = 0;
578 1 : int supplied_flags = 0;
579 1 : int optimize = -1;
580 : int is_ast;
581 : PyCompilerFlags cf;
582 : PyObject *cmd;
583 : static char *kwlist[] = {"source", "filename", "mode", "flags",
584 : "dont_inherit", "optimize", NULL};
585 1 : int start[] = {Py_file_input, Py_eval_input, Py_single_input};
586 : PyObject *result;
587 :
588 1 : if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&s|iii:compile", kwlist,
589 : &cmd,
590 : PyUnicode_FSConverter, &filename_obj,
591 : &startstr, &supplied_flags,
592 : &dont_inherit, &optimize))
593 0 : return NULL;
594 :
595 1 : filename = PyBytes_AS_STRING(filename_obj);
596 1 : cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8;
597 :
598 1 : if (supplied_flags &
599 : ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
600 : {
601 0 : PyErr_SetString(PyExc_ValueError,
602 : "compile(): unrecognised flags");
603 0 : goto error;
604 : }
605 : /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
606 :
607 1 : if (optimize < -1 || optimize > 2) {
608 0 : PyErr_SetString(PyExc_ValueError,
609 : "compile(): invalid optimize value");
610 0 : goto error;
611 : }
612 :
613 1 : if (!dont_inherit) {
614 1 : PyEval_MergeCompilerFlags(&cf);
615 : }
616 :
617 1 : if (strcmp(startstr, "exec") == 0)
618 1 : mode = 0;
619 0 : else if (strcmp(startstr, "eval") == 0)
620 0 : mode = 1;
621 0 : else if (strcmp(startstr, "single") == 0)
622 0 : mode = 2;
623 : else {
624 0 : PyErr_SetString(PyExc_ValueError,
625 : "compile() arg 3 must be 'exec', 'eval' or 'single'");
626 0 : goto error;
627 : }
628 :
629 1 : is_ast = PyAST_Check(cmd);
630 1 : if (is_ast == -1)
631 0 : goto error;
632 1 : if (is_ast) {
633 0 : if (supplied_flags & PyCF_ONLY_AST) {
634 0 : Py_INCREF(cmd);
635 0 : result = cmd;
636 : }
637 : else {
638 : PyArena *arena;
639 : mod_ty mod;
640 :
641 0 : arena = PyArena_New();
642 0 : if (arena == NULL)
643 0 : goto error;
644 0 : mod = PyAST_obj2mod(cmd, arena, mode);
645 0 : if (mod == NULL) {
646 0 : PyArena_Free(arena);
647 0 : goto error;
648 : }
649 0 : if (!PyAST_Validate(mod)) {
650 0 : PyArena_Free(arena);
651 0 : goto error;
652 : }
653 0 : result = (PyObject*)PyAST_CompileEx(mod, filename,
654 : &cf, optimize, arena);
655 0 : PyArena_Free(arena);
656 : }
657 0 : goto finally;
658 : }
659 :
660 1 : str = source_as_string(cmd, "compile", "string, bytes, AST or code", &cf);
661 1 : if (str == NULL)
662 0 : goto error;
663 :
664 1 : result = Py_CompileStringExFlags(str, filename, start[mode], &cf, optimize);
665 1 : goto finally;
666 :
667 : error:
668 0 : result = NULL;
669 : finally:
670 1 : Py_DECREF(filename_obj);
671 1 : return result;
672 : }
673 :
674 : PyDoc_STRVAR(compile_doc,
675 : "compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
676 : \n\
677 : Compile the source string (a Python module, statement or expression)\n\
678 : into a code object that can be executed by exec() or eval().\n\
679 : The filename will be used for run-time error messages.\n\
680 : The mode must be 'exec' to compile a module, 'single' to compile a\n\
681 : single (interactive) statement, or 'eval' to compile an expression.\n\
682 : The flags argument, if present, controls which future statements influence\n\
683 : the compilation of the code.\n\
684 : The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
685 : the effects of any future statements in effect in the code calling\n\
686 : compile; if absent or zero these statements do influence the compilation,\n\
687 : in addition to any features explicitly specified.");
688 :
689 : static PyObject *
690 1 : builtin_dir(PyObject *self, PyObject *args)
691 : {
692 1 : PyObject *arg = NULL;
693 :
694 1 : if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
695 0 : return NULL;
696 1 : return PyObject_Dir(arg);
697 : }
698 :
699 : PyDoc_STRVAR(dir_doc,
700 : "dir([object]) -> list of strings\n"
701 : "\n"
702 : "If called without an argument, return the names in the current scope.\n"
703 : "Else, return an alphabetized list of names comprising (some of) the attributes\n"
704 : "of the given object, and of attributes reachable from it.\n"
705 : "If the object supplies a method named __dir__, it will be used; otherwise\n"
706 : "the default dir() logic is used and returns:\n"
707 : " for a module object: the module's attributes.\n"
708 : " for a class object: its attributes, and recursively the attributes\n"
709 : " of its bases.\n"
710 : " for any other object: its attributes, its class's attributes, and\n"
711 : " recursively the attributes of its class's base classes.");
712 :
713 : static PyObject *
714 0 : builtin_divmod(PyObject *self, PyObject *args)
715 : {
716 : PyObject *v, *w;
717 :
718 0 : if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
719 0 : return NULL;
720 0 : return PyNumber_Divmod(v, w);
721 : }
722 :
723 : PyDoc_STRVAR(divmod_doc,
724 : "divmod(x, y) -> (div, mod)\n\
725 : \n\
726 : Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
727 :
728 :
729 : static PyObject *
730 0 : builtin_eval(PyObject *self, PyObject *args)
731 : {
732 0 : PyObject *cmd, *result, *tmp = NULL;
733 0 : PyObject *globals = Py_None, *locals = Py_None;
734 : char *str;
735 : PyCompilerFlags cf;
736 :
737 0 : if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
738 0 : return NULL;
739 0 : if (locals != Py_None && !PyMapping_Check(locals)) {
740 0 : PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
741 0 : return NULL;
742 : }
743 0 : if (globals != Py_None && !PyDict_Check(globals)) {
744 0 : PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
745 : "globals must be a real dict; try eval(expr, {}, mapping)"
746 : : "globals must be a dict");
747 0 : return NULL;
748 : }
749 0 : if (globals == Py_None) {
750 0 : globals = PyEval_GetGlobals();
751 0 : if (locals == Py_None)
752 0 : locals = PyEval_GetLocals();
753 : }
754 0 : else if (locals == Py_None)
755 0 : locals = globals;
756 :
757 0 : if (globals == NULL || locals == NULL) {
758 0 : PyErr_SetString(PyExc_TypeError,
759 : "eval must be given globals and locals "
760 : "when called without a frame");
761 0 : return NULL;
762 : }
763 :
764 0 : if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
765 0 : if (PyDict_SetItemString(globals, "__builtins__",
766 : PyEval_GetBuiltins()) != 0)
767 0 : return NULL;
768 : }
769 :
770 0 : if (PyCode_Check(cmd)) {
771 0 : if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
772 0 : PyErr_SetString(PyExc_TypeError,
773 : "code object passed to eval() may not contain free variables");
774 0 : return NULL;
775 : }
776 0 : return PyEval_EvalCode(cmd, globals, locals);
777 : }
778 :
779 0 : cf.cf_flags = PyCF_SOURCE_IS_UTF8;
780 0 : str = source_as_string(cmd, "eval", "string, bytes or code", &cf);
781 0 : if (str == NULL)
782 0 : return NULL;
783 :
784 0 : while (*str == ' ' || *str == '\t')
785 0 : str++;
786 :
787 0 : (void)PyEval_MergeCompilerFlags(&cf);
788 0 : result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
789 0 : Py_XDECREF(tmp);
790 0 : return result;
791 : }
792 :
793 : PyDoc_STRVAR(eval_doc,
794 : "eval(source[, globals[, locals]]) -> value\n\
795 : \n\
796 : Evaluate the source in the context of globals and locals.\n\
797 : The source may be a string representing a Python expression\n\
798 : or a code object as returned by compile().\n\
799 : The globals must be a dictionary and locals can be any mapping,\n\
800 : defaulting to the current globals and locals.\n\
801 : If only globals is given, locals defaults to it.\n");
802 :
803 : static PyObject *
804 48 : builtin_exec(PyObject *self, PyObject *args)
805 : {
806 : PyObject *v;
807 48 : PyObject *prog, *globals = Py_None, *locals = Py_None;
808 :
809 48 : if (!PyArg_UnpackTuple(args, "exec", 1, 3, &prog, &globals, &locals))
810 0 : return NULL;
811 :
812 48 : if (globals == Py_None) {
813 0 : globals = PyEval_GetGlobals();
814 0 : if (locals == Py_None) {
815 0 : locals = PyEval_GetLocals();
816 : }
817 0 : if (!globals || !locals) {
818 0 : PyErr_SetString(PyExc_SystemError,
819 : "globals and locals cannot be NULL");
820 0 : return NULL;
821 : }
822 : }
823 48 : else if (locals == Py_None)
824 48 : locals = globals;
825 :
826 48 : if (!PyDict_Check(globals)) {
827 0 : PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
828 0 : globals->ob_type->tp_name);
829 0 : return NULL;
830 : }
831 48 : if (!PyMapping_Check(locals)) {
832 0 : PyErr_Format(PyExc_TypeError,
833 : "arg 3 must be a mapping or None, not %.100s",
834 0 : locals->ob_type->tp_name);
835 0 : return NULL;
836 : }
837 48 : if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
838 48 : if (PyDict_SetItemString(globals, "__builtins__",
839 : PyEval_GetBuiltins()) != 0)
840 0 : return NULL;
841 : }
842 :
843 48 : if (PyCode_Check(prog)) {
844 46 : if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
845 0 : PyErr_SetString(PyExc_TypeError,
846 : "code object passed to exec() may not "
847 : "contain free variables");
848 0 : return NULL;
849 : }
850 46 : v = PyEval_EvalCode(prog, globals, locals);
851 : }
852 : else {
853 : char *str;
854 : PyCompilerFlags cf;
855 2 : cf.cf_flags = PyCF_SOURCE_IS_UTF8;
856 2 : str = source_as_string(prog, "exec",
857 : "string, bytes or code", &cf);
858 2 : if (str == NULL)
859 0 : return NULL;
860 2 : if (PyEval_MergeCompilerFlags(&cf))
861 2 : v = PyRun_StringFlags(str, Py_file_input, globals,
862 : locals, &cf);
863 : else
864 0 : v = PyRun_String(str, Py_file_input, globals, locals);
865 : }
866 48 : if (v == NULL)
867 0 : return NULL;
868 48 : Py_DECREF(v);
869 48 : Py_RETURN_NONE;
870 : }
871 :
872 : PyDoc_STRVAR(exec_doc,
873 : "exec(object[, globals[, locals]])\n\
874 : \n\
875 : Read and execute code from an object, which can be a string or a code\n\
876 : object.\n\
877 : The globals and locals are dictionaries, defaulting to the current\n\
878 : globals and locals. If only globals is given, locals defaults to it.");
879 :
880 :
881 : static PyObject *
882 883 : builtin_getattr(PyObject *self, PyObject *args)
883 : {
884 883 : PyObject *v, *result, *dflt = NULL;
885 : PyObject *name;
886 :
887 883 : if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
888 0 : return NULL;
889 :
890 883 : if (!PyUnicode_Check(name)) {
891 0 : PyErr_SetString(PyExc_TypeError,
892 : "getattr(): attribute name must be string");
893 0 : return NULL;
894 : }
895 883 : result = PyObject_GetAttr(v, name);
896 1247 : if (result == NULL && dflt != NULL &&
897 364 : PyErr_ExceptionMatches(PyExc_AttributeError))
898 : {
899 364 : PyErr_Clear();
900 364 : Py_INCREF(dflt);
901 364 : result = dflt;
902 : }
903 883 : return result;
904 : }
905 :
906 : PyDoc_STRVAR(getattr_doc,
907 : "getattr(object, name[, default]) -> value\n\
908 : \n\
909 : Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
910 : When a default argument is given, it is returned when the attribute doesn't\n\
911 : exist; without it, an exception is raised in that case.");
912 :
913 :
914 : static PyObject *
915 13 : builtin_globals(PyObject *self)
916 : {
917 : PyObject *d;
918 :
919 13 : d = PyEval_GetGlobals();
920 13 : Py_XINCREF(d);
921 13 : return d;
922 : }
923 :
924 : PyDoc_STRVAR(globals_doc,
925 : "globals() -> dictionary\n\
926 : \n\
927 : Return the dictionary containing the current scope's global variables.");
928 :
929 :
930 : static PyObject *
931 514 : builtin_hasattr(PyObject *self, PyObject *args)
932 : {
933 : PyObject *v;
934 : PyObject *name;
935 :
936 514 : if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
937 0 : return NULL;
938 514 : if (!PyUnicode_Check(name)) {
939 0 : PyErr_SetString(PyExc_TypeError,
940 : "hasattr(): attribute name must be string");
941 0 : return NULL;
942 : }
943 514 : v = PyObject_GetAttr(v, name);
944 514 : if (v == NULL) {
945 108 : if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
946 108 : PyErr_Clear();
947 108 : Py_RETURN_FALSE;
948 : }
949 0 : return NULL;
950 : }
951 406 : Py_DECREF(v);
952 406 : Py_RETURN_TRUE;
953 : }
954 :
955 : PyDoc_STRVAR(hasattr_doc,
956 : "hasattr(object, name) -> bool\n\
957 : \n\
958 : Return whether the object has an attribute with the given name.\n\
959 : (This is done by calling getattr(object, name) and catching AttributeError.)");
960 :
961 :
962 : static PyObject *
963 0 : builtin_id(PyObject *self, PyObject *v)
964 : {
965 0 : return PyLong_FromVoidPtr(v);
966 : }
967 :
968 : PyDoc_STRVAR(id_doc,
969 : "id(object) -> integer\n\
970 : \n\
971 : Return the identity of an object. This is guaranteed to be unique among\n\
972 : simultaneously existing objects. (Hint: it's the object's memory address.)");
973 :
974 :
975 : /* map object ************************************************************/
976 :
977 : typedef struct {
978 : PyObject_HEAD
979 : PyObject *iters;
980 : PyObject *func;
981 : } mapobject;
982 :
983 : static PyObject *
984 2 : map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
985 : {
986 : PyObject *it, *iters, *func;
987 : mapobject *lz;
988 : Py_ssize_t numargs, i;
989 :
990 2 : if (type == &PyMap_Type && !_PyArg_NoKeywords("map()", kwds))
991 0 : return NULL;
992 :
993 2 : numargs = PyTuple_Size(args);
994 2 : if (numargs < 2) {
995 0 : PyErr_SetString(PyExc_TypeError,
996 : "map() must have at least two arguments.");
997 0 : return NULL;
998 : }
999 :
1000 2 : iters = PyTuple_New(numargs-1);
1001 2 : if (iters == NULL)
1002 0 : return NULL;
1003 :
1004 4 : for (i=1 ; i<numargs ; i++) {
1005 : /* Get iterator. */
1006 2 : it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1007 2 : if (it == NULL) {
1008 0 : Py_DECREF(iters);
1009 0 : return NULL;
1010 : }
1011 2 : PyTuple_SET_ITEM(iters, i-1, it);
1012 : }
1013 :
1014 : /* create mapobject structure */
1015 2 : lz = (mapobject *)type->tp_alloc(type, 0);
1016 2 : if (lz == NULL) {
1017 0 : Py_DECREF(iters);
1018 0 : return NULL;
1019 : }
1020 2 : lz->iters = iters;
1021 2 : func = PyTuple_GET_ITEM(args, 0);
1022 2 : Py_INCREF(func);
1023 2 : lz->func = func;
1024 :
1025 2 : return (PyObject *)lz;
1026 : }
1027 :
1028 : static void
1029 2 : map_dealloc(mapobject *lz)
1030 : {
1031 2 : PyObject_GC_UnTrack(lz);
1032 2 : Py_XDECREF(lz->iters);
1033 2 : Py_XDECREF(lz->func);
1034 2 : Py_TYPE(lz)->tp_free(lz);
1035 2 : }
1036 :
1037 : static int
1038 0 : map_traverse(mapobject *lz, visitproc visit, void *arg)
1039 : {
1040 0 : Py_VISIT(lz->iters);
1041 0 : Py_VISIT(lz->func);
1042 0 : return 0;
1043 : }
1044 :
1045 : static PyObject *
1046 11 : map_next(mapobject *lz)
1047 : {
1048 : PyObject *val;
1049 : PyObject *argtuple;
1050 : PyObject *result;
1051 : Py_ssize_t numargs, i;
1052 :
1053 11 : numargs = PyTuple_Size(lz->iters);
1054 11 : argtuple = PyTuple_New(numargs);
1055 11 : if (argtuple == NULL)
1056 0 : return NULL;
1057 :
1058 20 : for (i=0 ; i<numargs ; i++) {
1059 11 : val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i));
1060 11 : if (val == NULL) {
1061 2 : Py_DECREF(argtuple);
1062 2 : return NULL;
1063 : }
1064 9 : PyTuple_SET_ITEM(argtuple, i, val);
1065 : }
1066 9 : result = PyObject_Call(lz->func, argtuple, NULL);
1067 9 : Py_DECREF(argtuple);
1068 9 : return result;
1069 : }
1070 :
1071 : static PyObject *
1072 0 : map_reduce(mapobject *lz)
1073 : {
1074 0 : Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
1075 0 : PyObject *args = PyTuple_New(numargs+1);
1076 : Py_ssize_t i;
1077 0 : if (args == NULL)
1078 0 : return NULL;
1079 0 : Py_INCREF(lz->func);
1080 0 : PyTuple_SET_ITEM(args, 0, lz->func);
1081 0 : for (i = 0; i<numargs; i++){
1082 0 : PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
1083 0 : Py_INCREF(it);
1084 0 : PyTuple_SET_ITEM(args, i+1, it);
1085 : }
1086 :
1087 0 : return Py_BuildValue("ON", Py_TYPE(lz), args);
1088 : }
1089 :
1090 : static PyMethodDef map_methods[] = {
1091 : {"__reduce__", (PyCFunction)map_reduce, METH_NOARGS, reduce_doc},
1092 : {NULL, NULL} /* sentinel */
1093 : };
1094 :
1095 :
1096 : PyDoc_STRVAR(map_doc,
1097 : "map(func, *iterables) --> map object\n\
1098 : \n\
1099 : Make an iterator that computes the function using arguments from\n\
1100 : each of the iterables. Stops when the shortest iterable is exhausted.");
1101 :
1102 : PyTypeObject PyMap_Type = {
1103 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
1104 : "map", /* tp_name */
1105 : sizeof(mapobject), /* tp_basicsize */
1106 : 0, /* tp_itemsize */
1107 : /* methods */
1108 : (destructor)map_dealloc, /* tp_dealloc */
1109 : 0, /* tp_print */
1110 : 0, /* tp_getattr */
1111 : 0, /* tp_setattr */
1112 : 0, /* tp_reserved */
1113 : 0, /* tp_repr */
1114 : 0, /* tp_as_number */
1115 : 0, /* tp_as_sequence */
1116 : 0, /* tp_as_mapping */
1117 : 0, /* tp_hash */
1118 : 0, /* tp_call */
1119 : 0, /* tp_str */
1120 : PyObject_GenericGetAttr, /* tp_getattro */
1121 : 0, /* tp_setattro */
1122 : 0, /* tp_as_buffer */
1123 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1124 : Py_TPFLAGS_BASETYPE, /* tp_flags */
1125 : map_doc, /* tp_doc */
1126 : (traverseproc)map_traverse, /* tp_traverse */
1127 : 0, /* tp_clear */
1128 : 0, /* tp_richcompare */
1129 : 0, /* tp_weaklistoffset */
1130 : PyObject_SelfIter, /* tp_iter */
1131 : (iternextfunc)map_next, /* tp_iternext */
1132 : map_methods, /* tp_methods */
1133 : 0, /* tp_members */
1134 : 0, /* tp_getset */
1135 : 0, /* tp_base */
1136 : 0, /* tp_dict */
1137 : 0, /* tp_descr_get */
1138 : 0, /* tp_descr_set */
1139 : 0, /* tp_dictoffset */
1140 : 0, /* tp_init */
1141 : PyType_GenericAlloc, /* tp_alloc */
1142 : map_new, /* tp_new */
1143 : PyObject_GC_Del, /* tp_free */
1144 : };
1145 :
1146 : static PyObject *
1147 0 : builtin_next(PyObject *self, PyObject *args)
1148 : {
1149 : PyObject *it, *res;
1150 0 : PyObject *def = NULL;
1151 :
1152 0 : if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
1153 0 : return NULL;
1154 0 : if (!PyIter_Check(it)) {
1155 0 : PyErr_Format(PyExc_TypeError,
1156 : "'%.200s' object is not an iterator",
1157 0 : it->ob_type->tp_name);
1158 0 : return NULL;
1159 : }
1160 :
1161 0 : res = (*it->ob_type->tp_iternext)(it);
1162 0 : if (res != NULL) {
1163 0 : return res;
1164 0 : } else if (def != NULL) {
1165 0 : if (PyErr_Occurred()) {
1166 0 : if(!PyErr_ExceptionMatches(PyExc_StopIteration))
1167 0 : return NULL;
1168 0 : PyErr_Clear();
1169 : }
1170 0 : Py_INCREF(def);
1171 0 : return def;
1172 0 : } else if (PyErr_Occurred()) {
1173 0 : return NULL;
1174 : } else {
1175 0 : PyErr_SetNone(PyExc_StopIteration);
1176 0 : return NULL;
1177 : }
1178 : }
1179 :
1180 : PyDoc_STRVAR(next_doc,
1181 : "next(iterator[, default])\n\
1182 : \n\
1183 : Return the next item from the iterator. If default is given and the iterator\n\
1184 : is exhausted, it is returned instead of raising StopIteration.");
1185 :
1186 :
1187 : static PyObject *
1188 409 : builtin_setattr(PyObject *self, PyObject *args)
1189 : {
1190 : PyObject *v;
1191 : PyObject *name;
1192 : PyObject *value;
1193 :
1194 409 : if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
1195 0 : return NULL;
1196 409 : if (PyObject_SetAttr(v, name, value) != 0)
1197 28 : return NULL;
1198 381 : Py_INCREF(Py_None);
1199 381 : return Py_None;
1200 : }
1201 :
1202 : PyDoc_STRVAR(setattr_doc,
1203 : "setattr(object, name, value)\n\
1204 : \n\
1205 : Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
1206 : ``x.y = v''.");
1207 :
1208 :
1209 : static PyObject *
1210 0 : builtin_delattr(PyObject *self, PyObject *args)
1211 : {
1212 : PyObject *v;
1213 : PyObject *name;
1214 :
1215 0 : if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
1216 0 : return NULL;
1217 0 : if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
1218 0 : return NULL;
1219 0 : Py_INCREF(Py_None);
1220 0 : return Py_None;
1221 : }
1222 :
1223 : PyDoc_STRVAR(delattr_doc,
1224 : "delattr(object, name)\n\
1225 : \n\
1226 : Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
1227 : ``del x.y''.");
1228 :
1229 :
1230 : static PyObject *
1231 154 : builtin_hash(PyObject *self, PyObject *v)
1232 : {
1233 : Py_hash_t x;
1234 :
1235 154 : x = PyObject_Hash(v);
1236 154 : if (x == -1)
1237 0 : return NULL;
1238 154 : return PyLong_FromSsize_t(x);
1239 : }
1240 :
1241 : PyDoc_STRVAR(hash_doc,
1242 : "hash(object) -> integer\n\
1243 : \n\
1244 : Return a hash value for the object. Two objects with the same value have\n\
1245 : the same hash value. The reverse is not necessarily true, but likely.");
1246 :
1247 :
1248 : static PyObject *
1249 0 : builtin_hex(PyObject *self, PyObject *v)
1250 : {
1251 0 : return PyNumber_ToBase(v, 16);
1252 : }
1253 :
1254 : PyDoc_STRVAR(hex_doc,
1255 : "hex(number) -> string\n\
1256 : \n\
1257 : Return the hexadecimal representation of an integer.");
1258 :
1259 :
1260 : static PyObject *
1261 12 : builtin_iter(PyObject *self, PyObject *args)
1262 : {
1263 12 : PyObject *v, *w = NULL;
1264 :
1265 12 : if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
1266 0 : return NULL;
1267 12 : if (w == NULL)
1268 12 : return PyObject_GetIter(v);
1269 0 : if (!PyCallable_Check(v)) {
1270 0 : PyErr_SetString(PyExc_TypeError,
1271 : "iter(v, w): v must be callable");
1272 0 : return NULL;
1273 : }
1274 0 : return PyCallIter_New(v, w);
1275 : }
1276 :
1277 : PyDoc_STRVAR(iter_doc,
1278 : "iter(iterable) -> iterator\n\
1279 : iter(callable, sentinel) -> iterator\n\
1280 : \n\
1281 : Get an iterator from an object. In the first form, the argument must\n\
1282 : supply its own iterator, or be a sequence.\n\
1283 : In the second form, the callable is called until it returns the sentinel.");
1284 :
1285 :
1286 : static PyObject *
1287 12953 : builtin_len(PyObject *self, PyObject *v)
1288 : {
1289 : Py_ssize_t res;
1290 :
1291 12953 : res = PyObject_Size(v);
1292 12953 : if (res < 0 && PyErr_Occurred())
1293 0 : return NULL;
1294 12953 : return PyLong_FromSsize_t(res);
1295 : }
1296 :
1297 : PyDoc_STRVAR(len_doc,
1298 : "len(object) -> integer\n\
1299 : \n\
1300 : Return the number of items of a sequence or mapping.");
1301 :
1302 :
1303 : static PyObject *
1304 0 : builtin_locals(PyObject *self)
1305 : {
1306 : PyObject *d;
1307 :
1308 0 : d = PyEval_GetLocals();
1309 0 : Py_XINCREF(d);
1310 0 : return d;
1311 : }
1312 :
1313 : PyDoc_STRVAR(locals_doc,
1314 : "locals() -> dictionary\n\
1315 : \n\
1316 : Update and return a dictionary containing the current scope's local variables.");
1317 :
1318 :
1319 : static PyObject *
1320 1558 : min_max(PyObject *args, PyObject *kwds, int op)
1321 : {
1322 1558 : PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
1323 1558 : const char *name = op == Py_LT ? "min" : "max";
1324 :
1325 1558 : if (PyTuple_Size(args) > 1)
1326 1558 : v = args;
1327 0 : else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
1328 0 : return NULL;
1329 :
1330 1558 : if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
1331 0 : keyfunc = PyDict_GetItemString(kwds, "key");
1332 0 : if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
1333 0 : PyErr_Format(PyExc_TypeError,
1334 : "%s() got an unexpected keyword argument", name);
1335 0 : return NULL;
1336 : }
1337 0 : Py_INCREF(keyfunc);
1338 : }
1339 :
1340 1558 : it = PyObject_GetIter(v);
1341 1558 : if (it == NULL) {
1342 0 : Py_XDECREF(keyfunc);
1343 0 : return NULL;
1344 : }
1345 :
1346 1558 : maxitem = NULL; /* the result */
1347 1558 : maxval = NULL; /* the value associated with the result */
1348 6232 : while (( item = PyIter_Next(it) )) {
1349 : /* get the value from the key function */
1350 3116 : if (keyfunc != NULL) {
1351 0 : val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
1352 0 : if (val == NULL)
1353 0 : goto Fail_it_item;
1354 : }
1355 : /* no key function; the value is the item */
1356 : else {
1357 3116 : val = item;
1358 3116 : Py_INCREF(val);
1359 : }
1360 :
1361 : /* maximum value and item are unset; set them */
1362 3116 : if (maxval == NULL) {
1363 1558 : maxitem = item;
1364 1558 : maxval = val;
1365 : }
1366 : /* maximum value and item are set; update them as necessary */
1367 : else {
1368 1558 : int cmp = PyObject_RichCompareBool(val, maxval, op);
1369 1558 : if (cmp < 0)
1370 0 : goto Fail_it_item_and_val;
1371 1558 : else if (cmp > 0) {
1372 113 : Py_DECREF(maxval);
1373 113 : Py_DECREF(maxitem);
1374 113 : maxval = val;
1375 113 : maxitem = item;
1376 : }
1377 : else {
1378 1445 : Py_DECREF(item);
1379 1445 : Py_DECREF(val);
1380 : }
1381 : }
1382 : }
1383 1558 : if (PyErr_Occurred())
1384 0 : goto Fail_it;
1385 1558 : if (maxval == NULL) {
1386 0 : PyErr_Format(PyExc_ValueError,
1387 : "%s() arg is an empty sequence", name);
1388 : assert(maxitem == NULL);
1389 : }
1390 : else
1391 1558 : Py_DECREF(maxval);
1392 1558 : Py_DECREF(it);
1393 1558 : Py_XDECREF(keyfunc);
1394 1558 : return maxitem;
1395 :
1396 : Fail_it_item_and_val:
1397 0 : Py_DECREF(val);
1398 : Fail_it_item:
1399 0 : Py_DECREF(item);
1400 : Fail_it:
1401 0 : Py_XDECREF(maxval);
1402 0 : Py_XDECREF(maxitem);
1403 0 : Py_DECREF(it);
1404 0 : Py_XDECREF(keyfunc);
1405 0 : return NULL;
1406 : }
1407 :
1408 : static PyObject *
1409 1399 : builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
1410 : {
1411 1399 : return min_max(args, kwds, Py_LT);
1412 : }
1413 :
1414 : PyDoc_STRVAR(min_doc,
1415 : "min(iterable[, key=func]) -> value\n\
1416 : min(a, b, c, ...[, key=func]) -> value\n\
1417 : \n\
1418 : With a single iterable argument, return its smallest item.\n\
1419 : With two or more arguments, return the smallest argument.");
1420 :
1421 :
1422 : static PyObject *
1423 159 : builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
1424 : {
1425 159 : return min_max(args, kwds, Py_GT);
1426 : }
1427 :
1428 : PyDoc_STRVAR(max_doc,
1429 : "max(iterable[, key=func]) -> value\n\
1430 : max(a, b, c, ...[, key=func]) -> value\n\
1431 : \n\
1432 : With a single iterable argument, return its largest item.\n\
1433 : With two or more arguments, return the largest argument.");
1434 :
1435 :
1436 : static PyObject *
1437 0 : builtin_oct(PyObject *self, PyObject *v)
1438 : {
1439 0 : return PyNumber_ToBase(v, 8);
1440 : }
1441 :
1442 : PyDoc_STRVAR(oct_doc,
1443 : "oct(number) -> string\n\
1444 : \n\
1445 : Return the octal representation of an integer.");
1446 :
1447 :
1448 : static PyObject *
1449 1434 : builtin_ord(PyObject *self, PyObject* obj)
1450 : {
1451 : long ord;
1452 : Py_ssize_t size;
1453 :
1454 1434 : if (PyBytes_Check(obj)) {
1455 0 : size = PyBytes_GET_SIZE(obj);
1456 0 : if (size == 1) {
1457 0 : ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
1458 0 : return PyLong_FromLong(ord);
1459 : }
1460 : }
1461 1434 : else if (PyUnicode_Check(obj)) {
1462 1434 : if (PyUnicode_READY(obj) == -1)
1463 0 : return NULL;
1464 1434 : size = PyUnicode_GET_LENGTH(obj);
1465 1434 : if (size == 1) {
1466 1434 : ord = (long)PyUnicode_READ_CHAR(obj, 0);
1467 1434 : return PyLong_FromLong(ord);
1468 : }
1469 : }
1470 0 : else if (PyByteArray_Check(obj)) {
1471 : /* XXX Hopefully this is temporary */
1472 0 : size = PyByteArray_GET_SIZE(obj);
1473 0 : if (size == 1) {
1474 0 : ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj));
1475 0 : return PyLong_FromLong(ord);
1476 : }
1477 : }
1478 : else {
1479 0 : PyErr_Format(PyExc_TypeError,
1480 : "ord() expected string of length 1, but " \
1481 0 : "%.200s found", obj->ob_type->tp_name);
1482 0 : return NULL;
1483 : }
1484 :
1485 0 : PyErr_Format(PyExc_TypeError,
1486 : "ord() expected a character, "
1487 : "but string of length %zd found",
1488 : size);
1489 0 : return NULL;
1490 : }
1491 :
1492 : PyDoc_VAR(ord_doc) = PyDoc_STR(
1493 : "ord(c) -> integer\n\
1494 : \n\
1495 : Return the integer ordinal of a one-character string."
1496 : );
1497 :
1498 :
1499 : static PyObject *
1500 0 : builtin_pow(PyObject *self, PyObject *args)
1501 : {
1502 0 : PyObject *v, *w, *z = Py_None;
1503 :
1504 0 : if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
1505 0 : return NULL;
1506 0 : return PyNumber_Power(v, w, z);
1507 : }
1508 :
1509 : PyDoc_STRVAR(pow_doc,
1510 : "pow(x, y[, z]) -> number\n\
1511 : \n\
1512 : With two arguments, equivalent to x**y. With three arguments,\n\
1513 : equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
1514 :
1515 :
1516 :
1517 : static PyObject *
1518 0 : builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
1519 : {
1520 : static char *kwlist[] = {"sep", "end", "file", "flush", 0};
1521 : static PyObject *dummy_args;
1522 0 : PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
1523 : int i, err;
1524 :
1525 0 : if (dummy_args == NULL && !(dummy_args = PyTuple_New(0)))
1526 0 : return NULL;
1527 0 : if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOOO:print",
1528 : kwlist, &sep, &end, &file, &flush))
1529 0 : return NULL;
1530 0 : if (file == NULL || file == Py_None) {
1531 0 : file = PySys_GetObject("stdout");
1532 : /* sys.stdout may be None when FILE* stdout isn't connected */
1533 0 : if (file == Py_None)
1534 0 : Py_RETURN_NONE;
1535 : }
1536 :
1537 0 : if (sep == Py_None) {
1538 0 : sep = NULL;
1539 : }
1540 0 : else if (sep && !PyUnicode_Check(sep)) {
1541 0 : PyErr_Format(PyExc_TypeError,
1542 : "sep must be None or a string, not %.200s",
1543 0 : sep->ob_type->tp_name);
1544 0 : return NULL;
1545 : }
1546 0 : if (end == Py_None) {
1547 0 : end = NULL;
1548 : }
1549 0 : else if (end && !PyUnicode_Check(end)) {
1550 0 : PyErr_Format(PyExc_TypeError,
1551 : "end must be None or a string, not %.200s",
1552 0 : end->ob_type->tp_name);
1553 0 : return NULL;
1554 : }
1555 :
1556 0 : for (i = 0; i < PyTuple_Size(args); i++) {
1557 0 : if (i > 0) {
1558 0 : if (sep == NULL)
1559 0 : err = PyFile_WriteString(" ", file);
1560 : else
1561 0 : err = PyFile_WriteObject(sep, file,
1562 : Py_PRINT_RAW);
1563 0 : if (err)
1564 0 : return NULL;
1565 : }
1566 0 : err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
1567 : Py_PRINT_RAW);
1568 0 : if (err)
1569 0 : return NULL;
1570 : }
1571 :
1572 0 : if (end == NULL)
1573 0 : err = PyFile_WriteString("\n", file);
1574 : else
1575 0 : err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
1576 0 : if (err)
1577 0 : return NULL;
1578 :
1579 0 : if (flush != NULL) {
1580 : PyObject *tmp;
1581 0 : int do_flush = PyObject_IsTrue(flush);
1582 0 : if (do_flush == -1)
1583 0 : return NULL;
1584 0 : else if (do_flush) {
1585 0 : tmp = PyObject_CallMethod(file, "flush", "");
1586 0 : if (tmp == NULL)
1587 0 : return NULL;
1588 : else
1589 0 : Py_DECREF(tmp);
1590 : }
1591 : }
1592 :
1593 0 : Py_RETURN_NONE;
1594 : }
1595 :
1596 : PyDoc_STRVAR(print_doc,
1597 : "print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\
1598 : \n\
1599 : Prints the values to a stream, or to sys.stdout by default.\n\
1600 : Optional keyword arguments:\n\
1601 : file: a file-like object (stream); defaults to the current sys.stdout.\n\
1602 : sep: string inserted between values, default a space.\n\
1603 : end: string appended after the last value, default a newline.\n\
1604 : flush: whether to forcibly flush the stream.");
1605 :
1606 :
1607 : static PyObject *
1608 0 : builtin_input(PyObject *self, PyObject *args)
1609 : {
1610 0 : PyObject *promptarg = NULL;
1611 0 : PyObject *fin = PySys_GetObject("stdin");
1612 0 : PyObject *fout = PySys_GetObject("stdout");
1613 0 : PyObject *ferr = PySys_GetObject("stderr");
1614 : PyObject *tmp;
1615 : long fd;
1616 : int tty;
1617 :
1618 : /* Parse arguments */
1619 0 : if (!PyArg_UnpackTuple(args, "input", 0, 1, &promptarg))
1620 0 : return NULL;
1621 :
1622 : /* Check that stdin/out/err are intact */
1623 0 : if (fin == NULL || fin == Py_None) {
1624 0 : PyErr_SetString(PyExc_RuntimeError,
1625 : "input(): lost sys.stdin");
1626 0 : return NULL;
1627 : }
1628 0 : if (fout == NULL || fout == Py_None) {
1629 0 : PyErr_SetString(PyExc_RuntimeError,
1630 : "input(): lost sys.stdout");
1631 0 : return NULL;
1632 : }
1633 0 : if (ferr == NULL || ferr == Py_None) {
1634 0 : PyErr_SetString(PyExc_RuntimeError,
1635 : "input(): lost sys.stderr");
1636 0 : return NULL;
1637 : }
1638 :
1639 : /* First of all, flush stderr */
1640 0 : tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
1641 0 : if (tmp == NULL)
1642 0 : PyErr_Clear();
1643 : else
1644 0 : Py_DECREF(tmp);
1645 :
1646 : /* We should only use (GNU) readline if Python's sys.stdin and
1647 : sys.stdout are the same as C's stdin and stdout, because we
1648 : need to pass it those. */
1649 0 : tmp = _PyObject_CallMethodId(fin, &PyId_fileno, "");
1650 0 : if (tmp == NULL) {
1651 0 : PyErr_Clear();
1652 0 : tty = 0;
1653 : }
1654 : else {
1655 0 : fd = PyLong_AsLong(tmp);
1656 0 : Py_DECREF(tmp);
1657 0 : if (fd < 0 && PyErr_Occurred())
1658 0 : return NULL;
1659 0 : tty = fd == fileno(stdin) && isatty(fd);
1660 : }
1661 0 : if (tty) {
1662 0 : tmp = _PyObject_CallMethodId(fout, &PyId_fileno, "");
1663 0 : if (tmp == NULL)
1664 0 : PyErr_Clear();
1665 : else {
1666 0 : fd = PyLong_AsLong(tmp);
1667 0 : Py_DECREF(tmp);
1668 0 : if (fd < 0 && PyErr_Occurred())
1669 0 : return NULL;
1670 0 : tty = fd == fileno(stdout) && isatty(fd);
1671 : }
1672 : }
1673 :
1674 : /* If we're interactive, use (GNU) readline */
1675 0 : if (tty) {
1676 0 : PyObject *po = NULL;
1677 : char *prompt;
1678 0 : char *s = NULL;
1679 0 : PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
1680 0 : PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
1681 : char *stdin_encoding_str, *stdin_errors_str;
1682 : PyObject *result;
1683 : size_t len;
1684 : _Py_IDENTIFIER(encoding);
1685 : _Py_IDENTIFIER(errors);
1686 :
1687 0 : stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
1688 0 : stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
1689 0 : if (!stdin_encoding || !stdin_errors)
1690 : /* stdin is a text stream, so it must have an
1691 : encoding. */
1692 : goto _readline_errors;
1693 0 : stdin_encoding_str = _PyUnicode_AsString(stdin_encoding);
1694 0 : stdin_errors_str = _PyUnicode_AsString(stdin_errors);
1695 0 : if (!stdin_encoding_str || !stdin_errors_str)
1696 : goto _readline_errors;
1697 0 : tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
1698 0 : if (tmp == NULL)
1699 0 : PyErr_Clear();
1700 : else
1701 0 : Py_DECREF(tmp);
1702 0 : if (promptarg != NULL) {
1703 : /* We have a prompt, encode it as stdout would */
1704 : char *stdout_encoding_str, *stdout_errors_str;
1705 : PyObject *stringpo;
1706 0 : stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
1707 0 : stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
1708 0 : if (!stdout_encoding || !stdout_errors)
1709 : goto _readline_errors;
1710 0 : stdout_encoding_str = _PyUnicode_AsString(stdout_encoding);
1711 0 : stdout_errors_str = _PyUnicode_AsString(stdout_errors);
1712 0 : if (!stdout_encoding_str || !stdout_errors_str)
1713 : goto _readline_errors;
1714 0 : stringpo = PyObject_Str(promptarg);
1715 0 : if (stringpo == NULL)
1716 0 : goto _readline_errors;
1717 0 : po = PyUnicode_AsEncodedString(stringpo,
1718 : stdout_encoding_str, stdout_errors_str);
1719 0 : Py_CLEAR(stdout_encoding);
1720 0 : Py_CLEAR(stdout_errors);
1721 0 : Py_CLEAR(stringpo);
1722 0 : if (po == NULL)
1723 0 : goto _readline_errors;
1724 0 : prompt = PyBytes_AsString(po);
1725 0 : if (prompt == NULL)
1726 0 : goto _readline_errors;
1727 : }
1728 : else {
1729 0 : po = NULL;
1730 0 : prompt = "";
1731 : }
1732 0 : s = PyOS_Readline(stdin, stdout, prompt);
1733 0 : if (s == NULL) {
1734 0 : if (!PyErr_Occurred())
1735 0 : PyErr_SetNone(PyExc_KeyboardInterrupt);
1736 0 : goto _readline_errors;
1737 : }
1738 :
1739 0 : len = strlen(s);
1740 0 : if (len == 0) {
1741 0 : PyErr_SetNone(PyExc_EOFError);
1742 0 : result = NULL;
1743 : }
1744 : else {
1745 0 : if (len > PY_SSIZE_T_MAX) {
1746 0 : PyErr_SetString(PyExc_OverflowError,
1747 : "input: input too long");
1748 0 : result = NULL;
1749 : }
1750 : else {
1751 0 : len--; /* strip trailing '\n' */
1752 0 : if (len != 0 && s[len-1] == '\r')
1753 0 : len--; /* strip trailing '\r' */
1754 0 : result = PyUnicode_Decode(s, len, stdin_encoding_str,
1755 : stdin_errors_str);
1756 : }
1757 : }
1758 0 : Py_DECREF(stdin_encoding);
1759 0 : Py_DECREF(stdin_errors);
1760 0 : Py_XDECREF(po);
1761 0 : PyMem_FREE(s);
1762 0 : return result;
1763 : _readline_errors:
1764 0 : Py_XDECREF(stdin_encoding);
1765 0 : Py_XDECREF(stdout_encoding);
1766 0 : Py_XDECREF(stdin_errors);
1767 0 : Py_XDECREF(stdout_errors);
1768 0 : Py_XDECREF(po);
1769 0 : return NULL;
1770 : }
1771 :
1772 : /* Fallback if we're not interactive */
1773 0 : if (promptarg != NULL) {
1774 0 : if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0)
1775 0 : return NULL;
1776 : }
1777 0 : tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
1778 0 : if (tmp == NULL)
1779 0 : PyErr_Clear();
1780 : else
1781 0 : Py_DECREF(tmp);
1782 0 : return PyFile_GetLine(fin, -1);
1783 : }
1784 :
1785 : PyDoc_STRVAR(input_doc,
1786 : "input([prompt]) -> string\n\
1787 : \n\
1788 : Read a string from standard input. The trailing newline is stripped.\n\
1789 : If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1790 : On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1791 : is printed without a trailing newline before reading.");
1792 :
1793 :
1794 : static PyObject *
1795 2 : builtin_repr(PyObject *self, PyObject *v)
1796 : {
1797 2 : return PyObject_Repr(v);
1798 : }
1799 :
1800 : PyDoc_STRVAR(repr_doc,
1801 : "repr(object) -> string\n\
1802 : \n\
1803 : Return the canonical string representation of the object.\n\
1804 : For most object types, eval(repr(object)) == object.");
1805 :
1806 :
1807 : static PyObject *
1808 0 : builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
1809 : {
1810 : static PyObject *round_str = NULL;
1811 0 : PyObject *ndigits = NULL;
1812 : static char *kwlist[] = {"number", "ndigits", 0};
1813 : PyObject *number, *round;
1814 :
1815 0 : if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:round",
1816 : kwlist, &number, &ndigits))
1817 0 : return NULL;
1818 :
1819 0 : if (Py_TYPE(number)->tp_dict == NULL) {
1820 0 : if (PyType_Ready(Py_TYPE(number)) < 0)
1821 0 : return NULL;
1822 : }
1823 :
1824 0 : if (round_str == NULL) {
1825 0 : round_str = PyUnicode_InternFromString("__round__");
1826 0 : if (round_str == NULL)
1827 0 : return NULL;
1828 : }
1829 :
1830 0 : round = _PyType_Lookup(Py_TYPE(number), round_str);
1831 0 : if (round == NULL) {
1832 0 : PyErr_Format(PyExc_TypeError,
1833 : "type %.100s doesn't define __round__ method",
1834 0 : Py_TYPE(number)->tp_name);
1835 0 : return NULL;
1836 : }
1837 :
1838 0 : if (ndigits == NULL)
1839 0 : return PyObject_CallFunction(round, "O", number);
1840 : else
1841 0 : return PyObject_CallFunction(round, "OO", number, ndigits);
1842 : }
1843 :
1844 : PyDoc_STRVAR(round_doc,
1845 : "round(number[, ndigits]) -> number\n\
1846 : \n\
1847 : Round a number to a given precision in decimal digits (default 0 digits).\n\
1848 : This returns an int when called with one argument, otherwise the\n\
1849 : same type as the number. ndigits may be negative.");
1850 :
1851 :
1852 : static PyObject *
1853 0 : builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
1854 : {
1855 0 : PyObject *newlist, *v, *seq, *keyfunc=NULL, *newargs;
1856 : PyObject *callable;
1857 : static char *kwlist[] = {"iterable", "key", "reverse", 0};
1858 : int reverse;
1859 : _Py_IDENTIFIER(sort);
1860 :
1861 : /* args 1-3 should match listsort in Objects/listobject.c */
1862 0 : if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted",
1863 : kwlist, &seq, &keyfunc, &reverse))
1864 0 : return NULL;
1865 :
1866 0 : newlist = PySequence_List(seq);
1867 0 : if (newlist == NULL)
1868 0 : return NULL;
1869 :
1870 0 : callable = _PyObject_GetAttrId(newlist, &PyId_sort);
1871 0 : if (callable == NULL) {
1872 0 : Py_DECREF(newlist);
1873 0 : return NULL;
1874 : }
1875 :
1876 0 : newargs = PyTuple_GetSlice(args, 1, 4);
1877 0 : if (newargs == NULL) {
1878 0 : Py_DECREF(newlist);
1879 0 : Py_DECREF(callable);
1880 0 : return NULL;
1881 : }
1882 :
1883 0 : v = PyObject_Call(callable, newargs, kwds);
1884 0 : Py_DECREF(newargs);
1885 0 : Py_DECREF(callable);
1886 0 : if (v == NULL) {
1887 0 : Py_DECREF(newlist);
1888 0 : return NULL;
1889 : }
1890 0 : Py_DECREF(v);
1891 0 : return newlist;
1892 : }
1893 :
1894 : PyDoc_STRVAR(sorted_doc,
1895 : "sorted(iterable, key=None, reverse=False) --> new sorted list");
1896 :
1897 : static PyObject *
1898 0 : builtin_vars(PyObject *self, PyObject *args)
1899 : {
1900 0 : PyObject *v = NULL;
1901 : PyObject *d;
1902 :
1903 0 : if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
1904 0 : return NULL;
1905 0 : if (v == NULL) {
1906 0 : d = PyEval_GetLocals();
1907 0 : if (d == NULL) {
1908 0 : if (!PyErr_Occurred())
1909 0 : PyErr_SetString(PyExc_SystemError,
1910 : "vars(): no locals!?");
1911 : }
1912 : else
1913 0 : Py_INCREF(d);
1914 : }
1915 : else {
1916 : _Py_IDENTIFIER(__dict__);
1917 0 : d = _PyObject_GetAttrId(v, &PyId___dict__);
1918 0 : if (d == NULL) {
1919 0 : PyErr_SetString(PyExc_TypeError,
1920 : "vars() argument must have __dict__ attribute");
1921 0 : return NULL;
1922 : }
1923 : }
1924 0 : return d;
1925 : }
1926 :
1927 : PyDoc_STRVAR(vars_doc,
1928 : "vars([object]) -> dictionary\n\
1929 : \n\
1930 : Without arguments, equivalent to locals().\n\
1931 : With an argument, equivalent to object.__dict__.");
1932 :
1933 : static PyObject*
1934 0 : builtin_sum(PyObject *self, PyObject *args)
1935 : {
1936 : PyObject *seq;
1937 0 : PyObject *result = NULL;
1938 : PyObject *temp, *item, *iter;
1939 :
1940 0 : if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
1941 0 : return NULL;
1942 :
1943 0 : iter = PyObject_GetIter(seq);
1944 0 : if (iter == NULL)
1945 0 : return NULL;
1946 :
1947 0 : if (result == NULL) {
1948 0 : result = PyLong_FromLong(0);
1949 0 : if (result == NULL) {
1950 0 : Py_DECREF(iter);
1951 0 : return NULL;
1952 : }
1953 : } else {
1954 : /* reject string values for 'start' parameter */
1955 0 : if (PyUnicode_Check(result)) {
1956 0 : PyErr_SetString(PyExc_TypeError,
1957 : "sum() can't sum strings [use ''.join(seq) instead]");
1958 0 : Py_DECREF(iter);
1959 0 : return NULL;
1960 : }
1961 0 : if (PyBytes_Check(result)) {
1962 0 : PyErr_SetString(PyExc_TypeError,
1963 : "sum() can't sum bytes [use b''.join(seq) instead]");
1964 0 : Py_DECREF(iter);
1965 0 : return NULL;
1966 : }
1967 0 : if (PyByteArray_Check(result)) {
1968 0 : PyErr_SetString(PyExc_TypeError,
1969 : "sum() can't sum bytearray [use b''.join(seq) instead]");
1970 0 : Py_DECREF(iter);
1971 0 : return NULL;
1972 : }
1973 :
1974 0 : Py_INCREF(result);
1975 : }
1976 :
1977 : #ifndef SLOW_SUM
1978 : /* Fast addition by keeping temporary sums in C instead of new Python objects.
1979 : Assumes all inputs are the same type. If the assumption fails, default
1980 : to the more general routine.
1981 : */
1982 0 : if (PyLong_CheckExact(result)) {
1983 : int overflow;
1984 0 : long i_result = PyLong_AsLongAndOverflow(result, &overflow);
1985 : /* If this already overflowed, don't even enter the loop. */
1986 0 : if (overflow == 0) {
1987 0 : Py_DECREF(result);
1988 0 : result = NULL;
1989 : }
1990 0 : while(result == NULL) {
1991 0 : item = PyIter_Next(iter);
1992 0 : if (item == NULL) {
1993 0 : Py_DECREF(iter);
1994 0 : if (PyErr_Occurred())
1995 0 : return NULL;
1996 0 : return PyLong_FromLong(i_result);
1997 : }
1998 0 : if (PyLong_CheckExact(item)) {
1999 0 : long b = PyLong_AsLongAndOverflow(item, &overflow);
2000 0 : long x = i_result + b;
2001 0 : if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
2002 0 : i_result = x;
2003 0 : Py_DECREF(item);
2004 0 : continue;
2005 : }
2006 : }
2007 : /* Either overflowed or is not an int. Restore real objects and process normally */
2008 0 : result = PyLong_FromLong(i_result);
2009 0 : temp = PyNumber_Add(result, item);
2010 0 : Py_DECREF(result);
2011 0 : Py_DECREF(item);
2012 0 : result = temp;
2013 0 : if (result == NULL) {
2014 0 : Py_DECREF(iter);
2015 0 : return NULL;
2016 : }
2017 : }
2018 : }
2019 :
2020 0 : if (PyFloat_CheckExact(result)) {
2021 0 : double f_result = PyFloat_AS_DOUBLE(result);
2022 0 : Py_DECREF(result);
2023 0 : result = NULL;
2024 0 : while(result == NULL) {
2025 0 : item = PyIter_Next(iter);
2026 0 : if (item == NULL) {
2027 0 : Py_DECREF(iter);
2028 0 : if (PyErr_Occurred())
2029 0 : return NULL;
2030 0 : return PyFloat_FromDouble(f_result);
2031 : }
2032 0 : if (PyFloat_CheckExact(item)) {
2033 : PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2034 0 : f_result += PyFloat_AS_DOUBLE(item);
2035 : PyFPE_END_PROTECT(f_result)
2036 0 : Py_DECREF(item);
2037 0 : continue;
2038 : }
2039 0 : if (PyLong_CheckExact(item)) {
2040 : long value;
2041 : int overflow;
2042 0 : value = PyLong_AsLongAndOverflow(item, &overflow);
2043 0 : if (!overflow) {
2044 : PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
2045 0 : f_result += (double)value;
2046 : PyFPE_END_PROTECT(f_result)
2047 0 : Py_DECREF(item);
2048 0 : continue;
2049 : }
2050 : }
2051 0 : result = PyFloat_FromDouble(f_result);
2052 0 : temp = PyNumber_Add(result, item);
2053 0 : Py_DECREF(result);
2054 0 : Py_DECREF(item);
2055 0 : result = temp;
2056 0 : if (result == NULL) {
2057 0 : Py_DECREF(iter);
2058 0 : return NULL;
2059 : }
2060 : }
2061 : }
2062 : #endif
2063 :
2064 : for(;;) {
2065 0 : item = PyIter_Next(iter);
2066 0 : if (item == NULL) {
2067 : /* error, or end-of-sequence */
2068 0 : if (PyErr_Occurred()) {
2069 0 : Py_DECREF(result);
2070 0 : result = NULL;
2071 : }
2072 0 : break;
2073 : }
2074 : /* It's tempting to use PyNumber_InPlaceAdd instead of
2075 : PyNumber_Add here, to avoid quadratic running time
2076 : when doing 'sum(list_of_lists, [])'. However, this
2077 : would produce a change in behaviour: a snippet like
2078 :
2079 : empty = []
2080 : sum([[x] for x in range(10)], empty)
2081 :
2082 : would change the value of empty. */
2083 0 : temp = PyNumber_Add(result, item);
2084 0 : Py_DECREF(result);
2085 0 : Py_DECREF(item);
2086 0 : result = temp;
2087 0 : if (result == NULL)
2088 0 : break;
2089 0 : }
2090 0 : Py_DECREF(iter);
2091 0 : return result;
2092 : }
2093 :
2094 : PyDoc_STRVAR(sum_doc,
2095 : "sum(iterable[, start]) -> value\n\
2096 : \n\
2097 : Returns the sum of an iterable of numbers (NOT strings) plus the value\n\
2098 : of parameter 'start' (which defaults to 0). When the iterable is\n\
2099 : empty, returns start.");
2100 :
2101 :
2102 : static PyObject *
2103 3183 : builtin_isinstance(PyObject *self, PyObject *args)
2104 : {
2105 : PyObject *inst;
2106 : PyObject *cls;
2107 : int retval;
2108 :
2109 3183 : if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
2110 0 : return NULL;
2111 :
2112 3183 : retval = PyObject_IsInstance(inst, cls);
2113 3183 : if (retval < 0)
2114 0 : return NULL;
2115 3183 : return PyBool_FromLong(retval);
2116 : }
2117 :
2118 : PyDoc_STRVAR(isinstance_doc,
2119 : "isinstance(object, class-or-type-or-tuple) -> bool\n\
2120 : \n\
2121 : Return whether an object is an instance of a class or of a subclass thereof.\n\
2122 : With a type as second argument, return whether that is the object's type.\n\
2123 : The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
2124 : isinstance(x, A) or isinstance(x, B) or ... (etc.).");
2125 :
2126 :
2127 : static PyObject *
2128 72 : builtin_issubclass(PyObject *self, PyObject *args)
2129 : {
2130 : PyObject *derived;
2131 : PyObject *cls;
2132 : int retval;
2133 :
2134 72 : if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
2135 0 : return NULL;
2136 :
2137 72 : retval = PyObject_IsSubclass(derived, cls);
2138 72 : if (retval < 0)
2139 0 : return NULL;
2140 72 : return PyBool_FromLong(retval);
2141 : }
2142 :
2143 : PyDoc_STRVAR(issubclass_doc,
2144 : "issubclass(C, B) -> bool\n\
2145 : \n\
2146 : Return whether class C is a subclass (i.e., a derived class) of class B.\n\
2147 : When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
2148 : is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
2149 :
2150 :
2151 : typedef struct {
2152 : PyObject_HEAD
2153 : Py_ssize_t tuplesize;
2154 : PyObject *ittuple; /* tuple of iterators */
2155 : PyObject *result;
2156 : } zipobject;
2157 :
2158 : static PyObject *
2159 1 : zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2160 : {
2161 : zipobject *lz;
2162 : Py_ssize_t i;
2163 : PyObject *ittuple; /* tuple of iterators */
2164 : PyObject *result;
2165 1 : Py_ssize_t tuplesize = PySequence_Length(args);
2166 :
2167 1 : if (type == &PyZip_Type && !_PyArg_NoKeywords("zip()", kwds))
2168 0 : return NULL;
2169 :
2170 : /* args must be a tuple */
2171 : assert(PyTuple_Check(args));
2172 :
2173 : /* obtain iterators */
2174 1 : ittuple = PyTuple_New(tuplesize);
2175 1 : if (ittuple == NULL)
2176 0 : return NULL;
2177 1 : for (i=0; i < tuplesize; ++i) {
2178 0 : PyObject *item = PyTuple_GET_ITEM(args, i);
2179 0 : PyObject *it = PyObject_GetIter(item);
2180 0 : if (it == NULL) {
2181 0 : if (PyErr_ExceptionMatches(PyExc_TypeError))
2182 0 : PyErr_Format(PyExc_TypeError,
2183 : "zip argument #%zd must support iteration",
2184 : i+1);
2185 0 : Py_DECREF(ittuple);
2186 0 : return NULL;
2187 : }
2188 0 : PyTuple_SET_ITEM(ittuple, i, it);
2189 : }
2190 :
2191 : /* create a result holder */
2192 1 : result = PyTuple_New(tuplesize);
2193 1 : if (result == NULL) {
2194 0 : Py_DECREF(ittuple);
2195 0 : return NULL;
2196 : }
2197 1 : for (i=0 ; i < tuplesize ; i++) {
2198 0 : Py_INCREF(Py_None);
2199 0 : PyTuple_SET_ITEM(result, i, Py_None);
2200 : }
2201 :
2202 : /* create zipobject structure */
2203 1 : lz = (zipobject *)type->tp_alloc(type, 0);
2204 1 : if (lz == NULL) {
2205 0 : Py_DECREF(ittuple);
2206 0 : Py_DECREF(result);
2207 0 : return NULL;
2208 : }
2209 1 : lz->ittuple = ittuple;
2210 1 : lz->tuplesize = tuplesize;
2211 1 : lz->result = result;
2212 :
2213 1 : return (PyObject *)lz;
2214 : }
2215 :
2216 : static void
2217 1 : zip_dealloc(zipobject *lz)
2218 : {
2219 1 : PyObject_GC_UnTrack(lz);
2220 1 : Py_XDECREF(lz->ittuple);
2221 1 : Py_XDECREF(lz->result);
2222 1 : Py_TYPE(lz)->tp_free(lz);
2223 1 : }
2224 :
2225 : static int
2226 0 : zip_traverse(zipobject *lz, visitproc visit, void *arg)
2227 : {
2228 0 : Py_VISIT(lz->ittuple);
2229 0 : Py_VISIT(lz->result);
2230 0 : return 0;
2231 : }
2232 :
2233 : static PyObject *
2234 0 : zip_next(zipobject *lz)
2235 : {
2236 : Py_ssize_t i;
2237 0 : Py_ssize_t tuplesize = lz->tuplesize;
2238 0 : PyObject *result = lz->result;
2239 : PyObject *it;
2240 : PyObject *item;
2241 : PyObject *olditem;
2242 :
2243 0 : if (tuplesize == 0)
2244 0 : return NULL;
2245 0 : if (Py_REFCNT(result) == 1) {
2246 0 : Py_INCREF(result);
2247 0 : for (i=0 ; i < tuplesize ; i++) {
2248 0 : it = PyTuple_GET_ITEM(lz->ittuple, i);
2249 0 : item = (*Py_TYPE(it)->tp_iternext)(it);
2250 0 : if (item == NULL) {
2251 0 : Py_DECREF(result);
2252 0 : return NULL;
2253 : }
2254 0 : olditem = PyTuple_GET_ITEM(result, i);
2255 0 : PyTuple_SET_ITEM(result, i, item);
2256 0 : Py_DECREF(olditem);
2257 : }
2258 : } else {
2259 0 : result = PyTuple_New(tuplesize);
2260 0 : if (result == NULL)
2261 0 : return NULL;
2262 0 : for (i=0 ; i < tuplesize ; i++) {
2263 0 : it = PyTuple_GET_ITEM(lz->ittuple, i);
2264 0 : item = (*Py_TYPE(it)->tp_iternext)(it);
2265 0 : if (item == NULL) {
2266 0 : Py_DECREF(result);
2267 0 : return NULL;
2268 : }
2269 0 : PyTuple_SET_ITEM(result, i, item);
2270 : }
2271 : }
2272 0 : return result;
2273 : }
2274 :
2275 : static PyObject *
2276 0 : zip_reduce(zipobject *lz)
2277 : {
2278 : /* Just recreate the zip with the internal iterator tuple */
2279 0 : return Py_BuildValue("OO", Py_TYPE(lz), lz->ittuple);
2280 : }
2281 :
2282 : static PyMethodDef zip_methods[] = {
2283 : {"__reduce__", (PyCFunction)zip_reduce, METH_NOARGS, reduce_doc},
2284 : {NULL, NULL} /* sentinel */
2285 : };
2286 :
2287 : PyDoc_STRVAR(zip_doc,
2288 : "zip(iter1 [,iter2 [...]]) --> zip object\n\
2289 : \n\
2290 : Return a zip object whose .__next__() method returns a tuple where\n\
2291 : the i-th element comes from the i-th iterable argument. The .__next__()\n\
2292 : method continues until the shortest iterable in the argument sequence\n\
2293 : is exhausted and then it raises StopIteration.");
2294 :
2295 : PyTypeObject PyZip_Type = {
2296 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
2297 : "zip", /* tp_name */
2298 : sizeof(zipobject), /* tp_basicsize */
2299 : 0, /* tp_itemsize */
2300 : /* methods */
2301 : (destructor)zip_dealloc, /* tp_dealloc */
2302 : 0, /* tp_print */
2303 : 0, /* tp_getattr */
2304 : 0, /* tp_setattr */
2305 : 0, /* tp_reserved */
2306 : 0, /* tp_repr */
2307 : 0, /* tp_as_number */
2308 : 0, /* tp_as_sequence */
2309 : 0, /* tp_as_mapping */
2310 : 0, /* tp_hash */
2311 : 0, /* tp_call */
2312 : 0, /* tp_str */
2313 : PyObject_GenericGetAttr, /* tp_getattro */
2314 : 0, /* tp_setattro */
2315 : 0, /* tp_as_buffer */
2316 : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2317 : Py_TPFLAGS_BASETYPE, /* tp_flags */
2318 : zip_doc, /* tp_doc */
2319 : (traverseproc)zip_traverse, /* tp_traverse */
2320 : 0, /* tp_clear */
2321 : 0, /* tp_richcompare */
2322 : 0, /* tp_weaklistoffset */
2323 : PyObject_SelfIter, /* tp_iter */
2324 : (iternextfunc)zip_next, /* tp_iternext */
2325 : zip_methods, /* tp_methods */
2326 : 0, /* tp_members */
2327 : 0, /* tp_getset */
2328 : 0, /* tp_base */
2329 : 0, /* tp_dict */
2330 : 0, /* tp_descr_get */
2331 : 0, /* tp_descr_set */
2332 : 0, /* tp_dictoffset */
2333 : 0, /* tp_init */
2334 : PyType_GenericAlloc, /* tp_alloc */
2335 : zip_new, /* tp_new */
2336 : PyObject_GC_Del, /* tp_free */
2337 : };
2338 :
2339 :
2340 : static PyMethodDef builtin_methods[] = {
2341 : {"__build_class__", (PyCFunction)builtin___build_class__,
2342 : METH_VARARGS | METH_KEYWORDS, build_class_doc},
2343 : {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
2344 : {"abs", builtin_abs, METH_O, abs_doc},
2345 : {"all", builtin_all, METH_O, all_doc},
2346 : {"any", builtin_any, METH_O, any_doc},
2347 : {"ascii", builtin_ascii, METH_O, ascii_doc},
2348 : {"bin", builtin_bin, METH_O, bin_doc},
2349 : {"callable", builtin_callable, METH_O, callable_doc},
2350 : {"chr", builtin_chr, METH_VARARGS, chr_doc},
2351 : {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
2352 : {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
2353 : {"dir", builtin_dir, METH_VARARGS, dir_doc},
2354 : {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
2355 : {"eval", builtin_eval, METH_VARARGS, eval_doc},
2356 : {"exec", builtin_exec, METH_VARARGS, exec_doc},
2357 : {"format", builtin_format, METH_VARARGS, format_doc},
2358 : {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
2359 : {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2360 : {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2361 : {"hash", builtin_hash, METH_O, hash_doc},
2362 : {"hex", builtin_hex, METH_O, hex_doc},
2363 : {"id", builtin_id, METH_O, id_doc},
2364 : {"input", builtin_input, METH_VARARGS, input_doc},
2365 : {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2366 : {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2367 : {"iter", builtin_iter, METH_VARARGS, iter_doc},
2368 : {"len", builtin_len, METH_O, len_doc},
2369 : {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
2370 : {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
2371 : {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
2372 : {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc},
2373 : {"oct", builtin_oct, METH_O, oct_doc},
2374 : {"ord", builtin_ord, METH_O, ord_doc},
2375 : {"pow", builtin_pow, METH_VARARGS, pow_doc},
2376 : {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
2377 : {"repr", builtin_repr, METH_O, repr_doc},
2378 : {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
2379 : {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
2380 : {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
2381 : {"sum", builtin_sum, METH_VARARGS, sum_doc},
2382 : {"vars", builtin_vars, METH_VARARGS, vars_doc},
2383 : {NULL, NULL},
2384 : };
2385 :
2386 : PyDoc_STRVAR(builtin_doc,
2387 : "Built-in functions, exceptions, and other objects.\n\
2388 : \n\
2389 : Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
2390 :
2391 : static struct PyModuleDef builtinsmodule = {
2392 : PyModuleDef_HEAD_INIT,
2393 : "builtins",
2394 : builtin_doc,
2395 : -1, /* multiple "initialization" just copies the module dict. */
2396 : builtin_methods,
2397 : NULL,
2398 : NULL,
2399 : NULL,
2400 : NULL
2401 : };
2402 :
2403 :
2404 : PyObject *
2405 1 : _PyBuiltin_Init(void)
2406 : {
2407 : PyObject *mod, *dict, *debug;
2408 1 : mod = PyModule_Create(&builtinsmodule);
2409 1 : if (mod == NULL)
2410 0 : return NULL;
2411 1 : dict = PyModule_GetDict(mod);
2412 :
2413 : #ifdef Py_TRACE_REFS
2414 : /* "builtins" exposes a number of statically allocated objects
2415 : * that, before this code was added in 2.3, never showed up in
2416 : * the list of "all objects" maintained by Py_TRACE_REFS. As a
2417 : * result, programs leaking references to None and False (etc)
2418 : * couldn't be diagnosed by examining sys.getobjects(0).
2419 : */
2420 : #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
2421 : #else
2422 : #define ADD_TO_ALL(OBJECT) (void)0
2423 : #endif
2424 :
2425 : #define SETBUILTIN(NAME, OBJECT) \
2426 : if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
2427 : return NULL; \
2428 : ADD_TO_ALL(OBJECT)
2429 :
2430 1 : SETBUILTIN("None", Py_None);
2431 1 : SETBUILTIN("Ellipsis", Py_Ellipsis);
2432 1 : SETBUILTIN("NotImplemented", Py_NotImplemented);
2433 1 : SETBUILTIN("False", Py_False);
2434 1 : SETBUILTIN("True", Py_True);
2435 1 : SETBUILTIN("bool", &PyBool_Type);
2436 1 : SETBUILTIN("memoryview", &PyMemoryView_Type);
2437 1 : SETBUILTIN("bytearray", &PyByteArray_Type);
2438 1 : SETBUILTIN("bytes", &PyBytes_Type);
2439 1 : SETBUILTIN("classmethod", &PyClassMethod_Type);
2440 1 : SETBUILTIN("complex", &PyComplex_Type);
2441 1 : SETBUILTIN("dict", &PyDict_Type);
2442 1 : SETBUILTIN("enumerate", &PyEnum_Type);
2443 1 : SETBUILTIN("filter", &PyFilter_Type);
2444 1 : SETBUILTIN("float", &PyFloat_Type);
2445 1 : SETBUILTIN("frozenset", &PyFrozenSet_Type);
2446 1 : SETBUILTIN("property", &PyProperty_Type);
2447 1 : SETBUILTIN("int", &PyLong_Type);
2448 1 : SETBUILTIN("list", &PyList_Type);
2449 1 : SETBUILTIN("map", &PyMap_Type);
2450 1 : SETBUILTIN("object", &PyBaseObject_Type);
2451 1 : SETBUILTIN("range", &PyRange_Type);
2452 1 : SETBUILTIN("reversed", &PyReversed_Type);
2453 1 : SETBUILTIN("set", &PySet_Type);
2454 1 : SETBUILTIN("slice", &PySlice_Type);
2455 1 : SETBUILTIN("staticmethod", &PyStaticMethod_Type);
2456 1 : SETBUILTIN("str", &PyUnicode_Type);
2457 1 : SETBUILTIN("super", &PySuper_Type);
2458 1 : SETBUILTIN("tuple", &PyTuple_Type);
2459 1 : SETBUILTIN("type", &PyType_Type);
2460 1 : SETBUILTIN("zip", &PyZip_Type);
2461 1 : debug = PyBool_FromLong(Py_OptimizeFlag == 0);
2462 1 : if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
2463 0 : Py_XDECREF(debug);
2464 0 : return NULL;
2465 : }
2466 1 : Py_XDECREF(debug);
2467 :
2468 1 : return mod;
2469 : #undef ADD_TO_ALL
2470 : #undef SETBUILTIN
2471 : }
|