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