Skip to main content

IPC implementation of ctypes API

Project description

cpypes

cpypes is a drop-in substitute for ctypes that runs the compiled code in a separate process.

This provides separation between the Python process and the process running the compiled code, which could allow you to debug the compiled code more easily, or even run it on a separate machine!

Quick Start

pip install cpypes
>>> import cpypes

>>> libc = cpypes.CDLL("libc.so.6")
>>> dst = cpypes.create_string_buffer(10)
>>> libc.strncpy(dst, b"Hello, world!", 5)
-1783489504
>>> dst.value
b'Hello'

By default, cpypes starts up a server for itself. You can also start the server manually and then use

cpypes.connect(("127.0.0.1", 5555))

to connect to it (you should do right after importing it). The server is typically installed at .venv/cpypes/server/main. To start it listening on a port, run

.venv/cpypes/server/main 5555

Use Case Examples

  • You write a C library (.so) and want to write unit tests for the public functions. You can write the unit tests in Python, run them with pytest, and run the library under valgrind!

  • Say you have a .so on an embedded Linux device that you want to test out. You can run the cpypes server on the embedded device (you'll need to compile the server for that architecture, and make sure the embedded device has libffi on it) and then connect to it with cpypes running on your laptop!

Requirements

cpypes currently supports only Linux.

cpypes requires make, gcc, and libffi-dev to build the server.

Caveats

Because it runs the compiled code in a separate process, cpypes is inherently unsuitable for some applications. For example, running printf in cpypes will always write to the server's standard output rather than the current process's (which, incidentally, will most likely cause a fatal error for cpypes). You can certainly use file descriptors in the server; you just need to make sure you don't try to use the client's file descriptors in the server's context, or anything equivalent.

Coming Soon

  • Option to start server under valgrind.

  • Support for more types.

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

cpypes-0.0.1.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

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

cpypes-0.0.1-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cpypes-0.0.1.tar.gz
Algorithm Hash digest
SHA256 82de92c29ad23dcc089ca32ff29ddf874d8edb8fa9aaeb1cf99d9c8b1a98b544
MD5 41938f1b52e96b56bd4bcea17d3e5201
BLAKE2b-256 5879bc3e583eb41d31492ff41f91618e2280361a9a9b0d473cd971f6d7f8b2d3

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for cpypes-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0f675b19753fbd2ac9dc739fb210d9b1f63c94f540480876543c5d44c844392
MD5 f9014ac9b949fcf87b53cfbc61e71467
BLAKE2b-256 63fba522f57e8d5b37fc817c3ddad6c758cc2cd6a2d81783dd4fe430a1500020

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