if platform.architecture()[1] == 'ELF': from ctypes import PYFUNCTYPE, cdll _mydll = cdll.LoadLibrary("libcncapi.so") else: from ctypes import WINFUNCTYPE, windll if platform.architecture()[0] == '64bit': _mydll = windll.LoadLibrary("cncapi64.dll") else: _mydll = windll.LoadLibrary("cncapi.dll") try: CNC_KEEP_UI_ALIVE_FUNCTION = WINFUNCTYPE(c_void_p) except: CNC_KEEP_UI_ALIVE_FUNCTION = PYFUNCTYPE(c_void_p) _mydll.CncGetAPIVersion.argtypes = [ c_char_p ] _mydll.CncGetAPIVersion.restype = None _mydll.CncGetServerVersion.argtypes = [ c_char_p ] _mydll.CncGetServerVersion.restype = c_int _mydll.CncConnectServer.argtypes = [ c_char_p ] _mydll.CncConnectServer.restype = c_int _mydll.CncDisConnectServer.argtypes = [ ] _mydll.CncDisConnectServer.restype = c_int _mydll.CncGetSetupPassword.argtypes = [ ] _mydll.CncGetSetupPassword.restype = c_char_p
def _encode_capsule_key_file(licfile): prototype = PYFUNCTYPE(py_object, c_char_p, c_char_p) dlfunc = prototype(('encode_capsule_key_file', _pytransform)) return dlfunc(licfile.encode(), None)
def generate_module_key(pubname, key): t_key = c_char * 32 prototype = PYFUNCTYPE(py_object, c_char_p, t_key, c_char_p) dlfunc = prototype(('generate_module_key', _pytransform)) return dlfunc(pubname.encode(), t_key(*key), None)
def clean_obj(obj, kind): prototype = PYFUNCTYPE(c_int, py_object, c_int) dlfunc = prototype(('clean_obj', _pytransform)) return dlfunc(obj, kind)
def _generate_pytransform_key(licfile, pubkey): prototype = PYFUNCTYPE(py_object, c_char_p, py_object) dlfunc = prototype(('generate_pytransform_key', _pytransform)) return dlfunc(licfile.encode() if sys.version_info[0] == 3 else licfile, pubkey)
def generate_license_file(filename, priname, rcode, start=-1, count=1): prototype = PYFUNCTYPE(c_int, c_char_p, c_char_p, c_char_p, c_int, c_int) dlfunc = prototype(('generate_project_license_files', _pytransform)) return dlfunc(filename.encode(), priname.encode(), rcode.encode(), start, count) if sys.version_info[0] == 3 \ else dlfunc(filename, priname, rcode, start, count)
def get_registration_code(): prototype = PYFUNCTYPE(py_object) dlfunc = prototype(('get_registration_code', _pytransform)) return dlfunc()
def generate_license_file(filename, priname, rcode, start=-1, count=1): prototype = PYFUNCTYPE(c_int, c_char_p, c_char_p, c_char_p, c_int, c_int) dlfunc = prototype(('generate_project_license_files', _pytransform)) return dlfunc(filename, priname, rcode, start, count)
def init_runtime(systrace=0, sysprofile=1, threadtrace=0, threadprofile=1): pyarmor_init(is_runtime=1) prototype = PYFUNCTYPE(c_int, c_int, c_int, c_int, c_int) _init_runtime = prototype(('init_runtime', _pytransform)) _init_runtime(systrace, sysprofile, threadtrace, threadprofile)
def load_sfac(): # Boilerplate declaration code: def decl_fn(a, *args): a.argtypes = args[:-1] a.restype = args[-1] def void_fn(a, *args): decl_fn(a, *(args + (None, ))) def int_fn(a, *args): decl_fn(a, *(args + (c_int, ))) def dbl_fn(a, *args): decl_fn(a, *(args + (c_double, ))) def arr_t(*shape): return reduce(lambda x, y: x * y, reversed(shape), c_double) nparr = np.ctypeslib.ndpointer(dtype=np.float64, flags='C_CONTIGUOUS') dblarr = POINTER(c_double) vecarr = POINTER(c_double) vec = arr_t(3) def type_list(type, *names): return [(a, type) for a in names] def mkstruct(*args): def concat(y): return reduce(lambda x, y: x + y, y, []) def type_em(args): return type_list(*args) return concat(map(type_em, args)) def nparr_t(*shape): return np.ctypeslib.ndpointer(dtype=np.float64, shape=shape, flags='C_CONTIGUOUS') def np_intarr_t(*shape): return np.ctypeslib.ndpointer(dtype=np.int32, shape=shape, flags='C_CONTIGUOUS') class sfac_t(Structure): _fields_ = mkstruct( (c_int, "order"), (c_int * 3, "K"), (c_int, "ldim"), (arr_t(3, 3), "L", "iL"), (c_double, "iV", "iK"), (dblarr, "bspc"), (dblarr * 3, "iB"), (c_void_p, "fft_neg", "fft_pos"), (dblarr, "A", "dA", "Q")) sfac_p = POINTER(sfac_t) rad_fn = PYFUNCTYPE(c_double, c_double, POINTER(c_double), c_void_p) # load library cwd = os.path.dirname(os.path.abspath(__file__)) sfac = CDLL(os.path.join(cwd, "libsfac.so")) int_fn(sfac.sfac_ctor, sfac_p, nparr_t(6), np_intarr_t(3), c_int) int_fn(sfac.sfac_dtor, sfac_p) int_fn(sfac.set_A, sfac_p, rad_fn, c_void_p) void_fn(sfac.set_L, sfac_p, nparr_t(6)) void_fn(sfac.sfac, sfac_p, c_int, nparr, nparr) dbl_fn(sfac.en, sfac_p) dbl_fn(sfac.de1, sfac_p, nparr_t(6)) void_fn(sfac.de2, sfac_p, c_int, nparr, nparr, nparr) return sfac_t, rad_fn, sfac
def get_license_code(): prototype = PYFUNCTYPE(py_object) dlfunc = prototype(('get_registration_code', _pytransform)) licinfo = dlfunc() return licinfo['CODE']
def get_license_code(): prototype = PYFUNCTYPE(py_object) dlfunc = prototype(('get_registration_code', _pytransform)) rcode = dlfunc().decode() index = rcode.find('*CODE:') return rcode[index+6:]
self._checktype('get battery level', enums.EventType.bettery_level) return lib.event_get_battery_level(self) @property def emg(self): """ emg -> tuple of 8 ints Returns the EMG data on an *emg* event. """ self._checktype('get emg', enums.EventType.emg) return tuple(lib.event_get_emg(self, i) for i in range(8)) # Callback function type for libmyo_run(). hub_t.run() expects # a slightly different interface. HandlerCallback = PYFUNCTYPE(c_int, py_object, Event) # Backwards compatibility error_details_t = ErrorDetails hub_t = Hub myo_t = Myo event_t = Event handler_t = HandlerCallback # The globally loaded MyoLibrary. Todo: It would be nice if it was not # global but could just be created on a local level and all contents be # accessed through it. lib = MyoLibrary()
_elpl = cdll.LoadLibrary('libelperiodic.so') _elpl.prdic_init.argtypes = [c_double, c_double] _elpl.prdic_init.restype = c_void_p _elpl.prdic_procrastinate.argtypes = [ c_void_p, ] _elpl.prdic_free.argtypes = [ c_void_p, ] _elpl.prdic_addband.argtypes = [c_void_p, c_double] _elpl.prdic_addband.restype = c_int _elpl.prdic_useband.argtypes = [c_void_p, c_int] _elpl.prdic_set_epoch.argtypes = [c_void_p, POINTER(timespec)] _elpl_cbtype = PYFUNCTYPE(None, py_object) _elpl.prdic_call_from_thread.argtypes = [c_void_p, _elpl_cbtype, py_object] _elpl.prdic_call_from_thread.restype = c_int _elpl.prdic_CFT_enable.argtypes = [c_void_p, c_int] _elpl.prdic_CFT_enable.restype = c_int class _elpl_cb(object): def __init__(self, handler, args): self.handler = handler self.args = args # def __del__(self): # print('_ptrcall.__del__(%s)' % (self,))
def init_runtime(): prototype = PYFUNCTYPE(c_int, c_int, c_int, c_int, c_int) _init_runtime = prototype(('init_runtime', _pytransform)) return _init_runtime(0, 0, 0, 0)
def import_module(modname, filename): prototype = PYFUNCTYPE(py_object, c_char_p, c_char_p) _import_module = prototype(('import_module', _pytransform)) return _import_module(modname, filename)
def encrypt_code_object(pubkey, co, flags, suffix=''): _pytransform.set_option(6, suffix.encode()) prototype = PYFUNCTYPE(py_object, py_object, py_object, c_int) dlfunc = prototype(('encrypt_code_object', _pytransform)) return dlfunc(pubkey, co, flags)
def exec_file(filename): prototype = PYFUNCTYPE(c_int, c_char_p) _exec_file = prototype(('exec_file', _pytransform)) return _exec_file(filename)
def generate_license_key(prikey, keysize, rcode): prototype = PYFUNCTYPE(py_object, c_char_p, c_int, c_char_p) dlfunc = prototype(('generate_license_key', _pytransform)) return dlfunc(prikey, keysize, rcode) if sys.version_info[0] == 2 \ else dlfunc(prikey, keysize, rcode.encode())
def encrypt_code_object(pubkey, co, flags): prototype = PYFUNCTYPE(py_object, py_object, py_object, c_int) dlfunc = prototype(('encrypt_code_object', _pytransform)) return dlfunc(pubkey, co, flags)
def get_expired_days(): prototype = PYFUNCTYPE(py_object) dlfunc = prototype(('get_expired_days', _pytransform)) return dlfunc()
def _generate_pytransform_key(licfile, pubkey): prototype = PYFUNCTYPE(py_object, c_char_p, py_object) dlfunc = prototype(('generate_pytransform_key', _pytransform)) return dlfunc(licfile.encode(), pubkey)
def _generate_project_capsule(): prototype = PYFUNCTYPE(py_object) dlfunc = prototype(('generate_project_capsule', _pytransform)) return dlfunc()
def import_module(modname, filename): '''Only for old version, before PyArmor 3''' prototype = PYFUNCTYPE(py_object, c_char_p, c_char_p) _import_module = prototype(('import_module', _pytransform)) return _import_module(modname.encode(), filename.encode())
def encrypt_project_files(proname, filelist, mode=0): prototype = PYFUNCTYPE(c_int, c_char_p, py_object, c_int) dlfunc = prototype(('encrypt_project_files', _pytransform)) return dlfunc(proname.encode(), filelist, mode)
def exec_file(filename): '''Only for old version, before PyArmor 3''' prototype = PYFUNCTYPE(c_int, c_char_p) _exec_file = prototype(('exec_file', _pytransform)) return _exec_file(filename.encode())
def encrypt_files(key, filelist, mode=0): t_key = c_char * 32 prototype = PYFUNCTYPE(c_int, t_key, py_object, c_int) dlfunc = prototype(('encrypt_files', _pytransform)) return dlfunc(t_key(*key), filelist, mode)
def version_info(): prototype = PYFUNCTYPE(py_object) dlfunc = prototype(('version_info', _pytransform)) return dlfunc()
def old_init_runtime(systrace=0, sysprofile=1, threadtrace=0, threadprofile=1): '''Only for old version, before PyArmor 3''' pyarmor_init(is_runtime=1) prototype = PYFUNCTYPE(c_int, c_int, c_int, c_int, c_int) _init_runtime = prototype(('init_runtime', _pytransform)) return _init_runtime(systrace, sysprofile, threadtrace, threadprofile)
# py - Python # py_raw - Raw Python points (py_object, c_void_p) # mx - MATLAB / mxArray* # mx_raw - void* representing mxArray* (mx_array_t, c_void_p) # mex - MEX function # Use PYFUNCTYPE so we maintain GIL, so that MATLAB doesn't huff, and puff, # and crash with a segfault. # py_raw_t (mx_raw_t mx_raw_handle, int nargout, py_raw_t py_raw_in) mx_raw_t = c_uint64 # TODO: For py_raw_t, consider using py_object in lieu of c_uint64. # (No need for .util.Erasure, then.) py_raw_t = c_uint64 c_mx_feval_py_raw_t = PYFUNCTYPE(py_raw_t, mx_raw_t, c_int, py_raw_t) c_mx_raw_ref_t = PYFUNCTYPE(c_int, mx_raw_t) # Simple example. c_simple_t = PYFUNCTYPE(c_int) # Globals mx_funcs = {} funcs = {} def free(): global funcs global mx_funcs funcs = {} mx_funcs = {}