Debian Codegraph @

pypy/module/_hpy_universal/test/_vendored/test_cpy_compat.py

raw symbols SCIP
1 from .support import HPyTest #, make_hpy_abi_fixture
2 import pytest
3
4 class TestCPythonCompatibility(HPyTest):
5 USE_CPYEXT = True
6
7
8 # hpy_abi = make_hpy_abi_fixture('with hybrid', class_fixture=True)
9
10 # One note about the supports_refcounts() in the tests below: on
11 # CPython, handles are actually implemented as INCREF/DECREF, so we can
12 # check e.g. after an HPy_Dup the refcnt is += 1. However, on PyPy they
13 # are implemented in a completely different way which is unrelated to the
14 # refcnt (this is the whole point of HPy, after all :)). So in many of the
15 # following tests, checking the actual result of the function doesn't
16 # really make sens on PyPy. We still run the functions to ensure they do
17 # not crash, though.
18
19 def _test_abi(self):
20 mod = self.make_module("""
21 #include <Python.h>
22 HPyDef_METH(f, "f", HPyFunc_NOARGS)
23 static HPy f_impl(HPyContext *ctx, HPy self)
24 {
25 return HPyUnicode_FromString(ctx, HPY_ABI);
26 }
27 @EXPORT(f)
28 @INIT
29 """)
30 hpy_abi = mod.f()
31 expected = self.compiler.hpy_abi
32 if expected in ('hybrid+debug', 'hybrid+trace'):
33 expected = 'hybrid'
34 assert hpy_abi == expected
35
36 def test_frompyobject(self):
37 mod = self.make_module("""
38 #include <Python.h>
39 HPyDef_METH(f, "f", HPyFunc_NOARGS)
40 static HPy f_impl(HPyContext *ctx, HPy self)
41 {
42 PyObject *o = PyList_New(0);
43 Py_ssize_t initial_refcount = o->ob_refcnt;
44 HPy h = HPy_FromPyObject(ctx, o);
45 Py_ssize_t final_refcount = o->ob_refcnt;
46
47 PyList_Append(o, PyLong_FromLong(1234));
48 PyList_Append(o, PyLong_FromSsize_t(final_refcount -
49 initial_refcount));
50 Py_DECREF(o);
51 return h;
52 }
53 @EXPORT(f)
54 @INIT
55 """)
56 x = mod.f()
57 assert x[0] == 1234
58 assert len(x) == 2
59 if self.supports_refcounts():
60 assert x == [1234, +1]
61
62 def test_frompyobject_null(self):
63 mod = self.make_module("""
64 #include <Python.h>
65 HPyDef_METH(f, "f", HPyFunc_NOARGS)
66 static HPy f_impl(HPyContext *ctx, HPy self)
67 {
68 HPy h = HPy_FromPyObject(ctx, NULL);
69 if (HPy_IsNull(h)) {
70 return HPy_Dup(ctx, ctx->h_True);
71 }
72 else {
73 return HPy_Dup(ctx, ctx->h_False);
74 }
75 }
76 @EXPORT(f)
77 @INIT
78 """)
79 assert mod.f()
80
81 def test_aspyobject(self):
82 mod = self.make_module("""
83 #include <Python.h>
84 HPyDef_METH(f, "f", HPyFunc_O)
85 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
86 {
87 PyObject *o = HPy_AsPyObject(ctx, arg);
88 long val = PyLong_AsLong(o);
89 Py_DecRef(o);
90 return HPyLong_FromLong(ctx, val*2);
91 }
92 @EXPORT(f)
93 @INIT
94 """)
95 assert mod.f(21) == 42
96
97 def test_aspyobject_null(self):
98 mod = self.make_module("""
99 #include <Python.h>
100 HPyDef_METH(f, "f", HPyFunc_NOARGS)
101 static HPy f_impl(HPyContext *ctx, HPy self)
102 {
103 PyObject *o = HPy_AsPyObject(ctx, HPy_NULL);
104 if (o == NULL) {
105 return HPy_Dup(ctx, ctx->h_True);
106 }
107 else {
108 return HPy_Dup(ctx, ctx->h_False);
109 }
110 }
111 @EXPORT(f)
112 @INIT
113 """)
114 assert mod.f()
115
116 def test_aspyobject_custom_class(self):
117 mod = self.make_module("""
118 #include <Python.h>
119 HPyDef_METH(f, "f", HPyFunc_O)
120 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
121 {
122 PyObject *o = HPy_AsPyObject(ctx, arg);
123 PyObject *o_res = PyObject_CallMethod(o, "foo", "");
124 HPy h_res = HPy_FromPyObject(ctx, o_res);
125 Py_DecRef(o);
126 Py_DecRef(o_res);
127 return h_res;
128 }
129 @EXPORT(f)
130 @INIT
131 """)
132 class MyClass:
133 def foo(self):
134 return 1234
135 obj = MyClass()
136 assert mod.f(obj) == 1234
137
138 def test_hpy_close(self):
139 mod = self.make_module("""
140 #include <Python.h>
141 HPyDef_METH(f, "f", HPyFunc_NOARGS)
142 static HPy f_impl(HPyContext *ctx, HPy self)
143 {
144 PyObject *o = PyList_New(0);
145
146 HPy h = HPy_FromPyObject(ctx, o);
147 Py_ssize_t initial_refcount = o->ob_refcnt;
148 HPy_Close(ctx, h);
149 Py_ssize_t final_refcount = o->ob_refcnt;
150
151 Py_DECREF(o);
152 return HPyLong_FromLong(ctx, (long)(final_refcount -
153 initial_refcount));
154 }
155 @EXPORT(f)
156 @INIT
157 """)
158 x = mod.f()
159 if self.supports_refcounts():
160 assert x == -1
161
162 def test_hpy_dup(self):
163 mod = self.make_module("""
164 #include <Python.h>
165 HPyDef_METH(f, "f", HPyFunc_NOARGS)
166 static HPy f_impl(HPyContext *ctx, HPy self)
167 {
168 PyObject *o = PyList_New(0);
169
170 HPy h = HPy_FromPyObject(ctx, o);
171 Py_ssize_t initial_refcount = o->ob_refcnt;
172 HPy h2 = HPy_Dup(ctx, h);
173 Py_ssize_t final_refcount = o->ob_refcnt;
174
175 HPy_Close(ctx, h);
176 HPy_Close(ctx, h2);
177 Py_DECREF(o);
178 return HPyLong_FromLong(ctx, (long)(final_refcount -
179 initial_refcount));
180 }
181 @EXPORT(f)
182 @INIT
183 """)
184 x = mod.f()
185 if self.supports_refcounts():
186 assert x == +1
187
188 def test_many_handles(self):
189 if self.runappdirect:
190 num_handles = 10000
191 else:
192 num_handles = 30
193 mod = self.make_module("""
194 #include <Python.h>
195 #define NUM_HANDLES %d
196
197 HPyDef_METH(f, "f", HPyFunc_NOARGS)
198 static HPy f_impl(HPyContext *ctx, HPy self)
199 {
200 PyObject *o = PyList_New(0);
201 Py_ssize_t final_refcount;
202
203 Py_ssize_t result = -42;
204 HPy handles[NUM_HANDLES];
205 int i;
206 Py_ssize_t initial_refcount = o->ob_refcnt;
207 for (i = 0; i < NUM_HANDLES; i++)
208 handles[i] = HPy_FromPyObject(ctx, o);
209 for (i = 0; i < NUM_HANDLES; i++)
210 if (HPy_IsNull(handles[i]))
211 goto error;
212 for (i = 0; i < NUM_HANDLES; i++)
213 HPy_Close(ctx, handles[i]);
214 final_refcount = o->ob_refcnt;
215 result = final_refcount - initial_refcount;
216
217 error:
218 return HPyLong_FromLong(ctx, (long)result);
219 }
220 @EXPORT(f)
221 @INIT
222 """ % num_handles)
223 assert mod.f() == 0