From f1e0cc90855090ef989f0240ca851d7b14799028 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 26 Apr 2018 11:43:48 -0400 Subject: [PATCH] Fix "from js import foo" --- src/js2python.cpp | 10 ++++------ test/test_python.py | 6 ++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/js2python.cpp b/src/js2python.cpp index 3fcff5ead..45b0c51e5 100644 --- a/src/js2python.cpp +++ b/src/js2python.cpp @@ -32,13 +32,11 @@ PyObject *jsToPython(val x) { PyObject *pypy_x = py_x.x; Py_INCREF(pypy_x); return pypy_x; + } else if (!x["byteLength"].isUndefined()) { + std::string x_str = x.as(); + return PyBytes_FromStringAndSize(x_str.c_str(), x_str.size()); } else { - try { - std::string x_str = x.as(); - return PyBytes_FromStringAndSize(x_str.c_str(), x_str.size()); - } catch (...) { - return JsProxy_cnew(x); - } + return JsProxy_cnew(x); } } diff --git a/test/test_python.py b/test/test_python.py index e80200ebd..5f5e01f67 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -20,6 +20,12 @@ def test_print(selenium): assert 'This should be logged' in selenium.logs +def test_import_js(selenium): + result = selenium.run( + "from js import window\nwindow.title = 'Foo'\nwindow.title") + assert result == 'Foo' + + def test_run_core_python_test(python_test, selenium): selenium.run( "import sys\n"