Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion IPython/core/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ class HistoryOutput:
output_type: typing.Literal[
"out_stream", "err_stream", "display_data", "execute_result"
]
bundle: typing.Dict[str, str]
bundle: typing.Dict[str, str | list[str]]


class HistoryManager(HistoryAccessor):
Expand Down
4 changes: 2 additions & 2 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3063,11 +3063,11 @@ def write(data, *args, **kwargs):
output_stream = outputs[-1]
if output_stream is None:
output_stream = HistoryOutput(
output_type=output_type, bundle={"stream": ""}
output_type=output_type, bundle={"stream": []}
)
outputs_by_counter[execution_count].append(output_stream)

output_stream.bundle["stream"] += data # Append to existing stream
output_stream.bundle["stream"].append(data) # Append to existing stream
return result

stream.write = write
Expand Down
6 changes: 4 additions & 2 deletions IPython/core/magics/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,11 @@ def notebook(self, s):
for output in outputs[execution_count]:
for mime_type, data in output.bundle.items():
if output.output_type == "out_stream":
cell.outputs.append(v4.new_output("stream", text=[data]))
text = data if isinstance(data, list) else [data]
cell.outputs.append(v4.new_output("stream", text=text))
elif output.output_type == "err_stream":
err_output = v4.new_output("stream", text=[data])
text = data if isinstance(data, list) else [data]
err_output = v4.new_output("stream", text=text)
err_output.name = "stderr"
cell.outputs.append(err_output)
elif output.output_type == "execute_result":
Expand Down
10 changes: 10 additions & 0 deletions tests/test_interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import shutil
import sys
import tempfile
import time
import unittest
import pytest
from unittest import mock
Expand Down Expand Up @@ -84,6 +85,15 @@ def test_run_cell_multiline(self):
self.assertEqual(res.success, True)
self.assertEqual(res.result, None)

def test_stream_performance(self):
"""It should be fast to execute."""
src = "for i in range(250_000): print(i)"
start = time.perf_counter()
ip.run_cell(src)
end = time.perf_counter()
duration = end - start
assert duration < 10

def test_multiline_string_cells(self):
"Code sprinkled with multiline strings should execute (GH-306)"
ip.run_cell("tmp=0")
Expand Down
Loading