File: modulegen.py

package info (click to toggle)
pybindgen 0.20.0%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,932 kB
  • sloc: python: 15,981; cpp: 1,889; ansic: 617; makefile: 86; sh: 4
file content (36 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /usr/bin/env python

import sys

import pybindgen
from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
from pybindgen import CppMethod, CppConstructor, CppClass, Enum
from pybindgen.typehandlers.base import ForwardWrapperBase


class BufferReturn(ReturnValue):
    CTYPES = []

    def __init__(self, ctype, length_expression):
        super(BufferReturn, self).__init__(ctype, is_const=False)
        self.length_expression = length_expression

    def convert_c_to_python(self, wrapper):
        pybuf = wrapper.after_call.declare_variable("PyObject*", "pybuf")
        wrapper.after_call.write_code("%s = PyBuffer_FromReadWriteMemory(retval, (%s)*sizeof(short int));" % (pybuf, self.length_expression))
        wrapper.build_params.add_parameter("N", [pybuf], prepend=True)


def my_module_gen(out_file):

    mod = Module('c')
    mod.add_include('"c.h"')

    mod.add_function("GetBuffer", BufferReturn("unsigned short int*", "GetBufferLen()"), [])
    mod.add_function("GetBufferLen", ReturnValue.new("int"), [])
    mod.add_function("GetBufferChecksum", ReturnValue.new("unsigned short"), [])

    mod.generate(FileCodeSink(out_file))

if __name__ == '__main__':
    my_module_gen(sys.stdout)