From 735305c79fbe3e87567714f0edd5cbe54872c83c Mon Sep 17 00:00:00 2001 From: Xinyu Li Date: Fri, 5 Jan 2024 19:30:42 -0800 Subject: [PATCH] python-binding-fixed --- python/ionpy/Builder.py | 7 +++-- python/ionpy/Port.py | 67 +++++++++++++++++++++++++++++++++++++++- python/ionpy/native.py | 68 +++++++++++++++++++++++++++++++++++++++-- python/test/test_all.py | 2 -- 4 files changed, 137 insertions(+), 7 deletions(-) diff --git a/python/ionpy/Builder.py b/python/ionpy/Builder.py index 7d8229ce..7b7ccfaf 100644 --- a/python/ionpy/Builder.py +++ b/python/ionpy/Builder.py @@ -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') diff --git a/python/ionpy/Port.py b/python/ionpy/Port.py index e74150be..acc7d1c0 100644 --- a/python/ionpy/Port.py +++ b/python/ionpy/Port.py @@ -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, @@ -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') diff --git a/python/ionpy/native.py b/python/ionpy/native.py index 25f2c3e3..a9ac8179 100644 --- a/python/ionpy/native.py +++ b/python/ionpy/native.py @@ -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): @@ -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 @@ -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 diff --git a/python/test/test_all.py b/python/test/test_all.py index 1f8a3851..c2698907 100644 --- a/python/test/test_all.py +++ b/python/test/test_all.py @@ -88,5 +88,3 @@ def case2(): def test_all(): case1() case2() - -test_all() \ No newline at end of file