Add first version of pyodide.py

This commit is contained in:
Michael Droettboom 2018-05-09 14:33:52 -04:00
parent d385b7c9f1
commit aa433aaf81
4 changed files with 27 additions and 0 deletions

View File

@ -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`; \

17
src/pyodide.py Normal file
View File

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

1
test/data.txt Normal file
View File

@ -0,0 +1 @@
HELLO

View File

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