Commit ea75b225 authored by Michael Droettboom's avatar Michael Droettboom

Refactor pythonexc2js

parent a83e98b7
...@@ -6,6 +6,11 @@ ...@@ -6,6 +6,11 @@
#include "jsproxy.h" #include "jsproxy.h"
#include "pyproxy.h" #include "pyproxy.h"
static PyObject* tbmod = NULL;
static int
_python2js_unicode(PyObject* x);
int int
pythonexc2js() pythonexc2js()
{ {
...@@ -27,65 +32,61 @@ pythonexc2js() ...@@ -27,65 +32,61 @@ pythonexc2js()
goto exit; goto exit;
} }
PyObject* tbmod = PyImport_ImportModule("traceback");
if (tbmod == NULL) { if (tbmod == NULL) {
PyObject* repr = PyObject_Repr(value); tbmod = PyImport_ImportModule("traceback");
if (repr == NULL) { if (tbmod == NULL) {
excval = hiwire_string_ascii((int)"Could not get repr for exception"); PyObject* repr = PyObject_Repr(value);
} else { if (repr == NULL) {
excval = python2js(repr); excval = hiwire_string_ascii((int)"Could not get repr for exception");
Py_DECREF(repr); } else {
excval = _python2js_unicode(repr);
Py_DECREF(repr);
}
goto exit;
} }
}
PyObject* format_exception;
if (traceback == NULL || traceback == Py_None) {
no_traceback = 1;
format_exception = PyObject_GetAttrString(tbmod, "format_exception_only");
} else {
format_exception = PyObject_GetAttrString(tbmod, "format_exception");
}
if (format_exception == NULL) {
excval =
hiwire_string_ascii((int)"Could not get format_exception function");
} else { } else {
PyObject* format_exception; PyObject* pylines;
if (traceback == NULL || traceback == Py_None) { if (no_traceback) {
no_traceback = 1; pylines =
format_exception = PyObject_GetAttrString(tbmod, "format_exception_only"); PyObject_CallFunctionObjArgs(format_exception, type, value, NULL);
} else { } else {
format_exception = PyObject_GetAttrString(tbmod, "format_exception"); pylines = PyObject_CallFunctionObjArgs(
format_exception, type, value, traceback, NULL);
} }
if (format_exception == NULL) { if (pylines == NULL) {
excval = excval =
hiwire_string_ascii((int)"Could not get format_exception function"); hiwire_string_ascii((int)"Error calling traceback.format_exception");
PyErr_Print();
PyErr_Clear();
goto exit;
} else { } else {
PyObject* pylines; PyObject* empty = PyUnicode_FromString("");
if (no_traceback) { PyObject* pystr = PyUnicode_Join(empty, pylines);
pylines = printf("Python exception:\n");
PyObject_CallFunctionObjArgs(format_exception, type, value, NULL); printf("%s\n", PyUnicode_AsUTF8(pystr));
} else { excval = _python2js_unicode(pystr);
pylines = PyObject_CallFunctionObjArgs( Py_DECREF(pystr);
format_exception, type, value, traceback, NULL); Py_DECREF(empty);
} Py_DECREF(pylines);
if (pylines == NULL) {
excval =
hiwire_string_ascii((int)"Error calling traceback.format_exception");
PyErr_Print();
PyErr_Clear();
goto exit;
} else {
PyObject* newline = PyUnicode_FromString("");
PyObject* pystr = PyUnicode_Join(newline, pylines);
printf("Python exception:\n");
printf("%s\n", PyUnicode_AsUTF8(pystr));
excval = python2js(pystr);
Py_DECREF(pystr);
Py_DECREF(newline);
Py_DECREF(pylines);
}
Py_DECREF(format_exception);
} }
Py_DECREF(tbmod); Py_DECREF(format_exception);
} }
exit: exit:
Py_XDECREF(type);
Py_XDECREF(value);
Py_XDECREF(traceback);
PyErr_Clear(); PyErr_Clear();
hiwire_throw_error(excval); hiwire_throw_error(excval);
return HW_ERROR; return HW_ERROR;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment