bpo-38140: Make dict and weakref offsets opaque for C heap types (#16076)
* Make dict and weakref offsets opaque for C heap types
* Add news
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index b1d045c..79059bc 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -12,6 +12,7 @@
import threading
import time
import unittest
+import weakref
from test import support
from test.support import MISSING_C_DOCSTRINGS
from test.support.script_helper import assert_python_failure, assert_python_ok
@@ -437,6 +438,32 @@
# Test that subtype_dealloc decref the newly assigned __class__ only once
self.assertEqual(new_type_refcnt, sys.getrefcount(A))
+ def test_heaptype_with_dict(self):
+ inst = _testcapi.HeapCTypeWithDict()
+ inst.foo = 42
+ self.assertEqual(inst.foo, 42)
+ self.assertEqual(inst.dictobj, inst.__dict__)
+ self.assertEqual(inst.dictobj, {"foo": 42})
+
+ inst = _testcapi.HeapCTypeWithDict()
+ self.assertEqual({}, inst.__dict__)
+
+ def test_heaptype_with_negative_dict(self):
+ inst = _testcapi.HeapCTypeWithNegativeDict()
+ inst.foo = 42
+ self.assertEqual(inst.foo, 42)
+ self.assertEqual(inst.dictobj, inst.__dict__)
+ self.assertEqual(inst.dictobj, {"foo": 42})
+
+ inst = _testcapi.HeapCTypeWithNegativeDict()
+ self.assertEqual({}, inst.__dict__)
+
+ def test_heaptype_with_weakref(self):
+ inst = _testcapi.HeapCTypeWithWeakref()
+ ref = weakref.ref(inst)
+ self.assertEqual(ref(), inst)
+ self.assertEqual(inst.weakreflist, ref)
+
def test_c_subclass_of_heap_ctype_with_tpdealloc_decrefs_once(self):
subclass_instance = _testcapi.HeapCTypeSubclass()
type_refcnt = sys.getrefcount(_testcapi.HeapCTypeSubclass)