<commit_before>/*
 * python.cc
 *
 * This python module lets us easily test libchecksieve using python. The
 * 'test' target will automatically compile this against libchecksieve.a
 */

#include <Python.h>

#include "checksieve.h"
#include "sieve_driver.hh"

static PyObject *parse_string(PyObject *self, PyObject *args) {
    const char *sieve;
    PyObject *quiet;

    if (!PyArg_ParseTuple(args, "sO:parse_string", &sieve, &quiet))
        return NULL;

    sieve::driver driver(PyObject_IsTrue(quiet));
    return Py_BuildValue("i", driver.parse_string(sieve));
}

static PyMethodDef checksieve_methods[] = {
    {"parse_string", parse_string, METH_VARARGS, "guhhh"},
    {NULL, NULL}
};

PyMODINIT_FUNC initchecksieve(void) {
    Py_InitModule3( "checksieve", checksieve_methods, "syntax-check a sieve");
}
<commit_msg>Add support for python 3 in unit test C extension.<commit_after>/*
 * python.cc
 *
 * This python module lets us easily test libchecksieve using python. The
 * 'test' target will automatically compile this against libchecksieve.a
 */

#include <Python.h>

#include "checksieve.h"
#include "sieve_driver.hh"

static PyObject *parse_string(PyObject *, PyObject *);

static PyMethodDef checksieve_methods[] = {
    {"parse_string", parse_string, METH_VARARGS, "guhhh"},
    {NULL, NULL}
};

#define PY_MODNAME "checksieve"
#define PY_MODDESC "syntax-check a mail sieve"

#if PY_MAJOR_VERSION >= 3
#define PY_MODINIT(name) PyMODINIT_FUNC PyInit_##name(void)

static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    PY_MODNAME,
    PY_MODDESC,
    -1,
    checksieve_methods,
    NULL,
    NULL,
    NULL,
    NULL,
};

#define PY_MODCREATE() PyModule_Create(&moduledef)
#else
#define PY_MODINIT(name) PyMODINIT_FUNC init##name(void)
#define PY_MODCREATE() Py_InitModule3( PY_MODNAME, checksieve_methods, PY_MODDESC )
#endif // PY_MAJOR_VERSION >= 3

static PyObject *parse_string(PyObject *self, PyObject *args) {
    const char *sieve;
    PyObject *quiet;

    if (!PyArg_ParseTuple(args, "sO:parse_string", &sieve, &quiet))
        return NULL;

    sieve::driver driver(PyObject_IsTrue(quiet));
    return Py_BuildValue("i", driver.parse_string(sieve));
}

PY_MODINIT(checksieve) {
    PY_MODCREATE();
}

{'fraction_non_alphanumeric': 0.11764705882352941, 'fraction_numerical': 0.00267379679144385, 'mean_word_length': 4.756410256410256, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3545867', 'n_tokens_mistral': 791, 'n_tokens_neox': 770, 'n_words': 187}