<commit_before>#include <Python.h>
#include "../libcwap.h"

static PyObject * serial_reader = NULL;

static size_t action_read_wrapper(char * buf, size_t size) {
    //these args are set up by libcwap.c, just fill'em with pythonic goodness
    PyObject *result = NULL;
    PyObject *arglist;
    int ret_size = 0;
    // Create a tuple to feed back to python
    arglist = Py_BuildValue("(s,)", size);
    // Actually call python: Have python grab size bytes from GPIO, I think?
    result = PyObject_CallObject(serial_reader, arglist);
    Py_DECREF(arglist);
    if (result == NULL)
        return 0; // stuff went tits-up.
    if (PyArg_ParseTuple(result, "s", buf))
        ret_size = size; // TODO Should verify length of s
    Py_DECREF(result);
    return ret_size;
}

static PyObject * pycwap_action(PyObject *dummy, PyObject *args) {
    PyObject *result = NULL;
    PyObject *temp;

    if (PyArg_ParseTuple(args, "O:set_callback", &temp)) {
        if (!PyCallable_Check(temp)) {
            PyErr_SetString(PyExc_TypeError, "parameter must be callable");
            return NULL;
        }
        Py_XINCREF(temp);         /* Add a reference to new callback */
        Py_XDECREF(serial_reader);  /* Dispose of previous callback */
        serial_reader = temp;       /* Remember new callback */
        /* Boilerplate to return "None" */
        Py_INCREF(Py_None);
        result = Py_None;
    }
    libcwap_action(action_read_wrapper);
    return result;
}

static PyMethodDef LibCwapMethods[] = {

    {"action",  pycwap_action, METH_VARARGS,
     "Make things happen."},
    //{"register",  pycwap_register, METH_VARARGS | METH_KEYWORDS,
    // "Register new things."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};


PyMODINIT_FUNC
initlibcwap(void) {
    (void) Py_InitModule("libcwap", LibCwapMethods);
}<commit_msg>What appears to be a working python C binding thing<commit_after>#include <Python.h>
#include "../libcwap.h"

static PyObject * serial_reader = NULL;

static size_t action_read_wrapper(char * buf, size_t size) {
    //these args are set up by libcwap.c, just fill'em with pythonic goodness
    PyObject *result = NULL;
    PyObject *arglist;
    Py_ssize_t ret_size = 0;
    // Create a tuple to feed back to python
    arglist = Py_BuildValue("I", size);
    // Actually call python: Have python grab size bytes from GPIO, I think?
    result = PyObject_CallObject(serial_reader, arglist);
    Py_DECREF(arglist);
    if (result == NULL)
        return 0; // stuff went tits-up.
    char from_python[size];
    if (!PyArg_ParseTuple(result, "s#", from_python, &ret_size))
        ret_size = 0;
    memcpy(buf, from_python, ret_size);
    Py_DECREF(result);
    return ret_size;
}

static PyObject * pycwap_action(PyObject *dummy, PyObject *args) {
    PyObject *result = NULL;

    if (PyArg_ParseTuple(args, "O:set_callback", &serial_reader)) {
        if (!PyCallable_Check(serial_reader)) {
            PyErr_SetString(PyExc_TypeError, "Parameter must be callable");
            return NULL;
        }
        Py_XINCREF(serial_reader); /* Increment reference count to new callback */
        /* Boilerplate to return "None" */
        Py_INCREF(Py_None);
        result = Py_None;
    }
    libcwap_action(action_read_wrapper);
    Py_XDECREF(serial_reader);  /* Dispose of callback */
    return result;
}

static PyMethodDef LibCwapMethods[] = {

    {"action",  pycwap_action, METH_VARARGS,
     "Make things happen."},
    //{"register",  pycwap_register, METH_VARARGS | METH_KEYWORDS,
    // "Register new things."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};


PyMODINIT_FUNC
initlibcwap(void) {
    (void) Py_InitModule("libcwap", LibCwapMethods);
}
{'fraction_non_alphanumeric': 0.11449353448275862, 'fraction_numerical': 0.0018857758620689656, 'mean_word_length': 3.0446623093681917, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2844829', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1227, 'n_words': 323}