annotate cos/python/Python/bltinmodule.c @ 189:5334b86ac1db

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