Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failures with long Python interpreter paths #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Fix failures with long Python interpreter paths
When `sys.executable` is long, it may exceed the kernel limits for shebangs, which results in `OSError: [Errno 8] Exec format error: 'foo'` when launching a tool injected into `PATH.

This is fixed by symlinking the Python interpreter to a short path and emitting that into the shebang.
  • Loading branch information
fmeum committed Jun 7, 2024
commit 7f0854cbf60101895be2e1de1872f1de5b8f5c51
6 changes: 5 additions & 1 deletion testpath/commands.py
Original file line number Diff line number Diff line change
@@ -74,8 +74,12 @@ def __init__(self, name, content=None, python=''):
self.command_dir = tempfile.mkdtemp()

if content is None:
# Shebangs have a limited length, so symlink the Python executable
# to a short name that can't conflict with the real command.
tmp_python = os.path.join(self.command_dir, 'python-' + name)
os.symlink(sys.executable, tmp_python)
content = _record_run.format(
python=sys.executable, recording_file=self.recording_file,
python=tmp_python, recording_file=self.recording_file,
extra_code=python,
)
elif python:
23 changes: 23 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
from subprocess import call, check_output
import unittest
import shutil
import sys
import tempfile

from testpath.commands import *

@@ -35,6 +38,26 @@ def test_assert_calls_twice(self):
with assert_calls('git'):
pass

def test_assert_calls_long_python_path(self):
current_python = sys.executable
tmp = tempfile.mkdtemp()
try:
tmp_python_dir = tmp
for _ in range(10):
tmp_python_dir = os.path.join(tmp_python_dir, 'looooooooooooooooooooooooooooooooooooooooooong_segment')
os.makedirs(tmp_python_dir)
tmp_python = os.path.join(tmp_python_dir, 'python')
os.symlink(sys.executable, tmp_python)
sys.executable = tmp_python

with assert_calls('i_dont_exist'):
call(['i_dont_exist'])
with assert_calls('git'):
call(['git'])
finally:
sys.executable = current_python
shutil.rmtree(tmp, ignore_errors=True)

def test_fixed_output():
t = 'Sat 24 Apr 17:11:58 BST 2021\n'
with MockCommand.fixed_output('date', t) as mock_date: