Interfacing a dynamic shared library gives me different results in 2.7 versus 3.5
I have a dynamic library doing some numerical computations. I used ctypes to interact it by passing numpy arrays back and forth. Python 3.5 gives me the correct results. Python 2.7 gives me different, erroneous results, but it never crashes. How is this possible? There is no string operations involved whatsoever. -- https://mail.python.org/mailman/listinfo/python-list
Re: Interfacing a dynamic shared library gives me different results in 2.7 versus 3.5
Thanks for all the replies. It turned out that the Apple OS X stock python 2.7 gives the wrong results, but other distributions like 2.7 from miniconda gives the correct results. Facepalm. -- https://mail.python.org/mailman/listinfo/python-list
Re: Interfacing a dynamic shared library gives me different results in 2.7 versus 3.5
Hello ChrisA, I don't quite understand, the binary shared library contains no python interfaces, it should be independent of python. As a matter of fact, I have successfully used it in Conda python 2.7, 3.5, Julialang as well as c++ executables. I think the fact that only stock python 2.7 failed to run correctly indicates some bug in that python distribution. > When you use a binary shared library, it has to be compiled against > the correct Python. You're messing around with ctypes, so basically > you've voided your warranty; *everything* you're doing is > platform-specific. Have fun. :) > > ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Interfacing a dynamic shared library gives me different results in 2.7 versus 3.5
The c function has a signature as follows: int cfun(int len_data, float* data, int* a, int num_a, int flag1, int flag2, int flag3, float* param, float* out1, float* out2, float* out3) and in python: import numpy as np import ctypes as ct data = np.atleast_2d(np.float32(data)) a = np.atleast_2d(np.int32(a)) if a else np.zeros((2, 1), dtype=np.int32) param = np.atleast_2d(np.float32(param)) num_a = activity.shape[1] len_data = data.shape[1] num_inc = len_data//256 cf = ct.POINTER(ct.c_float) hr = np.zeros((2, num_inc), dtype=np.float32) of = np.zeros((num_inc, 207), dtype=np.float32) gait = np.zeros((num_inc, 14), dtype=np.float32) pt_of = of.ctypes.data_as(cf) if do_of else None pt_gait = gait.ctypes.data_as(cf) if do_gait else None pt_param = param.ctypes.data_as(cf) if param else None dl.run_plt_hrm(len_data, data.ctypes.data_as(cf), activity.ctypes.data_as(ct.POINTER(ct.c_int)), num_act, do_long_fft+0, do_cls_mitigate+0, do_weighted_average+0, pt_param, hr.ctypes.data_as(cf), pt_of, pt_gait) -- https://mail.python.org/mailman/listinfo/python-list
Re: Interfacing a dynamic shared library gives me different results in 2.7 versus 3.5
Here is a summary of what I did with numpy and the dll I have verified that the values entering the last dll call (dl.cfunction) are identical across platforms. The c function has a signature as follows: int cfunction(int len_data, float* data, int* ac, int num_ac, int flag1, int flag2, int flag3, float* param, float* out) and in python: import numpy as np import ctypes as ct dl = ct.cdll.LoadLibrary('xxx.dylib') # data, ac, param are loaded from somewhere else. data = np.atleast_2d(np.float32(data)) ac = np.atleast_2d(np.int32(a)) if a else np.zeros((2, 1), dtype=np.int32) param = np.atleast_2d(np.float32(param)) flag1 = True flag2 = True flag3 = True num_ac = ac.shape[1] len_data = data.shape[1] num_inc = len_data//200 out = np.zeros((2, num_inc), dtype=np.float32) pt_param = param.ctypes.data_as(cf) if param else None cf = ct.POINTER(ct.c_float) dl.cfunction(len_data, data.ctypes.data_as(cf), ac.ctypes.data_as(ct.POINTER(ct.c_int)), num_ac, flag1+0, flag2+0, flag3+0, pt_param, out.ctypes.data_as(cf)) -- https://mail.python.org/mailman/listinfo/python-list