Don't implicitly convert tuple and bytes objects into Javascript (#1234)

This commit is contained in:
Hood Chatham 2021-02-14 03:18:57 -08:00 committed by GitHub
parent eb2bd5c458
commit ad611d608d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 27 deletions

View File

@ -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

View File

@ -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];
}