Skip to content

fix infinite loop in append #3480

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

Merged
merged 2 commits into from
Sep 6, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Fixed infinite loop when appending Text to same instance https://github.com/Textualize/rich/pull/3480

## [13.8.0] - 2024-08-26

Expand Down
4 changes: 2 additions & 2 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ def append(
self._text.append(text.plain)
self._spans.extend(
_Span(start + text_length, end + text_length, style)
for start, end, style in text._spans
for start, end, style in text._spans.copy()
)
self._length += len(text)
return self
Expand All @@ -1020,7 +1020,7 @@ def append_text(self, text: "Text") -> "Text":
self._text.append(text.plain)
self._spans.extend(
_Span(start + text_length, end + text_length, style)
for start, end, style in text._spans
for start, end, style in text._spans.copy()
)
self._length += len(text)
return self
Expand Down
11 changes: 11 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,14 @@ def test_append_tokens() -> None:
output = capture.get()
print(repr(output))
assert output == "long text that will be wrapped with a \ncontrol code \n\n"


def test_append_loop_regression() -> None:
"""Regression text for https://github.com/Textualize/rich/issues/3479"""
a = Text("one", "blue")
a.append(a)
assert a.plain == "oneone"

b = Text("two", "blue")
b.append_text(b)
assert b.plain == "twotwo"
Loading