Debian Codegraph @

Python/clinic/bltinmodule.c.h

raw
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 *
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 *
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 *
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 = NULL;
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 /* Cleanup for filename */
365 Py_XDECREF(filename);
366
367 return return_value;
368 }
369
370 PyDoc_STRVAR(builtin_divmod__doc__,
371 "divmod($module, x, y, /)\n"
372 "--\n"
373 "\n"
374 "Return the tuple (x//y, x%y). Invariant: div*y + mod == x.");
375
376 #define BUILTIN_DIVMOD_METHODDEF \
377 {"divmod", _PyCFunction_CAST(builtin_divmod), METH_FASTCALL, builtin_divmod__doc__},
378
379 static PyObject *
380 builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y);
381
382 static PyObject *
383 builtin_divmod(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
384 {
385 PyObject *return_value = NULL;
386 PyObject *x;
387 PyObject *y;
388
389 if (!_PyArg_CheckPositional("divmod", nargs, 2, 2)) {
390 goto exit;
391 }
392 x = args[0];
393 y = args[1];
394 return_value = builtin_divmod_impl(module, x, y);
395
396 exit:
397 return return_value;
398 }
399
400 PyDoc_STRVAR(builtin_eval__doc__,
401 "eval($module, source, /, globals=None, locals=None)\n"
402 "--\n"
403 "\n"
404 "Evaluate the given source in the context of globals and locals.\n"
405 "\n"
406 "The source may be a string representing a Python expression\n"
407 "or a code object as returned by compile().\n"
408 "The globals must be a dictionary and locals can be any mapping,\n"
409 "defaulting to the current globals and locals.\n"
410 "If only globals is given, locals defaults to it.");
411
412 #define BUILTIN_EVAL_METHODDEF \
413 {"eval", _PyCFunction_CAST(builtin_eval), METH_FASTCALL|METH_KEYWORDS, builtin_eval__doc__},
414
415 static PyObject *
416 builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
417 PyObject *locals);
418
419 static PyObject *
420 builtin_eval(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
421 {
422 PyObject *return_value = NULL;
423 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
424
425 #define NUM_KEYWORDS 2
426 static struct {
427 PyGC_Head _this_is_not_used;
428 PyObject_VAR_HEAD
429 PyObject *ob_item[NUM_KEYWORDS];
430 } _kwtuple = {
431 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
432 .ob_item = { &_Py_ID(globals), &_Py_ID(locals), },
433 };
434 #undef NUM_KEYWORDS
435 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
436
437 #else // !Py_BUILD_CORE
438 # define KWTUPLE NULL
439 #endif // !Py_BUILD_CORE
440
441 static const char * const _keywords[] = {"", "globals", "locals", NULL};
442 static _PyArg_Parser _parser = {
443 .keywords = _keywords,
444 .fname = "eval",
445 .kwtuple = KWTUPLE,
446 };
447 #undef KWTUPLE
448 PyObject *argsbuf[3];
449 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
450 PyObject *source;
451 PyObject *globals = Py_None;
452 PyObject *locals = Py_None;
453
454 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
455 if (!args) {
456 goto exit;
457 }
458 source = args[0];
459 if (!noptargs) {
460 goto skip_optional_pos;
461 }
462 if (args[1]) {
463 globals = args[1];
464 if (!--noptargs) {
465 goto skip_optional_pos;
466 }
467 }
468 locals = args[2];
469 skip_optional_pos:
470 return_value = builtin_eval_impl(module, source, globals, locals);
471
472 exit:
473 return return_value;
474 }
475
476 PyDoc_STRVAR(builtin_exec__doc__,
477 "exec($module, source, /, globals=None, locals=None, *, closure=None)\n"
478 "--\n"
479 "\n"
480 "Execute the given source in the context of globals and locals.\n"
481 "\n"
482 "The source may be a string representing one or more Python statements\n"
483 "or a code object as returned by compile().\n"
484 "The globals must be a dictionary and locals can be any mapping,\n"
485 "defaulting to the current globals and locals.\n"
486 "If only globals is given, locals defaults to it.\n"
487 "The closure must be a tuple of cellvars, and can only be used\n"
488 "when source is a code object requiring exactly that many cellvars.");
489
490 #define BUILTIN_EXEC_METHODDEF \
491 {"exec", _PyCFunction_CAST(builtin_exec), METH_FASTCALL|METH_KEYWORDS, builtin_exec__doc__},
492
493 static PyObject *
494 builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
495 PyObject *locals, PyObject *closure);
496
497 static PyObject *
498 builtin_exec(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
499 {
500 PyObject *return_value = NULL;
501 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
502
503 #define NUM_KEYWORDS 3
504 static struct {
505 PyGC_Head _this_is_not_used;
506 PyObject_VAR_HEAD
507 PyObject *ob_item[NUM_KEYWORDS];
508 } _kwtuple = {
509 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
510 .ob_item = { &_Py_ID(globals), &_Py_ID(locals), &_Py_ID(closure), },
511 };
512 #undef NUM_KEYWORDS
513 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
514
515 #else // !Py_BUILD_CORE
516 # define KWTUPLE NULL
517 #endif // !Py_BUILD_CORE
518
519 static const char * const _keywords[] = {"", "globals", "locals", "closure", NULL};
520 static _PyArg_Parser _parser = {
521 .keywords = _keywords,
522 .fname = "exec",
523 .kwtuple = KWTUPLE,
524 };
525 #undef KWTUPLE
526 PyObject *argsbuf[4];
527 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
528 PyObject *source;
529 PyObject *globals = Py_None;
530 PyObject *locals = Py_None;
531 PyObject *closure = NULL;
532
533 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
534 if (!args) {
535 goto exit;
536 }
537 source = args[0];
538 if (!noptargs) {
539 goto skip_optional_pos;
540 }
541 if (args[1]) {
542 globals = args[1];
543 if (!--noptargs) {
544 goto skip_optional_pos;
545 }
546 }
547 if (args[2]) {
548 locals = args[2];
549 if (!--noptargs) {
550 goto skip_optional_pos;
551 }
552 }
553 skip_optional_pos:
554 if (!noptargs) {
555 goto skip_optional_kwonly;
556 }
557 closure = args[3];
558 skip_optional_kwonly:
559 return_value = builtin_exec_impl(module, source, globals, locals, closure);
560
561 exit:
562 return return_value;
563 }
564
565 PyDoc_STRVAR(builtin_globals__doc__,
566 "globals($module, /)\n"
567 "--\n"
568 "\n"
569 "Return the dictionary containing the current scope\'s global variables.\n"
570 "\n"
571 "NOTE: Updates to this dictionary *will* affect name lookups in the current\n"
572 "global scope and vice-versa.");
573
574 #define BUILTIN_GLOBALS_METHODDEF \
575 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, builtin_globals__doc__},
576
577 static PyObject *
578 builtin_globals_impl(PyObject *module);
579
580 static PyObject *
581 builtin_globals(PyObject *module, PyObject *Py_UNUSED(ignored))
582 {
583 return builtin_globals_impl(module);
584 }
585
586 PyDoc_STRVAR(builtin_hasattr__doc__,
587 "hasattr($module, obj, name, /)\n"
588 "--\n"
589 "\n"
590 "Return whether the object has an attribute with the given name.\n"
591 "\n"
592 "This is done by calling getattr(obj, name) and catching AttributeError.");
593
594 #define BUILTIN_HASATTR_METHODDEF \
595 {"hasattr", _PyCFunction_CAST(builtin_hasattr), METH_FASTCALL, builtin_hasattr__doc__},
596
597 static PyObject *
598 builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name);
599
600 static PyObject *
601 builtin_hasattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
602 {
603 PyObject *return_value = NULL;
604 PyObject *obj;
605 PyObject *name;
606
607 if (!_PyArg_CheckPositional("hasattr", nargs, 2, 2)) {
608 goto exit;
609 }
610 obj = args[0];
611 name = args[1];
612 return_value = builtin_hasattr_impl(module, obj, name);
613
614 exit:
615 return return_value;
616 }
617
618 PyDoc_STRVAR(builtin_id__doc__,
619 "id($module, obj, /)\n"
620 "--\n"
621 "\n"
622 "Return the identity of an object.\n"
623 "\n"
624 "This is guaranteed to be unique among simultaneously existing objects.\n"
625 "(CPython uses the object\'s memory address.)");
626
627 #define BUILTIN_ID_METHODDEF \
628 {"id", (PyCFunction)builtin_id, METH_O, builtin_id__doc__},
629
630 PyDoc_STRVAR(builtin_setattr__doc__,
631 "setattr($module, obj, name, value, /)\n"
632 "--\n"
633 "\n"
634 "Sets the named attribute on the given object to the specified value.\n"
635 "\n"
636 "setattr(x, \'y\', v) is equivalent to ``x.y = v``");
637
638 #define BUILTIN_SETATTR_METHODDEF \
639 {"setattr", _PyCFunction_CAST(builtin_setattr), METH_FASTCALL, builtin_setattr__doc__},
640
641 static PyObject *
642 builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
643 PyObject *value);
644
645 static PyObject *
646 builtin_setattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
647 {
648 PyObject *return_value = NULL;
649 PyObject *obj;
650 PyObject *name;
651 PyObject *value;
652
653 if (!_PyArg_CheckPositional("setattr", nargs, 3, 3)) {
654 goto exit;
655 }
656 obj = args[0];
657 name = args[1];
658 value = args[2];
659 return_value = builtin_setattr_impl(module, obj, name, value);
660
661 exit:
662 return return_value;
663 }
664
665 PyDoc_STRVAR(builtin_delattr__doc__,
666 "delattr($module, obj, name, /)\n"
667 "--\n"
668 "\n"
669 "Deletes the named attribute from the given object.\n"
670 "\n"
671 "delattr(x, \'y\') is equivalent to ``del x.y``");
672
673 #define BUILTIN_DELATTR_METHODDEF \
674 {"delattr", _PyCFunction_CAST(builtin_delattr), METH_FASTCALL, builtin_delattr__doc__},
675
676 static PyObject *
677 builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name);
678
679 static PyObject *
680 builtin_delattr(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
681 {
682 PyObject *return_value = NULL;
683 PyObject *obj;
684 PyObject *name;
685
686 if (!_PyArg_CheckPositional("delattr", nargs, 2, 2)) {
687 goto exit;
688 }
689 obj = args[0];
690 name = args[1];
691 return_value = builtin_delattr_impl(module, obj, name);
692
693 exit:
694 return return_value;
695 }
696
697 PyDoc_STRVAR(builtin_hash__doc__,
698 "hash($module, obj, /)\n"
699 "--\n"
700 "\n"
701 "Return the hash value for the given object.\n"
702 "\n"
703 "Two objects that compare equal must also have the same hash value, but the\n"
704 "reverse is not necessarily true.");
705
706 #define BUILTIN_HASH_METHODDEF \
707 {"hash", (PyCFunction)builtin_hash, METH_O, builtin_hash__doc__},
708
709 PyDoc_STRVAR(builtin_hex__doc__,
710 "hex($module, number, /)\n"
711 "--\n"
712 "\n"
713 "Return the hexadecimal representation of an integer.\n"
714 "\n"
715 " >>> hex(12648430)\n"
716 " \'0xc0ffee\'");
717
718 #define BUILTIN_HEX_METHODDEF \
719 {"hex", (PyCFunction)builtin_hex, METH_O, builtin_hex__doc__},
720
721 PyDoc_STRVAR(builtin_aiter__doc__,
722 "aiter($module, async_iterable, /)\n"
723 "--\n"
724 "\n"
725 "Return an AsyncIterator for an AsyncIterable object.");
726
727 #define BUILTIN_AITER_METHODDEF \
728 {"aiter", (PyCFunction)builtin_aiter, METH_O, builtin_aiter__doc__},
729
730 PyDoc_STRVAR(builtin_anext__doc__,
731 "anext($module, aiterator, default=<unrepresentable>, /)\n"
732 "--\n"
733 "\n"
734 "Return the next item from the async iterator.\n"
735 "\n"
736 "If default is given and the async iterator is exhausted,\n"
737 "it is returned instead of raising StopAsyncIteration.");
738
739 #define BUILTIN_ANEXT_METHODDEF \
740 {"anext", _PyCFunction_CAST(builtin_anext), METH_FASTCALL, builtin_anext__doc__},
741
742 static PyObject *
743 builtin_anext_impl(PyObject *module, PyObject *aiterator,
744 PyObject *default_value);
745
746 static PyObject *
747 builtin_anext(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
748 {
749 PyObject *return_value = NULL;
750 PyObject *aiterator;
751 PyObject *default_value = NULL;
752
753 if (!_PyArg_CheckPositional("anext", nargs, 1, 2)) {
754 goto exit;
755 }
756 aiterator = args[0];
757 if (nargs < 2) {
758 goto skip_optional;
759 }
760 default_value = args[1];
761 skip_optional:
762 return_value = builtin_anext_impl(module, aiterator, default_value);
763
764 exit:
765 return return_value;
766 }
767
768 PyDoc_STRVAR(builtin_len__doc__,
769 "len($module, obj, /)\n"
770 "--\n"
771 "\n"
772 "Return the number of items in a container.");
773
774 #define BUILTIN_LEN_METHODDEF \
775 {"len", (PyCFunction)builtin_len, METH_O, builtin_len__doc__},
776
777 PyDoc_STRVAR(builtin_locals__doc__,
778 "locals($module, /)\n"
779 "--\n"
780 "\n"
781 "Return a dictionary containing the current scope\'s local variables.\n"
782 "\n"
783 "NOTE: Whether or not updates to this dictionary will affect name lookups in\n"
784 "the local scope and vice-versa is *implementation dependent* and not\n"
785 "covered by any backwards compatibility guarantees.");
786
787 #define BUILTIN_LOCALS_METHODDEF \
788 {"locals", (PyCFunction)builtin_locals, METH_NOARGS, builtin_locals__doc__},
789
790 static PyObject *
791 builtin_locals_impl(PyObject *module);
792
793 static PyObject *
794 builtin_locals(PyObject *module, PyObject *Py_UNUSED(ignored))
795 {
796 return builtin_locals_impl(module);
797 }
798
799 PyDoc_STRVAR(builtin_oct__doc__,
800 "oct($module, number, /)\n"
801 "--\n"
802 "\n"
803 "Return the octal representation of an integer.\n"
804 "\n"
805 " >>> oct(342391)\n"
806 " \'0o1234567\'");
807
808 #define BUILTIN_OCT_METHODDEF \
809 {"oct", (PyCFunction)builtin_oct, METH_O, builtin_oct__doc__},
810
811 PyDoc_STRVAR(builtin_ord__doc__,
812 "ord($module, character, /)\n"
813 "--\n"
814 "\n"
815 "Return the ordinal value of a character.\n"
816 "\n"
817 "If the argument is a one-character string, return the Unicode code\n"
818 "point of that character.\n"
819 "\n"
820 "If the argument is a bytes or bytearray object of length 1, return its\n"
821 "single byte value.");
822
823 #define BUILTIN_ORD_METHODDEF \
824 {"ord", (PyCFunction)builtin_ord, METH_O, builtin_ord__doc__},
825
826 PyDoc_STRVAR(builtin_pow__doc__,
827 "pow($module, /, base, exp, mod=None)\n"
828 "--\n"
829 "\n"
830 "Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments\n"
831 "\n"
832 "Some types, such as ints, are able to use a more efficient algorithm when\n"
833 "invoked using the three argument form.");
834
835 #define BUILTIN_POW_METHODDEF \
836 {"pow", _PyCFunction_CAST(builtin_pow), METH_FASTCALL|METH_KEYWORDS, builtin_pow__doc__},
837
838 static PyObject *
839 builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp,
840 PyObject *mod);
841
842 static PyObject *
843 builtin_pow(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
844 {
845 PyObject *return_value = NULL;
846 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
847
848 #define NUM_KEYWORDS 3
849 static struct {
850 PyGC_Head _this_is_not_used;
851 PyObject_VAR_HEAD
852 PyObject *ob_item[NUM_KEYWORDS];
853 } _kwtuple = {
854 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
855 .ob_item = { &_Py_ID(base), &_Py_ID(exp), &_Py_ID(mod), },
856 };
857 #undef NUM_KEYWORDS
858 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
859
860 #else // !Py_BUILD_CORE
861 # define KWTUPLE NULL
862 #endif // !Py_BUILD_CORE
863
864 static const char * const _keywords[] = {"base", "exp", "mod", NULL};
865 static _PyArg_Parser _parser = {
866 .keywords = _keywords,
867 .fname = "pow",
868 .kwtuple = KWTUPLE,
869 };
870 #undef KWTUPLE
871 PyObject *argsbuf[3];
872 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
873 PyObject *base;
874 PyObject *exp;
875 PyObject *mod = Py_None;
876
877 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
878 if (!args) {
879 goto exit;
880 }
881 base = args[0];
882 exp = args[1];
883 if (!noptargs) {
884 goto skip_optional_pos;
885 }
886 mod = args[2];
887 skip_optional_pos:
888 return_value = builtin_pow_impl(module, base, exp, mod);
889
890 exit:
891 return return_value;
892 }
893
894 PyDoc_STRVAR(builtin_print__doc__,
895 "print($module, /, *args, sep=\' \', end=\'\\n\', file=None, flush=False)\n"
896 "--\n"
897 "\n"
898 "Prints the values to a stream, or to sys.stdout by default.\n"
899 "\n"
900 " sep\n"
901 " string inserted between values, default a space.\n"
902 " end\n"
903 " string appended after the last value, default a newline.\n"
904 " file\n"
905 " a file-like object (stream); defaults to the current sys.stdout.\n"
906 " flush\n"
907 " whether to forcibly flush the stream.");
908
909 #define BUILTIN_PRINT_METHODDEF \
910 {"print", _PyCFunction_CAST(builtin_print), METH_FASTCALL|METH_KEYWORDS, builtin_print__doc__},
911
912 static PyObject *
913 builtin_print_impl(PyObject *module, PyObject *args, PyObject *sep,
914 PyObject *end, PyObject *file, int flush);
915
916 static PyObject *
917 builtin_print(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
918 {
919 PyObject *return_value = NULL;
920 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
921
922 #define NUM_KEYWORDS 4
923 static struct {
924 PyGC_Head _this_is_not_used;
925 PyObject_VAR_HEAD
926 PyObject *ob_item[NUM_KEYWORDS];
927 } _kwtuple = {
928 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
929 .ob_item = { &_Py_ID(sep), &_Py_ID(end), &_Py_ID(file), &_Py_ID(flush), },
930 };
931 #undef NUM_KEYWORDS
932 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
933
934 #else // !Py_BUILD_CORE
935 # define KWTUPLE NULL
936 #endif // !Py_BUILD_CORE
937
938 static const char * const _keywords[] = {"sep", "end", "file", "flush", NULL};
939 static _PyArg_Parser _parser = {
940 .keywords = _keywords,
941 .fname = "print",
942 .kwtuple = KWTUPLE,
943 };
944 #undef KWTUPLE
945 PyObject *argsbuf[5];
946 Py_ssize_t noptargs = 0 + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
947 PyObject *__clinic_args = NULL;
948 PyObject *sep = Py_None;
949 PyObject *end = Py_None;
950 PyObject *file = Py_None;
951 int flush = 0;
952
953 args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, 0, argsbuf);
954 if (!args) {
955 goto exit;
956 }
957 __clinic_args = args[0];
958 if (!noptargs) {
959 goto skip_optional_kwonly;
960 }
961 if (args[1]) {
962 sep = args[1];
963 if (!--noptargs) {
964 goto skip_optional_kwonly;
965 }
966 }
967 if (args[2]) {
968 end = args[2];
969 if (!--noptargs) {
970 goto skip_optional_kwonly;
971 }
972 }
973 if (args[3]) {
974 file = args[3];
975 if (!--noptargs) {
976 goto skip_optional_kwonly;
977 }
978 }
979 flush = PyObject_IsTrue(args[4]);
980 if (flush < 0) {
981 goto exit;
982 }
983 skip_optional_kwonly:
984 return_value = builtin_print_impl(module, __clinic_args, sep, end, file, flush);
985
986 exit:
987 Py_XDECREF(__clinic_args);
988 return return_value;
989 }
990
991 PyDoc_STRVAR(builtin_input__doc__,
992 "input($module, prompt=\'\', /)\n"
993 "--\n"
994 "\n"
995 "Read a string from standard input. The trailing newline is stripped.\n"
996 "\n"
997 "The prompt string, if given, is printed to standard output without a\n"
998 "trailing newline before reading input.\n"
999 "\n"
1000 "If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.\n"
1001 "On *nix systems, readline is used if available.");
1002
1003 #define BUILTIN_INPUT_METHODDEF \
1004 {"input", _PyCFunction_CAST(builtin_input), METH_FASTCALL, builtin_input__doc__},
1005
1006 static PyObject *
1007 builtin_input_impl(PyObject *module, PyObject *prompt);
1008
1009 static PyObject *
1010 builtin_input(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
1011 {
1012 PyObject *return_value = NULL;
1013 PyObject *prompt = NULL;
1014
1015 if (!_PyArg_CheckPositional("input", nargs, 0, 1)) {
1016 goto exit;
1017 }
1018 if (nargs < 1) {
1019 goto skip_optional;
1020 }
1021 prompt = args[0];
1022 skip_optional:
1023 return_value = builtin_input_impl(module, prompt);
1024
1025 exit:
1026 return return_value;
1027 }
1028
1029 PyDoc_STRVAR(builtin_repr__doc__,
1030 "repr($module, obj, /)\n"
1031 "--\n"
1032 "\n"
1033 "Return the canonical string representation of the object.\n"
1034 "\n"
1035 "For many object types, including most builtins, eval(repr(obj)) == obj.");
1036
1037 #define BUILTIN_REPR_METHODDEF \
1038 {"repr", (PyCFunction)builtin_repr, METH_O, builtin_repr__doc__},
1039
1040 PyDoc_STRVAR(builtin_round__doc__,
1041 "round($module, /, number, ndigits=None)\n"
1042 "--\n"
1043 "\n"
1044 "Round a number to a given precision in decimal digits.\n"
1045 "\n"
1046 "The return value is an integer if ndigits is omitted or None. Otherwise\n"
1047 "the return value has the same type as the number. ndigits may be negative.");
1048
1049 #define BUILTIN_ROUND_METHODDEF \
1050 {"round", _PyCFunction_CAST(builtin_round), METH_FASTCALL|METH_KEYWORDS, builtin_round__doc__},
1051
1052 static PyObject *
1053 builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits);
1054
1055 static PyObject *
1056 builtin_round(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1057 {
1058 PyObject *return_value = NULL;
1059 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1060
1061 #define NUM_KEYWORDS 2
1062 static struct {
1063 PyGC_Head _this_is_not_used;
1064 PyObject_VAR_HEAD
1065 PyObject *ob_item[NUM_KEYWORDS];
1066 } _kwtuple = {
1067 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1068 .ob_item = { &_Py_ID(number), &_Py_ID(ndigits), },
1069 };
1070 #undef NUM_KEYWORDS
1071 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1072
1073 #else // !Py_BUILD_CORE
1074 # define KWTUPLE NULL
1075 #endif // !Py_BUILD_CORE
1076
1077 static const char * const _keywords[] = {"number", "ndigits", NULL};
1078 static _PyArg_Parser _parser = {
1079 .keywords = _keywords,
1080 .fname = "round",
1081 .kwtuple = KWTUPLE,
1082 };
1083 #undef KWTUPLE
1084 PyObject *argsbuf[2];
1085 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1086 PyObject *number;
1087 PyObject *ndigits = Py_None;
1088
1089 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
1090 if (!args) {
1091 goto exit;
1092 }
1093 number = args[0];
1094 if (!noptargs) {
1095 goto skip_optional_pos;
1096 }
1097 ndigits = args[1];
1098 skip_optional_pos:
1099 return_value = builtin_round_impl(module, number, ndigits);
1100
1101 exit:
1102 return return_value;
1103 }
1104
1105 PyDoc_STRVAR(builtin_sum__doc__,
1106 "sum($module, iterable, /, start=0)\n"
1107 "--\n"
1108 "\n"
1109 "Return the sum of a \'start\' value (default: 0) plus an iterable of numbers\n"
1110 "\n"
1111 "When the iterable is empty, return the start value.\n"
1112 "This function is intended specifically for use with numeric values and may\n"
1113 "reject non-numeric types.");
1114
1115 #define BUILTIN_SUM_METHODDEF \
1116 {"sum", _PyCFunction_CAST(builtin_sum), METH_FASTCALL|METH_KEYWORDS, builtin_sum__doc__},
1117
1118 static PyObject *
1119 builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start);
1120
1121 static PyObject *
1122 builtin_sum(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1123 {
1124 PyObject *return_value = NULL;
1125 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1126
1127 #define NUM_KEYWORDS 1
1128 static struct {
1129 PyGC_Head _this_is_not_used;
1130 PyObject_VAR_HEAD
1131 PyObject *ob_item[NUM_KEYWORDS];
1132 } _kwtuple = {
1133 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1134 .ob_item = { &_Py_ID(start), },
1135 };
1136 #undef NUM_KEYWORDS
1137 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1138
1139 #else // !Py_BUILD_CORE
1140 # define KWTUPLE NULL
1141 #endif // !Py_BUILD_CORE
1142
1143 static const char * const _keywords[] = {"", "start", NULL};
1144 static _PyArg_Parser _parser = {
1145 .keywords = _keywords,
1146 .fname = "sum",
1147 .kwtuple = KWTUPLE,
1148 };
1149 #undef KWTUPLE
1150 PyObject *argsbuf[2];
1151 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1152 PyObject *iterable;
1153 PyObject *start = NULL;
1154
1155 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
1156 if (!args) {
1157 goto exit;
1158 }
1159 iterable = args[0];
1160 if (!noptargs) {
1161 goto skip_optional_pos;
1162 }
1163 start = args[1];
1164 skip_optional_pos:
1165 return_value = builtin_sum_impl(module, iterable, start);
1166
1167 exit:
1168 return return_value;
1169 }
1170
1171 PyDoc_STRVAR(builtin_isinstance__doc__,
1172 "isinstance($module, obj, class_or_tuple, /)\n"
1173 "--\n"
1174 "\n"
1175 "Return whether an object is an instance of a class or of a subclass thereof.\n"
1176 "\n"
1177 "A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to\n"
1178 "check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)\n"
1179 "or ...`` etc.");
1180
1181 #define BUILTIN_ISINSTANCE_METHODDEF \
1182 {"isinstance", _PyCFunction_CAST(builtin_isinstance), METH_FASTCALL, builtin_isinstance__doc__},
1183
1184 static PyObject *
1185 builtin_isinstance_impl(PyObject *module, PyObject *obj,
1186 PyObject *class_or_tuple);
1187
1188 static PyObject *
1189 builtin_isinstance(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
1190 {
1191 PyObject *return_value = NULL;
1192 PyObject *obj;
1193 PyObject *class_or_tuple;
1194
1195 if (!_PyArg_CheckPositional("isinstance", nargs, 2, 2)) {
1196 goto exit;
1197 }
1198 obj = args[0];
1199 class_or_tuple = args[1];
1200 return_value = builtin_isinstance_impl(module, obj, class_or_tuple);
1201
1202 exit:
1203 return return_value;
1204 }
1205
1206 PyDoc_STRVAR(builtin_issubclass__doc__,
1207 "issubclass($module, cls, class_or_tuple, /)\n"
1208 "--\n"
1209 "\n"
1210 "Return whether \'cls\' is derived from another class or is the same class.\n"
1211 "\n"
1212 "A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to\n"
1213 "check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)\n"
1214 "or ...``.");
1215
1216 #define BUILTIN_ISSUBCLASS_METHODDEF \
1217 {"issubclass", _PyCFunction_CAST(builtin_issubclass), METH_FASTCALL, builtin_issubclass__doc__},
1218
1219 static PyObject *
1220 builtin_issubclass_impl(PyObject *module, PyObject *cls,
1221 PyObject *class_or_tuple);
1222
1223 static PyObject *
1224 builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
1225 {
1226 PyObject *return_value = NULL;
1227 PyObject *cls;
1228 PyObject *class_or_tuple;
1229
1230 if (!_PyArg_CheckPositional("issubclass", nargs, 2, 2)) {
1231 goto exit;
1232 }
1233 cls = args[0];
1234 class_or_tuple = args[1];
1235 return_value = builtin_issubclass_impl(module, cls, class_or_tuple);
1236
1237 exit:
1238 return return_value;
1239 }
1240 /*[clinic end generated code: output=39173c70790d9cd0 input=a9049054013a1b77]*/