gh-112640: Add `kwdefaults` parameter to `types.FunctionType.__new__` (#112641)

This commit is contained in:
Nikita Sobolev 2024-01-11 11:42:30 +03:00 committed by GitHub
parent f653caa5a8
commit 2ac4cf4743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 13 deletions

View File

@ -1015,6 +1015,7 @@ _PyStaticObjects_CheckRefcnt(PyInterpreterState *interp) {
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(kw)); _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(kw));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(kw1)); _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(kw1));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(kw2)); _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(kw2));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(kwdefaults));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(lambda)); _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(lambda));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(last)); _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(last));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(last_exc)); _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(last_exc));

View File

@ -504,6 +504,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(kw) STRUCT_FOR_ID(kw)
STRUCT_FOR_ID(kw1) STRUCT_FOR_ID(kw1)
STRUCT_FOR_ID(kw2) STRUCT_FOR_ID(kw2)
STRUCT_FOR_ID(kwdefaults)
STRUCT_FOR_ID(lambda) STRUCT_FOR_ID(lambda)
STRUCT_FOR_ID(last) STRUCT_FOR_ID(last)
STRUCT_FOR_ID(last_exc) STRUCT_FOR_ID(last_exc)

View File

@ -1013,6 +1013,7 @@ extern "C" {
INIT_ID(kw), \ INIT_ID(kw), \
INIT_ID(kw1), \ INIT_ID(kw1), \
INIT_ID(kw2), \ INIT_ID(kw2), \
INIT_ID(kwdefaults), \
INIT_ID(lambda), \ INIT_ID(lambda), \
INIT_ID(last), \ INIT_ID(last), \
INIT_ID(last_exc), \ INIT_ID(last_exc), \

View File

@ -1353,6 +1353,9 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) {
string = &_Py_ID(kw2); string = &_Py_ID(kw2);
assert(_PyUnicode_CheckConsistency(string, 1)); assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string); _PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(kwdefaults);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(lambda); string = &_Py_ID(lambda);
assert(_PyUnicode_CheckConsistency(string, 1)); assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string); _PyUnicode_InternInPlace(interp, &string);

View File

@ -2263,5 +2263,38 @@ class CoroutineTests(unittest.TestCase):
'close', 'throw'})) 'close', 'throw'}))
class FunctionTests(unittest.TestCase):
def test_function_type_defaults(self):
def ex(a, /, b, *, c):
return a + b + c
func = types.FunctionType(
ex.__code__, {}, "func", (1, 2), None, {'c': 3},
)
self.assertEqual(func(), 6)
self.assertEqual(func.__defaults__, (1, 2))
self.assertEqual(func.__kwdefaults__, {'c': 3})
func = types.FunctionType(
ex.__code__, {}, "func", None, None, None,
)
self.assertEqual(func.__defaults__, None)
self.assertEqual(func.__kwdefaults__, None)
def test_function_type_wrong_defaults(self):
def ex(a, /, b, *, c):
return a + b + c
with self.assertRaisesRegex(TypeError, 'arg 4'):
types.FunctionType(
ex.__code__, {}, "func", 1, None, {'c': 3},
)
with self.assertRaisesRegex(TypeError, 'arg 6'):
types.FunctionType(
ex.__code__, {}, "func", None, None, 3,
)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -0,0 +1,2 @@
Add ``kwdefaults`` parameter to :data:`types.FunctionType` to set
default keyword argument values.

View File

@ -9,7 +9,8 @@ preserve
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords() #include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
PyDoc_STRVAR(func_new__doc__, PyDoc_STRVAR(func_new__doc__,
"function(code, globals, name=None, argdefs=None, closure=None)\n" "function(code, globals, name=None, argdefs=None, closure=None,\n"
" kwdefaults=None)\n"
"--\n" "--\n"
"\n" "\n"
"Create a function object.\n" "Create a function object.\n"
@ -23,11 +24,14 @@ PyDoc_STRVAR(func_new__doc__,
" argdefs\n" " argdefs\n"
" a tuple that specifies the default argument values\n" " a tuple that specifies the default argument values\n"
" closure\n" " closure\n"
" a tuple that supplies the bindings for free variables"); " a tuple that supplies the bindings for free variables\n"
" kwdefaults\n"
" a dictionary that specifies the default keyword argument values");
static PyObject * static PyObject *
func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals, func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
PyObject *name, PyObject *defaults, PyObject *closure); PyObject *name, PyObject *defaults, PyObject *closure,
PyObject *kwdefaults);
static PyObject * static PyObject *
func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
@ -35,14 +39,14 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *return_value = NULL; PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 5 #define NUM_KEYWORDS 6
static struct { static struct {
PyGC_Head _this_is_not_used; PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS]; PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = { } _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(code), &_Py_ID(globals), &_Py_ID(name), &_Py_ID(argdefs), &_Py_ID(closure), }, .ob_item = { &_Py_ID(code), &_Py_ID(globals), &_Py_ID(name), &_Py_ID(argdefs), &_Py_ID(closure), &_Py_ID(kwdefaults), },
}; };
#undef NUM_KEYWORDS #undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base) #define KWTUPLE (&_kwtuple.ob_base.ob_base)
@ -51,14 +55,14 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
# define KWTUPLE NULL # define KWTUPLE NULL
#endif // !Py_BUILD_CORE #endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", NULL}; static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", "kwdefaults", NULL};
static _PyArg_Parser _parser = { static _PyArg_Parser _parser = {
.keywords = _keywords, .keywords = _keywords,
.fname = "function", .fname = "function",
.kwtuple = KWTUPLE, .kwtuple = KWTUPLE,
}; };
#undef KWTUPLE #undef KWTUPLE
PyObject *argsbuf[5]; PyObject *argsbuf[6];
PyObject * const *fastargs; PyObject * const *fastargs;
Py_ssize_t nargs = PyTuple_GET_SIZE(args); Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2; Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2;
@ -67,8 +71,9 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *name = Py_None; PyObject *name = Py_None;
PyObject *defaults = Py_None; PyObject *defaults = Py_None;
PyObject *closure = Py_None; PyObject *closure = Py_None;
PyObject *kwdefaults = Py_None;
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 5, 0, argsbuf); fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 6, 0, argsbuf);
if (!fastargs) { if (!fastargs) {
goto exit; goto exit;
} }
@ -97,11 +102,17 @@ func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto skip_optional_pos; goto skip_optional_pos;
} }
} }
closure = fastargs[4]; if (fastargs[4]) {
closure = fastargs[4];
if (!--noptargs) {
goto skip_optional_pos;
}
}
kwdefaults = fastargs[5];
skip_optional_pos: skip_optional_pos:
return_value = func_new_impl(type, code, globals, name, defaults, closure); return_value = func_new_impl(type, code, globals, name, defaults, closure, kwdefaults);
exit: exit:
return return_value; return return_value;
} }
/*[clinic end generated code: output=ff7b995500d2bee6 input=a9049054013a1b77]*/ /*[clinic end generated code: output=10947342188f38a9 input=a9049054013a1b77]*/

View File

@ -809,14 +809,17 @@ function.__new__ as func_new
a tuple that specifies the default argument values a tuple that specifies the default argument values
closure: object = None closure: object = None
a tuple that supplies the bindings for free variables a tuple that supplies the bindings for free variables
kwdefaults: object = None
a dictionary that specifies the default keyword argument values
Create a function object. Create a function object.
[clinic start generated code]*/ [clinic start generated code]*/
static PyObject * static PyObject *
func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals, func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
PyObject *name, PyObject *defaults, PyObject *closure) PyObject *name, PyObject *defaults, PyObject *closure,
/*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/ PyObject *kwdefaults)
/*[clinic end generated code: output=de72f4c22ac57144 input=20c9c9f04ad2d3f2]*/
{ {
PyFunctionObject *newfunc; PyFunctionObject *newfunc;
Py_ssize_t nclosure; Py_ssize_t nclosure;
@ -843,6 +846,11 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
return NULL; return NULL;
} }
} }
if (kwdefaults != Py_None && !PyDict_Check(kwdefaults)) {
PyErr_SetString(PyExc_TypeError,
"arg 6 (kwdefaults) must be None or dict");
return NULL;
}
/* check that the closure is well-formed */ /* check that the closure is well-formed */
nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
@ -879,6 +887,9 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
if (closure != Py_None) { if (closure != Py_None) {
newfunc->func_closure = Py_NewRef(closure); newfunc->func_closure = Py_NewRef(closure);
} }
if (kwdefaults != Py_None) {
newfunc->func_kwdefaults = Py_NewRef(kwdefaults);
}
return (PyObject *)newfunc; return (PyObject *)newfunc;
} }