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