forked from hpyproject/hpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_capsule_legacy.py
86 lines (70 loc) · 2.81 KB
/
test_capsule_legacy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pytest
from .support import HPyTest, make_hpy_abi_fixture
from .test_capsule import CapsuleTemplate
hpy_abi = make_hpy_abi_fixture('with hybrid')
class TestHPyCapsuleLegacy(HPyTest):
ExtensionTemplate = CapsuleTemplate
def test_legacy_capsule_compat(self):
import pytest
mod = self.make_module("""
@DEFINE_strdup
#include <Python.h>
#include <string.h>
static int dummy = 123;
static void legacy_destructor(PyObject *capsule)
{
/* We need to use C lib 'free' because the string was
created with 'strdup0'. */
free((void *) PyCapsule_GetName(capsule));
}
HPyDef_METH(Create_pycapsule, "create_pycapsule", HPyFunc_O)
static HPy Create_pycapsule_impl(HPyContext *ctx, HPy self, HPy arg)
{
HPy_ssize_t n;
const char *name = HPyUnicode_AsUTF8AndSize(ctx, arg, &n);
char *name_copy = strdup0(name);
if (name_copy == NULL) {
HPyErr_SetString(ctx, ctx->h_MemoryError, "out of memory");
return HPy_NULL;
}
PyObject *legacy_caps = PyCapsule_New(&dummy, (const char *) name_copy,
legacy_destructor);
HPy res = HPy_FromPyObject(ctx, legacy_caps);
Py_DECREF(legacy_caps);
return res;
}
HPyDef_METH(Capsule_get, "get", HPyFunc_O)
static HPy Capsule_get_impl(HPyContext *ctx, HPy self, HPy arg)
{
HPy res = HPy_NULL;
HPy h_value = HPy_NULL;
int *ptr = NULL;
const char *name = HPyCapsule_GetName(ctx, arg);
if (name == NULL && HPyErr_Occurred(ctx)) {
return HPy_NULL;
}
HPy h_name = HPyUnicode_FromString(ctx, name);
if (HPy_IsNull(h_name)) {
goto finish;
}
ptr = (int *) HPyCapsule_GetPointer(ctx, arg, name);
if (ptr == NULL && HPyErr_Occurred(ctx)) {
goto finish;
}
h_value = HPyLong_FromLong(ctx, *ptr);
if (HPy_IsNull(h_value)) {
goto finish;
}
res = HPyTuple_Pack(ctx, 2, h_name, h_value);
finish:
HPy_Close(ctx, h_name);
HPy_Close(ctx, h_value);
return res;
}
@EXPORT(Create_pycapsule)
@EXPORT(Capsule_get)
@INIT
""")
name = "legacy_capsule"
p = mod.create_pycapsule(name)
assert mod.get(p) == (name, 123)