Skip to content

Commit

Permalink
Merge d892129 into a187c1b
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed May 29, 2022
2 parents a187c1b + d892129 commit 99b6dbe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
8 changes: 7 additions & 1 deletion qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,6 @@ def _readline(self, prompt='', callback=None, password=False):
while self._reading:
QtCore.QCoreApplication.processEvents()
return self._get_input_buffer(force=True).rstrip('\n')

else:
self._reading_callback = lambda: \
callback(self._get_input_buffer(force=True).rstrip('\n'))
Expand Down Expand Up @@ -2480,6 +2479,12 @@ def _show_prompt(self, prompt=None, html=False, newline=True,
If set, a separator will be written before the prompt.
"""
self._flush_pending_stream()

# This is necessary to solve out-of-order insertion of mixed stdin and
# stdout stream texts.
# Fixes spyder-ide/spyder#17710
QtCore.QCoreApplication.processEvents()

cursor = self._get_end_cursor()

# Save the current position to support _append*(before_prompt=True).
Expand All @@ -2504,6 +2509,7 @@ def _show_prompt(self, prompt=None, html=False, newline=True,
# Write the prompt.
if separator:
self._append_plain_text(self._prompt_sep)

if prompt is None:
if self._prompt_html is None:
self._append_plain_text(self._prompt)
Expand Down
52 changes: 52 additions & 0 deletions qtconsole/tests/test_00_console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,58 @@ def test_debug(qtconsole, qtbot):
assert control.toPlainText().strip().split()[-1] == "abcd"


@flaky(max_runs=3)
def test_input_and_print(qtconsole, qtbot):
"""
Test that we print correctly mixed input and print statements.
This is a regression test for spyder-ide/spyder#17710.
"""
window = qtconsole.window
shell = window.active_frontend
control = shell._control

def wait_for_input():
qtbot.waitUntil(
lambda: control.toPlainText().splitlines()[-1] == 'Write input: '
)

# Wait until the console is fully up
qtbot.waitUntil(lambda: shell._prompt_html is not None,
timeout=SHELL_TIMEOUT)

# Run a for loop with mixed input and print statements
code = """
user_input = None
while user_input != '':
user_input = input('Write input: ')
print('Input was entered!')
"""
shell.execute(code)
wait_for_input()

# Interact with the 'for' loop for a certain number of repetitions
repetitions = 3
for _ in range(repetitions):
qtbot.keyClicks(control, '1')
qtbot.keyClick(control, QtCore.Qt.Key_Enter)
wait_for_input()

# Get out of the for loop
qtbot.keyClick(control, QtCore.Qt.Key_Enter)
qtbot.waitUntil(lambda: not shell._reading)
qtbot.waitUntil(lambda: shell._prompt_html is not None,
timeout=SHELL_TIMEOUT)

# Assert that printed correctly the expected output in the console.
output = (
" ...: \n" +
"Write input: 1\nInput was entered!\n" * repetitions +
"Write input: \nInput was entered!\n"
)
assert output in control.toPlainText()


@pytest.mark.skipif(no_display, reason="Doesn't work without a display")
class TestConsoleWidget(unittest.TestCase):

Expand Down

0 comments on commit 99b6dbe

Please sign in to comment.