From 1463c020bf2d478a8d456451aad934b15429766c Mon Sep 17 00:00:00 2001 From: patchwright Date: Wed, 17 Jun 2026 23:19:15 +0200 Subject: [PATCH] fix: don't call winterm for OSC title codes when not converting (#407) --- colorama/ansitowin32.py | 3 ++- colorama/tests/ansitowin32_test.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index 7966fbc..4da35f6 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -269,7 +269,8 @@ def convert_osc(self, text): # 1 - change icon (we don't support this) # 2 - change title if params[0] in '02': - winterm.set_title(params[1]) + if self.convert and winterm: + winterm.set_title(params[1]) return text diff --git a/colorama/tests/ansitowin32_test.py b/colorama/tests/ansitowin32_test.py index 8ad0247..bf480d9 100644 --- a/colorama/tests/ansitowin32_test.py +++ b/colorama/tests/ansitowin32_test.py @@ -237,6 +237,15 @@ def test_osc_codes(self): stream.write(code) self.assertEqual(winterm.set_title.call_count, 2) + def test_osc_codes_not_converted_when_stripping(self): + # Issue #407: with strip=True and convert=False (e.g. no PTY on Linux), + # OSC title sequences must be stripped without touching winterm, which + # is None on non-Windows platforms. Calling it would raise AttributeError. + mockStdout = Mock() + with patch('colorama.ansitowin32.winterm', None): + stream = AnsiToWin32(mockStdout, strip=True, convert=False) + stream.write('\033]2;colorama_test_title\x07') # must not raise + def test_native_windows_ansi(self): with ExitStack() as stack: def p(a, b):