26943

Load shared object using ctyped.cdll.loadLibrary from dist-packages folder

<h3>Question</h3>

I created this setup.py to compile a libDIV.so shared library and install it.

<pre class="lang-py prettyprint-override">from distutils.core import setup, Extension libDIV = Extension( 'DIV', sources = ['example.c'] ) setup ( name = 'divv', version = '0.1', description = 'DIVV, you want it', ext_modules = [libDIV], packages = [''], package_data={'': ['libDIV.so']} )

After running python3 setup.py install, the shared library is placed in the dist-packages folder as:

<pre class="lang-py prettyprint-override">/usr/local/lib/python3.4/dist-packages/DIV.cpython-34m.so

In an extra file example.py I want to load the library using ctypes.cdll.LoadLibrary, such that example.py can wrap it's functionality in more elegant, type checked python functions.

<pre class="lang-py prettyprint-override">import ctypes lib = ctypes.cdll.LoadLibrary('DIV.so') # not sure what to do here def divide_modulo(a, b): div = ctypes.c_int(0) rest = ctypes.c_int(0) lib.divide_modulo(a, b, ctypes.byref(div), ctypes.byref(rest)) return (div.value, rest.value) print(divide_modulo(11, 4)) class DynamicArray(ctypes.Structure): _fields_ = [("n", ctypes.c_int), ("r", ctypes.POINTER(ctypes.c_int))]

However I'm not sure how to pass the right file name and location for the created shared object file.

How should I distribute / configure setup.py for a library of c-files which build into a shared library and some python code to wrap the c-functionality?

For completeness, this is example.c:

<pre class="lang-cpp prettyprint-override">#include <stdlib.h> void divide_modulo(int a, int b, int *div, int *rest) { *div = a / b; *rest = a % b; } typedef struct dynamic_array { int n; int *r; } dynamic_array; dynamic_array return_dynamic_array(int n) { dynamic_array r; r.n = n; r.r = (int *)malloc(sizeof(int) * n); unsigned int i = 0; for (; i < n; ++i) r.r[i] = i; return r; }

EDIT:

Currently I do this to find the file name:

<pre class="lang-py prettyprint-override">import ctypes, numpy, site, os.path from scipy.sparse import csr_matrix so_file = next( os.path.join(directory, filename) for directory in site.getsitepackages() for filename in os.listdir(directory) if filename.startswith("DIV.") and filename.endswith(".so") ) lib = ctypes.cdll.LoadLibrary(so_file)

But honestly, this is quite smelly. I'm not sure the file will always be called DIV.something.so and that vice versa that other files are not named like this.

来源:https://stackoverflow.com/questions/34391655/load-shared-object-using-ctyped-cdll-loadlibrary-from-dist-packages-folder

Recommend

  • How to animate to wrap_content?
  • PrismaticJoint limits not enforced
  • AWS CDK how to create an API Gateway backed by Lambda from OpenApi spec?
  • Convert time values to numeric while keeping time characteristics
  • Tokenizing non English Text in Python
  • integer to word conversion in java using map continue question
  • Program to find every list of X in Prolog
  • How to call an activity in another project?
  • Python: DLL load failed: %1 is not a valid Win32 application
  • Invalid Django TIME_ZONE
  • Mongoid doesn't update position field
  • unable to drag anymore into fullcalendar when filter is applied on external events
  • Why is Azure giving me random 500 errors?
  • /.git/hooks/: No such file or directory protocol error: expected control record on Mac osx
  • Is there a way to create more than one deployment with msbuild on a single build?
  • Input field doesn't receive keyboard events when rendering with value property?
  • Linq-to-SQL to search only DATE portions of a date
  • google fusion table- not able to color the layer in map after 5 colors
  • Existing data serialized as hash produces error when upgrading to Rails 5
  • Using Facebook Graph API with ASP.NET
  • android 3G signal strength
  • Winston logger not write to file
  • UIImage to UIColor array of pixel colors
  • Read specific column from Parquet without using Spark
  • Detect when MathJax has finished loading in UIWebView
  • Enable CORS on Tomcat 8.0.30
  • Vue.js 2: Vue cannot find files from /assets folder (v-for)
  • Azure NodeJS Error: ENOENT, open 'D:\\home\\site\\wwwroot\\bin\\views\\'
  • Sorting Custom Listview Items Using Spinner Android
  • Python Equivalent of Java's 'Keystore'?
  • Greedy Algorithm in “C”
  • Remove stopwords and tolower function slow on a Corpus in R
  • MayAVI install on Python 3.6 [duplicate]
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • How to use FirstOrDefault inside Include
  • How to check if object is null in Java?