Skip to content

gh-135228: When @dataclass(slots=True) replaces a dataclass, make the original class collectible (take 2) #137047

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
more paranoid
  • Loading branch information
JelleZijlstra committed Jul 25, 2025
commit 250f3e9935d8188c27dcd91e6cb61293166475c9
13 changes: 11 additions & 2 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2666,13 +2666,22 @@ sys__clear_type_descriptors(PyObject *module, PyObject *type)
return NULL;
}
PyObject *dict = _PyType_GetDict(typeobj);
if (PyDict_PopString(dict, "__dict__", NULL) < 0) {
PyObject *dunder_dict = NULL;
if (PyDict_PopString(dict, "__dict__", &dunder_dict) < 0) {
return NULL;
}
if (PyDict_PopString(dict, "__weakref__", NULL) < 0) {
PyObject *dunder_weakref = NULL;
if (PyDict_PopString(dict, "__weakref__", &dunder_weakref) < 0) {
PyType_Modified(typeobj);
Py_XDECREF(dunder_dict);
return NULL;
}
PyType_Modified(typeobj);
Copy link
Member Author

Choose a reason for hiding this comment

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

This seems to be technically not thread-safe, because somebody in another thread may be working with the type between the PopString calls and the PyType_Modified call. However, this is also true for the various functions in typeobject.c that modify the type object.

Copy link
Member

Choose a reason for hiding this comment

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

Could anything be deallocated as a result of these pops? Could have a reentrancy problem even without threads present.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess to prevent that we can hold onto a reference to the old value (by passing the third arg to PyDict_PopString) and DECREF it only after we call PyType_Modified.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that seems safer.

// We try to hold onto a reference to these until after we call
// PyType_Modified(), in case their deallocation triggers somer user code
// that tries to do something to the type.
Py_XDECREF(dunder_dict);
Py_XDECREF(dunder_weakref);
Py_RETURN_NONE;
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy