Skip to main content

Quick and painless wrapping C code into Python

Project description

PyPI version https://img.shields.io/badge/coverage-100%25-%2326543A

Quick and painless wrapping C code into Python.

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.

  • 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. Python extension modules are typically several times larger and a bare-bones Cython-ised import numpy extension is several MBs.

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. Using such types will generally reduce performance down to near pure Python levels anyway so this is a small loss in practice.

  • You can’t use C++.

Shared Caveats

Before you commit yourself to any non Pure-Python you should bear in mind that:

  • You’ll need to ship wheels for every platform you wish to support. Otherwise, users of your code will have to install a C compiler to run it. This means that you either need access to all platforms, or you will have to setup Continuous Integration to build you package on a cloud server. Linux users can get around this by using Vagrant.

  • Linux wheels must be built on a manylinux Docker image in order to be widely compatible with most distributions of Linux.

  • Recent macOS versions will typically block or delete any binary file you produce unless you either purchase a codesign license or your software becomes famous enough to be whitelisted for you by Apple (binaries uploaded to PyPI seem to be except automatically). Windows users face a similar, albeit lesser, problem with Microsoft Defender.

Supported Compilers

The following OS/compiler combinations are fully supported and tested regularly.

Compiler

Linux

Windows

macOS

FreeBSD

Cygwin/msys2

Android*

gcc

clang

MSVC

TinyCC

* Using Termux.

Installation

cslug requires a C compiler to compile C code. Its favourite compiler is gcc. Linux distributions typically come with it preinstalled. If you are on another OS or just don’t have it then you should get it with mingw-w64. Windows users are recommended to download WinLibs without LLVM/Clang/LLD/LLDB (although cslug works with clang too) and add its mingw64/bin directory to PATH.

Check that you have it set up by running the following in a terminal:

gcc -v

By default, cslug will use gcc if it can find it. On macOS or FreeBSD it will switch to clang if gcc is unavailable. To use any other supported compiler, cslug respects the CC environment variable. Set it to the name or full path of your alternative compiler.

Install cslug itself with the usual:

pip install cslug

Whilst cslug is still in its 0.x versions, breaking changes may occur on minor version increments. Please don’t assume forward compatibility - pick a version you like and pin it in a requirements.txt. Inspect the changelog for anything that may break your code.

Quickstart

Check out our quickstart page on readthedocs to get started.

Credits

Hall of fame for contributions to cslug.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

cslug-0.5.2-py3-none-any.whl (36.9 kB view details)

Uploaded Python 3

File details

Details for the file cslug-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: cslug-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 36.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.1.dev1+gf543114 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for cslug-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 72740c219850b1916e6d7b894edc5bc24789b5ad596c60296abaae1810e11d31
MD5 b153eed7cc3b0b6d6a656059aee3f1a8
BLAKE2b-256 01f1fee48e322826b7da8edc3f541e54fc8497b6859875a593187b4abf9e1322

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page