Skip to main content

Import C files into Python, illegally.

Project description

Crimes

Blur the line between C and Python.

First, write some C code:

/* hello.c */
#include <stdio.h>

void hello(void) {
    printf("Hello world from C!\n");
}

Then import that C code into Python:

import crimes

crimes.commit()

from hello import hello

hello()  # prints "Hello world from C!"

All you had to do is call crimes.commit()!

What if I am a terrible C programmer?

Not to worry! crimes will print your syntax errors as part of the normal Python traceback:

/* hello.c */
#include <stdio.h>

/* missing ')' on the next line: */
void hello(void {
    printf("Hello world from C!\n");
}
import crimes

crimes.commit()

from hello import hello  # raises an error here

Gives you this error:

Traceback (most recent call last):
  File "/tmp/example.py", line 6, in <module>
    from hello import hello  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/hello.c", line 5, in hello.c
    void hello(void {
                    ^
crimes.exceptions.CCompileError: expected ';', ',' or ')' before '{' token

Install

[!WARNING] This is currently pretty hacky. I provide absolutely no guarantee that this will work on your machine. Here's how I got it working on my machine, which running macOS. I highly doubt this library works at all on Windows.

First, install the library:

pip install crimes

Next, make sure you have a compatible version of GCC. Any version of GCC 9.0 or greater should work. On most Linux distros, you probably already have this installed. On macOS though... Apple thought it was a good idea to symlink gcc to clang. This does not work. So first, install actual GCC using brew:

brew install gcc@13

Next, before using this module, make sure that CC is set to use gcc-13. You can do this universally like this:

export CC=gcc-13

...but I prefer to do it per-process:

env CC=gcc-13 python3 my-program-that-commits-crimes.py

Copying

Copyright © 2023 Eddie Antonio Santos. Apache-2.0 licensed. See LICENSE for details. Especially the Disclaimer of Warranty!

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

crimes-0.1.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

crimes-0.1.1-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file crimes-0.1.1.tar.gz.

File metadata

  • Download URL: crimes-0.1.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.4.0

File hashes

Hashes for crimes-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fdfc0066982a91aaf37f79485b697db27770f249636f25d21c40524f62850aa9
MD5 f3ea7db4030f68d463e743bd1a213714
BLAKE2b-256 4b4b2bfa502d40be1f880df92c1e6b51b71d8b9d81d09adad1e6a1cf28be0b49

See more details on using hashes here.

File details

Details for the file crimes-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: crimes-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.4.0

File hashes

Hashes for crimes-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 96fe487a9baa0f5051f710a7fd4644be6e6eb0645c46cd7dc50a58640355ff2d
MD5 bf14f16a22bc0af59e344d5d8c5a30b3
BLAKE2b-256 189d294f3b63311739a95f94d39544aee27ec530356f6c4d2fc19ef5cbaa5c9e

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