-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
gh-87390: Fix starred tuple equality and pickling #92249
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
Changes from all commits
335747d
5d384b9
4143451
f33b28c
57e18e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Give ``types.GenericAlias`` constructor an optional third ``bool`` argument that controls whether it represents an unpacked type (e.g. ``*tuple[int, str]``). |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -567,6 +567,9 @@ ga_richcompare(PyObject *a, PyObject *b, int op) | |||||||
|
||||||||
gaobject *aa = (gaobject *)a; | ||||||||
gaobject *bb = (gaobject *)b; | ||||||||
if (aa->starred != bb->starred) { | ||||||||
Py_RETURN_FALSE; | ||||||||
} | ||||||||
int eq = PyObject_RichCompareBool(aa->origin, bb->origin, Py_EQ); | ||||||||
if (eq < 0) { | ||||||||
return NULL; | ||||||||
|
@@ -604,8 +607,13 @@ static PyObject * | |||||||
ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) | ||||||||
{ | ||||||||
gaobject *alias = (gaobject *)self; | ||||||||
return Py_BuildValue("O(OO)", Py_TYPE(alias), | ||||||||
alias->origin, alias->args); | ||||||||
PyObject *starred = PyBool_FromLong(alias->starred); | ||||||||
PyObject *value = Py_BuildValue("O(OOO)", Py_TYPE(alias), | ||||||||
alias->origin, alias->args, starred); | ||||||||
// Avoid double increment of reference count on Py_True/Py_False - once from | ||||||||
// PyBool_FromLong, and once from Py_BuildValue. | ||||||||
Py_CLEAR(starred); | ||||||||
return value; | ||||||||
} | ||||||||
|
||||||||
static PyObject * | ||||||||
|
@@ -685,7 +693,7 @@ static PyGetSetDef ga_properties[] = { | |||||||
* Returns 1 on success, 0 on failure. | ||||||||
*/ | ||||||||
static inline int | ||||||||
setup_ga(gaobject *alias, PyObject *origin, PyObject *args) { | ||||||||
setup_ga(gaobject *alias, PyObject *origin, PyObject *args, bool starred) { | ||||||||
if (!PyTuple_Check(args)) { | ||||||||
args = PyTuple_Pack(1, args); | ||||||||
if (args == NULL) { | ||||||||
|
@@ -700,6 +708,7 @@ setup_ga(gaobject *alias, PyObject *origin, PyObject *args) { | |||||||
alias->origin = origin; | ||||||||
alias->args = args; | ||||||||
alias->parameters = NULL; | ||||||||
alias->starred = starred; | ||||||||
alias->weakreflist = NULL; | ||||||||
|
||||||||
if (PyVectorcall_Function(origin) != NULL) { | ||||||||
|
@@ -718,16 +727,33 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||||||
if (!_PyArg_NoKeywords("GenericAlias", kwds)) { | ||||||||
return NULL; | ||||||||
} | ||||||||
if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) { | ||||||||
if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 3)) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the creation signature in the docs https://docs.python.org/3/library/types.html#types.GenericAlias. Also, this doesn't break backwards compatibility right? IIUC, the two-argument form will still work. |
||||||||
return NULL; | ||||||||
} | ||||||||
|
||||||||
PyObject *origin = PyTuple_GET_ITEM(args, 0); | ||||||||
PyObject *arguments = PyTuple_GET_ITEM(args, 1); | ||||||||
|
||||||||
Comment on lines
734
to
+736
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code has gotten complex enough that I recommend using https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTuple or https://docs.python.org/3/c-api/arg.html#c.PyArg_UnpackTuple instead to parse our arguments for us. The format value list is on that page. But off the top of my head, it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Argument clinic can also be used here. |
||||||||
bool starred; | ||||||||
if (PyTuple_GET_SIZE(args) < 3) { | ||||||||
starred = false; | ||||||||
} else { | ||||||||
PyObject *py_starred = PyTuple_GET_ITEM(args, 2); | ||||||||
if (!PyBool_Check(py_starred)) { | ||||||||
PyErr_SetString(PyExc_TypeError, | ||||||||
"Third argument to constructor of GenericAlias " | ||||||||
"must be a bool (since it's a flag controlling " | ||||||||
"whether the alias is unpacked)"); | ||||||||
Comment on lines
+745
to
+746
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We don't need the explanation. |
||||||||
return NULL; | ||||||||
} | ||||||||
starred = PyLong_AsLong(py_starred); | ||||||||
mrahtz marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised you don't need to cast to (bool) here (or maybe it's implicit, I don't remember how C treats this). |
||||||||
} | ||||||||
|
||||||||
gaobject *self = (gaobject *)type->tp_alloc(type, 0); | ||||||||
if (self == NULL) { | ||||||||
return NULL; | ||||||||
} | ||||||||
if (!setup_ga(self, origin, arguments)) { | ||||||||
if (!setup_ga(self, origin, arguments, starred)) { | ||||||||
Py_DECREF(self); | ||||||||
return NULL; | ||||||||
} | ||||||||
|
@@ -837,7 +863,7 @@ Py_GenericAlias(PyObject *origin, PyObject *args) | |||||||
if (alias == NULL) { | ||||||||
return NULL; | ||||||||
} | ||||||||
if (!setup_ga(alias, origin, args)) { | ||||||||
if (!setup_ga(alias, origin, args, false)) { | ||||||||
Py_DECREF(alias); | ||||||||
return NULL; | ||||||||
} | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
BTW, if you don't want to use the code above, you can avoid the double incref by using the format string
O(OON)
.N
doesn't incref. https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue