| 1 |
#include <Python.h> |
| 2 |
#include "hpy.h" |
| 3 |
|
| 4 |
#ifndef HPY_ABI_CPYTHON |
| 5 |
|
| 6 |
# include "handles.h" |
| 7 |
#endif |
| 8 |
|
| 9 |
_HPy_HIDDEN HPy |
| 10 |
ctx_Capsule_New(HPyContext *ctx, void *pointer, const char *name, |
| 11 |
HPyCapsule_Destructor *destructor) |
| 12 |
{ |
| 13 |
PyObject *res; |
| 14 |
if (destructor) { |
| 15 |
|
| 16 |
if ((destructor->cpy_trampoline == NULL || destructor->impl == NULL)) { |
| 17 |
PyErr_SetString(PyExc_ValueError, "Invalid HPyCapsule destructor"); |
| 18 |
return HPy_NULL; |
| 19 |
} |
| 20 |
res = PyCapsule_New(pointer, name, destructor->cpy_trampoline); |
| 21 |
} else { |
| 22 |
res = PyCapsule_New(pointer, name, NULL); |
| 23 |
} |
| 24 |
return _py2h(res); |
| 25 |
} |
| 26 |
|
| 27 |
_HPy_HIDDEN int |
| 28 |
ctx_Capsule_SetDestructor(HPyContext *ctx, HPy h_capsule, |
| 29 |
HPyCapsule_Destructor *destructor) |
| 30 |
{ |
| 31 |
if (destructor) { |
| 32 |
|
| 33 |
if ((destructor->cpy_trampoline == NULL || destructor->impl == NULL)) { |
| 34 |
PyErr_SetString(PyExc_ValueError, "Invalid HPyCapsule destructor"); |
| 35 |
return -1; |
| 36 |
} |
| 37 |
return PyCapsule_SetDestructor(_h2py(h_capsule), destructor->cpy_trampoline); |
| 38 |
} |
| 39 |
return PyCapsule_SetDestructor(_h2py(h_capsule), NULL); |
| 40 |
} |
| 41 |
|
| 42 |
#ifndef HPY_ABI_CPYTHON |
| 43 |
_HPy_HIDDEN void * |
| 44 |
ctx_Capsule_Get(HPyContext *ctx, HPy capsule, _HPyCapsule_key key, const char *name) |
| 45 |
{ |
| 46 |
switch (key) |
| 47 |
{ |
| 48 |
case HPyCapsule_key_Pointer: |
| 49 |
return PyCapsule_GetPointer(_h2py(capsule), name); |
| 50 |
case HPyCapsule_key_Name: |
| 51 |
return (void *) PyCapsule_GetName(_h2py(capsule)); |
| 52 |
case HPyCapsule_key_Context: |
| 53 |
return PyCapsule_GetContext(_h2py(capsule)); |
| 54 |
case HPyCapsule_key_Destructor: |
| 55 |
PyErr_SetString(PyExc_ValueError, "Invalid operation: get HPyCapsule_key_Destructor"); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
assert(0); |
| 60 |
return NULL; |
| 61 |
} |
| 62 |
|
| 63 |
_HPy_HIDDEN int |
| 64 |
ctx_Capsule_Set(HPyContext *ctx, HPy capsule, _HPyCapsule_key key, void *value) |
| 65 |
{ |
| 66 |
switch (key) |
| 67 |
{ |
| 68 |
case HPyCapsule_key_Pointer: |
| 69 |
return PyCapsule_SetPointer(_h2py(capsule), value); |
| 70 |
case HPyCapsule_key_Name: |
| 71 |
return PyCapsule_SetName(_h2py(capsule), (const char *) value); |
| 72 |
case HPyCapsule_key_Context: |
| 73 |
return PyCapsule_SetContext(_h2py(capsule), value); |
| 74 |
case HPyCapsule_key_Destructor: |
| 75 |
return ctx_Capsule_SetDestructor(ctx, capsule, (HPyCapsule_Destructor *) value); |
| 76 |
} |
| 77 |
|
| 78 |
assert(0); |
| 79 |
return -1; |
| 80 |
} |
| 81 |
#endif |