|
| 1 | +import weakref |
| 2 | +import unittest |
| 3 | +from test.support import import_helper |
| 4 | + |
| 5 | +_testcapi = import_helper.import_module('_testcapi') |
| 6 | +_testlimitedcapi = import_helper.import_module('_testlimitedcapi') |
| 7 | +NULL = None |
| 8 | + |
| 9 | +class Object: |
| 10 | + pass |
| 11 | + |
| 12 | +class Ref(weakref.ReferenceType): |
| 13 | + pass |
| 14 | + |
| 15 | + |
| 16 | +class CAPIWeakrefTest(unittest.TestCase): |
| 17 | + def test_pyweakref_check(self): |
| 18 | + # Test PyWeakref_Check() |
| 19 | + check = _testlimitedcapi.pyweakref_check |
| 20 | + obj = Object() |
| 21 | + self.assertEqual(check(obj), 0) |
| 22 | + self.assertEqual(check(weakref.ref(obj)), 1) |
| 23 | + self.assertEqual(check(Ref(obj)), 1) |
| 24 | + self.assertEqual(check(weakref.proxy(obj)), 1) |
| 25 | + |
| 26 | + # CRASHES check(NULL) |
| 27 | + |
| 28 | + def test_pyweakref_checkref(self): |
| 29 | + # Test PyWeakref_CheckRef() |
| 30 | + checkref = _testlimitedcapi.pyweakref_checkref |
| 31 | + obj = Object() |
| 32 | + self.assertEqual(checkref(obj), 0) |
| 33 | + self.assertEqual(checkref(weakref.ref(obj)), 1) |
| 34 | + self.assertEqual(checkref(Ref(obj)), 1) |
| 35 | + self.assertEqual(checkref(weakref.proxy(obj)), 0) |
| 36 | + |
| 37 | + # CRASHES checkref(NULL) |
| 38 | + |
| 39 | + def test_pyweakref_checkrefexact(self): |
| 40 | + # Test PyWeakref_CheckRefExact() |
| 41 | + checkrefexact = _testlimitedcapi.pyweakref_checkrefexact |
| 42 | + obj = Object() |
| 43 | + self.assertEqual(checkrefexact(obj), 0) |
| 44 | + self.assertEqual(checkrefexact(weakref.ref(obj)), 1) |
| 45 | + self.assertEqual(checkrefexact(Ref(obj)), 0) |
| 46 | + self.assertEqual(checkrefexact(weakref.proxy(obj)), 0) |
| 47 | + |
| 48 | + # CRASHES checkrefexact(NULL) |
| 49 | + |
| 50 | + def test_pyweakref_checkproxy(self): |
| 51 | + # Test PyWeakref_CheckProxy() |
| 52 | + checkproxy = _testlimitedcapi.pyweakref_checkproxy |
| 53 | + obj = Object() |
| 54 | + self.assertEqual(checkproxy(obj), 0) |
| 55 | + self.assertEqual(checkproxy(weakref.ref(obj)), 0) |
| 56 | + self.assertEqual(checkproxy(Ref(obj)), 0) |
| 57 | + self.assertEqual(checkproxy(weakref.proxy(obj)), 1) |
| 58 | + |
| 59 | + # CRASHES checkproxy(NULL) |
| 60 | + |
| 61 | + def test_pyweakref_getref(self): |
| 62 | + # Test PyWeakref_GetRef() |
| 63 | + getref = _testcapi.pyweakref_getref |
| 64 | + obj = Object() |
| 65 | + wr = weakref.ref(obj) |
| 66 | + wp = weakref.proxy(obj) |
| 67 | + self.assertEqual(getref(wr), (1, obj)) |
| 68 | + self.assertEqual(getref(wp), (1, obj)) |
| 69 | + del obj |
| 70 | + self.assertEqual(getref(wr), 0) |
| 71 | + self.assertEqual(getref(wp), 0) |
| 72 | + |
| 73 | + self.assertRaises(TypeError, getref, 42) |
| 74 | + self.assertRaises(SystemError, getref, NULL) |
| 75 | + |
| 76 | + def test_pyweakref_isdead(self): |
| 77 | + # Test PyWeakref_IsDead() |
| 78 | + isdead = _testcapi.pyweakref_isdead |
| 79 | + obj = Object() |
| 80 | + wr = weakref.ref(obj) |
| 81 | + wp = weakref.proxy(obj) |
| 82 | + self.assertEqual(isdead(wr), 0) |
| 83 | + self.assertEqual(isdead(wp), 0) |
| 84 | + del obj |
| 85 | + self.assertEqual(isdead(wr), 1) |
| 86 | + self.assertEqual(isdead(wp), 1) |
| 87 | + |
| 88 | + self.assertRaises(TypeError, isdead, 42) |
| 89 | + self.assertRaises(SystemError, isdead, NULL) |
| 90 | + |
| 91 | + def test_pyweakref_newref(self): |
| 92 | + # Test PyWeakref_NewRef() |
| 93 | + newref = _testlimitedcapi.pyweakref_newref |
| 94 | + obj = Object() |
| 95 | + wr = newref(obj) |
| 96 | + self.assertIs(type(wr), weakref.ReferenceType) |
| 97 | + # PyWeakref_NewRef() handles None callback as NULL callback |
| 98 | + wr = newref(obj, None) |
| 99 | + self.assertIs(type(wr), weakref.ReferenceType) |
| 100 | + log = [] |
| 101 | + wr = newref(obj, log.append) |
| 102 | + self.assertIs(type(wr), weakref.ReferenceType) |
| 103 | + self.assertEqual(log, []) |
| 104 | + del obj |
| 105 | + self.assertEqual(log, [wr]) |
| 106 | + |
| 107 | + self.assertRaises(TypeError, newref, []) |
| 108 | + # CRASHES newref(NULL) |
| 109 | + |
| 110 | + def test_pyweakref_newproxy(self): |
| 111 | + # Test PyWeakref_NewProxy() |
| 112 | + newproxy = _testlimitedcapi.pyweakref_newproxy |
| 113 | + obj = Object() |
| 114 | + wp = newproxy(obj) |
| 115 | + self.assertIs(type(wp), weakref.ProxyType) |
| 116 | + # PyWeakref_NewProxy() handles None callback as NULL callback |
| 117 | + wp = newproxy(obj, None) |
| 118 | + self.assertIs(type(wp), weakref.ProxyType) |
| 119 | + log = [] |
| 120 | + wp = newproxy(obj, log.append) |
| 121 | + self.assertIs(type(wp), weakref.ProxyType) |
| 122 | + self.assertEqual(log, []) |
| 123 | + del obj |
| 124 | + self.assertEqual(log, [wp]) |
| 125 | + |
| 126 | + def func(): |
| 127 | + pass |
| 128 | + wp = newproxy(func) |
| 129 | + self.assertIs(type(wp), weakref.CallableProxyType) |
| 130 | + |
| 131 | + self.assertRaises(TypeError, newproxy, []) |
| 132 | + # CRASHES newproxy(NULL) |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + unittest.main() |
0 commit comments