Skip to content

Commit 6dc0f48

Browse files
committed
Use a new private sys function instead
1 parent 916ce1b commit 6dc0f48

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

Lib/dataclasses.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,7 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
13401340

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

13481345
return newcls
13491346

@@ -1739,11 +1736,3 @@ def _replace(self, /, **changes):
17391736
# changes that aren't fields, this will correctly raise a
17401737
# TypeError.
17411738
return self.__class__(**changes)
1742-
1743-
1744-
# Hack to the get the underlying dict out of a mappingproxy
1745-
# Use it with: cls.__dict__ | _deproxier
1746-
class _Deproxier:
1747-
def __ror__(self, other):
1748-
return other
1749-
_deproxier = _Deproxier()

Python/clinic/sysmodule.c.h

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/sysmodule.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,38 @@ sys__baserepl_impl(PyObject *module)
26412641
Py_RETURN_NONE;
26422642
}
26432643

2644+
/*[clinic input]
2645+
sys._clear_type_descriptors
2646+
2647+
type: object
2648+
/
2649+
2650+
Private function for clearing certain descriptors from a type's dictionary.
2651+
2652+
See gh-135228 for context.
2653+
[clinic start generated code]*/
2654+
2655+
static PyObject *
2656+
sys__clear_type_descriptors(PyObject *module, PyObject *type)
2657+
/*[clinic end generated code: output=7d5cefcf861909e0 input=5fdc23500d477de6]*/
2658+
{
2659+
if (!PyType_Check(type)) {
2660+
PyErr_SetString(PyExc_TypeError, "argument must be a type");
2661+
return NULL;
2662+
}
2663+
PyTypeObject *typeobj = (PyTypeObject *)(type);
2664+
PyObject *dict = _PyType_GetDict(typeobj);
2665+
if (PyDict_PopString(dict, "__dict__", NULL) < 0) {
2666+
return NULL;
2667+
}
2668+
if (PyDict_PopString(dict, "__weakref__", NULL) < 0) {
2669+
return NULL;
2670+
}
2671+
PyType_Modified(typeobj);
2672+
Py_RETURN_NONE;
2673+
}
2674+
2675+
26442676
/*[clinic input]
26452677
sys._is_gil_enabled -> bool
26462678
@@ -2837,6 +2869,7 @@ static PyMethodDef sys_methods[] = {
28372869
SYS__STATS_DUMP_METHODDEF
28382870
#endif
28392871
SYS__GET_CPU_COUNT_CONFIG_METHODDEF
2872+
SYS__CLEAR_TYPE_DESCRIPTORS_METHODDEF
28402873
SYS__IS_GIL_ENABLED_METHODDEF
28412874
SYS__DUMP_TRACELETS_METHODDEF
28422875
{NULL, NULL} // sentinel

0 commit comments

Comments
 (0)