Skip to content

Commit

Permalink
fix: Prevent __pypackages__ leaking into a PDM subcommand
Browse files Browse the repository at this point in the history
For example when running `pdm multirun pdm install`.
  • Loading branch information
pawamoy committed Oct 18, 2023
1 parent 89412e4 commit f192795
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/pdm_multirun/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING
import sys
from contextlib import contextmanager
from typing import TYPE_CHECKING, Iterator

from pdm import termui
from pdm.cli.commands.run import Command as RunCommand
Expand All @@ -24,6 +26,27 @@ def _comma_separated_list(value: str) -> list[str]:
return value.split(",")


@contextmanager
def _clean_sys_path(project: Project, options: argparse.Namespace) -> Iterator[None]:
if options.script != "pdm":
yield
return
old_syspath = sys.path
clean = [path for path in old_syspath if "__pypackages__" not in path]
if clean == old_syspath:
yield
return
project.core.ui.echo(
"Temporarily removed `__pypackages__` from `sys.path`",
verbosity=termui.Verbosity.DETAIL,
)
sys.path = clean
try:
yield
finally:
sys.path = old_syspath


class MultirunCommand(RunCommand):
"""Run a command under multiple Python versions."""

Expand Down Expand Up @@ -67,7 +90,8 @@ def handle(self, project: Project, options: argparse.Namespace) -> None: # noqa
project.core.ui.echo(f"Skipped interpreter/venv: {selected}", verbosity=termui.Verbosity.DETAIL)
continue
try:
super().handle(project, options)
with _clean_sys_path(project, options):
super().handle(project, options)
except SystemExit as exit:
if exit.code:
self._use(project, options, old_python)
Expand Down

0 comments on commit f192795

Please sign in to comment.