From 588ac8ddf039d556c323656acb82d0688d2632b8 Mon Sep 17 00:00:00 2001 From: ghangz <152254226+ghangz@users.noreply.github.com> Date: Wed, 1 Jul 2026 11:43:13 +0800 Subject: [PATCH 1/2] Accept common false values for profiler env --- mcoplib/profiler.py | 4 ++-- unit_test/test_profiler_env.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 unit_test/test_profiler_env.py diff --git a/mcoplib/profiler.py b/mcoplib/profiler.py index adfba41..c10d000 100644 --- a/mcoplib/profiler.py +++ b/mcoplib/profiler.py @@ -6,11 +6,11 @@ def _is_profiler_enabled() -> bool: """ - Env switch: PROFILER_ENABLED==0 -> disabled, else enabled. + Env switch: PROFILER_ENABLED=false/0/off/no -> disabled, else enabled. """ v = os.getenv("PROFILER_ENABLED", "1") try: - return not (str(v).strip() == "0") + return str(v).strip().lower() not in {"0", "false", "off", "no"} except Exception: return True diff --git a/unit_test/test_profiler_env.py b/unit_test/test_profiler_env.py new file mode 100644 index 0000000..343bf4a --- /dev/null +++ b/unit_test/test_profiler_env.py @@ -0,0 +1,12 @@ +from mcoplib.profiler import _is_profiler_enabled + + +def test_profiler_disabled_by_common_false_values(monkeypatch): + for value in ("0", "false", "False", "off", "NO"): + monkeypatch.setenv("PROFILER_ENABLED", value) + assert not _is_profiler_enabled() + + +def test_profiler_enabled_by_default(monkeypatch): + monkeypatch.delenv("PROFILER_ENABLED", raising=False) + assert _is_profiler_enabled() From 89a34126a7e566ff9b06598068c050f483eeba65 Mon Sep 17 00:00:00 2001 From: ghangz <152254226+ghangz@users.noreply.github.com> Date: Wed, 1 Jul 2026 15:45:30 +0800 Subject: [PATCH 2/2] Simplify profiler boolean environment parsing --- mcoplib/profiler.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mcoplib/profiler.py b/mcoplib/profiler.py index c10d000..8b319aa 100644 --- a/mcoplib/profiler.py +++ b/mcoplib/profiler.py @@ -9,10 +9,7 @@ def _is_profiler_enabled() -> bool: Env switch: PROFILER_ENABLED=false/0/off/no -> disabled, else enabled. """ v = os.getenv("PROFILER_ENABLED", "1") - try: - return str(v).strip().lower() not in {"0", "false", "off", "no"} - except Exception: - return True + return v.strip().lower() not in {"0", "false", "off", "no"} def _timestamp() -> str: