Skip to content

Commit

Permalink
python-binding-fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinyu Li authored and Xinyu Li committed Jan 6, 2024
1 parent 0ec0e62 commit 735305c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 7 deletions.
7 changes: 5 additions & 2 deletions python/ionpy/Builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def load(self, filename: str):
if ret != 0:
raise Exception('Invalid operation')

def run(self, port_map: PortMap):
ret = ion_builder_run_with_port_map(self.obj, port_map.obj)
def run(self, port_map: PortMap = None):
if port_map is None:
ret = ion_builder_run(self.obj)
else:
ret = ion_builder_run_with_port_map(self.obj, port_map.obj)
if ret != 0:
raise Exception('Invalid operation')
67 changes: 66 additions & 1 deletion python/ionpy/Port.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@
ion_port_create,
ion_port_create_with_index,
ion_port_destroy,

ion_port_bind_i8,
ion_port_bind_i16,
ion_port_bind_i32,
ion_port_bind_i64,

ion_port_bind_u1,
ion_port_bind_u8,
ion_port_bind_u16,
ion_port_bind_u32,
ion_port_bind_u64,

ion_port_bind_f32,
ion_port_bind_f64,
ion_port_bind_buffer
)

from .Type import Type

from .Buffer import Buffer

class Port:
def __init__(self,
Expand Down Expand Up @@ -39,3 +54,53 @@ def __getitem__(self, index):
def __del__(self):
if self.obj: # check not nullptr
ion_port_destroy(self.obj)


# should use numpy type?
def bind_i8(self, v: int):
if ion_port_bind_i8(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_i16(self, v: int):
if ion_port_bind_i16(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_i32(self, v: int):
if ion_port_bind_i32(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_i64(self, v: int):
if ion_port_bind_i64(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_u1(self, v: bool):
if ion_port_bind_u1(self.obj, ctypes.byref(v))!= 0:
raise Exception('Invalid operation')

def bind_u8(self, v: int):
if ion_port_bind_u8(self.obj, ctypes.byref(v))!= 0:
raise Exception('Invalid operation')

def bind_u16(self, v: int):
if ion_port_bind_u16(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_u32(self, v: int):
if ion_port_bind_u32(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_u64(self, v: int):
if ion_port_bind_u64(self.obj, ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_f32(self, v: float):
if ion_port_bind_f32(self.obj,ctypes.byref(v)) != 0:
raise Exception('Invalid operation')

def bind_f64(self, v: float):
if ion_port_bind_f64(self.obj, ctypes.byref(v))!= 0:
raise Exception('Invalid operation')

def bind_buffer(self, buffer: Buffer):
if ion_port_bind_buffer(self.obj, buffer.obj) != 0:
raise Exception('Invalid operation')
68 changes: 66 additions & 2 deletions python/ionpy/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
from ctypes.util import find_library

import os
import platform

if os.name == 'nt':
if platform.system() == 'Windows':
ion_core_module = find_library('ion-core.dll')
elif os.name == 'posix':
elif platform.system() == 'Darwin':
ion_core_module = 'libion-core.dylib'
elif platform.system() == 'Linux':
ion_core_module = 'libion-core.so'

# libion-core.so must be in a directory listed in $LD_LIBRARY_PATH.
# ion-core.dll must be in a directory listed in %PATH%.
# libion-core.dylib must be in a directory listed in $DYLD_LIBRARY_PATH.
ion_core = ctypes.cdll.LoadLibrary(ion_core_module)

class c_ion_type_t(ctypes.Structure):
Expand All @@ -33,6 +37,7 @@ class c_builder_compile_option_t(ctypes.Structure):
c_ion_buffer_t = ctypes.POINTER(ctypes.c_int)
c_ion_port_map_t = ctypes.POINTER(ctypes.c_int)

c_ion_port_bind_t = ctypes.POINTER(ctypes.c_int)

# int ion_port_create(ion_port_t *, const char *, ion_type_t, int);
ion_port_create = ion_core.ion_port_create
Expand All @@ -49,6 +54,65 @@ class c_builder_compile_option_t(ctypes.Structure):
ion_port_destroy.restype = ctypes.c_int
ion_port_destroy.argtypes = [ c_ion_port_t ]

# int ion_port_bind_i8(ion_port_t, int8_t);
ion_port_bind_i8 = ion_core.ion_port_bind_i8
ion_port_bind_i8.restype = ctypes.c_int
ion_port_bind_i8.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int8) ]

# int ion_port_bind_i16(ion_port_t, int16_t);
ion_port_bind_i16 = ion_core.ion_port_bind_i16
ion_port_bind_i16.restype = ctypes.c_int
ion_port_bind_i16.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int16) ]

# int ion_port_bind_i32(ion_port_t, int32_t);
ion_port_bind_i32 = ion_core.ion_port_bind_i32
ion_port_bind_i32.restype = ctypes.c_int
ion_port_bind_i32.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int32)]

# int ion_port_bind_i64(ion_port_t, int64_t);
ion_port_bind_i64 = ion_core.ion_port_bind_i64
ion_port_bind_i64.restype = ctypes.c_int
ion_port_bind_i64.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_int64) ]

# int ion_port_map_set_u1(ion_port_t, bool);
ion_port_bind_u1 = ion_core.ion_port_bind_u1
ion_port_bind_u1.restype = ctypes.c_int
ion_port_bind_u1.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_bool) ]

# int ion_port_bind_u8(ion_port_t, uint8_t);
ion_port_bind_u8 = ion_core.ion_port_bind_u8
ion_port_bind_u8.restype = ctypes.c_int
ion_port_bind_u8.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint8) ]

# int ion_port_bind_u16(ion_port_t, uint16_t);
ion_port_bind_u16 = ion_core.ion_port_bind_u16
ion_port_bind_u16.restype = ctypes.c_int
ion_port_bind_u16.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint16) ]

# int ion_port_bind_u32(ion_port_t, uint32_t);
ion_port_bind_u32 = ion_core.ion_port_bind_u32
ion_port_bind_u32.restype = ctypes.c_int
ion_port_bind_u32.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint32) ]

# int ion_port_bind_u64(ion_port_t, uint64_t);
ion_port_bind_u64 = ion_core.ion_port_bind_u64
ion_port_bind_u64.restype = ctypes.c_int
ion_port_bind_u64.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint64) ]

# int ion_port_bind_f32(ion_port_t, float);
ion_port_bind_f32 = ion_core.ion_port_bind_f32
ion_port_bind_f32.restype = ctypes.c_int
ion_port_bind_f32.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_float) ]

# int ion_port_bind_f64(ion_port_map_t, ion_port_t, double);
ion_port_bind_f64 = ion_core.ion_port_bind_f64
ion_port_bind_f64.restype = ctypes.c_int
ion_port_bind_f64.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_double) ]

# int ion_port_bind_buffer(ion_port_t, ion_buffer_t);
ion_port_bind_buffer = ion_core.ion_port_bind_buffer
ion_port_bind_buffer.restype = ctypes.c_int
ion_port_bind_buffer.argtypes = [c_ion_port_t, c_ion_buffer_t ]

# int ion_param_create(ion_param_t *, const char *, const char *);
ion_param_create = ion_core.ion_param_create
Expand Down
2 changes: 0 additions & 2 deletions python/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,3 @@ def case2():
def test_all():
case1()
case2()

test_all()

0 comments on commit 735305c

Please sign in to comment.