Skip to main content

Inline C function declarations

Project description

ExternC - Embedding C code in python

Example

import externc

# ExternC modifies builtins, so no `externc.` necessary
fn = c("""
  // remember to properly escape backslashes!
  printf("Hello from C\\n");
  Py_RETURN_NONE;
""")

fn() # "Hello from C"

Benchmark

c.py:

import externc
c(r"""
for(int i = 0; i < 1000000000; i++) {
  if(i % (2 << 16) == 0) {
    printf("%d\n", i);
  }
}
Py_RETURN_NONE;
""")()

python.py:

for i in range(1000000000):
  if(i % (2 << 16) == 0):
    print(i)
$ time python c.py
...

real    0m2.615s
user    0m1.593s
sys     0m0.518s
$ time python python.py
...

real    3m25.695s
user    3m25.423s
sys     0m0.194s

The difference is quite obvious

Use cases

Use ExternC

  • for interfacing with C APIs
  • for working with raw pointers
  • for working with capsules
  • when speed is more important than stability

Do not use ExternC

  • when your code must be version independent
  • when your code must be stable (exception: you have used C a lot before)
  • when your code must be distributable
  • when your code must run anywhere but the PC you wrote and tested it on
  • in production

Technicalities

The C function is registered as METH_VARARGS | METH_KEYWORDS. It is of signature PyObject* (PyObject*, PyObject*, PyObject), which is known in the Python extension environment as PyCFunctionWithKeywords. The arguments have the names self, args and kwargs, while the function is named func and the generated module externc_# where # is a hexadecimal number that is incremented by one with every call to c without an explicit module name. The function name and the module name can be modified by passing the funcname and name keyword arguments to c, respectively. funcname is also used as the name of the function to be exported from the module.

The default compiler used is clang with a few default options. The compilers submodule can be used to configure clang with feature flags, linked files and additional include directories or define another compiler, which can be passed to c via the compiler argument.

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

externc-1.1.1.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

externc-1.1.1-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file externc-1.1.1.tar.gz.

File metadata

  • Download URL: externc-1.1.1.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.12.0a2+

File hashes

Hashes for externc-1.1.1.tar.gz
Algorithm Hash digest
SHA256 34f9eaae10e8868a819ecef4faa85c0c482d5c1c6947543729ce3cd3b0342a2b
MD5 4a69a3837012627c13b35ae040bf70dc
BLAKE2b-256 b891a6f1b14cb252c2af63771ea3cc2dcd3fb9ced6956d8f8ff89825cc35320e

See more details on using hashes here.

File details

Details for the file externc-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: externc-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.12.0a2+

File hashes

Hashes for externc-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9fce4ca37393219b4b3c2e31d75e76d7f065c577e739e1d2a06ebeb1ff7ea8a3
MD5 5abec2642561f8c99182be29d201b8d0
BLAKE2b-256 8f41a9a9be3ee232e2d609721f51ef468e84f62a36db680d2d59dd944b0a7db7

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