Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(func_new__doc__, |
| 6 | "function(code, globals, name=None, argdefs=None, closure=None)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Create a function object.\n" |
| 10 | "\n" |
| 11 | " code\n" |
| 12 | " a code object\n" |
| 13 | " globals\n" |
| 14 | " the globals dictionary\n" |
| 15 | " name\n" |
| 16 | " a string that overrides the name from the code object\n" |
| 17 | " argdefs\n" |
| 18 | " a tuple that specifies the default argument values\n" |
| 19 | " closure\n" |
| 20 | " a tuple that supplies the bindings for free variables"); |
| 21 | |
| 22 | static PyObject * |
| 23 | func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals, |
| 24 | PyObject *name, PyObject *defaults, PyObject *closure); |
| 25 | |
| 26 | static PyObject * |
| 27 | func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 28 | { |
| 29 | PyObject *return_value = NULL; |
| 30 | static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 31 | static _PyArg_Parser _parser = {NULL, _keywords, "function", 0}; |
| 32 | PyObject *argsbuf[5]; |
| 33 | PyObject * const *fastargs; |
| 34 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 35 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2; |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 36 | PyCodeObject *code; |
| 37 | PyObject *globals; |
| 38 | PyObject *name = Py_None; |
| 39 | PyObject *defaults = Py_None; |
| 40 | PyObject *closure = Py_None; |
| 41 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 42 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 5, 0, argsbuf); |
| 43 | if (!fastargs) { |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 44 | goto exit; |
| 45 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 46 | if (!PyObject_TypeCheck(fastargs[0], &PyCode_Type)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 47 | _PyArg_BadArgument("function", "argument 'code'", (&PyCode_Type)->tp_name, fastargs[0]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 48 | goto exit; |
| 49 | } |
| 50 | code = (PyCodeObject *)fastargs[0]; |
| 51 | if (!PyDict_Check(fastargs[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 52 | _PyArg_BadArgument("function", "argument 'globals'", "dict", fastargs[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 53 | goto exit; |
| 54 | } |
| 55 | globals = fastargs[1]; |
| 56 | if (!noptargs) { |
| 57 | goto skip_optional_pos; |
| 58 | } |
| 59 | if (fastargs[2]) { |
| 60 | name = fastargs[2]; |
| 61 | if (!--noptargs) { |
| 62 | goto skip_optional_pos; |
| 63 | } |
| 64 | } |
| 65 | if (fastargs[3]) { |
| 66 | defaults = fastargs[3]; |
| 67 | if (!--noptargs) { |
| 68 | goto skip_optional_pos; |
| 69 | } |
| 70 | } |
| 71 | closure = fastargs[4]; |
| 72 | skip_optional_pos: |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 73 | return_value = func_new_impl(type, code, globals, name, defaults, closure); |
| 74 | |
| 75 | exit: |
| 76 | return return_value; |
| 77 | } |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 78 | /*[clinic end generated code: output=3d96afa3396e5c82 input=a9049054013a1b77]*/ |