Skip to main content

Clean interface into compiled code

Project description

pythy

A short and sweet Python interface to compiled code

Overview

pythy dynamically generates a Python interface for calling functions in a shared library using the interface exposed by a C header file.

pythy's coolest feature is its ability to run compiled code under the Valgrind debugger. This allows you to test compiled code for memory errors directly from Python, for example while running automated testing.

Installation

pip install pythy

Minimal example

Create add.c with the following contents:

#include "add.h"

int add(int a, int b) {
    return a + b;
}

and add.h with

int add(int a, int b);

The compile add.c into a Linux shared object with the command gcc -fPIC -shared funcs.c -o funcs.so.

Then run the following Python code, which creates a module module and populates it with a wrapper for the add function from your C code, complete with the correct type information:

import pythy

module = pythy.create_interface("add.h", "add.so")
print(module.add(1, 2))

That's it! You've run your compiled C code directly from Python.

pythy also has the ability to generate docstrings from your header file and even automatically create classes.

Minimal example with Valgrind

TODO: Add a minimal example demonstrating usage with Valgrind.

Memory spaces

TODO: Move this section to a C implementation guide.

Strings (i.e., all variables with C type char*) are passed between Python and the compiled code by passing the literal bytes in the string (without the null terminator). All other data types are passed as a numerical value.

To take care of allocated memory, when compiled code returns a char*

  1. The Python code is not responsible for any direct cleanup, except for calling dlclose on the SO.

  2. The returned pointer is guaranteed to remain valid until another function in the same SO is called.

This is most easily implemented by the SO maintaining a global char* variable that tracks the last char* returned from a function. Whenever another char* must be allocated and returned, the global is realloced and returned.

This doesn't scale well to multi-threaded programs, but for now it seems like the easiest way to do things.

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

pythy-0.0.1.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pythy-0.0.1-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file pythy-0.0.1.tar.gz.

File metadata

  • Download URL: pythy-0.0.1.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pythy-0.0.1.tar.gz
Algorithm Hash digest
SHA256 cf9723664ca366188c4704eee913381967ab779eb3510a48abc1e8db1f56b42a
MD5 24ad9da79307e149e2c3d9bbce3f58cd
BLAKE2b-256 5765e7dddaded5820588e4e6735248418239174653c4db31dad5a6f7849c2deb

See more details on using hashes here.

File details

Details for the file pythy-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pythy-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pythy-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a95de6f66eed8e05ee24783c49c98db9a99ef6bc977ad58b2330088d716d964
MD5 310ff756b6e3ca4d3afa4c777324fbe3
BLAKE2b-256 b95e352fb932915a2957ec2567f33d34bbf38a19d32b5a6316a3545ab4beff2b

See more details on using hashes here.

Supported by

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