Commit Graph

6 Commits

Author SHA1 Message Date
Igor Lins e Silva 2cab6f5a5b style: reformat tree with ruff 0.15.9
One-time mechanical reformat so `ruff format --check .` passes under the
newly pinned ruff. Layout only (assert-message parenthesization etc.),
no behavior change. 29 files: 28 under tests/ + 1 tools helper, no core
mempalace/ modules. Produced by `ruff format .`.
2026-05-17 23:20:01 -03:00
mvalentsev 4b1ab50750 fix(init): keep PYTHONPATH env for embedded callers, strip only in CLI entry points
Split the leaked-PYTHONPATH workaround: __init__.py now only filters
sys.path (fixes the ABI crash from #1423 at import time), while
os.environ.pop("PYTHONPATH") moves to cli:main() and mcp_server:main()
where the strip affects only subprocesses we spawn ourselves.

Host applications that import mempalace as a library and rely on
PYTHONPATH for their own child subprocesses are no longer affected.

Refs #1423.
2026-05-13 22:53:36 +05:00
mvalentsev d09cd9dab1 fix(init): preserve cwd marker; decouple test (#1423)
When PYTHONPATH is `.`, both the leaked entry and the implicit
empty-string CWD marker on sys.path normalize to `.`. The previous
filter would remove the CWD marker too, silently changing import
resolution. The sys.path filter now preserves any empty-string entry
unconditionally.

Also drop the asymmetric `.strip()` on PYTHONPATH tokens. CPython
does not strip whitespace from PYTHONPATH before populating sys.path,
so stripping on one side only could let whitespace-padded leaks
through.

Test extended with a `dot` parametrize case and a dedicated
test_init_preserves_cwd_marker_when_pythonpath_collides. Test child
code now asserts on the sentinel-prefix substring directly instead of
re-implementing the production normalization, so future changes to
_norm cannot silently mask test regressions.
2026-05-13 22:53:36 +05:00
mvalentsev c4fdc1ed8c fix(init): normalize path comparison for sys.path strip (#1423)
Previous commit compared sys.path entries to PYTHONPATH tokens by
byte-exact string equality, which let leaks slip through on Windows
(case-insensitive paths) and across trailing-separator or whitespace
quirks (a leading or trailing space in PYTHONPATH would not match the
trimmed sys.path entry the interpreter actually populated).

Both sides of the comparison now run through os.path.normcase +
os.path.normpath so case differences and trailing separators no longer
defeat the filter, and PYTHONPATH tokens are trimmed before the
match.

Test extended to 6 parametrized cases (single, multi-path, trailing
separator, leading pathsep, empty, unset) with explicit ids and
stderr included in assertion diagnostics. Synthetic leak placeholder
replaces /tmp so the test is portable across CI runners.
2026-05-13 22:53:36 +05:00
mvalentsev 765478d37a fix(init): also strip leaked sys.path entries (#1423)
Popping PYTHONPATH from os.environ alone is insufficient: the interpreter
populates sys.path from PYTHONPATH at startup, before package init runs.
Transitive imports of compiled extensions (pydantic_core,
chromadb_rust_bindings) would still resolve from the leaked path even
after the env var is cleared.

Now also remove the matching entries from sys.path so the import search
path no longer contains the wrong Python's site-packages.

Test extended with a multi-path PYTHONPATH case and assertions on both
os.environ and sys.path after import.
2026-05-13 22:53:36 +05:00
mvalentsev af7b6dfebc fix: drop leaked PYTHONPATH on package init (#1423)
Venvs do not strip PYTHONPATH from inheritance. On multi-Python systems
where PYTHONPATH points at a different Python's site-packages, transitive
imports of compiled extensions (pydantic_core, chromadb_rust_bindings)
load the wrong ABI and crash with ModuleNotFoundError on
pydantic_core._pydantic_core.

Place the pop in mempalace/__init__.py (instead of cli.py as the reporter
proposed) so all entry points are covered: the mempalace shell command,
python -m mempalace, and import mempalace from a library.

Tests: tests/test_init.py runs the strip in a subprocess, parametrized
over set / unset PYTHONPATH.

Closes #1423.
2026-05-13 22:53:36 +05:00