mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
[3.13] gh-118814: Fix the TypeVar constructor when name is passed by keyword (GH-122664) (GH-122806)
Fix _PyArg_UnpackKeywordsWithVararg for the case when argument for
positional-or-keyword parameter is passed by keyword.
There was only one such case in the stdlib -- the TypeVar constructor.
(cherry picked from commit 540fcc62f5
)
This commit is contained in:
parent
bc8368e9d0
commit
1fd1c6c738
6 changed files with 165 additions and 7 deletions
|
@ -3334,11 +3334,27 @@ class ClinicFunctionalTest(unittest.TestCase):
|
||||||
self.assertEqual(ac_tester.vararg(1, 2, 3, 4), (1, (2, 3, 4)))
|
self.assertEqual(ac_tester.vararg(1, 2, 3, 4), (1, (2, 3, 4)))
|
||||||
|
|
||||||
def test_vararg_with_default(self):
|
def test_vararg_with_default(self):
|
||||||
with self.assertRaises(TypeError):
|
fn = ac_tester.vararg_with_default
|
||||||
ac_tester.vararg_with_default()
|
self.assertRaises(TypeError, fn)
|
||||||
self.assertEqual(ac_tester.vararg_with_default(1, b=False), (1, (), False))
|
self.assertRaises(TypeError, fn, 1, a=2)
|
||||||
self.assertEqual(ac_tester.vararg_with_default(1, 2, 3, 4), (1, (2, 3, 4), False))
|
self.assertEqual(fn(1, b=2), (1, (), True))
|
||||||
self.assertEqual(ac_tester.vararg_with_default(1, 2, 3, 4, b=True), (1, (2, 3, 4), True))
|
self.assertEqual(fn(1, 2, 3, 4), (1, (2, 3, 4), False))
|
||||||
|
self.assertEqual(fn(1, 2, 3, 4, b=5), (1, (2, 3, 4), True))
|
||||||
|
self.assertEqual(fn(a=1), (1, (), False))
|
||||||
|
self.assertEqual(fn(a=1, b=2), (1, (), True))
|
||||||
|
|
||||||
|
def test_vararg_with_default2(self):
|
||||||
|
fn = ac_tester.vararg_with_default2
|
||||||
|
self.assertRaises(TypeError, fn)
|
||||||
|
self.assertRaises(TypeError, fn, 1, a=2)
|
||||||
|
self.assertEqual(fn(1, b=2), (1, (), 2, None))
|
||||||
|
self.assertEqual(fn(1, b=2, c=3), (1, (), 2, 3))
|
||||||
|
self.assertEqual(fn(1, 2, 3), (1, (2, 3), None, None))
|
||||||
|
self.assertEqual(fn(1, 2, 3, b=4), (1, (2, 3), 4, None))
|
||||||
|
self.assertEqual(fn(1, 2, 3, b=4, c=5), (1, (2, 3), 4, 5))
|
||||||
|
self.assertEqual(fn(a=1), (1, (), None, None))
|
||||||
|
self.assertEqual(fn(a=1, b=2), (1, (), 2, None))
|
||||||
|
self.assertEqual(fn(a=1, b=2, c=3), (1, (), 2, 3))
|
||||||
|
|
||||||
def test_vararg_with_only_defaults(self):
|
def test_vararg_with_only_defaults(self):
|
||||||
self.assertEqual(ac_tester.vararg_with_only_defaults(), ((), None))
|
self.assertEqual(ac_tester.vararg_with_only_defaults(), ((), None))
|
||||||
|
|
|
@ -578,6 +578,55 @@ class TypeVarTests(BaseTestCase):
|
||||||
self.assertEqual(T.__name__, "T")
|
self.assertEqual(T.__name__, "T")
|
||||||
self.assertEqual(T.__constraints__, ())
|
self.assertEqual(T.__constraints__, ())
|
||||||
self.assertIs(T.__bound__, None)
|
self.assertIs(T.__bound__, None)
|
||||||
|
self.assertIs(T.__default__, typing.NoDefault)
|
||||||
|
self.assertIs(T.__covariant__, False)
|
||||||
|
self.assertIs(T.__contravariant__, False)
|
||||||
|
self.assertIs(T.__infer_variance__, False)
|
||||||
|
|
||||||
|
T = TypeVar(name="T", bound=type)
|
||||||
|
self.assertEqual(T.__name__, "T")
|
||||||
|
self.assertEqual(T.__constraints__, ())
|
||||||
|
self.assertIs(T.__bound__, type)
|
||||||
|
self.assertIs(T.__default__, typing.NoDefault)
|
||||||
|
self.assertIs(T.__covariant__, False)
|
||||||
|
self.assertIs(T.__contravariant__, False)
|
||||||
|
self.assertIs(T.__infer_variance__, False)
|
||||||
|
|
||||||
|
T = TypeVar(name="T", default=())
|
||||||
|
self.assertEqual(T.__name__, "T")
|
||||||
|
self.assertEqual(T.__constraints__, ())
|
||||||
|
self.assertIs(T.__bound__, None)
|
||||||
|
self.assertIs(T.__default__, ())
|
||||||
|
self.assertIs(T.__covariant__, False)
|
||||||
|
self.assertIs(T.__contravariant__, False)
|
||||||
|
self.assertIs(T.__infer_variance__, False)
|
||||||
|
|
||||||
|
T = TypeVar(name="T", covariant=True)
|
||||||
|
self.assertEqual(T.__name__, "T")
|
||||||
|
self.assertEqual(T.__constraints__, ())
|
||||||
|
self.assertIs(T.__bound__, None)
|
||||||
|
self.assertIs(T.__default__, typing.NoDefault)
|
||||||
|
self.assertIs(T.__covariant__, True)
|
||||||
|
self.assertIs(T.__contravariant__, False)
|
||||||
|
self.assertIs(T.__infer_variance__, False)
|
||||||
|
|
||||||
|
T = TypeVar(name="T", contravariant=True)
|
||||||
|
self.assertEqual(T.__name__, "T")
|
||||||
|
self.assertEqual(T.__constraints__, ())
|
||||||
|
self.assertIs(T.__bound__, None)
|
||||||
|
self.assertIs(T.__default__, typing.NoDefault)
|
||||||
|
self.assertIs(T.__covariant__, False)
|
||||||
|
self.assertIs(T.__contravariant__, True)
|
||||||
|
self.assertIs(T.__infer_variance__, False)
|
||||||
|
|
||||||
|
T = TypeVar(name="T", infer_variance=True)
|
||||||
|
self.assertEqual(T.__name__, "T")
|
||||||
|
self.assertEqual(T.__constraints__, ())
|
||||||
|
self.assertIs(T.__bound__, None)
|
||||||
|
self.assertIs(T.__default__, typing.NoDefault)
|
||||||
|
self.assertIs(T.__covariant__, False)
|
||||||
|
self.assertIs(T.__contravariant__, False)
|
||||||
|
self.assertIs(T.__infer_variance__, True)
|
||||||
|
|
||||||
|
|
||||||
class TypeParameterDefaultsTests(BaseTestCase):
|
class TypeParameterDefaultsTests(BaseTestCase):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix the :class:`typing.TypeVar` constructor when name is passed by keyword.
|
|
@ -1033,6 +1033,25 @@ vararg_with_default_impl(PyObject *module, PyObject *a, PyObject *args,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*[clinic input]
|
||||||
|
vararg_with_default2
|
||||||
|
|
||||||
|
a: object
|
||||||
|
*args: object
|
||||||
|
b: object = None
|
||||||
|
c: object = None
|
||||||
|
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
vararg_with_default2_impl(PyObject *module, PyObject *a, PyObject *args,
|
||||||
|
PyObject *b, PyObject *c)
|
||||||
|
/*[clinic end generated code: output=a0fb7c37796e2129 input=59fb22f5f0a8925f]*/
|
||||||
|
{
|
||||||
|
return pack_arguments_newref(4, a, args, b, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*[clinic input]
|
/*[clinic input]
|
||||||
vararg_with_only_defaults
|
vararg_with_only_defaults
|
||||||
|
|
||||||
|
@ -1907,6 +1926,7 @@ static PyMethodDef tester_methods[] = {
|
||||||
VARARG_AND_POSONLY_METHODDEF
|
VARARG_AND_POSONLY_METHODDEF
|
||||||
VARARG_METHODDEF
|
VARARG_METHODDEF
|
||||||
VARARG_WITH_DEFAULT_METHODDEF
|
VARARG_WITH_DEFAULT_METHODDEF
|
||||||
|
VARARG_WITH_DEFAULT2_METHODDEF
|
||||||
VARARG_WITH_ONLY_DEFAULTS_METHODDEF
|
VARARG_WITH_ONLY_DEFAULTS_METHODDEF
|
||||||
GH_32092_OOB_METHODDEF
|
GH_32092_OOB_METHODDEF
|
||||||
GH_32092_KW_PASS_METHODDEF
|
GH_32092_KW_PASS_METHODDEF
|
||||||
|
|
74
Modules/clinic/_testclinic.c.h
generated
74
Modules/clinic/_testclinic.c.h
generated
|
@ -2543,6 +2543,78 @@ exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(vararg_with_default2__doc__,
|
||||||
|
"vararg_with_default2($module, /, a, *args, b=None, c=None)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n");
|
||||||
|
|
||||||
|
#define VARARG_WITH_DEFAULT2_METHODDEF \
|
||||||
|
{"vararg_with_default2", _PyCFunction_CAST(vararg_with_default2), METH_FASTCALL|METH_KEYWORDS, vararg_with_default2__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
vararg_with_default2_impl(PyObject *module, PyObject *a, PyObject *args,
|
||||||
|
PyObject *b, PyObject *c);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
vararg_with_default2(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 3
|
||||||
|
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_LATIN1_CHR('a'), _Py_LATIN1_CHR('b'), _Py_LATIN1_CHR('c'), },
|
||||||
|
};
|
||||||
|
#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", "c", NULL};
|
||||||
|
static _PyArg_Parser _parser = {
|
||||||
|
.keywords = _keywords,
|
||||||
|
.fname = "vararg_with_default2",
|
||||||
|
.kwtuple = KWTUPLE,
|
||||||
|
};
|
||||||
|
#undef KWTUPLE
|
||||||
|
PyObject *argsbuf[4];
|
||||||
|
Py_ssize_t noptargs = Py_MIN(nargs, 1) + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
|
||||||
|
PyObject *a;
|
||||||
|
PyObject *__clinic_args = NULL;
|
||||||
|
PyObject *b = Py_None;
|
||||||
|
PyObject *c = Py_None;
|
||||||
|
|
||||||
|
args = _PyArg_UnpackKeywordsWithVararg(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, 1, argsbuf);
|
||||||
|
if (!args) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
a = args[0];
|
||||||
|
__clinic_args = args[1];
|
||||||
|
if (!noptargs) {
|
||||||
|
goto skip_optional_kwonly;
|
||||||
|
}
|
||||||
|
if (args[2]) {
|
||||||
|
b = args[2];
|
||||||
|
if (!--noptargs) {
|
||||||
|
goto skip_optional_kwonly;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c = args[3];
|
||||||
|
skip_optional_kwonly:
|
||||||
|
return_value = vararg_with_default2_impl(module, a, __clinic_args, b, c);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
Py_XDECREF(__clinic_args);
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(vararg_with_only_defaults__doc__,
|
PyDoc_STRVAR(vararg_with_only_defaults__doc__,
|
||||||
"vararg_with_only_defaults($module, /, *args, b=None)\n"
|
"vararg_with_only_defaults($module, /, *args, b=None)\n"
|
||||||
"--\n"
|
"--\n"
|
||||||
|
@ -3219,4 +3291,4 @@ _testclinic_TestClass_get_defining_class_arg(PyObject *self, PyTypeObject *cls,
|
||||||
exit:
|
exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=62aebbac06ec6588 input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=834b0137adc2bbad input=a9049054013a1b77]*/
|
||||||
|
|
|
@ -2574,7 +2574,7 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
|
||||||
*
|
*
|
||||||
* Otherwise, we leave a place at `buf[vararg]` for vararg tuple
|
* Otherwise, we leave a place at `buf[vararg]` for vararg tuple
|
||||||
* so the index is `i + 1`. */
|
* so the index is `i + 1`. */
|
||||||
if (nargs < vararg && i != vararg) {
|
if (i < vararg) {
|
||||||
buf[i] = current_arg;
|
buf[i] = current_arg;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue