Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
gh-158540: Resolve symlinks when adding the script directory to sys.path
``python script.py`` resolves symlinks when computing ``sys.path[0]``, so
a script reached through a symlink (``link.py -> sub/where.py``) imports
modules from the real script's directory.  Apply ``os.path.realpath()``
before taking the directory name, and make sure the result is placed at
the front of ``sys.path`` even if it was already listed.

Add a regression test for a symlinked script.
  • Loading branch information
hetaozdh committed Oct 1, 2026
commit 264a36c03cc21ad5188096cc881db62a2fc6d89c
8 changes: 4 additions & 4 deletions Lib/profiling/sampling/_sync_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ def _execute_script(script_path: str, script_args: List[str], cwd: str) -> None:
if not os.path.isfile(script_path):
raise TargetError(f"Script not found: {script_path}")

# Put directory containing the script (not the current working directory) on sys.path so that modules next to it can be imported. Similar to ``python script.py``
script_dir = os.path.dirname(script_path)
if script_dir not in sys.path:
sys.path.insert(0, script_dir)
script_dir = os.path.dirname(os.path.realpath(script_path))
if script_dir in sys.path:
sys.path.remove(script_dir)
sys.path.insert(0, script_dir)

# Replace sys.argv to match original script call
sys.argv = [script_path] + script_args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import tempfile
import unittest

from test.support import SHORT_TIMEOUT, requires_subprocess
from test.support import SHORT_TIMEOUT, os_helper, requires_subprocess


@requires_subprocess()
Expand Down Expand Up @@ -79,6 +79,35 @@ def test_script_directory_on_sys_path(self):
self.assertIn("HELPER: helper imported", stdout)
self.assertIn(f"PATH0: {os.path.realpath(script_dir)}", stdout)

@os_helper.skip_unless_symlink
def test_symlinked_script_uses_real_directory(self):
# gh-158540: ``python script.py`` resolves symlinks when computing
# sys.path[0], so a symlinked script must import modules next to the
# real script, not next to the link.
with tempfile.TemporaryDirectory() as tmpdir:
script_dir = os.path.join(tmpdir, "sub")
os.mkdir(script_dir)
with open(os.path.join(script_dir, "helper.py"), "w") as f:
f.write("message = 'helper imported'\n")
with open(os.path.join(script_dir, "where.py"), "w") as f:
f.write(
"import os\n"
"import sys\n"
"print('PATH0:', os.path.realpath(sys.path[0]))\n"
"import helper\n"
"print('HELPER:', helper.message)\n"
)
os.symlink(
os.path.join("sub", "where.py"),
os.path.join(tmpdir, "link.py"),
)

stdout, stderr = self.run_coordinator(tmpdir, "link.py")

self.assertNotIn("ModuleNotFoundError", stderr)
self.assertIn("HELPER: helper imported", stdout)
self.assertIn(f"PATH0: {os.path.realpath(script_dir)}", stdout)


if __name__ == "__main__":
unittest.main()