Skip to main content

Creates C code skeleton for handcrafted Python extension modules

Project description

Full of Eels

FullOfEels is an assistant program to generate the skeleton code for a handcrafted artisanal Python module to be implemented in native C.

It is not a replacement for SWIG or SIP. If you have an existing C/C++ library and want to create a Python wrapper with the same API, either of those will do just fine. Nor is it a replacement for ctypes.

FullOfEels works in the opposite direction. You start by specifying the Python API that you want: module, classes, methods, and functions. This program will create the initial .h/.c files for you, generating empty C functions with argument parsing code, struct definitions for the object and type, module init function ... all the the boring but necessary bits. You then fill them out with working C code.

How do you specify the Python API you want? By writing it in Python. You create a module that defines what the functions and classes will be, with all the arguments and methods, but none of the code that defines how it works - methods and functions should all just pass. FullOfEels keeps docstrings and some globals, but actual statements are discarded.

Why might you prefer this? Because you can design a more Pythonic API. The great advantage of SWIG/SIP is that the API is already known and (ideally) documentation exists. The disadvantage is that you're just recreating a C/C++ API in Python, and that programmers will have to be able to understand C/C++ to understand the documentation. There are times when this is the best solution. Fullofeels is for when it isn't.

Written by Hugh(Hugo) Fisher, Canberra Australia, 2016-now.
Released under MIT license
All feedback welcome: hugo.fisher@gmail.com (Yes, hugo not hugh)

Example

The Python "Extending Python with C or C++" web pages start by creating a spam module with a single system() function. With fullOfEels you start this by writing spam.py which is just

def system(command):
    """Execute a shell command."""
    pass

Then you run

python -m fullofeels -print spam.py

which will create

spam.h
spam.c
setup.py

Look inside and you'll see all the skeleton code needed for the complete Python extension module. Run

python setup.py build_ext --inplace

and you will have a valid module. From your Python interpreter you can import it and invoke the spam.system("hello") function, which will just print out the function name and arguments (specified by the -print CLI option).

Because fullOfEeels is an assistant, not an automated solution, you still need to fill in the function body yourself to make it do anything useful.

For the "Defining New Types" web example you would write custom.py:

class Custom(object):
    """Custom objects"""

    def __new__(cls):
        pass

    def __init__(self, first, last, number: int):
        pass

    def __del__(self):
        pass

    def name(self) -> str:
        """Return the name, combining the first and last name"""
        pass

and again run fullOfEels on it. The generated C code will have the all the code necessary to declare and initialize the Custom type structure. As before you could build this module, import it, and even create Custom objects. But without additional code they won't do anything and won't have any attributes or properties.

TODO

Heap allocated types

Look at new Python C API for 3.10+

Tested on

Python 2.7  Fedora Core 24, MacOS 10.14 XCode 11
Python 3.5  Fedora Core 24
Python 3.6  Windows 8.1 MSVC 2017
Python 3.7  Fedora Core 31, Windows 10 MSVC 2019
Python 3.9  Fedora Core 31

Versions

1.7 Mar 2021 Type annotations recognised for parameters

1.6 Feb 2021 Automate testing on Windows, cleanup for PyPI release

1.5 Jan 2019 Replace separate program with -m fullofeels package

1.4 Nov 2017 Generate setup.py for module

1.3 Oct 2017 Properties on class become C fields in object struct

1.2 Feb 2017 Can specify argument types for PyArg_ParseTuple

1.1 Nov 2016 Special slots for collections, numbers, buffer interface

1.0 Apr 2016 Modules, objects, classes, methods

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

fullofeels-1.7.0.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

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

fullofeels-1.7.0-py3-none-any.whl (35.0 kB view details)

Uploaded Python 3

File details

Details for the file fullofeels-1.7.0.tar.gz.

File metadata

  • Download URL: fullofeels-1.7.0.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for fullofeels-1.7.0.tar.gz
Algorithm Hash digest
SHA256 3b3e8d7b5099d9dbe480e6ec0a0cf19de900299c323a7f42bd9c4eee1b4a102e
MD5 50dfb0a2c57f8513dbbe47abe8a7e75a
BLAKE2b-256 56a77b271e573739e90a74c1d130c1115597ee298ee17283ef16ecc64b2d4ab8

See more details on using hashes here.

File details

Details for the file fullofeels-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: fullofeels-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 35.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for fullofeels-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5adc9e3885d24bc10340f55e1646f8ff3aac211f5bed8cfd8dac9723e94e3813
MD5 cfc187aa578014af3817c52b8c1efe1b
BLAKE2b-256 ca0f3e5a8f64cb2931d03ce6b919e8262af4d43d2f55713001b0bd0660d34860

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