Topics

Subscribe

Loading C Plugins for PureDocs

I finally had a chance to sit back down with PureDocs, after getting the build process straightened out, and figure out how to load C plugins. Turns out, it's pretty simple with ctypes. Let's imagine I have a C library, compiled and linked as libtest, containing the following (a simple modification of my original example):

#include <stdio.h>
#include "puredocs.h"

Resource * test_function() {
    Resource * r = puredocs_createResource("foo");
    Resource * c = puredocs_createResource("bar");

    puredocs_setDocString(r, "omg some docs lol");
    puredocs_setDocString(c, "some bar docs omg lol");

    puredocs_setProperty(r, "feh", "omg");

    if ((puredocs_addChild(r, c)) < 0) {
        printf("OH NOES!\n");
    }

    return r;
}

This function can easily be called when loading the libary with``ctypes``'s PyDLL type (won't release the Python interpreter's GIL, which is important if we're creating Python objects we want to use). The restype of the function needs to be set to ctypes.py_object, since we're returning a Python object. The whole process looks like this:

>>> from ctypes import *
>>> t = pydll.LoadLibrary("libtest.dylib")
>>> t.test_function.restype=py_object
>>> r = t.test_function()
>>> r
<Resource foo [<Resource bar []>]>

The whole point of this exercise is basically to make it possible to write C libraries that serve as plugins for PureDocs that don't necessarily have any knowledge of Python. They'll be linked against a PureDocs library that provides the glue-code. So, for PureDocs's purpose, I now just need to wrap this around some library loading and registration code, and we'll be ready to go!

Comments

Name
Email