Commit 222457c2 authored by Roman Yurchak's avatar Roman Yurchak Committed by GitHub

Merge pull request #208 from mdboom/long-ints

Handle Python long ints as Javascript floats
parents 95379aa0 b7769908
......@@ -127,9 +127,23 @@ _python2js(PyObject* x, PyObject* map)
} else if (x == Py_False) {
return hiwire_false();
} else if (PyLong_Check(x)) {
long x_long = PyLong_AsLongLong(x);
if (x_long == -1 && PyErr_Occurred()) {
return HW_ERROR;
int overflow;
long x_long = PyLong_AsLongAndOverflow(x, &overflow);
if (x_long == -1) {
if (overflow) {
PyObject* py_float = PyNumber_Float(x);
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);
} else if (PyErr_Occurred()) {
return HW_ERROR;
}
}
return hiwire_int(x_long);
} else if (PyFloat_Check(x)) {
......
......@@ -58,6 +58,15 @@ def test_python2js(selenium):
""")
def test_python2js_long_ints(selenium):
assert selenium.run('2**30') == 2**30
assert selenium.run('2**31') == 2**31
assert selenium.run('2**30 - 1 + 2**30') == (2**30 - 1 + 2**30)
assert selenium.run('2**32 / 2**4') == (2**32 / 2**4)
assert selenium.run('-2**30') == -2**30
assert selenium.run('-2**31') == -2**31
def test_pythonexc2js(selenium):
try:
selenium.run_js('return pyodide.runPython("5 / 0")')
......
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