| 1 |
from rpython.rtyper.lltypesystem import rffi |
| 2 |
from rpython.rlib.rarithmetic import widen |
| 3 |
from pypy.interpreter.error import oefmt |
| 4 |
from pypy.module._hpy_universal.apiset import API |
| 5 |
from pypy.module._hpy_universal import llapi |
| 6 |
from pypy.objspace.std.capsuleobject import W_Capsule |
| 7 |
|
| 8 |
Keys = llapi.cts.gettype("_HPyCapsule_key") |
| 9 |
|
| 10 |
def _get_legal_capsule(space, handles, h_capsule, func): |
| 11 |
if not h_capsule: |
| 12 |
raise oefmt(space.w_ValueError, |
| 13 |
"%s called with NULL capsule", func) |
| 14 |
w_capsule = handles.deref(h_capsule) |
| 15 |
if not isinstance(w_capsule, W_Capsule): |
| 16 |
raise oefmt(space.w_ValueError, |
| 17 |
"%s called with invalid capsule object", func) |
| 18 |
if not w_capsule.pointer: |
| 19 |
raise oefmt(space.w_ValueError, |
| 20 |
"%s called with invalid capsule object", func) |
| 21 |
return w_capsule |
| 22 |
|
| 23 |
|
| 24 |
def name_matches(w_capsule, name): |
| 25 |
|
| 26 |
if not name: |
| 27 |
return not w_capsule.name |
| 28 |
if not w_capsule.name: |
| 29 |
return not name |
| 30 |
i = 0 |
| 31 |
while True: |
| 32 |
if w_capsule.name[i] == '\x00': |
| 33 |
return name[i] == '\x00' |
| 34 |
if name[i] != w_capsule.name[i]: |
| 35 |
return False |
| 36 |
i+= 1 |
| 37 |
return True |
| 38 |
|
| 39 |
def check_destructor_impl(space, ptr): |
| 40 |
if not ptr: |
| 41 |
return |
| 42 |
if not ptr.c_impl: |
| 43 |
raise oefmt(space.w_ValueError, |
| 44 |
"invalid HPyCapsule destructor") |
| 45 |
|
| 46 |
@API.func("HPy HPyCapsule_New(HPyContext *ctx, void *pointer, const char *name, HPyCapsule_Destructor *destructor)") |
| 47 |
def HPyCapsule_New(space, handles, ctx, pointer, name, destructor): |
| 48 |
if not pointer: |
| 49 |
raise oefmt(space.w_ValueError, "HPyCapsule_New called with null pointer") |
| 50 |
check_destructor_impl(space, destructor) |
| 51 |
w_capsule = W_Capsule(space, pointer, name) |
| 52 |
if destructor: |
| 53 |
w_capsule.set_destructor_hpy(space, destructor) |
| 54 |
return handles.new(w_capsule) |
| 55 |
|
| 56 |
@API.func("void * HPyCapsule_Get(HPyContext *ctx, HPy capsule, int key, const char *name)") |
| 57 |
def HPyCapsule_Get(space, handles, ctx, h_capsule, key, name): |
| 58 |
w_capsule = _get_legal_capsule(space, handles, h_capsule, "HPyCapsule_Get") |
| 59 |
key = widen(key) |
| 60 |
if key == Keys.HPyCapsule_key_Pointer: |
| 61 |
if not name_matches(w_capsule, name): |
| 62 |
raise oefmt(space.w_ValueError, |
| 63 |
"HPyCapsule_Get called with incorrect name") |
| 64 |
return w_capsule.pointer |
| 65 |
elif key == Keys.HPyCapsule_key_Name: |
| 66 |
return rffi.cast(rffi.VOIDP, w_capsule.name) |
| 67 |
elif key == Keys.HPyCapsule_key_Context: |
| 68 |
return w_capsule.context |
| 69 |
elif key == Keys.HPyCapsule_key_Destructor: |
| 70 |
raise oefmt(space.w_ValueError, |
| 71 |
"Invalid operation: get HPyCapsule_key_Destructor") |
| 72 |
return rffi.cast(rffi.VOIDP, 0) |
| 73 |
|
| 74 |
@API.func("int HPyCapsule_Set(HPyContext *, HPy, int, void *)", error_value=API.int(-1)) |
| 75 |
def HPyCapsule_Set(space, handles, ctx, h_capsule, key, ptr): |
| 76 |
w_capsule = _get_legal_capsule(space, handles, h_capsule, "HPyCapsule_Set") |
| 77 |
key = widen(key) |
| 78 |
if key == Keys.HPyCapsule_key_Pointer: |
| 79 |
w_capsule.pointer = ptr |
| 80 |
elif key == Keys.HPyCapsule_key_Name: |
| 81 |
w_capsule.name = rffi.cast(rffi.CONST_CCHARP, ptr) |
| 82 |
elif key == Keys.HPyCapsule_key_Context: |
| 83 |
w_capsule.context = ptr |
| 84 |
elif key == Keys.HPyCapsule_key_Destructor: |
| 85 |
destructor = llapi.cts.cast("HPyCapsule_Destructor *", ptr) |
| 86 |
check_destructor_impl(space, destructor) |
| 87 |
if destructor: |
| 88 |
w_capsule.set_destructor_hpy(space, destructor) |
| 89 |
else: |
| 90 |
raise oefmt(space.w_ValueError, |
| 91 |
"Invalid operation: unknown key") |
| 92 |
return API.int(0) |
| 93 |
|
| 94 |
@API.func("int HPyCapsule_IsValid(HPyContext *ctx, HPy capsule, const char *utf8_name)", error_value=API.int(-1)) |
| 95 |
def HPyCapsule_IsValid(space, handles, ctx, h_capsule, name): |
| 96 |
if not h_capsule: |
| 97 |
return API.int(0) |
| 98 |
w_capsule = handles.deref(h_capsule) |
| 99 |
if not isinstance(w_capsule, W_Capsule): |
| 100 |
return API.int(0) |
| 101 |
if not w_capsule.pointer: |
| 102 |
return API.int(0) |
| 103 |
return API.int(name_matches(w_capsule, name)) |