From f2ee29531bd01354a5acf26f7c98dfadf70e6ab1 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 6 Sep 2024 10:32:01 +0100 Subject: [PATCH] fix infinite loop in append --- CHANGELOG.md | 5 +++++ rich/text.py | 4 ++-- tests/test_text.py | 11 +++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e14bd69a6..3fd65c515 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ## [13.8.0] - 2024-08-26 diff --git a/rich/text.py b/rich/text.py index 69e19b645..1f5353364 100644 --- a/rich/text.py +++ b/rich/text.py @@ -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 @@ -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 diff --git a/tests/test_text.py b/tests/test_text.py index 704462b45..1a22e79d2 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -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"