Skip to content

Commit

Permalink
Merge pull request #3480 from Textualize/fix-infinite-append
Browse files Browse the repository at this point in the history
fix infinite loop in append
  • Loading branch information
willmcgugan committed Sep 6, 2024
2 parents 9ec4191 + f44e8bd commit 22c2cff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
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"

0 comments on commit 22c2cff

Please sign in to comment.