Skip to content

Instantly share code, notes, and snippets.

View steve-s's full-sized avatar

Stepan Sindelar steve-s

View GitHub Profile
#include <Python.h>
static int spike_exec(PyObject *module);
static PyObject *nb_add(PyObject *self, PyObject *args);
typedef struct { int ID; } mod_state_t;
static PyModuleDef_Slot spike_slots[] = {
{Py_mod_exec, spike_exec},
{0, NULL}
};
> raise HPyLeakError(leaks)
E hpy.debug.leakdetector.HPyLeakError: 10 unclosed handles:
E <DebugHandle 0x5606bb9f81b0 for 1 * foo>
E Allocation stacktrace:
E python3.8/site-packages/hpy/universal.cpython-38d-x86_64-linux-gnu.so(debug_ctx_New+0x60) [0x7f4af12793c1]
E kiwisolver.hpy.so(+0x67fc5) [0x7f4af0eaffc5]
E kiwisolver.hpy.so(_ZN10kiwisolver15new_from_globalEP13_HPyContext_s9HPyGlobalPv+0x55) [0x7f4af0eb236f]
E kiwisolver.hpy.so(_ZN10kiwisolver9BinaryMulclIPNS_8VariableEdEE6_HPy_sP13_HPyContext_sT_T0_S4_S4_+0x4e) [0x7f4af0ebbc4e]
E kiwisolver.hpy.so(_ZN10kiwisolver9BinaryAddclIPNS_8VariableEdEE6_HPy_sP13_HPyContext_sT_T0_S4_S4_+0x5e) [0x7ff3e41cdba4]
template<> inline
HPy BinaryAdd::operator()( HPyContext *ctx, Variable* first, double second, HPy h_first, HPy h_second )
{
HPy temp = BinaryMul()( ctx, first, 1.0, h_first, HPy_NULL );
if( HPy_IsNull(temp) )
return HPy_NULL;
HPy result = operator()( ctx, Term_AsStruct( ctx, temp ), second, temp, h_second );
HPy_Close(ctx, temp);
return result;
}
template<> inline
HPy BinaryAdd::operator()( HPyContext *ctx, Variable* first, double second, HPy h_first, HPy h_second )
{
HPy temp = BinaryMul()( ctx, first, 1.0, h_first, HPy_NULL );
if( HPy_IsNull(temp) )
return HPy_NULL;
return operator()( ctx, Term_AsStruct( ctx, temp ), second, temp, h_second );
}
static inline int HPy_IsTrue(HPyContext *ctx, HPy h)
{
return PyObject_IsTrue(_h2py(h));
}
// In CPython ABI mode, HPy handles == PyObject*
static inline PyObject* _h2py(HPy h) {
return (PyObject*) h._i;
}
HPyAPI_FUNC int HPy_IsTrue(HPyContext *ctx, HPy h) {
return ctx->ctx_IsTrue ( ctx, h );
}
def __bootstrap__():
from sys import modules
from hpy.universal import load
from pkg_resources import resource_filename
ext_filepath = resource_filename(__name__, 'kiwisolver.hpy.so')
m = load('kiwisolver', ext_filepath, debug=is_debug)
modules[__name__] = m
python setup.py --hpy-abi=universal build_ext
typedef struct {
// ...
HPy (*ctx_Absolute)(HPyContext *ctx, HPy h1);
int (*ctx_IsTrue)(HPyContext *ctx, HPy h);
HPy (*ctx_Type_FromSpec)(HPyContext *ctx, HPyType_Spec *spec, HPyType_SpecParam *params);
// ...
} HPyContext;
// ...
#include "Python.h"
static PyObject *myabs(PyObject *self, PyObject *arg) {
return Py_Absolute(arg);
}
static PyMethodDef methods[] = {
{"myabs", myabs, METH_O, ""},
{NULL, NULL, 0, NULL}
};