Skip to content

Commit 31ba91f

Browse files
committed
Send only last output to stdout
1 parent 0750f69 commit 31ba91f

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

jupyter_spaces/space.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,16 @@ def execute(self, source):
104104
source (str): Source code.
105105
"""
106106
tree = ast.parse(source=source)
107+
self._execute(body=tree.body[:-1], mode="exec")
108+
self._execute(body=tree.body[-1:], mode="single")
107109

108-
interactive_tree = ast.Interactive(body=tree.body)
110+
def _execute(self, body, mode):
111+
tree_types = {"exec": ast.Module, "single": ast.Interactive}
112+
tree = tree_types[mode](body=body)
109113
if sys.version_info > (3, 8):
110-
interactive_tree.type_ignores = tree.type_ignores
111-
112-
compiled_interactive_tree = compile(
113-
source=interactive_tree, filename="<string>", mode="single"
114-
)
115-
exec(compiled_interactive_tree, self._execution_namespace)
114+
tree.type_ignores = []
115+
code = compile(source=tree, filename="<string>", mode=mode)
116+
exec(code, self._execution_namespace)
116117

117118

118119
class _ChainNamespace(ChainMap, dict):

tests/test_magics.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def test_space_reference_deletions_persist_in_new_magic_call(ip):
6565
ip.run_cell_magic(magic_name="space", line="tomato", cell="x")
6666

6767

68+
def test_space_reference_assignments_persist_in_same_magic_call(ip):
69+
ip.run_cell_magic(magic_name="space", line="tomato", cell="x = 1; y = x + 1")
70+
71+
6872
def test_space_references_assignments_are_confined_in_one_space_only(ip):
6973
ip.run_cell_magic(magic_name="space", line="tomato", cell="x = 99")
7074
ip.run_cell_magic(magic_name="space", line="potato", cell="x = 100")
@@ -151,11 +155,26 @@ def test_get_spaces_reflects_extension_reload(ip):
151155
assert not ip.user_global_ns["spaces"]
152156

153157

154-
def test_space_outputs_to_console(ip, capsys):
158+
def test_last_output_is_sent_to_stdout(ip, capsys):
155159
ip.run_cell_magic(magic_name="space", line="tomato", cell="100")
156160
assert capsys.readouterr().out == "100\n"
157161

158162

159-
def test_space_can_print_to_console(ip, capsys):
163+
def test_only_last_output_is_sent_to_stdout(ip, capsys):
164+
ip.run_cell_magic(magic_name="space", line="tomato", cell="1\n2")
165+
assert capsys.readouterr().out == "2\n"
166+
167+
168+
def test_space_can_print_to_stdout(ip, capsys):
160169
ip.run_cell_magic(magic_name="space", line="tomato", cell="print(100)")
161170
assert capsys.readouterr().out == "100\n"
171+
172+
173+
def test_none_does_not_produce_any_stdout(ip, capsys):
174+
ip.run_cell_magic(magic_name="space", line="tomato", cell="None")
175+
assert capsys.readouterr().out == ""
176+
177+
178+
def test_statement_does_not_produce_any_stdout(ip, capsys):
179+
ip.run_cell_magic(magic_name="space", line="tomato", cell="x = 1")
180+
assert capsys.readouterr().out == ""

tests/test_space.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ def test_space_representation(self):
9595
space.namespace["y"] = 1
9696
assert repr(space) == "Space(name=tomato, size=2)"
9797

98+
def test_last_output_is_sent_to_stdout(self, capsys):
99+
space = Space(name="tomato", outer_space={})
100+
space.execute(source="1")
101+
assert capsys.readouterr().out == "1\n"
102+
103+
def test_only_last_output_is_sent_to_stdout(self, capsys):
104+
space = Space(name="tomato", outer_space={})
105+
space.execute(source="1\n2")
106+
assert capsys.readouterr().out == "2\n"
107+
108+
def test_none_does_not_produce_any_stdout(self, capsys):
109+
space = Space(name="tomato", outer_space={})
110+
space.execute(source="None")
111+
assert capsys.readouterr().out == ""
112+
113+
def test_statement_does_not_produce_any_stdout(self, capsys):
114+
space = Space(name="tomato", outer_space={})
115+
space.execute(source="x = 1")
116+
assert capsys.readouterr().out == ""
117+
118+
def test_use_variable_defined_in_same_source(self):
119+
space = Space(name="tomato", outer_space={})
120+
space.execute(source="x = 1; y = x + 1")
121+
assert space.namespace == {"x": 1, "y": 2}
122+
98123

99124
class TestSpaceRegister:
100125
def test_get_new_space(self):

0 commit comments

Comments
 (0)