From ad611d608d056c0135b458e8d805336fe0ffc52e Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 14 Feb 2021 03:18:57 -0800 Subject: [PATCH] Don't implicitly convert tuple and bytes objects into Javascript (#1234) --- src/core/python2js.c | 27 ++++++++------------------- src/tests/test_typeconversions.py | 17 +++++++++-------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/core/python2js.c b/src/core/python2js.c index f53e00189..c84f95b3c 100644 --- a/src/core/python2js.c +++ b/src/core/python2js.c @@ -273,7 +273,7 @@ _python2js_dict(PyObject* x, PyObject* map, int depth) } while (0) static JsRef -_python2js_immutable(PyObject* x, PyObject* map, int depth) +_python2js_immutable(PyObject* x) { if (x == Py_None) { return Js_undefined; @@ -287,14 +287,10 @@ _python2js_immutable(PyObject* x, PyObject* map, int depth) 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 (JsException_Check(x)) { return JsException_AsJs(x); - } else if (PyTuple_Check(x)) { - return _python2js_sequence(x, map, depth); } return NULL; } @@ -302,8 +298,8 @@ _python2js_immutable(PyObject* x, PyObject* map, int depth) static JsRef _python2js_deep(PyObject* x, PyObject* map, int depth) { - RETURN_IF_SUCCEEDS(_python2js_immutable(x, map, depth)); - if (PyList_Check(x)) { + RETURN_IF_SUCCEEDS(_python2js_immutable(x)); + if (PyList_Check(x) || PyTuple_Check(x)) { return _python2js_sequence(x, map, depth); } if (PyDict_Check(x)) { @@ -321,11 +317,10 @@ static JsRef _python2js(PyObject* x, PyObject* map, int depth) { if (depth == 0) { - RETURN_IF_SUCCEEDS(_python2js_immutable(x, map, 0)); + RETURN_IF_SUCCEEDS(_python2js_immutable(x)); return pyproxy_new(x); - } else { - return _python2js_deep(x, map, depth - 1); } + return _python2js_deep(x, map, depth - 1); } /* During conversion of collection types (lists and dicts) from Python to @@ -384,15 +379,9 @@ _python2js_cache(PyObject* x, PyObject* map, int depth) JsRef python2js(PyObject* x) { - PyObject* map = PyDict_New(); - JsRef result = _python2js_cache(x, map, 0); - Py_DECREF(map); - - if (result == NULL) { - pythonexc2js(); - } - - return result; + RETURN_IF_SUCCEEDS(_python2js_immutable(x)); + RETURN_IF_SUCCEEDS(pyproxy_new(x)); + pythonexc2js(); } JsRef diff --git a/src/tests/test_typeconversions.py b/src/tests/test_typeconversions.py index e5b29a392..d66e1fb34 100644 --- a/src/tests/test_typeconversions.py +++ b/src/tests/test_typeconversions.py @@ -14,12 +14,14 @@ def test_python2js(selenium): assert selenium.run_js('return pyodide.runPython("\'ιωδιούχο\'") === "ιωδιούχο"') assert selenium.run_js('return pyodide.runPython("\'碘化物\'") === "碘化物"') assert selenium.run_js('return pyodide.runPython("\'🐍\'") === "🐍"') - assert selenium.run_js( - "let x = pyodide.runPython(\"b'bytes'\");\n" - "return (x instanceof window.Uint8ClampedArray) && " - "(x.length === 5) && " - "(x[0] === 98)" - ) + # TODO: replace with suitable test for the behavior of bytes objects once we + # get the new behavior specified. + # assert selenium.run_js( + # "let x = pyodide.runPython(\"b'bytes'\");\n" + # "return (x instanceof window.Uint8ClampedArray) && " + # "(x.length === 5) && " + # "(x[0] === 98)" + # ) assert selenium.run_js( """ let proxy = pyodide.runPython("[1, 2, 3]"); @@ -437,10 +439,9 @@ def test_python2js_with_depth(selenium): throw new Error(`Assertion failed: ${msg}`); } } - let depths = [0, 3, 3, 3, 6, 6, 6] for(let i=0; i < 7; i++){ let x = pyodide._module.test_python2js_with_depth("a", i); - for(let j=0; j < depths[i]; j++){ + for(let j=0; j < i; j++){ assert(Array.isArray(x), `i: ${i}, j: ${j}`); x = x[1]; }