From 6c9f4fbbf3b2560c90956a04562727242ab80980 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Tue, 4 Dec 2018 00:17:27 +0100 Subject: [PATCH 1/3] Handle multi-line string input --- src/pyodide.py | 4 ++++ test/conftest.py | 6 ------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/pyodide.py b/src/pyodide.py index 48b0a55c0..59e7624cb 100644 --- a/src/pyodide.py +++ b/src/pyodide.py @@ -4,6 +4,7 @@ A library of helper utilities for connecting Python to the browser environment. import ast import io +from textwrap import dedent __version__ = '0.1.11' @@ -24,6 +25,9 @@ def eval_code(code, ns): """ Runs a string of code, the last part of which may be an expression. """ + # handle mis-indented input from multi-line strings + code = dedent(code) + mod = ast.parse(code) if isinstance(mod.body[-1], ast.Expr): expr = ast.Expression(mod.body[-1].value) diff --git a/test/conftest.py b/test/conftest.py index b4a973148..93388db29 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -102,17 +102,11 @@ class SeleniumWrapper: self.driver.execute_script("window.logs = []") def run(self, code): - if isinstance(code, str) and code.startswith('\n'): - # we have a multiline string, fix indentation - code = textwrap.dedent(code) return self.run_js( 'return pyodide.runPython({!r})'.format(code)) def run_async(self, code): from selenium.common.exceptions import TimeoutException - if isinstance(code, str) and code.startswith('\n'): - # we have a multiline string, fix indentation - code = textwrap.dedent(code) self.run_js( """ window.done = false; From 30d150386d118188fd3cd0f7192367917c29c78b Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Tue, 4 Dec 2018 13:37:36 +0100 Subject: [PATCH 2/3] Revert change for runPythonAsync --- test/conftest.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/conftest.py b/test/conftest.py index 93388db29..69e8c312c 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -107,6 +107,9 @@ class SeleniumWrapper: def run_async(self, code): from selenium.common.exceptions import TimeoutException + if isinstance(code, str) and code.startswith('\n'): + # we have a multiline string, fix indentation + code = textwrap.dedent(code) self.run_js( """ window.done = false; From 18ec5bbb26a6305861d4d85a0c244bbd47f6d7d7 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Thu, 6 Dec 2018 15:39:28 +0100 Subject: [PATCH 3/3] Also dedent code in find_imports --- src/pyodide.py | 3 +++ test/conftest.py | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pyodide.py b/src/pyodide.py index 59e7624cb..41cd9651d 100644 --- a/src/pyodide.py +++ b/src/pyodide.py @@ -48,6 +48,9 @@ def find_imports(code): Finds the imports in a string of code and returns a list of their package names. """ + # handle mis-indented input from multi-line strings + code = dedent(code) + mod = ast.parse(code) imports = set() for node in ast.walk(mod): diff --git a/test/conftest.py b/test/conftest.py index 69e8c312c..93388db29 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -107,9 +107,6 @@ class SeleniumWrapper: def run_async(self, code): from selenium.common.exceptions import TimeoutException - if isinstance(code, str) and code.startswith('\n'): - # we have a multiline string, fix indentation - code = textwrap.dedent(code) self.run_js( """ window.done = false;