Commit 2d8334d0 authored by Romain Casati's avatar Romain Casati

The console uses __main__.__dict__ as locals.

parent 84ed860a
......@@ -15,6 +15,8 @@ class InteractiveConsole(_pyodide_console.InteractiveConsole):
""" This is the Python's part of Basthon kernel """
def __init__(self, *args, **kwargs):
self.execution_count = None
if 'locals' not in kwargs:
kwargs['locals'] = sys.modules['__main__'].__dict__
super().__init__(*args, **kwargs)
self.runner = _pyodide_base.CodeRunner(globals=self.locals,
filename="<input>")
......@@ -41,7 +43,7 @@ class InteractiveConsole(_pyodide_console.InteractiveConsole):
self.execution_count = 0
self.locals.clear()
self.locals.update({
'__name__': '__console__',
'__name__': '__main__',
'__doc__': None,
'_': '',
'__': '',
......
......@@ -8,6 +8,11 @@ def test_setup(selenium):
assert selenium.run("1 + 1") == 2
def test_globals(selenium):
data = selenium.run_basthon("import __main__; set(dir(__main__)) == set(['In', 'Out', '_', '__', '___', '__builtins__', '__doc__', '__eval_data__', '__main__', '__name__'])")
assert data['result']['result']['text/plain'] == "True"
def test_result(selenium):
data = selenium.run_basthon("1 + 1")['result']
assert data['result']['text/plain'] == '2' and data['execution_count'] == 1
......
def test_globals(selenium):
# this ensure ou __main__ is properly connected to globals()
# since doctest.testmod will look in __main__
selenium.run_basthon("""
import doctest
def my_test(x):
'''
>>> my_test(2)
4
>>> my_test(4)
15
'''
return x ** 2
""")
data = selenium.run_basthon("str(doctest.testmod(verbose=False))")
assert data['result']['result']['text/plain'] == "'TestResults(failed=1, attempted=2)'"
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