mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
GH-133912: Fix PyObject_GenericSetDict
to handle inline values (GH-134725)
This commit is contained in:
parent
f6324bc7ee
commit
9fbd66a93d
6 changed files with 95 additions and 53 deletions
|
@ -1010,6 +1010,8 @@ enum _PyAnnotateFormat {
|
||||||
_Py_ANNOTATE_FORMAT_STRING = 4,
|
_Py_ANNOTATE_FORMAT_STRING = 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int _PyObject_SetDict(PyObject *obj, PyObject *value);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -264,3 +264,13 @@ class TypeTests(unittest.TestCase):
|
||||||
ManualHeapType = _testcapi.ManualHeapType
|
ManualHeapType = _testcapi.ManualHeapType
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
self.assertIsInstance(ManualHeapType(), ManualHeapType)
|
self.assertIsInstance(ManualHeapType(), ManualHeapType)
|
||||||
|
|
||||||
|
def test_extension_managed_dict_type(self):
|
||||||
|
ManagedDictType = _testcapi.ManagedDictType
|
||||||
|
obj = ManagedDictType()
|
||||||
|
obj.foo = 42
|
||||||
|
self.assertEqual(obj.foo, 42)
|
||||||
|
self.assertEqual(obj.__dict__, {'foo': 42})
|
||||||
|
obj.__dict__ = {'bar': 3}
|
||||||
|
self.assertEqual(obj.__dict__, {'bar': 3})
|
||||||
|
self.assertEqual(obj.bar, 3)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix the C API function ``PyObject_GenericSetDict`` to handle extension
|
||||||
|
classes with inline values.
|
|
@ -3175,6 +3175,48 @@ create_manual_heap_type(void)
|
||||||
return (PyObject *)type;
|
return (PyObject *)type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PyObject_VAR_HEAD
|
||||||
|
} ManagedDictObject;
|
||||||
|
|
||||||
|
int ManagedDict_traverse(PyObject *self, visitproc visit, void *arg) {
|
||||||
|
PyObject_VisitManagedDict(self, visit, arg);
|
||||||
|
Py_VISIT(Py_TYPE(self));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ManagedDict_clear(PyObject *self) {
|
||||||
|
PyObject_ClearManagedDict(self);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyGetSetDef ManagedDict_getset[] = {
|
||||||
|
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
|
||||||
|
{NULL, NULL, NULL, NULL, NULL},
|
||||||
|
};
|
||||||
|
|
||||||
|
static PyType_Slot ManagedDict_slots[] = {
|
||||||
|
{Py_tp_new, (void *)PyType_GenericNew},
|
||||||
|
{Py_tp_getset, (void *)ManagedDict_getset},
|
||||||
|
{Py_tp_traverse, (void *)ManagedDict_traverse},
|
||||||
|
{Py_tp_clear, (void *)ManagedDict_clear},
|
||||||
|
{0}
|
||||||
|
};
|
||||||
|
|
||||||
|
static PyType_Spec ManagedDict_spec = {
|
||||||
|
"_testcapi.ManagedDictType",
|
||||||
|
sizeof(ManagedDictObject),
|
||||||
|
0, // itemsize
|
||||||
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_MANAGED_DICT | Py_TPFLAGS_HEAPTYPE | Py_TPFLAGS_HAVE_GC,
|
||||||
|
ManagedDict_slots
|
||||||
|
};
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
create_managed_dict_type(void)
|
||||||
|
{
|
||||||
|
return PyType_FromSpec(&ManagedDict_spec);
|
||||||
|
}
|
||||||
|
|
||||||
static struct PyModuleDef _testcapimodule = {
|
static struct PyModuleDef _testcapimodule = {
|
||||||
PyModuleDef_HEAD_INIT,
|
PyModuleDef_HEAD_INIT,
|
||||||
.m_name = "_testcapi",
|
.m_name = "_testcapi",
|
||||||
|
@ -3315,6 +3357,13 @@ PyInit__testcapi(void)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyObject *managed_dict_type = create_managed_dict_type();
|
||||||
|
if (managed_dict_type == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (PyModule_Add(m, "ManagedDictType", managed_dict_type) < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* Include tests from the _testcapi/ directory */
|
/* Include tests from the _testcapi/ directory */
|
||||||
if (_PyTestCapi_Init_Vectorcall(m) < 0) {
|
if (_PyTestCapi_Init_Vectorcall(m) < 0) {
|
||||||
|
|
|
@ -2016,40 +2016,11 @@ PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
|
||||||
int
|
int
|
||||||
PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
|
PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
|
||||||
{
|
{
|
||||||
PyObject **dictptr = _PyObject_GetDictPtr(obj);
|
|
||||||
if (dictptr == NULL) {
|
|
||||||
if (_PyType_HasFeature(Py_TYPE(obj), Py_TPFLAGS_INLINE_VALUES) &&
|
|
||||||
_PyObject_GetManagedDict(obj) == NULL
|
|
||||||
) {
|
|
||||||
/* Was unable to convert to dict */
|
|
||||||
PyErr_NoMemory();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
PyErr_SetString(PyExc_AttributeError,
|
|
||||||
"This object has no __dict__");
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
|
PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (!PyDict_Check(value)) {
|
return _PyObject_SetDict(obj, value);
|
||||||
PyErr_Format(PyExc_TypeError,
|
|
||||||
"__dict__ must be set to a dictionary, "
|
|
||||||
"not a '%.200s'", Py_TYPE(value)->tp_name);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
Py_BEGIN_CRITICAL_SECTION(obj);
|
|
||||||
PyObject *olddict = *dictptr;
|
|
||||||
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, Py_NewRef(value));
|
|
||||||
#ifdef Py_GIL_DISABLED
|
|
||||||
_PyObject_XDecRefDelayed(olddict);
|
|
||||||
#else
|
|
||||||
Py_XDECREF(olddict);
|
|
||||||
#endif
|
|
||||||
Py_END_CRITICAL_SECTION();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3948,10 +3948,39 @@ subtype_dict(PyObject *obj, void *context)
|
||||||
return PyObject_GenericGetDict(obj, context);
|
return PyObject_GenericGetDict(obj, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
_PyObject_SetDict(PyObject *obj, PyObject *value)
|
||||||
|
{
|
||||||
|
if (value != NULL && !PyDict_Check(value)) {
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"__dict__ must be set to a dictionary, "
|
||||||
|
"not a '%.200s'", Py_TYPE(value)->tp_name);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
|
||||||
|
return _PyObject_SetManagedDict(obj, value);
|
||||||
|
}
|
||||||
|
PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
|
||||||
|
if (dictptr == NULL) {
|
||||||
|
PyErr_SetString(PyExc_AttributeError,
|
||||||
|
"This object has no __dict__");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
Py_BEGIN_CRITICAL_SECTION(obj);
|
||||||
|
PyObject *olddict = *dictptr;
|
||||||
|
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, Py_NewRef(value));
|
||||||
|
#ifdef Py_GIL_DISABLED
|
||||||
|
_PyObject_XDecRefDelayed(olddict);
|
||||||
|
#else
|
||||||
|
Py_XDECREF(olddict);
|
||||||
|
#endif
|
||||||
|
Py_END_CRITICAL_SECTION();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
subtype_setdict(PyObject *obj, PyObject *value, void *context)
|
subtype_setdict(PyObject *obj, PyObject *value, void *context)
|
||||||
{
|
{
|
||||||
PyObject **dictptr;
|
|
||||||
PyTypeObject *base;
|
PyTypeObject *base;
|
||||||
|
|
||||||
base = get_builtin_base_with_dict(Py_TYPE(obj));
|
base = get_builtin_base_with_dict(Py_TYPE(obj));
|
||||||
|
@ -3969,28 +3998,7 @@ subtype_setdict(PyObject *obj, PyObject *value, void *context)
|
||||||
}
|
}
|
||||||
return func(descr, obj, value);
|
return func(descr, obj, value);
|
||||||
}
|
}
|
||||||
/* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */
|
return _PyObject_SetDict(obj, value);
|
||||||
if (value != NULL && !PyDict_Check(value)) {
|
|
||||||
PyErr_Format(PyExc_TypeError,
|
|
||||||
"__dict__ must be set to a dictionary, "
|
|
||||||
"not a '%.200s'", Py_TYPE(value)->tp_name);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
|
|
||||||
return _PyObject_SetManagedDict(obj, value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
dictptr = _PyObject_ComputedDictPointer(obj);
|
|
||||||
if (dictptr == NULL) {
|
|
||||||
PyErr_SetString(PyExc_AttributeError,
|
|
||||||
"This object has no __dict__");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
Py_CLEAR(*dictptr);
|
|
||||||
*dictptr = Py_XNewRef(value);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue