Commit 1d764555 authored by Romain Casati's avatar Romain Casati

Adding a more method for easy ps1/ps2 setting + a link to the REPL banner.

parent b6dc06d5
Pipeline #386180 failed with stage
in 1 minute and 58 seconds
......@@ -303,6 +303,21 @@
}
};
/**
* Is the source ready to be evaluated or want we more?
* Usefull to set ps1/ps2 for teminal prompt.
*/
that.more = function (source) {
return pyodide.globals.basthon.kernel.more(source);
};
/**
* Mimic the CPython's REPL banner.
*/
that.banner = function () {
return pyodide.globals.basthon.kernel.banner();
};
/**
* Complete a code at the end (usefull for tab completion).
*
......
......@@ -62,6 +62,21 @@ class InteractiveConsole(_pyodide_console.InteractiveConsole):
self.stop()
self.start()
def more(self, source):
""" Is the source ready to be evaluated or want we more?
Usefull to set ps1/ps2 for teminal prompt.
"""
try:
code = self.compile(source, self.filename, "exec")
except (OverflowError, SyntaxError, ValueError):
return False
if code is None:
return True
return False
def showtraceback(self):
""" Remove one more traceback level. """
ei = sys.exc_info()
......
......@@ -132,6 +132,6 @@ def execution_count():
return _console.execution_count
eval = _console.eval
complete = _console.complete
# copying methods from _console to this module
for f in ('eval', 'complete', 'banner', 'more'):
globals()[f] = getattr(_console, f)
......@@ -86,3 +86,22 @@ def test_import_not_hacked(selenium):
# 0.16.1 shows numpy's warnings (Python 3.8)
if selenium.run_js("return pyodide.version()") != "0.16.1":
assert data['stderr'] == ""
def test_banner(selenium):
banner = selenium.run_js("return Basthon.banner();")
assert banner.startswith("Python 3.")
assert 'Type "help", "copyright", "credits" or "license" for more information' in banner
def test_more(selenium):
tests = [
("for i in range(10)", False),
("for i in range(10):", True),
("for i in range(10):\\n print(i)", False),
("def f(x)", False),
("def f(x):", True),
("def f(x):\\n return 2 * x + 1", False),
]
for t, v in tests:
assert selenium.run_js(f"""return Basthon.more("{t}");""") == v
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment