From f4400b12f1fc88d480f2721d9f5167f7c2424ba0 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Wed, 19 Dec 2018 14:22:18 -0500 Subject: [PATCH] Add a proxy to get global objects from Python --- src/pyodide.js | 3 +++ test/test_python.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/pyodide.js b/src/pyodide.js index 895fdd133..f7ef61dd1 100644 --- a/src/pyodide.js +++ b/src/pyodide.js @@ -235,6 +235,7 @@ var languagePluginLoader = new Promise((resolve, reject) => { let PUBLIC_API = [ 'loadPackage', 'loadedPackages', + 'py', 'pyimport', 'repr', 'runPython', @@ -277,6 +278,8 @@ var languagePluginLoader = new Promise((resolve, reject) => { .then((response) => response.json()) .then((json) => { fixRecursionLimit(window.pyodide); + window.pyodide.py = + window.pyodide.runPython('import sys\nsys.modules["__main__"]'); window.pyodide = makePublicAPI(window.pyodide, PUBLIC_API); window.pyodide._module.packages = json; resolve(); diff --git a/test/test_python.py b/test/test_python.py index ecc74de27..6bcaf764f 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -599,3 +599,14 @@ def test_runpythonasync_exception_after_import(selenium_standalone): assert "ZeroDivisionError" in str(e) else: assert False + + +def test_py(selenium_standalone): + selenium_standalone.run( + """ + def func(): + return 42 + """ + ) + + assert selenium_standalone.run_js('return pyodide.py.func()') == 42