Branch data Line data Source code
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 : :
10 : :
11 : : PyDoc_STRVAR(func_new__doc__,
12 : : "function(code, globals, name=None, argdefs=None, closure=None)\n"
13 : : "--\n"
14 : : "\n"
15 : : "Create a function object.\n"
16 : : "\n"
17 : : " code\n"
18 : : " a code object\n"
19 : : " globals\n"
20 : : " the globals dictionary\n"
21 : : " name\n"
22 : : " a string that overrides the name from the code object\n"
23 : : " argdefs\n"
24 : : " a tuple that specifies the default argument values\n"
25 : : " closure\n"
26 : : " a tuple that supplies the bindings for free variables");
27 : :
28 : : static PyObject *
29 : : func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
30 : : PyObject *name, PyObject *defaults, PyObject *closure);
31 : :
32 : : static PyObject *
33 : 0 : func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
34 : : {
35 : 0 : PyObject *return_value = NULL;
36 : : #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
37 : :
38 : : #define NUM_KEYWORDS 5
39 : : static struct {
40 : : PyGC_Head _this_is_not_used;
41 : : PyObject_VAR_HEAD
42 : : PyObject *ob_item[NUM_KEYWORDS];
43 : : } _kwtuple = {
44 : : .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
45 : : .ob_item = { &_Py_ID(code), &_Py_ID(globals), &_Py_ID(name), &_Py_ID(argdefs), &_Py_ID(closure), },
46 : : };
47 : : #undef NUM_KEYWORDS
48 : : #define KWTUPLE (&_kwtuple.ob_base.ob_base)
49 : :
50 : : #else // !Py_BUILD_CORE
51 : : # define KWTUPLE NULL
52 : : #endif // !Py_BUILD_CORE
53 : :
54 : : static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", NULL};
55 : : static _PyArg_Parser _parser = {
56 : : .keywords = _keywords,
57 : : .fname = "function",
58 : : .kwtuple = KWTUPLE,
59 : : };
60 : : #undef KWTUPLE
61 : : PyObject *argsbuf[5];
62 : : PyObject * const *fastargs;
63 : 0 : Py_ssize_t nargs = PyTuple_GET_SIZE(args);
64 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2;
65 : : PyCodeObject *code;
66 : : PyObject *globals;
67 : 0 : PyObject *name = Py_None;
68 : 0 : PyObject *defaults = Py_None;
69 : 0 : PyObject *closure = Py_None;
70 : :
71 [ # # # # : 0 : fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 5, 0, argsbuf);
# # # # ]
72 [ # # ]: 0 : if (!fastargs) {
73 : 0 : goto exit;
74 : : }
75 [ # # ]: 0 : if (!PyObject_TypeCheck(fastargs[0], &PyCode_Type)) {
76 : 0 : _PyArg_BadArgument("function", "argument 'code'", (&PyCode_Type)->tp_name, fastargs[0]);
77 : 0 : goto exit;
78 : : }
79 : 0 : code = (PyCodeObject *)fastargs[0];
80 [ # # ]: 0 : if (!PyDict_Check(fastargs[1])) {
81 : 0 : _PyArg_BadArgument("function", "argument 'globals'", "dict", fastargs[1]);
82 : 0 : goto exit;
83 : : }
84 : 0 : globals = fastargs[1];
85 [ # # ]: 0 : if (!noptargs) {
86 : 0 : goto skip_optional_pos;
87 : : }
88 [ # # ]: 0 : if (fastargs[2]) {
89 : 0 : name = fastargs[2];
90 [ # # ]: 0 : if (!--noptargs) {
91 : 0 : goto skip_optional_pos;
92 : : }
93 : : }
94 [ # # ]: 0 : if (fastargs[3]) {
95 : 0 : defaults = fastargs[3];
96 [ # # ]: 0 : if (!--noptargs) {
97 : 0 : goto skip_optional_pos;
98 : : }
99 : : }
100 : 0 : closure = fastargs[4];
101 : 0 : skip_optional_pos:
102 : 0 : return_value = func_new_impl(type, code, globals, name, defaults, closure);
103 : :
104 : 0 : exit:
105 : 0 : return return_value;
106 : : }
107 : : /*[clinic end generated code: output=777cead7b1f6fad3 input=a9049054013a1b77]*/
|