Debian Codegraph @

examples/buffer/modulegen.py

raw symbols SCIP
1 #! /usr/bin/env python
2
3 import sys
4
5 import pybindgen
6 from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
7 from pybindgen import CppMethod, CppConstructor, CppClass, Enum
8 from pybindgen.typehandlers.base import ForwardWrapperBase
9
10
11 class BufferReturn(ReturnValue):
12 CTYPES = []
13
14 def __init__(self, ctype, length_expression):
15 super(BufferReturn, self).__init__(ctype, is_const=False)
16 self.length_expression = length_expression
17
18 def convert_c_to_python(self, wrapper):
19 pybuf = wrapper.after_call.declare_variable("PyObject*", "pybuf")
20 wrapper.after_call.write_code("%s = PyBuffer_FromReadWriteMemory(retval, (%s)*sizeof(short int));" % (pybuf, self.length_expression))
21 wrapper.build_params.add_parameter("N", [pybuf], prepend=True)
22
23
24 def my_module_gen(out_file):
25
26 mod = Module('c')
27 mod.add_include('"c.h"')
28
29 mod.add_function("GetBuffer", BufferReturn("unsigned short int*", "GetBufferLen()"), [])
30 mod.add_function("GetBufferLen", ReturnValue.new("int"), [])
31 mod.add_function("GetBufferChecksum", ReturnValue.new("unsigned short"), [])
32
33 mod.generate(FileCodeSink(out_file))
34
35 if __name__ == '__main__':
36 my_module_gen(sys.stdout)