1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
10
11 PyDoc_STRVAR(builtin___import____doc__,
12 "__import__($module, /, name, globals=None, locals=None, fromlist=(),\n"
13 " level=0)\n"
14 "--\n"
15 "\n"
16 "Import a module.\n"
17 "\n"
18 "Because this function is meant for use by the Python\n"
19 "interpreter and not for general use, it is better to use\n"
20 "importlib.import_module() to programmatically import a module.\n"
21 "\n"
22 "The globals argument is only used to determine the context;\n"
23 "they are not modified. The locals argument is unused. The fromlist\n"
24 "should be a list of names to emulate ``from name import ...``, or an\n"
25 "empty list to emulate ``import name``.\n"
26 "When importing a module from a package, note that __import__(\'A.B\', ...)\n"
27 "returns package A when fromlist is empty, but its submodule B when\n"
28 "fromlist is not empty. The level argument is used to determine whether to\n"
29 "perform absolute or relative imports: 0 is absolute, while a positive number\n"
30 "is the number of parent directories to search relative to the current module.");
31
32 #define BUILTIN___IMPORT___METHODDEF \
33 {"__import__", _PyCFunction_CAST(builtin___import__), METH_FASTCALL|METH_KEYWORDS, builtin___import____doc__},
34
35 static PyObject *
36 builtin___import___impl(PyObject *module, PyObject *name, PyObject *globals,
37 PyObject *locals, PyObject *fromlist, int level);
38
39 static PyObject *
builtin___import__(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)40 builtin___import__(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
41 {
42 PyObject *return_value = NULL;
43 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
44
45 #define NUM_KEYWORDS 5
46 static struct {
47 PyGC_Head _this_is_not_used;
48 PyObject_VAR_HEAD
49 PyObject *ob_item[NUM_KEYWORDS];
50 } _kwtuple = {
51 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
52 .ob_item = { &_Py_ID(name), &_Py_ID(globals), &_Py_ID(locals), &_Py_ID(fromlist), &_Py_ID(level), },
53 };
54 #undef NUM_KEYWORDS
55 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
56
57 #else // !Py_BUILD_CORE
58 # define KWTUPLE NULL
59 #endif // !Py_BUILD_CORE
60
61 static const char * const _keywords[] = {"name", "globals", "locals", "fromlist", "level", NULL};
62 static _PyArg_Parser _parser = {
63 .keywords = _keywords,
64 .fname = "__import__",
65 .kwtuple = KWTUPLE,
66 };
67 #undef KWTUPLE
68 PyObject *argsbuf[5];
69 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
70 PyObject *name;
71 PyObject *globals = NULL;
72 PyObject *locals = NULL;
73 PyObject *fromlist = NULL;
74 int level = 0;
75
76 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 5, 0, argsbuf);
77 if (!args) {
78 goto exit;
79 }
80 name = args[0];
81 if (!noptargs) {
82 goto skip_optional_pos;
83 }
84 if (args[1]) {
85 globals = args[1];
86 if (!--noptargs) {
87 goto skip_optional_pos;
88 }
89 }
90 if (args[2]) {
91 locals = args[2];
92 if (!--noptargs) {
93 goto skip_optional_pos;
94 }
95 }
96 if (args[3]) {
97 fromlist = args[3];
98 if (!--noptargs) {
99 goto skip_optional_pos;
100 }
101 }
102 level = PyLong_AsInt(args[4]);
103 if (level == -1 && PyErr_Occurred()) {
104 goto exit;
105 }
106 skip_optional_pos:
107 return_value = builtin___import___impl(module, name, globals, locals, fromlist, level);
108
109 exit:
110 return return_value;
111 }
112
113 PyDoc_STRVAR(builtin_abs__doc__,
114 "abs($module, x, /)\n"
115 "--\n"
116 "\n"
117 "Return the absolute value of the argument.");
118
119 #define BUILTIN_ABS_METHODDEF \
120 {"abs", (PyCFunction)builtin_abs, METH_O, builtin_abs__doc__},
121
122 PyDoc_STRVAR(builtin_all__doc__,
123 "all($module, iterable, /)\n"
124 "--\n"
125 "\n"
126 "Return True if bool(x) is True for all values x in the iterable.\n"
127 "\n"
128 "If the iterable is empty, return True.");
129
130 #define BUILTIN_ALL_METHODDEF \
131 {"all", (PyCFunction)builtin_all, METH_O, builtin_all__doc__},
132
133 PyDoc_STRVAR(builtin_any__doc__,
134 "any($module, iterable, /)\n"
135 "--\n"
136 "\n"
137 "Return True if bool(x) is True for any x in the iterable.\n"
138 "\n"
139 "If the iterable is empty, return False.");
140
141 #define BUILTIN_ANY_METHODDEF \
142 {"any", (PyCFunction)builtin_any, METH_O, builtin_any__doc__},
143
144 PyDoc_STRVAR(builtin_ascii__doc__,
145 "ascii($module, obj, /)\n"
146 "--\n"
147 "\n"
148 "Return an ASCII-only representation of an object.\n"
149 "\n"
150 "As repr(), return a string containing a printable representation of an\n"
151 "object, but escape the non-ASCII characters in the string returned by\n"
152 "repr() using \\\\x, \\\\u or \\\\U escapes. This generates a string similar\n"
153 "to that returned by repr() in Python 2.");
154
155 #define BUILTIN_ASCII_METHODDEF \
156 {"ascii", (PyCFunction)builtin_ascii, METH_O, builtin_ascii__doc__},
157
158 PyDoc_STRVAR(builtin_bin__doc__,
159 "bin($module, number, /)\n"
160 "--\n"
161 "\n"
162 "Return the binary representation of an integer.\n"
163 "\n"
164 " >>> bin(2796202)\n"
165 " \'0b1010101010101010101010\'");
166
167 #define BUILTIN_BIN_METHODDEF \
168 {"bin", (PyCFunction)builtin_bin, METH_O, builtin_bin__doc__},
169
170 PyDoc_STRVAR(builtin_callable__doc__,
171 "callable($module, obj, /)\n"
172 "--\n"
173 "\n"
174 "Return whether the object is callable (i.e., some kind of function).\n"
175 "\n"
176 "Note that classes are callable, as are instances of classes with a\n"
177 "__call__() method.");
178
179 #define BUILTIN_CALLABLE_METHODDEF \
180 {"callable", (PyCFunction)builtin_callable, METH_O, builtin_callable__doc__},
181
182 PyDoc_STRVAR(builtin_format__doc__,
183 "format($module, value, format_spec=\'\', /)\n"
184 "--\n"
185 "\n"
186 "Return type(value).__format__(value, format_spec)\n"
187 "\n"
188 "Many built-in types implement format_spec according to the\n"
189 "Format Specification Mini-language. See help(\'FORMATTING\').\n"
190 "\n"
191 "If type(value) does not supply a method named __format__\n"
192 "and format_spec is empty, then str(value) is returned.\n"
193 "See also help(\'SPECIALMETHODS\').");
194
195 #define BUILTIN_FORMAT_METHODDEF \
196 {"format", _PyCFunction_CAST(builtin_format), METH_FASTCALL, builtin_format__doc__},
197
198 static PyObject *
199 builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec);
200
201 static PyObject *
builtin_format(PyObject * module,PyObject * const * args,Py_ssize_t nargs)202 builtin_format(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
203 {
204 PyObject *return_value = NULL;
205 PyObject *value;
206 PyObject *format_spec = NULL;
207
208 if (!_PyArg_CheckPositional("format", nargs, 1, 2)) {
209 goto exit;
210 }
211 value = args[0];
212 if (nargs < 2) {
213 goto skip_optional;
214 }
215 if (!PyUnicode_Check(args[1])) {
216 _PyArg_BadArgument("format", "argument 2", "str", args[1]);
217 goto exit;
218 }
219 format_spec = args[1];
220 skip_optional:
221 return_value = builtin_format_impl(module, value, format_spec);
222
223 exit:
224 return return_value;
225 }
226
227 PyDoc_STRVAR(builtin_chr__doc__,
228 "chr($module, i, /)\n"
229 "--\n"
230 "\n"
231 "Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
232
233 #define BUILTIN_CHR_METHODDEF \
234 {"chr", (PyCFunction)builtin_chr, METH_O, builtin_chr__doc__},
235
236 PyDoc_STRVAR(builtin_compile__doc__,
237 "compile($module, /, source, filename, mode, flags=0,\n"
238 " dont_inherit=False, optimize=-1, *, _feature_version=-1)\n"
239 "--\n"
240 "\n"
241 "Compile source into a code object that can be executed by exec() or eval().\n"
242 "\n"
243 "The source code may represent a Python module, statement or expression.\n"
244 "The filename will be used for run-time error messages.\n"
245 "The mode must be \'exec\' to compile a module, \'single\' to compile a\n"
246 "single (interactive) statement, or \'eval\' to compile an expression.\n"
247 "The flags argument, if present, controls which future statements influence\n"
248 "the compilation of the code.\n"
249 "The dont_inherit argument, if true, stops the compilation inheriting\n"
250 "the effects of any future statements in effect in the code calling\n"
251 "compile; if absent or false these statements do influence the compilation,\n"
252 "in addition to any features explicitly specified.");
253
254 #define BUILTIN_COMPILE_METHODDEF \
255 {"compile", _PyCFunction_CAST(builtin_compile), METH_FASTCALL|METH_KEYWORDS, builtin_compile__doc__},
256
257 static PyObject *
258 builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
259 const char *mode, int flags, int dont_inherit,
260 int optimize, int feature_version);
261
262 static PyObject *
builtin_compile(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)263 builtin_compile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
264 {
265 PyObject *return_value = NULL;
266 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
267
268 #define NUM_KEYWORDS 7
269 static struct {
270 PyGC_Head _this_is_not_used;
271 PyObject_VAR_HEAD
272 PyObject *ob_item[NUM_KEYWORDS];
273 } _kwtuple = {
274 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
275 .ob_item = { &_Py_ID(source), &_Py_ID(filename), &_Py_ID(mode), &_Py_ID(flags), &_Py_ID(dont_inherit), &_Py_ID(optimize), &_Py_ID(_feature_version), },
276 };
277 #undef NUM_KEYWORDS
278 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
279
280 #else // !Py_BUILD_CORE
281 # define KWTUPLE NULL
282 #endif // !Py_BUILD_CORE
283
284 static const char * const _keywords[] = {"source", "filename", "mode", "flags", "dont_inherit", "optimize", "_feature_version", NULL};
285 static _PyArg_Parser _parser = {
286 .keywords = _keywords,
287 .fname = "compile",
288 .kwtuple = KWTUPLE,
289 };
290 #undef KWTUPLE
291 PyObject *argsbuf[7];
292 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
293 PyObject *source;
294 PyObject *filename;
295 const char *mode;
296 int flags = 0;
297 int dont_inherit = 0;
298 int optimize = -1;
299 int feature_version = -1;
300
301 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 6, 0, argsbuf);
302 if (!args) {
303 goto exit;
304 }
305 source = args[0];
306 if (!PyUnicode_FSDecoder(args[1], &filename)) {
307 goto exit;
308 }
309 if (!PyUnicode_Check(args[2])) {
310 _PyArg_BadArgument("compile", "argument 'mode'", "str", args[2]);
311 goto exit;
312 }
313 Py_ssize_t mode_length;
314 mode = PyUnicode_AsUTF8AndSize(args[2], &mode_length);
315 if (mode == NULL) {
316 goto exit;
317 }
318 if (strlen(mode) != (size_t)mode_length) {
319 PyErr_SetString(PyExc_ValueError, "embedded null character");
320 goto exit;
321 }
322 if (!noptargs) {
323 goto skip_optional_pos;
324 }
325 if (args[3]) {
326 flags = PyLong_AsInt(args[3]);
327 if (flags == -1 && PyErr_Occurred()) {
328 goto exit;
329 }
330 if (!--noptargs) {
331 goto skip_optional_pos;
332 }
333 }
334 if (args[4]) {
335 dont_inherit = PyObject_IsTrue(args[4]);
336 if (dont_inherit < 0) {
337 goto exit;
338 }
339 if (!--noptargs) {
340 goto skip_optional_pos;
341 }
342 }
343 if (args[5]) {
344 optimize = PyLong_AsInt(args[5]);
345 if (optimize == -1 && PyErr_Occurred()) {
346 goto exit;
347 }
348 if (!--noptargs) {
349 goto skip_optional_pos;
350 }
351 }
352 skip_optional_pos:
353 if (!noptargs) {
354 goto skip_optional_kwonly;
355 }
356 feature_version = PyLong_AsInt(args[6]);
357 if (feature_version == -1 && PyErr_Occurred()) {
358 goto exit;
359 }
360 skip_optional_kwonly:
361 return_value = builtin_compile_impl(module, source, filename, mode, flags, dont_inherit, optimize, feature_version);
362
363 exit:
364 return return_value;
365 }
366
367 PyDoc_STRVAR(builtin_divmod__doc__,
368 "divmod($module, x, y, /)\n"
369 "--\n"
370 "\n"
371 "Return the tuple (x//y, x%y). Invariant: div*y + mod == x.");
372
373 #define BUILTIN_DIVMOD_METHODDEF \
374 {"divmod", _PyCFunction_CAST(builtin_divmod), METH_FASTCALL, builtin_divmod__doc__},
375
376 static PyObject *
377 builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y);
378
379 static PyObject *
builtin_divmod(PyObject * module,PyObject * const * args,Py_ssize_t nargs)380 builtin_divmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
381 {
382 PyObject *return_value = NULL;
383 PyObject *x;
384 PyObject *y;
385
386 if (!_PyArg_CheckPositional("divmod", nargs, 2, 2)) {
387 goto exit;
388 }
389 x = args[0];
390 y = args[1];
391 return_value = builtin_divmod_impl(module, x, y);
392
393 exit:
394 return return_value;
395 }
396
397 PyDoc_STRVAR(builtin_eval__doc__,
398 "eval($module, source, /, globals=None, locals=None)\n"
399 "--\n"
400 "\n"
401 "Evaluate the given source in the context of globals and locals.\n"
402 "\n"
403 "The source may be a string representing a Python expression\n"
404 "or a code object as returned by compile().\n"
405 "The globals must be a dictionary and locals can be any mapping,\n"
406 "defaulting to the current globals and locals.\n"
407 "If only globals is given, locals defaults to it.");
408
409 #define BUILTIN_EVAL_METHODDEF \
410 {"eval", _PyCFunction_CAST(builtin_eval), METH_FASTCALL|METH_KEYWORDS, builtin_eval__doc__},
411
412 static PyObject *
413 builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
414 PyObject *locals);
415
416 static PyObject *
builtin_eval(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)417 builtin_eval(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
418 {
419 PyObject *return_value = NULL;
420 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
421
422 #define NUM_KEYWORDS 2
423 static struct {
424 PyGC_Head _this_is_not_used;
425 PyObject_VAR_HEAD
426 PyObject *ob_item[NUM_KEYWORDS];
427 } _kwtuple = {
428 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
429 .ob_item = { &_Py_ID(globals), &_Py_ID(locals), },
430 };
431 #undef NUM_KEYWORDS
432 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
433
434 #else // !Py_BUILD_CORE
435 # define KWTUPLE NULL
436 #endif // !Py_BUILD_CORE
437
438 static const char * const _keywords[] = {"", "globals", "locals", NULL};
439 static _PyArg_Parser _parser = {
440 .keywords = _keywords,
441 .fname = "eval",
442 .kwtuple = KWTUPLE,
443 };
444 #undef KWTUPLE
445 PyObject *argsbuf[3];
446 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
447 PyObject *source;
448 PyObject *globals = Py_None;
449 PyObject *locals = Py_None;
450
451 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
452 if (!args) {
453 goto exit;
454 }
455 source = args[0];
456 if (!noptargs) {
457 goto skip_optional_pos;
458 }
459 if (args[1]) {
460 globals = args[1];
461 if (!--noptargs) {
462 goto skip_optional_pos;
463 }
464 }
465 locals = args[2];
466 skip_optional_pos:
467 return_value = builtin_eval_impl(module, source, globals, locals);
468
469 exit:
470 return return_value;
471 }
472
473 PyDoc_STRVAR(builtin_exec__doc__,
474 "exec($module, source, /, globals=None, locals=None, *, closure=None)\n"
475 "--\n"
476 "\n"
477 "Execute the given source in the context of globals and locals.\n"
478 "\n"
479 "The source may be a string representing one or more Python statements\n"
480 "or a code object as returned by compile().\n"
481 "The globals must be a dictionary and locals can be any mapping,\n"
482 "defaulting to the current globals and locals.\n"
483 "If only globals is given, locals defaults to it.\n"
484 "The closure must be a tuple of cellvars, and can only be used\n"
485 "when source is a code object requiring exactly that many cellvars.");
486
487 #define BUILTIN_EXEC_METHODDEF \
488 {"exec", _PyCFunction_CAST(builtin_exec), METH_FASTCALL|METH_KEYWORDS, builtin_exec__doc__},
489
490 static PyObject *
491 builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
492 PyObject *locals, PyObject *closure);
493
494 static PyObject *
builtin_exec(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)495 builtin_exec(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
496 {
497 PyObject *return_value = NULL;
498 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
499
500 #define NUM_KEYWORDS 3
501 static struct {
502 PyGC_Head _this_is_not_used;
503 PyObject_VAR_HEAD
504 PyObject *ob_item[NUM_KEYWORDS];
505 } _kwtuple = {
506 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
507 .ob_item = { &_Py_ID(globals), &_Py_ID(locals), &_Py_ID(closure), },
508 };
509 #undef NUM_KEYWORDS
510 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
511
512 #else // !Py_BUILD_CORE
513 # define KWTUPLE NULL
514 #endif // !Py_BUILD_CORE
515
516 static const char * const _keywords[] = {"", "globals", "locals", "closure", NULL};
517 static _PyArg_Parser _parser = {
518 .keywords = _keywords,
519 .fname = "exec",
520 .kwtuple = KWTUPLE,
521 };
522 #undef KWTUPLE
523 PyObject *argsbuf[4];
524 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
525 PyObject *source;
526 PyObject *globals = Py_None;
527 PyObject *locals = Py_None;
528 PyObject *closure = NULL;
529
530 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
531 if (!args) {
532 goto exit;
533 }
534 source = args[0];
535 if (!noptargs) {
536 goto skip_optional_pos;
537 }
538 if (args[1]) {
539 globals = args[1];
540 if (!--noptargs) {
541 goto skip_optional_pos;
542 }
543 }
544 if (args[2]) {
545 locals = args[2];
546 if (!--noptargs) {
547 goto skip_optional_pos;
548 }
549 }
550 skip_optional_pos:
551 if (!noptargs) {
552 goto skip_optional_kwonly;
553 }
554 closure = args[3];
555 skip_optional_kwonly:
556 return_value = builtin_exec_impl(module, source, globals, locals, closure);
557
558 exit:
559 return return_value;
560 }
561
562 PyDoc_STRVAR(builtin_globals__doc__,
563 "globals($module, /)\n"
564 "--\n"
565 "\n"
566 "Return the dictionary containing the current scope\'s global variables.\n"
567 "\n"
568 "NOTE: Updates to this dictionary *will* affect name lookups in the current\n"
569 "global scope and vice-versa.");
570
571 #define BUILTIN_GLOBALS_METHODDEF \
572 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, builtin_globals__doc__},
573
574 static PyObject *
575 builtin_globals_impl(PyObject *module);
576
577 static PyObject *
builtin_globals(PyObject * module,PyObject * Py_UNUSED (ignored))578 builtin_globals(PyObject *module, PyObject *Py_UNUSED(ignored))
579 {
580 return builtin_globals_impl(module);
581 }
582
583 PyDoc_STRVAR(builtin_hasattr__doc__,
584 "hasattr($module, obj, name, /)\n"
585 "--\n"
586 "\n"
587 "Return whether the object has an attribute with the given name.\n"
588 "\n"
589 "This is done by calling getattr(obj, name) and catching AttributeError.");
590
591 #define BUILTIN_HASATTR_METHODDEF \
592 {"hasattr", _PyCFunction_CAST(builtin_hasattr), METH_FASTCALL, builtin_hasattr__doc__},
593
594 static PyObject *
595 builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name);
596
597 static PyObject *
builtin_hasattr(PyObject * module,PyObject * const * args,Py_ssize_t nargs)598 builtin_hasattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
599 {
600 PyObject *return_value = NULL;
601 PyObject *obj;
602 PyObject *name;
603
604 if (!_PyArg_CheckPositional("hasattr", nargs, 2, 2)) {
605 goto exit;
606 }
607 obj = args[0];
608 name = args[1];
609 return_value = builtin_hasattr_impl(module, obj, name);
610
611 exit:
612 return return_value;
613 }
614
615 PyDoc_STRVAR(builtin_id__doc__,
616 "id($module, obj, /)\n"
617 "--\n"
618 "\n"
619 "Return the identity of an object.\n"
620 "\n"
621 "This is guaranteed to be unique among simultaneously existing objects.\n"
622 "(CPython uses the object\'s memory address.)");
623
624 #define BUILTIN_ID_METHODDEF \
625 {"id", (PyCFunction)builtin_id, METH_O, builtin_id__doc__},
626
627 PyDoc_STRVAR(builtin_setattr__doc__,
628 "setattr($module, obj, name, value, /)\n"
629 "--\n"
630 "\n"
631 "Sets the named attribute on the given object to the specified value.\n"
632 "\n"
633 "setattr(x, \'y\', v) is equivalent to ``x.y = v``");
634
635 #define BUILTIN_SETATTR_METHODDEF \
636 {"setattr", _PyCFunction_CAST(builtin_setattr), METH_FASTCALL, builtin_setattr__doc__},
637
638 static PyObject *
639 builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
640 PyObject *value);
641
642 static PyObject *
builtin_setattr(PyObject * module,PyObject * const * args,Py_ssize_t nargs)643 builtin_setattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
644 {
645 PyObject *return_value = NULL;
646 PyObject *obj;
647 PyObject *name;
648 PyObject *value;
649
650 if (!_PyArg_CheckPositional("setattr", nargs, 3, 3)) {
651 goto exit;
652 }
653 obj = args[0];
654 name = args[1];
655 value = args[2];
656 return_value = builtin_setattr_impl(module, obj, name, value);
657
658 exit:
659 return return_value;
660 }
661
662 PyDoc_STRVAR(builtin_delattr__doc__,
663 "delattr($module, obj, name, /)\n"
664 "--\n"
665 "\n"
666 "Deletes the named attribute from the given object.\n"
667 "\n"
668 "delattr(x, \'y\') is equivalent to ``del x.y``");
669
670 #define BUILTIN_DELATTR_METHODDEF \
671 {"delattr", _PyCFunction_CAST(builtin_delattr), METH_FASTCALL, builtin_delattr__doc__},
672
673 static PyObject *
674 builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name);
675
676 static PyObject *
builtin_delattr(PyObject * module,PyObject * const * args,Py_ssize_t nargs)677 builtin_delattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
678 {
679 PyObject *return_value = NULL;
680 PyObject *obj;
681 PyObject *name;
682
683 if (!_PyArg_CheckPositional("delattr", nargs, 2, 2)) {
684 goto exit;
685 }
686 obj = args[0];
687 name = args[1];
688 return_value = builtin_delattr_impl(module, obj, name);
689
690 exit:
691 return return_value;
692 }
693
694 PyDoc_STRVAR(builtin_hash__doc__,
695 "hash($module, obj, /)\n"
696 "--\n"
697 "\n"
698 "Return the hash value for the given object.\n"
699 "\n"
700 "Two objects that compare equal must also have the same hash value, but the\n"
701 "reverse is not necessarily true.");
702
703 #define BUILTIN_HASH_METHODDEF \
704 {"hash", (PyCFunction)builtin_hash, METH_O, builtin_hash__doc__},
705
706 PyDoc_STRVAR(builtin_hex__doc__,
707 "hex($module, number, /)\n"
708 "--\n"
709 "\n"
710 "Return the hexadecimal representation of an integer.\n"
711 "\n"
712 " >>> hex(12648430)\n"
713 " \'0xc0ffee\'");
714
715 #define BUILTIN_HEX_METHODDEF \
716 {"hex", (PyCFunction)builtin_hex, METH_O, builtin_hex__doc__},
717
718 PyDoc_STRVAR(builtin_aiter__doc__,
719 "aiter($module, async_iterable, /)\n"
720 "--\n"
721 "\n"
722 "Return an AsyncIterator for an AsyncIterable object.");
723
724 #define BUILTIN_AITER_METHODDEF \
725 {"aiter", (PyCFunction)builtin_aiter, METH_O, builtin_aiter__doc__},
726
727 PyDoc_STRVAR(builtin_anext__doc__,
728 "anext($module, aiterator, default=<unrepresentable>, /)\n"
729 "--\n"
730 "\n"
731 "Return the next item from the async iterator.\n"
732 "\n"
733 "If default is given and the async iterator is exhausted,\n"
734 "it is returned instead of raising StopAsyncIteration.");
735
736 #define BUILTIN_ANEXT_METHODDEF \
737 {"anext", _PyCFunction_CAST(builtin_anext), METH_FASTCALL, builtin_anext__doc__},
738
739 static PyObject *
740 builtin_anext_impl(PyObject *module, PyObject *aiterator,
741 PyObject *default_value);
742
743 static PyObject *
builtin_anext(PyObject * module,PyObject * const * args,Py_ssize_t nargs)744 builtin_anext(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
745 {
746 PyObject *return_value = NULL;
747 PyObject *aiterator;
748 PyObject *default_value = NULL;
749
750 if (!_PyArg_CheckPositional("anext", nargs, 1, 2)) {
751 goto exit;
752 }
753 aiterator = args[0];
754 if (nargs < 2) {
755 goto skip_optional;
756 }
757 default_value = args[1];
758 skip_optional:
759 return_value = builtin_anext_impl(module, aiterator, default_value);
760
761 exit:
762 return return_value;
763 }
764
765 PyDoc_STRVAR(builtin_len__doc__,
766 "len($module, obj, /)\n"
767 "--\n"
768 "\n"
769 "Return the number of items in a container.");
770
771 #define BUILTIN_LEN_METHODDEF \
772 {"len", (PyCFunction)builtin_len, METH_O, builtin_len__doc__},
773
774 PyDoc_STRVAR(builtin_locals__doc__,
775 "locals($module, /)\n"
776 "--\n"
777 "\n"
778 "Return a dictionary containing the current scope\'s local variables.\n"
779 "\n"
780 "NOTE: Whether or not updates to this dictionary will affect name lookups in\n"
781 "the local scope and vice-versa is *implementation dependent* and not\n"
782 "covered by any backwards compatibility guarantees.");
783
784 #define BUILTIN_LOCALS_METHODDEF \
785 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, builtin_locals__doc__},
786
787 static PyObject *
788 builtin_locals_impl(PyObject *module);
789
790 static PyObject *
builtin_locals(PyObject * module,PyObject * Py_UNUSED (ignored))791 builtin_locals(PyObject *module, PyObject *Py_UNUSED(ignored))
792 {
793 return builtin_locals_impl(module);
794 }
795
796 PyDoc_STRVAR(builtin_oct__doc__,
797 "oct($module, number, /)\n"
798 "--\n"
799 "\n"
800 "Return the octal representation of an integer.\n"
801 "\n"
802 " >>> oct(342391)\n"
803 " \'0o1234567\'");
804
805 #define BUILTIN_OCT_METHODDEF \
806 {"oct", (PyCFunction)builtin_oct, METH_O, builtin_oct__doc__},
807
808 PyDoc_STRVAR(builtin_ord__doc__,
809 "ord($module, c, /)\n"
810 "--\n"
811 "\n"
812 "Return the Unicode code point for a one-character string.");
813
814 #define BUILTIN_ORD_METHODDEF \
815 {"ord", (PyCFunction)builtin_ord, METH_O, builtin_ord__doc__},
816
817 PyDoc_STRVAR(builtin_pow__doc__,
818 "pow($module, /, base, exp, mod=None)\n"
819 "--\n"
820 "\n"
821 "Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments\n"
822 "\n"
823 "Some types, such as ints, are able to use a more efficient algorithm when\n"
824 "invoked using the three argument form.");
825
826 #define BUILTIN_POW_METHODDEF \
827 {"pow", _PyCFunction_CAST(builtin_pow), METH_FASTCALL|METH_KEYWORDS, builtin_pow__doc__},
828
829 static PyObject *
830 builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp,
831 PyObject *mod);
832
833 static PyObject *
builtin_pow(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)834 builtin_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
835 {
836 PyObject *return_value = NULL;
837 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
838
839 #define NUM_KEYWORDS 3
840 static struct {
841 PyGC_Head _this_is_not_used;
842 PyObject_VAR_HEAD
843 PyObject *ob_item[NUM_KEYWORDS];
844 } _kwtuple = {
845 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
846 .ob_item = { &_Py_ID(base), &_Py_ID(exp), &_Py_ID(mod), },
847 };
848 #undef NUM_KEYWORDS
849 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
850
851 #else // !Py_BUILD_CORE
852 # define KWTUPLE NULL
853 #endif // !Py_BUILD_CORE
854
855 static const char * const _keywords[] = {"base", "exp", "mod", NULL};
856 static _PyArg_Parser _parser = {
857 .keywords = _keywords,
858 .fname = "pow",
859 .kwtuple = KWTUPLE,
860 };
861 #undef KWTUPLE
862 PyObject *argsbuf[3];
863 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
864 PyObject *base;
865 PyObject *exp;
866 PyObject *mod = Py_None;
867
868 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
869 if (!args) {
870 goto exit;
871 }
872 base = args[0];
873 exp = args[1];
874 if (!noptargs) {
875 goto skip_optional_pos;
876 }
877 mod = args[2];
878 skip_optional_pos:
879 return_value = builtin_pow_impl(module, base, exp, mod);
880
881 exit:
882 return return_value;
883 }
884
885 PyDoc_STRVAR(builtin_print__doc__,
886 "print($module, /, *args, sep=\' \', end=\'\\n\', file=None, flush=False)\n"
887 "--\n"
888 "\n"
889 "Prints the values to a stream, or to sys.stdout by default.\n"
890 "\n"
891 " sep\n"
892 " string inserted between values, default a space.\n"
893 " end\n"
894 " string appended after the last value, default a newline.\n"
895 " file\n"
896 " a file-like object (stream); defaults to the current sys.stdout.\n"
897 " flush\n"
898 " whether to forcibly flush the stream.");
899
900 #define BUILTIN_PRINT_METHODDEF \
901 {"print", _PyCFunction_CAST(builtin_print), METH_FASTCALL|METH_KEYWORDS, builtin_print__doc__},
902
903 static PyObject *
904 builtin_print_impl(PyObject *module, PyObject *args, PyObject *sep,
905 PyObject *end, PyObject *file, int flush);
906
907 static PyObject *
builtin_print(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)908 builtin_print(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
909 {
910 PyObject *return_value = NULL;
911 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
912
913 #define NUM_KEYWORDS 4
914 static struct {
915 PyGC_Head _this_is_not_used;
916 PyObject_VAR_HEAD
917 PyObject *ob_item[NUM_KEYWORDS];
918 } _kwtuple = {
919 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
920 .ob_item = { &_Py_ID(sep), &_Py_ID(end), &_Py_ID(file), &_Py_ID(flush), },
921 };
922 #undef NUM_KEYWORDS
923 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
924
925 #else // !Py_BUILD_CORE
926 # define KWTUPLE NULL
927 #endif // !Py_BUILD_CORE
928
929 static const char * const _keywords[] = {"sep", "end", "file", "flush", NULL};
930 static _PyArg_Parser _parser = {
931 .keywords = _keywords,
932 .fname = "print",
933 .kwtuple = KWTUPLE,
934 };
935 #undef KWTUPLE
936 PyObject *argsbuf[5];
937 Py_ssize_t noptargs = 0 + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
938 PyObject *__clinic_args = NULL;
939 PyObject *sep = Py_None;
940 PyObject *end = Py_None;
941 PyObject *file = Py_None;
942 int flush = 0;
943
944 args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, 0, argsbuf);
945 if (!args) {
946 goto exit;
947 }
948 __clinic_args = args[0];
949 if (!noptargs) {
950 goto skip_optional_kwonly;
951 }
952 if (args[1]) {
953 sep = args[1];
954 if (!--noptargs) {
955 goto skip_optional_kwonly;
956 }
957 }
958 if (args[2]) {
959 end = args[2];
960 if (!--noptargs) {
961 goto skip_optional_kwonly;
962 }
963 }
964 if (args[3]) {
965 file = args[3];
966 if (!--noptargs) {
967 goto skip_optional_kwonly;
968 }
969 }
970 flush = PyObject_IsTrue(args[4]);
971 if (flush < 0) {
972 goto exit;
973 }
974 skip_optional_kwonly:
975 return_value = builtin_print_impl(module, __clinic_args, sep, end, file, flush);
976
977 exit:
978 Py_XDECREF(__clinic_args);
979 return return_value;
980 }
981
982 PyDoc_STRVAR(builtin_input__doc__,
983 "input($module, prompt=\'\', /)\n"
984 "--\n"
985 "\n"
986 "Read a string from standard input. The trailing newline is stripped.\n"
987 "\n"
988 "The prompt string, if given, is printed to standard output without a\n"
989 "trailing newline before reading input.\n"
990 "\n"
991 "If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.\n"
992 "On *nix systems, readline is used if available.");
993
994 #define BUILTIN_INPUT_METHODDEF \
995 {"input", _PyCFunction_CAST(builtin_input), METH_FASTCALL, builtin_input__doc__},
996
997 static PyObject *
998 builtin_input_impl(PyObject *module, PyObject *prompt);
999
1000 static PyObject *
builtin_input(PyObject * module,PyObject * const * args,Py_ssize_t nargs)1001 builtin_input(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
1002 {
1003 PyObject *return_value = NULL;
1004 PyObject *prompt = NULL;
1005
1006 if (!_PyArg_CheckPositional("input", nargs, 0, 1)) {
1007 goto exit;
1008 }
1009 if (nargs < 1) {
1010 goto skip_optional;
1011 }
1012 prompt = args[0];
1013 skip_optional:
1014 return_value = builtin_input_impl(module, prompt);
1015
1016 exit:
1017 return return_value;
1018 }
1019
1020 PyDoc_STRVAR(builtin_repr__doc__,
1021 "repr($module, obj, /)\n"
1022 "--\n"
1023 "\n"
1024 "Return the canonical string representation of the object.\n"
1025 "\n"
1026 "For many object types, including most builtins, eval(repr(obj)) == obj.");
1027
1028 #define BUILTIN_REPR_METHODDEF \
1029 {"repr", (PyCFunction)builtin_repr, METH_O, builtin_repr__doc__},
1030
1031 PyDoc_STRVAR(builtin_round__doc__,
1032 "round($module, /, number, ndigits=None)\n"
1033 "--\n"
1034 "\n"
1035 "Round a number to a given precision in decimal digits.\n"
1036 "\n"
1037 "The return value is an integer if ndigits is omitted or None. Otherwise\n"
1038 "the return value has the same type as the number. ndigits may be negative.");
1039
1040 #define BUILTIN_ROUND_METHODDEF \
1041 {"round", _PyCFunction_CAST(builtin_round), METH_FASTCALL|METH_KEYWORDS, builtin_round__doc__},
1042
1043 static PyObject *
1044 builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits);
1045
1046 static PyObject *
builtin_round(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1047 builtin_round(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1048 {
1049 PyObject *return_value = NULL;
1050 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1051
1052 #define NUM_KEYWORDS 2
1053 static struct {
1054 PyGC_Head _this_is_not_used;
1055 PyObject_VAR_HEAD
1056 PyObject *ob_item[NUM_KEYWORDS];
1057 } _kwtuple = {
1058 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1059 .ob_item = { &_Py_ID(number), &_Py_ID(ndigits), },
1060 };
1061 #undef NUM_KEYWORDS
1062 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1063
1064 #else // !Py_BUILD_CORE
1065 # define KWTUPLE NULL
1066 #endif // !Py_BUILD_CORE
1067
1068 static const char * const _keywords[] = {"number", "ndigits", NULL};
1069 static _PyArg_Parser _parser = {
1070 .keywords = _keywords,
1071 .fname = "round",
1072 .kwtuple = KWTUPLE,
1073 };
1074 #undef KWTUPLE
1075 PyObject *argsbuf[2];
1076 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1077 PyObject *number;
1078 PyObject *ndigits = Py_None;
1079
1080 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
1081 if (!args) {
1082 goto exit;
1083 }
1084 number = args[0];
1085 if (!noptargs) {
1086 goto skip_optional_pos;
1087 }
1088 ndigits = args[1];
1089 skip_optional_pos:
1090 return_value = builtin_round_impl(module, number, ndigits);
1091
1092 exit:
1093 return return_value;
1094 }
1095
1096 PyDoc_STRVAR(builtin_sum__doc__,
1097 "sum($module, iterable, /, start=0)\n"
1098 "--\n"
1099 "\n"
1100 "Return the sum of a \'start\' value (default: 0) plus an iterable of numbers\n"
1101 "\n"
1102 "When the iterable is empty, return the start value.\n"
1103 "This function is intended specifically for use with numeric values and may\n"
1104 "reject non-numeric types.");
1105
1106 #define BUILTIN_SUM_METHODDEF \
1107 {"sum", _PyCFunction_CAST(builtin_sum), METH_FASTCALL|METH_KEYWORDS, builtin_sum__doc__},
1108
1109 static PyObject *
1110 builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start);
1111
1112 static PyObject *
builtin_sum(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1113 builtin_sum(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1114 {
1115 PyObject *return_value = NULL;
1116 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1117
1118 #define NUM_KEYWORDS 1
1119 static struct {
1120 PyGC_Head _this_is_not_used;
1121 PyObject_VAR_HEAD
1122 PyObject *ob_item[NUM_KEYWORDS];
1123 } _kwtuple = {
1124 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1125 .ob_item = { &_Py_ID(start), },
1126 };
1127 #undef NUM_KEYWORDS
1128 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1129
1130 #else // !Py_BUILD_CORE
1131 # define KWTUPLE NULL
1132 #endif // !Py_BUILD_CORE
1133
1134 static const char * const _keywords[] = {"", "start", NULL};
1135 static _PyArg_Parser _parser = {
1136 .keywords = _keywords,
1137 .fname = "sum",
1138 .kwtuple = KWTUPLE,
1139 };
1140 #undef KWTUPLE
1141 PyObject *argsbuf[2];
1142 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1143 PyObject *iterable;
1144 PyObject *start = NULL;
1145
1146 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
1147 if (!args) {
1148 goto exit;
1149 }
1150 iterable = args[0];
1151 if (!noptargs) {
1152 goto skip_optional_pos;
1153 }
1154 start = args[1];
1155 skip_optional_pos:
1156 return_value = builtin_sum_impl(module, iterable, start);
1157
1158 exit:
1159 return return_value;
1160 }
1161
1162 PyDoc_STRVAR(builtin_isinstance__doc__,
1163 "isinstance($module, obj, class_or_tuple, /)\n"
1164 "--\n"
1165 "\n"
1166 "Return whether an object is an instance of a class or of a subclass thereof.\n"
1167 "\n"
1168 "A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to\n"
1169 "check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)\n"
1170 "or ...`` etc.");
1171
1172 #define BUILTIN_ISINSTANCE_METHODDEF \
1173 {"isinstance", _PyCFunction_CAST(builtin_isinstance), METH_FASTCALL, builtin_isinstance__doc__},
1174
1175 static PyObject *
1176 builtin_isinstance_impl(PyObject *module, PyObject *obj,
1177 PyObject *class_or_tuple);
1178
1179 static PyObject *
builtin_isinstance(PyObject * module,PyObject * const * args,Py_ssize_t nargs)1180 builtin_isinstance(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
1181 {
1182 PyObject *return_value = NULL;
1183 PyObject *obj;
1184 PyObject *class_or_tuple;
1185
1186 if (!_PyArg_CheckPositional("isinstance", nargs, 2, 2)) {
1187 goto exit;
1188 }
1189 obj = args[0];
1190 class_or_tuple = args[1];
1191 return_value = builtin_isinstance_impl(module, obj, class_or_tuple);
1192
1193 exit:
1194 return return_value;
1195 }
1196
1197 PyDoc_STRVAR(builtin_issubclass__doc__,
1198 "issubclass($module, cls, class_or_tuple, /)\n"
1199 "--\n"
1200 "\n"
1201 "Return whether \'cls\' is derived from another class or is the same class.\n"
1202 "\n"
1203 "A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to\n"
1204 "check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)\n"
1205 "or ...``.");
1206
1207 #define BUILTIN_ISSUBCLASS_METHODDEF \
1208 {"issubclass", _PyCFunction_CAST(builtin_issubclass), METH_FASTCALL, builtin_issubclass__doc__},
1209
1210 static PyObject *
1211 builtin_issubclass_impl(PyObject *module, PyObject *cls,
1212 PyObject *class_or_tuple);
1213
1214 static PyObject *
builtin_issubclass(PyObject * module,PyObject * const * args,Py_ssize_t nargs)1215 builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
1216 {
1217 PyObject *return_value = NULL;
1218 PyObject *cls;
1219 PyObject *class_or_tuple;
1220
1221 if (!_PyArg_CheckPositional("issubclass", nargs, 2, 2)) {
1222 goto exit;
1223 }
1224 cls = args[0];
1225 class_or_tuple = args[1];
1226 return_value = builtin_issubclass_impl(module, cls, class_or_tuple);
1227
1228 exit:
1229 return return_value;
1230 }
1231 /*[clinic end generated code: output=435d3f286a863c49 input=a9049054013a1b77]*/
1232