mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
[3.13] gh-132713: Fix typing.Union[index] race condition (GH-132802) (#132839)
gh-132713: Fix typing.Union[index] race condition (GH-132802)
Add union_init_parameters() helper function. Use a critical section
to initialize the 'parameters' member.
(cherry picked from commit dc3e9638c2)
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
3de0f55f34
commit
341b86e095
1 changed files with 20 additions and 11 deletions
|
|
@ -270,16 +270,28 @@ static PyMemberDef union_members[] = {
|
|||
{0}
|
||||
};
|
||||
|
||||
// Populate __parameters__ if needed.
|
||||
static int
|
||||
union_init_parameters(unionobject *alias)
|
||||
{
|
||||
int result = 0;
|
||||
Py_BEGIN_CRITICAL_SECTION(alias);
|
||||
if (alias->parameters == NULL) {
|
||||
alias->parameters = _Py_make_parameters(alias->args);
|
||||
if (alias->parameters == NULL) {
|
||||
result = -1;
|
||||
}
|
||||
}
|
||||
Py_END_CRITICAL_SECTION();
|
||||
return result;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
union_getitem(PyObject *self, PyObject *item)
|
||||
{
|
||||
unionobject *alias = (unionobject *)self;
|
||||
// Populate __parameters__ if needed.
|
||||
if (alias->parameters == NULL) {
|
||||
alias->parameters = _Py_make_parameters(alias->args);
|
||||
if (alias->parameters == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (union_init_parameters(alias) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
|
||||
|
|
@ -314,11 +326,8 @@ static PyObject *
|
|||
union_parameters(PyObject *self, void *Py_UNUSED(unused))
|
||||
{
|
||||
unionobject *alias = (unionobject *)self;
|
||||
if (alias->parameters == NULL) {
|
||||
alias->parameters = _Py_make_parameters(alias->args);
|
||||
if (alias->parameters == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (union_init_parameters(alias) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return Py_NewRef(alias->parameters);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue