| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| 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 | ||||||
|---|---|---|---|---|---|---|---|---|
| Expand Up | @@ -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; | ||||||||
| Expand Down Expand Up | @@ -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 * | ||||||||
| Expand Down Expand Up | @@ -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) { | ||||||||
| Expand All | @@ -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) { | ||||||||
| Expand All | @@ -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)) { | ||||||||
|
Comment thread
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityPlease 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.
Sorry, something went wrong.
All reactions
|
||||||||
| return NULL; | ||||||||
| } | ||||||||
|
|
||||||||
| PyObject *origin = PyTuple_GET_ITEM(args, 0); | ||||||||
| PyObject *arguments = PyTuple_GET_ITEM(args, 1); | ||||||||
|
|
||||||||
|
Comment thread
Comment on lines
734
to
+736
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis 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 OO|O:GenericAlias. Then you keep your bool checking/error raising code below.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityArgument clinic can also be used here.
Sorry, something went wrong.
JelleZijlstra reacted with thumbs up emoji
All reactions
|
||||||||
| 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 thread
Comment on lines
+745
to
+746
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
We don't need the explanation.
Sorry, something went wrong.
All reactions
|
||||||||
| return NULL; | ||||||||
| } | ||||||||
| starred = PyLong_AsLong(py_starred); | ||||||||
|
Comment thread
mrahtz marked this conversation as resolved.
Comment thread
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI'm surprised you don't need to cast to (bool) here (or maybe it's implicit, I don't remember how C treats this).
Sorry, something went wrong.
All reactions
|
||||||||
| } | ||||||||
|
|
||||||||
| 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; | ||||||||
| } | ||||||||
| Expand Down Expand Up | @@ -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; | ||||||||
| } | ||||||||
| Expand Down | ||||||||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityHow 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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.