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