Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix interaction between Console(record=True) and Capture context manager #2343

Merged
merged 6 commits into from Jun 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow exceptions that are raised while a Live is rendered to be displayed and/or processed https://github.com/Textualize/rich/pull/2305
- Fix crashes that can happen with `inspect` when docstrings contain some special control codes https://github.com/Textualize/rich/pull/2294
- Fix edges used in first row of tables when `show_header=False` https://github.com/Textualize/rich/pull/2330
- Fix interaction between `Capture` contexts and `Console(record=True)` https://github.com/Textualize/rich/pull/2343
- Fixed hash issue in Styles class https://github.com/Textualize/rich/pull/2346

## [12.4.4] - 2022-05-24
Expand Down
8 changes: 4 additions & 4 deletions rich/console.py
Expand Up @@ -1959,11 +1959,11 @@ def _check_buffer(self) -> None:
del self._buffer[:]
return
with self._lock:
if self._buffer_index == 0:
if self.record:
with self._record_buffer_lock:
self._record_buffer.extend(self._buffer[:])

if self.record:
with self._record_buffer_lock:
self._record_buffer.extend(self._buffer[:])
if self._buffer_index == 0:

if self.is_jupyter: # pragma: no cover
from .jupyter import display
Expand Down
17 changes: 17 additions & 0 deletions tests/test_console.py
Expand Up @@ -351,6 +351,23 @@ def test_capture():
assert capture.get() == "Hello\n"


def test_capture_and_record(capsys):
recorder = Console(record=True)
recorder.print("ABC")

with recorder.capture() as capture:
recorder.print("Hello")

assert capture.get() == "Hello\n"

recorded_text = recorder.export_text()
out, err = capsys.readouterr()

assert recorded_text == "ABC\nHello\n"
assert capture.get() == "Hello\n"
assert out == "ABC\n"


def test_input(monkeypatch, capsys):
def fake_input(prompt=""):
console.file.write(prompt)
Expand Down