From 3680e13bc0bbd0a68b69858623f738f1d8a6ead4 Mon Sep 17 00:00:00 2001 From: "S. M. Mohiuddin Khan Shiam" <147746955+mohiuddin-khan-shiam@users.noreply.github.com> Date: Wed, 25 Jun 2025 01:19:22 +0600 Subject: [PATCH] hygiene: fix mutable default argument in `vttests/common.py::sgr_n` (#19028) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sgr_n previously used a mutable list (`seq=[]`) as its default parameter. In Python, default arguments are instantiated once at function‐definition time and then shared across all calls. If any caller mutated that list (e.g., `seq.append(...)`), later invocations would inherit the mutated state, producing unpredictable or corrupted VT-test sequences. --- src/tools/vttests/common.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/tools/vttests/common.py b/src/tools/vttests/common.py index 2ecd854145..b08d23618b 100644 --- a/src/tools/vttests/common.py +++ b/src/tools/vttests/common.py @@ -59,7 +59,18 @@ def clear_all(): def sgr(code=0): csi('{}m'.format(code)) -def sgr_n(seq=[]): +def sgr_n(seq=None): + """Apply multiple SGR (Select Graphic Rendition) parameters. + + Parameters + ---------- + seq : Iterable of int | None + Sequence of numeric SGR codes to emit. If ``None`` (default) an empty + sequence is assumed. A mutable default argument must be avoided to + prevent unwanted state sharing between calls. + """ + if seq is None: + seq = [] csi('{}m'.format(';'.join(str(code) for code in seq))) def tbc():