Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc functions #56

Merged
merged 6 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/porting-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,25 @@ xxx

`PyModule_AddObject()`: replace with a regular `HPy_SetAttr_s()`. There is no `HPyModule_AddObject()` because it has an unusual refcount behaviour (stealing a reference but only when it returns 0).

Py_tp_dealloc becomes HPy_tp_destroy. We changed the name a little bit
because only "lightweight" destructors are supported. Use tp_finalize if
you really need to do things with the context or with the handle of the
object.

PyList_New(5)/PyList_SET_ITEM() becomes::

HPyListBuilder builder = HPyListBuilder_New(ctx, 5);
HPyListBuilder_Set(ctx, builder, 0, h_item0);
...
HPyListBuilder_Append(ctx, builder, h_item5);
...
HPy h_list = HPyListBuilder_Build(ctx, builder);

For lists of (say) integers::

HPyListBuilder_i builder = HPyListBuilder_i_New(ctx, 5);
HPyListBuilder_i_Set(ctx, builder, 0, 42);
...
HPy h_list = HPyListBuilder_i_Build(ctx, builder);

And similar for building tuples or bytes
8 changes: 4 additions & 4 deletions hpy/devel/include/common/autogen_hpyfunc_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#define _HPyFunc_DECLARE_HPyFunc_SSIZESSIZEOBJARGPROC(SYM) static int SYM(HPyContext ctx, HPy, HPy_ssize_t, HPy_ssize_t, HPy)
#define _HPyFunc_DECLARE_HPyFunc_OBJOBJARGPROC(SYM) static int SYM(HPyContext ctx, HPy, HPy, HPy)
#define _HPyFunc_DECLARE_HPyFunc_FREEFUNC(SYM) static void SYM(HPyContext ctx, void *)
#define _HPyFunc_DECLARE_HPyFunc_DESTRUCTOR(SYM) static void SYM(HPyContext ctx, HPy)
#define _HPyFunc_DECLARE_HPyFunc_GETATTRFUNC(SYM) static HPy SYM(HPyContext ctx, HPy, char *)
#define _HPyFunc_DECLARE_HPyFunc_GETATTROFUNC(SYM) static HPy SYM(HPyContext ctx, HPy, HPy)
#define _HPyFunc_DECLARE_HPyFunc_SETATTRFUNC(SYM) static int SYM(HPyContext ctx, HPy, char *, HPy)
Expand All @@ -35,7 +34,8 @@
#define _HPyFunc_DECLARE_HPyFunc_ITERNEXTFUNC(SYM) static HPy SYM(HPyContext ctx, HPy)
#define _HPyFunc_DECLARE_HPyFunc_DESCRGETFUNC(SYM) static HPy SYM(HPyContext ctx, HPy, HPy, HPy)
#define _HPyFunc_DECLARE_HPyFunc_DESCRSETFUNC(SYM) static int SYM(HPyContext ctx, HPy, HPy, HPy)
#define _HPyFunc_DECLARE_HPyFunc_INITPROC(SYM) static int SYM(HPyContext ctx, HPy, HPy, HPy)
#define _HPyFunc_DECLARE_HPyFunc_INITPROC(SYM) static int SYM(HPyContext ctx, HPy self, HPy *args, HPy_ssize_t nargs, HPy kw)
#define _HPyFunc_DECLARE_HPyFunc_DESTROYFUNC(SYM) static void SYM(void *)

typedef HPy (*HPyFunc_noargs)(HPyContext ctx, HPy self);
typedef HPy (*HPyFunc_o)(HPyContext ctx, HPy self, HPy arg);
Expand All @@ -52,7 +52,6 @@ typedef int (*HPyFunc_ssizeobjargproc)(HPyContext ctx, HPy, HPy_ssize_t, HPy);
typedef int (*HPyFunc_ssizessizeobjargproc)(HPyContext ctx, HPy, HPy_ssize_t, HPy_ssize_t, HPy);
typedef int (*HPyFunc_objobjargproc)(HPyContext ctx, HPy, HPy, HPy);
typedef void (*HPyFunc_freefunc)(HPyContext ctx, void *);
typedef void (*HPyFunc_destructor)(HPyContext ctx, HPy);
typedef HPy (*HPyFunc_getattrfunc)(HPyContext ctx, HPy, char *);
typedef HPy (*HPyFunc_getattrofunc)(HPyContext ctx, HPy, HPy);
typedef int (*HPyFunc_setattrfunc)(HPyContext ctx, HPy, char *, HPy);
Expand All @@ -64,4 +63,5 @@ typedef HPy (*HPyFunc_getiterfunc)(HPyContext ctx, HPy);
typedef HPy (*HPyFunc_iternextfunc)(HPyContext ctx, HPy);
typedef HPy (*HPyFunc_descrgetfunc)(HPyContext ctx, HPy, HPy, HPy);
typedef int (*HPyFunc_descrsetfunc)(HPyContext ctx, HPy, HPy, HPy);
typedef int (*HPyFunc_initproc)(HPyContext ctx, HPy, HPy, HPy);
typedef int (*HPyFunc_initproc)(HPyContext ctx, HPy self, HPy *args, HPy_ssize_t nargs, HPy kw);
typedef void (*HPyFunc_destroyfunc)(void *);
15 changes: 15 additions & 0 deletions hpy/devel/include/common/autogen_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ HPyAPI_STORAGE double _HPy_IMPL_NAME(Float_AsDouble)(HPyContext ctx, HPy h)
return PyFloat_AsDouble(_h2py(h));
}

HPyAPI_STORAGE HPy_ssize_t _HPy_IMPL_NAME_NOPREFIX(Length)(HPyContext ctx, HPy h)
{
return PyObject_Length(_h2py(h));
}

HPyAPI_STORAGE int _HPy_IMPL_NAME(Number_Check)(HPyContext ctx, HPy h)
{
return PyNumber_Check(_h2py(h));
Expand Down Expand Up @@ -363,6 +368,11 @@ HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Unicode_FromWideChar)(HPyContext ctx, const wc
return _py2h(PyUnicode_FromWideChar(w, size));
}

HPyAPI_STORAGE int _HPy_IMPL_NAME(List_Check)(HPyContext ctx, HPy h)
{
return PyList_Check(_h2py(h));
}

HPyAPI_STORAGE HPy _HPy_IMPL_NAME(List_New)(HPyContext ctx, HPy_ssize_t len)
{
return _py2h(PyList_New(len));
Expand All @@ -373,6 +383,11 @@ HPyAPI_STORAGE int _HPy_IMPL_NAME(List_Append)(HPyContext ctx, HPy h_list, HPy h
return PyList_Append(_h2py(h_list), _h2py(h_item));
}

HPyAPI_STORAGE int _HPy_IMPL_NAME(Dict_Check)(HPyContext ctx, HPy h)
{
return PyDict_Check(_h2py(h));
}

HPyAPI_STORAGE HPy _HPy_IMPL_NAME(Dict_New)(HPyContext ctx)
{
return _py2h(PyDict_New());
Expand Down
3 changes: 2 additions & 1 deletion hpy/devel/include/common/hpyfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ typedef enum {
HPyFunc_NOARGS = 3, // METH_NOARGS
HPyFunc_O = 4, // METH_O

HPyFunc_DESTROYFUNC,

HPyFunc_UNARYFUNC,
HPyFunc_BINARYFUNC,
HPyFunc_TERNARYFUNC,
Expand All @@ -18,7 +20,6 @@ typedef enum {
HPyFunc_SSIZESSIZEOBJARGPROC,
HPyFunc_OBJOBJARGPROC,
HPyFunc_FREEFUNC,
HPyFunc_DESTRUCTOR,
HPyFunc_GETATTRFUNC,
HPyFunc_GETATTROFUNC,
HPyFunc_SETATTRFUNC,
Expand Down
11 changes: 7 additions & 4 deletions hpy/devel/include/common/typeslots.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ typedef enum {
HPy_sq_item = 44,
HPy_sq_length = 45,
HPy_sq_repeat = 46,
HPy_tp_alloc = 47,
/*HPy_tp_alloc = 47,*/
HPy_tp_base = 48,
HPy_tp_bases = 49,
HPy_tp_call = 50,
HPy_tp_clear = 51,
HPy_tp_dealloc = 52,
/*HPy_tp_dealloc = 52,*/
HPy_tp_del = 53,
HPy_tp_descr_get = 54,
HPy_tp_descr_set = 55,
Expand All @@ -74,13 +74,16 @@ typedef enum {
HPy_tp_traverse = 71,
HPy_tp_members = 72,
HPy_tp_getset = 73,
HPy_tp_free = 74,
/*HPy_tp_free = 74,*/
HPy_nb_matrix_multiply = 75,
HPy_nb_inplace_matrix_multiply = 76,
HPy_am_await = 77,
HPy_am_aiter = 78,
HPy_am_anext = 79,
HPy_tp_finalize = 80
HPy_tp_finalize = 80,

/* extra HPy slots */
HPy_tp_destroy = 1000,
} HPySlot_Slot;

#endif // HPY_UNIVERSAL_TYPESLOTS_H
10 changes: 0 additions & 10 deletions hpy/devel/include/cpython/autogen_hpyfunc_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@
{ \
return (IMPL(_HPyGetContext(), arg0)); \
}
#define _HPyFunc_TRAMPOLINE_HPyFunc_DESTRUCTOR(SYM, IMPL) \
static void SYM(cpy_PyObject *arg0) \
{ \
return (IMPL(_HPyGetContext(), _py2h(arg0))); \
}
#define _HPyFunc_TRAMPOLINE_HPyFunc_GETATTRFUNC(SYM, IMPL) \
static cpy_PyObject *SYM(cpy_PyObject *arg0, char *arg1) \
{ \
Expand Down Expand Up @@ -123,8 +118,3 @@
{ \
return (IMPL(_HPyGetContext(), _py2h(arg0), _py2h(arg1), _py2h(arg2))); \
}
#define _HPyFunc_TRAMPOLINE_HPyFunc_INITPROC(SYM, IMPL) \
static int SYM(cpy_PyObject *arg0, cpy_PyObject *arg1, cpy_PyObject *arg2) \
{ \
return (IMPL(_HPyGetContext(), _py2h(arg0), _py2h(arg1), _py2h(arg2))); \
}
20 changes: 20 additions & 0 deletions hpy/devel/include/cpython/hpyfunc_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,24 @@
items, nargs, _py2h(kw))); \
}

#define _HPyFunc_TRAMPOLINE_HPyFunc_INITPROC(SYM, IMPL) \
static int \
SYM(PyObject *self, PyObject *args, PyObject *kw) \
{ \
/* get the tuple elements as an array of "PyObject *", which */ \
/* is equivalent to an array of "HPy" with enough casting... */ \
HPy *items = (HPy *)&PyTuple_GET_ITEM(args, 0); \
Py_ssize_t nargs = PyTuple_GET_SIZE(args); \
return IMPL(_HPyGetContext(), _py2h(self), \
items, nargs, _py2h(kw)); \
}

#define _HPyFunc_TRAMPOLINE_HPyFunc_DESTROYFUNC(SYM, IMPL) \
static void \
SYM(PyObject *self) \
{ \
IMPL(self); \
Py_TYPE(self)->tp_free(self); \
}

#endif // HPY_CPYTHON_HPYFUNC_TRAMPOLINES_H
4 changes: 4 additions & 0 deletions hpy/devel/include/universal/autogen_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct _HPyContext_s {
long (*ctx_Long_AsLong)(HPyContext ctx, HPy h);
HPy (*ctx_Float_FromDouble)(HPyContext ctx, double v);
double (*ctx_Float_AsDouble)(HPyContext ctx, HPy h);
HPy_ssize_t (*ctx_Length)(HPyContext ctx, HPy h);
int (*ctx_Number_Check)(HPyContext ctx, HPy h);
HPy (*ctx_Add)(HPyContext ctx, HPy h1, HPy h2);
HPy (*ctx_Subtract)(HPyContext ctx, HPy h1, HPy h2);
Expand Down Expand Up @@ -104,12 +105,15 @@ struct _HPyContext_s {
int (*ctx_Unicode_Check)(HPyContext ctx, HPy h);
HPy (*ctx_Unicode_AsUTF8String)(HPyContext ctx, HPy h);
HPy (*ctx_Unicode_FromWideChar)(HPyContext ctx, const wchar_t *w, HPy_ssize_t size);
int (*ctx_List_Check)(HPyContext ctx, HPy h);
HPy (*ctx_List_New)(HPyContext ctx, HPy_ssize_t len);
int (*ctx_List_Append)(HPyContext ctx, HPy h_list, HPy h_item);
int (*ctx_Dict_Check)(HPyContext ctx, HPy h);
HPy (*ctx_Dict_New)(HPyContext ctx);
int (*ctx_Dict_SetItem)(HPyContext ctx, HPy h_dict, HPy h_key, HPy h_val);
HPy (*ctx_Dict_GetItem)(HPyContext ctx, HPy h_dict, HPy h_key);
HPy (*ctx_FromPyObject)(HPyContext ctx, cpy_PyObject *obj);
cpy_PyObject *(*ctx_AsPyObject)(HPyContext ctx, HPy h);
void (*ctx_CallRealFunctionFromTrampoline)(HPyContext ctx, HPyFunc_Signature sig, void *func, void *args);
void (*ctx_CallDestroyAndThenDealloc)(HPyContext ctx, void *func, cpy_PyObject *self);
};
29 changes: 0 additions & 29 deletions hpy/devel/include/universal/autogen_hpyfunc_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,6 @@ typedef struct {
return; \
}

typedef struct {
cpy_PyObject *arg0;
} _HPyFunc_args_DESTRUCTOR;

#define _HPyFunc_TRAMPOLINE_HPyFunc_DESTRUCTOR(SYM, IMPL) \
static void SYM(cpy_PyObject *arg0) \
{ \
_HPyFunc_args_DESTRUCTOR a = { arg0 }; \
_HPy_CallRealFunctionFromTrampoline( \
_ctx_for_trampolines, HPyFunc_DESTRUCTOR, IMPL, &a); \
return; \
}

typedef struct {
cpy_PyObject *arg0;
char *arg1;
Expand Down Expand Up @@ -353,19 +340,3 @@ typedef struct {
return a.result; \
}

typedef struct {
cpy_PyObject *arg0;
cpy_PyObject *arg1;
cpy_PyObject *arg2;
int result;
} _HPyFunc_args_INITPROC;

#define _HPyFunc_TRAMPOLINE_HPyFunc_INITPROC(SYM, IMPL) \
static int SYM(cpy_PyObject *arg0, cpy_PyObject *arg1, cpy_PyObject *arg2) \
{ \
_HPyFunc_args_INITPROC a = { arg0, arg1, arg2 }; \
_HPy_CallRealFunctionFromTrampoline( \
_ctx_for_trampolines, HPyFunc_INITPROC, IMPL, &a); \
return a.result; \
}

16 changes: 16 additions & 0 deletions hpy/devel/include/universal/autogen_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ static inline double HPyFloat_AsDouble(HPyContext ctx, HPy h) {
return ctx->ctx_Float_AsDouble ( ctx, h );
}

static inline HPy_ssize_t HPy_Length(HPyContext ctx, HPy h) {
return ctx->ctx_Length ( ctx, h );
}

static inline int HPyNumber_Check(HPyContext ctx, HPy h) {
return ctx->ctx_Number_Check ( ctx, h );
}
Expand Down Expand Up @@ -336,6 +340,10 @@ static inline HPy HPyUnicode_FromWideChar(HPyContext ctx, const wchar_t *w, HPy_
return ctx->ctx_Unicode_FromWideChar ( ctx, w, size );
}

static inline int HPyList_Check(HPyContext ctx, HPy h) {
return ctx->ctx_List_Check ( ctx, h );
}

static inline HPy HPyList_New(HPyContext ctx, HPy_ssize_t len) {
return ctx->ctx_List_New ( ctx, len );
}
Expand All @@ -344,6 +352,10 @@ static inline int HPyList_Append(HPyContext ctx, HPy h_list, HPy h_item) {
return ctx->ctx_List_Append ( ctx, h_list, h_item );
}

static inline int HPyDict_Check(HPyContext ctx, HPy h) {
return ctx->ctx_Dict_Check ( ctx, h );
}

static inline HPy HPyDict_New(HPyContext ctx) {
return ctx->ctx_Dict_New ( ctx );
}
Expand All @@ -368,3 +380,7 @@ static inline void _HPy_CallRealFunctionFromTrampoline(HPyContext ctx, HPyFunc_S
ctx->ctx_CallRealFunctionFromTrampoline ( ctx, sig, func, args );
}

static inline void _HPy_CallDestroyAndThenDealloc(HPyContext ctx, void *func, cpy_PyObject *self) {
ctx->ctx_CallDestroyAndThenDealloc ( ctx, func, self );
}

27 changes: 27 additions & 0 deletions hpy/devel/include/universal/hpyfunc_trampolines.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ typedef struct {
cpy_PyObject *result;
} _HPyFunc_args_KEYWORDS;

typedef struct {
cpy_PyObject *self;
cpy_PyObject *args;
cpy_PyObject *kw;
int result;
} _HPyFunc_args_INITPROC;


#define _HPyFunc_TRAMPOLINE_HPyFunc_NOARGS(SYM, IMPL) \
static cpy_PyObject * \
Expand Down Expand Up @@ -70,5 +77,25 @@ typedef struct {
return a.result; \
}

#define _HPyFunc_TRAMPOLINE_HPyFunc_INITPROC(SYM, IMPL) \
static int \
SYM(cpy_PyObject *self, cpy_PyObject *args, cpy_PyObject *kw) \
{ \
_HPyFunc_args_INITPROC a = { self, args, kw }; \
_HPy_CallRealFunctionFromTrampoline( \
_ctx_for_trampolines, HPyFunc_INITPROC, IMPL, &a); \
return a.result; \
}

/* special case: this function is used as 'tp_dealloc', but from the user
point of view the slot is HPy_tp_destroy. */
#define _HPyFunc_TRAMPOLINE_HPyFunc_DESTROYFUNC(SYM, IMPL) \
static void \
SYM(cpy_PyObject *self) \
{ \
_HPy_CallDestroyAndThenDealloc( \
_ctx_for_trampolines, IMPL, self); \
}


#endif // HPY_UNIVERSAL_HPYFUNC_TRAMPOLINES_H
11 changes: 10 additions & 1 deletion hpy/devel/src/runtime/ctx_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ legacy_slots_count(PyType_Slot slots[], HPy_ssize_t *slot_count,
}
}

static int
hpy_slot_to_cpy_slot(HPySlot_Slot src)
{
switch (src) {
case HPy_tp_destroy: return Py_tp_dealloc;
default: return src; /* same numeric value by default */
}
}


/*
* Create a PyMethodDef which contains:
Expand Down Expand Up @@ -166,7 +175,7 @@ create_slot_defs(HPyType_Spec *hpyspec)
if (src->kind != HPyDef_Kind_Slot)
continue;
PyType_Slot *dst = &result[dst_idx++];
dst->slot = src->slot.slot;
dst->slot = hpy_slot_to_cpy_slot(src->slot.slot);
dst->pfunc = src->slot.cpy_trampoline;
}
}
Expand Down
8 changes: 5 additions & 3 deletions hpy/tools/autogen/hpyfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .autogenfile import AutoGenFile
from .parse import toC, find_typedecl

SPECIAL_CASES = ('NOARGS', 'O', 'VARARGS', 'KEYWORDS', 'INITPROC',
'DESTROYFUNC')

class autogen_hpyfunc_declare_h(AutoGenFile):
PATH = 'hpy/devel/include/common/autogen_hpyfunc_declare.h'
Expand Down Expand Up @@ -48,7 +50,7 @@ def generate(self):
w = lines.append
for hpyfunc in self.api.hpyfunc_typedefs:
NAME = hpyfunc.base_name().upper()
if NAME in ['NOARGS', 'O', 'VARARGS', 'KEYWORDS']:
if NAME in SPECIAL_CASES:
continue
#
tramp_node = deepcopy(hpyfunc.node.type.type)
Expand Down Expand Up @@ -99,7 +101,7 @@ def generate(self):
for hpyfunc in self.api.hpyfunc_typedefs:
name = hpyfunc.base_name()
NAME = name.upper()
if NAME in ['NOARGS', 'O', 'VARARGS', 'KEYWORDS']:
if NAME in SPECIAL_CASES:
continue
#
c_ret_type = toC(hpyfunc.return_type())
Expand Down Expand Up @@ -137,7 +139,7 @@ def generate(self):
for hpyfunc in self.api.hpyfunc_typedefs:
name = hpyfunc.base_name()
NAME = name.upper()
if NAME in ['NOARGS', 'O', 'VARARGS', 'KEYWORDS']:
if NAME in SPECIAL_CASES:
continue
#
tramp_node = deepcopy(hpyfunc.node.type.type)
Expand Down
Loading