Debian Codegraph @

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

raw symbols SCIP
1 """
2 NOTE: these tests are also meant to be run as PyPy "applevel" tests.
3
4 This means that global imports will NOT be visible inside the test
5 functions. In particular, you have to "import pytest" inside the test in order
6 to be able to use e.g. pytest.raises (which on PyPy will be implemented by a
7 "fake pytest module")
8 """
9 import pytest
10 from .support import HPyTest, DefaultExtensionTemplate
11
12 class CapsuleTemplate(DefaultExtensionTemplate):
13
14 def DEFINE_strdup(self):
15 return """
16 #include <string.h>
17
18 static char *strdup0(const char *s)
19 {
20 size_t n = strlen(s) + 1;
21 char *copy = (char *) malloc(n * sizeof(char));
22 if (copy == NULL) {
23 return NULL;
24 }
25 strncpy(copy, s, n);
26 return copy;
27 }
28 """
29
30 def DEFINE_SomeObject(self):
31 return """
32 #include <string.h>
33
34 typedef struct {
35 int value;
36 char message[];
37 } SomeObject;
38
39 static SomeObject *create_payload(int value, char *message)
40 {
41 size_t n_message = strlen(message) + 1;
42 SomeObject *pointer = (SomeObject *)
43 malloc(sizeof(SomeObject) + n_message * sizeof(char));
44 if (pointer == NULL) {
45 return NULL;
46 }
47 pointer->value = value;
48 strncpy(pointer->message, message, n_message);
49 return pointer;
50 }
51 """
52
53 def DEFINE_Capsule_New(self, destructor="NULL"):
54 return """
55 #include <string.h>
56
57 static const char *_capsule_name = "some_capsule";
58
59 #define CAPSULE_NAME _capsule_name
60
61 HPyDef_METH(Capsule_New, "capsule_new", HPyFunc_VARARGS)
62 static HPy Capsule_New_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
63 {
64 HPy res;
65 int value;
66 char *message;
67 void *ptr;
68
69 if (nargs > 0)
70 {
71 if (!HPyArg_Parse(ctx, NULL, args, nargs, "is", &value, &message)) {
72 return HPy_NULL;
73 }
74 ptr = (void *) create_payload(value, message);
75 if (ptr == NULL) {
76 HPyErr_SetString(ctx, ctx->h_MemoryError, "out of memory");
77 return HPy_NULL;
78 }
79 res = HPyCapsule_New(ctx, ptr, CAPSULE_NAME, %s);
80 if (HPy_IsNull(res)) {
81 free(ptr);
82 }
83 return res;
84 }
85 /* just for error case testing */
86 return HPyCapsule_New(ctx, NULL, CAPSULE_NAME, NULL);
87 }
88 """ % destructor
89
90 def DEFINE_Payload_Free(self):
91 return """
92 #include <string.h>
93
94 HPyDef_METH(Payload_Free, "payload_free", HPyFunc_O)
95 static HPy Payload_Free_impl(HPyContext *ctx, HPy self, HPy arg)
96 {
97 const char *name = HPyCapsule_GetName(ctx, arg);
98 if (name == NULL && HPyErr_Occurred(ctx)) {
99 return HPy_NULL;
100 }
101
102 void *pointer = HPyCapsule_GetPointer(ctx, arg, name);
103 if (pointer == NULL && HPyErr_Occurred(ctx)) {
104 return HPy_NULL;
105 }
106 free(pointer);
107
108 void *context = HPyCapsule_GetContext(ctx, arg);
109 if (context == NULL && HPyErr_Occurred(ctx)) {
110 return HPy_NULL;
111 }
112 free(context);
113
114 return HPy_Dup(ctx, ctx->h_None);
115 }
116 """
117
118 def DEFINE_Capsule_GetName(self):
119 return """
120 HPyDef_METH(Capsule_GetName, "capsule_getname", HPyFunc_O)
121 static HPy Capsule_GetName_impl(HPyContext *ctx, HPy self, HPy arg)
122 {
123 const char *name = HPyCapsule_GetName(ctx, arg);
124 if (name == NULL) {
125 return HPy_NULL;
126 }
127 return HPyUnicode_FromString(ctx, name);
128 }
129 """
130
131 def DEFINE_Capsule_GetPointer(self):
132 return """
133 static HPy payload_as_tuple(HPyContext *ctx, SomeObject *pointer)
134 {
135 HPy value = HPyLong_FromLong(ctx, pointer->value);
136 HPy message = HPyUnicode_FromString(ctx, pointer->message);
137 HPy result = HPyTuple_Pack(ctx, 2, value, message);
138 HPy_Close(ctx, value);
139 HPy_Close(ctx, message);
140 return result;
141 }
142
143 HPyDef_METH(Capsule_GetPointer, "capsule_getpointer", HPyFunc_O)
144 static HPy Capsule_GetPointer_impl(HPyContext *ctx, HPy self, HPy arg)
145 {
146 SomeObject *pointer = (SomeObject *) HPyCapsule_GetPointer(ctx, arg, CAPSULE_NAME);
147 if (pointer == NULL) {
148 return HPy_NULL;
149 }
150 return payload_as_tuple(ctx, pointer);
151 }
152 """
153
154 class TestHPyCapsule(HPyTest):
155
156 ExtensionTemplate = CapsuleTemplate
157
158 def test_capsule_new(self):
159 import pytest
160 mod = self.make_module("""
161 @DEFINE_SomeObject
162 @DEFINE_Capsule_New
163 @DEFINE_Capsule_GetName
164 @DEFINE_Payload_Free
165
166 @EXPORT(Capsule_New)
167 @EXPORT(Capsule_GetName)
168 @EXPORT(Payload_Free)
169
170 @INIT
171 """)
172 p = mod.capsule_new(789, "Hello, World!")
173 try:
174 assert mod.capsule_getname(p) == "some_capsule"
175 finally:
176 # manually free the payload to avoid a memleak
177 mod.payload_free(p)
178 with pytest.raises(ValueError):
179 mod.capsule_new()
180
181 def test_capsule_getter_and_setter(self):
182 import pytest
183 mod = self.make_module("""
184 #include <string.h>
185
186 @DEFINE_strdup
187 @DEFINE_SomeObject
188 @DEFINE_Capsule_New
189 @DEFINE_Capsule_GetPointer
190 @DEFINE_Capsule_GetName
191 @DEFINE_Payload_Free
192
193 HPyDef_METH(Capsule_SetPointer, "capsule_setpointer", HPyFunc_VARARGS)
194 static HPy Capsule_SetPointer_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
195 {
196 HPy capsule;
197 int value;
198 char *message;
199 int non_null_pointer;
200 if (!HPyArg_Parse(ctx, NULL, args, nargs, "Oisi",
201 &capsule, &value, &message, &non_null_pointer)) {
202 return HPy_NULL;
203 }
204
205 /* avoid memleak; get and later free previous pointer */
206 void *old_ptr= HPyCapsule_GetPointer(ctx, capsule, CAPSULE_NAME);
207 if (old_ptr == NULL && HPyErr_Occurred(ctx)) {
208 return HPy_NULL;
209 }
210
211 SomeObject *pointer = NULL;
212 if (non_null_pointer) {
213 pointer = create_payload(value, message);
214 if (pointer == NULL) {
215 HPyErr_SetString(ctx, ctx->h_MemoryError, "out of memory");
216 return HPy_NULL;
217 }
218 }
219
220 if (HPyCapsule_SetPointer(ctx, capsule, (void *) pointer) < 0) {
221 if (non_null_pointer) {
222 free(pointer);
223 }
224 return HPy_NULL;
225 }
226 free(old_ptr);
227 return HPy_Dup(ctx, ctx->h_None);
228 }
229
230 HPyDef_METH(Capsule_GetContext, "capsule_getcontext", HPyFunc_O)
231 static HPy Capsule_GetContext_impl(HPyContext *ctx, HPy self, HPy arg)
232 {
233 SomeObject *context = (SomeObject *) HPyCapsule_GetContext(ctx, arg);
234 if (context == NULL) {
235 return HPyErr_Occurred(ctx) ? HPy_NULL : HPy_Dup(ctx, ctx->h_None);
236 }
237 return payload_as_tuple(ctx, context);
238 }
239
240 HPyDef_METH(Capsule_SetContext, "capsule_setcontext", HPyFunc_VARARGS)
241 static HPy Capsule_SetContext_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
242 {
243 HPy capsule;
244 int value;
245 char *message;
246 if (!HPyArg_Parse(ctx, NULL, args, nargs, "Ois", &capsule, &value, &message)) {
247 return HPy_NULL;
248 }
249
250 /* avoid memleak; get and free previous context */
251 void *old_context = HPyCapsule_GetContext(ctx, capsule);
252 if (old_context == NULL && HPyErr_Occurred(ctx)) {
253 return HPy_NULL;
254 }
255 free(old_context);
256
257 SomeObject *context = create_payload(value, message);
258 if (context == NULL) {
259 HPyErr_SetString(ctx, ctx->h_MemoryError, "out of memory");
260 return HPy_NULL;
261 }
262 if (HPyCapsule_SetContext(ctx, capsule, (void *) context) < 0) {
263 return HPy_NULL;
264 }
265 return HPy_Dup(ctx, ctx->h_None);
266 }
267
268 HPyDef_METH(Capsule_SetName, "capsule_setname", HPyFunc_VARARGS)
269 static HPy Capsule_SetName_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
270 {
271 HPy capsule;
272 const char *name;
273 if (!HPyArg_Parse(ctx, NULL, args, nargs, "Os", &capsule, &name)) {
274 return HPy_NULL;
275 }
276
277 /* avoid memleak; get and free previous context */
278 const char *old_name = HPyCapsule_GetName(ctx, capsule);
279 if (old_name == NULL && HPyErr_Occurred(ctx)) {
280 return HPy_NULL;
281 }
282 if (old_name != CAPSULE_NAME) {
283 free((void *) old_name);
284 }
285
286 char *name_copy = strdup0(name);
287 if (name_copy == NULL) {
288 HPyErr_SetString(ctx, ctx->h_MemoryError, "out of memory");
289 return HPy_NULL;
290 }
291
292 if (HPyCapsule_SetName(ctx, capsule, (const char *) name_copy) < 0) {
293 return HPy_NULL;
294 }
295 return HPy_Dup(ctx, ctx->h_None);
296 }
297
298 static HPyCapsule_Destructor invalid_dtor = { NULL, NULL };
299
300 HPyDef_METH(Capsule_SetDestructor, "capsule_set_destructor", HPyFunc_VARARGS)
301 static HPy Capsule_SetDestructor_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
302 {
303 HPy capsule;
304 HPy null_dtor;
305 HPyCapsule_Destructor *dtor;
306 if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &capsule, &null_dtor)) {
307 return HPy_NULL;
308 }
309
310 if (HPy_IsTrue(ctx, null_dtor)) {
311 dtor = NULL;
312 } else {
313 dtor = &invalid_dtor;
314 }
315
316 if (HPyCapsule_SetDestructor(ctx, capsule, dtor) < 0) {
317 return HPy_NULL;
318 }
319 return HPy_Dup(ctx, ctx->h_None);
320 }
321
322 HPyDef_METH(Capsule_free_name, "capsule_freename", HPyFunc_O)
323 static HPy Capsule_free_name_impl(HPyContext *ctx, HPy self, HPy arg)
324 {
325 /* avoid memleak; get and free previous context */
326 const char *old_name = HPyCapsule_GetName(ctx, arg);
327 if (old_name == NULL && HPyErr_Occurred(ctx)) {
328 return HPy_NULL;
329 }
330 if (old_name != CAPSULE_NAME) {
331 free((void *) old_name);
332 }
333 return HPy_Dup(ctx, ctx->h_None);
334 }
335
336 @EXPORT(Capsule_New)
337 @EXPORT(Capsule_GetPointer)
338 @EXPORT(Capsule_SetPointer)
339 @EXPORT(Capsule_GetContext)
340 @EXPORT(Capsule_SetContext)
341 @EXPORT(Capsule_GetName)
342 @EXPORT(Capsule_SetName)
343 @EXPORT(Capsule_SetDestructor)
344 @EXPORT(Capsule_free_name)
345 @EXPORT(Payload_Free)
346
347 @INIT
348 """)
349 p = mod.capsule_new(789, "Hello, World!")
350 try:
351 assert mod.capsule_getpointer(p) == (789, "Hello, World!")
352 assert mod.capsule_setpointer(p, 456, "lorem ipsum", True) is None
353 assert mod.capsule_getpointer(p) == (456, "lorem ipsum")
354
355 assert mod.capsule_getcontext(p) == None
356 assert mod.capsule_setcontext(p, 123, "hello") is None
357 assert mod.capsule_getcontext(p) == (123, "hello")
358
359 assert mod.capsule_getname(p) == "some_capsule"
360 assert mod.capsule_setname(p, "foo") is None
361 assert mod.capsule_getname(p) == "foo"
362
363 assert mod.capsule_set_destructor(p, True) is None
364
365 not_a_capsule = "hello"
366 with pytest.raises(ValueError):
367 mod.capsule_getpointer(not_a_capsule)
368 with pytest.raises(ValueError):
369 mod.capsule_setpointer(not_a_capsule, 0, "", True)
370 with pytest.raises(ValueError):
371 mod.capsule_setpointer(p, 456, "lorem ipsum", False)
372 with pytest.raises(ValueError):
373 mod.capsule_getcontext(not_a_capsule)
374 with pytest.raises(ValueError):
375 mod.capsule_setcontext(not_a_capsule, 0, "")
376 with pytest.raises(ValueError):
377 mod.capsule_getname(not_a_capsule)
378 with pytest.raises(ValueError):
379 mod.capsule_setname(not_a_capsule, "")
380 with pytest.raises(ValueError):
381 mod.capsule_set_destructor(not_a_capsule, True)
382 with pytest.raises(ValueError):
383 mod.capsule_set_destructor(p, False)
384 finally:
385 # manually free the payload to avoid a memleak
386 mod.payload_free(p)
387 mod.capsule_freename(p)
388
389 def test_capsule_isvalid(self):
390 import pytest
391 mod = self.make_module("""
392 @DEFINE_SomeObject
393 @DEFINE_Capsule_New
394 @DEFINE_Capsule_GetName
395 @DEFINE_Payload_Free
396
397 HPyDef_METH(Capsule_isvalid, "capsule_isvalid", HPyFunc_VARARGS)
398 static HPy Capsule_isvalid_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
399 {
400 HPy capsule;
401 const char *name;
402 if (!HPyArg_Parse(ctx, NULL, args, nargs, "Os", &capsule, &name)) {
403 return HPy_NULL;
404 }
405 return HPyBool_FromLong(ctx, HPyCapsule_IsValid(ctx, capsule, name));
406 }
407
408 @EXPORT(Capsule_New)
409 @EXPORT(Capsule_GetName)
410 @EXPORT(Capsule_isvalid)
411 @EXPORT(Payload_Free)
412
413 @INIT
414 """)
415 p = mod.capsule_new(789, "Hello, World!")
416 name = mod.capsule_getname(p)
417 try:
418 assert mod.capsule_isvalid(p, name)
419 assert not mod.capsule_isvalid(p, "asdf")
420 assert not mod.capsule_isvalid("asdf", name)
421 finally:
422 # manually free the payload to avoid a memleak since the
423 # capsule doesn't have a destructor
424 mod.payload_free(p)
425
426 @pytest.mark.syncgc
427 def test_capsule_new_with_destructor(self):
428 import gc
429 mod = self.make_module("""
430 static int pointer_freed = 0;
431
432 HPyCapsule_DESTRUCTOR(mydtor)
433 static void mydtor_impl(const char *name, void *pointer, void *context)
434 {
435 free(pointer);
436 pointer_freed = 1;
437 }
438
439 @DEFINE_SomeObject
440 @DEFINE_Capsule_New(&mydtor)
441 @DEFINE_Capsule_GetName
442 @DEFINE_Payload_Free
443
444 HPyDef_METH(Pointer_freed, "pointer_freed", HPyFunc_NOARGS)
445 static HPy Pointer_freed_impl(HPyContext *ctx, HPy self)
446 {
447 return HPyBool_FromLong(ctx, pointer_freed);
448 }
449
450 @EXPORT(Capsule_New)
451 @EXPORT(Capsule_GetName)
452 @EXPORT(Pointer_freed)
453
454 @INIT
455 """)
456 p = mod.capsule_new(789, "Hello, World!")
457 assert mod.capsule_getname(p) == "some_capsule"
458 del p
459 self.debug_collect()
460 assert mod.pointer_freed()
461
462 def test_capsule_new_with_invalid_destructor(self):
463 import pytest
464 mod = self.make_module("""
465 static HPyCapsule_Destructor mydtor = { NULL, NULL };
466
467 @DEFINE_SomeObject
468 @DEFINE_Capsule_New(&mydtor)
469 @EXPORT(Capsule_New)
470 @INIT
471 """)
472 with pytest.raises(ValueError):
473 mod.capsule_new(789, "Hello, World!")