Reference

This document describes the specifics of the modules, classes, and methods provided by c. It refrains from discussing how the parts might be used in a wider scope; the Usage document should be read for such information.

c

🐚 About

c is a Python project providing a metapath hook for loading C extension modules without prior compilation by a ‘setup.py’ script. It uses the sysconfig module to extract the necessary information for compiling an extension module.

Loadable modules are identified by the ‘.py.c’, ‘.py.m’, and ‘.py.c++’ suffix. Composition of multiple files is not directly supported, but preprocessor directives may be used in order to combine multiple files.

Sample

Typical usage:

>>> import package.libfoo
>>> package.libfoo.return_true()
True

Where:

package/
        __init__.py
        libfoo.py.c

libfoo.py.c:

/*
 * libfoo.py.c - test library for the c loader
 */
#include <Python.h>
#include <structmember.h>

static PyObject *
return_true(PyObject *self)
{
        Py_INCREF(Py_True);
        return(Py_True);
}

/* METH_O, METH_VARARGS, METH_VARKEYWORDS, METH_NOARGS */
static PyMethodDef methods[] = {
        {"return_true", (PyCFunction) return_true, METH_NOARGS, "return `True`"},
        {NULL}
};

static struct PyModuleDef module = {
        PyModuleDef_HEAD_INIT,
        "package.libfoo",       /* name of module */
        NULL,                                   /* module documentation, may be NULL */
        -1,                                     /* size of per-interpreter state of the module,
                                                                or -1 if the module keeps state in global variables. */
        methods,
};

PyMODINIT_FUNC
PyInit_libfoo(void)
{
        PyObject *mod;
        mod = PyModule_Create(&module);
        return(mod);
}

c.lib

Compilation and linkage basics.

c.lib.compile(target, filename, type)

Construct the parameters to be used to compile and link the new executable.

Construct the parameters to be used to compile and link the new executable.

c.loader

Loader implementation for automatic compilation and linking of C-API extensions upon import.

class c.loader.CLoader(pkg, name, source, type='c')

Bases: builtins.object

Compile and Linke C-API modules with import statements.

is_package(*args, **kw)

cloader does not handle packages

c.loader.get_suffixes(_suffixes, *args, **kw)

c.loader override of imp.get_suffixes

c.loader.install()

Install the meta path hook and override imp.get_suffixes

c.loader.pthfile = '# THIS FILE MAKES THE *.py.(c|m|c++) FILES IMPORTABLE\nimport c.loader; c.loader.install()\n'

Contents of the cloader.pth file.

c.loader.remove()

Remove the meta path hook.

Table Of Contents

Previous topic

Usage

This Page