Skip to content

Fix mutable default argument in vttests/common.py::sgr_n #19028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mohiuddin-khan-shiam
Copy link

Overview

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.

What’s fixed

  • Replaced the default with seq=None and create a new list when None is passed.
  • Added a clear docstring detailing the function and the rationale for the change.

Why it matters

  • Prevents hidden state leakage between test cases, ensuring reproducible VT escape-sequence output.
  • Guards against subtle bugs that could invalidate rendering tests or complicate debugging.

Impact

No behavior change for existing callers that pass an explicit sequence.
All internal tools and CI VT tests now operate with guaranteed clean state on each call to sgr_n.

[sgr_n](cci:1://file:///d:/Github/terminal/src/tools/vttests/common.py:61:0-73:58) 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.

#### What’s fixed
* Replaced the default with `seq=None` and create a new list when `None` is passed.
* Added a clear docstring detailing the function and the rationale for the change.

#### Why it matters
* Prevents hidden state leakage between test cases, ensuring reproducible VT escape-sequence output.
* Guards against subtle bugs that could invalidate rendering tests or complicate debugging.

#### Impact
No behavior change for existing callers that pass an explicit sequence.
All internal tools and CI VT tests now operate with guaranteed clean state on each call to [sgr_n](cci:1://file:///d:/Github/terminal/src/tools/vttests/common.py:61:0-73:58).
Copy link
Member

@lhecker lhecker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually still need vttests at all anymore? Its tests are very basic...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants