Skip to content

Commit 52ffded

Browse files
[3.13] gh-151130: Add more tests for PyWeakref_* C API (GH-151131) (GH-151141) (#151148)
(cherry picked from commit cb96d5e) (cherry picked from commit c3cd75a)
1 parent 99dc88d commit 52ffded

13 files changed

Lines changed: 250 additions & 2 deletions

Lib/test/test_capi/test_weakref.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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_newref(self):
77+
# Test PyWeakref_NewRef()
78+
newref = _testlimitedcapi.pyweakref_newref
79+
obj = Object()
80+
wr = newref(obj)
81+
self.assertIs(type(wr), weakref.ReferenceType)
82+
# PyWeakref_NewRef() handles None callback as NULL callback
83+
wr = newref(obj, None)
84+
self.assertIs(type(wr), weakref.ReferenceType)
85+
log = []
86+
wr = newref(obj, log.append)
87+
self.assertIs(type(wr), weakref.ReferenceType)
88+
self.assertEqual(log, [])
89+
del obj
90+
self.assertEqual(log, [wr])
91+
92+
self.assertRaises(TypeError, newref, [])
93+
# CRASHES newref(NULL)
94+
95+
def test_pyweakref_newproxy(self):
96+
# Test PyWeakref_NewProxy()
97+
newproxy = _testlimitedcapi.pyweakref_newproxy
98+
obj = Object()
99+
wp = newproxy(obj)
100+
self.assertIs(type(wp), weakref.ProxyType)
101+
# PyWeakref_NewProxy() handles None callback as NULL callback
102+
wp = newproxy(obj, None)
103+
self.assertIs(type(wp), weakref.ProxyType)
104+
log = []
105+
wp = newproxy(obj, log.append)
106+
self.assertIs(type(wp), weakref.ProxyType)
107+
self.assertEqual(log, [])
108+
del obj
109+
self.assertEqual(log, [wp])
110+
111+
def func():
112+
pass
113+
wp = newproxy(func)
114+
self.assertIs(type(wp), weakref.CallableProxyType)
115+
116+
self.assertRaises(TypeError, newproxy, [])
117+
# CRASHES newproxy(NULL)
118+
119+
120+
if __name__ == "__main__":
121+
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add more tests for ``PyWeakref_*`` C API.

Modules/Setup.stdlib.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@
163163
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
164164
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
165165
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
166-
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/monitoring.c
167-
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/file.c
166+
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/monitoring.c _testcapi/weakref.c
167+
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/file.c _testlimitedcapi/weakref.c
168168
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
169169
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
170170

Modules/_testcapi/parts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ int _PyTestCapi_Init_Hash(PyObject *module);
6060
int _PyTestCapi_Init_Time(PyObject *module);
6161
int _PyTestCapi_Init_Monitoring(PyObject *module);
6262
int _PyTestCapi_Init_Object(PyObject *module);
63+
int _PyTestCapi_Init_Weakref(PyObject *mod);
6364

6465
#endif // Py_TESTCAPI_PARTS_H

Modules/_testcapi/weakref.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "parts.h"
2+
#include "util.h"
3+
4+
5+
static PyObject *
6+
pyweakref_getref(PyObject *module, PyObject *ref)
7+
{
8+
NULLABLE(ref);
9+
PyObject *obj = UNINITIALIZED_PTR;
10+
int rc = PyWeakref_GetRef(ref, &obj);
11+
if (rc == -1 && PyErr_Occurred()) {
12+
assert(obj == NULL);
13+
return NULL;
14+
}
15+
if (obj == NULL) {
16+
return Py_BuildValue("i", rc);
17+
}
18+
else {
19+
assert(obj != UNINITIALIZED_PTR);
20+
return Py_BuildValue("iN", rc, obj);
21+
}
22+
}
23+
24+
25+
static PyMethodDef test_methods[] = {
26+
{"pyweakref_getref", pyweakref_getref, METH_O},
27+
{NULL},
28+
};
29+
30+
int
31+
_PyTestCapi_Init_Weakref(PyObject *m)
32+
{
33+
return PyModule_AddFunctions(m, test_methods);
34+
}

Modules/_testcapimodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4329,6 +4329,9 @@ PyInit__testcapi(void)
43294329
if (_PyTestCapi_Init_Object(m) < 0) {
43304330
return NULL;
43314331
}
4332+
if (_PyTestCapi_Init_Weakref(m) < 0) {
4333+
return NULL;
4334+
}
43324335

43334336
PyState_AddModule(m, &_testcapimodule);
43344337
return m;

Modules/_testlimitedcapi.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,8 @@ PyInit__testlimitedcapi(void)
8686
if (_PyTestLimitedCAPI_Init_File(mod) < 0) {
8787
return NULL;
8888
}
89+
if (_PyTestLimitedCAPI_Init_Weakref(mod) < 0) {
90+
return NULL;
91+
}
8992
return mod;
9093
}

Modules/_testlimitedcapi/parts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ int _PyTestLimitedCAPI_Init_Tuple(PyObject *module);
4141
int _PyTestLimitedCAPI_Init_Unicode(PyObject *module);
4242
int _PyTestLimitedCAPI_Init_VectorcallLimited(PyObject *module);
4343
int _PyTestLimitedCAPI_Init_File(PyObject *module);
44+
int _PyTestLimitedCAPI_Init_Weakref(PyObject *module);
4445

4546
#endif // Py_TESTLIMITEDCAPI_PARTS_H

Modules/_testlimitedcapi/weakref.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "pyconfig.h" // Py_GIL_DISABLED
2+
#ifndef Py_GIL_DISABLED
3+
// Need limited C API 3.5 for PyModule_AddFunctions()
4+
# define Py_LIMITED_API 0x03050000
5+
#endif
6+
7+
#include "parts.h"
8+
#include "util.h"
9+
10+
11+
static PyObject *
12+
pyweakref_check(PyObject *module, PyObject *obj)
13+
{
14+
NULLABLE(obj);
15+
return PyLong_FromLong(PyWeakref_Check(obj));
16+
}
17+
18+
static PyObject *
19+
pyweakref_checkref(PyObject *module, PyObject *obj)
20+
{
21+
NULLABLE(obj);
22+
return PyLong_FromLong(PyWeakref_CheckRef(obj));
23+
}
24+
25+
static PyObject *
26+
pyweakref_checkrefexact(PyObject *module, PyObject *obj)
27+
{
28+
NULLABLE(obj);
29+
return PyLong_FromLong(PyWeakref_CheckRefExact(obj));
30+
}
31+
32+
static PyObject *
33+
pyweakref_checkproxy(PyObject *module, PyObject *obj)
34+
{
35+
NULLABLE(obj);
36+
return PyLong_FromLong(PyWeakref_CheckProxy(obj));
37+
}
38+
39+
static PyObject *
40+
pyweakref_newref(PyObject *module, PyObject *args)
41+
{
42+
PyObject *obj;
43+
PyObject *callback = NULL;
44+
if (!PyArg_ParseTuple(args, "O|O", &obj, &callback)) {
45+
return NULL;
46+
}
47+
NULLABLE(obj);
48+
return PyWeakref_NewRef(obj, callback);
49+
}
50+
51+
static PyObject *
52+
pyweakref_newproxy(PyObject *module, PyObject *args)
53+
{
54+
PyObject *obj;
55+
PyObject *callback = NULL;
56+
if (!PyArg_ParseTuple(args, "O|O", &obj, &callback)) {
57+
return NULL;
58+
}
59+
NULLABLE(obj);
60+
return PyWeakref_NewProxy(obj, callback);
61+
}
62+
63+
64+
static PyMethodDef test_methods[] = {
65+
{"pyweakref_check", pyweakref_check, METH_O},
66+
{"pyweakref_checkref", pyweakref_checkref, METH_O},
67+
{"pyweakref_checkrefexact", pyweakref_checkrefexact, METH_O},
68+
{"pyweakref_checkproxy", pyweakref_checkproxy, METH_O},
69+
{"pyweakref_newref", pyweakref_newref, METH_VARARGS},
70+
{"pyweakref_newproxy", pyweakref_newproxy, METH_VARARGS},
71+
{NULL},
72+
};
73+
74+
int
75+
_PyTestLimitedCAPI_Init_Weakref(PyObject *m)
76+
{
77+
return PyModule_AddFunctions(m, test_methods);
78+
}

PCbuild/_testcapi.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<ClCompile Include="..\Modules\_testcapi\gc.c" />
127127
<ClCompile Include="..\Modules\_testcapi\run.c" />
128128
<ClCompile Include="..\Modules\_testcapi\monitoring.c" />
129+
<ClCompile Include="..\Modules\_testcapi\weakref.c" />
129130
</ItemGroup>
130131
<ItemGroup>
131132
<ResourceCompile Include="..\PC\python_nt.rc" />

0 commit comments

Comments
 (0)