Skip to content

Commit

Permalink
Tiny example of use of cffi to call
Browse files Browse the repository at this point in the history
Not obviously better than ctypes it seems
  • Loading branch information
ianhbell committed Aug 29, 2024
1 parent 3d1a3a8 commit 352cf95
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions interface/C/test_teqpc_FFI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
A small script showing how to use the cffi library in Python to call the teqp
shared library
"""
from cffi import FFI
import json

ffi = FFI()
ffi.cdef(open('teqpc.h').read().replace('EXPORT_CODE','').replace('CONVENTION',''))

# This next line will need to be changed to the absolute path of the library that was compiled
C = ffi.dlopen('bld/Debug/libteqpc.dylib')

handle = ffi.new("long long*")
buffer = ffi.new("char[]", ("?"*300).encode('ascii'))

spec = ffi.new("char[]", json.dumps({'kind':'vdW1', 'model': {'a': 1, 'b': 2}}).encode('ascii'))
C.build_model(spec, handle, buffer, 300)

print(handle[0])
print(ffi.string(buffer).decode('ascii'))

0 comments on commit 352cf95

Please sign in to comment.