Skip to main content

Cee EXtensions Interpolation

Project description

Cee EXtensions Interpolation

Rationale

cexi aims to facilitate developing of C extension for CPython. In particular interfacing between python and C. In general building C extensions is a tedious process, especially when it comes to interfacing between two languages. cexi aims to make it easier by providing a simple interface to write C extensions in a pythonic way. It's not a replacement for Cython, CFFI, SWIG, Boost.Python or PyBind11, but rather a tool to make writing C extensions easier. It's main features are:

  • pythonic interface to write C extensions
  • automatic re/compilation of C extensions
  • automatic loading of C extensions

Code example

from cexi import Module, s

class Foo(Module):

    class options:
        flags = ['-O3']

    """
    #include "stdio.h"
    """

    @s.cee
    def foo(x: int) -> int:
        """
        return x + 1;
        """

    @s.share
    def bar(self, x: int) -> int:
        return x ** 2

    @s.py
    def baz(x: int, y: int) -> (int, int):
        """
        printf("baz\\n");
        int ret = x + y;
        bar(ret, &ret);
        return(foo(x + y), ret);
        """

    def wrapped_baz(self, x, y):
        print(f'calling {self.baz}')
        return self.baz(x, y)

Foo().foo(1)  # => 2
ret = Foo().wrapped_baz(3, 4) # => prints
                              # calling <cexi.proxy.Proxy object at 0x7f4bae56cf10>
                              # baz

print(ret)  # => prints (8, 49)


class Foo(Module, near=__file__):
    pass


class Foo(Module):
    directory = Path('/some/path')

Breakdown

Each extension begins with a class definition that inherits from cexi.Module. User can define flags that will be passed to compiler via options class attribute.

class Foo(Module):

    class options:
        flags = ['-O3']

Module docstring is directly inserted at the beginning of cee extension. It's useful to include headers that will be used in extension's code.

class Foo(Module):
    """
    #include "stdio.h"
    """

Then user can define functions that will be compiled as extension's functions. There are three types of functions:

  • @s.cee - plain cee function, it's useful to organize plain cee routines. .cee functions aren't available for use in python, they're simply excluded from final class namespace
  • @s.share - .share is a plain python function that is added to module to be used in cee code it's signature is transformed as such: func([input parameters]) -> <output parameters> => int func([input parameters], [pointers to output parameters]) e.g. bar here becomes int bar(int x, int *a), it returns non-zero value on failure & modify pointer to the return value
  • @s.py - .py is a cee extension function. it's available from python & can use both .cee & .share functions at will Untagged functions are left untouched, i.e. they're just instance's methods. cexi.Module children act as singletones - on instantiation they're get loaded/compiled by default they are built inside temporaty directories, but users are able to make modules persistent. There are two ways to do it:
  • near parameter - if it's set to __file__ module will be built in directory at __file__/../<module name>_cexi_module
  • directory class attribute - if it's set module will be built in specified directory

Persistent extensions compiled on-demand. If extension is persistent cexi first loads it's module. Then it compares revisions, which are hashed module's contents. Thus if class definition is changed since last compilation it will be re-compiled and re-loaded.

You can also compile persistent extensions with command

python -mcexi <module path>:<class name>

Installation

pip install cexi

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

cexi-0.0.3.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

cexi-0.0.3-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file cexi-0.0.3.tar.gz.

File metadata

  • Download URL: cexi-0.0.3.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for cexi-0.0.3.tar.gz
Algorithm Hash digest
SHA256 32ead3ed73683db7b446db94c085e5ff3aad55d3f77417fbc688562dfd8ac53c
MD5 3486a73456782341f2144e40c9186fd0
BLAKE2b-256 f6be92718cf196924e68fb38f6d99aa412bd2991c12a02a2077ff1900d28d041

See more details on using hashes here.

File details

Details for the file cexi-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: cexi-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for cexi-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c4840e129a39761d3f400a1ef25eb9949c357bf6d0695a66461403225406256d
MD5 9d08e3412f07dd4d0285e86a9fa9945b
BLAKE2b-256 992404a6d8cacaf7a9b497a9978dd8a97dda4f83490695a9750d2ad0e1f8f061

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