Skip to content

Commit

Permalink
Merge pull request #308 from fangerer/fa/capsule
Browse files Browse the repository at this point in the history
Introduce capsule API
  • Loading branch information
mattip authored Nov 21, 2022
2 parents 41a5a0a + e619d22 commit 414be8b
Show file tree
Hide file tree
Showing 17 changed files with 1,009 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hpy/debug/src/autogen_debug_ctx_init.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions hpy/debug/src/autogen_debug_wrappers.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions hpy/devel/include/hpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,23 @@ typedef struct _HPyContext_s HPyContext;
typedef intptr_t HPy_ssize_t;
typedef intptr_t HPy_hash_t;
typedef uint32_t HPy_UCS4;

/* HPyCapsule field keys */
typedef enum {
HPyCapsule_key_Pointer = 0,
HPyCapsule_key_Name = 1,
HPyCapsule_key_Context = 2,
HPyCapsule_key_Destructor = 3,
} _HPyCapsule_key;

#else
typedef Py_ssize_t HPy_ssize_t;
typedef Py_hash_t HPy_hash_t;
typedef Py_UCS4 HPy_UCS4;
#endif

typedef void (*HPyCapsule_Destructor)(const char *name, void *pointer, void *context);


/* ~~~~~~~~~~~~~~~~ Additional #includes ~~~~~~~~~~~~~~~~ */

Expand Down
5 changes: 5 additions & 0 deletions hpy/devel/include/hpy/cpython/autogen_api_impl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions hpy/devel/include/hpy/cpython/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,49 @@ HPyAPI_FUNC int HPyErr_Occurred(HPyContext *ctx) {
return ctx_Err_Occurred(ctx);
}

HPyAPI_FUNC HPy HPyCapsule_New(HPyContext *ctx, void *pointer, const char *name, HPyCapsule_Destructor destructor)
{
return ctx_Capsule_New(ctx, pointer, name, destructor);
}

HPyAPI_FUNC void * HPyCapsule_GetPointer(HPyContext *ctx, HPy capsule, const char *name)
{
return PyCapsule_GetPointer(_h2py(capsule), name);
}

HPyAPI_FUNC const char * HPyCapsule_GetName(HPyContext *ctx, HPy capsule)
{
return PyCapsule_GetName(_h2py(capsule));
}

HPyAPI_FUNC void * HPyCapsule_GetContext(HPyContext *ctx, HPy capsule)
{
return PyCapsule_GetContext(_h2py(capsule));
}

HPyAPI_FUNC HPyCapsule_Destructor HPyCapsule_GetDestructor(HPyContext *ctx, HPy capsule)
{
return ctx_Capsule_GetDestructor(ctx, capsule);
}

HPyAPI_FUNC int HPyCapsule_SetPointer(HPyContext *ctx, HPy capsule, void *pointer)
{
return PyCapsule_SetPointer(_h2py(capsule), pointer);
}

HPyAPI_FUNC int HPyCapsule_SetName(HPyContext *ctx, HPy capsule, const char *name)
{
return PyCapsule_SetName(_h2py(capsule), name);
}

HPyAPI_FUNC int HPyCapsule_SetContext(HPyContext *ctx, HPy capsule, void *context)
{
return PyCapsule_SetContext(_h2py(capsule), context);
}

HPyAPI_FUNC int HPyCapsule_SetDestructor(HPyContext *ctx, HPy capsule, HPyCapsule_Destructor destructor)
{
return ctx_Capsule_SetDestructor(ctx, capsule, destructor);
}

#endif /* !HPY_CPYTHON_MISC_H */
21 changes: 21 additions & 0 deletions hpy/devel/include/hpy/runtime/ctx_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ _HPy_HIDDEN void ctx_TupleBuilder_Cancel(HPyContext *ctx,
// ctx_tuple.c
_HPy_HIDDEN HPy ctx_Tuple_FromArray(HPyContext *ctx, HPy items[], HPy_ssize_t n);

// ctx_capsule.c
_HPy_HIDDEN HPy ctx_Capsule_New(HPyContext *ctx,
void *pointer,
const char *name,
HPyCapsule_Destructor destructor);
_HPy_HIDDEN HPyCapsule_Destructor ctx_Capsule_GetDestructor(HPyContext *ctx,
HPy h_capsule);
_HPy_HIDDEN int ctx_Capsule_SetDestructor(HPyContext *ctx,
HPy h_capsule,
HPyCapsule_Destructor destructor);
#ifdef HPY_UNIVERSAL_ABI
_HPy_HIDDEN void* ctx_Capsule_Get(HPyContext *ctx,
HPy capsule,
_HPyCapsule_key key,
const char *name);
_HPy_HIDDEN int ctx_Capsule_Set(HPyContext *ctx,
HPy capsule,
_HPyCapsule_key key,
void *value);
#endif

// ctx_type.c
_HPy_HIDDEN void* ctx_AsStruct_Object(HPyContext *ctx, HPy h);
_HPy_HIDDEN void* ctx_AsStruct_Legacy(HPyContext *ctx, HPy h);
Expand Down
4 changes: 4 additions & 0 deletions hpy/devel/include/hpy/universal/autogen_ctx.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions hpy/devel/include/hpy/universal/autogen_trampolines.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions hpy/devel/include/hpy/universal/misc_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,61 @@ HPy_FatalError(HPyContext *ctx, const char *message) {
abort();
}

static inline void *
HPyCapsule_GetPointer(HPyContext *ctx, HPy capsule, const char *name)
{
return ctx->ctx_Capsule_Get(
ctx, capsule, HPyCapsule_key_Pointer, name);
}

static inline const char *
HPyCapsule_GetName(HPyContext *ctx, HPy capsule)
{
return (const char *) ctx->ctx_Capsule_Get(
ctx, capsule, HPyCapsule_key_Name, NULL);
}

static inline void *
HPyCapsule_GetContext(HPyContext *ctx, HPy capsule)
{
return ctx->ctx_Capsule_Get(
ctx, capsule, HPyCapsule_key_Context, NULL);
}

static inline HPyCapsule_Destructor
HPyCapsule_GetDestructor(HPyContext *ctx, HPy capsule)
{
return (HPyCapsule_Destructor) ctx->ctx_Capsule_Get(
ctx, capsule, HPyCapsule_key_Destructor, NULL);
}

static inline int
HPyCapsule_SetPointer(HPyContext *ctx, HPy capsule, void *pointer)
{
return ctx->ctx_Capsule_Set(
ctx, capsule, HPyCapsule_key_Pointer, pointer);
}

static inline int
HPyCapsule_SetName(HPyContext *ctx, HPy capsule, const char *name)
{
return ctx->ctx_Capsule_Set(
ctx, capsule, HPyCapsule_key_Name, (void *) name);
}

static inline int
HPyCapsule_SetContext(HPyContext *ctx, HPy capsule, void *context)
{
return ctx->ctx_Capsule_Set(
ctx, capsule, HPyCapsule_key_Context, context);
}

static inline int
HPyCapsule_SetDestructor(HPyContext *ctx, HPy capsule,
HPyCapsule_Destructor destructor)
{
return ctx->ctx_Capsule_Set(
ctx, capsule, HPyCapsule_key_Destructor, (void *) destructor);
}

#endif /* HPY_MISC_TRAMPOLINES_H */
Loading

0 comments on commit 414be8b

Please sign in to comment.