Skip to content
Snippets Groups Projects
Commit d83b7274 authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen
Browse files

Finished exception handling (successfully pass through of exceptions).

parent 8eee7271
No related branches found
No related tags found
No related merge requests found
...@@ -39,11 +39,9 @@ char *bisondynlib_lookup_hash(void *handle) ...@@ -39,11 +39,9 @@ char *bisondynlib_lookup_hash(void *handle)
PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, int debug) PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, int debug)
{ {
PyObject *(*pparser)(PyObject *, void *, void *, int); PyObject *(*pparser)(PyObject *, void *, void *, int);
//PyObject *result;
//printf("bisondynlib_run: looking up parser\n");
pparser = bisondynlib_lookup_parser(handle); pparser = bisondynlib_lookup_parser(handle);
//printf("bisondynlib_run: calling parser, py_input=0x%lx\n", in);
if (!pparser) { if (!pparser) {
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"bisondynlib_lookup_parser() returned NULL"); "bisondynlib_lookup_parser() returned NULL");
...@@ -52,8 +50,10 @@ PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, in ...@@ -52,8 +50,10 @@ PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, in
(*pparser)(parser, cb, in, debug); (*pparser)(parser, cb, in, debug);
//printf("bisondynlib_run: back from parser\n"); // Do not ignore a raised exception, but pass the exception through.
//return result; if (PyErr_Occurred())
return NULL;
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
......
...@@ -88,6 +88,7 @@ cdef public object py_callback(object parser, char *target, int option, \ ...@@ -88,6 +88,7 @@ cdef public object py_callback(object parser, char *target, int option, \
Py_INCREF(names) Py_INCREF(names)
Py_INCREF(values) Py_INCREF(values)
# Construct handler's names and values list.
for i in range(nargs): for i in range(nargs):
termname = <char*>va_arg(ap, str_type) termname = <char*>va_arg(ap, str_type)
PyList_SetItem(names, i, termname) PyList_SetItem(names, i, termname)
...@@ -103,24 +104,16 @@ cdef public object py_callback(object parser, char *target, int option, \ ...@@ -103,24 +104,16 @@ cdef public object py_callback(object parser, char *target, int option, \
PyList_SetItem(values, i, valobj) PyList_SetItem(values, i, valobj)
Py_INCREF(valobj) Py_INCREF(valobj)
#if parser.verbose: va_end(ap)
# print 'py_callback: calling handler:', \
# (target, option, names, values)
# Set the signal handler and a timeout alarm # Set the signal handler and a timeout alarm
#signal.signal(signal.SIGALRM, parser.handle_timeout) #signal.signal(signal.SIGALRM, parser.handle_timeout)
#signal.alarm(parser.timeout) #signal.alarm(parser.timeout)
va_end(ap)
res = parser._handle(target, option, names, values) res = parser._handle(target, option, names, values)
#signal.alarm(0) #signal.alarm(0)
#if parser.verbose:
# print 'py_callback: handler returned:', res
return res return res
# callback routine for reading input # callback routine for reading input
...@@ -280,36 +273,11 @@ cdef class ParserEngine: ...@@ -280,36 +273,11 @@ cdef class ParserEngine:
def generate_exception_handler(self): def generate_exception_handler(self):
s = '' s = ''
#s = s + ' if ($$ && $$ != Py_None && PyObject_HasAttrString($$, "_pyBisonError"))\n'
#s = s + ' {\n'
#s = s + ' yyerror(PyString_AsString(PyObject_GetAttrString(py_parser, "last_error")));\n'
#s = s + ' Py_INCREF(Py_None);\n'
#s = s + ' YYERROR;\n'
#s = s + ' }\n'
s += ' if ($$ && $$ != Py_None)\n'
s += ' {\n' s += ' {\n'
s += ' if (PyObject_HasAttrString($$, "_pyBisonError"))\n' s += ' PyObject* obj = PyErr_Occurred();\n'
s += ' {\n' s += ' if (obj)\n'
s += ' //PyObject* last_error = PyObject_GetAttrString(py_parser, "last_error");\n'
s += ' //if (last_error && PyString_Check(last_error))\n'
s += ' // yyerror(PyString_AsString(last_error));\n'
s += ' //else\n'
s += ' // yyerror("No \\"last_error\\" attribute set in BisonError or not a string");\n'
s += ' Py_INCREF(Py_None);\n'
s += ' YYERROR;\n' s += ' YYERROR;\n'
s += ' }\n'
s += ' }\n' s += ' }\n'
#s += ' else\n'
#s += ' {\n'
#s += ' PyObject* obj = PyErr_Occurred();\n'
#s += ' if (obj)\n'
#s += ' {\n'
#s += ' fprintf(stderr, "exception caught in bison_:\\n");\n'
#s += ' PyErr_Print();\n'
#s += ' YYERROR;\n'
#s += ' }\n'
#s += ' }\n'
return s return s
......
...@@ -324,15 +324,8 @@ class BisonParser(object): ...@@ -324,15 +324,8 @@ class BisonParser(object):
print 'BisonParser._handle: call handler at line %s with: %s' \ print 'BisonParser._handle: call handler at line %s with: %s' \
% (hdlrline, str((targetname, option, names, values))) % (hdlrline, str((targetname, option, names, values)))
#self.last = handler(target=targetname, option=option, names=names, self.last = handler(target=targetname, option=option, names=names,
# values=values) values=values)
try:
self.last = handler(target=targetname, option=option,
names=names, values=values)
except Exception as e:
#traceback.print_exception(*sys.exc_info())
return self.error(e, sys.exc_info())
# raise
#if self.verbose: #if self.verbose:
# print 'handler for %s returned %s' \ # print 'handler for %s returned %s' \
...@@ -396,11 +389,11 @@ class BisonParser(object): ...@@ -396,11 +389,11 @@ class BisonParser(object):
while not self.file.closed: while not self.file.closed:
# do the parsing job, spew if error # do the parsing job, spew if error
self.last = None self.last = None
self.last_error = None
self.engine.runEngine(debug)
if self.last_error: try:
self.report_last_error(filename, self.last_error) self.engine.runEngine(debug)
except Exception as e:
self.report_last_error(filename, e)
if self.verbose: if self.verbose:
print 'Parser.run: back from engine' print 'Parser.run: back from engine'
...@@ -442,24 +435,24 @@ class BisonParser(object): ...@@ -442,24 +435,24 @@ class BisonParser(object):
return bytes return bytes
def _error(self, linenum, msg, tok): #def _error(self, linenum, msg, tok):
# TODO: should this function be removed? # # TODO: should this function be removed?
print 'Parser: line %s: syntax error "%s" before "%s"' \ # print 'Parser: line %s: syntax error "%s" before "%s"' \
% (linenum, msg, tok) # % (linenum, msg, tok)
def error(self, exception, traceback_info): #def error(self, exception, traceback_info):
""" # """
Return the result of this method from a handler to notify a syntax error # Return the result of this method from a handler to notify a syntax error
""" # """
# TODO: should this function be removed? # # TODO: should this function be removed?
self.last_error = BisonError(exception, traceback_info) # self.last_error = BisonError(exception, traceback_info)
return self.last_error # return self.last_error
def exception(self, exception): #def exception(self, exception):
# TODO: should this function be removed? # # TODO: should this function be removed?
self.lastexception = exception # self.lastexception = exception
return BisonException(exception) # return BisonException(exception)
def report_last_error(self, filename, error): def report_last_error(self, filename, error):
if filename != None: if filename != None:
...@@ -478,11 +471,13 @@ class BisonParser(object): ...@@ -478,11 +471,13 @@ class BisonParser(object):
print >>sys.stderr, msg print >>sys.stderr, msg
else: else:
print error
if not self.interactive: if not self.interactive:
raise error.value raise
if self.verbose:
traceback.print_exc()
traceback.print_exception(*error.traceback_info) print 'ERROR:', error
def toxml(self): def toxml(self):
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment