Debian Codegraph @

pypy/module/cpyext/capsule.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype, rffi
2 from pypy.module.cpyext.api import (bootstrap_function, slot_function,
3 PyObject, cts, parse_dir)
4 from pypy.module.cpyext.pyobject import (make_ref, from_ref,
5 make_typedescr, track_reference)
6 from pypy.interpreter.error import oefmt
7 from pypy.objspace.std.capsuleobject import W_Capsule
8
9 cts.parse_header(parse_dir / 'cpyext_capsule.h')
10 PyCapsule = cts.gettype("PyCapsule")
11
12 @bootstrap_function
13 def init_capsuleobject(space):
14 "Type description of PyCapsuleobject"
15 make_typedescr(W_Capsule.typedef,
16 basestruct=PyCapsule,
17 attach=capsule_attach,
18 realize=capsule_realize,
19 dealloc=capsule_dealloc,
20 )
21
22 def capsule_attach(space, py_obj, w_obj, w_userdata=None):
23 """
24 Fills a newly allocated PyCapsule with the given capsule object. The
25 value must only be modified through PyCapsule_Set interfaces
26 """
27 assert isinstance(w_obj, W_Capsule)
28 pycapsule_obj = cts.cast("PyCapsule*", py_obj)
29 pycapsule_obj.c_pointer = w_obj.pointer
30 pycapsule_obj.c_name = w_obj.name
31 pycapsule_obj.c_context = w_obj.context
32
33 def capsule_realize(space, obj):
34 # Allocate and fill a w_obj from a pyobj
35 py_obj = cts.cast("PyCapsule*", obj)
36 w_obj = W_Capsule(space, py_obj.c_pointer, py_obj.c_name)
37 w_obj.context = py_obj.c_context
38 track_reference(space, obj, w_obj)
39 return w_obj
40
41 @slot_function([PyObject], lltype.Void)
42 def capsule_dealloc(space, py_obj):
43 """Frees allocated PyBytesObject resources.
44 """
45 from pypy.module.cpyext.object import _dealloc
46 py_capsule = cts.cast("PyCapsule *", py_obj)
47 if py_capsule.c_destructor:
48 py_capsule.c_destructor(py_obj)
49 _dealloc(space, py_obj)
50
51 @cts.decl("""PyObject *
52 PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)""",
53 result_is_ll=True)
54 def PyCapsule_New(space, pointer, name, destructor):
55 if not pointer:
56 raise oefmt(space.w_ValueError, "PyCapsule_New called with null pointer")
57 w_obj = W_Capsule(space, pointer, name)
58 pyobj = cts.cast("PyCapsule *", make_ref(space, w_obj))
59 pyobj.c_destructor = destructor
60 return pyobj
61
62 @cts.decl("int PyCapsule_SetPointer(PyObject *capsule, void *pointer)", error=-1)
63 def PyCapsule_SetPointer(space, py_obj, pointer):
64 # Set both the capsule and the w_obj. We can't use the attach/realize
65 # mechanism since this is in-place modification
66 py_capsule = cts.cast("PyCapsule*", py_obj)
67 py_capsule.c_pointer = pointer
68 w_obj = from_ref(space, py_obj)
69 assert isinstance(w_obj, W_Capsule)
70 w_obj.pointer = pointer
71 return 0
72
73 @cts.decl("int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)", error=-1)
74 def PyCapsule_SetDestructor(space, py_obj, destructor):
75 # Set both the capsule and the w_obj. We can't use the attach/realize
76 # mechanism since this is in-place modification
77 py_capsule = cts.cast("PyCapsule*", py_obj)
78 py_capsule.c_destructor = destructor
79 return 0
80
81 @cts.decl("int PyCapsule_SetName(PyObject *capsule, const char *name)", error=-1)
82 def PyCapsule_SetName(space, py_obj, name):
83 # Set both the capsule and the w_obj. We can't use the attach/realize
84 # mechanism since this is in-place modification
85 py_capsule = cts.cast("PyCapsule*", py_obj)
86 py_capsule.c_name = name
87 w_obj = from_ref(space, py_obj)
88 assert isinstance(w_obj, W_Capsule)
89 w_obj.name = name
90 return 0
91
92 @cts.decl("int PyCapsule_SetContext(PyObject *capsule, void *context)", error=-1)
93 def PyCapsule_SetContext(space, py_obj, context):
94 # Set both the capsule and the w_obj. We can't use the attach/realize
95 # mechanism since this is in-place modification
96 py_capsule = cts.cast("PyCapsule*", py_obj)
97 py_capsule.c_context = context
98 w_obj = from_ref(space, py_obj)
99 assert isinstance(w_obj, W_Capsule)
100 w_obj.context = context
101 return 0