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,16 +32,20 @@ pythonexc2js() ...@@ -27,16 +32,20 @@ pythonexc2js()
goto exit; goto exit;
} }
PyObject* tbmod = PyImport_ImportModule("traceback"); if (tbmod == NULL) {
tbmod = PyImport_ImportModule("traceback");
if (tbmod == NULL) { if (tbmod == NULL) {
PyObject* repr = PyObject_Repr(value); PyObject* repr = PyObject_Repr(value);
if (repr == NULL) { if (repr == NULL) {
excval = hiwire_string_ascii((int)"Could not get repr for exception"); excval = hiwire_string_ascii((int)"Could not get repr for exception");
} else { } else {
excval = python2js(repr); excval = _python2js_unicode(repr);
Py_DECREF(repr); Py_DECREF(repr);
} }
} else { goto exit;
}
}
PyObject* format_exception; PyObject* format_exception;
if (traceback == NULL || traceback == Py_None) { if (traceback == NULL || traceback == Py_None) {
no_traceback = 1; no_traceback = 1;
...@@ -63,29 +72,21 @@ pythonexc2js() ...@@ -63,29 +72,21 @@ pythonexc2js()
PyErr_Clear(); PyErr_Clear();
goto exit; goto exit;
} else { } else {
PyObject* newline = PyUnicode_FromString(""); PyObject* empty = PyUnicode_FromString("");
PyObject* pystr = PyUnicode_Join(newline, pylines); PyObject* pystr = PyUnicode_Join(empty, pylines);
printf("Python exception:\n"); printf("Python exception:\n");
printf("%s\n", PyUnicode_AsUTF8(pystr)); printf("%s\n", PyUnicode_AsUTF8(pystr));
excval = python2js(pystr); excval = _python2js_unicode(pystr);
Py_DECREF(pystr); Py_DECREF(pystr);
Py_DECREF(newline); Py_DECREF(empty);
Py_DECREF(pylines); Py_DECREF(pylines);
} }
Py_DECREF(format_exception); Py_DECREF(format_exception);
} }
Py_DECREF(tbmod);
}
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