Skip to content

Commit 22c2cff

Browse files
authored
Merge pull request #3480 from Textualize/fix-infinite-append
fix infinite loop in append
2 parents 9ec4191 + f44e8bd commit 22c2cff

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
### Fixed
11+
12+
- Fixed infinite loop when appending Text to same instance https://github.com/Textualize/rich/pull/3480
813

914
## [13.8.0] - 2024-08-26
1015

rich/text.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ def append(
998998
self._text.append(text.plain)
999999
self._spans.extend(
10001000
_Span(start + text_length, end + text_length, style)
1001-
for start, end, style in text._spans
1001+
for start, end, style in text._spans.copy()
10021002
)
10031003
self._length += len(text)
10041004
return self
@@ -1020,7 +1020,7 @@ def append_text(self, text: "Text") -> "Text":
10201020
self._text.append(text.plain)
10211021
self._spans.extend(
10221022
_Span(start + text_length, end + text_length, style)
1023-
for start, end, style in text._spans
1023+
for start, end, style in text._spans.copy()
10241024
)
10251025
self._length += len(text)
10261026
return self

tests/test_text.py

+11
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,14 @@ def test_append_tokens() -> None:
10011001
output = capture.get()
10021002
print(repr(output))
10031003
assert output == "long text that will be wrapped with a \ncontrol code \n\n"
1004+
1005+
1006+
def test_append_loop_regression() -> None:
1007+
"""Regression text for https://github.com/Textualize/rich/issues/3479"""
1008+
a = Text("one", "blue")
1009+
a.append(a)
1010+
assert a.plain == "oneone"
1011+
1012+
b = Text("two", "blue")
1013+
b.append_text(b)
1014+
assert b.plain == "twotwo"

0 commit comments

Comments
 (0)