def _cast_ptr2(x, _type): if isinstance(x, ffi.CData): if (_type.item == ffi.typeof(x) or (_type.item.cname == 'void' and ffi.typeof(x).kind in ['struct', 'union'])): return ffi.addressof(x), x return x, x if isinstance(x, _collections.Iterable): if _type.item.kind == 'pointer': ptrs = [_cast_ptr(i, _type.item) for i in x] ret = ffi.new(_type.item.cname+'[]', [i for i, _ in ptrs]) _weakkey_dict[ret] = tuple(i for _, i in ptrs if i != ffi.NULL) else: ret = ffi.new(_type.item.cname+'[]', x) return ret, ret return ffi.cast(_type, x), x
def _cstr(x): if not isinstance(x, ffi.CData): return x t = ffi.typeof(x) if 'item' not in dir(t) or t.item.cname != 'char': return x if PY3: return ffi.string(x).decode('ascii') else: return ffi.string(x)
{% for constructor in model.constructors %} def {{constructor.name}}({{constructor_params(constructor)}}): return _new('{{constructor.name}}', {{constructor_params_call(constructor)}}) {% endfor %} def _callApi(fn, *args): def _(x, _type): if x is None: return ffi.NULL if _type.kind == 'pointer': ptr, _ = _cast_ptr(x, _type) return ptr return x fn_args = [_(i, j) for i, j in zip(args, ffi.typeof(fn).args)] return fn(*fn_args) {# Macro for function parameters #} {%- macro params_def(f) -%} {% if f.count %} {% set members = f.members[:-2] %} {% elif f.allocate %} {% set members = f.members[:-1] %} {% else %} {% set members = f.members %} {% endif %} {%- for m in members -%} {{m.name}}
def _new(ctype, **kwargs): _type = ffi.typeof(ctype)
def _callApi(fn, *args): fn_args = [_auto_handle(i, j) for i, j in zip(args, ffi.typeof(fn).args)]