Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pixelmap/gui/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from __future__ import annotations

import logging
import math
import os
import sys
import threading
Expand All @@ -45,14 +46,24 @@


def _env_float(name: str, default: float) -> float:
"""Read a threshold from the environment; blank or unset means ``default``.

Finite zero and negatives are returned as-is (callers treat them as "off").
``nan`` and ``±inf`` parse as floats but aren't usable thresholds -- NaN
breaks every comparison, and infinity silently disables the check -- so they
are rejected like any other non-number.
"""
raw = os.environ.get(name, "").strip()
if not raw:
return default
try:
return float(raw)
value = float(raw)
except ValueError:
log.warning("[watchdog] ignoring %s=%r: not a number, using %s", name, raw, default)
value = math.nan
if not math.isfinite(value):
log.warning("[watchdog] ignoring %s=%r: not a finite number, using %s", name, raw, default)
return default
return value


def _rss_mb() -> float:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,37 @@ def test_env_float(monkeypatch, raw, expected):
assert watchdog._env_float("PIXELMAP_TEST_KNOB", 7.0) == expected


@pytest.mark.parametrize(
"raw, expected",
# Finite zero and negatives stay accepted: they deliberately switch a check off.
[(" 12 ", 12.0), ("-1", -1.0), ("-0", 0.0), ("1e3", 1000.0)],
)
def test_env_float_accepts_finite_values(monkeypatch, raw, expected):
monkeypatch.setenv("PIXELMAP_TEST_KNOB", raw)
assert watchdog._env_float("PIXELMAP_TEST_KNOB", 7.0) == expected


@pytest.mark.parametrize("raw", ["nan", "NaN", "inf", "-inf", "Infinity", " inf "])
def test_env_float_rejects_non_finite_values(monkeypatch, caplog, raw):
"""``float()`` parses these, but none is a usable threshold: NaN makes every
stall comparison misbehave, and ±inf silently disables a check."""
monkeypatch.setenv("PIXELMAP_TEST_KNOB", raw)
with caplog.at_level(logging.WARNING, logger=watchdog.log.name):
assert watchdog._env_float("PIXELMAP_TEST_KNOB", 7.0) == 7.0
assert "PIXELMAP_TEST_KNOB" in caplog.text


@pytest.mark.parametrize("raw", [None, "", " "])
def test_env_float_unset_or_blank_is_silent(monkeypatch, caplog, raw):
if raw is None:
monkeypatch.delenv("PIXELMAP_TEST_KNOB", raising=False)
else:
monkeypatch.setenv("PIXELMAP_TEST_KNOB", raw)
with caplog.at_level(logging.WARNING, logger=watchdog.log.name):
assert watchdog._env_float("PIXELMAP_TEST_KNOB", 7.0) == 7.0
assert caplog.text == ""


# --------------------------------------------------------------------------
# install()
# --------------------------------------------------------------------------
Expand Down
Loading