diff --git a/Makefile b/Makefile index dfaa1e26d..0e5e18dca 100644 --- a/Makefile +++ b/Makefile @@ -144,6 +144,7 @@ root/.built: \ src/lazy_import.py \ src/sitecustomize.py \ src/webbrowser.py \ + src/pyodide.py \ remove_modules.txt rm -rf root mkdir -p root/lib @@ -154,6 +155,7 @@ root/.built: \ cp src/webbrowser.py root/lib/python$(PYMINOR) cp src/_testcapi.py root/lib/python$(PYMINOR) cp src/pystone.py root/lib/python$(PYMINOR) + cp src/pyodide.py root/lib/python$(PYMINOR)/site-packages ( \ cd root/lib/python$(PYMINOR); \ rm -fr `cat ../../../remove_modules.txt`; \ diff --git a/src/pyodide.py b/src/pyodide.py new file mode 100644 index 000000000..e361c3128 --- /dev/null +++ b/src/pyodide.py @@ -0,0 +1,17 @@ +# A library of helper utilities for connecting Python to the browser environment + +from js import XMLHttpRequest + +import io + +def open_url(url): + """ + Fetches a given *url* and returns a io.StringIO to access its contents. + """ + req = XMLHttpRequest.new() + req.open('GET', url, False) + req.send(None) + return io.StringIO(req.response) + + +__all__ = ['open_url'] diff --git a/test/data.txt b/test/data.txt new file mode 100644 index 000000000..e427984d4 --- /dev/null +++ b/test/data.txt @@ -0,0 +1 @@ +HELLO diff --git a/test/test_python.py b/test/test_python.py index afb52f426..dd2752763 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -48,6 +48,13 @@ def test_py_proxy(selenium): assert selenium.run("hasattr(f, 'baz')") == False + +def test_open_url(selenium): + assert selenium.run( + "import pyodide\n" + "pyodide.open_url('../test/data.txt').read()\n") == 'HELLO\n' + + def test_run_core_python_test(python_test, selenium): selenium.run( "import sys\n"