Re: [PATCH v2] kconfig: tests: avoid mutating extra_env dicts in Conf

From: Nicolas Schier

Date: Fri Dec 04 2026 - 12:22:22 EST


On Fri, Dec 04, 2026 at 06:26:18PM +0800, Pavee Zheng via B4 Relay wrote:
> From: Pavee Zheng <pavee.zheng@xxxxxxxxx>
>
> Ruff's B006 (mutable-argument-default) lint flags using mutable data
> structures such as {} as default argument values because Python
> evaluates default arguments once at function definition time and shares
> the same object across all calls, so in-place mutations persist across
> subsequent calls.
>
> Conf._run_conf() and Conf._allconfig() both declare extra_env={} and
> mutate extra_env in place:
>
> - Conf._allconfig() assigns extra_env['KCONFIG_ALLCONFIG'] when
> all_config is set. No existing test currently passes a non-None
> all_config to allyesconfig(), allmodconfig(), allnoconfig(), or
> alldefconfig(), so this does not trigger a failure today, but a
> future caller doing so would leave a stale KCONFIG_ALLCONFIG entry
> in the shared default dictionary for subsequent calls in the same
> pytest session.
> - Conf._run_conf() assigns extra_env['srctree'], mutating both its
> default dictionary and caller-owned dictionaries passed via
> extra_env (such as warn_changed_input in
> scripts/kconfig/tests/warn_changed_input/__init__.py).
>
> As a defensive cleanup and readability improvement, default extra_env
> to None in both methods. In _run_conf(), merge extra_env into the local
> env dictionary and set env['srctree'] there instead of mutating
> extra_env. In _allconfig(), build a fresh local dictionary copy before
> inserting KCONFIG_ALLCONFIG.
>
> Signed-off-by: Pavee Zheng <pavee.zheng@xxxxxxxxx>
> ---
> Found with ruff (B006) and prepared with AI coding assistant help
> (Antigravity). Verified on top of kbuild/linux.git (kbuild-for-next,
> 1b9279a567d0) with ruff 0.17.6 ("ruff check --select B006,F,E,W
> scripts/kconfig/tests/conftest.py", 2 B006 diagnostics before and 0
> after), scripts/checkpatch.pl --strict, and in a Linux container
> (Python 3.11.2, pytest 7.2.1) with "make O=/tmp/build testconfig"
> (35 passed) plus an end-to-end regression check against the compiled
> scripts/kconfig/conf binary confirming no KCONFIG_ALLCONFIG leak across
> allnoconfig() calls and no in-place mutation of caller-owned extra_env
> dictionaries in _run_conf() or _allconfig().
>
> Changes in v2:
> - Drop both Fixes: tags since no existing test triggers the all_config
> leak, as pointed out by Julian Braha.
> - Reframe the commit message around Ruff lint B006 and readability /
> defensive cleanup.
> - Move AI assistance and verification details below the '---' cut line.
> - Rebase onto kbuild/linux.git (kbuild-for-next, 1b9279a567d0) and trim
> stale Fixes-derived recipients.
> - Link to v1: https://patch.msgid.link/20261203-patch-kconfig-tests-extra-env-v1-1-e48cf80d6d59@xxxxxxxxx
> ---
> scripts/kconfig/tests/conftest.py | 16 ++++++++++------
> 1 file changed, 10 insertions(+), 6 deletions(-)
>

Thanks!

Reviewed-by: Nicolas Schier <n.schier@xxxxxxxxx>

--
Nicolas