8000 [3.14] GH-133912: Fix `PyObject_GenericSetDict` to handle inline valu… · python/cpython@4670ddd · GitHub
[go: up one dir, main page]

Skip to content

Commit 4670ddd

Browse files
[3.14] GH-133912: Fix PyObject_GenericSetDict to handle inline values (GH-134725) (#134859)
1 parent 2d40dd0 commit 4670ddd

File tree

6 files changed

+95
-53
lines changed

6 files changed

+95
-53
lines changed

Include/internal/pycore_object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,8 @@ enum _PyAnnotateFormat {
10071007
_Py_ANNOTATE_FORMAT_STRING = 4,
10081008
};
10091009

1010+
int _PyObject_SetDict(PyObject *obj, PyObject *value);
1011+
10101012
#ifdef __cplusplus
10111013
}
10121014
#endif

Lib/test/test_capi/test_type.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,13 @@ def test_manual_heap_type(self):
264264
ManualHeapType = _testcapi.ManualHeapType
265265
for i in range(100):
266266
self.assertIsInstance(ManualHeapType(), ManualHeapType)
267+
268+
def test_extension_managed_dict_type(self):
269+
ManagedDictType = _testcapi.ManagedDictType
270+
obj = ManagedDictType()
271+
obj.foo = 42
272+
self.assertEqual(obj.foo, 42)
273+
self.assertEqual(obj.__dict__, {'foo': 42})
274+
obj.__dict__ = {'bar': 3}
275+
self.assertEqual(obj.__dict__, {'bar': 3})
276+
self.assertEqual(obj.bar, 3)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the C API function ``PyObject_GenericSetDict`` to handle extension
2+
classes with inline values.

Modules/_testcapimodule.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,6 +3175,48 @@ create_manual_heap_type(void)
31753175
return (PyObject *)type;
31763176
}
31773177

3178+
typedef struct {
3179+
PyObject_VAR_HEAD
3180+
} ManagedDictObject;
3181+
3182+
int ManagedDict_traverse(PyObject *self, visitproc visit, void *arg) {
3183+
PyObject_VisitManagedDict(self, visit, arg);
3184+
Py_VISIT(Py_TYPE(self));
3185+
return 0;
3186+
}
3187+
3188+
int ManagedDict_clear(PyObject *self) {
3189+
PyObject_ClearManagedDict(self);
3190+
return 0;
3191+
}
3192+
3193+
static PyGetSetDef ManagedDict_getset[] = {
3194+
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
3195+
{NULL, NULL, NULL, NULL, NULL},
3196+
};
3197+
3198+
static PyType_Slot ManagedDict_slots[] = {
3199+
{Py_tp_new, (void *)PyType_GenericNew},
3200+
{Py_tp_getset, (void *)ManagedDict_getset},
3201+
{Py_tp_traverse, (void *)ManagedDict_traverse},
3202+
{Py_tp_clear, (void *)ManagedDict_clear},
3203+
{0}
3204+
};
3205+
3206+
static PyType_Spec ManagedDict_spec = {
3207+
"_testcapi.ManagedDictType",
3208+
sizeof(ManagedDictObject),
3209+
0, // itemsize
3210+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_MANAGED_DICT | Py_TPFLAGS_HEAPTYPE | Py_TPFLAGS_HAVE_GC,
3211+
ManagedDict_slots
3212+
};
3213+
3214+
static PyObject *
3215+
create_managed_dict_type(void)
3216+
{
3217+
return PyType_FromSpec(&ManagedDict_spec);
3218+
}
3219+
31783220
static struct PyModuleDef _testcapimodule = {
31793221
PyModuleDef_HEAD_INIT,
31803222
.m_name = "_testcapi",
@@ -3315,6 +3357,13 @@ PyInit__testcapi(void)
33153357
return NULL;
33163358
}
33173359

3360+
PyObject *managed_dict_type = create_managed_dict_type();
3361+
if (managed_dict_type == NULL) {
3362+
return NULL;
3363+
}
3364+
if (PyModule_Add(m, "ManagedDictType", managed_dict_type) < 0) {
3365+
return NULL;
3366+
}
33183367

33193368
/* Include tests from the _testcapi/ directory */
33203369
if (_PyTestCapi_Init_Vectorcall(m) < 0) {

Objects/object.c

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,40 +1906,11 @@ PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
19061906
int
19071907
PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
19081908
{
1909-
PyObject **dictptr = _PyObject_GetDictPtr(obj);
1910-
if (dictptr == NULL) {
1911-
if (_PyType_HasFeature(Py_TYPE(obj), Py_TPFLAGS_INLINE_VALUES) &&
1912-
_PyObject_GetManagedDict(obj) == NULL
1913-
) {
1914-
/* Was unable to convert to dict */
1915-
PyErr_NoMemory();
1916-
}
1917-
else {
1918-
PyErr_SetString(PyExc_AttributeError,
1919-
"This object has no __dict__");
1920-
}
1921-
return -1;
1922-
}
19231909
if (value == NULL) {
19241910
PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
19251911
return -1;
19261912
}
1927-
if (!PyDict_Check(value)) {
1928-
PyErr_Format(PyExc_TypeError,
1929-
"__dict__ must be set to a dictionary, "
1930-
"not a '%.200s'", Py_TYPE(value)->tp_name);
1931-
return -1;
1932-
}
1933-
Py_BEGIN_CRITICAL_SECTION(obj);
1934-
PyObject *olddict = *dictptr;
1935-
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, Py_NewRef(value));
1936-
#ifdef Py_GIL_DISABLED
1937-
_PyObject_XDecRefDelayed(olddict);
1938-
#else
1939-
Py_XDECREF(olddict);
1940-
#endif
1941-
Py_END_CRITICAL_SECTION();
1942-
return 0;
1913+
return _PyObject_SetDict(obj, value);
19431914
}
19441915

19451916

Objects/typeobject.c

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,10 +3676,39 @@ subtype_dict(PyObject *obj, void *context)
36763676
return PyObject_GenericGetDict(obj, context);
36773677
}
36783678

3679+
int
3680+
_PyObject_SetDict(PyObject *obj, PyObject *value)
3681+
{
3682+
if (value != NULL && !PyDict_Check(value)) {
3683+
PyErr_Format(PyExc_TypeError,
3684+
"__dict__ must be set to a dictionary, "
3685+
"not a '%.200s'", Py_TYPE(value)->tp_name);
3686+
return -1;
3687+
}
3688+
if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
3689+
return _PyObject_SetManagedDict(obj, value);
3690+
}
3691+
PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
3692+
if (dictptr == NULL) {
3693+
PyErr_SetString(PyExc_AttributeError,
3694+
"This object has no __dict__");
3695+
return -1;
3696+
}
3697+
Py_BEGIN_CRITICAL_SECTION(obj);
3698+
PyObject *olddict = *dictptr;
3699+
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, Py_NewRef(value));
3700+
#ifdef Py_GIL_DISABLED
3701+
_PyObject_XDecRefDelayed(olddict);
3702+
#else
3703+
Py_XDECREF(olddict);
3704+
#endif
3705+
Py_END_CRITICAL_SECTION();
3706+
return 0;
3707+
}
3708+
36793709
static int
36803710
subtype_setdict(PyObject *obj, PyObject *value, void *context)
36813711
{
3682-
PyObject **dictptr;
36833712
PyTypeObject *base;
36843713

36853714
base = get_builtin_base_with_dict(Py_TYPE(obj));
@@ -3697,28 +3726,7 @@ subtype_setdict(PyObject *obj, PyObject *value, void *context)
36973726
}
36983727
return func(descr, obj, value);
36993728
}
3700-
/* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */
3701-
if (value != NULL && !PyDict_Check(value)) {
3702-
PyErr_Format(PyExc_TypeError,
3703-
"__dict__ must be set to a dictionary, "
3704-
"not a '%.200s'", Py_TYPE(value)->tp_name);
3705-
return -1;
3706-
}
3707-
3708-
if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
3709-
return _PyObject_SetManagedDict(obj, value);
3710-
}
3711-
else {
3712-
dictptr = _PyObject_ComputedDictPointer(obj);
3713-
if (dictptr == NULL) {
3714-
PyErr_SetString(PyExc_AttributeError,
3715-
"This object has no __dict__");
3716-
return -1;
3717-
}
3718-
Py_CLEAR(*dictptr);
3719-
*dictptr = Py_XNewRef(value);
3720-
}
3721-
return 0;
3729+
return _PyObject_SetDict(obj, value);
37223730
}
37233731

37243732
static PyObject *

0 commit comments

Comments
 (0)
0