Skip to content
Merged
Show file tree
Hide file tree
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
Use a new private sys function instead
  • Loading branch information
JelleZijlstra committed Jul 23, 2025
commit 6dc0f4804655b57efd7db6637876910335b09d4d
13 changes: 1 addition & 12 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,10 +1340,7 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):

# gh-135228: Make sure the original class can be garbage collected.
# Bypass mapping proxy to allow __dict__ to be removed
old_cls_dict = cls.__dict__ | _deproxier
old_cls_dict.pop('__dict__', None)
if "__weakref__" in cls.__dict__:
del cls.__weakref__
sys._clear_type_descriptors(cls)

return newcls

Expand Down Expand Up @@ -1739,11 +1736,3 @@ def _replace(self, /, **changes):
# changes that aren't fields, this will correctly raise a
# TypeError.
return self.__class__(**changes)


# Hack to the get the underlying dict out of a mappingproxy
# Use it with: cls.__dict__ | _deproxier
class _Deproxier:
def __ror__(self, other):
return other
_deproxier = _Deproxier()
13 changes: 12 additions & 1 deletion Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2641,6 +2641,38 @@ sys__baserepl_impl(PyObject *module)
Py_RETURN_NONE;
}

/*[clinic input]
sys._clear_type_descriptors

type: object
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
/

Private function for clearing certain descriptors from a type's dictionary.

See gh-135228 for context.
[clinic start generated code]*/

static PyObject *
sys__clear_type_descriptors(PyObject *module, PyObject *type)
/*[clinic end generated code: output=7d5cefcf861909e0 input=5fdc23500d477de6]*/
{
if (!PyType_Check(type)) {
PyErr_SetString(PyExc_TypeError, "argument must be a type");
return NULL;
}
PyTypeObject *typeobj = (PyTypeObject *)(type);
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
PyObject *dict = _PyType_GetDict(typeobj);
if (PyDict_PopString(dict, "__dict__", NULL) < 0) {
return NULL;
}
if (PyDict_PopString(dict, "__weakref__", NULL) < 0) {
return NULL;
}
PyType_Modified(typeobj);
Comment thread
JelleZijlstra marked this conversation as resolved.
Py_RETURN_NONE;
}


/*[clinic input]
sys._is_gil_enabled -> bool

Expand Down Expand Up @@ -2837,6 +2869,7 @@ static PyMethodDef sys_methods[] = {
SYS__STATS_DUMP_METHODDEF
#endif
SYS__GET_CPU_COUNT_CONFIG_METHODDEF
SYS__CLEAR_TYPE_DESCRIPTORS_METHODDEF
SYS__IS_GIL_ENABLED_METHODDEF
SYS__DUMP_TRACELETS_METHODDEF
{NULL, NULL} // sentinel
Expand Down