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 func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
34 {
35 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 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
64 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2;
65 PyCodeObject *code;
66 PyObject *globals;
67 PyObject *name = Py_None;
68 PyObject *defaults = Py_None;
69 PyObject *closure = Py_None;
70
71 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 5, 0, argsbuf);
72 if (!fastargs) {
73 goto exit;
74 }
75 if (!PyObject_TypeCheck(fastargs[0], &PyCode_Type)) {
76 _PyArg_BadArgument("function", "argument 'code'", (&PyCode_Type)->tp_name, fastargs[0]);
77 goto exit;
78 }
79 code = (PyCodeObject *)fastargs[0];
80 if (!PyDict_Check(fastargs[1])) {
81 _PyArg_BadArgument("function", "argument 'globals'", "dict", fastargs[1]);
82 goto exit;
83 }
84 globals = fastargs[1];
85 if (!noptargs) {
86 goto skip_optional_pos;
87 }
88 if (fastargs[2]) {
89 name = fastargs[2];
90 if (!--noptargs) {
91 goto skip_optional_pos;
92 }
93 }
94 if (fastargs[3]) {
95 defaults = fastargs[3];
96 if (!--noptargs) {
97 goto skip_optional_pos;
98 }
99 }
100 closure = fastargs[4];
101 skip_optional_pos:
102 return_value = func_new_impl(type, code, globals, name, defaults, closure);
103
104 exit:
105 return return_value;
106 }
107 /*[clinic end generated code: output=777cead7b1f6fad3 input=a9049054013a1b77]*/