Load a shared library (and access a 32-bit library from 64-bit Python)
Project description
This package loads a shared library in Python. It is basically just a thin wrapper around ctypes (for libraries that use the __cdecl or __stdcall calling convention), Python for .NET (for libraries that use Microsoft’s .NET Framework, CLR), Py4J (for Java .jar or .class files) and comtypes (for libraries that use the Component Object Model).
However, the primary advantage is that it is possible to communicate with a 32-bit library from 64-bit Python.
MSL-LoadLib is a pure-python package, but, Python for .NET depends on the .NET Common Language Runtime (CLR) on Windows and Mono Runtime on Linux/macOS, and Py4J depends on having a Java Virtual Machine installed.
Install
To install MSL-LoadLib run:
pip install msl-loadlib
Alternatively, using the MSL Package Manager run:
msl install loadlib
Optional dependencies:
To set up your environment on Linux, please follow the instructions on the prerequisites section of the documentation.
Examples
If you are loading a 64-bit library in 64-bit Python (or a 32-bit library in 32-bit Python), then you can directly load the library using LoadLibrary.
The following examples load a 64-bit library in a 64-bit Python interpreter. If you are using a 32-bit Python interpreter then replace the 64 with 32 in the filename.
Import the LoadLibrary class and the directory where the example libraries are located
>>> from msl.loadlib import LoadLibrary
>>> from msl.examples.loadlib import EXAMPLES_DIR
If the file extension is not included then a default extension, .dll (Windows), .so (Linux) or .dylib (macOS), is used.
Load a C++ library and call the add function
>>> cpp = LoadLibrary(EXAMPLES_DIR + '/cpp_lib64')
>>> cpp.lib.add(1, 2)
3
Load a FORTRAN library and call the factorial function
>>> fortran = LoadLibrary(EXAMPLES_DIR + '/fortran_lib64')
With a FORTRAN library you must pass values by reference using ctypes, and, since the returned value is not of type int we must configure ctypes for a value of type double to be returned
>>> from ctypes import byref, c_int, c_double
>>> fortran.lib.factorial.restype = c_double
>>> fortran.lib.factorial(byref(c_int(37)))
1.3763753091226343e+43
Load Java byte code and call the cos function
>>> java = LoadLibrary(EXAMPLES_DIR + '/Trig.class')
>>> java.lib.Trig.cos(1.234)
0.33046510807172985
Python interacts with the Java Virtual Machine via a local network socket and therefore the connection needs to be closed when you are done using the Java library
>>> java.gateway.shutdown()
Load a .NET library and call the reverse_string function, we must specify that the library type is a .NET library by passing in the 'net' argument
>>> net = LoadLibrary(EXAMPLES_DIR + '/dotnet_lib64.dll', 'net')
>>> net.lib.StringManipulation().reverse_string('abcdefghijklmnopqrstuvwxyz')
'zyxwvutsrqponmlkjihgfedcba'
To load a Component Object Model (COM) library pass in the library’s Program ID. NOTE: This example will only work on Windows.
Here we load the FileSystemObject library and include the 'com' argument to indicate that it is a COM library.
>>> com = LoadLibrary('Scripting.FileSystemObject', 'com')
We then use the library to create, edit and close a text file
>>> fp = com.lib.CreateTextFile('a_new_file.txt')
>>> fp.WriteLine('This is a test')
0
>>> fp.Close()
0
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file msl-loadlib-0.10.0.tar.gz
.
File metadata
- Download URL: msl-loadlib-0.10.0.tar.gz
- Upload date:
- Size: 1.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.29.0 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9da9fa8f9502f06559e1c69f2f29092ee6c0b365f5a573e9bd777a0cde39b0d7 |
|
MD5 | 6d3d66c831ccdf3ce91c554fe62c570c |
|
BLAKE2b-256 | e54e1db9ba6432fb13cd09368fc3ee1fd50645abd4d3af0e991b6e9f2c565ea1 |
Provenance
File details
Details for the file msl_loadlib-0.10.0-py2.py3-none-win_amd64.whl
.
File metadata
- Download URL: msl_loadlib-0.10.0-py2.py3-none-win_amd64.whl
- Upload date:
- Size: 12.5 MB
- Tags: Python 2, Python 3, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.29.0 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99d0d76c95b108626e1e5d45f65413142c5841303afa7dec7011d60e12c31761 |
|
MD5 | 56b126cbe34a1953d95ccaa3cb6fcfdb |
|
BLAKE2b-256 | 2f7fa09f427d3c349491ec4d24af0e0f36ad8b035480945f6cb59ec2dfefbbb9 |
Provenance
File details
Details for the file msl_loadlib-0.10.0-py2.py3-none-win32.whl
.
File metadata
- Download URL: msl_loadlib-0.10.0-py2.py3-none-win32.whl
- Upload date:
- Size: 12.5 MB
- Tags: Python 2, Python 3, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.29.0 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10c6820a7210b10cb7c2939d83b58ff2011b54d7bafbbb4c72bc96bb7d6cbb01 |
|
MD5 | ebabc69ac0f22977c3c4298d94b768be |
|
BLAKE2b-256 | 0cdda5dfe6e81abafa3ccefa397754680f462b09e5d43199abfac605c2ba2654 |
Provenance
File details
Details for the file msl_loadlib-0.10.0-py2.py3-none-manylinux1_x86_64.whl
.
File metadata
- Download URL: msl_loadlib-0.10.0-py2.py3-none-manylinux1_x86_64.whl
- Upload date:
- Size: 20.7 MB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/6.0.0 pkginfo/1.9.6 requests/2.29.0 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5d7d45b0a75cf4d125c157ac2bbd85304969bbbde3de1d8986acf1f10d5c6b3 |
|
MD5 | 90cf2cc8a35b77ebd5e0dd314bf4e8b1 |
|
BLAKE2b-256 | 44905968cc9687e7c862504e1e1b3a8bd62fee63d2eca692de700bf1d0a6b720 |
Provenance
File details
Details for the file msl_loadlib-0.10.0-py2.py3-none-manylinux1_i686.whl
.
File metadata
- Download URL: msl_loadlib-0.10.0-py2.py3-none-manylinux1_i686.whl
- Upload date:
- Size: 20.7 MB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.29.0 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21c7576866647269c3192cbc16f7de106a5516994b5190145b654200977a7d33 |
|
MD5 | 63b748af3c312c71480dccf0473af33e |
|
BLAKE2b-256 | b0ed00ef007a22d81a413554eee9d41dee1282d81f8a4ea34d3341d1fe995cd3 |