Write simple initialization function as well as a test function.

This commit is contained in:
Danila Fedorin 2017-05-05 19:59:13 -07:00
parent a2faeb0c9d
commit a5ce4bc032
2 changed files with 63 additions and 0 deletions

27
include/pyliblexmodule.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef PYLIBLEX_H
#define PYLIBLEX_H
#include "Python.h"
static PyObject* pyliblex_init(PyObject* self, PyObject* args);
static PyObject* pyliblex_add_definitions(PyObject* self, PyObject* args);
static PyObject* pyliblex_lex(PyObject* self, PyObject* args);
static PyObject* pyliblex_test(PyObject* self, PyObject* args);
static const char module_docstring[] = "A wrapper around a homemade lexing library, liblex.";
static PyMethodDef module_methods[] = {
{"init", pyliblex_init, METH_VARARGS, "Initialize a liblex library object." },
{"test", pyliblex_test, METH_VARARGS, "Tests whether the library is working." },
{ NULL, NULL, 0, NULL }
};
static struct PyModuleDef module_object = {
PyModuleDef_HEAD_INIT,
"pyliblex",
module_docstring,
-1,
module_methods
};
PyMODINIT_FUNC PyInit_pyliblex();
#endif

36
src/pyliblexmodule.c Normal file
View File

@ -0,0 +1,36 @@
#include "pyliblexmodule.h"
#include "liblex.h"
#include "eval.h"
static void _pyliblex_free_eval(PyObject* object){
eval_config* config = PyCapsule_GetPointer(object, NULL);
if(config){
liblex_result result = eval_config_free(config);
if(result != LIBLEX_SUCCESS){
PyErr_NoMemory();
}
}
}
static PyObject* pyliblex_init(PyObject* self, PyObject* args){
PyObject* return_object = NULL;
eval_config* config = malloc(sizeof(*config));
if(config){
eval_config_init(config);
return_object = PyCapsule_New(config, NULL, _pyliblex_free_eval);
} else {
return_object = PyErr_NoMemory();
}
return return_object;
}
static PyObject* pyliblex_add_definitions(PyObject* self, PyObject* args);
static PyObject* pyliblex_lex(PyObject* self, PyObject* args);
static PyObject* pyliblex_test(PyObject* self, PyObject* args) {
return PyUnicode_FromString("Test from liblex!");
}
PyMODINIT_FUNC PyInit_pyliblex() {
return PyModule_Create(&module_object);
}