Implement throwing exceptions.
This commit is contained in:
parent
69afdab402
commit
46f52665cb
|
@ -8,6 +8,9 @@ static PyObject* pyliblex_add_definition(PyObject* self, PyObject* args);
|
||||||
static PyObject* pyliblex_lex(PyObject* self, PyObject* args);
|
static PyObject* pyliblex_lex(PyObject* self, PyObject* args);
|
||||||
static PyObject* pyliblex_test(PyObject* self, PyObject* args);
|
static PyObject* pyliblex_test(PyObject* self, PyObject* args);
|
||||||
|
|
||||||
|
static PyObject* invalid_regex_exception;
|
||||||
|
static PyObject* failed_lex_exception;
|
||||||
|
|
||||||
static const char module_docstring[] = "A wrapper around a homemade lexing library, liblex.";
|
static const char module_docstring[] = "A wrapper around a homemade lexing library, liblex.";
|
||||||
static PyMethodDef module_methods[] = {
|
static PyMethodDef module_methods[] = {
|
||||||
{"init", pyliblex_init, METH_VARARGS, "Initialize a liblex library object." },
|
{"init", pyliblex_init, METH_VARARGS, "Initialize a liblex library object." },
|
||||||
|
|
|
@ -36,8 +36,10 @@ static PyObject* pyliblex_add_definition(PyObject* self, PyObject* args){
|
||||||
eval_config* config = PyCapsule_GetPointer(capsule, NULL);
|
eval_config* config = PyCapsule_GetPointer(capsule, NULL);
|
||||||
if(config){
|
if(config){
|
||||||
liblex_result result = eval_config_add(config, regex, id);
|
liblex_result result = eval_config_add(config, regex, id);
|
||||||
if(result != LIBLEX_SUCCESS){
|
if(result == LIBLEX_MALLOC){
|
||||||
return_object = PyErr_NoMemory();
|
return_object = PyErr_NoMemory();
|
||||||
|
} else if(result != LIBLEX_SUCCESS){
|
||||||
|
PyErr_SetString(invalid_regex_exception, "Unable to construct regex.");
|
||||||
} else {
|
} else {
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return_object = Py_None;
|
return_object = Py_None;
|
||||||
|
@ -86,8 +88,10 @@ static PyObject* pyliblex_lex(PyObject* self, PyObject* args){
|
||||||
|
|
||||||
ll_init(&match_ll);
|
ll_init(&match_ll);
|
||||||
result = eval_all(string, 0, config, &match_ll);
|
result = eval_all(string, 0, config, &match_ll);
|
||||||
if(result != LIBLEX_SUCCESS){
|
if(result == LIBLEX_MALLOC){
|
||||||
return_object = PyErr_NoMemory();
|
return_object = PyErr_NoMemory();
|
||||||
|
} if(result != LIBLEX_SUCCESS){
|
||||||
|
PyErr_SetString(failed_lex_exception, "Unable to parse text.");
|
||||||
} else {
|
} else {
|
||||||
int return_code = 0;
|
int return_code = 0;
|
||||||
PyObject* list = PyList_New(0);
|
PyObject* list = PyList_New(0);
|
||||||
|
@ -115,5 +119,32 @@ static PyObject* pyliblex_test(PyObject* self, PyObject* args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
PyMODINIT_FUNC PyInit_pyliblex() {
|
PyMODINIT_FUNC PyInit_pyliblex() {
|
||||||
return PyModule_Create(&module_object);
|
invalid_regex_exception = NULL;
|
||||||
|
failed_lex_exception = NULL;
|
||||||
|
PyObject* module = NULL;
|
||||||
|
int clear_exceptions = 1;
|
||||||
|
invalid_regex_exception = PyErr_NewException("pyliblex.InvalidRegexException", NULL, NULL);
|
||||||
|
failed_lex_exception = PyErr_NewException("pyliblex.FailedLexException", NULL, NULL);
|
||||||
|
|
||||||
|
if(invalid_regex_exception && failed_lex_exception){
|
||||||
|
module = PyModule_Create(&module_object);
|
||||||
|
if(module) {
|
||||||
|
if(!(PyModule_AddObject(module, "InvalidRegexException", invalid_regex_exception) ||
|
||||||
|
PyModule_AddObject(module, "FailedLexException", failed_lex_exception))){
|
||||||
|
Py_INCREF(invalid_regex_exception);
|
||||||
|
Py_INCREF(failed_lex_exception);
|
||||||
|
clear_exceptions = 0;
|
||||||
|
} else {
|
||||||
|
Py_DECREF(module);
|
||||||
|
module = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(clear_exceptions){
|
||||||
|
if(invalid_regex_exception) Py_DECREF(invalid_regex_exception);
|
||||||
|
if(failed_lex_exception) Py_DECREF(failed_lex_exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return module;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user