Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-90370: Avoid temporary varargs tuple creation in argument passing #30312

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b68176d
Avoid temporary `varargs` tuple creation in argument passing
colorfulappl Dec 31, 2021
43b9410
Optimize argument passing of builtin print() with modified Argument C…
colorfulappl Dec 31, 2021
9b37a77
Revert modification of PyAPI_FUNC _PyArg_UnpackKeywordsWithVararg to …
colorfulappl Jan 4, 2022
46e4472
📜🤖 Added by blurb_it.
blurb-it[bot] Jan 4, 2022
e06f343
Revert _PyArg_UnpackKeywordsWithVararg and add _PyArg_UnpackKeywordsW…
colorfulappl Jan 4, 2022
d35410b
Fix a bug which allows more than one varargs
colorfulappl Feb 22, 2022
0a9fe91
Do not copy posargs and vararg during argument parsing
colorfulappl Mar 22, 2022
9188052
Rename _PyArg_UnpackKeywordsWithVarargFast, it returns kwargs only now
colorfulappl Mar 23, 2022
d245ec0
Check type of varargs when generating argument parser
colorfulappl Mar 23, 2022
8d16685
Fix varargssize calculation in class init and add test cases
colorfulappl Mar 23, 2022
f4213ea
Merge branch 'main' into opt_ac
colorfulappl Mar 23, 2022
156f8d6
Rerun make clinic
colorfulappl Mar 23, 2022
c32aed4
Edit documentation
colorfulappl Mar 23, 2022
7ef5623
Merge branch 'main' into opt_ac
colorfulappl Feb 3, 2023
3ac2821
Fix errors introduced by merging and rerun make clinic
colorfulappl Feb 3, 2023
f63a704
Update news
colorfulappl Feb 3, 2023
8f61f5e
Fix varargssize in new_or_init
colorfulappl Feb 6, 2023
1eff215
Simplify the code a bit
colorfulappl Feb 6, 2023
5baf0f5
Optimize generated varargssize assignment
colorfulappl Feb 6, 2023
30c0606
Fix argument passing in new_or_init
colorfulappl Feb 7, 2023
d9e7408
Add tests for class method `__new__`
colorfulappl Feb 7, 2023
0f04a95
Merge branch 'main' into opt_ac
colorfulappl Feb 7, 2023
85a8ac5
Correct test function name
colorfulappl Feb 8, 2023
a9d1424
Fix vararg parsing when its name is not `args`
colorfulappl Feb 8, 2023
d6ff01a
Merge branch 'main' into opt_ac
colorfulappl Feb 8, 2023
597152e
Add testcases for undeclared keyword arguments
colorfulappl Feb 8, 2023
3119fa6
Merge branch 'main' into opt_ac
erlend-aasland Apr 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions Lib/test/clinic.test
Original file line number Diff line number Diff line change
Expand Up @@ -4108,6 +4108,136 @@ static PyObject *
test_paramname_module_impl(PyObject *module, PyObject *mod)
/*[clinic end generated code: output=4a2a849ecbcc8b53 input=afefe259667f13ba]*/

/*[clinic input]
test_vararg_with_other_name

*strange_name_vararg: object

[clinic start generated code]*/

PyDoc_STRVAR(test_vararg_with_other_name__doc__,
"test_vararg_with_other_name($module, /, *strange_name_vararg)\n"
"--\n"
"\n");

#define TEST_VARARG_WITH_OTHER_NAME_METHODDEF \
{"test_vararg_with_other_name", _PyCFunction_CAST(test_vararg_with_other_name), METH_FASTCALL, test_vararg_with_other_name__doc__},

static PyObject *
test_vararg_with_other_name_impl(PyObject *module, Py_ssize_t varargssize,
PyObject *const *strange_name_vararg);

static PyObject *
test_vararg_with_other_name(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
Py_ssize_t varargssize = nargs;
PyObject *const *strange_name_vararg;

if (!_PyArg_CheckPositional("test_vararg_with_other_name", nargs, 0, PY_SSIZE_T_MAX)) {
goto exit;
}
strange_name_vararg = args + 0;
return_value = test_vararg_with_other_name_impl(module, varargssize, strange_name_vararg);

exit:
return return_value;
}

static PyObject *
test_vararg_with_other_name_impl(PyObject *module, Py_ssize_t varargssize,
PyObject *const *strange_name_vararg)
/*[clinic end generated code: output=7340df6f8a2b95a8 input=44747a55460ea494]*/

/*[clinic input]
test_vararg_with_default_with_other_name

a as other_a: object
*args as other_args: object
b as other_b: bool = False

[clinic start generated code]*/

PyDoc_STRVAR(test_vararg_with_default_with_other_name__doc__,
"test_vararg_with_default_with_other_name($module, /, a, *args, b=False)\n"
"--\n"
"\n");

#define TEST_VARARG_WITH_DEFAULT_WITH_OTHER_NAME_METHODDEF \
{"test_vararg_with_default_with_other_name", _PyCFunction_CAST(test_vararg_with_default_with_other_name), METH_FASTCALL|METH_KEYWORDS, test_vararg_with_default_with_other_name__doc__},

static PyObject *
test_vararg_with_default_with_other_name_impl(PyObject *module,
PyObject *other_a,
Py_ssize_t varargssize,
PyObject *const *other_args,
int other_b);

static PyObject *
test_vararg_with_default_with_other_name(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)

#define NUM_KEYWORDS 2
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(a), &_Py_ID(b), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)

#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE

static const char * const _keywords[] = {"a", "b", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "test_vararg_with_default_with_other_name",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
PyObject *const *fastargs;
Py_ssize_t noptargs = Py_MIN(nargs, 1) + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Py_ssize_t varargssize = Py_MAX(nargs - 1, 0);
PyObject *other_a;
PyObject *const *other_args;
int other_b = 0;

fastargs = _PyArg_UnpackKeywordsWithVarargKwonly(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, argsbuf);
if (!fastargs) {
goto exit;
}
other_a = args[0];
other_args = args + 1;
if (!noptargs) {
goto skip_optional_kwonly;
}
other_b = PyObject_IsTrue(fastargs[0]);
if (other_b < 0) {
goto exit;
}
skip_optional_kwonly:
return_value = test_vararg_with_default_with_other_name_impl(module, other_a, varargssize, other_args, other_b);

exit:
return return_value;
}

static PyObject *
test_vararg_with_default_with_other_name_impl(PyObject *module,
PyObject *other_a,
Py_ssize_t varargssize,
PyObject *const *other_args,
int other_b)
/*[clinic end generated code: output=d2149d17eba67671 input=779a17a90096c922]*/


/*[clinic input]
module TestModule
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,17 @@ def test_vararg_with_only_defaults(self):
self.assertEqual(ac_tester.vararg_with_only_defaults(1, 2, 3, 4), ((1, 2, 3, 4), None))
self.assertEqual(ac_tester.vararg_with_only_defaults(1, 2, 3, 4, b=5), ((1, 2, 3, 4), 5))

def test_vararg_with_other_name(self):
self.assertEqual(ac_tester.vararg_with_other_name(), ((), ))
self.assertEqual(ac_tester.vararg_with_other_name(1, 2, 3, 4), ((1, 2, 3, 4), ))

def test_vararg_with_default_with_other_name(self):
with self.assertRaises(TypeError):
ac_tester.vararg_with_default_with_other_name()
self.assertEqual(ac_tester.vararg_with_default_with_other_name(1, b=False), (1, (), False))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add an exception test with _b.

self.assertEqual(ac_tester.vararg_with_default_with_other_name(1, 2, 3, 4), (1, (2, 3, 4), False))
self.assertEqual(ac_tester.vararg_with_default_with_other_name(1, 2, 3, 4, b=True), (1, (2, 3, 4), True))

def test_class_new_vararg_only(self):
self.assertEqual(ac_tester.class_new_vararg_only(), ((), ))
self.assertEqual(ac_tester.class_new_vararg_only(1, 2, 3, 4), ((1, 2, 3, 4), ))
Expand Down
51 changes: 51 additions & 0 deletions Modules/_testclinic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,55 @@ vararg_with_only_defaults_impl(PyObject *module, Py_ssize_t varargssize,
}


/*[clinic input]
vararg_with_other_name

*strange_name_vararg: object

[clinic start generated code]*/

static PyObject *
vararg_with_other_name_impl(PyObject *module, Py_ssize_t varargssize,
PyObject *const *strange_name_vararg)
/*[clinic end generated code: output=73a50a717838e561 input=db56857a45dfa50e]*/
{
PyObject *vararg_tuple = pack_varargs_to_tuple(varargssize, strange_name_vararg);
if (!vararg_tuple) {
return NULL;
}
PyObject *result = pack_arguments_newref(1, vararg_tuple);
Py_DECREF(vararg_tuple);
return result;
}


/*[clinic input]
vararg_with_default_with_other_name

a as other_a: object
*args as other_args: object
b as other_b: bool = False

[clinic start generated code]*/

static PyObject *
vararg_with_default_with_other_name_impl(PyObject *module, PyObject *other_a,
Py_ssize_t varargssize,
PyObject *const *other_args,
int other_b)
/*[clinic end generated code: output=a7ac0fa0c132a04f input=b895540d71cd74f9]*/
{
PyObject *vararg_tuple = pack_varargs_to_tuple(varargssize, other_args);
if (!vararg_tuple) {
return NULL;
}
PyObject *obj_b = other_b ? Py_True : Py_False;
PyObject *result = pack_arguments_newref(3, other_a, vararg_tuple, obj_b);
Py_DECREF(vararg_tuple);
return result;
}



static PyTypeObject TestClass_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
Expand Down Expand Up @@ -1313,6 +1362,8 @@ static PyMethodDef tester_methods[] = {
VARARG_METHODDEF
VARARG_WITH_DEFAULT_METHODDEF
VARARG_WITH_ONLY_DEFAULTS_METHODDEF
VARARG_WITH_OTHER_NAME_METHODDEF
VARARG_WITH_DEFAULT_WITH_OTHER_NAME_METHODDEF
{"class_new_vararg_only",
_PyCFunction_CAST(_testclinic_TestClassVarargOnly),
METH_VARARGS | METH_KEYWORDS,
Expand Down
102 changes: 101 additions & 1 deletion Modules/clinic/_testclinic.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,9 @@ def parser_body(prototype, *fields, declarations=''):
if p.is_vararg():
if not new_or_init:
parser_code.append(normalize_snippet("""
%s = %s + %d;
%s = args + %d;
""" % (
p.converter.parser_name,
p.name,
vararg
), indent=4))
else:
Expand Down