spacepaste

  1.  
  2. #!/usr/bin/env python
  3. from awesome_new_c_binding_thingy import c_function, In, Out, Error, c_int, c_int_ptr
  4. # this decorated, annotated Python function has all the information necessesary
  5. # to create the below bindings code, either ahead of time or just in time, etc
  6. @c_function('example.h', 'libraryname')
  7. def foo(x: In[c_int], y: In[c_int], result: Out[c_int_ptr]) -> Error[c_int]:
  8. pass
  9. #
  10. # Magically generated code
  11. #
  12. from cffi import FFI
  13. ffi = FFI()
  14. ffi.cdef('''\
  15. int foo(int x, int y, int* result);
  16. ''')
  17. lib = ffi.verify(r'''\
  18. #include <example.h>
  19. ''',
  20. libraries=['libraryname'],
  21. )
  22. def foo(x: int, y: int) -> int:
  23. result = ffi.new('int*')
  24. error = lib.foo(x, y, result)
  25. if error:
  26. raise RuntimeError('foo returned %d' % error)
  27. return result[0]
  28.