From f2ee29531bd01354a5acf26f7c98dfadf70e6ab1 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 6 Sep 2024 10:32:01 +0100 Subject: [PATCH 1/2] 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 e14bd69a..3fd65c51 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 69e19b64..1f535336 100644 --- a/rich/text.py +++ b/rich/text.py @@ -998,7 +998,7 @@ class Text(JupyterMixin): 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 @@ class Text(JupyterMixin): 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 704462b4..1a22e79d 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" From f44e8bd743aaea1301899c8b0ca1e6c6dd456c70 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 6 Sep 2024 10:32:40 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fd65c51..63116cf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed infinite loop when appending Text to same instance +- Fixed infinite loop when appending Text to same instance https://github.com/Textualize/rich/pull/3480 ## [13.8.0] - 2024-08-26