Quick and painless wrapping C code into Python
Project description
Quick and painless wrapping C code into Python.
Free software: MIT license
Documentation: https://cslug.readthedocs.io.
The cslug package provides a thin layer on top of the built-in ctypes library, making it easier to load functions and structures from C into Python.
Alternatives
Mixing C with Python is nothing new - there are plenty of other ways. A nice comparison of the various methods can be found here. cslug aims to be the simplest although it certainly isn’t the most flexible.
Using ctypes driven wrapping has both advantages and disadvantages over Python extension modules and tools that write them (such as Cython).
Advantages
C code can be just plain high school level C. Even a hello world Python extension module is some 40 lines of incomprehensible macros. This does not apply to Cython.
Binaries are not linked against Python and are therefore not tied to a specific Python version. A Python extension module needs to be recompiled for every minor version of Python (3.6, 3.7, 3.8, 3.9) and for every platform (Windows, macOS, Linux) whereas a cslug binary need only be compiled for every platform.
You can use virtually any C compiler. Python extension modules must be built with clang on macOS and MSVC on Windows.
File sizes of binaries are very small. 1000 lines of C code equates to about 20KB of binary on Linux.
Disadvantages
The surrounding Python code is less automated. A Python extension module looks and feels like a native Python module out the box whereas a small wrapper function is generally required for ctypes.
You can’t use native Python types such as list or dict within C code. However, using such types will generally reduce performance down to near pure Python levels anyway.
You can’t use C++.
Supported Compilers
Compiler |
Linux |
Windows |
macOS |
FreeBSD |
Cygwin/msys2 |
---|---|---|---|---|---|
✓ |
✓ |
✓ |
✓ |
✓ |
|
✓ |
✗ |
✓ |
✓ |
✗ |
|
MSVC |
✗ |
✗ |
✗ |
✗ |
✗ |
✓ |
✓ |
✗ |
✗ |
✗ |
Installation
cslug requires a C compiler to compile C code. Its favourite compiler is gcc. If you’re on Linux you probably already have it. If you are on another OS then you should get it with mingw. To check you have it run the following in terminal:
gcc -v
To use any other supported compiler, cslug respects the CC environment variable. Set it to the name or full path of your alternative compiler.
To install cslug itself use the usual:
pip install cslug
This package is very much in its beta-stages and its API will likely move around. Please don’t assume forward compatibility - pick a version you like and pin it in a requirements.txt.
Quickstart
Check out our quickstart page on readthedocs to get started.
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
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 Distributions
Built Distribution
File details
Details for the file cslug-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: cslug-0.1.1-py3-none-any.whl
- Upload date:
- Size: 33.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb5c7e9cf46c279a0c6d50d746889dd45fa84961c27ff6381e3917faa03388f3 |
|
MD5 | 8f956721a07bb78a5efb460503ebc706 |
|
BLAKE2b-256 | 3cebbdcae93c3c08a28d7f19775691761eb0eff5dfe4185c100145bf247f7e5d |