/src/cpython/Python/bltinmodule.c
Line | Count | Source |
1 | | /* Built-in functions */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "pycore_ast.h" // _PyAST_Validate() |
5 | | #include "pycore_call.h" // _PyObject_CallNoArgs() |
6 | | #include "pycore_cell.h" // PyCell_GetRef() |
7 | | #include "pycore_ceval.h" // _PyEval_Vector() |
8 | | #include "pycore_compile.h" // _PyAST_Compile() |
9 | | #include "pycore_fileutils.h" // _PyFile_Flush |
10 | | #include "pycore_floatobject.h" // _PyFloat_ExactDealloc() |
11 | | #include "pycore_interp.h" // _PyInterpreterState_GetConfig() |
12 | | #include "pycore_import.h" // _PyImport_LazyImportModuleLevelObject () |
13 | | #include "pycore_iterobject.h" // _PyCallIter_NewEx() |
14 | | #include "pycore_long.h" // _PyLong_CompactValue |
15 | | #include "pycore_modsupport.h" // _PyArg_NoKwnames() |
16 | | #include "pycore_object.h" // _Py_AddToAllObjects() |
17 | | #include "pycore_pyerrors.h" // _PyErr_NoMemory() |
18 | | #include "pycore_pystate.h" // _PyThreadState_GET() |
19 | | #include "pycore_pythonrun.h" // _Py_SourceAsString() |
20 | | #include "pycore_tuple.h" // _PyTuple_Recycle() |
21 | | |
22 | | #include "clinic/bltinmodule.c.h" |
23 | | |
24 | | #ifdef HAVE_UNISTD_H |
25 | | # include <unistd.h> // isatty() |
26 | | #endif |
27 | | |
28 | | |
29 | | static PyObject* |
30 | | update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs) |
31 | 11.4k | { |
32 | 11.4k | Py_ssize_t i, j; |
33 | 11.4k | PyObject *base, *meth, *new_base, *result, *new_bases = NULL; |
34 | 11.4k | assert(PyTuple_Check(bases)); |
35 | | |
36 | 21.5k | for (i = 0; i < nargs; i++) { |
37 | 10.0k | base = args[i]; |
38 | 10.0k | if (PyType_Check(base)) { |
39 | 9.88k | if (new_bases) { |
40 | | /* If we already have made a replacement, then we append every normal base, |
41 | | otherwise just skip it. */ |
42 | 0 | if (PyList_Append(new_bases, base) < 0) { |
43 | 0 | goto error; |
44 | 0 | } |
45 | 0 | } |
46 | 9.88k | continue; |
47 | 9.88k | } |
48 | 197 | if (PyObject_GetOptionalAttr(base, &_Py_ID(__mro_entries__), &meth) < 0) { |
49 | 0 | goto error; |
50 | 0 | } |
51 | 197 | if (!meth) { |
52 | 0 | if (new_bases) { |
53 | 0 | if (PyList_Append(new_bases, base) < 0) { |
54 | 0 | goto error; |
55 | 0 | } |
56 | 0 | } |
57 | 0 | continue; |
58 | 0 | } |
59 | 197 | new_base = PyObject_CallOneArg(meth, bases); |
60 | 197 | Py_DECREF(meth); |
61 | 197 | if (!new_base) { |
62 | 0 | goto error; |
63 | 0 | } |
64 | 197 | if (!PyTuple_Check(new_base)) { |
65 | 0 | PyErr_SetString(PyExc_TypeError, |
66 | 0 | "__mro_entries__ must return a tuple"); |
67 | 0 | Py_DECREF(new_base); |
68 | 0 | goto error; |
69 | 0 | } |
70 | 197 | if (!new_bases) { |
71 | | /* If this is a first successful replacement, create new_bases list and |
72 | | copy previously encountered bases. */ |
73 | 193 | if (!(new_bases = PyList_New(i))) { |
74 | 0 | Py_DECREF(new_base); |
75 | 0 | goto error; |
76 | 0 | } |
77 | 209 | for (j = 0; j < i; j++) { |
78 | 16 | base = args[j]; |
79 | 16 | PyList_SET_ITEM(new_bases, j, Py_NewRef(base)); |
80 | 16 | } |
81 | 193 | } |
82 | 197 | j = PyList_GET_SIZE(new_bases); |
83 | 197 | if (PyList_SetSlice(new_bases, j, j, new_base) < 0) { |
84 | 0 | Py_DECREF(new_base); |
85 | 0 | goto error; |
86 | 0 | } |
87 | 197 | Py_DECREF(new_base); |
88 | 197 | } |
89 | 11.4k | if (!new_bases) { |
90 | 11.2k | return bases; |
91 | 11.2k | } |
92 | 193 | result = PyList_AsTuple(new_bases); |
93 | 193 | Py_DECREF(new_bases); |
94 | 193 | return result; |
95 | | |
96 | 0 | error: |
97 | 0 | Py_XDECREF(new_bases); |
98 | 0 | return NULL; |
99 | 11.4k | } |
100 | | |
101 | | /* AC: cannot convert yet, waiting for *args support */ |
102 | | static PyObject * |
103 | | builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, |
104 | | PyObject *kwnames) |
105 | 11.4k | { |
106 | 11.4k | PyObject *func, *name, *winner, *prep; |
107 | 11.4k | PyObject *cls = NULL, *cell = NULL, *ns = NULL, *meta = NULL, *orig_bases = NULL; |
108 | 11.4k | PyObject *mkw = NULL, *bases = NULL; |
109 | 11.4k | int isclass = 0; /* initialize to prevent gcc warning */ |
110 | | |
111 | 11.4k | if (nargs < 2) { |
112 | 0 | PyErr_SetString(PyExc_TypeError, |
113 | 0 | "__build_class__: not enough arguments"); |
114 | 0 | return NULL; |
115 | 0 | } |
116 | 11.4k | func = args[0]; /* Better be callable */ |
117 | 11.4k | if (!PyFunction_Check(func)) { |
118 | 0 | PyErr_SetString(PyExc_TypeError, |
119 | 0 | "__build_class__: func must be a function"); |
120 | 0 | return NULL; |
121 | 0 | } |
122 | 11.4k | name = args[1]; |
123 | 11.4k | if (!PyUnicode_Check(name)) { |
124 | 0 | PyErr_SetString(PyExc_TypeError, |
125 | 0 | "__build_class__: name is not a string"); |
126 | 0 | return NULL; |
127 | 0 | } |
128 | 11.4k | orig_bases = PyTuple_FromArray(args + 2, nargs - 2); |
129 | 11.4k | if (orig_bases == NULL) |
130 | 0 | return NULL; |
131 | | |
132 | 11.4k | bases = update_bases(orig_bases, args + 2, nargs - 2); |
133 | 11.4k | if (bases == NULL) { |
134 | 0 | Py_DECREF(orig_bases); |
135 | 0 | return NULL; |
136 | 0 | } |
137 | | |
138 | 11.4k | if (kwnames == NULL) { |
139 | 10.7k | meta = NULL; |
140 | 10.7k | mkw = NULL; |
141 | 10.7k | } |
142 | 710 | else { |
143 | 710 | mkw = _PyStack_AsDict(args + nargs, kwnames); |
144 | 710 | if (mkw == NULL) { |
145 | 0 | goto error; |
146 | 0 | } |
147 | | |
148 | 710 | if (PyDict_Pop(mkw, &_Py_ID(metaclass), &meta) < 0) { |
149 | 0 | goto error; |
150 | 0 | } |
151 | 710 | if (meta != NULL) { |
152 | | /* metaclass is explicitly given, check if it's indeed a class */ |
153 | 550 | isclass = PyType_Check(meta); |
154 | 550 | } |
155 | 710 | } |
156 | 11.4k | if (meta == NULL) { |
157 | | /* if there are no bases, use type: */ |
158 | 10.9k | if (PyTuple_GET_SIZE(bases) == 0) { |
159 | 2.69k | meta = (PyObject *) (&PyType_Type); |
160 | 2.69k | } |
161 | | /* else get the type of the first base */ |
162 | 8.23k | else { |
163 | 8.23k | PyObject *base0 = PyTuple_GET_ITEM(bases, 0); |
164 | 8.23k | meta = (PyObject *)Py_TYPE(base0); |
165 | 8.23k | } |
166 | 10.9k | Py_INCREF(meta); |
167 | 10.9k | isclass = 1; /* meta is really a class */ |
168 | 10.9k | } |
169 | | |
170 | 11.4k | if (isclass) { |
171 | | /* meta is really a class, so check for a more derived |
172 | | metaclass, or possible metaclass conflicts: */ |
173 | 11.4k | winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta, |
174 | 11.4k | bases); |
175 | 11.4k | if (winner == NULL) { |
176 | 0 | goto error; |
177 | 0 | } |
178 | 11.4k | if (winner != meta) { |
179 | 212 | Py_SETREF(meta, Py_NewRef(winner)); |
180 | 212 | } |
181 | 11.4k | } |
182 | | /* else: meta is not a class, so we cannot do the metaclass |
183 | | calculation, so we will use the explicitly given object as it is */ |
184 | 11.4k | if (PyObject_GetOptionalAttr(meta, &_Py_ID(__prepare__), &prep) < 0) { |
185 | 0 | ns = NULL; |
186 | 0 | } |
187 | 11.4k | else if (prep == NULL) { |
188 | 0 | ns = PyDict_New(); |
189 | 0 | } |
190 | 11.4k | else { |
191 | 11.4k | PyObject *pargs[2] = {name, bases}; |
192 | 11.4k | ns = PyObject_VectorcallDict(prep, pargs, 2, mkw); |
193 | 11.4k | Py_DECREF(prep); |
194 | 11.4k | } |
195 | 11.4k | if (ns == NULL) { |
196 | 0 | goto error; |
197 | 0 | } |
198 | 11.4k | if (!PyMapping_Check(ns)) { |
199 | 0 | PyErr_Format(PyExc_TypeError, |
200 | 0 | "%.200s.__prepare__() must return a mapping, not %.200s", |
201 | 0 | isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>", |
202 | 0 | Py_TYPE(ns)->tp_name); |
203 | 0 | goto error; |
204 | 0 | } |
205 | 11.4k | PyThreadState *tstate = _PyThreadState_GET(); |
206 | 11.4k | EVAL_CALL_STAT_INC(EVAL_CALL_BUILD_CLASS); |
207 | 11.4k | cell = _PyEval_Vector(tstate, (PyFunctionObject *)func, ns, NULL, 0, NULL); |
208 | 11.4k | if (cell != NULL) { |
209 | 11.4k | if (bases != orig_bases) { |
210 | 193 | if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) { |
211 | 0 | goto error; |
212 | 0 | } |
213 | 193 | } |
214 | 11.4k | PyObject *margs[3] = {name, bases, ns}; |
215 | 11.4k | cls = PyObject_VectorcallDict(meta, margs, 3, mkw); |
216 | 11.4k | if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) { |
217 | 1.21k | PyObject *cell_cls = PyCell_GetRef((PyCellObject *)cell); |
218 | 1.21k | if (cell_cls != cls) { |
219 | 0 | if (cell_cls == NULL) { |
220 | 0 | const char *msg = |
221 | 0 | "__class__ not set defining %.200R as %.200R. " |
222 | 0 | "Was __classcell__ propagated to type.__new__?"; |
223 | 0 | PyErr_Format(PyExc_RuntimeError, msg, name, cls); |
224 | 0 | } else { |
225 | 0 | const char *msg = |
226 | 0 | "__class__ set to %.200R defining %.200R as %.200R"; |
227 | 0 | PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls); |
228 | 0 | } |
229 | 0 | Py_XDECREF(cell_cls); |
230 | 0 | Py_SETREF(cls, NULL); |
231 | 0 | goto error; |
232 | 0 | } |
233 | 1.21k | else { |
234 | 1.21k | Py_DECREF(cell_cls); |
235 | 1.21k | } |
236 | 1.21k | } |
237 | 11.4k | } |
238 | 11.4k | error: |
239 | 11.4k | Py_XDECREF(cell); |
240 | 11.4k | Py_XDECREF(ns); |
241 | 11.4k | Py_XDECREF(meta); |
242 | 11.4k | Py_XDECREF(mkw); |
243 | 11.4k | if (bases != orig_bases) { |
244 | 193 | Py_DECREF(orig_bases); |
245 | 193 | } |
246 | 11.4k | Py_DECREF(bases); |
247 | 11.4k | return cls; |
248 | 11.4k | } |
249 | | |
250 | | PyDoc_STRVAR(build_class_doc, |
251 | | "__build_class__(func, name, /, *bases, [metaclass], **kwds) -> class\n\ |
252 | | \n\ |
253 | | Internal helper function used by the class statement."); |
254 | | |
255 | | /*[clinic input] |
256 | | __import__ as builtin___import__ |
257 | | |
258 | | name: object |
259 | | globals: object(c_default="NULL") = None |
260 | | locals: object(c_default="NULL") = None |
261 | | fromlist: object(c_default="NULL") = () |
262 | | level: int = 0 |
263 | | |
264 | | Import a module. |
265 | | |
266 | | Because this function is meant for use by the Python |
267 | | interpreter and not for general use, it is better to use |
268 | | importlib.import_module() to programmatically import a module. |
269 | | |
270 | | The globals argument is only used to determine the context; |
271 | | they are not modified. The locals argument is unused. The fromlist |
272 | | should be a list of names to emulate ``from name import ...``, or an |
273 | | empty list to emulate ``import name``. |
274 | | When importing a module from a package, note that __import__('A.B', ...) |
275 | | returns package A when fromlist is empty, but its submodule B when |
276 | | fromlist is not empty. The level argument is used to determine whether |
277 | | to perform absolute or relative imports: 0 is absolute, while a positive |
278 | | number is the number of parent directories to search relative to the |
279 | | current module. |
280 | | [clinic start generated code]*/ |
281 | | |
282 | | static PyObject * |
283 | | builtin___import___impl(PyObject *module, PyObject *name, PyObject *globals, |
284 | | PyObject *locals, PyObject *fromlist, int level) |
285 | | /*[clinic end generated code: output=4febeda88a0cd245 input=e3096a230383f72d]*/ |
286 | 236k | { |
287 | 236k | return PyImport_ImportModuleLevelObject(name, globals, locals, |
288 | 236k | fromlist, level); |
289 | 236k | } |
290 | | |
291 | | |
292 | | /*[clinic input] |
293 | | __lazy_import__ as builtin___lazy_import__ |
294 | | |
295 | | name: object |
296 | | globals: object(c_default="NULL") = None |
297 | | locals: object(c_default="NULL") = None |
298 | | fromlist: object(c_default="NULL") = () |
299 | | level: int = 0 |
300 | | |
301 | | Lazily imports a module. |
302 | | |
303 | | Returns either the module to be imported or a imp.lazy_module object |
304 | | which indicates the module to be lazily imported. |
305 | | [clinic start generated code]*/ |
306 | | |
307 | | static PyObject * |
308 | | builtin___lazy_import___impl(PyObject *module, PyObject *name, |
309 | | PyObject *globals, PyObject *locals, |
310 | | PyObject *fromlist, int level) |
311 | | /*[clinic end generated code: output=300f1771094b9e8c input=9c85cccd6a885b9b]*/ |
312 | 0 | { |
313 | 0 | PyObject *builtins; |
314 | 0 | PyThreadState *tstate = PyThreadState_GET(); |
315 | 0 | if (globals == NULL) { |
316 | 0 | globals = PyEval_GetGlobals(); |
317 | 0 | if (globals == NULL) { |
318 | 0 | PyErr_SetString(PyExc_TypeError, |
319 | 0 | "__lazy_import__() missing globals " |
320 | 0 | "when called without a frame"); |
321 | 0 | return NULL; |
322 | 0 | } |
323 | 0 | } |
324 | 0 | if (locals == NULL) { |
325 | 0 | locals = globals; |
326 | 0 | } |
327 | |
|
328 | 0 | if (!PyDict_Check(globals)) { |
329 | 0 | PyErr_Format(PyExc_TypeError, |
330 | 0 | "expect dict for globals, got %T", globals); |
331 | 0 | return NULL; |
332 | 0 | } |
333 | | |
334 | 0 | if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) { |
335 | 0 | return NULL; |
336 | 0 | } |
337 | 0 | if (builtins == NULL) { |
338 | 0 | PyErr_SetString(PyExc_ValueError, |
339 | 0 | "unable to get builtins for lazy import"); |
340 | 0 | return NULL; |
341 | 0 | } |
342 | 0 | if (PyModule_Check(builtins)) { |
343 | 0 | PyObject *builtins_dict = Py_XNewRef(PyModule_GetDict(builtins)); |
344 | 0 | if (builtins_dict == NULL) { |
345 | 0 | Py_DECREF(builtins); |
346 | 0 | PyErr_SetString(PyExc_AttributeError, |
347 | 0 | "builtins module has no dict"); |
348 | 0 | return NULL; |
349 | 0 | } |
350 | 0 | Py_SETREF(builtins, builtins_dict); |
351 | 0 | } |
352 | | |
353 | 0 | PyObject *res = _PyImport_LazyImportModuleLevelObject( |
354 | 0 | tstate, name, builtins, globals, locals, fromlist, level); |
355 | 0 | Py_DECREF(builtins); |
356 | 0 | return res; |
357 | 0 | } |
358 | | |
359 | | /*[clinic input] |
360 | | abs as builtin_abs |
361 | | |
362 | | number: object |
363 | | / |
364 | | |
365 | | Return the absolute value of the argument. |
366 | | [clinic start generated code]*/ |
367 | | |
368 | | static PyObject * |
369 | | builtin_abs(PyObject *module, PyObject *number) |
370 | | /*[clinic end generated code: output=861a9db97dee0595 input=a356196903543505]*/ |
371 | 1.98k | { |
372 | 1.98k | return PyNumber_Absolute(number); |
373 | 1.98k | } |
374 | | |
375 | | /*[clinic input] |
376 | | all as builtin_all |
377 | | |
378 | | iterable: object |
379 | | / |
380 | | |
381 | | Return True if bool(x) is True for all values x in the iterable. |
382 | | |
383 | | If the iterable is empty, return True. |
384 | | [clinic start generated code]*/ |
385 | | |
386 | | static PyObject * |
387 | | builtin_all(PyObject *module, PyObject *iterable) |
388 | | /*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/ |
389 | 16 | { |
390 | 16 | PyObject *it, *item; |
391 | 16 | PyObject *(*iternext)(PyObject *); |
392 | 16 | int cmp; |
393 | | |
394 | 16 | it = PyObject_GetIter(iterable); |
395 | 16 | if (it == NULL) |
396 | 0 | return NULL; |
397 | 16 | iternext = *Py_TYPE(it)->tp_iternext; |
398 | | |
399 | 16 | for (;;) { |
400 | 16 | item = iternext(it); |
401 | 16 | if (item == NULL) |
402 | 0 | break; |
403 | 16 | cmp = PyObject_IsTrue(item); |
404 | 16 | Py_DECREF(item); |
405 | 16 | if (cmp < 0) { |
406 | 0 | Py_DECREF(it); |
407 | 0 | return NULL; |
408 | 0 | } |
409 | 16 | if (cmp == 0) { |
410 | 16 | Py_DECREF(it); |
411 | 16 | Py_RETURN_FALSE; |
412 | 16 | } |
413 | 16 | } |
414 | 0 | Py_DECREF(it); |
415 | 0 | if (PyErr_Occurred()) { |
416 | 0 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
417 | 0 | PyErr_Clear(); |
418 | 0 | else |
419 | 0 | return NULL; |
420 | 0 | } |
421 | 0 | Py_RETURN_TRUE; |
422 | 0 | } |
423 | | |
424 | | /*[clinic input] |
425 | | any as builtin_any |
426 | | |
427 | | iterable: object |
428 | | / |
429 | | |
430 | | Return True if bool(x) is True for any x in the iterable. |
431 | | |
432 | | If the iterable is empty, return False. |
433 | | [clinic start generated code]*/ |
434 | | |
435 | | static PyObject * |
436 | | builtin_any(PyObject *module, PyObject *iterable) |
437 | | /*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/ |
438 | 1.19M | { |
439 | 1.19M | PyObject *it, *item; |
440 | 1.19M | PyObject *(*iternext)(PyObject *); |
441 | 1.19M | int cmp; |
442 | | |
443 | 1.19M | it = PyObject_GetIter(iterable); |
444 | 1.19M | if (it == NULL) |
445 | 0 | return NULL; |
446 | 1.19M | iternext = *Py_TYPE(it)->tp_iternext; |
447 | | |
448 | 6.68M | for (;;) { |
449 | 6.68M | item = iternext(it); |
450 | 6.68M | if (item == NULL) |
451 | 1.14M | break; |
452 | 5.54M | cmp = PyObject_IsTrue(item); |
453 | 5.54M | Py_DECREF(item); |
454 | 5.54M | if (cmp < 0) { |
455 | 0 | Py_DECREF(it); |
456 | 0 | return NULL; |
457 | 0 | } |
458 | 5.54M | if (cmp > 0) { |
459 | 47.7k | Py_DECREF(it); |
460 | 47.7k | Py_RETURN_TRUE; |
461 | 47.7k | } |
462 | 5.54M | } |
463 | 1.14M | Py_DECREF(it); |
464 | 1.14M | if (PyErr_Occurred()) { |
465 | 0 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
466 | 0 | PyErr_Clear(); |
467 | 0 | else |
468 | 0 | return NULL; |
469 | 0 | } |
470 | 1.14M | Py_RETURN_FALSE; |
471 | 1.14M | } |
472 | | |
473 | | /*[clinic input] |
474 | | ascii as builtin_ascii |
475 | | |
476 | | obj: object |
477 | | / |
478 | | |
479 | | Return an ASCII-only representation of an object. |
480 | | |
481 | | As repr(), return a string containing a printable representation of an |
482 | | object, but escape the non-ASCII characters in the string returned by |
483 | | repr() using \\x, \\u or \\U escapes. This generates a string similar |
484 | | to that returned by repr() in Python 2. |
485 | | [clinic start generated code]*/ |
486 | | |
487 | | static PyObject * |
488 | | builtin_ascii(PyObject *module, PyObject *obj) |
489 | | /*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/ |
490 | 0 | { |
491 | 0 | return PyObject_ASCII(obj); |
492 | 0 | } |
493 | | |
494 | | |
495 | | /*[clinic input] |
496 | | bin as builtin_bin |
497 | | |
498 | | integer: object |
499 | | / |
500 | | |
501 | | Return the binary representation of an integer. |
502 | | |
503 | | >>> bin(2796202) |
504 | | '0b1010101010101010101010' |
505 | | [clinic start generated code]*/ |
506 | | |
507 | | static PyObject * |
508 | | builtin_bin(PyObject *module, PyObject *integer) |
509 | | /*[clinic end generated code: output=533f9388441805cc input=d16518f148341e70]*/ |
510 | 0 | { |
511 | 0 | return PyNumber_ToBase(integer, 2); |
512 | 0 | } |
513 | | |
514 | | |
515 | | /*[clinic input] |
516 | | callable as builtin_callable |
517 | | |
518 | | obj: object |
519 | | / |
520 | | |
521 | | Return whether the object is callable (i.e., some kind of function). |
522 | | |
523 | | Note that classes are callable, as are instances of classes with a |
524 | | __call__() method. |
525 | | [clinic start generated code]*/ |
526 | | |
527 | | static PyObject * |
528 | | builtin_callable(PyObject *module, PyObject *obj) |
529 | | /*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/ |
530 | 29.1k | { |
531 | 29.1k | return PyBool_FromLong((long)PyCallable_Check(obj)); |
532 | 29.1k | } |
533 | | |
534 | | static PyObject * |
535 | | builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords) |
536 | 0 | { |
537 | 0 | PyObject *hook = PySys_GetAttrString("breakpointhook"); |
538 | 0 | if (hook == NULL) { |
539 | 0 | return NULL; |
540 | 0 | } |
541 | | |
542 | 0 | if (PySys_Audit("builtins.breakpoint", "O", hook) < 0) { |
543 | 0 | Py_DECREF(hook); |
544 | 0 | return NULL; |
545 | 0 | } |
546 | | |
547 | 0 | PyObject *retval = PyObject_Vectorcall(hook, args, nargs, keywords); |
548 | 0 | Py_DECREF(hook); |
549 | 0 | return retval; |
550 | 0 | } |
551 | | |
552 | | PyDoc_STRVAR(breakpoint_doc, |
553 | | "breakpoint($module, /, *args, **kws)\n\ |
554 | | --\n\ |
555 | | \n\ |
556 | | Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept\n\ |
557 | | whatever arguments are passed.\n\ |
558 | | \n\ |
559 | | By default, this drops you into the pdb debugger."); |
560 | | |
561 | | typedef struct { |
562 | | PyObject_HEAD |
563 | | PyObject *func; |
564 | | PyObject *it; |
565 | | } filterobject; |
566 | | |
567 | 100 | #define _filterobject_CAST(op) ((filterobject *)(op)) |
568 | | |
569 | | static PyObject * |
570 | | filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
571 | 0 | { |
572 | 0 | PyObject *func, *seq; |
573 | 0 | PyObject *it; |
574 | 0 | filterobject *lz; |
575 | |
|
576 | 0 | if ((type == &PyFilter_Type || type->tp_init == PyFilter_Type.tp_init) && |
577 | 0 | !_PyArg_NoKeywords("filter", kwds)) |
578 | 0 | return NULL; |
579 | | |
580 | 0 | if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq)) |
581 | 0 | return NULL; |
582 | | |
583 | | /* Get iterator. */ |
584 | 0 | it = PyObject_GetIter(seq); |
585 | 0 | if (it == NULL) |
586 | 0 | return NULL; |
587 | | |
588 | | /* create filterobject structure */ |
589 | 0 | lz = (filterobject *)type->tp_alloc(type, 0); |
590 | 0 | if (lz == NULL) { |
591 | 0 | Py_DECREF(it); |
592 | 0 | return NULL; |
593 | 0 | } |
594 | | |
595 | 0 | lz->func = Py_NewRef(func); |
596 | 0 | lz->it = it; |
597 | |
|
598 | 0 | return (PyObject *)lz; |
599 | 0 | } |
600 | | |
601 | | static PyObject * |
602 | | filter_vectorcall(PyObject *type, PyObject * const*args, |
603 | | size_t nargsf, PyObject *kwnames) |
604 | 22 | { |
605 | 22 | PyTypeObject *tp = _PyType_CAST(type); |
606 | 22 | if (tp == &PyFilter_Type && !_PyArg_NoKwnames("filter", kwnames)) { |
607 | 0 | return NULL; |
608 | 0 | } |
609 | | |
610 | 22 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
611 | 22 | if (!_PyArg_CheckPositional("filter", nargs, 2, 2)) { |
612 | 0 | return NULL; |
613 | 0 | } |
614 | | |
615 | 22 | PyObject *it = PyObject_GetIter(args[1]); |
616 | 22 | if (it == NULL) { |
617 | 0 | return NULL; |
618 | 0 | } |
619 | | |
620 | 22 | filterobject *lz = (filterobject *)tp->tp_alloc(tp, 0); |
621 | | |
622 | 22 | if (lz == NULL) { |
623 | 0 | Py_DECREF(it); |
624 | 0 | return NULL; |
625 | 0 | } |
626 | | |
627 | 22 | lz->func = Py_NewRef(args[0]); |
628 | 22 | lz->it = it; |
629 | | |
630 | 22 | return (PyObject *)lz; |
631 | 22 | } |
632 | | |
633 | | static void |
634 | | filter_dealloc(PyObject *self) |
635 | 22 | { |
636 | 22 | filterobject *lz = _filterobject_CAST(self); |
637 | 22 | PyObject_GC_UnTrack(lz); |
638 | 22 | Py_XDECREF(lz->func); |
639 | 22 | Py_XDECREF(lz->it); |
640 | 22 | Py_TYPE(lz)->tp_free(lz); |
641 | 22 | } |
642 | | |
643 | | static int |
644 | | filter_traverse(PyObject *self, visitproc visit, void *arg) |
645 | 0 | { |
646 | 0 | filterobject *lz = _filterobject_CAST(self); |
647 | 0 | Py_VISIT(lz->it); |
648 | 0 | Py_VISIT(lz->func); |
649 | 0 | return 0; |
650 | 0 | } |
651 | | |
652 | | static PyObject * |
653 | | filter_next(PyObject *self) |
654 | 78 | { |
655 | 78 | filterobject *lz = _filterobject_CAST(self); |
656 | 78 | PyObject *item; |
657 | 78 | PyObject *it = lz->it; |
658 | 78 | long ok; |
659 | 78 | PyObject *(*iternext)(PyObject *); |
660 | 78 | int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type; |
661 | | |
662 | 78 | iternext = *Py_TYPE(it)->tp_iternext; |
663 | 90 | for (;;) { |
664 | 90 | item = iternext(it); |
665 | 90 | if (item == NULL) |
666 | 22 | return NULL; |
667 | | |
668 | 68 | if (checktrue) { |
669 | 12 | ok = PyObject_IsTrue(item); |
670 | 56 | } else { |
671 | 56 | PyObject *good; |
672 | 56 | good = PyObject_CallOneArg(lz->func, item); |
673 | 56 | if (good == NULL) { |
674 | 0 | Py_DECREF(item); |
675 | 0 | return NULL; |
676 | 0 | } |
677 | 56 | ok = PyObject_IsTrue(good); |
678 | 56 | Py_DECREF(good); |
679 | 56 | } |
680 | 68 | if (ok > 0) |
681 | 56 | return item; |
682 | 12 | Py_DECREF(item); |
683 | 12 | if (ok < 0) |
684 | 0 | return NULL; |
685 | 12 | } |
686 | 78 | } |
687 | | |
688 | | static PyObject * |
689 | | filter_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
690 | 0 | { |
691 | 0 | filterobject *lz = _filterobject_CAST(self); |
692 | 0 | return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it); |
693 | 0 | } |
694 | | |
695 | | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
696 | | |
697 | | static PyMethodDef filter_methods[] = { |
698 | | {"__reduce__", filter_reduce, METH_NOARGS, reduce_doc}, |
699 | | {NULL, NULL} /* sentinel */ |
700 | | }; |
701 | | |
702 | | PyDoc_STRVAR(filter_doc, |
703 | | "filter(function, iterable, /)\n\ |
704 | | --\n\ |
705 | | \n\ |
706 | | Return an iterator yielding those items of iterable for which\n\ |
707 | | function(item) is true. If function is None, return the items that\n\ |
708 | | are true."); |
709 | | |
710 | | PyTypeObject PyFilter_Type = { |
711 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
712 | | "filter", /* tp_name */ |
713 | | sizeof(filterobject), /* tp_basicsize */ |
714 | | 0, /* tp_itemsize */ |
715 | | /* methods */ |
716 | | filter_dealloc, /* tp_dealloc */ |
717 | | 0, /* tp_vectorcall_offset */ |
718 | | 0, /* tp_getattr */ |
719 | | 0, /* tp_setattr */ |
720 | | 0, /* tp_as_async */ |
721 | | 0, /* tp_repr */ |
722 | | 0, /* tp_as_number */ |
723 | | 0, /* tp_as_sequence */ |
724 | | 0, /* tp_as_mapping */ |
725 | | 0, /* tp_hash */ |
726 | | 0, /* tp_call */ |
727 | | 0, /* tp_str */ |
728 | | PyObject_GenericGetAttr, /* tp_getattro */ |
729 | | 0, /* tp_setattro */ |
730 | | 0, /* tp_as_buffer */ |
731 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
732 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
733 | | filter_doc, /* tp_doc */ |
734 | | filter_traverse, /* tp_traverse */ |
735 | | 0, /* tp_clear */ |
736 | | 0, /* tp_richcompare */ |
737 | | 0, /* tp_weaklistoffset */ |
738 | | PyObject_SelfIter, /* tp_iter */ |
739 | | filter_next, /* tp_iternext */ |
740 | | filter_methods, /* tp_methods */ |
741 | | 0, /* tp_members */ |
742 | | 0, /* tp_getset */ |
743 | | 0, /* tp_base */ |
744 | | 0, /* tp_dict */ |
745 | | 0, /* tp_descr_get */ |
746 | | 0, /* tp_descr_set */ |
747 | | 0, /* tp_dictoffset */ |
748 | | 0, /* tp_init */ |
749 | | PyType_GenericAlloc, /* tp_alloc */ |
750 | | filter_new, /* tp_new */ |
751 | | PyObject_GC_Del, /* tp_free */ |
752 | | .tp_vectorcall = filter_vectorcall |
753 | | }; |
754 | | |
755 | | |
756 | | /*[clinic input] |
757 | | format as builtin_format |
758 | | |
759 | | value: object |
760 | | format_spec: unicode(c_default="NULL") = '' |
761 | | / |
762 | | |
763 | | Return type(value).__format__(value, format_spec) |
764 | | |
765 | | Many built-in types implement format_spec according to the |
766 | | Format Specification Mini-language. See help('FORMATTING'). |
767 | | |
768 | | If type(value) does not supply a method named __format__ |
769 | | and format_spec is empty, then str(value) is returned. |
770 | | See also help('SPECIALMETHODS'). |
771 | | [clinic start generated code]*/ |
772 | | |
773 | | static PyObject * |
774 | | builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec) |
775 | | /*[clinic end generated code: output=2f40bdfa4954b077 input=45ef3934b86d5624]*/ |
776 | 0 | { |
777 | 0 | return PyObject_Format(value, format_spec); |
778 | 0 | } |
779 | | |
780 | | /*[clinic input] |
781 | | @permit_long_summary |
782 | | chr as builtin_chr |
783 | | |
784 | | i: object |
785 | | / |
786 | | |
787 | | Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff. |
788 | | [clinic start generated code]*/ |
789 | | |
790 | | static PyObject * |
791 | | builtin_chr(PyObject *module, PyObject *i) |
792 | | /*[clinic end generated code: output=d34f25b8035a9b10 input=a9b255f2d2e503f0]*/ |
793 | 7.41M | { |
794 | 7.41M | int overflow; |
795 | 7.41M | long v = PyLong_AsLongAndOverflow(i, &overflow); |
796 | 7.41M | if (v == -1 && PyErr_Occurred()) { |
797 | 0 | return NULL; |
798 | 0 | } |
799 | 7.41M | if (overflow) { |
800 | 0 | v = overflow < 0 ? INT_MIN : INT_MAX; |
801 | | /* Allow PyUnicode_FromOrdinal() to raise an exception */ |
802 | 0 | } |
803 | 7.41M | #if SIZEOF_INT < SIZEOF_LONG |
804 | 7.41M | else if (v < INT_MIN) { |
805 | 0 | v = INT_MIN; |
806 | 0 | } |
807 | 7.41M | else if (v > INT_MAX) { |
808 | 15 | v = INT_MAX; |
809 | 15 | } |
810 | 7.41M | #endif |
811 | 7.41M | return PyUnicode_FromOrdinal(v); |
812 | 7.41M | } |
813 | | |
814 | | |
815 | | /*[clinic input] |
816 | | @permit_long_summary |
817 | | compile as builtin_compile |
818 | | |
819 | | source: object |
820 | | filename: unicode_fs_decoded |
821 | | mode: str |
822 | | flags: int = 0 |
823 | | dont_inherit: bool = False |
824 | | optimize: int = -1 |
825 | | * |
826 | | module as modname: object = None |
827 | | _feature_version as feature_version: int = -1 |
828 | | |
829 | | Compile source into a code object that can be executed by exec() or eval(). |
830 | | |
831 | | The source code may represent a Python module, statement or |
832 | | expression. |
833 | | The filename will be used for run-time error messages. |
834 | | The mode must be 'exec' to compile a module, 'single' to compile a |
835 | | single (interactive) statement, or 'eval' to compile an expression. |
836 | | The flags argument, if present, controls which future statements |
837 | | influence the compilation of the code. |
838 | | The dont_inherit argument, if true, stops the compilation inheriting |
839 | | the effects of any future statements in effect in the code calling |
840 | | compile; if absent or false these statements do influence the |
841 | | compilation, in addition to any features explicitly specified. |
842 | | [clinic start generated code]*/ |
843 | | |
844 | | static PyObject * |
845 | | builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, |
846 | | const char *mode, int flags, int dont_inherit, |
847 | | int optimize, PyObject *modname, int feature_version) |
848 | | /*[clinic end generated code: output=9a0dce1945917a86 input=444c4fe466a97279]*/ |
849 | 64.4k | { |
850 | 64.4k | PyObject *source_copy; |
851 | 64.4k | const char *str; |
852 | 64.4k | int compile_mode = -1; |
853 | 64.4k | int is_ast; |
854 | 64.4k | int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input}; |
855 | 64.4k | PyObject *result; |
856 | | |
857 | 64.4k | PyCompilerFlags cf = _PyCompilerFlags_INIT; |
858 | 64.4k | cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8; |
859 | 64.4k | if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) { |
860 | 0 | cf.cf_feature_version = feature_version; |
861 | 0 | } |
862 | | |
863 | 64.4k | if (flags & |
864 | 64.4k | ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_COMPILE_MASK)) |
865 | 0 | { |
866 | 0 | PyErr_SetString(PyExc_ValueError, |
867 | 0 | "compile(): unrecognised flags"); |
868 | 0 | goto error; |
869 | 0 | } |
870 | | /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ |
871 | | |
872 | 64.4k | if (optimize < -1 || optimize > 2) { |
873 | 0 | PyErr_SetString(PyExc_ValueError, |
874 | 0 | "compile(): invalid optimize value"); |
875 | 0 | goto error; |
876 | 0 | } |
877 | 64.4k | if (modname == Py_None) { |
878 | 64.3k | modname = NULL; |
879 | 64.3k | } |
880 | 140 | else if (!PyUnicode_Check(modname)) { |
881 | 0 | PyErr_Format(PyExc_TypeError, |
882 | 0 | "compile() argument 'module' must be str or None, not %T", |
883 | 0 | modname); |
884 | 0 | goto error; |
885 | 0 | } |
886 | | |
887 | 64.4k | if (!dont_inherit) { |
888 | 64.3k | PyEval_MergeCompilerFlags(&cf); |
889 | 64.3k | } |
890 | | |
891 | 64.4k | if (strcmp(mode, "exec") == 0) |
892 | 64.4k | compile_mode = 0; |
893 | 4 | else if (strcmp(mode, "eval") == 0) |
894 | 4 | compile_mode = 1; |
895 | 0 | else if (strcmp(mode, "single") == 0) |
896 | 0 | compile_mode = 2; |
897 | 0 | else if (strcmp(mode, "func_type") == 0) { |
898 | 0 | if (!(flags & PyCF_ONLY_AST)) { |
899 | 0 | PyErr_SetString(PyExc_ValueError, |
900 | 0 | "compile() mode 'func_type' requires flag PyCF_ONLY_AST"); |
901 | 0 | goto error; |
902 | 0 | } |
903 | 0 | compile_mode = 3; |
904 | 0 | } |
905 | 0 | else { |
906 | 0 | const char *msg; |
907 | 0 | if (flags & PyCF_ONLY_AST) |
908 | 0 | msg = "compile() mode must be 'exec', 'eval', 'single' or 'func_type'"; |
909 | 0 | else |
910 | 0 | msg = "compile() mode must be 'exec', 'eval' or 'single'"; |
911 | 0 | PyErr_SetString(PyExc_ValueError, msg); |
912 | 0 | goto error; |
913 | 0 | } |
914 | | |
915 | 64.4k | is_ast = PyAST_Check(source); |
916 | 64.4k | if (is_ast == -1) |
917 | 0 | goto error; |
918 | 64.4k | if (is_ast) { |
919 | 0 | PyArena *arena = _PyArena_New(); |
920 | 0 | if (arena == NULL) { |
921 | 0 | goto error; |
922 | 0 | } |
923 | | |
924 | 0 | if (flags & PyCF_ONLY_AST) { |
925 | 0 | mod_ty mod = PyAST_obj2mod(source, arena, compile_mode); |
926 | 0 | if (mod == NULL || !_PyAST_Validate(mod)) { |
927 | 0 | _PyArena_Free(arena); |
928 | 0 | goto error; |
929 | 0 | } |
930 | 0 | int syntax_check_only = ((flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */ |
931 | 0 | if (_PyCompile_AstPreprocess(mod, filename, &cf, optimize, arena, |
932 | 0 | syntax_check_only, modname) < 0) |
933 | 0 | { |
934 | 0 | _PyArena_Free(arena); |
935 | 0 | goto error; |
936 | 0 | } |
937 | 0 | result = PyAST_mod2obj(mod); |
938 | 0 | } |
939 | 0 | else { |
940 | 0 | mod_ty mod = PyAST_obj2mod(source, arena, compile_mode); |
941 | 0 | if (mod == NULL || !_PyAST_Validate(mod)) { |
942 | 0 | _PyArena_Free(arena); |
943 | 0 | goto error; |
944 | 0 | } |
945 | 0 | result = (PyObject*)_PyAST_Compile(mod, filename, |
946 | 0 | &cf, optimize, arena, modname); |
947 | 0 | } |
948 | 0 | _PyArena_Free(arena); |
949 | 0 | goto finally; |
950 | 0 | } |
951 | | |
952 | 64.4k | str = _Py_SourceAsString(source, "compile", "string, bytes or AST", &cf, &source_copy); |
953 | 64.4k | if (str == NULL) |
954 | 5 | goto error; |
955 | | |
956 | | #ifdef Py_GIL_DISABLED |
957 | | // Disable immortalization of code constants for explicit |
958 | | // compile() calls to get consistent frozen outputs between the default |
959 | | // and free-threaded builds. |
960 | | _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); |
961 | | tstate->suppress_co_const_immortalization++; |
962 | | #endif |
963 | | |
964 | 64.4k | result = _Py_CompileString(str, filename, |
965 | 64.4k | start[compile_mode], &cf, |
966 | 64.4k | optimize, modname); |
967 | | |
968 | | #ifdef Py_GIL_DISABLED |
969 | | tstate->suppress_co_const_immortalization--; |
970 | | #endif |
971 | | |
972 | 64.4k | Py_XDECREF(source_copy); |
973 | 64.4k | goto finally; |
974 | | |
975 | 5 | error: |
976 | 5 | result = NULL; |
977 | 64.4k | finally: |
978 | 64.4k | return result; |
979 | 5 | } |
980 | | |
981 | | /* AC: cannot convert yet, as needs PEP 457 group support in inspect */ |
982 | | /*[clinic input] |
983 | | dir as builtin_dir |
984 | | |
985 | | object as arg: object = NULL |
986 | | / |
987 | | |
988 | | Return an alphabetized list of the attributes of the object. |
989 | | |
990 | | If called without an argument, return the names in the current scope. |
991 | | Else, return an alphabetized list of names comprising (some of) the |
992 | | attributes of the given object, and of attributes reachable from it. |
993 | | If the object supplies a method named __dir__, it will be used; |
994 | | otherwise the default dir() logic is used and returns: |
995 | | for a module object: the module's attributes. |
996 | | for a class object: its attributes, and recursively the attributes |
997 | | of its bases. |
998 | | for any other object: its attributes, its class's attributes, and |
999 | | recursively the attributes of its class's base classes. |
1000 | | [clinic start generated code]*/ |
1001 | | |
1002 | | static PyObject * |
1003 | | builtin_dir_impl(PyObject *module, PyObject *arg) |
1004 | | /*[clinic end generated code: output=24f2c7a52c1e3b08 input=7fe1b2a5ca9ae355]*/ |
1005 | 5.89k | { |
1006 | 5.89k | return PyObject_Dir(arg); |
1007 | 5.89k | } |
1008 | | |
1009 | | /*[clinic input] |
1010 | | divmod as builtin_divmod |
1011 | | |
1012 | | x: object |
1013 | | y: object |
1014 | | / |
1015 | | |
1016 | | Return the tuple (x//y, x%y). Invariant: div*y + mod == x. |
1017 | | [clinic start generated code]*/ |
1018 | | |
1019 | | static PyObject * |
1020 | | builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y) |
1021 | | /*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/ |
1022 | 536k | { |
1023 | 536k | return PyNumber_Divmod(x, y); |
1024 | 536k | } |
1025 | | |
1026 | | |
1027 | | /*[clinic input] |
1028 | | eval as builtin_eval |
1029 | | |
1030 | | source: object |
1031 | | / |
1032 | | globals: object = None |
1033 | | locals: object = None |
1034 | | |
1035 | | Evaluate the given source in the context of globals and locals. |
1036 | | |
1037 | | The source may be a string representing a Python expression |
1038 | | or a code object as returned by compile(). |
1039 | | The globals must be a dictionary and locals can be any mapping, |
1040 | | defaulting to the current globals and locals. |
1041 | | If only globals is given, locals defaults to it. |
1042 | | [clinic start generated code]*/ |
1043 | | |
1044 | | static PyObject * |
1045 | | builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals, |
1046 | | PyObject *locals) |
1047 | | /*[clinic end generated code: output=0a0824aa70093116 input=7c7bce5299a89062]*/ |
1048 | 192 | { |
1049 | 192 | PyThreadState *tstate = _PyThreadState_GET(); |
1050 | 192 | PyObject *result = NULL, *source_copy; |
1051 | 192 | const char *str; |
1052 | | |
1053 | 192 | if (locals != Py_None && !PyMapping_Check(locals)) { |
1054 | 0 | PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); |
1055 | 0 | return NULL; |
1056 | 0 | } |
1057 | 192 | if (globals != Py_None && !PyAnyDict_Check(globals)) { |
1058 | 0 | PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ? |
1059 | 0 | "globals must be a real dict or a frozendict; " |
1060 | 0 | "try eval(expr, {}, mapping)" |
1061 | 0 | : "globals must be a dict or a frozendict"); |
1062 | 0 | return NULL; |
1063 | 0 | } |
1064 | | |
1065 | 192 | int fromframe = 0; |
1066 | 192 | if (globals != Py_None) { |
1067 | 192 | Py_INCREF(globals); |
1068 | 192 | } |
1069 | 0 | else if (_PyEval_GetFrame() != NULL) { |
1070 | 0 | fromframe = 1; |
1071 | 0 | globals = PyEval_GetGlobals(); |
1072 | 0 | assert(globals != NULL); |
1073 | 0 | Py_INCREF(globals); |
1074 | 0 | } |
1075 | 0 | else { |
1076 | 0 | globals = _PyEval_GetGlobalsFromRunningMain(tstate); |
1077 | 0 | if (globals == NULL) { |
1078 | 0 | if (!_PyErr_Occurred(tstate)) { |
1079 | 0 | PyErr_SetString(PyExc_TypeError, |
1080 | 0 | "eval must be given globals and locals " |
1081 | 0 | "when called without a frame"); |
1082 | 0 | } |
1083 | 0 | return NULL; |
1084 | 0 | } |
1085 | 0 | Py_INCREF(globals); |
1086 | 0 | } |
1087 | | |
1088 | 192 | if (locals != Py_None) { |
1089 | 0 | Py_INCREF(locals); |
1090 | 0 | } |
1091 | 192 | else if (fromframe) { |
1092 | 0 | locals = _PyEval_GetFrameLocals(); |
1093 | 0 | if (locals == NULL) { |
1094 | 0 | assert(PyErr_Occurred()); |
1095 | 0 | Py_DECREF(globals); |
1096 | 0 | return NULL; |
1097 | 0 | } |
1098 | 0 | } |
1099 | 192 | else { |
1100 | 192 | locals = Py_NewRef(globals); |
1101 | 192 | } |
1102 | | |
1103 | 192 | if (_PyEval_EnsureBuiltins(tstate, globals, NULL) < 0) { |
1104 | 0 | goto error; |
1105 | 0 | } |
1106 | | |
1107 | 192 | if (PyCode_Check(source)) { |
1108 | 0 | if (PySys_Audit("exec", "O", source) < 0) { |
1109 | 0 | goto error; |
1110 | 0 | } |
1111 | | |
1112 | 0 | if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { |
1113 | 0 | PyErr_SetString(PyExc_TypeError, |
1114 | 0 | "code object passed to eval() may not contain free variables"); |
1115 | 0 | goto error; |
1116 | 0 | } |
1117 | 0 | result = PyEval_EvalCode(source, globals, locals); |
1118 | 0 | } |
1119 | 192 | else { |
1120 | 192 | PyCompilerFlags cf = _PyCompilerFlags_INIT; |
1121 | 192 | cf.cf_flags = PyCF_SOURCE_IS_UTF8; |
1122 | 192 | str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy); |
1123 | 192 | if (str == NULL) |
1124 | 0 | goto error; |
1125 | | |
1126 | 192 | while (*str == ' ' || *str == '\t') |
1127 | 0 | str++; |
1128 | | |
1129 | 192 | (void)PyEval_MergeCompilerFlags(&cf); |
1130 | | #ifdef Py_GIL_DISABLED |
1131 | | // Don't immortalize code constants for explicit eval() calls |
1132 | | // to avoid memory leaks. |
1133 | | _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); |
1134 | | tstate->suppress_co_const_immortalization++; |
1135 | | #endif |
1136 | 192 | result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); |
1137 | | #ifdef Py_GIL_DISABLED |
1138 | | tstate->suppress_co_const_immortalization--; |
1139 | | #endif |
1140 | 192 | Py_XDECREF(source_copy); |
1141 | 192 | } |
1142 | | |
1143 | 192 | error: |
1144 | 192 | Py_XDECREF(globals); |
1145 | 192 | Py_XDECREF(locals); |
1146 | 192 | return result; |
1147 | 192 | } |
1148 | | |
1149 | | /*[clinic input] |
1150 | | exec as builtin_exec |
1151 | | |
1152 | | source: object |
1153 | | / |
1154 | | globals: object = None |
1155 | | locals: object = None |
1156 | | * |
1157 | | closure: object(c_default="NULL") = None |
1158 | | |
1159 | | Execute the given source in the context of globals and locals. |
1160 | | |
1161 | | The source may be a string representing one or more Python statements |
1162 | | or a code object as returned by compile(). |
1163 | | The globals must be a dictionary and locals can be any mapping, |
1164 | | defaulting to the current globals and locals. |
1165 | | If only globals is given, locals defaults to it. |
1166 | | The closure must be a tuple of cellvars, and can only be used |
1167 | | when source is a code object requiring exactly that many cellvars. |
1168 | | [clinic start generated code]*/ |
1169 | | |
1170 | | static PyObject * |
1171 | | builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals, |
1172 | | PyObject *locals, PyObject *closure) |
1173 | | /*[clinic end generated code: output=7579eb4e7646743d input=25e989b6d87a3a21]*/ |
1174 | 6.37k | { |
1175 | 6.37k | PyThreadState *tstate = _PyThreadState_GET(); |
1176 | 6.37k | PyObject *v; |
1177 | | |
1178 | 6.37k | int fromframe = 0; |
1179 | 6.37k | if (globals != Py_None) { |
1180 | 6.37k | Py_INCREF(globals); |
1181 | 6.37k | } |
1182 | 0 | else if (_PyEval_GetFrame() != NULL) { |
1183 | 0 | fromframe = 1; |
1184 | 0 | globals = PyEval_GetGlobals(); |
1185 | 0 | assert(globals != NULL); |
1186 | 0 | Py_INCREF(globals); |
1187 | 0 | } |
1188 | 0 | else { |
1189 | 0 | globals = _PyEval_GetGlobalsFromRunningMain(tstate); |
1190 | 0 | if (globals == NULL) { |
1191 | 0 | if (!_PyErr_Occurred(tstate)) { |
1192 | 0 | PyErr_SetString(PyExc_SystemError, |
1193 | 0 | "globals and locals cannot be NULL"); |
1194 | 0 | } |
1195 | 0 | goto error; |
1196 | 0 | } |
1197 | 0 | Py_INCREF(globals); |
1198 | 0 | } |
1199 | | |
1200 | 6.37k | if (locals != Py_None) { |
1201 | 103 | Py_INCREF(locals); |
1202 | 103 | } |
1203 | 6.26k | else if (fromframe) { |
1204 | 0 | locals = _PyEval_GetFrameLocals(); |
1205 | 0 | if (locals == NULL) { |
1206 | 0 | assert(PyErr_Occurred()); |
1207 | 0 | Py_DECREF(globals); |
1208 | 0 | return NULL; |
1209 | 0 | } |
1210 | 0 | } |
1211 | 6.26k | else { |
1212 | 6.26k | locals = Py_NewRef(globals); |
1213 | 6.26k | } |
1214 | | |
1215 | 6.37k | if (!PyAnyDict_Check(globals)) { |
1216 | 0 | PyErr_Format(PyExc_TypeError, |
1217 | 0 | "exec() globals must be a dict or a frozendict, not %T", |
1218 | 0 | globals); |
1219 | 0 | goto error; |
1220 | 0 | } |
1221 | 6.37k | if (!PyMapping_Check(locals)) { |
1222 | 0 | PyErr_Format(PyExc_TypeError, |
1223 | 0 | "locals must be a mapping or None, not %.100s", |
1224 | 0 | Py_TYPE(locals)->tp_name); |
1225 | 0 | goto error; |
1226 | 0 | } |
1227 | | |
1228 | 6.37k | if (_PyEval_EnsureBuiltins(tstate, globals, NULL) < 0) { |
1229 | 0 | goto error; |
1230 | 0 | } |
1231 | | |
1232 | 6.37k | if (closure == Py_None) { |
1233 | 0 | closure = NULL; |
1234 | 0 | } |
1235 | | |
1236 | 6.37k | if (PyCode_Check(source)) { |
1237 | 6.09k | Py_ssize_t num_free = PyCode_GetNumFree((PyCodeObject *)source); |
1238 | 6.09k | if (num_free == 0) { |
1239 | 6.09k | if (closure) { |
1240 | 0 | PyErr_SetString(PyExc_TypeError, |
1241 | 0 | "cannot use a closure with this code object"); |
1242 | 0 | goto error; |
1243 | 0 | } |
1244 | 6.09k | } else { |
1245 | 0 | int closure_is_ok = |
1246 | 0 | closure |
1247 | 0 | && PyTuple_CheckExact(closure) |
1248 | 0 | && (PyTuple_GET_SIZE(closure) == num_free); |
1249 | 0 | if (closure_is_ok) { |
1250 | 0 | for (Py_ssize_t i = 0; i < num_free; i++) { |
1251 | 0 | PyObject *cell = PyTuple_GET_ITEM(closure, i); |
1252 | 0 | if (!PyCell_Check(cell)) { |
1253 | 0 | closure_is_ok = 0; |
1254 | 0 | break; |
1255 | 0 | } |
1256 | 0 | } |
1257 | 0 | } |
1258 | 0 | if (!closure_is_ok) { |
1259 | 0 | PyErr_Format(PyExc_TypeError, |
1260 | 0 | "code object requires a closure of exactly length %zd", |
1261 | 0 | num_free); |
1262 | 0 | goto error; |
1263 | 0 | } |
1264 | 0 | } |
1265 | | |
1266 | 6.09k | if (PySys_Audit("exec", "O", source) < 0) { |
1267 | 0 | goto error; |
1268 | 0 | } |
1269 | | |
1270 | 6.09k | if (!closure) { |
1271 | 6.09k | v = PyEval_EvalCode(source, globals, locals); |
1272 | 6.09k | } else { |
1273 | 0 | v = PyEval_EvalCodeEx(source, globals, locals, |
1274 | 0 | NULL, 0, |
1275 | 0 | NULL, 0, |
1276 | 0 | NULL, 0, |
1277 | 0 | NULL, |
1278 | 0 | closure); |
1279 | 0 | } |
1280 | 6.09k | } |
1281 | 281 | else { |
1282 | 281 | if (closure != NULL) { |
1283 | 0 | PyErr_SetString(PyExc_TypeError, |
1284 | 0 | "closure can only be used when source is a code object"); |
1285 | 0 | goto error; |
1286 | 0 | } |
1287 | 281 | PyObject *source_copy; |
1288 | 281 | const char *str; |
1289 | 281 | PyCompilerFlags cf = _PyCompilerFlags_INIT; |
1290 | 281 | cf.cf_flags = PyCF_SOURCE_IS_UTF8; |
1291 | 281 | str = _Py_SourceAsString(source, "exec", |
1292 | 281 | "string, bytes or code", &cf, |
1293 | 281 | &source_copy); |
1294 | 281 | if (str == NULL) |
1295 | 0 | goto error; |
1296 | 281 | if (PyEval_MergeCompilerFlags(&cf)) |
1297 | 281 | v = PyRun_StringFlags(str, Py_file_input, globals, |
1298 | 281 | locals, &cf); |
1299 | 0 | else |
1300 | 0 | v = PyRun_String(str, Py_file_input, globals, locals); |
1301 | 281 | Py_XDECREF(source_copy); |
1302 | 281 | } |
1303 | 6.37k | if (v == NULL) |
1304 | 3.64k | goto error; |
1305 | 2.73k | Py_DECREF(globals); |
1306 | 2.73k | Py_DECREF(locals); |
1307 | 2.73k | Py_DECREF(v); |
1308 | 2.73k | Py_RETURN_NONE; |
1309 | | |
1310 | 3.64k | error: |
1311 | 3.64k | Py_XDECREF(globals); |
1312 | 3.64k | Py_XDECREF(locals); |
1313 | 3.64k | return NULL; |
1314 | 6.37k | } |
1315 | | |
1316 | | |
1317 | | /* AC: cannot convert yet, as needs PEP 457 group support in inspect */ |
1318 | | static PyObject * |
1319 | | builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1320 | 12.6M | { |
1321 | 12.6M | PyObject *v, *name, *result; |
1322 | | |
1323 | 12.6M | if (!_PyArg_CheckPositional("getattr", nargs, 2, 3)) |
1324 | 0 | return NULL; |
1325 | | |
1326 | 12.6M | v = args[0]; |
1327 | 12.6M | name = args[1]; |
1328 | 12.6M | if (nargs > 2) { |
1329 | 6.03M | if (PyObject_GetOptionalAttr(v, name, &result) == 0) { |
1330 | 4.02M | PyObject *dflt = args[2]; |
1331 | 4.02M | return Py_NewRef(dflt); |
1332 | 4.02M | } |
1333 | 6.03M | } |
1334 | 6.63M | else { |
1335 | 6.63M | result = PyObject_GetAttr(v, name); |
1336 | 6.63M | } |
1337 | 8.63M | return result; |
1338 | 12.6M | } |
1339 | | |
1340 | | PyDoc_STRVAR(getattr_doc, |
1341 | | "getattr(object, name[, default]) -> value\n\ |
1342 | | \n\ |
1343 | | Get a named attribute from an object.\n\ |
1344 | | \n\ |
1345 | | getattr(x, 'y') is equivalent to x.y.\n\ |
1346 | | When a default argument is given, it is returned when the attribute\n\ |
1347 | | doesn't exist; without it, an exception is raised in that case."); |
1348 | | |
1349 | | |
1350 | | /*[clinic input] |
1351 | | globals as builtin_globals |
1352 | | |
1353 | | Return the dictionary containing the current scope's global variables. |
1354 | | |
1355 | | NOTE: Updates to this dictionary *will* affect name lookups in the |
1356 | | current global scope and vice-versa. |
1357 | | [clinic start generated code]*/ |
1358 | | |
1359 | | static PyObject * |
1360 | | builtin_globals_impl(PyObject *module) |
1361 | | /*[clinic end generated code: output=e5dd1527067b94d2 input=6d725a9b48d1eaeb]*/ |
1362 | 974 | { |
1363 | 974 | PyObject *globals; |
1364 | 974 | if (_PyEval_GetFrame() != NULL) { |
1365 | 974 | globals = PyEval_GetGlobals(); |
1366 | 974 | assert(globals != NULL); |
1367 | 974 | return Py_NewRef(globals); |
1368 | 974 | } |
1369 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
1370 | 0 | globals = _PyEval_GetGlobalsFromRunningMain(tstate); |
1371 | 0 | if (globals == NULL) { |
1372 | 0 | if (_PyErr_Occurred(tstate)) { |
1373 | 0 | return NULL; |
1374 | 0 | } |
1375 | 0 | Py_RETURN_NONE; |
1376 | 0 | } |
1377 | 0 | return Py_NewRef(globals); |
1378 | 0 | } |
1379 | | |
1380 | | |
1381 | | /*[clinic input] |
1382 | | hasattr as builtin_hasattr |
1383 | | |
1384 | | obj: object |
1385 | | name: object |
1386 | | / |
1387 | | |
1388 | | Return whether the object has an attribute with the given name. |
1389 | | |
1390 | | This is done by calling getattr(obj, name) and catching AttributeError. |
1391 | | [clinic start generated code]*/ |
1392 | | |
1393 | | static PyObject * |
1394 | | builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name) |
1395 | | /*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/ |
1396 | 9.11M | { |
1397 | 9.11M | PyObject *v; |
1398 | | |
1399 | 9.11M | if (PyObject_GetOptionalAttr(obj, name, &v) < 0) { |
1400 | 0 | return NULL; |
1401 | 0 | } |
1402 | 9.11M | if (v == NULL) { |
1403 | 236k | Py_RETURN_FALSE; |
1404 | 236k | } |
1405 | 8.88M | Py_DECREF(v); |
1406 | 8.88M | Py_RETURN_TRUE; |
1407 | 9.11M | } |
1408 | | |
1409 | | |
1410 | | /* AC: gdb's integration with CPython relies on builtin_id having |
1411 | | * the *exact* parameter names of "self" and "v", so we ensure we |
1412 | | * preserve those name rather than using the AC defaults. |
1413 | | */ |
1414 | | /*[clinic input] |
1415 | | id as builtin_id |
1416 | | |
1417 | | self: self(type="PyModuleDef *") |
1418 | | obj as v: object |
1419 | | / |
1420 | | |
1421 | | Return the identity of an object. |
1422 | | |
1423 | | This is guaranteed to be unique among simultaneously existing objects. |
1424 | | (CPython uses the object's memory address.) |
1425 | | [clinic start generated code]*/ |
1426 | | |
1427 | | static PyObject * |
1428 | | builtin_id_impl(PyModuleDef *self, PyObject *v) |
1429 | | /*[clinic end generated code: output=4908a6782ed343e9 input=5a534136419631f4]*/ |
1430 | 19.2k | { |
1431 | 19.2k | PyObject *id = PyLong_FromVoidPtr(v); |
1432 | | |
1433 | 19.2k | if (id && PySys_Audit("builtins.id", "O", id) < 0) { |
1434 | 0 | Py_DECREF(id); |
1435 | 0 | return NULL; |
1436 | 0 | } |
1437 | | |
1438 | 19.2k | return id; |
1439 | 19.2k | } |
1440 | | |
1441 | | |
1442 | | /* map object ************************************************************/ |
1443 | | |
1444 | | typedef struct { |
1445 | | PyObject_HEAD |
1446 | | PyObject *iters; |
1447 | | PyObject *func; |
1448 | | int strict; |
1449 | | } mapobject; |
1450 | | |
1451 | 75.2M | #define _mapobject_CAST(op) ((mapobject *)(op)) |
1452 | | |
1453 | | static PyObject * |
1454 | | map_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
1455 | 0 | { |
1456 | 0 | PyObject *it, *iters, *func; |
1457 | 0 | mapobject *lz; |
1458 | 0 | Py_ssize_t numargs, i; |
1459 | 0 | int strict = 0; |
1460 | |
|
1461 | 0 | if (kwds) { |
1462 | 0 | PyObject *empty = PyTuple_New(0); |
1463 | 0 | if (empty == NULL) { |
1464 | 0 | return NULL; |
1465 | 0 | } |
1466 | 0 | static char *kwlist[] = {"strict", NULL}; |
1467 | 0 | int parsed = PyArg_ParseTupleAndKeywords( |
1468 | 0 | empty, kwds, "|$p:map", kwlist, &strict); |
1469 | 0 | Py_DECREF(empty); |
1470 | 0 | if (!parsed) { |
1471 | 0 | return NULL; |
1472 | 0 | } |
1473 | 0 | } |
1474 | | |
1475 | 0 | numargs = PyTuple_Size(args); |
1476 | 0 | if (numargs < 2) { |
1477 | 0 | PyErr_SetString(PyExc_TypeError, |
1478 | 0 | "map() must have at least two arguments."); |
1479 | 0 | return NULL; |
1480 | 0 | } |
1481 | | |
1482 | 0 | iters = PyTuple_New(numargs-1); |
1483 | 0 | if (iters == NULL) |
1484 | 0 | return NULL; |
1485 | | |
1486 | 0 | for (i=1 ; i<numargs ; i++) { |
1487 | | /* Get iterator. */ |
1488 | 0 | it = PyObject_GetIter(PyTuple_GET_ITEM(args, i)); |
1489 | 0 | if (it == NULL) { |
1490 | 0 | Py_DECREF(iters); |
1491 | 0 | return NULL; |
1492 | 0 | } |
1493 | 0 | PyTuple_SET_ITEM(iters, i-1, it); |
1494 | 0 | } |
1495 | | |
1496 | | /* create mapobject structure */ |
1497 | 0 | lz = (mapobject *)type->tp_alloc(type, 0); |
1498 | 0 | if (lz == NULL) { |
1499 | 0 | Py_DECREF(iters); |
1500 | 0 | return NULL; |
1501 | 0 | } |
1502 | 0 | lz->iters = iters; |
1503 | 0 | func = PyTuple_GET_ITEM(args, 0); |
1504 | 0 | lz->func = Py_NewRef(func); |
1505 | 0 | lz->strict = strict; |
1506 | |
|
1507 | 0 | return (PyObject *)lz; |
1508 | 0 | } |
1509 | | |
1510 | | static PyObject * |
1511 | | map_vectorcall(PyObject *type, PyObject * const*args, |
1512 | | size_t nargsf, PyObject *kwnames) |
1513 | 115k | { |
1514 | 115k | PyTypeObject *tp = _PyType_CAST(type); |
1515 | | |
1516 | 115k | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
1517 | 115k | if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) != 0) { |
1518 | | // Fallback to map_new() |
1519 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
1520 | 0 | return _PyObject_MakeTpCall(tstate, type, args, nargs, kwnames); |
1521 | 0 | } |
1522 | | |
1523 | 115k | if (nargs < 2) { |
1524 | 0 | PyErr_SetString(PyExc_TypeError, |
1525 | 0 | "map() must have at least two arguments."); |
1526 | 0 | return NULL; |
1527 | 0 | } |
1528 | | |
1529 | 115k | PyObject *iters = PyTuple_New(nargs-1); |
1530 | 115k | if (iters == NULL) { |
1531 | 0 | return NULL; |
1532 | 0 | } |
1533 | | |
1534 | 231k | for (int i=1; i<nargs; i++) { |
1535 | 115k | PyObject *it = PyObject_GetIter(args[i]); |
1536 | 115k | if (it == NULL) { |
1537 | 0 | Py_DECREF(iters); |
1538 | 0 | return NULL; |
1539 | 0 | } |
1540 | 115k | PyTuple_SET_ITEM(iters, i-1, it); |
1541 | 115k | } |
1542 | | |
1543 | 115k | mapobject *lz = (mapobject *)tp->tp_alloc(tp, 0); |
1544 | 115k | if (lz == NULL) { |
1545 | 0 | Py_DECREF(iters); |
1546 | 0 | return NULL; |
1547 | 0 | } |
1548 | 115k | lz->iters = iters; |
1549 | 115k | lz->func = Py_NewRef(args[0]); |
1550 | 115k | lz->strict = 0; |
1551 | | |
1552 | 115k | return (PyObject *)lz; |
1553 | 115k | } |
1554 | | |
1555 | | static void |
1556 | | map_dealloc(PyObject *self) |
1557 | 115k | { |
1558 | 115k | mapobject *lz = _mapobject_CAST(self); |
1559 | 115k | PyObject_GC_UnTrack(lz); |
1560 | 115k | Py_XDECREF(lz->iters); |
1561 | 115k | Py_XDECREF(lz->func); |
1562 | 115k | Py_TYPE(lz)->tp_free(lz); |
1563 | 115k | } |
1564 | | |
1565 | | static int |
1566 | | map_traverse(PyObject *self, visitproc visit, void *arg) |
1567 | 1.84k | { |
1568 | 1.84k | mapobject *lz = _mapobject_CAST(self); |
1569 | 1.84k | Py_VISIT(lz->iters); |
1570 | 1.84k | Py_VISIT(lz->func); |
1571 | 1.84k | return 0; |
1572 | 1.84k | } |
1573 | | |
1574 | | static PyObject * |
1575 | | map_next(PyObject *self) |
1576 | 75.1M | { |
1577 | 75.1M | mapobject *lz = _mapobject_CAST(self); |
1578 | 75.1M | Py_ssize_t i; |
1579 | 75.1M | PyObject *small_stack[_PY_FASTCALL_SMALL_STACK]; |
1580 | 75.1M | PyObject **stack; |
1581 | 75.1M | PyObject *result = NULL; |
1582 | 75.1M | PyThreadState *tstate = _PyThreadState_GET(); |
1583 | | |
1584 | 75.1M | const Py_ssize_t niters = PyTuple_GET_SIZE(lz->iters); |
1585 | 75.1M | if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) { |
1586 | 75.1M | stack = small_stack; |
1587 | 75.1M | } |
1588 | 0 | else { |
1589 | 0 | stack = PyMem_Malloc(niters * sizeof(stack[0])); |
1590 | 0 | if (stack == NULL) { |
1591 | 0 | _PyErr_NoMemory(tstate); |
1592 | 0 | return NULL; |
1593 | 0 | } |
1594 | 0 | } |
1595 | | |
1596 | 75.1M | Py_ssize_t nargs = 0; |
1597 | 150M | for (i = 0; i < niters; i++) { |
1598 | 75.1M | PyObject *it = PyTuple_GET_ITEM(lz->iters, i); |
1599 | 75.1M | PyObject *val = Py_TYPE(it)->tp_iternext(it); |
1600 | 75.1M | if (val == NULL) { |
1601 | 111k | if (lz->strict) { |
1602 | 0 | goto check; |
1603 | 0 | } |
1604 | 111k | goto exit_no_result; |
1605 | 111k | } |
1606 | 75.0M | stack[i] = val; |
1607 | 75.0M | nargs++; |
1608 | 75.0M | } |
1609 | | |
1610 | 75.0M | result = _PyObject_VectorcallTstate(tstate, lz->func, stack, nargs, NULL); |
1611 | 75.0M | goto exit; |
1612 | | |
1613 | 0 | check: |
1614 | 0 | if (PyErr_Occurred()) { |
1615 | 0 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) { |
1616 | | // next() on argument i raised an exception (not StopIteration) |
1617 | 0 | goto exit_no_result; |
1618 | 0 | } |
1619 | 0 | PyErr_Clear(); |
1620 | 0 | } |
1621 | 0 | if (i) { |
1622 | | // ValueError: map() argument 2 is shorter than argument 1 |
1623 | | // ValueError: map() argument 3 is shorter than arguments 1-2 |
1624 | 0 | const char* plural = i == 1 ? " " : "s 1-"; |
1625 | 0 | PyErr_Format(PyExc_ValueError, |
1626 | 0 | "map() argument %zd is shorter than argument%s%zd", |
1627 | 0 | i + 1, plural, i); |
1628 | 0 | goto exit_no_result; |
1629 | 0 | } |
1630 | 0 | for (i = 1; i < niters; i++) { |
1631 | 0 | PyObject *it = PyTuple_GET_ITEM(lz->iters, i); |
1632 | 0 | PyObject *val = (*Py_TYPE(it)->tp_iternext)(it); |
1633 | 0 | if (val) { |
1634 | 0 | Py_DECREF(val); |
1635 | 0 | const char* plural = i == 1 ? " " : "s 1-"; |
1636 | 0 | PyErr_Format(PyExc_ValueError, |
1637 | 0 | "map() argument %zd is longer than argument%s%zd", |
1638 | 0 | i + 1, plural, i); |
1639 | 0 | goto exit_no_result; |
1640 | 0 | } |
1641 | 0 | if (PyErr_Occurred()) { |
1642 | 0 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) { |
1643 | | // next() on argument i raised an exception (not StopIteration) |
1644 | 0 | goto exit_no_result; |
1645 | 0 | } |
1646 | 0 | PyErr_Clear(); |
1647 | 0 | } |
1648 | | // Argument i is exhausted. So far so good... |
1649 | 0 | } |
1650 | | // All arguments are exhausted. Success! |
1651 | | |
1652 | 111k | exit_no_result: |
1653 | 111k | assert(result == NULL); |
1654 | | |
1655 | 75.1M | exit: |
1656 | 150M | for (i = 0; i < nargs; i++) { |
1657 | 75.0M | Py_DECREF(stack[i]); |
1658 | 75.0M | } |
1659 | 75.1M | if (stack != small_stack) { |
1660 | 0 | PyMem_Free(stack); |
1661 | 0 | } |
1662 | 75.1M | return result; |
1663 | 111k | } |
1664 | | |
1665 | | static PyObject * |
1666 | | map_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
1667 | 0 | { |
1668 | 0 | mapobject *lz = _mapobject_CAST(self); |
1669 | 0 | Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters); |
1670 | 0 | PyObject *args = PyTuple_New(numargs+1); |
1671 | 0 | Py_ssize_t i; |
1672 | 0 | if (args == NULL) |
1673 | 0 | return NULL; |
1674 | 0 | PyTuple_SET_ITEM(args, 0, Py_NewRef(lz->func)); |
1675 | 0 | for (i = 0; i<numargs; i++){ |
1676 | 0 | PyObject *it = PyTuple_GET_ITEM(lz->iters, i); |
1677 | 0 | PyTuple_SET_ITEM(args, i+1, Py_NewRef(it)); |
1678 | 0 | } |
1679 | |
|
1680 | 0 | if (lz->strict) { |
1681 | 0 | return Py_BuildValue("ONO", Py_TYPE(lz), args, Py_True); |
1682 | 0 | } |
1683 | 0 | return Py_BuildValue("ON", Py_TYPE(lz), args); |
1684 | 0 | } |
1685 | | |
1686 | | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
1687 | | |
1688 | | static PyObject * |
1689 | | map_setstate(PyObject *self, PyObject *state) |
1690 | 0 | { |
1691 | 0 | int strict = PyObject_IsTrue(state); |
1692 | 0 | if (strict < 0) { |
1693 | 0 | return NULL; |
1694 | 0 | } |
1695 | 0 | mapobject *lz = _mapobject_CAST(self); |
1696 | 0 | lz->strict = strict; |
1697 | 0 | Py_RETURN_NONE; |
1698 | 0 | } |
1699 | | |
1700 | | static PyMethodDef map_methods[] = { |
1701 | | {"__reduce__", map_reduce, METH_NOARGS, reduce_doc}, |
1702 | | {"__setstate__", map_setstate, METH_O, setstate_doc}, |
1703 | | {NULL, NULL} /* sentinel */ |
1704 | | }; |
1705 | | |
1706 | | |
1707 | | PyDoc_STRVAR(map_doc, |
1708 | | "map(function, iterable, /, *iterables, strict=False)\n\ |
1709 | | --\n\ |
1710 | | \n\ |
1711 | | Make an iterator that computes the function using arguments from\n\ |
1712 | | each of the iterables. Stops when the shortest iterable is exhausted.\n\ |
1713 | | \n\ |
1714 | | If strict is true and one of the arguments is exhausted before the\n\ |
1715 | | others, raise a ValueError."); |
1716 | | |
1717 | | PyTypeObject PyMap_Type = { |
1718 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1719 | | "map", /* tp_name */ |
1720 | | sizeof(mapobject), /* tp_basicsize */ |
1721 | | 0, /* tp_itemsize */ |
1722 | | /* methods */ |
1723 | | map_dealloc, /* tp_dealloc */ |
1724 | | 0, /* tp_vectorcall_offset */ |
1725 | | 0, /* tp_getattr */ |
1726 | | 0, /* tp_setattr */ |
1727 | | 0, /* tp_as_async */ |
1728 | | 0, /* tp_repr */ |
1729 | | 0, /* tp_as_number */ |
1730 | | 0, /* tp_as_sequence */ |
1731 | | 0, /* tp_as_mapping */ |
1732 | | 0, /* tp_hash */ |
1733 | | 0, /* tp_call */ |
1734 | | 0, /* tp_str */ |
1735 | | PyObject_GenericGetAttr, /* tp_getattro */ |
1736 | | 0, /* tp_setattro */ |
1737 | | 0, /* tp_as_buffer */ |
1738 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
1739 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
1740 | | map_doc, /* tp_doc */ |
1741 | | map_traverse, /* tp_traverse */ |
1742 | | 0, /* tp_clear */ |
1743 | | 0, /* tp_richcompare */ |
1744 | | 0, /* tp_weaklistoffset */ |
1745 | | PyObject_SelfIter, /* tp_iter */ |
1746 | | map_next, /* tp_iternext */ |
1747 | | map_methods, /* tp_methods */ |
1748 | | 0, /* tp_members */ |
1749 | | 0, /* tp_getset */ |
1750 | | 0, /* tp_base */ |
1751 | | 0, /* tp_dict */ |
1752 | | 0, /* tp_descr_get */ |
1753 | | 0, /* tp_descr_set */ |
1754 | | 0, /* tp_dictoffset */ |
1755 | | 0, /* tp_init */ |
1756 | | PyType_GenericAlloc, /* tp_alloc */ |
1757 | | map_new, /* tp_new */ |
1758 | | PyObject_GC_Del, /* tp_free */ |
1759 | | .tp_vectorcall = map_vectorcall |
1760 | | }; |
1761 | | |
1762 | | |
1763 | | /* AC: cannot convert yet, as needs PEP 457 group support in inspect */ |
1764 | | static PyObject * |
1765 | | builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1766 | 2.83M | { |
1767 | 2.83M | PyObject *it, *res; |
1768 | | |
1769 | 2.83M | if (!_PyArg_CheckPositional("next", nargs, 1, 2)) |
1770 | 0 | return NULL; |
1771 | | |
1772 | 2.83M | it = args[0]; |
1773 | 2.83M | if (!PyIter_Check(it)) { |
1774 | 0 | PyErr_Format(PyExc_TypeError, |
1775 | 0 | "'%.200s' object is not an iterator", |
1776 | 0 | Py_TYPE(it)->tp_name); |
1777 | 0 | return NULL; |
1778 | 0 | } |
1779 | | |
1780 | 2.83M | res = (*Py_TYPE(it)->tp_iternext)(it); |
1781 | 2.83M | if (res != NULL) { |
1782 | 2.77M | return res; |
1783 | 2.77M | } else if (nargs > 1) { |
1784 | 46.3k | PyObject *def = args[1]; |
1785 | 46.3k | if (PyErr_Occurred()) { |
1786 | 0 | if(!PyErr_ExceptionMatches(PyExc_StopIteration)) |
1787 | 0 | return NULL; |
1788 | 0 | PyErr_Clear(); |
1789 | 0 | } |
1790 | 46.3k | return Py_NewRef(def); |
1791 | 46.3k | } else if (PyErr_Occurred()) { |
1792 | 0 | return NULL; |
1793 | 13.2k | } else { |
1794 | 13.2k | PyErr_SetNone(PyExc_StopIteration); |
1795 | 13.2k | return NULL; |
1796 | 13.2k | } |
1797 | 2.83M | } |
1798 | | |
1799 | | PyDoc_STRVAR(next_doc, |
1800 | | "next(iterator[, default])\n\ |
1801 | | \n\ |
1802 | | Return the next item from the iterator. If default is given and the\n\ |
1803 | | iterator is exhausted, it is returned instead of raising StopIteration."); |
1804 | | |
1805 | | |
1806 | | /*[clinic input] |
1807 | | setattr as builtin_setattr |
1808 | | |
1809 | | obj: object |
1810 | | name: object |
1811 | | value: object |
1812 | | / |
1813 | | |
1814 | | Sets the named attribute on the given object to the specified value. |
1815 | | |
1816 | | setattr(x, 'y', v) is equivalent to ``x.y = v`` |
1817 | | [clinic start generated code]*/ |
1818 | | |
1819 | | static PyObject * |
1820 | | builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name, |
1821 | | PyObject *value) |
1822 | | /*[clinic end generated code: output=dc2ce1d1add9acb4 input=5e26417f2e8598d4]*/ |
1823 | 851k | { |
1824 | 851k | if (PyObject_SetAttr(obj, name, value) != 0) |
1825 | 0 | return NULL; |
1826 | 851k | Py_RETURN_NONE; |
1827 | 851k | } |
1828 | | |
1829 | | |
1830 | | /*[clinic input] |
1831 | | delattr as builtin_delattr |
1832 | | |
1833 | | obj: object |
1834 | | name: object |
1835 | | / |
1836 | | |
1837 | | Deletes the named attribute from the given object. |
1838 | | |
1839 | | delattr(x, 'y') is equivalent to ``del x.y`` |
1840 | | [clinic start generated code]*/ |
1841 | | |
1842 | | static PyObject * |
1843 | | builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name) |
1844 | | /*[clinic end generated code: output=85134bc58dff79fa input=164865623abe7216]*/ |
1845 | 1.73k | { |
1846 | 1.73k | if (PyObject_DelAttr(obj, name) < 0) { |
1847 | 0 | return NULL; |
1848 | 0 | } |
1849 | 1.73k | Py_RETURN_NONE; |
1850 | 1.73k | } |
1851 | | |
1852 | | |
1853 | | /*[clinic input] |
1854 | | hash as builtin_hash |
1855 | | |
1856 | | obj: object |
1857 | | / |
1858 | | |
1859 | | Return the integer hash value for the given object. |
1860 | | |
1861 | | Two objects that compare equal must also have the same hash value, but |
1862 | | the reverse is not necessarily true. Hash values may differ between |
1863 | | Python processes. Not all objects are hashable; calling hash() on an |
1864 | | unhashable object raises TypeError. |
1865 | | [clinic start generated code]*/ |
1866 | | |
1867 | | static PyObject * |
1868 | | builtin_hash(PyObject *module, PyObject *obj) |
1869 | | /*[clinic end generated code: output=237668e9d7688db7 input=70a242ff65f6717c]*/ |
1870 | 228k | { |
1871 | 228k | Py_hash_t x; |
1872 | | |
1873 | 228k | x = PyObject_Hash(obj); |
1874 | 228k | if (x == -1) |
1875 | 0 | return NULL; |
1876 | 228k | return PyLong_FromSsize_t(x); |
1877 | 228k | } |
1878 | | |
1879 | | |
1880 | | /*[clinic input] |
1881 | | hex as builtin_hex |
1882 | | |
1883 | | integer: object |
1884 | | / |
1885 | | |
1886 | | Return the hexadecimal representation of an integer. |
1887 | | |
1888 | | >>> hex(12648430) |
1889 | | '0xc0ffee' |
1890 | | [clinic start generated code]*/ |
1891 | | |
1892 | | static PyObject * |
1893 | | builtin_hex(PyObject *module, PyObject *integer) |
1894 | | /*[clinic end generated code: output=e5de857ba61aae08 input=3bef4746efc62fac]*/ |
1895 | 262 | { |
1896 | 262 | return PyNumber_ToBase(integer, 16); |
1897 | 262 | } |
1898 | | |
1899 | | |
1900 | | /*[clinic input] |
1901 | | @text_signature "($module, object, /, [stop_value], *, stop_exception=StopIteration)" |
1902 | | iter as builtin_iter |
1903 | | |
1904 | | object: object |
1905 | | / |
1906 | | stop_value: object = NULL |
1907 | | * |
1908 | | stop_exception: object = NULL |
1909 | | |
1910 | | Get an iterator from an object. |
1911 | | |
1912 | | In the first form, the argument must supply its own iterator, or be a |
1913 | | sequence. In the second form, the callable is called until it returns |
1914 | | the stop value or raises the specified exception. |
1915 | | [clinic start generated code]*/ |
1916 | | |
1917 | | static PyObject * |
1918 | | builtin_iter_impl(PyObject *module, PyObject *object, PyObject *stop_value, |
1919 | | PyObject *stop_exception) |
1920 | | /*[clinic end generated code: output=eb9c9ae8f77bf400 input=d3a2f767f29d9ae6]*/ |
1921 | 75.8k | { |
1922 | 75.8k | if (stop_value == NULL && stop_exception == NULL) { |
1923 | 75.8k | return PyObject_GetIter(object); |
1924 | 75.8k | } |
1925 | 4 | if (!PyCallable_Check(object)) { |
1926 | 0 | PyErr_SetString(PyExc_TypeError, |
1927 | 0 | "iter(): the first argument must be callable"); |
1928 | 0 | return NULL; |
1929 | 0 | } |
1930 | 4 | return _PyCallIter_NewEx(object, stop_value, stop_exception); |
1931 | 4 | } |
1932 | | |
1933 | | |
1934 | | /*[clinic input] |
1935 | | @text_signature "($module, object, /, [stop_value], *, stop_exception=StopAsyncIteration)" |
1936 | | aiter as builtin_aiter |
1937 | | |
1938 | | object: object |
1939 | | / |
1940 | | stop_value: object = NULL |
1941 | | * |
1942 | | stop_exception: object = NULL |
1943 | | |
1944 | | Return an AsyncIterator for an AsyncIterable object. |
1945 | | |
1946 | | In the second form, the callable is called and its result is awaited |
1947 | | until it returns the stop value or raises the specified exception. |
1948 | | [clinic start generated code]*/ |
1949 | | |
1950 | | static PyObject * |
1951 | | builtin_aiter_impl(PyObject *module, PyObject *object, PyObject *stop_value, |
1952 | | PyObject *stop_exception) |
1953 | | /*[clinic end generated code: output=2865edb3fbc45693 input=2adb37d12adafd0c]*/ |
1954 | 0 | { |
1955 | 0 | if (stop_value == NULL && stop_exception == NULL) { |
1956 | 0 | return PyObject_GetAIter(object); |
1957 | 0 | } |
1958 | 0 | if (!PyCallable_Check(object)) { |
1959 | 0 | PyErr_SetString(PyExc_TypeError, |
1960 | 0 | "aiter(): the first argument must be callable"); |
1961 | 0 | return NULL; |
1962 | 0 | } |
1963 | 0 | return _PyACallIter_New(object, stop_value, stop_exception); |
1964 | 0 | } |
1965 | | |
1966 | | /*[clinic input] |
1967 | | len as builtin_len |
1968 | | |
1969 | | obj: object |
1970 | | / |
1971 | | |
1972 | | Return the number of items in a container. |
1973 | | [clinic start generated code]*/ |
1974 | | |
1975 | | static PyObject * |
1976 | | builtin_len(PyObject *module, PyObject *obj) |
1977 | | /*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/ |
1978 | 2.43k | { |
1979 | 2.43k | Py_ssize_t res; |
1980 | | |
1981 | 2.43k | res = PyObject_Size(obj); |
1982 | 2.43k | if (res < 0) { |
1983 | 0 | assert(PyErr_Occurred()); |
1984 | 0 | return NULL; |
1985 | 0 | } |
1986 | 2.43k | return PyLong_FromSsize_t(res); |
1987 | 2.43k | } |
1988 | | |
1989 | | |
1990 | | /*[clinic input] |
1991 | | locals as builtin_locals |
1992 | | |
1993 | | Return a dictionary containing the current scope's local variables. |
1994 | | |
1995 | | NOTE: Whether or not updates to this dictionary will affect name |
1996 | | lookups in the local scope and vice-versa is *implementation |
1997 | | dependent* and not covered by any backwards compatibility |
1998 | | guarantees. |
1999 | | [clinic start generated code]*/ |
2000 | | |
2001 | | static PyObject * |
2002 | | builtin_locals_impl(PyObject *module) |
2003 | | /*[clinic end generated code: output=b46c94015ce11448 input=989cc75c22167c42]*/ |
2004 | 8 | { |
2005 | 8 | PyObject *locals; |
2006 | 8 | if (_PyEval_GetFrame() != NULL) { |
2007 | 8 | locals = _PyEval_GetFrameLocals(); |
2008 | 8 | assert(locals != NULL || PyErr_Occurred()); |
2009 | 8 | return locals; |
2010 | 8 | } |
2011 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2012 | 0 | locals = _PyEval_GetGlobalsFromRunningMain(tstate); |
2013 | 0 | if (locals == NULL) { |
2014 | 0 | if (_PyErr_Occurred(tstate)) { |
2015 | 0 | return NULL; |
2016 | 0 | } |
2017 | 0 | Py_RETURN_NONE; |
2018 | 0 | } |
2019 | 0 | return Py_NewRef(locals); |
2020 | 0 | } |
2021 | | |
2022 | | |
2023 | | static PyObject * |
2024 | | min_max(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, int op) |
2025 | 15.3M | { |
2026 | 15.3M | PyObject *it = NULL, *item, *val, *maxitem, *maxval, *keyfunc=NULL; |
2027 | 15.3M | PyObject *defaultval = NULL; |
2028 | 15.3M | static const char * const keywords[] = {"key", "default", NULL}; |
2029 | 15.3M | static _PyArg_Parser _parser_min = {"|$OO:min", keywords, 0}; |
2030 | 15.3M | static _PyArg_Parser _parser_max = {"|$OO:max", keywords, 0}; |
2031 | 15.3M | const char *name = (op == Py_LT) ? "min" : "max"; |
2032 | 15.3M | _PyArg_Parser *_parser = (op == Py_LT) ? &_parser_min : &_parser_max; |
2033 | | |
2034 | 15.3M | if (nargs == 0) { |
2035 | 0 | PyErr_Format(PyExc_TypeError, "%s expected at least 1 argument, got 0", name); |
2036 | 0 | return NULL; |
2037 | 0 | } |
2038 | | |
2039 | 15.3M | if (kwnames != NULL && !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, _parser, |
2040 | 13.7k | &keyfunc, &defaultval)) { |
2041 | 0 | return NULL; |
2042 | 0 | } |
2043 | | |
2044 | 15.3M | const int positional = nargs > 1; // False iff nargs == 1 |
2045 | 15.3M | if (positional && defaultval != NULL) { |
2046 | 0 | PyErr_Format(PyExc_TypeError, |
2047 | 0 | "Cannot specify a default for %s() with multiple " |
2048 | 0 | "positional arguments", name); |
2049 | 0 | return NULL; |
2050 | 0 | } |
2051 | | |
2052 | 15.3M | if (!positional) { |
2053 | 44.7k | it = PyObject_GetIter(args[0]); |
2054 | 44.7k | if (it == NULL) { |
2055 | 0 | return NULL; |
2056 | 0 | } |
2057 | 44.7k | } |
2058 | | |
2059 | 15.3M | if (keyfunc == Py_None) { |
2060 | 0 | keyfunc = NULL; |
2061 | 0 | } |
2062 | | |
2063 | 15.3M | maxitem = NULL; /* the result */ |
2064 | 15.3M | maxval = NULL; /* the value associated with the result */ |
2065 | 46.9M | while (1) { |
2066 | 46.9M | if (it == NULL) { |
2067 | 45.9M | if (nargs-- <= 0) { |
2068 | 15.3M | break; |
2069 | 15.3M | } |
2070 | 30.6M | item = *args++; |
2071 | 30.6M | Py_INCREF(item); |
2072 | 30.6M | } |
2073 | 1.01M | else { |
2074 | 1.01M | item = PyIter_Next(it); |
2075 | 1.01M | if (item == NULL) { |
2076 | 44.7k | if (PyErr_Occurred()) { |
2077 | 0 | goto Fail_it; |
2078 | 0 | } |
2079 | 44.7k | break; |
2080 | 44.7k | } |
2081 | 1.01M | } |
2082 | | |
2083 | | /* get the value from the key function */ |
2084 | 31.6M | if (keyfunc != NULL) { |
2085 | 26.0k | val = PyObject_CallOneArg(keyfunc, item); |
2086 | 26.0k | if (val == NULL) |
2087 | 0 | goto Fail_it_item; |
2088 | 26.0k | } |
2089 | | /* no key function; the value is the item */ |
2090 | 31.5M | else { |
2091 | 31.5M | val = Py_NewRef(item); |
2092 | 31.5M | } |
2093 | | |
2094 | | /* maximum value and item are unset; set them */ |
2095 | 31.6M | if (maxval == NULL) { |
2096 | 15.3M | maxitem = item; |
2097 | 15.3M | maxval = val; |
2098 | 15.3M | } |
2099 | | /* maximum value and item are set; update them as necessary */ |
2100 | 16.2M | else { |
2101 | 16.2M | int cmp = PyObject_RichCompareBool(val, maxval, op); |
2102 | 16.2M | if (cmp < 0) |
2103 | 0 | goto Fail_it_item_and_val; |
2104 | 16.2M | else if (cmp > 0) { |
2105 | 3.32M | Py_DECREF(maxval); |
2106 | 3.32M | Py_DECREF(maxitem); |
2107 | 3.32M | maxval = val; |
2108 | 3.32M | maxitem = item; |
2109 | 3.32M | } |
2110 | 12.9M | else { |
2111 | 12.9M | Py_DECREF(item); |
2112 | 12.9M | Py_DECREF(val); |
2113 | 12.9M | } |
2114 | 16.2M | } |
2115 | 31.6M | } |
2116 | 15.3M | if (maxval == NULL) { |
2117 | 0 | assert(maxitem == NULL); |
2118 | 0 | if (defaultval != NULL) { |
2119 | 0 | maxitem = Py_NewRef(defaultval); |
2120 | 0 | } else { |
2121 | 0 | PyErr_Format(PyExc_ValueError, |
2122 | 0 | "%s() iterable argument is empty", name); |
2123 | 0 | } |
2124 | 0 | } |
2125 | 15.3M | else |
2126 | 15.3M | Py_DECREF(maxval); |
2127 | 15.3M | Py_XDECREF(it); |
2128 | 15.3M | return maxitem; |
2129 | | |
2130 | 0 | Fail_it_item_and_val: |
2131 | 0 | Py_DECREF(val); |
2132 | 0 | Fail_it_item: |
2133 | 0 | Py_DECREF(item); |
2134 | 0 | Fail_it: |
2135 | 0 | Py_XDECREF(maxval); |
2136 | 0 | Py_XDECREF(maxitem); |
2137 | 0 | Py_XDECREF(it); |
2138 | 0 | return NULL; |
2139 | 0 | } |
2140 | | |
2141 | | /* AC: cannot convert yet, waiting for *args support */ |
2142 | | static PyObject * |
2143 | | builtin_min(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
2144 | 8.80M | { |
2145 | 8.80M | return min_max(args, nargs, kwnames, Py_LT); |
2146 | 8.80M | } |
2147 | | |
2148 | | PyDoc_STRVAR(min_doc, |
2149 | | "min(iterable, *[, default=obj, key=func]) -> value\n\ |
2150 | | min(arg1, arg2, *args, *[, key=func]) -> value\n\ |
2151 | | \n\ |
2152 | | With a single iterable argument, return its smallest item. The\n\ |
2153 | | default keyword-only argument specifies an object to return if\n\ |
2154 | | the provided iterable is empty.\n\ |
2155 | | With two or more positional arguments, return the smallest argument."); |
2156 | | |
2157 | | |
2158 | | /* AC: cannot convert yet, waiting for *args support */ |
2159 | | static PyObject * |
2160 | | builtin_max(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
2161 | 6.56M | { |
2162 | 6.56M | return min_max(args, nargs, kwnames, Py_GT); |
2163 | 6.56M | } |
2164 | | |
2165 | | PyDoc_STRVAR(max_doc, |
2166 | | "max(iterable, *[, default=obj, key=func]) -> value\n\ |
2167 | | max(arg1, arg2, *args, *[, key=func]) -> value\n\ |
2168 | | \n\ |
2169 | | With a single iterable argument, return its biggest item. The\n\ |
2170 | | default keyword-only argument specifies an object to return if\n\ |
2171 | | the provided iterable is empty.\n\ |
2172 | | With two or more positional arguments, return the largest argument."); |
2173 | | |
2174 | | |
2175 | | /*[clinic input] |
2176 | | oct as builtin_oct |
2177 | | |
2178 | | integer: object |
2179 | | / |
2180 | | |
2181 | | Return the octal representation of an integer. |
2182 | | |
2183 | | >>> oct(342391) |
2184 | | '0o1234567' |
2185 | | [clinic start generated code]*/ |
2186 | | |
2187 | | static PyObject * |
2188 | | builtin_oct(PyObject *module, PyObject *integer) |
2189 | | /*[clinic end generated code: output=8c15f2145a74c390 input=b97c377b15fedf8d]*/ |
2190 | 0 | { |
2191 | 0 | return PyNumber_ToBase(integer, 8); |
2192 | 0 | } |
2193 | | |
2194 | | |
2195 | | /*[clinic input] |
2196 | | ord as builtin_ord |
2197 | | |
2198 | | character as c: object |
2199 | | / |
2200 | | |
2201 | | Return the ordinal value of a character. |
2202 | | |
2203 | | If the argument is a one-character string, return the Unicode code |
2204 | | point of that character. |
2205 | | |
2206 | | If the argument is a bytes or bytearray object of length 1, return its |
2207 | | single byte value. |
2208 | | [clinic start generated code]*/ |
2209 | | |
2210 | | static PyObject * |
2211 | | builtin_ord(PyObject *module, PyObject *c) |
2212 | | /*[clinic end generated code: output=4fa5e87a323bae71 input=98d38480432e1177]*/ |
2213 | 136M | { |
2214 | 136M | long ord; |
2215 | 136M | Py_ssize_t size; |
2216 | | |
2217 | 136M | if (PyBytes_Check(c)) { |
2218 | 0 | size = PyBytes_GET_SIZE(c); |
2219 | 0 | if (size == 1) { |
2220 | 0 | ord = (long)((unsigned char)*PyBytes_AS_STRING(c)); |
2221 | 0 | return PyLong_FromLong(ord); |
2222 | 0 | } |
2223 | 0 | } |
2224 | 136M | else if (PyUnicode_Check(c)) { |
2225 | 136M | size = PyUnicode_GET_LENGTH(c); |
2226 | 136M | if (size == 1) { |
2227 | 136M | ord = (long)PyUnicode_READ_CHAR(c, 0); |
2228 | 136M | return PyLong_FromLong(ord); |
2229 | 136M | } |
2230 | 136M | } |
2231 | 0 | else if (PyByteArray_Check(c)) { |
2232 | | /* XXX Hopefully this is temporary */ |
2233 | 0 | size = PyByteArray_GET_SIZE(c); |
2234 | 0 | if (size == 1) { |
2235 | 0 | ord = (long)((unsigned char)*PyByteArray_AS_STRING(c)); |
2236 | 0 | return PyLong_FromLong(ord); |
2237 | 0 | } |
2238 | 0 | } |
2239 | 0 | else { |
2240 | 0 | PyErr_Format(PyExc_TypeError, |
2241 | 0 | "ord() expected string of length 1, but " \ |
2242 | 0 | "%.200s found", Py_TYPE(c)->tp_name); |
2243 | 0 | return NULL; |
2244 | 0 | } |
2245 | | |
2246 | 0 | PyErr_Format(PyExc_TypeError, |
2247 | 0 | "ord() expected a character, " |
2248 | 0 | "but string of length %zd found", |
2249 | 0 | size); |
2250 | 0 | return NULL; |
2251 | 136M | } |
2252 | | |
2253 | | |
2254 | | /*[clinic input] |
2255 | | @permit_long_summary |
2256 | | pow as builtin_pow |
2257 | | |
2258 | | base: object |
2259 | | exp: object |
2260 | | mod: object = None |
2261 | | |
2262 | | Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments |
2263 | | |
2264 | | Some types, such as ints, are able to use a more efficient algorithm |
2265 | | when invoked using the three argument form. |
2266 | | [clinic start generated code]*/ |
2267 | | |
2268 | | static PyObject * |
2269 | | builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp, |
2270 | | PyObject *mod) |
2271 | | /*[clinic end generated code: output=3ca1538221bbf15f input=0cd5c3ecc8003aec]*/ |
2272 | 4 | { |
2273 | 4 | return PyNumber_Power(base, exp, mod); |
2274 | 4 | } |
2275 | | |
2276 | | /*[clinic input] |
2277 | | print as builtin_print |
2278 | | |
2279 | | *objects: array |
2280 | | sep: object(c_default="Py_None") = ' ' |
2281 | | string inserted between values, default a space. |
2282 | | end: object(c_default="Py_None") = '\n' |
2283 | | string appended after the last value, default a newline. |
2284 | | file: object = None |
2285 | | a file-like object (stream); defaults to the current sys.stdout. |
2286 | | flush: bool = False |
2287 | | whether to forcibly flush the stream. |
2288 | | |
2289 | | Prints the values to a stream, or to sys.stdout by default. |
2290 | | |
2291 | | [clinic start generated code]*/ |
2292 | | |
2293 | | static PyObject * |
2294 | | builtin_print_impl(PyObject *module, PyObject * const *objects, |
2295 | | Py_ssize_t objects_length, PyObject *sep, PyObject *end, |
2296 | | PyObject *file, int flush) |
2297 | | /*[clinic end generated code: output=38d8def56c837bcc input=ff35cb3d59ee8115]*/ |
2298 | 0 | { |
2299 | 0 | int i, err; |
2300 | |
|
2301 | 0 | if (file == Py_None) { |
2302 | 0 | file = PySys_GetAttr(&_Py_ID(stdout)); |
2303 | 0 | if (file == NULL) { |
2304 | 0 | return NULL; |
2305 | 0 | } |
2306 | | |
2307 | | /* sys.stdout may be None when FILE* stdout isn't connected */ |
2308 | 0 | if (file == Py_None) { |
2309 | 0 | Py_DECREF(file); |
2310 | 0 | Py_RETURN_NONE; |
2311 | 0 | } |
2312 | 0 | } |
2313 | 0 | else { |
2314 | 0 | Py_INCREF(file); |
2315 | 0 | } |
2316 | | |
2317 | 0 | if (sep == Py_None) { |
2318 | 0 | sep = NULL; |
2319 | 0 | } |
2320 | 0 | else if (sep && !PyUnicode_Check(sep)) { |
2321 | 0 | PyErr_Format(PyExc_TypeError, |
2322 | 0 | "sep must be None or a string, not %.200s", |
2323 | 0 | Py_TYPE(sep)->tp_name); |
2324 | 0 | Py_DECREF(file); |
2325 | 0 | return NULL; |
2326 | 0 | } |
2327 | 0 | if (end == Py_None) { |
2328 | 0 | end = NULL; |
2329 | 0 | } |
2330 | 0 | else if (end && !PyUnicode_Check(end)) { |
2331 | 0 | PyErr_Format(PyExc_TypeError, |
2332 | 0 | "end must be None or a string, not %.200s", |
2333 | 0 | Py_TYPE(end)->tp_name); |
2334 | 0 | Py_DECREF(file); |
2335 | 0 | return NULL; |
2336 | 0 | } |
2337 | | |
2338 | 0 | for (i = 0; i < objects_length; i++) { |
2339 | 0 | if (i > 0) { |
2340 | 0 | if (sep == NULL) { |
2341 | 0 | err = PyFile_WriteString(" ", file); |
2342 | 0 | } |
2343 | 0 | else { |
2344 | 0 | err = PyFile_WriteObject(sep, file, Py_PRINT_RAW); |
2345 | 0 | } |
2346 | 0 | if (err) { |
2347 | 0 | Py_DECREF(file); |
2348 | 0 | return NULL; |
2349 | 0 | } |
2350 | 0 | } |
2351 | 0 | err = PyFile_WriteObject(objects[i], file, Py_PRINT_RAW); |
2352 | 0 | if (err) { |
2353 | 0 | Py_DECREF(file); |
2354 | 0 | return NULL; |
2355 | 0 | } |
2356 | 0 | } |
2357 | | |
2358 | 0 | if (end == NULL) { |
2359 | 0 | err = PyFile_WriteString("\n", file); |
2360 | 0 | } |
2361 | 0 | else { |
2362 | 0 | err = PyFile_WriteObject(end, file, Py_PRINT_RAW); |
2363 | 0 | } |
2364 | 0 | if (err) { |
2365 | 0 | Py_DECREF(file); |
2366 | 0 | return NULL; |
2367 | 0 | } |
2368 | | |
2369 | 0 | if (flush) { |
2370 | 0 | if (_PyFile_Flush(file) < 0) { |
2371 | 0 | Py_DECREF(file); |
2372 | 0 | return NULL; |
2373 | 0 | } |
2374 | 0 | } |
2375 | 0 | Py_DECREF(file); |
2376 | |
|
2377 | 0 | Py_RETURN_NONE; |
2378 | 0 | } |
2379 | | |
2380 | | |
2381 | | /*[clinic input] |
2382 | | input as builtin_input |
2383 | | |
2384 | | prompt: object(c_default="NULL") = "" |
2385 | | / |
2386 | | |
2387 | | Read a string from standard input. The trailing newline is stripped. |
2388 | | |
2389 | | The prompt string, if given, is printed to standard output without a |
2390 | | trailing newline before reading input. |
2391 | | |
2392 | | If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise |
2393 | | EOFError. |
2394 | | On *nix systems, readline is used if available. |
2395 | | [clinic start generated code]*/ |
2396 | | |
2397 | | static PyObject * |
2398 | | builtin_input_impl(PyObject *module, PyObject *prompt) |
2399 | | /*[clinic end generated code: output=83db5a191e7a0d60 input=ebb939c954639427]*/ |
2400 | 0 | { |
2401 | 0 | PyObject *fin = NULL; |
2402 | 0 | PyObject *fout = NULL; |
2403 | 0 | PyObject *ferr = NULL; |
2404 | 0 | PyObject *tmp; |
2405 | 0 | long fd; |
2406 | 0 | int tty; |
2407 | | |
2408 | | /* Check that stdin/out/err are intact */ |
2409 | 0 | fin = PySys_GetAttr(&_Py_ID(stdin)); |
2410 | 0 | if (fin == NULL) { |
2411 | 0 | goto error; |
2412 | 0 | } |
2413 | 0 | if (fin == Py_None) { |
2414 | 0 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin"); |
2415 | 0 | goto error; |
2416 | 0 | } |
2417 | 0 | fout = PySys_GetAttr(&_Py_ID(stdout)); |
2418 | 0 | if (fout == NULL) { |
2419 | 0 | goto error; |
2420 | 0 | } |
2421 | 0 | if (fout == Py_None) { |
2422 | 0 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); |
2423 | 0 | goto error; |
2424 | 0 | } |
2425 | 0 | ferr = PySys_GetAttr(&_Py_ID(stderr)); |
2426 | 0 | if (ferr == NULL) { |
2427 | 0 | goto error; |
2428 | 0 | } |
2429 | 0 | if (ferr == Py_None) { |
2430 | 0 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stderr"); |
2431 | 0 | goto error; |
2432 | 0 | } |
2433 | | |
2434 | 0 | if (PySys_Audit("builtins.input", "O", prompt ? prompt : Py_None) < 0) { |
2435 | 0 | goto error; |
2436 | 0 | } |
2437 | | |
2438 | | /* First of all, flush stderr */ |
2439 | 0 | if (_PyFile_Flush(ferr) < 0) { |
2440 | 0 | PyErr_Clear(); |
2441 | 0 | } |
2442 | | |
2443 | | /* We should only use (GNU) readline if Python's sys.stdin and |
2444 | | sys.stdout are the same as C's stdin and stdout, because we |
2445 | | need to pass it those. */ |
2446 | 0 | tmp = PyObject_CallMethodNoArgs(fin, &_Py_ID(fileno)); |
2447 | 0 | if (tmp == NULL) { |
2448 | 0 | PyErr_Clear(); |
2449 | 0 | tty = 0; |
2450 | 0 | } |
2451 | 0 | else { |
2452 | 0 | fd = PyLong_AsLong(tmp); |
2453 | 0 | Py_DECREF(tmp); |
2454 | 0 | if (fd < 0 && PyErr_Occurred()) { |
2455 | 0 | goto error; |
2456 | 0 | } |
2457 | 0 | tty = fd == fileno(stdin) && isatty(fd); |
2458 | 0 | } |
2459 | 0 | if (tty) { |
2460 | 0 | tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(fileno)); |
2461 | 0 | if (tmp == NULL) { |
2462 | 0 | PyErr_Clear(); |
2463 | 0 | tty = 0; |
2464 | 0 | } |
2465 | 0 | else { |
2466 | 0 | fd = PyLong_AsLong(tmp); |
2467 | 0 | Py_DECREF(tmp); |
2468 | 0 | if (fd < 0 && PyErr_Occurred()) |
2469 | 0 | goto error; |
2470 | 0 | tty = fd == fileno(stdout) && isatty(fd); |
2471 | 0 | } |
2472 | 0 | } |
2473 | | |
2474 | | /* If we're interactive, use (GNU) readline */ |
2475 | 0 | if (tty) { |
2476 | 0 | PyObject *po = NULL; |
2477 | 0 | const char *promptstr; |
2478 | 0 | char *s = NULL; |
2479 | 0 | PyObject *stdin_encoding = NULL, *stdin_errors = NULL; |
2480 | 0 | PyObject *stdout_encoding = NULL, *stdout_errors = NULL; |
2481 | 0 | const char *stdin_encoding_str, *stdin_errors_str; |
2482 | 0 | PyObject *result; |
2483 | 0 | size_t len; |
2484 | | |
2485 | | /* stdin is a text stream, so it must have an encoding. */ |
2486 | 0 | stdin_encoding = PyObject_GetAttr(fin, &_Py_ID(encoding)); |
2487 | 0 | if (stdin_encoding == NULL) { |
2488 | 0 | tty = 0; |
2489 | 0 | goto _readline_errors; |
2490 | 0 | } |
2491 | 0 | stdin_errors = PyObject_GetAttr(fin, &_Py_ID(errors)); |
2492 | 0 | if (stdin_errors == NULL) { |
2493 | 0 | tty = 0; |
2494 | 0 | goto _readline_errors; |
2495 | 0 | } |
2496 | 0 | if (!PyUnicode_Check(stdin_encoding) || |
2497 | 0 | !PyUnicode_Check(stdin_errors)) |
2498 | 0 | { |
2499 | 0 | tty = 0; |
2500 | 0 | goto _readline_errors; |
2501 | 0 | } |
2502 | 0 | stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding); |
2503 | 0 | if (stdin_encoding_str == NULL) { |
2504 | 0 | goto _readline_errors; |
2505 | 0 | } |
2506 | 0 | stdin_errors_str = PyUnicode_AsUTF8(stdin_errors); |
2507 | 0 | if (stdin_errors_str == NULL) { |
2508 | 0 | goto _readline_errors; |
2509 | 0 | } |
2510 | 0 | if (_PyFile_Flush(fout) < 0) { |
2511 | 0 | PyErr_Clear(); |
2512 | 0 | } |
2513 | 0 | if (prompt != NULL) { |
2514 | | /* We have a prompt, encode it as stdout would */ |
2515 | 0 | const char *stdout_encoding_str, *stdout_errors_str; |
2516 | 0 | PyObject *stringpo; |
2517 | 0 | stdout_encoding = PyObject_GetAttr(fout, &_Py_ID(encoding)); |
2518 | 0 | if (stdout_encoding == NULL) { |
2519 | 0 | tty = 0; |
2520 | 0 | goto _readline_errors; |
2521 | 0 | } |
2522 | 0 | stdout_errors = PyObject_GetAttr(fout, &_Py_ID(errors)); |
2523 | 0 | if (stdout_errors == NULL) { |
2524 | 0 | tty = 0; |
2525 | 0 | goto _readline_errors; |
2526 | 0 | } |
2527 | 0 | if (!PyUnicode_Check(stdout_encoding) || |
2528 | 0 | !PyUnicode_Check(stdout_errors)) |
2529 | 0 | { |
2530 | 0 | tty = 0; |
2531 | 0 | goto _readline_errors; |
2532 | 0 | } |
2533 | 0 | stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding); |
2534 | 0 | if (stdout_encoding_str == NULL) { |
2535 | 0 | goto _readline_errors; |
2536 | 0 | } |
2537 | 0 | stdout_errors_str = PyUnicode_AsUTF8(stdout_errors); |
2538 | 0 | if (stdout_errors_str == NULL) { |
2539 | 0 | goto _readline_errors; |
2540 | 0 | } |
2541 | 0 | stringpo = PyObject_Str(prompt); |
2542 | 0 | if (stringpo == NULL) |
2543 | 0 | goto _readline_errors; |
2544 | 0 | po = PyUnicode_AsEncodedString(stringpo, |
2545 | 0 | stdout_encoding_str, stdout_errors_str); |
2546 | 0 | Py_CLEAR(stdout_encoding); |
2547 | 0 | Py_CLEAR(stdout_errors); |
2548 | 0 | Py_CLEAR(stringpo); |
2549 | 0 | if (po == NULL) |
2550 | 0 | goto _readline_errors; |
2551 | 0 | assert(PyBytes_Check(po)); |
2552 | 0 | promptstr = PyBytes_AS_STRING(po); |
2553 | 0 | if ((Py_ssize_t)strlen(promptstr) != PyBytes_GET_SIZE(po)) { |
2554 | 0 | PyErr_SetString(PyExc_ValueError, |
2555 | 0 | "input: prompt string cannot contain null characters"); |
2556 | 0 | goto _readline_errors; |
2557 | 0 | } |
2558 | 0 | } |
2559 | 0 | else { |
2560 | 0 | po = NULL; |
2561 | 0 | promptstr = ""; |
2562 | 0 | } |
2563 | 0 | s = PyOS_Readline(stdin, stdout, promptstr); |
2564 | 0 | if (s == NULL) { |
2565 | 0 | PyErr_CheckSignals(); |
2566 | 0 | if (!PyErr_Occurred()) |
2567 | 0 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
2568 | 0 | goto _readline_errors; |
2569 | 0 | } |
2570 | | |
2571 | 0 | len = strlen(s); |
2572 | 0 | if (len == 0) { |
2573 | 0 | PyErr_SetNone(PyExc_EOFError); |
2574 | 0 | result = NULL; |
2575 | 0 | } |
2576 | 0 | else { |
2577 | 0 | if (len > PY_SSIZE_T_MAX) { |
2578 | 0 | PyErr_SetString(PyExc_OverflowError, |
2579 | 0 | "input: input too long"); |
2580 | 0 | result = NULL; |
2581 | 0 | } |
2582 | 0 | else { |
2583 | 0 | len--; /* strip trailing '\n' */ |
2584 | 0 | if (len != 0 && s[len-1] == '\r') |
2585 | 0 | len--; /* strip trailing '\r' */ |
2586 | 0 | result = PyUnicode_Decode(s, len, stdin_encoding_str, |
2587 | 0 | stdin_errors_str); |
2588 | 0 | } |
2589 | 0 | } |
2590 | 0 | Py_DECREF(stdin_encoding); |
2591 | 0 | Py_DECREF(stdin_errors); |
2592 | 0 | Py_XDECREF(po); |
2593 | 0 | PyMem_Free(s); |
2594 | |
|
2595 | 0 | if (result != NULL) { |
2596 | 0 | if (PySys_Audit("builtins.input/result", "O", result) < 0) { |
2597 | 0 | goto error; |
2598 | 0 | } |
2599 | 0 | } |
2600 | | |
2601 | 0 | Py_DECREF(fin); |
2602 | 0 | Py_DECREF(fout); |
2603 | 0 | Py_DECREF(ferr); |
2604 | 0 | return result; |
2605 | | |
2606 | 0 | _readline_errors: |
2607 | 0 | Py_XDECREF(stdin_encoding); |
2608 | 0 | Py_XDECREF(stdout_encoding); |
2609 | 0 | Py_XDECREF(stdin_errors); |
2610 | 0 | Py_XDECREF(stdout_errors); |
2611 | 0 | Py_XDECREF(po); |
2612 | 0 | if (tty) |
2613 | 0 | goto error; |
2614 | | |
2615 | 0 | PyErr_Clear(); |
2616 | 0 | } |
2617 | | |
2618 | | /* Fallback if we're not interactive */ |
2619 | 0 | if (prompt != NULL) { |
2620 | 0 | if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0) |
2621 | 0 | goto error; |
2622 | 0 | } |
2623 | 0 | if (_PyFile_Flush(fout) < 0) { |
2624 | 0 | PyErr_Clear(); |
2625 | 0 | } |
2626 | 0 | tmp = PyFile_GetLine(fin, -1); |
2627 | 0 | Py_DECREF(fin); |
2628 | 0 | Py_DECREF(fout); |
2629 | 0 | Py_DECREF(ferr); |
2630 | 0 | return tmp; |
2631 | | |
2632 | 0 | error: |
2633 | 0 | Py_XDECREF(fin); |
2634 | 0 | Py_XDECREF(fout); |
2635 | 0 | Py_XDECREF(ferr); |
2636 | 0 | return NULL; |
2637 | 0 | } |
2638 | | |
2639 | | |
2640 | | /*[clinic input] |
2641 | | repr as builtin_repr |
2642 | | |
2643 | | obj: object |
2644 | | / |
2645 | | |
2646 | | Return the canonical string representation of the object. |
2647 | | |
2648 | | For many object types, including most builtins, eval(repr(obj)) == obj. |
2649 | | [clinic start generated code]*/ |
2650 | | |
2651 | | static PyObject * |
2652 | | builtin_repr(PyObject *module, PyObject *obj) |
2653 | | /*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/ |
2654 | 93.9k | { |
2655 | 93.9k | return PyObject_Repr(obj); |
2656 | 93.9k | } |
2657 | | |
2658 | | |
2659 | | /*[clinic input] |
2660 | | round as builtin_round |
2661 | | |
2662 | | number: object |
2663 | | ndigits: object = None |
2664 | | |
2665 | | Round a number to a given precision in decimal digits. |
2666 | | |
2667 | | The return value is an integer if ndigits is omitted or None. |
2668 | | Otherwise the return value has the same type as the number. ndigits |
2669 | | may be negative. |
2670 | | [clinic start generated code]*/ |
2671 | | |
2672 | | static PyObject * |
2673 | | builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits) |
2674 | | /*[clinic end generated code: output=ff0d9dd176c02ede input=bdcb7c67bf4a4320]*/ |
2675 | 0 | { |
2676 | 0 | PyObject *result; |
2677 | 0 | if (ndigits == Py_None) { |
2678 | 0 | result = _PyObject_MaybeCallSpecialNoArgs(number, &_Py_ID(__round__)); |
2679 | 0 | } |
2680 | 0 | else { |
2681 | 0 | result = _PyObject_MaybeCallSpecialOneArg(number, &_Py_ID(__round__), |
2682 | 0 | ndigits); |
2683 | 0 | } |
2684 | 0 | if (result == NULL && !PyErr_Occurred()) { |
2685 | 0 | PyErr_Format(PyExc_TypeError, |
2686 | 0 | "type %.100s doesn't define __round__ method", |
2687 | 0 | Py_TYPE(number)->tp_name); |
2688 | 0 | } |
2689 | 0 | return result; |
2690 | 0 | } |
2691 | | |
2692 | | |
2693 | | /*AC: we need to keep the kwds dict intact to easily call into the |
2694 | | * list.sort method, which isn't currently supported in AC. So we just use |
2695 | | * the initially generated signature with a custom implementation. |
2696 | | */ |
2697 | | /* [disabled clinic input] |
2698 | | sorted as builtin_sorted |
2699 | | |
2700 | | iterable as seq: object |
2701 | | key as keyfunc: object = None |
2702 | | reverse: object = False |
2703 | | |
2704 | | Return a new list containing all items from the iterable in ascending order. |
2705 | | |
2706 | | A custom key function can be supplied to customize the sort order, and |
2707 | | the reverse flag can be set to request the result in descending order. |
2708 | | [end disabled clinic input]*/ |
2709 | | |
2710 | | PyDoc_STRVAR(builtin_sorted__doc__, |
2711 | | "sorted($module, iterable, /, *, key=None, reverse=False)\n" |
2712 | | "--\n" |
2713 | | "\n" |
2714 | | "Return a new list containing all items from the iterable in ascending order.\n" |
2715 | | "\n" |
2716 | | "A custom key function can be supplied to customize the sort order, and the\n" |
2717 | | "reverse flag can be set to request the result in descending order."); |
2718 | | |
2719 | | #define BUILTIN_SORTED_METHODDEF \ |
2720 | | {"sorted", _PyCFunction_CAST(builtin_sorted), METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__}, |
2721 | | |
2722 | | static PyObject * |
2723 | | builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
2724 | 4.17M | { |
2725 | 4.17M | PyObject *newlist, *v, *seq, *callable; |
2726 | | |
2727 | | /* Keyword arguments are passed through list.sort() which will check |
2728 | | them. */ |
2729 | 4.17M | if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq)) |
2730 | 0 | return NULL; |
2731 | | |
2732 | 4.17M | newlist = PySequence_List(seq); |
2733 | 4.17M | if (newlist == NULL) |
2734 | 0 | return NULL; |
2735 | | |
2736 | 4.17M | callable = PyObject_GetAttr(newlist, &_Py_ID(sort)); |
2737 | 4.17M | if (callable == NULL) { |
2738 | 0 | Py_DECREF(newlist); |
2739 | 0 | return NULL; |
2740 | 0 | } |
2741 | | |
2742 | 4.17M | assert(nargs >= 1); |
2743 | 4.17M | v = PyObject_Vectorcall(callable, args + 1, nargs - 1, kwnames); |
2744 | 4.17M | Py_DECREF(callable); |
2745 | 4.17M | if (v == NULL) { |
2746 | 0 | Py_DECREF(newlist); |
2747 | 0 | return NULL; |
2748 | 0 | } |
2749 | 4.17M | Py_DECREF(v); |
2750 | 4.17M | return newlist; |
2751 | 4.17M | } |
2752 | | |
2753 | | |
2754 | | /* AC: cannot convert yet, as needs PEP 457 group support in inspect */ |
2755 | | /*[clinic input] |
2756 | | vars as builtin_vars |
2757 | | |
2758 | | object as v: object = NULL |
2759 | | / |
2760 | | |
2761 | | Show vars. |
2762 | | |
2763 | | Without arguments, equivalent to locals(). |
2764 | | With an argument, equivalent to object.__dict__. |
2765 | | [clinic start generated code]*/ |
2766 | | |
2767 | | static PyObject * |
2768 | | builtin_vars_impl(PyObject *module, PyObject *v) |
2769 | | /*[clinic end generated code: output=a64017e4a4dc53fc input=577550318240f012]*/ |
2770 | 126 | { |
2771 | 126 | PyObject *d; |
2772 | | |
2773 | 126 | if (v == NULL) { |
2774 | 4 | if (_PyEval_GetFrame() != NULL) { |
2775 | 4 | d = _PyEval_GetFrameLocals(); |
2776 | 4 | } |
2777 | 0 | else { |
2778 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2779 | 0 | d = _PyEval_GetGlobalsFromRunningMain(tstate); |
2780 | 0 | if (d == NULL) { |
2781 | 0 | if (!_PyErr_Occurred(tstate)) { |
2782 | 0 | d = _PyEval_GetFrameLocals(); |
2783 | 0 | assert(_PyErr_Occurred(tstate)); |
2784 | 0 | } |
2785 | 0 | } |
2786 | 0 | else { |
2787 | 0 | Py_INCREF(d); |
2788 | 0 | } |
2789 | 0 | } |
2790 | 4 | } |
2791 | 122 | else { |
2792 | 122 | if (PyObject_GetOptionalAttr(v, &_Py_ID(__dict__), &d) == 0) { |
2793 | 0 | PyErr_SetString(PyExc_TypeError, |
2794 | 0 | "vars() argument must have __dict__ attribute"); |
2795 | 0 | } |
2796 | 122 | } |
2797 | 126 | return d; |
2798 | 126 | } |
2799 | | |
2800 | | |
2801 | | /* Improved Kahan–Babuška algorithm by Arnold Neumaier |
2802 | | Neumaier, A. (1974), Rundungsfehleranalyse einiger Verfahren |
2803 | | zur Summation endlicher Summen. Z. angew. Math. Mech., |
2804 | | 54: 39-51. https://doi.org/10.1002/zamm.19740540106 |
2805 | | https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Further_enhancements |
2806 | | */ |
2807 | | |
2808 | | typedef struct { |
2809 | | double hi; /* high-order bits for a running sum */ |
2810 | | double lo; /* a running compensation for lost low-order bits */ |
2811 | | } CompensatedSum; |
2812 | | |
2813 | | static inline CompensatedSum |
2814 | | cs_from_double(double x) |
2815 | 33.4k | { |
2816 | 33.4k | return (CompensatedSum) {x}; |
2817 | 33.4k | } |
2818 | | |
2819 | | static inline CompensatedSum |
2820 | | cs_add(CompensatedSum total, double x) |
2821 | 100k | { |
2822 | 100k | double t = total.hi + x; |
2823 | 100k | if (fabs(total.hi) >= fabs(x)) { |
2824 | 100k | total.lo += (total.hi - t) + x; |
2825 | 100k | } |
2826 | 0 | else { |
2827 | 0 | total.lo += (x - t) + total.hi; |
2828 | 0 | } |
2829 | 100k | return (CompensatedSum) {t, total.lo}; |
2830 | 100k | } |
2831 | | |
2832 | | static inline double |
2833 | | cs_to_double(CompensatedSum total) |
2834 | 33.4k | { |
2835 | | /* Avoid losing the sign on a negative result, |
2836 | | and don't let adding the compensation convert |
2837 | | an infinite or overflowed sum to a NaN. */ |
2838 | 33.4k | if (total.lo && isfinite(total.lo)) { |
2839 | 0 | return total.hi + total.lo; |
2840 | 0 | } |
2841 | 33.4k | return total.hi; |
2842 | 33.4k | } |
2843 | | |
2844 | | /*[clinic input] |
2845 | | @permit_long_summary |
2846 | | sum as builtin_sum |
2847 | | |
2848 | | iterable: object |
2849 | | / |
2850 | | start: object(c_default="NULL") = 0 |
2851 | | |
2852 | | Return the sum of a 'start' value (default: 0) plus an iterable of numbers |
2853 | | |
2854 | | When the iterable is empty, return the start value. |
2855 | | This function is intended specifically for use with numeric values and |
2856 | | may reject non-numeric types. |
2857 | | [clinic start generated code]*/ |
2858 | | |
2859 | | static PyObject * |
2860 | | builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) |
2861 | | /*[clinic end generated code: output=df758cec7d1d302f input=d464d57815196b73]*/ |
2862 | 11.6M | { |
2863 | 11.6M | PyObject *result = start; |
2864 | 11.6M | PyObject *temp, *item, *iter; |
2865 | | |
2866 | 11.6M | iter = PyObject_GetIter(iterable); |
2867 | 11.6M | if (iter == NULL) |
2868 | 0 | return NULL; |
2869 | | |
2870 | 11.6M | if (result == NULL) { |
2871 | 2.67M | result = PyLong_FromLong(0); |
2872 | 2.67M | if (result == NULL) { |
2873 | 0 | Py_DECREF(iter); |
2874 | 0 | return NULL; |
2875 | 0 | } |
2876 | 8.98M | } else { |
2877 | | /* reject string values for 'start' parameter */ |
2878 | 8.98M | if (PyUnicode_Check(result)) { |
2879 | 0 | PyErr_SetString(PyExc_TypeError, |
2880 | 0 | "sum() can't sum strings [use ''.join(seq) instead]"); |
2881 | 0 | Py_DECREF(iter); |
2882 | 0 | return NULL; |
2883 | 0 | } |
2884 | 8.98M | if (PyBytes_Check(result)) { |
2885 | 0 | PyErr_SetString(PyExc_TypeError, |
2886 | 0 | "sum() can't sum bytes [use b''.join(seq) instead]"); |
2887 | 0 | Py_DECREF(iter); |
2888 | 0 | return NULL; |
2889 | 0 | } |
2890 | 8.98M | if (PyByteArray_Check(result)) { |
2891 | 0 | PyErr_SetString(PyExc_TypeError, |
2892 | 0 | "sum() can't sum bytearray [use b''.join(seq) instead]"); |
2893 | 0 | Py_DECREF(iter); |
2894 | 0 | return NULL; |
2895 | 0 | } |
2896 | 8.98M | Py_INCREF(result); |
2897 | 8.98M | } |
2898 | | |
2899 | 11.6M | #ifndef SLOW_SUM |
2900 | | /* Fast addition by keeping temporary sums in C instead of new Python objects. |
2901 | | Assumes all inputs are the same type. If the assumption fails, default |
2902 | | to the more general routine. |
2903 | | */ |
2904 | 11.6M | if (PyLong_CheckExact(result)) { |
2905 | 2.67M | int overflow; |
2906 | 2.67M | Py_ssize_t i_result = PyLong_AsLongAndOverflow(result, &overflow); |
2907 | | /* If this already overflowed, don't even enter the loop. */ |
2908 | 2.67M | if (overflow == 0) { |
2909 | 2.67M | Py_SETREF(result, NULL); |
2910 | 2.67M | } |
2911 | 1.28G | while(result == NULL) { |
2912 | 1.28G | item = PyIter_Next(iter); |
2913 | 1.28G | if (item == NULL) { |
2914 | 2.64M | Py_DECREF(iter); |
2915 | 2.64M | if (PyErr_Occurred()) |
2916 | 0 | return NULL; |
2917 | 2.64M | return PyLong_FromSsize_t(i_result); |
2918 | 2.64M | } |
2919 | 1.28G | if (PyLong_CheckExact(item) || PyBool_Check(item)) { |
2920 | 1.28G | Py_ssize_t b; |
2921 | 1.28G | overflow = 0; |
2922 | | /* Single digits are common, fast, and cannot overflow on unpacking. */ |
2923 | 1.28G | if (_PyLong_IsCompact((PyLongObject *)item)) { |
2924 | 1.28G | b = _PyLong_CompactValue((PyLongObject *)item); |
2925 | 1.28G | } |
2926 | 0 | else { |
2927 | 0 | b = PyLong_AsLongAndOverflow(item, &overflow); |
2928 | 0 | } |
2929 | 1.28G | if (overflow == 0 && |
2930 | 1.28G | (i_result >= 0 ? (b <= PY_SSIZE_T_MAX - i_result) |
2931 | 1.28G | : (b >= PY_SSIZE_T_MIN - i_result))) |
2932 | 1.28G | { |
2933 | 1.28G | i_result += b; |
2934 | 1.28G | Py_DECREF(item); |
2935 | 1.28G | continue; |
2936 | 1.28G | } |
2937 | 1.28G | } |
2938 | | /* Either overflowed or is not an int. Restore real objects and process normally */ |
2939 | 33.4k | result = PyLong_FromSsize_t(i_result); |
2940 | 33.4k | if (result == NULL) { |
2941 | 0 | Py_DECREF(item); |
2942 | 0 | Py_DECREF(iter); |
2943 | 0 | return NULL; |
2944 | 0 | } |
2945 | 33.4k | temp = PyNumber_Add(result, item); |
2946 | 33.4k | Py_DECREF(result); |
2947 | 33.4k | Py_DECREF(item); |
2948 | 33.4k | result = temp; |
2949 | 33.4k | if (result == NULL) { |
2950 | 0 | Py_DECREF(iter); |
2951 | 0 | return NULL; |
2952 | 0 | } |
2953 | 33.4k | } |
2954 | 2.67M | } |
2955 | | |
2956 | 9.01M | if (PyFloat_CheckExact(result)) { |
2957 | 33.4k | CompensatedSum re_sum = cs_from_double(PyFloat_AS_DOUBLE(result)); |
2958 | 33.4k | Py_SETREF(result, NULL); |
2959 | 133k | while(result == NULL) { |
2960 | 133k | item = PyIter_Next(iter); |
2961 | 133k | if (item == NULL) { |
2962 | 33.4k | Py_DECREF(iter); |
2963 | 33.4k | if (PyErr_Occurred()) |
2964 | 0 | return NULL; |
2965 | 33.4k | return PyFloat_FromDouble(cs_to_double(re_sum)); |
2966 | 33.4k | } |
2967 | 100k | if (PyFloat_CheckExact(item)) { |
2968 | 100k | re_sum = cs_add(re_sum, PyFloat_AS_DOUBLE(item)); |
2969 | 100k | _Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc); |
2970 | 100k | continue; |
2971 | 100k | } |
2972 | 0 | if (PyLong_Check(item)) { |
2973 | 0 | double value = PyLong_AsDouble(item); |
2974 | 0 | if (value != -1.0 || !PyErr_Occurred()) { |
2975 | 0 | re_sum = cs_add(re_sum, value); |
2976 | 0 | Py_DECREF(item); |
2977 | 0 | continue; |
2978 | 0 | } |
2979 | 0 | else { |
2980 | 0 | Py_DECREF(item); |
2981 | 0 | Py_DECREF(iter); |
2982 | 0 | return NULL; |
2983 | 0 | } |
2984 | 0 | } |
2985 | 0 | result = PyFloat_FromDouble(cs_to_double(re_sum)); |
2986 | 0 | if (result == NULL) { |
2987 | 0 | Py_DECREF(item); |
2988 | 0 | Py_DECREF(iter); |
2989 | 0 | return NULL; |
2990 | 0 | } |
2991 | 0 | temp = PyNumber_Add(result, item); |
2992 | 0 | Py_DECREF(result); |
2993 | 0 | Py_DECREF(item); |
2994 | 0 | result = temp; |
2995 | 0 | if (result == NULL) { |
2996 | 0 | Py_DECREF(iter); |
2997 | 0 | return NULL; |
2998 | 0 | } |
2999 | 0 | } |
3000 | 33.4k | } |
3001 | | |
3002 | 8.98M | if (PyComplex_CheckExact(result)) { |
3003 | 0 | Py_complex z = PyComplex_AsCComplex(result); |
3004 | 0 | CompensatedSum re_sum = cs_from_double(z.real); |
3005 | 0 | CompensatedSum im_sum = cs_from_double(z.imag); |
3006 | 0 | Py_SETREF(result, NULL); |
3007 | 0 | while (result == NULL) { |
3008 | 0 | item = PyIter_Next(iter); |
3009 | 0 | if (item == NULL) { |
3010 | 0 | Py_DECREF(iter); |
3011 | 0 | if (PyErr_Occurred()) { |
3012 | 0 | return NULL; |
3013 | 0 | } |
3014 | 0 | return PyComplex_FromDoubles(cs_to_double(re_sum), |
3015 | 0 | cs_to_double(im_sum)); |
3016 | 0 | } |
3017 | 0 | if (PyComplex_CheckExact(item)) { |
3018 | 0 | z = PyComplex_AsCComplex(item); |
3019 | 0 | re_sum = cs_add(re_sum, z.real); |
3020 | 0 | im_sum = cs_add(im_sum, z.imag); |
3021 | 0 | Py_DECREF(item); |
3022 | 0 | continue; |
3023 | 0 | } |
3024 | 0 | if (PyLong_Check(item)) { |
3025 | 0 | double value = PyLong_AsDouble(item); |
3026 | 0 | if (value != -1.0 || !PyErr_Occurred()) { |
3027 | 0 | re_sum = cs_add(re_sum, value); |
3028 | 0 | Py_DECREF(item); |
3029 | 0 | continue; |
3030 | 0 | } |
3031 | 0 | else { |
3032 | 0 | Py_DECREF(item); |
3033 | 0 | Py_DECREF(iter); |
3034 | 0 | return NULL; |
3035 | 0 | } |
3036 | 0 | } |
3037 | 0 | if (PyFloat_Check(item)) { |
3038 | 0 | double value = PyFloat_AS_DOUBLE(item); |
3039 | 0 | re_sum = cs_add(re_sum, value); |
3040 | 0 | _Py_DECREF_SPECIALIZED(item, _PyFloat_ExactDealloc); |
3041 | 0 | continue; |
3042 | 0 | } |
3043 | 0 | result = PyComplex_FromDoubles(cs_to_double(re_sum), |
3044 | 0 | cs_to_double(im_sum)); |
3045 | 0 | if (result == NULL) { |
3046 | 0 | Py_DECREF(item); |
3047 | 0 | Py_DECREF(iter); |
3048 | 0 | return NULL; |
3049 | 0 | } |
3050 | 0 | temp = PyNumber_Add(result, item); |
3051 | 0 | Py_DECREF(result); |
3052 | 0 | Py_DECREF(item); |
3053 | 0 | result = temp; |
3054 | 0 | if (result == NULL) { |
3055 | 0 | Py_DECREF(iter); |
3056 | 0 | return NULL; |
3057 | 0 | } |
3058 | 0 | } |
3059 | 0 | } |
3060 | 8.98M | #endif |
3061 | | |
3062 | 27.9M | for(;;) { |
3063 | 27.9M | item = PyIter_Next(iter); |
3064 | 27.9M | if (item == NULL) { |
3065 | | /* error, or end-of-sequence */ |
3066 | 8.98M | if (PyErr_Occurred()) { |
3067 | 15.8k | Py_SETREF(result, NULL); |
3068 | 15.8k | } |
3069 | 8.98M | break; |
3070 | 8.98M | } |
3071 | | /* It's tempting to use PyNumber_InPlaceAdd instead of |
3072 | | PyNumber_Add here, to avoid quadratic running time |
3073 | | when doing 'sum(list_of_lists, [])'. However, this |
3074 | | would produce a change in behaviour: a snippet like |
3075 | | |
3076 | | empty = [] |
3077 | | sum([[x] for x in range(10)], empty) |
3078 | | |
3079 | | would change the value of empty. In fact, using |
3080 | | in-place addition rather that binary addition for |
3081 | | any of the steps introduces subtle behavior changes: |
3082 | | |
3083 | | https://bugs.python.org/issue18305 */ |
3084 | 18.9M | temp = PyNumber_Add(result, item); |
3085 | 18.9M | Py_DECREF(result); |
3086 | 18.9M | Py_DECREF(item); |
3087 | 18.9M | result = temp; |
3088 | 18.9M | if (result == NULL) |
3089 | 0 | break; |
3090 | 18.9M | } |
3091 | 8.98M | Py_DECREF(iter); |
3092 | 8.98M | return result; |
3093 | 8.98M | } |
3094 | | |
3095 | | |
3096 | | /*[clinic input] |
3097 | | @permit_long_summary |
3098 | | isinstance as builtin_isinstance |
3099 | | |
3100 | | obj: object |
3101 | | class_or_tuple: object |
3102 | | / |
3103 | | |
3104 | | Return whether an object is an instance of a class or of a subclass thereof. |
3105 | | |
3106 | | A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the |
3107 | | target to check against. This is equivalent to ``isinstance(x, A) or |
3108 | | isinstance(x, B) or ...`` etc. |
3109 | | [clinic start generated code]*/ |
3110 | | |
3111 | | static PyObject * |
3112 | | builtin_isinstance_impl(PyObject *module, PyObject *obj, |
3113 | | PyObject *class_or_tuple) |
3114 | | /*[clinic end generated code: output=6faf01472c13b003 input=5d74d547df498f38]*/ |
3115 | 2.38k | { |
3116 | 2.38k | int retval; |
3117 | | |
3118 | 2.38k | retval = PyObject_IsInstance(obj, class_or_tuple); |
3119 | 2.38k | if (retval < 0) |
3120 | 0 | return NULL; |
3121 | 2.38k | return PyBool_FromLong(retval); |
3122 | 2.38k | } |
3123 | | |
3124 | | |
3125 | | /*[clinic input] |
3126 | | issubclass as builtin_issubclass |
3127 | | |
3128 | | cls: object |
3129 | | class_or_tuple: object |
3130 | | / |
3131 | | |
3132 | | Return whether 'cls' is derived from another class or is the same class. |
3133 | | |
3134 | | A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the |
3135 | | target to check against. This is equivalent to ``issubclass(x, A) or |
3136 | | issubclass(x, B) or ...``. |
3137 | | [clinic start generated code]*/ |
3138 | | |
3139 | | static PyObject * |
3140 | | builtin_issubclass_impl(PyObject *module, PyObject *cls, |
3141 | | PyObject *class_or_tuple) |
3142 | | /*[clinic end generated code: output=358412410cd7a250 input=a91ce96345a6705d]*/ |
3143 | 12.0k | { |
3144 | 12.0k | int retval; |
3145 | | |
3146 | 12.0k | retval = PyObject_IsSubclass(cls, class_or_tuple); |
3147 | 12.0k | if (retval < 0) |
3148 | 0 | return NULL; |
3149 | 12.0k | return PyBool_FromLong(retval); |
3150 | 12.0k | } |
3151 | | |
3152 | | typedef struct { |
3153 | | PyObject_HEAD |
3154 | | Py_ssize_t tuplesize; |
3155 | | PyObject *ittuple; /* tuple of iterators */ |
3156 | | PyObject *result; |
3157 | | int strict; |
3158 | | } zipobject; |
3159 | | |
3160 | 2.09M | #define _zipobject_CAST(op) ((zipobject *)(op)) |
3161 | | |
3162 | | static PyObject * |
3163 | | zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
3164 | 73.1k | { |
3165 | 73.1k | zipobject *lz; |
3166 | 73.1k | Py_ssize_t i; |
3167 | 73.1k | PyObject *ittuple; /* tuple of iterators */ |
3168 | 73.1k | PyObject *result; |
3169 | 73.1k | Py_ssize_t tuplesize; |
3170 | 73.1k | int strict = 0; |
3171 | | |
3172 | 73.1k | if (kwds) { |
3173 | 48.8k | PyObject *empty = PyTuple_New(0); |
3174 | 48.8k | if (empty == NULL) { |
3175 | 0 | return NULL; |
3176 | 0 | } |
3177 | 48.8k | static char *kwlist[] = {"strict", NULL}; |
3178 | 48.8k | int parsed = PyArg_ParseTupleAndKeywords( |
3179 | 48.8k | empty, kwds, "|$p:zip", kwlist, &strict); |
3180 | 48.8k | Py_DECREF(empty); |
3181 | 48.8k | if (!parsed) { |
3182 | 0 | return NULL; |
3183 | 0 | } |
3184 | 48.8k | } |
3185 | | |
3186 | | /* args must be a tuple */ |
3187 | 73.1k | assert(PyTuple_Check(args)); |
3188 | 73.1k | tuplesize = PyTuple_GET_SIZE(args); |
3189 | | |
3190 | | /* obtain iterators */ |
3191 | 73.1k | ittuple = PyTuple_New(tuplesize); |
3192 | 73.1k | if (ittuple == NULL) |
3193 | 0 | return NULL; |
3194 | 5.41M | for (i=0; i < tuplesize; ++i) { |
3195 | 5.34M | PyObject *item = PyTuple_GET_ITEM(args, i); |
3196 | 5.34M | PyObject *it = PyObject_GetIter(item); |
3197 | 5.34M | if (it == NULL) { |
3198 | 0 | Py_DECREF(ittuple); |
3199 | 0 | return NULL; |
3200 | 0 | } |
3201 | 5.34M | PyTuple_SET_ITEM(ittuple, i, it); |
3202 | 5.34M | } |
3203 | | |
3204 | | /* create a result holder */ |
3205 | 73.1k | result = PyTuple_New(tuplesize); |
3206 | 73.1k | if (result == NULL) { |
3207 | 0 | Py_DECREF(ittuple); |
3208 | 0 | return NULL; |
3209 | 0 | } |
3210 | 5.41M | for (i=0 ; i < tuplesize ; i++) { |
3211 | 5.34M | PyTuple_SET_ITEM(result, i, Py_NewRef(Py_None)); |
3212 | 5.34M | } |
3213 | | |
3214 | | /* create zipobject structure */ |
3215 | 73.1k | lz = (zipobject *)type->tp_alloc(type, 0); |
3216 | 73.1k | if (lz == NULL) { |
3217 | 0 | Py_DECREF(ittuple); |
3218 | 0 | Py_DECREF(result); |
3219 | 0 | return NULL; |
3220 | 0 | } |
3221 | 73.1k | lz->ittuple = ittuple; |
3222 | 73.1k | lz->tuplesize = tuplesize; |
3223 | 73.1k | lz->result = result; |
3224 | 73.1k | lz->strict = strict; |
3225 | | |
3226 | 73.1k | return (PyObject *)lz; |
3227 | 73.1k | } |
3228 | | |
3229 | | static void |
3230 | | zip_dealloc(PyObject *self) |
3231 | 73.1k | { |
3232 | 73.1k | zipobject *lz = _zipobject_CAST(self); |
3233 | 73.1k | PyObject_GC_UnTrack(lz); |
3234 | 73.1k | Py_XDECREF(lz->ittuple); |
3235 | 73.1k | Py_XDECREF(lz->result); |
3236 | 73.1k | Py_TYPE(lz)->tp_free(lz); |
3237 | 73.1k | } |
3238 | | |
3239 | | static int |
3240 | | zip_traverse(PyObject *self, visitproc visit, void *arg) |
3241 | 1.28k | { |
3242 | 1.28k | zipobject *lz = _zipobject_CAST(self); |
3243 | 1.28k | Py_VISIT(lz->ittuple); |
3244 | 1.28k | Py_VISIT(lz->result); |
3245 | 1.28k | return 0; |
3246 | 1.28k | } |
3247 | | |
3248 | | static PyObject * |
3249 | | zip_next(PyObject *self) |
3250 | 2.02M | { |
3251 | 2.02M | zipobject *lz = _zipobject_CAST(self); |
3252 | | |
3253 | 2.02M | Py_ssize_t i; |
3254 | 2.02M | Py_ssize_t tuplesize = lz->tuplesize; |
3255 | 2.02M | PyObject *result = lz->result; |
3256 | 2.02M | PyObject *it; |
3257 | 2.02M | PyObject *item; |
3258 | 2.02M | PyObject *olditem; |
3259 | | |
3260 | 2.02M | if (tuplesize == 0) |
3261 | 65 | return NULL; |
3262 | | |
3263 | 2.02M | if (_PyObject_IsUniquelyReferenced(result)) { |
3264 | 2.00M | Py_INCREF(result); |
3265 | 11.0M | for (i=0 ; i < tuplesize ; i++) { |
3266 | 9.14M | it = PyTuple_GET_ITEM(lz->ittuple, i); |
3267 | 9.14M | item = (*Py_TYPE(it)->tp_iternext)(it); |
3268 | 9.14M | if (item == NULL) { |
3269 | 67.0k | Py_DECREF(result); |
3270 | 67.0k | if (lz->strict) { |
3271 | 47.6k | goto check; |
3272 | 47.6k | } |
3273 | 19.4k | return NULL; |
3274 | 67.0k | } |
3275 | 9.08M | olditem = PyTuple_GET_ITEM(result, i); |
3276 | 9.08M | PyTuple_SET_ITEM(result, i, item); |
3277 | 9.08M | Py_DECREF(olditem); |
3278 | 9.08M | } |
3279 | | // bpo-42536: The GC may have untracked this result tuple. Since we're |
3280 | | // recycling it, make sure it's tracked again: |
3281 | 1.94M | _PyTuple_Recycle(result); |
3282 | 1.94M | } else { |
3283 | 14.3k | result = PyTuple_New(tuplesize); |
3284 | 14.3k | if (result == NULL) |
3285 | 0 | return NULL; |
3286 | 10.4M | for (i=0 ; i < tuplesize ; i++) { |
3287 | 10.4M | it = PyTuple_GET_ITEM(lz->ittuple, i); |
3288 | 10.4M | item = (*Py_TYPE(it)->tp_iternext)(it); |
3289 | 10.4M | if (item == NULL) { |
3290 | 4.50k | Py_DECREF(result); |
3291 | 4.50k | if (lz->strict) { |
3292 | 181 | goto check; |
3293 | 181 | } |
3294 | 4.32k | return NULL; |
3295 | 4.50k | } |
3296 | 10.4M | PyTuple_SET_ITEM(result, i, item); |
3297 | 10.4M | } |
3298 | 14.3k | } |
3299 | 1.95M | return result; |
3300 | 47.8k | check: |
3301 | 47.8k | if (PyErr_Occurred()) { |
3302 | 0 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) { |
3303 | | // next() on argument i raised an exception (not StopIteration) |
3304 | 0 | return NULL; |
3305 | 0 | } |
3306 | 0 | PyErr_Clear(); |
3307 | 0 | } |
3308 | 47.8k | if (i) { |
3309 | | // ValueError: zip() argument 2 is shorter than argument 1 |
3310 | | // ValueError: zip() argument 3 is shorter than arguments 1-2 |
3311 | 0 | const char* plural = i == 1 ? " " : "s 1-"; |
3312 | 0 | return PyErr_Format(PyExc_ValueError, |
3313 | 0 | "zip() argument %zd is shorter than argument%s%zd", |
3314 | 0 | i + 1, plural, i); |
3315 | 0 | } |
3316 | 95.6k | for (i = 1; i < tuplesize; i++) { |
3317 | 47.8k | it = PyTuple_GET_ITEM(lz->ittuple, i); |
3318 | 47.8k | item = (*Py_TYPE(it)->tp_iternext)(it); |
3319 | 47.8k | if (item) { |
3320 | 0 | Py_DECREF(item); |
3321 | 0 | const char* plural = i == 1 ? " " : "s 1-"; |
3322 | 0 | return PyErr_Format(PyExc_ValueError, |
3323 | 0 | "zip() argument %zd is longer than argument%s%zd", |
3324 | 0 | i + 1, plural, i); |
3325 | 0 | } |
3326 | 47.8k | if (PyErr_Occurred()) { |
3327 | 0 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) { |
3328 | | // next() on argument i raised an exception (not StopIteration) |
3329 | 0 | return NULL; |
3330 | 0 | } |
3331 | 0 | PyErr_Clear(); |
3332 | 0 | } |
3333 | | // Argument i is exhausted. So far so good... |
3334 | 47.8k | } |
3335 | | // All arguments are exhausted. Success! |
3336 | 47.8k | return NULL; |
3337 | 47.8k | } |
3338 | | |
3339 | | static PyObject * |
3340 | | zip_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
3341 | 0 | { |
3342 | 0 | zipobject *lz = _zipobject_CAST(self); |
3343 | | /* Just recreate the zip with the internal iterator tuple */ |
3344 | 0 | if (lz->strict) { |
3345 | 0 | return PyTuple_Pack(3, Py_TYPE(lz), lz->ittuple, Py_True); |
3346 | 0 | } |
3347 | 0 | return _PyTuple_FromPair((PyObject *)Py_TYPE(lz), lz->ittuple); |
3348 | 0 | } |
3349 | | |
3350 | | static PyObject * |
3351 | | zip_setstate(PyObject *self, PyObject *state) |
3352 | 0 | { |
3353 | 0 | int strict = PyObject_IsTrue(state); |
3354 | 0 | if (strict < 0) { |
3355 | 0 | return NULL; |
3356 | 0 | } |
3357 | 0 | zipobject *lz = _zipobject_CAST(self); |
3358 | 0 | lz->strict = strict; |
3359 | 0 | Py_RETURN_NONE; |
3360 | 0 | } |
3361 | | |
3362 | | static PyMethodDef zip_methods[] = { |
3363 | | {"__reduce__", zip_reduce, METH_NOARGS, reduce_doc}, |
3364 | | {"__setstate__", zip_setstate, METH_O, setstate_doc}, |
3365 | | {NULL} /* sentinel */ |
3366 | | }; |
3367 | | |
3368 | | PyDoc_STRVAR(zip_doc, |
3369 | | "zip(*iterables, strict=False)\n\ |
3370 | | --\n\ |
3371 | | \n\ |
3372 | | The zip object yields n-length tuples, where n is the number of\n\ |
3373 | | iterables passed as positional arguments to zip(). The i-th element\n\ |
3374 | | in every tuple comes from the i-th iterable argument to zip(). This\n\ |
3375 | | continues until the shortest argument is exhausted.\n\ |
3376 | | \n\ |
3377 | | If strict is true and one of the arguments is exhausted before the\n\ |
3378 | | others, raise a ValueError.\n\ |
3379 | | \n\ |
3380 | | >>> list(zip('abcdefg', range(3), range(4)))\n\ |
3381 | | [('a', 0, 0), ('b', 1, 1), ('c', 2, 2)]"); |
3382 | | |
3383 | | PyTypeObject PyZip_Type = { |
3384 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
3385 | | "zip", /* tp_name */ |
3386 | | sizeof(zipobject), /* tp_basicsize */ |
3387 | | 0, /* tp_itemsize */ |
3388 | | /* methods */ |
3389 | | zip_dealloc, /* tp_dealloc */ |
3390 | | 0, /* tp_vectorcall_offset */ |
3391 | | 0, /* tp_getattr */ |
3392 | | 0, /* tp_setattr */ |
3393 | | 0, /* tp_as_async */ |
3394 | | 0, /* tp_repr */ |
3395 | | 0, /* tp_as_number */ |
3396 | | 0, /* tp_as_sequence */ |
3397 | | 0, /* tp_as_mapping */ |
3398 | | 0, /* tp_hash */ |
3399 | | 0, /* tp_call */ |
3400 | | 0, /* tp_str */ |
3401 | | PyObject_GenericGetAttr, /* tp_getattro */ |
3402 | | 0, /* tp_setattro */ |
3403 | | 0, /* tp_as_buffer */ |
3404 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
3405 | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
3406 | | zip_doc, /* tp_doc */ |
3407 | | zip_traverse, /* tp_traverse */ |
3408 | | 0, /* tp_clear */ |
3409 | | 0, /* tp_richcompare */ |
3410 | | 0, /* tp_weaklistoffset */ |
3411 | | PyObject_SelfIter, /* tp_iter */ |
3412 | | zip_next, /* tp_iternext */ |
3413 | | zip_methods, /* tp_methods */ |
3414 | | 0, /* tp_members */ |
3415 | | 0, /* tp_getset */ |
3416 | | 0, /* tp_base */ |
3417 | | 0, /* tp_dict */ |
3418 | | 0, /* tp_descr_get */ |
3419 | | 0, /* tp_descr_set */ |
3420 | | 0, /* tp_dictoffset */ |
3421 | | 0, /* tp_init */ |
3422 | | PyType_GenericAlloc, /* tp_alloc */ |
3423 | | zip_new, /* tp_new */ |
3424 | | PyObject_GC_Del, /* tp_free */ |
3425 | | }; |
3426 | | |
3427 | | |
3428 | | static PyMethodDef builtin_methods[] = { |
3429 | | {"__build_class__", _PyCFunction_CAST(builtin___build_class__), |
3430 | | METH_FASTCALL | METH_KEYWORDS, build_class_doc}, |
3431 | | BUILTIN___IMPORT___METHODDEF |
3432 | | BUILTIN___LAZY_IMPORT___METHODDEF |
3433 | | BUILTIN_ABS_METHODDEF |
3434 | | BUILTIN_ALL_METHODDEF |
3435 | | BUILTIN_ANY_METHODDEF |
3436 | | BUILTIN_ASCII_METHODDEF |
3437 | | BUILTIN_BIN_METHODDEF |
3438 | | {"breakpoint", _PyCFunction_CAST(builtin_breakpoint), METH_FASTCALL | METH_KEYWORDS, breakpoint_doc}, |
3439 | | BUILTIN_CALLABLE_METHODDEF |
3440 | | BUILTIN_CHR_METHODDEF |
3441 | | BUILTIN_COMPILE_METHODDEF |
3442 | | BUILTIN_DELATTR_METHODDEF |
3443 | | BUILTIN_DIR_METHODDEF |
3444 | | BUILTIN_DIVMOD_METHODDEF |
3445 | | BUILTIN_EVAL_METHODDEF |
3446 | | BUILTIN_EXEC_METHODDEF |
3447 | | BUILTIN_FORMAT_METHODDEF |
3448 | | {"getattr", _PyCFunction_CAST(builtin_getattr), METH_FASTCALL, getattr_doc}, |
3449 | | BUILTIN_GLOBALS_METHODDEF |
3450 | | BUILTIN_HASATTR_METHODDEF |
3451 | | BUILTIN_HASH_METHODDEF |
3452 | | BUILTIN_HEX_METHODDEF |
3453 | | BUILTIN_ID_METHODDEF |
3454 | | BUILTIN_INPUT_METHODDEF |
3455 | | BUILTIN_ISINSTANCE_METHODDEF |
3456 | | BUILTIN_ISSUBCLASS_METHODDEF |
3457 | | BUILTIN_ITER_METHODDEF |
3458 | | BUILTIN_AITER_METHODDEF |
3459 | | BUILTIN_LEN_METHODDEF |
3460 | | BUILTIN_LOCALS_METHODDEF |
3461 | | {"max", _PyCFunction_CAST(builtin_max), METH_FASTCALL | METH_KEYWORDS, max_doc}, |
3462 | | {"min", _PyCFunction_CAST(builtin_min), METH_FASTCALL | METH_KEYWORDS, min_doc}, |
3463 | | {"next", _PyCFunction_CAST(builtin_next), METH_FASTCALL, next_doc}, |
3464 | | BUILTIN_OCT_METHODDEF |
3465 | | BUILTIN_ORD_METHODDEF |
3466 | | BUILTIN_POW_METHODDEF |
3467 | | BUILTIN_PRINT_METHODDEF |
3468 | | BUILTIN_REPR_METHODDEF |
3469 | | BUILTIN_ROUND_METHODDEF |
3470 | | BUILTIN_SETATTR_METHODDEF |
3471 | | BUILTIN_SORTED_METHODDEF |
3472 | | BUILTIN_SUM_METHODDEF |
3473 | | BUILTIN_VARS_METHODDEF |
3474 | | {NULL, NULL}, |
3475 | | }; |
3476 | | |
3477 | | PyDoc_STRVAR(builtin_doc, |
3478 | | "Built-in functions, types, exceptions, and other objects.\n\ |
3479 | | \n\ |
3480 | | This module provides direct access to all 'built-in'\n\ |
3481 | | identifiers of Python; for example, builtins.len is\n\ |
3482 | | the full name for the built-in function len().\n\ |
3483 | | \n\ |
3484 | | This module is not normally accessed explicitly by most\n\ |
3485 | | applications, but can be useful in modules that provide\n\ |
3486 | | objects with the same name as a built-in value, but in\n\ |
3487 | | which the built-in of that name is also needed."); |
3488 | | |
3489 | | static struct PyModuleDef builtinsmodule = { |
3490 | | PyModuleDef_HEAD_INIT, |
3491 | | "builtins", |
3492 | | builtin_doc, |
3493 | | -1, /* multiple "initialization" just copies the module dict. */ |
3494 | | builtin_methods, |
3495 | | NULL, |
3496 | | NULL, |
3497 | | NULL, |
3498 | | NULL |
3499 | | }; |
3500 | | |
3501 | | |
3502 | | /* Builtins implemented in Python. |
3503 | | |
3504 | | Lib/_pybuiltins.py is frozen into the interpreter as a bootstrap module |
3505 | | (see Tools/build/freeze_modules.py), so it can be imported here before |
3506 | | the import system exists. The names in its __all__ are copied into the |
3507 | | builtins dict. */ |
3508 | | |
3509 | | int |
3510 | | _PyBuiltin_InitPythonFunctions(PyObject *dict) |
3511 | 37 | { |
3512 | 37 | if (PyImport_ImportFrozenModule("_pybuiltins") <= 0) { |
3513 | 0 | if (!PyErr_Occurred()) { |
3514 | 0 | PyErr_SetString(PyExc_ImportError, |
3515 | 0 | "frozen module _pybuiltins not found"); |
3516 | 0 | } |
3517 | 0 | return -1; |
3518 | 0 | } |
3519 | 37 | PyObject *mod = PyImport_AddModuleRef("_pybuiltins"); |
3520 | 37 | if (mod == NULL) { |
3521 | 0 | return -1; |
3522 | 0 | } |
3523 | | |
3524 | 37 | int rc = -1; |
3525 | 37 | PyObject *all = PyObject_GetAttr(mod, &_Py_ID(__all__)); |
3526 | 37 | if (all == NULL) { |
3527 | 0 | goto done; |
3528 | 0 | } |
3529 | 37 | Py_ssize_t n = PyList_Size(all); |
3530 | 37 | if (n < 0) { |
3531 | 0 | goto done; |
3532 | 0 | } |
3533 | 74 | for (Py_ssize_t i = 0; i < n; i++) { |
3534 | 37 | PyObject *name = PyList_GET_ITEM(all, i); |
3535 | 37 | PyObject *func = PyObject_GetAttr(mod, name); |
3536 | 37 | if (func == NULL) { |
3537 | 0 | goto done; |
3538 | 0 | } |
3539 | 37 | int r = PyDict_SetItem(dict, name, func); |
3540 | 37 | Py_DECREF(func); |
3541 | 37 | if (r < 0) { |
3542 | 0 | goto done; |
3543 | 0 | } |
3544 | 37 | } |
3545 | 37 | rc = 0; |
3546 | | |
3547 | 37 | done: |
3548 | 37 | Py_XDECREF(all); |
3549 | 37 | Py_DECREF(mod); |
3550 | 37 | return rc; |
3551 | 37 | } |
3552 | | |
3553 | | PyObject * |
3554 | | _PyBuiltin_Init(PyInterpreterState *interp) |
3555 | 37 | { |
3556 | 37 | PyObject *mod, *dict, *debug; |
3557 | | |
3558 | 37 | const PyConfig *config = _PyInterpreterState_GetConfig(interp); |
3559 | | |
3560 | 37 | mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION); |
3561 | 37 | if (mod == NULL) |
3562 | 0 | return NULL; |
3563 | | #ifdef Py_GIL_DISABLED |
3564 | | PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED); |
3565 | | #endif |
3566 | 37 | dict = PyModule_GetDict(mod); |
3567 | | |
3568 | | #ifdef Py_TRACE_REFS |
3569 | | /* "builtins" exposes a number of statically allocated objects |
3570 | | * that, before this code was added in 2.3, never showed up in |
3571 | | * the list of "all objects" maintained by Py_TRACE_REFS. As a |
3572 | | * result, programs leaking references to None and False (etc) |
3573 | | * couldn't be diagnosed by examining sys.getobjects(0). |
3574 | | */ |
3575 | | #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT)) |
3576 | | #else |
3577 | 1.22k | #define ADD_TO_ALL(OBJECT) (void)0 |
3578 | 37 | #endif |
3579 | | |
3580 | 37 | #define SETBUILTIN(NAME, OBJECT) \ |
3581 | 1.22k | if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \ |
3582 | 1.22k | return NULL; \ |
3583 | 1.22k | ADD_TO_ALL(OBJECT) |
3584 | | |
3585 | 37 | SETBUILTIN("None", Py_None); |
3586 | 37 | SETBUILTIN("Ellipsis", Py_Ellipsis); |
3587 | 37 | SETBUILTIN("NotImplemented", Py_NotImplemented); |
3588 | 37 | SETBUILTIN("False", Py_False); |
3589 | 37 | SETBUILTIN("True", Py_True); |
3590 | 37 | SETBUILTIN("bool", &PyBool_Type); |
3591 | 37 | SETBUILTIN("memoryview", &PyMemoryView_Type); |
3592 | 37 | SETBUILTIN("bytearray", &PyByteArray_Type); |
3593 | 37 | SETBUILTIN("bytes", &PyBytes_Type); |
3594 | 37 | SETBUILTIN("classmethod", &PyClassMethod_Type); |
3595 | 37 | SETBUILTIN("complex", &PyComplex_Type); |
3596 | 37 | SETBUILTIN("dict", &PyDict_Type); |
3597 | 37 | SETBUILTIN("enumerate", &PyEnum_Type); |
3598 | 37 | SETBUILTIN("filter", &PyFilter_Type); |
3599 | 37 | SETBUILTIN("float", &PyFloat_Type); |
3600 | 37 | SETBUILTIN("frozendict", &PyFrozenDict_Type); |
3601 | 37 | SETBUILTIN("frozenset", &PyFrozenSet_Type); |
3602 | 37 | SETBUILTIN("property", &PyProperty_Type); |
3603 | 37 | SETBUILTIN("int", &PyLong_Type); |
3604 | 37 | SETBUILTIN("list", &PyList_Type); |
3605 | 37 | SETBUILTIN("map", &PyMap_Type); |
3606 | 37 | SETBUILTIN("object", &PyBaseObject_Type); |
3607 | 37 | SETBUILTIN("range", &PyRange_Type); |
3608 | 37 | SETBUILTIN("reversed", &PyReversed_Type); |
3609 | 37 | SETBUILTIN("sentinel", &PySentinel_Type); |
3610 | 37 | SETBUILTIN("set", &PySet_Type); |
3611 | 37 | SETBUILTIN("slice", &PySlice_Type); |
3612 | 37 | SETBUILTIN("staticmethod", &PyStaticMethod_Type); |
3613 | 37 | SETBUILTIN("str", &PyUnicode_Type); |
3614 | 37 | SETBUILTIN("super", &PySuper_Type); |
3615 | 37 | SETBUILTIN("tuple", &PyTuple_Type); |
3616 | 37 | SETBUILTIN("type", &PyType_Type); |
3617 | 37 | SETBUILTIN("zip", &PyZip_Type); |
3618 | 37 | debug = PyBool_FromLong(config->optimization_level == 0); |
3619 | 37 | if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { |
3620 | 0 | Py_DECREF(debug); |
3621 | 0 | return NULL; |
3622 | 0 | } |
3623 | 37 | Py_DECREF(debug); |
3624 | | |
3625 | 37 | return mod; |
3626 | 37 | #undef ADD_TO_ALL |
3627 | 37 | #undef SETBUILTIN |
3628 | 37 | } |