Skip to main content

Load a shared library (and access a 32-bit library from 64-bit Python)

Project description

Documentation Status

Purpose

Load a shared library into Python.

This package is basically just a thin wrapper around ctypes and Python for .NET for loading a shared library into Python. However, the primary advantage is that it is possible to communicate with a 32-bit shared library from 64-bit Python.

Tested in Python 2.7, 3.3 - 3.6. The examples provided are currently only supported in Windows and Linux, however MSL-LoadLib should work properly with any OS (not tested) that Python for .NET supports (and if you do not need to load .NET libraries then MSL-LoadLib is a pure-python package so it is OS independent).

Example

If you are loading a 64-bit library into 64-bit Python, or a 32-bit library into 32-bit Python, then you can directly load the library using msl.loadlib.LoadLibrary.

Using a 64-bit Python interpreter, load the 64-bit C++ library named cpp_lib64. By default, msl.loadlib.LoadLibrary loads a library using ctypes.CDLL.

>>> from msl.loadlib import LoadLibrary
>>> cpp = LoadLibrary('./cpp_lib64')
>>> cpp
LoadLibrary object at 0x3e9f750; libtype=CDLL; path=D:/cpp_lib64.dll
>>> cpp.lib
<CDLL 'D:\cpp_lib64.dll', handle af1e0000 at 0x3e92f90>

Call the cpp_lib64.add function that calculates the sum of two integers

>>> cpp.lib.add(1, 2)
3

Inter-process communication is used to access a 32-bit shared library from a module that is running within a 64-bit Python interpreter. The procedure uses a client-server protocol where the client is a subclass of msl.loadlib.Client64 and the server is a subclass of msl.loadlib.Server32. See the tutorials for examples on how to implement inter-process communication.

Documentation

The documentation for MSL-LoadLib can be found here.

Install

To install MSL-LoadLib run:

pip install msl-loadlib

Alternatively, using the MSL Package Manager run:

msl install loadlib

To install the dependencies on Linux, please follow the instructions on the prerequisites section of the documentation.

Developers Guide

MSL-LoadLib uses pytest for testing the source code and sphinx for creating the documentation.

Run the tests (a coverage report is generated in the htmlcov/index.html file):

python setup.py test

Build the documentation, which can be viewed by opening the docs/_build/html/index.html file:

python setup.py docs

Automatically create the API documentation from the docstrings in the source code (uses sphinx-apidoc):

python setup.py apidoc

NOTE: By default, the docs/_autosummary folder that is created by running the apidoc command is automatically generated (it will overwrite existing files). As such, it is excluded from the repository (i.e., this folder is specified in the .gitignore file). If you want to keep the files located in docs/_autosummary you can rename the folder to be, for example, docs/_api and then the changes made to the files in the docs/_api folder will be kept and will be included in the repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msl-loadlib-0.3.2.tar.gz (30.2 kB view hashes)

Uploaded source

Built Distributions

msl_loadlib-0.3.2-py2.py3-none-win_amd64.whl (8.5 MB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page