Commit 8babceb7 authored by Michael Droettboom's avatar Michael Droettboom Committed by GitHub

Merge pull request #257 from mdboom/python2js-refactor

Refactor python2js
parents f6b9a35d ea75b225
......@@ -6,6 +6,11 @@
#include "jsproxy.h"
#include "pyproxy.h"
static PyObject* tbmod = NULL;
static int
_python2js_unicode(PyObject* x);
int
pythonexc2js()
{
......@@ -27,16 +32,20 @@ pythonexc2js()
goto exit;
}
PyObject* tbmod = PyImport_ImportModule("traceback");
if (tbmod == NULL) {
tbmod = PyImport_ImportModule("traceback");
if (tbmod == NULL) {
PyObject* repr = PyObject_Repr(value);
if (repr == NULL) {
excval = hiwire_string_ascii((int)"Could not get repr for exception");
} else {
excval = python2js(repr);
excval = _python2js_unicode(repr);
Py_DECREF(repr);
}
} else {
goto exit;
}
}
PyObject* format_exception;
if (traceback == NULL || traceback == Py_None) {
no_traceback = 1;
......@@ -63,29 +72,21 @@ pythonexc2js()
PyErr_Clear();
goto exit;
} else {
PyObject* newline = PyUnicode_FromString("");
PyObject* pystr = PyUnicode_Join(newline, pylines);
PyObject* empty = PyUnicode_FromString("");
PyObject* pystr = PyUnicode_Join(empty, pylines);
printf("Python exception:\n");
printf("%s\n", PyUnicode_AsUTF8(pystr));
excval = python2js(pystr);
excval = _python2js_unicode(pystr);
Py_DECREF(pystr);
Py_DECREF(newline);
Py_DECREF(empty);
Py_DECREF(pylines);
}
Py_DECREF(format_exception);
}
Py_DECREF(tbmod);
}
exit:
Py_XDECREF(type);
Py_XDECREF(value);
Py_XDECREF(traceback);
PyErr_Clear();
hiwire_throw_error(excval);
return HW_ERROR;
}
......@@ -118,15 +119,18 @@ int
_python2js_cache(PyObject* x, PyObject* map);
static int
_python2js(PyObject* x, PyObject* map)
_python2js_float(PyObject* x)
{
double x_double = PyFloat_AsDouble(x);
if (x_double == -1.0 && PyErr_Occurred()) {
return HW_ERROR;
}
return hiwire_double(x_double);
}
static int
_python2js_long(PyObject* x)
{
if (x == Py_None) {
return hiwire_undefined();
} else if (x == Py_True) {
return hiwire_true();
} else if (x == Py_False) {
return hiwire_false();
} else if (PyLong_Check(x)) {
int overflow;
long x_long = PyLong_AsLongAndOverflow(x, &overflow);
if (x_long == -1) {
......@@ -135,24 +139,17 @@ _python2js(PyObject* x, PyObject* map)
if (py_float == NULL) {
return HW_ERROR;
}
double x_double = PyFloat_AsDouble(py_float);
Py_DECREF(py_float);
if (x_double == -1.0 && PyErr_Occurred()) {
return HW_ERROR;
}
return hiwire_double(x_double);
return _python2js_float(py_float);
} else if (PyErr_Occurred()) {
return HW_ERROR;
}
}
return hiwire_int(x_long);
} else if (PyFloat_Check(x)) {
double x_double = PyFloat_AsDouble(x);
if (x_double == -1.0 && PyErr_Occurred()) {
return HW_ERROR;
}
return hiwire_double(x_double);
} else if (PyUnicode_Check(x)) {
}
static int
_python2js_unicode(PyObject* x)
{
int kind = PyUnicode_KIND(x);
int data = (int)PyUnicode_DATA(x);
int length = (int)PyUnicode_GET_LENGTH(x);
......@@ -167,16 +164,22 @@ _python2js(PyObject* x, PyObject* map)
PyErr_SetString(PyExc_ValueError, "Unknown Unicode KIND");
return HW_ERROR;
}
} else if (PyBytes_Check(x)) {
}
static int
_python2js_bytes(PyObject* x)
{
char* x_buff;
Py_ssize_t length;
if (PyBytes_AsStringAndSize(x, &x_buff, &length)) {
return HW_ERROR;
}
return hiwire_bytes((int)(void*)x_buff, length);
} else if (JsProxy_Check(x)) {
return JsProxy_AsJs(x);
} else if (PyList_Check(x) || is_type_name(x, "<class 'numpy.ndarray'>")) {
}
static int
_python2js_sequence(PyObject* x, PyObject* map)
{
int jsarray = hiwire_array();
if (_python2js_add_to_cache(map, x, jsarray)) {
hiwire_decref(jsarray);
......@@ -210,7 +213,11 @@ _python2js(PyObject* x, PyObject* map)
return HW_ERROR;
}
return jsarray;
} else if (PyDict_Check(x)) {
}
static int
_python2js_dict(PyObject* x, PyObject* map)
{
int jsdict = hiwire_object();
if (_python2js_add_to_cache(map, x, jsdict)) {
hiwire_decref(jsdict);
......@@ -241,6 +248,32 @@ _python2js(PyObject* x, PyObject* map)
return HW_ERROR;
}
return jsdict;
}
static int
_python2js(PyObject* x, PyObject* map)
{
if (x == Py_None) {
return hiwire_undefined();
} else if (x == Py_True) {
return hiwire_true();
} else if (x == Py_False) {
return hiwire_false();
} else if (PyLong_Check(x)) {
return _python2js_long(x);
} else if (PyFloat_Check(x)) {
return _python2js_float(x);
} else if (PyUnicode_Check(x)) {
return _python2js_unicode(x);
} else if (PyBytes_Check(x)) {
return _python2js_bytes(x);
} else if (JsProxy_Check(x)) {
return JsProxy_AsJs(x);
} else if (PyList_Check(x) || PyTuple_Check(x) ||
is_type_name(x, "<class 'numpy.ndarray'>")) {
return _python2js_sequence(x, map);
} else if (PyDict_Check(x)) {
return _python2js_dict(x, map);
} else {
Py_INCREF(x);
return pyproxy_new((int)x);
......@@ -265,25 +298,21 @@ _python2js_add_to_cache(PyObject* map, PyObject* pyparent, int jsparent)
/* Use the pointer converted to an int so cache is by identity, not hash */
PyObject* pyparentid = PyLong_FromSize_t((size_t)pyparent);
PyObject* jsparentid = PyLong_FromLong(jsparent);
if (PyDict_SetItem(map, pyparentid, jsparentid)) {
Py_DECREF(pyparentid);
Py_DECREF(jsparentid);
return HW_ERROR;
}
int result = PyDict_SetItem(map, pyparentid, jsparentid);
Py_DECREF(pyparentid);
Py_DECREF(jsparentid);
return 0;
return result ? HW_ERROR : 0;
}
int
_python2js_remove_from_cache(PyObject* map, PyObject* pyparent)
{
PyObject* pyparentid = PyLong_FromSize_t((size_t)pyparent);
if (PyDict_DelItem(map, pyparentid)) {
return HW_ERROR;
}
int result = PyDict_DelItem(map, pyparentid);
Py_DECREF(pyparentid);
return 0;
return result ? HW_ERROR : 0;
}
int
......
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