Skip to content

Commit

Permalink
Merge pull request #2343 from Textualize/capture-bugfix
Browse files Browse the repository at this point in the history
Fix interaction between `Console(record=True)` and `Capture` context manager
  • Loading branch information
willmcgugan committed Jun 17, 2022
2 parents 7dea1e7 + 19e518f commit dcc5fa3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
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

0 comments on commit dcc5fa3

Please sign in to comment.