annotate Python/bltinmodule.c @ 43855:ee081609cee8

Added some additional checks for sys.std?? is None, see #1440
author Christian Heimes <christian@cheimes.de>
date Thu, 15 Nov 2007 02:26:46 +0000
parents 2c97e24b16cf
children cbc8b3bd3157
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Built-in functions */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #include "node.h"
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35636
diff changeset
6 #include "code.h"
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
7 #include "eval.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
5317
c72c7e1930b3 Keep gcc -Wall and Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5223
diff changeset
9 #include <ctype.h>
c72c7e1930b3 Keep gcc -Wall and Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5223
diff changeset
10
17822
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
11 /* The default encoding used by the platform file system APIs
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
12 Can remain NULL for all platforms that don't have such a concept
43569
19319e70fc3e Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
13
19319e70fc3e Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
14 Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the
19319e70fc3e Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 43487
diff changeset
15 values for Py_FileSystemDefaultEncoding!
17822
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
16 */
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23868
diff changeset
17 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
17822
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
18 const char *Py_FileSystemDefaultEncoding = "mbcs";
27782
904f0e769820 My previous checkin caused compile() to no longer accept buffers, as noted
Just van Rossum <just@letterror.com>
parents: 27781
diff changeset
19 #elif defined(__APPLE__)
904f0e769820 My previous checkin caused compile() to no longer accept buffers, as noted
Just van Rossum <just@letterror.com>
parents: 27781
diff changeset
20 const char *Py_FileSystemDefaultEncoding = "utf-8";
17822
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
21 #else
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
22 const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
23 #endif
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17694
diff changeset
24
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
25 static PyObject *
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
26 builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
27 {
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
28 PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
29 PyObject *cls = NULL;
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
30 Py_ssize_t nargs, nbases;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
31
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
32 assert(args != NULL);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
33 if (!PyTuple_Check(args)) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
34 PyErr_SetString(PyExc_TypeError,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
35 "__build_class__: args is not a tuple");
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
36 return NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
37 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
38 nargs = PyTuple_GET_SIZE(args);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
39 if (nargs < 2) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
40 PyErr_SetString(PyExc_TypeError,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
41 "__build_class__: not enough arguments");
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
42 return NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
43 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
44 func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
45 name = PyTuple_GET_ITEM(args, 1);
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
46 if (!PyUnicode_Check(name)) {
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
47 PyErr_SetString(PyExc_TypeError,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
48 "__build_class__: name is not a string");
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
49 return NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
50 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
51 bases = PyTuple_GetSlice(args, 2, nargs);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
52 if (bases == NULL)
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
53 return NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
54 nbases = nargs - 2;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
55
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
56 if (kwds == NULL) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
57 meta = NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
58 mkw = NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
59 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
60 else {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
61 mkw = PyDict_Copy(kwds); /* Don't modify kwds passed in! */
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
62 if (mkw == NULL) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
63 Py_DECREF(bases);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
64 return NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
65 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
66 meta = PyDict_GetItemString(mkw, "metaclass");
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
67 if (meta != NULL) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
68 Py_INCREF(meta);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
69 if (PyDict_DelItemString(mkw, "metaclass") < 0) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
70 Py_DECREF(meta);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
71 Py_DECREF(mkw);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
72 Py_DECREF(bases);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
73 return NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
74 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
75 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
76 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
77 if (meta == NULL) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
78 if (PyTuple_GET_SIZE(bases) == 0)
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
79 meta = (PyObject *) (&PyType_Type);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
80 else {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
81 PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
82 meta = (PyObject *) (base0->ob_type);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
83 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
84 Py_INCREF(meta);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
85 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
86 prep = PyObject_GetAttrString(meta, "__prepare__");
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
87 if (prep == NULL) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
88 PyErr_Clear();
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
89 ns = PyDict_New();
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
90 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
91 else {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
92 PyObject *pargs = Py_BuildValue("OO", name, bases);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
93 if (pargs == NULL) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
94 Py_DECREF(prep);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
95 Py_DECREF(meta);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
96 Py_XDECREF(mkw);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
97 Py_DECREF(bases);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
98 return NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
99 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
100 ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
101 Py_DECREF(pargs);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
102 Py_DECREF(prep);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
103 if (ns == NULL) {
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
104 Py_DECREF(meta);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
105 Py_XDECREF(mkw);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
106 Py_DECREF(bases);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
107 return NULL;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
108 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
109 }
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
110 cell = PyObject_CallFunctionObjArgs(func, ns, NULL);
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
111 if (cell != NULL) {
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
112 PyObject *margs;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
113 margs = Py_BuildValue("OOO", name, bases, ns);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
114 if (margs != NULL) {
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
115 cls = PyEval_CallObjectWithKeywords(meta, margs, mkw);
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
116 Py_DECREF(margs);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
117 }
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
118 if (cls != NULL && PyCell_Check(cell)) {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
119 Py_INCREF(cls);
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
120 PyCell_SET(cell, cls);
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
121 }
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
122 Py_DECREF(cell);
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
123 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
124 Py_DECREF(ns);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
125 Py_DECREF(meta);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
126 Py_XDECREF(mkw);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
127 Py_DECREF(bases);
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
128 return cls;
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
129 }
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
130
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
131 PyDoc_STRVAR(build_class_doc,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
132 "__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
133 \n\
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
134 Internal helper function used by the class statement.");
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
135
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
136 static PyObject *
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
137 builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
138 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
139 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
140 "level", 0};
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
141 char *name;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
142 PyObject *globals = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
143 PyObject *locals = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
144 PyObject *fromlist = NULL;
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
145 int level = -1;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
146
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
147 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
148 kwlist, &name, &globals, &locals, &fromlist, &level))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
149 return NULL;
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
150 return PyImport_ImportModuleLevel(name, globals, locals,
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
151 fromlist, level);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
152 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
153
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
154 PyDoc_STRVAR(import_doc,
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
155 "__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
156 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
157 Import a module. The globals are only used to determine the context;\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
158 they are not modified. The locals are currently unused. The fromlist\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
159 should be a list of names to emulate ``from name import ...'', or an\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
160 empty list to emulate ``import name''.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
161 When importing a module from a package, note that __import__('A.B', ...)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
162 returns package A when fromlist is empty, but its submodule B when\n\
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
163 fromlist is not empty. Level is used to determine whether to perform \n\
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
164 absolute or relative imports. -1 is the original strategy of attempting\n\
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
165 both absolute and relative imports, 0 is absolute, a positive number\n\
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
166 is the number of parent directories to search relative to the current module.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
167
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
168
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
169 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
170 builtin_abs(PyObject *self, PyObject *v)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
171 {
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
172 return PyNumber_Absolute(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
175 PyDoc_STRVAR(abs_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
176 "abs(number) -> number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
177 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
178 Return the absolute value of the argument.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
179
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
180 static PyObject *
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
181 builtin_all(PyObject *self, PyObject *v)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
182 {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
183 PyObject *it, *item;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
184
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
185 it = PyObject_GetIter(v);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
186 if (it == NULL)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
187 return NULL;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
188
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
189 while ((item = PyIter_Next(it)) != NULL) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
190 int cmp = PyObject_IsTrue(item);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
191 Py_DECREF(item);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
192 if (cmp < 0) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
193 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
194 return NULL;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
195 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
196 if (cmp == 0) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
197 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
198 Py_RETURN_FALSE;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
199 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
200 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
201 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
202 if (PyErr_Occurred())
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
203 return NULL;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
204 Py_RETURN_TRUE;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
205 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
206
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
207 PyDoc_STRVAR(all_doc,
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
208 "all(iterable) -> bool\n\
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
209 \n\
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
210 Return True if bool(x) is True for all values x in the iterable.");
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
211
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
212 static PyObject *
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
213 builtin_any(PyObject *self, PyObject *v)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
214 {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
215 PyObject *it, *item;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
216
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
217 it = PyObject_GetIter(v);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
218 if (it == NULL)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
219 return NULL;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
220
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
221 while ((item = PyIter_Next(it)) != NULL) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
222 int cmp = PyObject_IsTrue(item);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
223 Py_DECREF(item);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
224 if (cmp < 0) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
225 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
226 return NULL;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
227 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
228 if (cmp == 1) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
229 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
230 Py_RETURN_TRUE;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
231 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
232 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
233 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
234 if (PyErr_Occurred())
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
235 return NULL;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
236 Py_RETURN_FALSE;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
237 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
238
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
239 PyDoc_STRVAR(any_doc,
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
240 "any(iterable) -> bool\n\
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
241 \n\
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
242 Return True if bool(x) is True for any x in the iterable.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
243
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
244
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
245 static PyObject *
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
246 builtin_bin(PyObject *self, PyObject *v)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
247 {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
248 return PyNumber_ToBase(v, 2);
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
249 }
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
250
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
251 PyDoc_STRVAR(bin_doc,
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
252 "bin(number) -> string\n\
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
253 \n\
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
254 Return the binary representation of an integer or long integer.");
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
255
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
256
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
257 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
258 builtin_filter(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
259 {
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
260 PyObject *itertools, *ifilter, *result;
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
261 itertools = PyImport_ImportModule("itertools");
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
262 if (itertools == NULL)
35264
b431ed7bcdc2 Fix cleanup DECREF logic in builtin_filter function.
Georg Brandl <georg@python.org>
parents: 34736
diff changeset
263 return NULL;
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
264 ifilter = PyObject_GetAttrString(itertools, "ifilter");
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
265 Py_DECREF(itertools);
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
266 if (ifilter == NULL)
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
267 return NULL;
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
268 result = PyObject_Call(ifilter, args, NULL);
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
269 Py_DECREF(ifilter);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
270 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
271 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
272
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
273 PyDoc_STRVAR(filter_doc,
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
274 "filter(predicate, iterable) -> iterator\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
275 \n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
276 Return an iterator yielding only those elements of the input iterable\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
277 for which the predicate (a Boolean function) returns true.\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
278 If the predicate is None, 'lambda x: bool(x)' is assumed.\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
279 (This is identical to itertools.ifilter().)");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
280
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
281 static PyObject *
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
282 builtin_format(PyObject *self, PyObject *args)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
283 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
284 static PyObject * format_str = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
285 PyObject *value;
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
286 PyObject *spec = NULL;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
287 PyObject *meth;
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
288 PyObject *empty = NULL;
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
289 PyObject *result = NULL;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
290
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
291 /* Initialize cached value */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
292 if (format_str == NULL) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
293 /* Initialize static variable needed by _PyType_Lookup */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
294 format_str = PyUnicode_FromString("__format__");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
295 if (format_str == NULL)
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
296 goto done;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
297 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
298
43172
d842457e4b0e Changed to use 'U' argument to PyArg_ParseTuple, instead of manually checking for unicode objects.
Eric Smith <eric@trueblade.com>
parents: 43121
diff changeset
299 if (!PyArg_ParseTuple(args, "O|U:format", &value, &spec))
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
300 goto done;
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
301
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
302 /* initialize the default value */
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
303 if (spec == NULL) {
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
304 empty = PyUnicode_FromUnicode(NULL, 0);
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
305 spec = empty;
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
306 }
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
307
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
308 /* Make sure the type is initialized. float gets initialized late */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
309 if (Py_Type(value)->tp_dict == NULL)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
310 if (PyType_Ready(Py_Type(value)) < 0)
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
311 goto done;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
312
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
313 /* Find the (unbound!) __format__ method (a borrowed reference) */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
314 meth = _PyType_Lookup(Py_Type(value), format_str);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
315 if (meth == NULL) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
316 PyErr_Format(PyExc_TypeError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
317 "Type %.100s doesn't define __format__",
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
318 Py_Type(value)->tp_name);
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
319 goto done;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
320 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
321
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
322 /* And call it, binding it to the value */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
323 result = PyObject_CallFunctionObjArgs(meth, value, spec, NULL);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
324
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
325 if (result && !PyUnicode_Check(result)) {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
326 PyErr_SetString(PyExc_TypeError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
327 "__format__ method did not return string");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
328 Py_DECREF(result);
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
329 result = NULL;
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
330 goto done;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
331 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
332
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
333 done:
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
334 Py_XDECREF(empty);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
335 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
336 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
337
43110
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
338 PyDoc_STRVAR(format_doc,
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
339 "format(value[, format_spec]) -> string\n\
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
340 \n\
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
341 Returns value.__format__(format_spec)\n\
cdb7b9f82cc4 Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents: 42899
diff changeset
342 format_spec defaults to \"\"");
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
343
41609
92ead87bf984 Add 8-bit chr() back as chr8().
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
344 static PyObject *
41955
4c5fbe65b7de Make the name of the C variables match the Python names
Walter Dörwald <walter@livinglogic.de>
parents: 41888
diff changeset
345 builtin_chr(PyObject *self, PyObject *args)
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
346 {
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
347 long x;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
348
41955
4c5fbe65b7de Make the name of the C variables match the Python names
Walter Dörwald <walter@livinglogic.de>
parents: 41888
diff changeset
349 if (!PyArg_ParseTuple(args, "l:chr", &x))
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
350 return NULL;
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
351
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24071
diff changeset
352 return PyUnicode_FromOrdinal(x);
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
353 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
354
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
355 PyDoc_VAR(chr_doc) = PyDoc_STR(
41585
825d966338ea Rename 'unicode' to 'str' in its tp_name field. Rename 'str' to 'str8'.
Guido van Rossum <guido@python.org>
parents: 41558
diff changeset
356 "chr(i) -> Unicode character\n\
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
357 \n\
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
358 Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
359 )
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
360 #ifndef Py_UNICODE_WIDE
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
361 PyDoc_STR(
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
362 "\nIf 0x10000 <= i, a surrogate pair is returned."
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
363 )
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
364 #endif
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
365 ;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
366
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
367
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
368 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
369 builtin_cmp(PyObject *self, PyObject *args)
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
370 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
371 PyObject *a, *b;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
372 int c;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
373
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
374 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
375 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
376 if (PyObject_Cmp(a, b, &c) < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
377 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
378 return PyInt_FromLong((long)c);
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
379 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
380
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
381 PyDoc_STRVAR(cmp_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
382 "cmp(x, y) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
383 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
384 Return negative if x<y, zero if x==y, positive if x>y.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
385
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
386
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
387 static char *
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
388 source_as_string(PyObject *cmd)
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
389 {
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
390 char *str;
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
391 Py_ssize_t size;
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
392
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
393 if (PyUnicode_Check(cmd)) {
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
394 cmd = _PyUnicode_AsDefaultEncodedString(cmd, NULL);
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
395 if (cmd == NULL)
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
396 return NULL;
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
397 }
42898
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
398 else if (!PyObject_CheckReadBuffer(cmd)) {
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
399 PyErr_SetString(PyExc_TypeError,
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
400 "eval()/exec() arg 1 must be a string, bytes or code object");
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
401 return NULL;
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42854
diff changeset
402 }
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
403 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) {
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
404 return NULL;
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
405 }
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
406 if (strlen(str) != size) {
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
407 PyErr_SetString(PyExc_TypeError,
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
408 "source code string cannot contain null bytes");
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
409 return NULL;
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
410 }
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
411 return str;
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
412 }
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
413
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
414 static PyObject *
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
415 builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
416 {
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
417 char *str;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
418 char *filename;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
419 char *startstr;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
420 int start;
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
421 int dont_inherit = 0;
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
422 int supplied_flags = 0;
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
423 PyCompilerFlags cf;
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
424 PyObject *cmd;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
425 static char *kwlist[] = {"source", "filename", "mode", "flags",
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
426 "dont_inherit", NULL};
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
427
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
428 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
429 kwlist, &cmd, &filename, &startstr,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
430 &supplied_flags, &dont_inherit))
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
431 return NULL;
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
432
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
433 cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8;
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
434
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
435 str = source_as_string(cmd);
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
436 if (str == NULL)
27782
904f0e769820 My previous checkin caused compile() to no longer accept buffers, as noted
Just van Rossum <just@letterror.com>
parents: 27781
diff changeset
437 return NULL;
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
438
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
439 if (strcmp(startstr, "exec") == 0)
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
440 start = Py_file_input;
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
441 else if (strcmp(startstr, "eval") == 0)
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
442 start = Py_eval_input;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
443 else if (strcmp(startstr, "single") == 0)
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
444 start = Py_single_input;
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
445 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
446 PyErr_SetString(PyExc_ValueError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
447 "compile() arg 3 must be 'exec' or 'eval' or 'single'");
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
448 return NULL;
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
449 }
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
450
27878
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 27795
diff changeset
451 if (supplied_flags &
36660
3c0b26113df6 Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents: 36604
diff changeset
452 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
27878
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 27795
diff changeset
453 {
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
454 PyErr_SetString(PyExc_ValueError,
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
455 "compile(): unrecognised flags");
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
456 return NULL;
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
457 }
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
458 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
459
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
460 if (!dont_inherit) {
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
461 PyEval_MergeCompilerFlags(&cf);
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
462 }
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
463 return Py_CompileStringFlags(str, filename, start, &cf);
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
464 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
465
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
466 PyDoc_STRVAR(compile_doc,
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
467 "compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
468 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
469 Compile the source string (a Python module, statement or expression)\n\
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
470 into a code object that can be executed by exec() or eval().\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
471 The filename will be used for run-time error messages.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
472 The mode must be 'exec' to compile a module, 'single' to compile a\n\
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
473 single (interactive) statement, or 'eval' to compile an expression.\n\
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
474 The flags argument, if present, controls which future statements influence\n\
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
475 the compilation of the code.\n\
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
476 The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
477 the effects of any future statements in effect in the code calling\n\
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
478 compile; if absent or zero these statements do influence the compilation,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
479 in addition to any features explicitly specified.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
480
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
481 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
482 builtin_dir(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 {
19665
08c3c9edd99b Make dir() wordier (see the new docstring). The new behavior is a mixed
Tim Peters <tim.peters@gmail.com>
parents: 19471
diff changeset
484 PyObject *arg = NULL;
08c3c9edd99b Make dir() wordier (see the new docstring). The new behavior is a mixed
Tim Peters <tim.peters@gmail.com>
parents: 19471
diff changeset
485
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
486 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
19665
08c3c9edd99b Make dir() wordier (see the new docstring). The new behavior is a mixed
Tim Peters <tim.peters@gmail.com>
parents: 19471
diff changeset
487 return NULL;
19705
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19670
diff changeset
488 return PyObject_Dir(arg);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
490
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
491 PyDoc_STRVAR(dir_doc,
19665
08c3c9edd99b Make dir() wordier (see the new docstring). The new behavior is a mixed
Tim Peters <tim.peters@gmail.com>
parents: 19471
diff changeset
492 "dir([object]) -> list of strings\n"
08c3c9edd99b Make dir() wordier (see the new docstring). The new behavior is a mixed
Tim Peters <tim.peters@gmail.com>
parents: 19471
diff changeset
493 "\n"
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
494 "If called without an argument, return the names in the current scope.\n"
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
495 "Else, return an alphabetized list of names comprising (some of) the attributes\n"
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
496 "of the given object, and of attributes reachable from it.\n"
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
497 "If the object supplies a method named __dir__, it will be used; otherwise\n"
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
498 "the default dir() logic is used and returns:\n"
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
499 " for a module object: the module's attributes.\n"
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
500 " for a class object: its attributes, and recursively the attributes\n"
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
501 " of its bases.\n"
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
502 " for any other object: its attributes, its class's attributes, and\n"
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40933
diff changeset
503 " recursively the attributes of its class's base classes.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
504
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
505 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
506 builtin_divmod(PyObject *self, PyObject *args)
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
507 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
508 PyObject *v, *w;
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
509
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
510 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
511 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
512 return PyNumber_Divmod(v, w);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
514
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
515 PyDoc_STRVAR(divmod_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
516 "divmod(x, y) -> (div, mod)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
517 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
518 Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
519
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
520
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
521 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
522 builtin_eval(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 {
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
524 PyObject *cmd, *result, *tmp = NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
525 PyObject *globals = Py_None, *locals = Py_None;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
526 char *str;
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
527 PyCompilerFlags cf;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
528
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
529 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
530 return NULL;
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
531 if (locals != Py_None && !PyMapping_Check(locals)) {
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
532 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
32507
682f74088602 * Fix missing return after error message is set.
Raymond Hettinger <python@rcn.com>
parents: 32463
diff changeset
533 return NULL;
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
534 }
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
535 if (globals != Py_None && !PyDict_Check(globals)) {
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
536 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
537 "globals must be a real dict; try eval(expr, {}, mapping)"
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
538 : "globals must be a dict");
32507
682f74088602 * Fix missing return after error message is set.
Raymond Hettinger <python@rcn.com>
parents: 32463
diff changeset
539 return NULL;
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
540 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
541 if (globals == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
542 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
543 if (locals == Py_None)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
544 locals = PyEval_GetLocals();
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
545 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
546 else if (locals == Py_None)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
547 locals = globals;
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
548
35567
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
549 if (globals == NULL || locals == NULL) {
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
550 PyErr_SetString(PyExc_TypeError,
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
551 "eval must be given globals and locals "
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
552 "when called without a frame");
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
553 return NULL;
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
554 }
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
555
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
556 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
557 if (PyDict_SetItemString(globals, "__builtins__",
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
558 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
559 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
560 }
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
561
18885
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
562 if (PyCode_Check(cmd)) {
21398
de125792bfaa Fix for SF bug [ #492403 ] exec() segfaults on closure's func_code
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21077
diff changeset
563 if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
18885
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
564 PyErr_SetString(PyExc_TypeError,
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
565 "code object passed to eval() may not contain free variables");
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
566 return NULL;
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
567 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
568 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
18885
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
569 }
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
570
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
571 str = source_as_string(cmd);
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
572 if (str == NULL)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 return NULL;
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
574
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
575 while (*str == ' ' || *str == '\t')
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
576 str++;
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
577
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
578 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
579 (void)PyEval_MergeCompilerFlags(&cf);
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
580 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
581 Py_XDECREF(tmp);
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
582 return result;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
584
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
585 PyDoc_STRVAR(eval_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
586 "eval(source[, globals[, locals]]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
587 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
588 Evaluate the source in the context of globals and locals.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
589 The source may be a string representing a Python expression\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
590 or a code object as returned by compile().\n\
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40411
diff changeset
591 The globals must be a dictionary and locals can be any mapping,\n\
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
592 defaulting to the current globals and locals.\n\
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
593 If only globals is given, locals defaults to it.\n");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
594
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
595 static PyObject *
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
596 builtin_exec(PyObject *self, PyObject *args)
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
597 {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
598 PyObject *v;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
599 PyObject *prog, *globals = Py_None, *locals = Py_None;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
600 int plain = 0;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
601
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
602 if (!PyArg_ParseTuple(args, "O|OO:exec", &prog, &globals, &locals))
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
603 return NULL;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
604
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
605 if (globals == Py_None) {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
606 globals = PyEval_GetGlobals();
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
607 if (locals == Py_None) {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
608 locals = PyEval_GetLocals();
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
609 plain = 1;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
610 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
611 if (!globals || !locals) {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
612 PyErr_SetString(PyExc_SystemError,
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
613 "globals and locals cannot be NULL");
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
614 return NULL;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
615 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
616 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
617 else if (locals == Py_None)
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
618 locals = globals;
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
619 if (!PyUnicode_Check(prog) &&
42071
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42013
diff changeset
620 !PyCode_Check(prog)) {
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
621 PyErr_Format(PyExc_TypeError,
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
622 "exec() arg 1 must be a string, file, or code "
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
623 "object, not %.100s", prog->ob_type->tp_name);
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
624 return NULL;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
625 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
626 if (!PyDict_Check(globals)) {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
627 PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
628 globals->ob_type->tp_name);
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
629 return NULL;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
630 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
631 if (!PyMapping_Check(locals)) {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
632 PyErr_Format(PyExc_TypeError,
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
633 "arg 3 must be a mapping or None, not %.100s",
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
634 locals->ob_type->tp_name);
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
635 return NULL;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
636 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
637 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
638 if (PyDict_SetItemString(globals, "__builtins__",
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
639 PyEval_GetBuiltins()) != 0)
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
640 return NULL;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
641 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
642
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
643 if (PyCode_Check(prog)) {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
644 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
645 PyErr_SetString(PyExc_TypeError,
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
646 "code object passed to exec() may not "
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
647 "contain free variables");
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
648 return NULL;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
649 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
650 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
651 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
652 else {
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
653 char *str = source_as_string(prog);
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
654 PyCompilerFlags cf;
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
655 if (str == NULL)
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
656 return NULL;
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
657 cf.cf_flags = PyCF_SOURCE_IS_UTF8;
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
658 if (PyEval_MergeCompilerFlags(&cf))
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
659 v = PyRun_StringFlags(str, Py_file_input, globals,
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
660 locals, &cf);
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
661 else
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
662 v = PyRun_String(str, Py_file_input, globals, locals);
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
663 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
664 if (v == NULL)
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
665 return NULL;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
666 Py_DECREF(v);
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
667 Py_RETURN_NONE;
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
668 }
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
669
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
670 PyDoc_STRVAR(exec_doc,
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
671 "exec(object[, globals[, locals]])\n\
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
672 \n\
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
673 Read and execute code from a object, which can be a string, a code\n\
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
674 object or a file object.\n\
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
675 The globals and locals are dictionaries, defaulting to the current\n\
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
676 globals and locals. If only globals is given, locals defaults to it.");
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
677
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
678
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
679 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
680 builtin_getattr(PyObject *self, PyObject *args)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
681 {
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
682 PyObject *v, *result, *dflt = NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
683 PyObject *name;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
684
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
685 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
686 return NULL;
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
687
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
688 if (!PyUnicode_Check(name)) {
18888
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
689 PyErr_SetString(PyExc_TypeError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
690 "getattr(): attribute name must be string");
18888
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
691 return NULL;
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
692 }
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
693 result = PyObject_GetAttr(v, name);
20471
bbdc0abfbb13 SF patch #471852 (anonymous) notes that getattr(obj, name, default)
Guido van Rossum <guido@python.org>
parents: 20307
diff changeset
694 if (result == NULL && dflt != NULL &&
bbdc0abfbb13 SF patch #471852 (anonymous) notes that getattr(obj, name, default)
Guido van Rossum <guido@python.org>
parents: 20307
diff changeset
695 PyErr_ExceptionMatches(PyExc_AttributeError))
bbdc0abfbb13 SF patch #471852 (anonymous) notes that getattr(obj, name, default)
Guido van Rossum <guido@python.org>
parents: 20307
diff changeset
696 {
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
697 PyErr_Clear();
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
698 Py_INCREF(dflt);
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
699 result = dflt;
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
700 }
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
701 return result;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
702 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
703
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
704 PyDoc_STRVAR(getattr_doc,
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
705 "getattr(object, name[, default]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
706 \n\
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
707 Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
708 When a default argument is given, it is returned when the attribute doesn't\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
709 exist; without it, an exception is raised in that case.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
710
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
711
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
712 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
713 builtin_globals(PyObject *self)
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
714 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
715 PyObject *d;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
716
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
717 d = PyEval_GetGlobals();
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39603
diff changeset
718 Py_XINCREF(d);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
719 return d;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
720 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
721
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
722 PyDoc_STRVAR(globals_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
723 "globals() -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
724 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
725 Return the dictionary containing the current scope's global variables.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
726
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
727
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
728 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
729 builtin_hasattr(PyObject *self, PyObject *args)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
730 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
731 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
732 PyObject *name;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
733
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
734 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
735 return NULL;
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
736 if (!PyUnicode_Check(name)) {
18890
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
737 PyErr_SetString(PyExc_TypeError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
738 "hasattr(): attribute name must be string");
18890
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
739 return NULL;
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
740 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
741 v = PyObject_GetAttr(v, name);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
742 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
743 PyErr_Clear();
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
744 Py_INCREF(Py_False);
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
745 return Py_False;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
746 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
747 Py_DECREF(v);
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
748 Py_INCREF(Py_True);
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
749 return Py_True;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
750 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
751
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
752 PyDoc_STRVAR(hasattr_doc,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
753 "hasattr(object, name) -> bool\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
754 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
755 Return whether the object has an attribute with the given name.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
756 (This is done by calling getattr(object, name) and catching exceptions.)");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
757
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
758
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
759 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
760 builtin_id(PyObject *self, PyObject *v)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
761 {
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
762 return PyLong_FromVoidPtr(v);
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
763 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
764
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
765 PyDoc_STRVAR(id_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
766 "id(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
767 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
768 Return the identity of an object. This is guaranteed to be unique among\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
769 simultaneously existing objects. (Hint: it's the object's memory address.)");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
770
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
771
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
772 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
773 builtin_map(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
774 {
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
775 PyObject *itertools, *imap, *result;
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
776 itertools = PyImport_ImportModule("itertools");
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
777 if (itertools == NULL)
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
778 return NULL;
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
779 imap = PyObject_GetAttrString(itertools, "imap");
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
780 Py_DECREF(itertools);
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
781 if (imap == NULL)
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
782 return NULL;
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
783 result = PyObject_Call(imap, args, NULL);
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
784 Py_DECREF(imap);
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
785 return result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
786 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
787
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
788 PyDoc_STRVAR(map_doc,
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
789 "map(function, iterable[, iterable, ...]) -> iterator\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
790 \n\
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
791 Return an iterator yielding the results of applying the function to the\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
792 items of the argument iterables(s). If more than one iterable is given,\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
793 the function is called with an argument list consisting of the\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
794 corresponding item of each iterable, until an iterable is exhausted.\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
795 If the function is None, 'lambda *a: a' is assumed.\n\
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
796 (This is identical to itertools.imap().)");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
797
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
798
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
799 static PyObject *
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
800 builtin_next(PyObject *self, PyObject *args)
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
801 {
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
802 PyObject *it, *res;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
803 PyObject *def = NULL;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
804
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
805 if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
806 return NULL;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
807 if (!PyIter_Check(it)) {
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
808 PyErr_Format(PyExc_TypeError,
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
809 "%.200s object is not an iterator", it->ob_type->tp_name);
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
810 return NULL;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
811 }
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
812
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
813 res = (*it->ob_type->tp_iternext)(it);
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
814 if (res == NULL) {
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
815 if (def) {
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
816 if (PyErr_Occurred() &&
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
817 !PyErr_ExceptionMatches(PyExc_StopIteration))
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
818 return NULL;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
819 PyErr_Clear();
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
820 Py_INCREF(def);
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
821 return def;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
822 } else if (PyErr_Occurred()) {
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
823 return NULL;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
824 } else {
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
825 PyErr_SetNone(PyExc_StopIteration);
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
826 return NULL;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
827 }
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
828 }
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
829 return res;
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
830 }
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
831
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
832 PyDoc_STRVAR(next_doc,
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
833 "next(iterator[, default])\n\
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
834 \n\
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
835 Return the next item from the iterator. If default is given and the iterator\n\
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
836 is exhausted, it is returned instead of raising StopIteration.");
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
837
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
838
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
839 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
840 builtin_setattr(PyObject *self, PyObject *args)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
841 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
842 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
843 PyObject *name;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
844 PyObject *value;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
845
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
846 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
847 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
848 if (PyObject_SetAttr(v, name, value) != 0)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
849 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
850 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
851 return Py_None;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
852 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
853
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
854 PyDoc_STRVAR(setattr_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
855 "setattr(object, name, value)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
856 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
857 Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
858 ``x.y = v''.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
859
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
860
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
861 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
862 builtin_delattr(PyObject *self, PyObject *args)
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
863 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
864 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
865 PyObject *name;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
866
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
867 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
868 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
869 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
870 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
871 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
872 return Py_None;
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
873 }
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
874
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
875 PyDoc_STRVAR(delattr_doc,
9514
a126107c433a Fix cosmetic bug in delattr docstring discovered by JvR.
Guido van Rossum <guido@python.org>
parents: 9188
diff changeset
876 "delattr(object, name)\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
877 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
878 Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
879 ``del x.y''.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
880
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
881
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
882 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
883 builtin_hash(PyObject *self, PyObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
884 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
885 long x;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
886
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
887 x = PyObject_Hash(v);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
888 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
889 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
890 return PyInt_FromLong(x);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
891 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
892
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
893 PyDoc_STRVAR(hash_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
894 "hash(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
895 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
896 Return a hash value for the object. Two objects with the same value have\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
897 the same hash value. The reverse is not necessarily true, but likely.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
898
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
899
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
900 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
901 builtin_hex(PyObject *self, PyObject *v)
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
902 {
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
903 return PyNumber_ToBase(v, 16);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
904 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
905
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
906 PyDoc_STRVAR(hex_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
907 "hex(number) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
908 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
909 Return the hexadecimal representation of an integer or long integer.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
910
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
911
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
912 static PyObject *
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
913 builtin_iter(PyObject *self, PyObject *args)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
914 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
915 PyObject *v, *w = NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
916
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
917 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
918 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
919 if (w == NULL)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
920 return PyObject_GetIter(v);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
921 if (!PyCallable_Check(v)) {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
922 PyErr_SetString(PyExc_TypeError,
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
923 "iter(v, w): v must be callable");
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
924 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
925 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
926 return PyCallIter_New(v, w);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
927 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
928
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
929 PyDoc_STRVAR(iter_doc,
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
930 "iter(collection) -> iterator\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
931 iter(callable, sentinel) -> iterator\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
932 \n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
933 Get an iterator from an object. In the first form, the argument must\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
934 supply its own iterator, or be a sequence.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
935 In the second form, the callable is called until it returns the sentinel.");
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
936
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
937
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
938 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
939 builtin_len(PyObject *self, PyObject *v)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
940 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
941 Py_ssize_t res;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
942
13576
61e4c29d53d8 replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13447
diff changeset
943 res = PyObject_Size(v);
8589
97dc6aa532e7 Fix a stupid little bug: len() of an unsized returns -1 and leaves an
Guido van Rossum <guido@python.org>
parents: 8581
diff changeset
944 if (res < 0 && PyErr_Occurred())
97dc6aa532e7 Fix a stupid little bug: len() of an unsized returns -1 and leaves an
Guido van Rossum <guido@python.org>
parents: 8581
diff changeset
945 return NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
946 return PyInt_FromSsize_t(res);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
947 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
948
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
949 PyDoc_STRVAR(len_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
950 "len(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
951 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
952 Return the number of items of a sequence or mapping.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
953
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
954
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
955 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
956 builtin_locals(PyObject *self)
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
957 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
958 PyObject *d;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
959
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
960 d = PyEval_GetLocals();
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39603
diff changeset
961 Py_XINCREF(d);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
962 return d;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
963 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
964
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
965 PyDoc_STRVAR(locals_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
966 "locals() -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
967 \n\
27076
753306f9b0a0 SF bug #655271: Slightly modify locals() doc
Raymond Hettinger <python@rcn.com>
parents: 26824
diff changeset
968 Update and return a dictionary containing the current scope's local variables.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
969
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
970
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
971 static PyObject *
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
972 min_max(PyObject *args, PyObject *kwds, int op)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
974 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
33099
ce016d0ec701 Patch #1005468: Disambiguate "min() or max()" exception string.
Martin v. Löwis <martin@v.loewis.de>
parents: 33019
diff changeset
975 const char *name = op == Py_LT ? "min" : "max";
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
976
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
977 if (PyTuple_Size(args) > 1)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
978 v = args;
33099
ce016d0ec701 Patch #1005468: Disambiguate "min() or max()" exception string.
Martin v. Löwis <martin@v.loewis.de>
parents: 33019
diff changeset
979 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 return NULL;
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
981
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
982 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
983 keyfunc = PyDict_GetItemString(kwds, "key");
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
984 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
985 PyErr_Format(PyExc_TypeError,
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
986 "%s() got an unexpected keyword argument", name);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
987 return NULL;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
988 }
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
989 }
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
990
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
991 it = PyObject_GetIter(v);
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
992 if (it == NULL)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 return NULL;
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
994
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
995 maxitem = NULL; /* the result */
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
996 maxval = NULL; /* the value associated with the result */
34123
4e8fb12f8378 Put parentheses around the assignment in the 'while' loop conditional
Brett Cannon <bcannon@gmail.com>
parents: 34085
diff changeset
997 while (( item = PyIter_Next(it) )) {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
998 /* get the value from the key function */
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
999 if (keyfunc != NULL) {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1000 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1001 if (val == NULL)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1002 goto Fail_it_item;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1003 }
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1004 /* no key function; the value is the item */
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1005 else {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1006 val = item;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1007 Py_INCREF(val);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1008 }
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1009
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1010 /* maximum value and item are unset; set them */
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1011 if (maxval == NULL) {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1012 maxitem = item;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1013 maxval = val;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1014 }
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1015 /* maximum value and item are set; update them as necessary */
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1016 else {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1017 int cmp = PyObject_RichCompareBool(val, maxval, op);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1018 if (cmp < 0)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1019 goto Fail_it_item_and_val;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1020 else if (cmp > 0) {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1021 Py_DECREF(maxval);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1022 Py_DECREF(maxitem);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1023 maxval = val;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1024 maxitem = item;
15949
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1025 }
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1026 else {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1027 Py_DECREF(item);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1028 Py_DECREF(val);
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1029 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1030 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 }
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1032 if (PyErr_Occurred())
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1033 goto Fail_it;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1034 if (maxval == NULL) {
33099
ce016d0ec701 Patch #1005468: Disambiguate "min() or max()" exception string.
Martin v. Löwis <martin@v.loewis.de>
parents: 33019
diff changeset
1035 PyErr_Format(PyExc_ValueError,
ce016d0ec701 Patch #1005468: Disambiguate "min() or max()" exception string.
Martin v. Löwis <martin@v.loewis.de>
parents: 33019
diff changeset
1036 "%s() arg is an empty sequence", name);
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1037 assert(maxitem == NULL);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1038 }
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1039 else
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1040 Py_DECREF(maxval);
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1041 Py_DECREF(it);
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1042 return maxitem;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1043
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1044 Fail_it_item_and_val:
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1045 Py_DECREF(val);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1046 Fail_it_item:
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1047 Py_DECREF(item);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1048 Fail_it:
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1049 Py_XDECREF(maxval);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1050 Py_XDECREF(maxitem);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1051 Py_DECREF(it);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1052 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1055 static PyObject *
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1056 builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057 {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1058 return min_max(args, kwds, Py_LT);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1061 PyDoc_STRVAR(min_doc,
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1062 "min(iterable[, key=func]) -> value\n\
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1063 min(a, b, c, ...[, key=func]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1064 \n\
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1065 With a single iterable argument, return its smallest item.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1066 With two or more arguments, return the smallest argument.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1067
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1068
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1069 static PyObject *
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1070 builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071 {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1072 return min_max(args, kwds, Py_GT);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1073 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1074
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1075 PyDoc_STRVAR(max_doc,
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1076 "max(iterable[, key=func]) -> value\n\
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1077 max(a, b, c, ...[, key=func]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1078 \n\
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1079 With a single iterable argument, return its largest item.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1080 With two or more arguments, return the largest argument.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1081
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1082
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1083 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1084 builtin_oct(PyObject *self, PyObject *v)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1085 {
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
1086 return PyNumber_ToBase(v, 8);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1087 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1088
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1089 PyDoc_STRVAR(oct_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1090 "oct(number) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1091 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1092 Return the octal representation of an integer or long integer.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1093
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1094
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1095 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1096 builtin_ord(PyObject *self, PyObject* obj)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 {
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1098 long ord;
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1099 Py_ssize_t size;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1100
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1101 if (PyString_Check(obj)) {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1102 size = PyString_GET_SIZE(obj);
15635
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1103 if (size == 1) {
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1104 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
15635
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1105 return PyInt_FromLong(ord);
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1106 }
41452
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1107 }
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1108 else if (PyUnicode_Check(obj)) {
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1109 size = PyUnicode_GET_SIZE(obj);
15635
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1110 if (size == 1) {
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1111 ord = (long)*PyUnicode_AS_UNICODE(obj);
15635
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1112 return PyInt_FromLong(ord);
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1113 }
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1114 #ifndef Py_UNICODE_WIDE
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1115 if (size == 2) {
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1116 /* Decode a valid surrogate pair */
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1117 int c0 = PyUnicode_AS_UNICODE(obj)[0];
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1118 int c1 = PyUnicode_AS_UNICODE(obj)[1];
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1119 if (0xD800 <= c0 && c0 <= 0xDBFF &&
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1120 0xDC00 <= c1 && c1 <= 0xDFFF) {
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1121 ord = ((((c0 & 0x03FF) << 10) | (c1 & 0x03FF)) +
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1122 0x00010000);
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1123 return PyInt_FromLong(ord);
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1124 }
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1125 }
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1126 #endif
41846
308e632702bf Enable new I/O. Disable creation of old files.
Guido van Rossum <guido@python.org>
parents: 41824
diff changeset
1127 }
41452
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1128 else if (PyBytes_Check(obj)) {
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1129 /* XXX Hopefully this is temporary */
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1130 size = PyBytes_GET_SIZE(obj);
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1131 if (size == 1) {
41650
23a8b3dfbe95 Given that ord() of a bytes object of length 1 is defined, it should
Guido van Rossum <guido@python.org>
parents: 41645
diff changeset
1132 ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
41452
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1133 return PyInt_FromLong(ord);
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1134 }
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1135 }
3c339de80010 Support marshal.dump(x, f) where f is not a real file.
Guido van Rossum <guido@python.org>
parents: 41226
diff changeset
1136 else {
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1137 PyErr_Format(PyExc_TypeError,
15654
6405f30aa08c CHange error messages for ord(), using "string" instead of "string or Unicode"
Andrew M. Kuchling <amk@amk.ca>
parents: 15637
diff changeset
1138 "ord() expected string of length 1, but " \
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1139 "%.200s found", obj->ob_type->tp_name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1140 return NULL;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1141 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1142
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
1143 PyErr_Format(PyExc_TypeError,
15654
6405f30aa08c CHange error messages for ord(), using "string" instead of "string or Unicode"
Andrew M. Kuchling <amk@amk.ca>
parents: 15637
diff changeset
1144 "ord() expected a character, "
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1145 "but string of length %zd found",
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1146 size);
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1147 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
1150 PyDoc_VAR(ord_doc) = PyDoc_STR(
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1151 "ord(c) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1152 \n\
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1153 Return the integer ordinal of a one-character string."
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
1154 )
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1155 #ifndef Py_UNICODE_WIDE
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
1156 PyDoc_STR(
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1157 "\nA valid surrogate pair is also accepted."
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
1158 )
42283
18e5ae91bd29 Make chr() and ord() return/accept surrogate pairs in narrow builds.
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1159 #endif
42291
3772103200ec Three patches by Amaury Forgeot d'Arc; SF patch# 1754484.
Guido van Rossum <guido@python.org>
parents: 42283
diff changeset
1160 ;
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1161
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1162
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1163 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1164 builtin_pow(PyObject *self, PyObject *args)
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1165 {
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1166 PyObject *v, *w, *z = Py_None;
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1167
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1168 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1169 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1170 return PyNumber_Power(v, w, z);
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
1171 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
1172
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1173 PyDoc_STRVAR(pow_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1174 "pow(x, y[, z]) -> number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1175 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1176 With two arguments, equivalent to x**y. With three arguments,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1177 equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1178
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1179
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1180
40410
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1181 static PyObject *
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1182 builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1183 {
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1184 static char *kwlist[] = {"sep", "end", "file", 0};
40922
be41da66ce37 Fix leak in the print function.
Georg Brandl <georg@python.org>
parents: 40788
diff changeset
1185 static PyObject *dummy_args;
40410
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1186 PyObject *sep = NULL, *end = NULL, *file = NULL;
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1187 int i, err;
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1188
40922
be41da66ce37 Fix leak in the print function.
Georg Brandl <georg@python.org>
parents: 40788
diff changeset
1189 if (dummy_args == NULL) {
be41da66ce37 Fix leak in the print function.
Georg Brandl <georg@python.org>
parents: 40788
diff changeset
1190 if (!(dummy_args = PyTuple_New(0)))
be41da66ce37 Fix leak in the print function.
Georg Brandl <georg@python.org>
parents: 40788
diff changeset
1191 return NULL;
be41da66ce37 Fix leak in the print function.
Georg Brandl <georg@python.org>
parents: 40788
diff changeset
1192 }
40788
e9a6234b26af * Remove PRINT_ITEM(_TO), PRINT_NEWLINE(_TO) opcodes.
Georg Brandl <georg@python.org>
parents: 40774
diff changeset
1193 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
40410
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1194 kwlist, &sep, &end, &file))
43844
2c97e24b16cf Another #1415 fix for Windows GUI apps
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
1195 return NULL;
2c97e24b16cf Another #1415 fix for Windows GUI apps
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
1196 if (file == NULL || file == Py_None) {
40410
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1197 file = PySys_GetObject("stdout");
43844
2c97e24b16cf Another #1415 fix for Windows GUI apps
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
1198 /* sys.stdout may be None when FILE* stdout isn't connected */
2c97e24b16cf Another #1415 fix for Windows GUI apps
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
1199 if (file == Py_None)
2c97e24b16cf Another #1415 fix for Windows GUI apps
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
1200 Py_RETURN_NONE;
2c97e24b16cf Another #1415 fix for Windows GUI apps
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
1201 }
40410
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1202
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1203 if (sep && sep != Py_None && !PyUnicode_Check(sep)) {
40411
b3ed053cc6a1 Check "sep" and "end" for stringness in Print().
Georg Brandl <georg@python.org>
parents: 40410
diff changeset
1204 PyErr_Format(PyExc_TypeError,
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1205 "sep must be None or a string, not %.200s",
40411
b3ed053cc6a1 Check "sep" and "end" for stringness in Print().
Georg Brandl <georg@python.org>
parents: 40410
diff changeset
1206 sep->ob_type->tp_name);
b3ed053cc6a1 Check "sep" and "end" for stringness in Print().
Georg Brandl <georg@python.org>
parents: 40410
diff changeset
1207 return NULL;
b3ed053cc6a1 Check "sep" and "end" for stringness in Print().
Georg Brandl <georg@python.org>
parents: 40410
diff changeset
1208 }
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1209 if (end && end != Py_None && !PyUnicode_Check(end)) {
40411
b3ed053cc6a1 Check "sep" and "end" for stringness in Print().
Georg Brandl <georg@python.org>
parents: 40410
diff changeset
1210 PyErr_Format(PyExc_TypeError,
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42817
diff changeset
1211 "end must be None or a string, not %.200s",
40411
b3ed053cc6a1 Check "sep" and "end" for stringness in Print().
Georg Brandl <georg@python.org>
parents: 40410
diff changeset
1212 end->ob_type->tp_name);
b3ed053cc6a1 Check "sep" and "end" for stringness in Print().
Georg Brandl <georg@python.org>
parents: 40410
diff changeset
1213 return NULL;
b3ed053cc6a1 Check "sep" and "end" for stringness in Print().
Georg Brandl <georg@python.org>
parents: 40410
diff changeset
1214 }
40410
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1215
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1216 for (i = 0; i < PyTuple_Size(args); i++) {
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1217 if (i > 0) {
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1218 if (sep == NULL || sep == Py_None)
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1219 err = PyFile_WriteString(" ", file);
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1220 else
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1221 err = PyFile_WriteObject(sep, file,
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1222 Py_PRINT_RAW);
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1223 if (err)
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1224 return NULL;
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1225 }
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1226 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1227 Py_PRINT_RAW);
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1228 if (err)
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1229 return NULL;
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1230 }
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1231
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1232 if (end == NULL || end == Py_None)
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1233 err = PyFile_WriteString("\n", file);
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1234 else
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1235 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1236 if (err)
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1237 return NULL;
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1238
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1239 Py_RETURN_NONE;
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1240 }
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1241
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1242 PyDoc_STRVAR(print_doc,
40774
793e0323d4d6 Essential changes for print function changes.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1243 "print(value, ..., file=None, sep=' ', end='\\n')\n\
40410
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1244 \n\
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1245 Prints the values to a stream, or to sys.stdout by default.\n\
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1246 Optional keyword arguments:\n\
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1247 file: a file-like object (stream); defaults to the current sys.stdout.\n\
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1248 sep: string inserted between values, default a space.\n\
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1249 end: string appended after the last value, default a newline.");
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1250
2e5011acb414 Add built-in Print() function. This is PEP 3105 except for the name;
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
1251
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1252 static PyObject *
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1253 builtin_input(PyObject *self, PyObject *args)
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1254 {
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1255 PyObject *promptarg = NULL;
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1256 PyObject *fin = PySys_GetObject("stdin");
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1257 PyObject *fout = PySys_GetObject("stdout");
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1258 PyObject *ferr = PySys_GetObject("stderr");
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1259 PyObject *tmp;
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1260 long fd;
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1261 int tty;
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1262
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1263 /* Parse arguments */
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1264 if (!PyArg_UnpackTuple(args, "input", 0, 1, &promptarg))
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1265 return NULL;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1266
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1267 /* Check that stdin/out/err are intact */
43855
ee081609cee8 Added some additional checks for sys.std?? is None, see #1440
Christian Heimes <christian@cheimes.de>
parents: 43844
diff changeset
1268 if (fin == NULL || fin == Py_None) {
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1269 PyErr_SetString(PyExc_RuntimeError,
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1270 "input(): lost sys.stdin");
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1271 return NULL;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1272 }
43855
ee081609cee8 Added some additional checks for sys.std?? is None, see #1440
Christian Heimes <christian@cheimes.de>
parents: 43844
diff changeset
1273 if (fout == NULL || fout == Py_None) {
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1274 PyErr_SetString(PyExc_RuntimeError,
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1275 "input(): lost sys.stdout");
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1276 return NULL;
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1277 }
43855
ee081609cee8 Added some additional checks for sys.std?? is None, see #1440
Christian Heimes <christian@cheimes.de>
parents: 43844
diff changeset
1278 if (ferr == NULL || ferr == Py_None) {
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1279 PyErr_SetString(PyExc_RuntimeError,
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1280 "input(): lost sys.stderr");
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1281 return NULL;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1282 }
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1283
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1284 /* First of all, flush stderr */
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1285 tmp = PyObject_CallMethod(ferr, "flush", "");
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1286 if (tmp == NULL)
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1287 PyErr_Clear();
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1288 else
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1289 Py_DECREF(tmp);
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1290
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1291 /* We should only use (GNU) readline if Python's sys.stdin and
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1292 sys.stdout are the same as C's stdin and stdout, because we
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1293 need to pass it those. */
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1294 tmp = PyObject_CallMethod(fin, "fileno", "");
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1295 if (tmp == NULL) {
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1296 PyErr_Clear();
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1297 tty = 0;
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1298 }
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1299 else {
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1300 fd = PyInt_AsLong(tmp);
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1301 Py_DECREF(tmp);
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1302 if (fd < 0 && PyErr_Occurred())
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1303 return NULL;
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1304 tty = fd == fileno(stdin) && isatty(fd);
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1305 }
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1306 if (tty) {
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1307 tmp = PyObject_CallMethod(fout, "fileno", "");
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1308 if (tmp == NULL)
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1309 PyErr_Clear();
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1310 else {
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1311 fd = PyInt_AsLong(tmp);
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1312 Py_DECREF(tmp);
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1313 if (fd < 0 && PyErr_Occurred())
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1314 return NULL;
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1315 tty = fd == fileno(stdout) && isatty(fd);
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1316 }
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1317 }
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1318
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1319 /* If we're interactive, use (GNU) readline */
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1320 if (tty) {
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1321 PyObject *po;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1322 char *prompt;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1323 char *s;
43216
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1324 PyObject *stdin_encoding;
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1325 PyObject *result;
43216
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1326
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1327 stdin_encoding = PyObject_GetAttrString(fin, "encoding");
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1328 if (!stdin_encoding)
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1329 /* stdin is a text stream, so it must have an
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1330 encoding. */
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1331 return NULL;
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1332 tmp = PyObject_CallMethod(fout, "flush", "");
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1333 if (tmp == NULL)
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1334 PyErr_Clear();
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1335 else
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1336 Py_DECREF(tmp);
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1337 if (promptarg != NULL) {
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1338 po = PyObject_Str(promptarg);
43216
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1339 if (po == NULL) {
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1340 Py_DECREF(stdin_encoding);
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1341 return NULL;
43216
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1342 }
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1343 prompt = PyString_AsString(po);
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1344 if (prompt == NULL) {
43216
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1345 Py_DECREF(stdin_encoding);
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1346 Py_DECREF(po);
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1347 return NULL;
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1348 }
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1349 }
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1350 else {
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1351 po = NULL;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1352 prompt = "";
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1353 }
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1354 s = PyOS_Readline(stdin, stdout, prompt);
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1355 Py_XDECREF(po);
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1356 if (s == NULL) {
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1357 if (!PyErr_Occurred())
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1358 PyErr_SetNone(PyExc_KeyboardInterrupt);
43216
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1359 Py_DECREF(stdin_encoding);
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1360 return NULL;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1361 }
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1362 if (*s == '\0') {
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1363 PyErr_SetNone(PyExc_EOFError);
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1364 result = NULL;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1365 }
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1366 else { /* strip trailing '\n' */
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1367 size_t len = strlen(s);
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1368 if (len > PY_SSIZE_T_MAX) {
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1369 PyErr_SetString(PyExc_OverflowError,
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1370 "input: input too long");
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1371 result = NULL;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1372 }
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1373 else {
43216
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1374 result = PyUnicode_Decode
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1375 (s, len-1,
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1376 PyUnicode_AsString(stdin_encoding),
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1377 NULL);
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1378 }
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1379 }
43216
a311ab2fc1a7 Decode input() with stdin.encoding. Fixes #1097.
Martin v. Löwis <martin@v.loewis.de>
parents: 43172
diff changeset
1380 Py_DECREF(stdin_encoding);
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1381 PyMem_FREE(s);
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1382 return result;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1383 }
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1384
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1385 /* Fallback if we're not interactive */
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1386 if (promptarg != NULL) {
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1387 if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0)
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1388 return NULL;
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1389 }
41888
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1390 tmp = PyObject_CallMethod(fout, "flush", "");
aa73206c9ce6 Make input9) behave properly with the new I/O library.
Guido van Rossum <guido@python.org>
parents: 41846
diff changeset
1391 if (tmp == NULL)
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1392 PyErr_Clear();
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1393 else
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1394 Py_DECREF(tmp);
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1395 return PyFile_GetLine(fin, -1);
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1396 }
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1397
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1398 PyDoc_STRVAR(input_doc,
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1399 "input([prompt]) -> string\n\
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1400 \n\
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1401 Read a string from standard input. The trailing newline is stripped.\n\
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1402 If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1403 On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1404 is printed without a trailing newline before reading.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1405
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1406
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1407 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1408 builtin_repr(PyObject *self, PyObject *v)
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1409 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1410 return PyObject_Repr(v);
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1411 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1412
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1413 PyDoc_STRVAR(repr_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1414 "repr(object) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1415 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1416 Return the canonical string representation of the object.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1417 For most object types, eval(repr(object)) == object.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1418
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1419
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1420 static PyObject *
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
1421 builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1422 {
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1423 #define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1424 static PyObject *round_str = NULL;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1425 int ndigits = UNDEF_NDIGITS;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
1426 static char *kwlist[] = {"number", "ndigits", 0};
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1427 PyObject *number, *round;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1428
42718
7fe2848fd41d Implement the round functionality for PEP 3141, and add tests for it.
Alex Martelli <aleaxit@gmail.com>
parents: 42713
diff changeset
1429 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:round",
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1430 kwlist, &number, &ndigits))
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
1431 return NULL;
42718
7fe2848fd41d Implement the round functionality for PEP 3141, and add tests for it.
Alex Martelli <aleaxit@gmail.com>
parents: 42713
diff changeset
1432
42760
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1433 if (Py_Type(number)->tp_dict == NULL) {
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1434 if (PyType_Ready(Py_Type(number)) < 0)
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1435 return NULL;
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1436 }
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1437
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1438 if (round_str == NULL) {
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1439 round_str = PyUnicode_FromString("__round__");
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1440 if (round_str == NULL)
42718
7fe2848fd41d Implement the round functionality for PEP 3141, and add tests for it.
Alex Martelli <aleaxit@gmail.com>
parents: 42713
diff changeset
1441 return NULL;
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1442 }
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1443
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1444 round = _PyType_Lookup(Py_Type(number), round_str);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1445 if (round == NULL) {
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1446 PyErr_Format(PyExc_TypeError,
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1447 "type %.100s doesn't define __round__ method",
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1448 Py_Type(number)->tp_name);
42718
7fe2848fd41d Implement the round functionality for PEP 3141, and add tests for it.
Alex Martelli <aleaxit@gmail.com>
parents: 42713
diff changeset
1449 return NULL;
7fe2848fd41d Implement the round functionality for PEP 3141, and add tests for it.
Alex Martelli <aleaxit@gmail.com>
parents: 42713
diff changeset
1450 }
7fe2848fd41d Implement the round functionality for PEP 3141, and add tests for it.
Alex Martelli <aleaxit@gmail.com>
parents: 42713
diff changeset
1451
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1452 if (ndigits == UNDEF_NDIGITS)
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1453 return PyObject_CallFunction(round, "O", number);
8287
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1454 else
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1455 return PyObject_CallFunction(round, "Oi", number, ndigits);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1456 #undef UNDEF_NDIGITS
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1457 }
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1458
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1459 PyDoc_STRVAR(round_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1460 "round(number[, ndigits]) -> floating point number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1461 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1462 Round a number to a given precision in decimal digits (default 0 digits).\n\
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1463 This returns an int when called with one argument, otherwise a float.\n\
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1464 Precision may be negative.");
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1465
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1466
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1467 static PyObject *
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1468 builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1469 {
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1470 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1471 PyObject *callable;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36660
diff changeset
1472 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
36034
fdf0ac36eb74 Fix an int/long mismatch identified here:
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1473 int reverse;
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1474
36034
fdf0ac36eb74 Fix an int/long mismatch identified here:
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1475 /* args 1-4 should match listsort in Objects/listobject.c */
35608
a670a5dd1fc6 Removed a check "if (args != NULL)" which is always True and makes no sense.
Armin Rigo <arigo@tunes.org>
parents: 35567
diff changeset
1476 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
a670a5dd1fc6 Removed a check "if (args != NULL)" which is always True and makes no sense.
Armin Rigo <arigo@tunes.org>
parents: 35567
diff changeset
1477 kwlist, &seq, &compare, &keyfunc, &reverse))
a670a5dd1fc6 Removed a check "if (args != NULL)" which is always True and makes no sense.
Armin Rigo <arigo@tunes.org>
parents: 35567
diff changeset
1478 return NULL;
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1479
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1480 newlist = PySequence_List(seq);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1481 if (newlist == NULL)
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1482 return NULL;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1483
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1484 callable = PyObject_GetAttrString(newlist, "sort");
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1485 if (callable == NULL) {
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1486 Py_DECREF(newlist);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1487 return NULL;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1488 }
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
1489
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1490 newargs = PyTuple_GetSlice(args, 1, 4);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1491 if (newargs == NULL) {
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1492 Py_DECREF(newlist);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1493 Py_DECREF(callable);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1494 return NULL;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1495 }
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1496
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1497 v = PyObject_Call(callable, newargs, kwds);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1498 Py_DECREF(newargs);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1499 Py_DECREF(callable);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1500 if (v == NULL) {
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1501 Py_DECREF(newlist);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1502 return NULL;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1503 }
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1504 Py_DECREF(v);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1505 return newlist;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1506 }
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1507
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1508 PyDoc_STRVAR(sorted_doc,
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1509 "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1510
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1511 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1512 builtin_vars(PyObject *self, PyObject *args)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1513 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1514 PyObject *v = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1515 PyObject *d;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1516
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1517 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1518 return NULL;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1519 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1520 d = PyEval_GetLocals();
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1521 if (d == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1522 if (!PyErr_Occurred())
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1523 PyErr_SetString(PyExc_SystemError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
1524 "vars(): no locals!?");
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1525 }
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1526 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1527 Py_INCREF(d);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1528 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1529 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1530 d = PyObject_GetAttrString(v, "__dict__");
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1531 if (d == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1532 PyErr_SetString(PyExc_TypeError,
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1533 "vars() argument must have __dict__ attribute");
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1534 return NULL;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1535 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1536 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1537 return d;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1538 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1539
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1540 PyDoc_STRVAR(vars_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1541 "vars([object]) -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1542 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1543 Without arguments, equivalent to locals().\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1544 With an argument, equivalent to object.__dict__.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1545
42713
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1546 static PyObject *
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1547 builtin_trunc(PyObject *self, PyObject *number)
42713
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1548 {
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1549 static PyObject *trunc_str = NULL;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1550 PyObject *trunc;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1551
42760
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1552 if (Py_Type(number)->tp_dict == NULL) {
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1553 if (PyType_Ready(Py_Type(number)) < 0)
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1554 return NULL;
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1555 }
e3e06b389406 Before calling _PyType_Lookup() the type needs to be initialized.
Guido van Rossum <guido@python.org>
parents: 42753
diff changeset
1556
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1557 if (trunc_str == NULL) {
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1558 trunc_str = PyUnicode_FromString("__trunc__");
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1559 if (trunc_str == NULL)
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1560 return NULL;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1561 }
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1562
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1563 trunc = _PyType_Lookup(Py_Type(number), trunc_str);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1564 if (trunc == NULL) {
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1565 PyErr_Format(PyExc_TypeError,
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1566 "type %.100s doesn't define __trunc__ method",
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1567 Py_Type(number)->tp_name);
42713
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1568 return NULL;
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1569 }
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42718
diff changeset
1570 return PyObject_CallFunction(trunc, "O", number);
42713
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1571 }
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1572
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1573 PyDoc_STRVAR(trunc_doc,
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1574 "trunc(Real) -> Integral\n\
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1575 \n\
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1576 returns the integral closest to x between 0 and x.");
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1577
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1578
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1579
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1580 static PyObject*
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1581 builtin_sum(PyObject *self, PyObject *args)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1582 {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1583 PyObject *seq;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1584 PyObject *result = NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1585 PyObject *temp, *item, *iter;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1586
30774
2d98710f272e Use PyArg_UnpackTuple() where possible.
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
1587 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1588 return NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1589
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1590 iter = PyObject_GetIter(seq);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1591 if (iter == NULL)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1592 return NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1593
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1594 if (result == NULL) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1595 result = PyInt_FromLong(0);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1596 if (result == NULL) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1597 Py_DECREF(iter);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1598 return NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1599 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1600 } else {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1601 /* reject string values for 'start' parameter */
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
1602 if (PyUnicode_Check(result)) {
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1603 PyErr_SetString(PyExc_TypeError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
1604 "sum() can't sum strings [use ''.join(seq) instead]");
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1605 Py_DECREF(iter);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1606 return NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1607 }
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
1608 if (PyBytes_Check(result)) {
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
1609 PyErr_SetString(PyExc_TypeError,
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
1610 "sum() can't sum bytes [use b''.join(seq) instead]");
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
1611 Py_DECREF(iter);
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
1612 return NULL;
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
1613 }
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
1614
28718
16ef5b9bc789 fixed a potential refcount bug (thanks Raymond!).
Alex Martelli <aleaxit@gmail.com>
parents: 28717
diff changeset
1615 Py_INCREF(result);
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1616 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1617
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1618 #ifndef SLOW_SUM
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1619 /* Fast addition by keeping temporary sums in C instead of new Python objects.
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1620 Assumes all inputs are the same type. If the assumption fails, default
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1621 to the more general routine.
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1622 */
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1623 if (PyInt_CheckExact(result)) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1624 long i_result = PyInt_AS_LONG(result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1625 Py_DECREF(result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1626 result = NULL;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1627 while(result == NULL) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1628 item = PyIter_Next(iter);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1629 if (item == NULL) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1630 Py_DECREF(iter);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1631 if (PyErr_Occurred())
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1632 return NULL;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1633 return PyInt_FromLong(i_result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1634 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1635 if (PyInt_CheckExact(item)) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1636 long b = PyInt_AS_LONG(item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1637 long x = i_result + b;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1638 if ((x^i_result) >= 0 || (x^b) >= 0) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1639 i_result = x;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1640 Py_DECREF(item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1641 continue;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1642 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1643 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1644 /* Either overflowed or is not an int. Restore real objects and process normally */
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1645 result = PyInt_FromLong(i_result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1646 temp = PyNumber_Add(result, item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1647 Py_DECREF(result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1648 Py_DECREF(item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1649 result = temp;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1650 if (result == NULL) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1651 Py_DECREF(iter);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1652 return NULL;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1653 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1654 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1655 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1656
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1657 if (PyFloat_CheckExact(result)) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1658 double f_result = PyFloat_AS_DOUBLE(result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1659 Py_DECREF(result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1660 result = NULL;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1661 while(result == NULL) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1662 item = PyIter_Next(iter);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1663 if (item == NULL) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1664 Py_DECREF(iter);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1665 if (PyErr_Occurred())
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1666 return NULL;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1667 return PyFloat_FromDouble(f_result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1668 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1669 if (PyFloat_CheckExact(item)) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1670 PyFPE_START_PROTECT("add", return 0)
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1671 f_result += PyFloat_AS_DOUBLE(item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1672 PyFPE_END_PROTECT(f_result)
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1673 Py_DECREF(item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1674 continue;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1675 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1676 if (PyInt_CheckExact(item)) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1677 PyFPE_START_PROTECT("add", return 0)
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1678 f_result += (double)PyInt_AS_LONG(item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1679 PyFPE_END_PROTECT(f_result)
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1680 Py_DECREF(item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1681 continue;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1682 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1683 result = PyFloat_FromDouble(f_result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1684 temp = PyNumber_Add(result, item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1685 Py_DECREF(result);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1686 Py_DECREF(item);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1687 result = temp;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1688 if (result == NULL) {
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1689 Py_DECREF(iter);
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1690 return NULL;
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1691 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1692 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1693 }
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1694 #endif
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43577
diff changeset
1695
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1696 for(;;) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1697 item = PyIter_Next(iter);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1698 if (item == NULL) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1699 /* error, or end-of-sequence */
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1700 if (PyErr_Occurred()) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1701 Py_DECREF(result);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1702 result = NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1703 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1704 break;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1705 }
30784
37285a0346c1 regressing the performance bugfix -- Guido wants the performance bug left
Alex Martelli <aleaxit@gmail.com>
parents: 30776
diff changeset
1706 temp = PyNumber_Add(result, item);
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1707 Py_DECREF(result);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1708 Py_DECREF(item);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1709 result = temp;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1710 if (result == NULL)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1711 break;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1712 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1713 Py_DECREF(iter);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1714 return result;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1715 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1716
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1717 PyDoc_STRVAR(sum_doc,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40411
diff changeset
1718 "sum(sequence[, start]) -> value\n\
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1719 \n\
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1720 Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40411
diff changeset
1721 of parameter 'start' (which defaults to 0). When the sequence is\n\
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40411
diff changeset
1722 empty, returns start.");
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1723
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1724
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1725 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1726 builtin_isinstance(PyObject *self, PyObject *args)
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1727 {
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1728 PyObject *inst;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1729 PyObject *cls;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1730 int retval;
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1731
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1732 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1733 return NULL;
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
1734
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1735 retval = PyObject_IsInstance(inst, cls);
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1736 if (retval < 0)
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
1737 return NULL;
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
1738 return PyBool_FromLong(retval);
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1739 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1740
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1741 PyDoc_STRVAR(isinstance_doc,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
1742 "isinstance(object, class-or-type-or-tuple) -> bool\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1743 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1744 Return whether an object is an instance of a class or of a subclass thereof.\n\
20307
aa681345bfab Implement isinstance(x, (A, B, ...)). Note that we only allow tuples,
Guido van Rossum <guido@python.org>
parents: 19903
diff changeset
1745 With a type as second argument, return whether that is the object's type.\n\
aa681345bfab Implement isinstance(x, (A, B, ...)). Note that we only allow tuples,
Guido van Rossum <guido@python.org>
parents: 19903
diff changeset
1746 The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1747 isinstance(x, A) or isinstance(x, B) or ... (etc.).");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1748
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1749
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1750 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1751 builtin_issubclass(PyObject *self, PyObject *args)
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1752 {
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1753 PyObject *derived;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1754 PyObject *cls;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1755 int retval;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1756
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1757 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1758 return NULL;
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
1759
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1760 retval = PyObject_IsSubclass(derived, cls);
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1761 if (retval < 0)
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1762 return NULL;
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
1763 return PyBool_FromLong(retval);
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1764 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1765
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1766 PyDoc_STRVAR(issubclass_doc,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
1767 "issubclass(C, B) -> bool\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1768 \n\
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 25924
diff changeset
1769 Return whether class C is a subclass (i.e., a derived class) of class B.\n\
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 25924
diff changeset
1770 When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 25924
diff changeset
1771 is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1772
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1773
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1774 static PyObject*
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1775 builtin_zip(PyObject *self, PyObject *args)
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1776 {
39686
aca709f33a6a SF patch 1546297 (with some tweaks):
Guido van Rossum <guido@python.org>
parents: 39656
diff changeset
1777 /* args must be a tuple */
aca709f33a6a SF patch 1546297 (with some tweaks):
Guido van Rossum <guido@python.org>
parents: 39656
diff changeset
1778 assert(PyTuple_Check(args));
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1779
39686
aca709f33a6a SF patch 1546297 (with some tweaks):
Guido van Rossum <guido@python.org>
parents: 39656
diff changeset
1780 return _PyZip_CreateIter(args);
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1781 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1782
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1783
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1784 PyDoc_STRVAR(zip_doc,
39656
08005ba6ceb9 Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents: 39645
diff changeset
1785 "zip(it1 [, it2 [...]]) -> iter([(it1[0], it2[0] ...), ...])\n\
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1786 \n\
39656
08005ba6ceb9 Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents: 39645
diff changeset
1787 Return an iterator yielding tuples, where each tuple contains the\n\
08005ba6ceb9 Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents: 39645
diff changeset
1788 corresponding element from each of the argument iterables.\n\
08005ba6ceb9 Make built-in zip() equal to itertools.izip().
Guido van Rossum <guido@python.org>
parents: 39645
diff changeset
1789 The returned iterator ends when the shortest argument iterable is exhausted.\n\
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
1790 (This is identical to itertools.izip().)");
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1791
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1792
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1793 static PyMethodDef builtin_methods[] = {
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
1794 {"__build_class__", (PyCFunction)builtin___build_class__,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 41107
diff changeset
1795 METH_VARARGS | METH_KEYWORDS, build_class_doc},
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
1796 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1797 {"abs", builtin_abs, METH_O, abs_doc},
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
1798 {"all", builtin_all, METH_O, all_doc},
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
1799 {"any", builtin_any, METH_O, any_doc},
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42071
diff changeset
1800 {"bin", builtin_bin, METH_O, bin_doc},
43121
8834e65683e7 Remove chr8, we're not likely to need it at this point
Neal Norwitz <nnorwitz@gmail.com>
parents: 43110
diff changeset
1801 {"chr", builtin_chr, METH_VARARGS, chr_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1802 {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1803 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1804 {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1805 {"dir", builtin_dir, METH_VARARGS, dir_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1806 {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1807 {"eval", builtin_eval, METH_VARARGS, eval_doc},
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39686
diff changeset
1808 {"exec", builtin_exec, METH_VARARGS, exec_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1809 {"filter", builtin_filter, METH_VARARGS, filter_doc},
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42760
diff changeset
1810 {"format", builtin_format, METH_VARARGS, format_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1811 {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1812 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1813 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1814 {"hash", builtin_hash, METH_O, hash_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1815 {"hex", builtin_hex, METH_O, hex_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1816 {"id", builtin_id, METH_O, id_doc},
40933
a2629be1b1d0 Add raw_input() back, named input(). Revive the old unittests too.
Guido van Rossum <guido@python.org>
parents: 40922
diff changeset
1817 {"input", builtin_input, METH_VARARGS, input_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1818 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1819 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1820 {"iter", builtin_iter, METH_VARARGS, iter_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1821 {"len", builtin_len, METH_O, len_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1822 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1823 {"map", builtin_map, METH_VARARGS, map_doc},
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1824 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1825 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41452
diff changeset
1826 {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1827 {"oct", builtin_oct, METH_O, oct_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1828 {"ord", builtin_ord, METH_O, ord_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1829 {"pow", builtin_pow, METH_VARARGS, pow_doc},
40774
793e0323d4d6 Essential changes for print function changes.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1830 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1831 {"repr", builtin_repr, METH_O, repr_doc},
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
1832 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1833 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
1834 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
1835 {"sum", builtin_sum, METH_VARARGS, sum_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1836 {"vars", builtin_vars, METH_VARARGS, vars_doc},
42713
aad17b1ab0c9 Implement the trunc builtin for PEP 3141
Alex Martelli <aleaxit@gmail.com>
parents: 42665
diff changeset
1837 {"trunc", builtin_trunc, METH_O, trunc_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1838 {"zip", builtin_zip, METH_VARARGS, zip_doc},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
1839 {NULL, NULL},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1840 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1841
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1842 PyDoc_STRVAR(builtin_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1843 "Built-in functions, exceptions, and other objects.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1844 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1845 Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1846
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1847 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1848 _PyBuiltin_Init(void)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1849 {
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1850 PyObject *mod, *dict, *debug;
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1851 mod = Py_InitModule4("__builtin__", builtin_methods,
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1852 builtin_doc, (PyObject *)NULL,
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1853 PYTHON_API_VERSION);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1854 if (mod == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1855 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1856 dict = PyModule_GetDict(mod);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1857
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1858 #ifdef Py_TRACE_REFS
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1859 /* __builtin__ exposes a number of statically allocated objects
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1860 * that, before this code was added in 2.3, never showed up in
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1861 * the list of "all objects" maintained by Py_TRACE_REFS. As a
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1862 * result, programs leaking references to None and False (etc)
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1863 * couldn't be diagnosed by examining sys.getobjects(0).
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1864 */
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1865 #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1866 #else
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1867 #define ADD_TO_ALL(OBJECT) (void)0
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1868 #endif
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1869
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1870 #define SETBUILTIN(NAME, OBJECT) \
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1871 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1872 return NULL; \
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1873 ADD_TO_ALL(OBJECT)
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1874
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1875 SETBUILTIN("None", Py_None);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1876 SETBUILTIN("Ellipsis", Py_Ellipsis);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1877 SETBUILTIN("NotImplemented", Py_NotImplemented);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
1878 SETBUILTIN("False", Py_False);
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
1879 SETBUILTIN("True", Py_True);
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
1880 SETBUILTIN("bool", &PyBool_Type);
43487
8c10aac37037 Breaking ground for PEP 3137 implementation:
Guido van Rossum <guido@python.org>
parents: 43216
diff changeset
1881 SETBUILTIN("memoryview", &PyMemoryView_Type);
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1882 SETBUILTIN("buffer", &PyBytes_Type);
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1883 SETBUILTIN("bytes", &PyString_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1884 SETBUILTIN("classmethod", &PyClassMethod_Type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1885 #ifndef WITHOUT_COMPLEX
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1886 SETBUILTIN("complex", &PyComplex_Type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1887 #endif
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
1888 SETBUILTIN("dict", &PyDict_Type);
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
1889 SETBUILTIN("enumerate", &PyEnum_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1890 SETBUILTIN("float", &PyFloat_Type);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
1891 SETBUILTIN("frozenset", &PyFrozenSet_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1892 SETBUILTIN("property", &PyProperty_Type);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40484
diff changeset
1893 SETBUILTIN("int", &PyLong_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1894 SETBUILTIN("list", &PyList_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1895 SETBUILTIN("object", &PyBaseObject_Type);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41609
diff changeset
1896 SETBUILTIN("range", &PyRange_Type);
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 30784
diff changeset
1897 SETBUILTIN("reversed", &PyReversed_Type);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
1898 SETBUILTIN("set", &PySet_Type);
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
1899 SETBUILTIN("slice", &PySlice_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1900 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
41558
29ae667f3239 Checkpoint. Manipulated things so that string literals are always
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1901 SETBUILTIN("str", &PyUnicode_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1902 SETBUILTIN("super", &PySuper_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1903 SETBUILTIN("tuple", &PyTuple_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1904 SETBUILTIN("type", &PyType_Type);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
1905 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1906 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1907 Py_XDECREF(debug);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1908 return NULL;
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1909 }
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1910 Py_XDECREF(debug);
6198
c43ca8dd0081 Removed obsolete exception PyExc_AccessError.
Barry Warsaw <barry@python.org>
parents: 6145
diff changeset
1911
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1912 return mod;
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
1913 #undef ADD_TO_ALL
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
1914 #undef SETBUILTIN
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1915 }