Debian Codegraph @

pypy/module/cpyext/test/test_capsule.py

raw symbols SCIP
1 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
2
3 class AppTestCapsule(AppTestCpythonExtensionBase):
4 def test_capsule_import(self):
5 module = self.import_extension('foo', [
6 ("set_ptr", "METH_O",
7 """
8 PyObject *capsule, *module;
9 void *ptr = PyLong_AsVoidPtr(args);
10 if (PyErr_Occurred()) return NULL;
11 capsule = PyCapsule_New(ptr, "foo._ptr", NULL);
12 if (PyErr_Occurred()) return NULL;
13 module = PyImport_ImportModule("foo");
14 PyModule_AddObject(module, "_ptr", capsule);
15 Py_DECREF(module);
16 if (PyErr_Occurred()) return NULL;
17 Py_RETURN_NONE;
18 """),
19 ("get_ptr", "METH_NOARGS",
20 """
21 void *ptr = PyCapsule_Import("foo._ptr", 0);
22 if (PyErr_Occurred()) return NULL;
23 return PyLong_FromVoidPtr(ptr);
24 """)])
25 module.set_ptr(1234)
26 assert 'capsule object "foo._ptr" at ' in str(module._ptr)
27 import gc; gc.collect()
28 assert module.get_ptr() == 1234
29 del module._ptr
30
31 def test_capsule_set(self):
32 # taken from _testcapimodule.c
33 module = self.import_extension('foo', [
34 ("test_capsule", "METH_NOARGS",
35 """
36 PyObject *object;
37 const char *error = NULL;
38 char *known = "known";
39 PyObject *gc_module = PyImport_ImportModule("gc");
40 PyObject *collect = PyObject_GetAttrString(gc_module, "collect");
41 #define FAIL(x) { error = (x); goto exit; }
42
43 #define CHECK_DESTRUCTOR \\
44 PyObject_CallFunction(collect, NULL); \\
45 PyObject_CallFunction(collect, NULL); \\
46 PyObject_CallFunction(collect, NULL); \\
47 if (capsule_error) { \\
48 FAIL(capsule_error); \\
49 } \\
50 else if (!capsule_destructor_call_count) { \\
51 FAIL("destructor not called!"); \\
52 } \\
53 capsule_destructor_call_count = 0;
54
55 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
56 PyCapsule_SetContext(object, capsule_context);
57 capsule_destructor(object);
58 CHECK_DESTRUCTOR;
59 Py_DECREF(object);
60 CHECK_DESTRUCTOR;
61
62 object = PyCapsule_New(known, "ignored", NULL);
63 PyCapsule_SetPointer(object, capsule_pointer);
64 PyCapsule_SetName(object, capsule_name);
65 PyCapsule_SetDestructor(object, capsule_destructor);
66 PyCapsule_SetContext(object, capsule_context);
67 capsule_destructor(object);
68 CHECK_DESTRUCTOR;
69 /* intentionally access using the wrong name */
70 void *pointer2 = PyCapsule_GetPointer(object, "the wrong name");
71 if (!PyErr_Occurred()) {
72 FAIL("PyCapsule_GetPointer should have failed but did not!");
73 }
74 PyErr_Clear();
75 if (pointer2) {
76 if (pointer2 == capsule_pointer) {
77 FAIL("PyCapsule_GetPointer should not have"
78 " returned the internal pointer!");
79 } else {
80 FAIL("PyCapsule_GetPointer should have "
81 "returned NULL pointer but did not!");
82 }
83 }
84 PyCapsule_SetDestructor(object, NULL);
85 Py_DECREF(object);
86 if (capsule_destructor_call_count) {
87 FAIL("destructor called when it should not have been!");
88 }
89
90 exit:
91 Py_DECREF(gc_module);
92 Py_DECREF(collect);
93 if (error) {
94 PyErr_Format(PyExc_RuntimeError, "test_capsule: %s", error);
95 return NULL;
96 }
97 Py_RETURN_NONE;
98 #undef FAIL
99
100 """),
101 ], prologue="""
102 #include <Python.h>
103 /* Coverage testing of capsule objects. */
104
105 static const char *capsule_name = "capsule name";
106 static char *capsule_pointer = "capsule pointer";
107 static char *capsule_context = "capsule context";
108 static const char *capsule_error = NULL;
109 static int
110 capsule_destructor_call_count = 0;
111
112 static void
113 capsule_destructor(PyObject *o) {
114 capsule_destructor_call_count++;
115 if (PyCapsule_GetContext(o) != capsule_context) {
116 capsule_error = "context did not match in destructor!";
117 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
118 capsule_error = "destructor did not match in destructor! (woah!)";
119 } else if (PyCapsule_GetName(o) != capsule_name) {
120 capsule_error = "name did not match in destructor!";
121 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
122 capsule_error = "pointer did not match in destructor!";
123 }
124 }
125 """
126 )
127 import gc
128 # make the calls to `collect` in C reach the `debug_collect` mock function
129 _collect = gc.collect
130 gc.collect = self.debug_collect
131 try:
132 module.test_capsule()
133 finally:
134 gc.collect = _collect
135
136 def test_capsule_destructor_via_gc_collect(self):
137 # Like test_capsule_set but the C code calls PyGC_Collect() directly
138 # instead of going through Python's gc.collect. This mirrors what
139 # _testcapimodule.c does in its test_capsule() function.
140 # On CPython, Py_DECREF immediately calls tp_dealloc so PyGC_Collect
141 # is a no-op for refcounted objects. On PyPy, PyGC_Collect() must
142 # trigger rawrefcount collection for the destructor to be called.
143 module = self.import_extension('foo_cap_gc', [
144 ("test_capsule_gc", "METH_NOARGS",
145 """
146 PyObject *object;
147 const char *error = NULL;
148 #define FAIL(x) { error = (x); goto exit; }
149 #define CHECK_DESTRUCTOR \\
150 PyGC_Collect(); \\
151 PyGC_Collect(); \\
152 if (cap_error) { \\
153 FAIL(cap_error); \\
154 } \\
155 else if (!cap_destructor_count) { \\
156 FAIL("destructor not called!"); \\
157 } \\
158 cap_destructor_count = 0;
159
160 object = PyCapsule_New(cap_pointer, cap_name, cap_destructor);
161 PyCapsule_SetContext(object, cap_context);
162 cap_destructor(object);
163 CHECK_DESTRUCTOR;
164 Py_DECREF(object);
165 CHECK_DESTRUCTOR;
166
167 object = PyCapsule_New(cap_pointer, "ignored", NULL);
168 PyCapsule_SetPointer(object, cap_pointer);
169 PyCapsule_SetName(object, cap_name);
170 PyCapsule_SetDestructor(object, cap_destructor);
171 PyCapsule_SetContext(object, cap_context);
172 cap_destructor(object);
173 CHECK_DESTRUCTOR;
174 PyCapsule_SetDestructor(object, NULL);
175 Py_DECREF(object);
176 if (cap_destructor_count) {
177 FAIL("destructor called when it should not have been!");
178 }
179
180 exit:
181 if (error) {
182 PyErr_Format(PyExc_RuntimeError, "test_capsule: %s", error);
183 return NULL;
184 }
185 Py_RETURN_NONE;
186 #undef FAIL
187 #undef CHECK_DESTRUCTOR
188 """),
189 ], prologue="""
190 #include <Python.h>
191 static const char *cap_name = "capsule name";
192 static char *cap_pointer = "capsule pointer";
193 static char *cap_context = "capsule context";
194 static const char *cap_error = NULL;
195 static int cap_destructor_count = 0;
196
197 static void
198 cap_destructor(PyObject *o) {
199 cap_destructor_count++;
200 if (PyCapsule_GetContext(o) != cap_context) {
201 cap_error = "context did not match in destructor!";
202 } else if (PyCapsule_GetDestructor(o) != cap_destructor) {
203 cap_error = "destructor did not match in destructor! (woah!)";
204 } else if (PyCapsule_GetName(o) != cap_name) {
205 cap_error = "name did not match in destructor!";
206 } else if (PyCapsule_GetPointer(o, cap_name) != cap_pointer) {
207 cap_error = "pointer did not match in destructor!";
208 }
209 }
210 """)
211 module.test_capsule_gc()