Skip to content

Commit

Permalink
Add handling for cross-compilation environments. (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 authored Oct 7, 2024
1 parent cbd4ae1 commit a16423f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions distlib/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ def _build_shebang(self, executable, post_interp):
"""
if os.name != 'posix':
simple_shebang = True
elif getattr(sys, "cross_compiling", False):
# In a cross-compiling environment, the shebang will likely be a
# script; this *must* be invoked with the "safe" version of the
# shebang, or else using os.exec() to run the entry script will
# fail, raising "OSError 8 [Errno 8] Exec format error".
simple_shebang = False
else:
# Add 3 for '#!' prefix and newline suffix.
shebang_length = len(executable) + len(post_interp) + 3
Expand Down
22 changes: 22 additions & 0 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ def test_custom_shebang(self):
actual = os.path.realpath(stdout.strip())
self.assertEqual(actual, expected.encode('utf-8'))

@unittest.skipIf(os.name != 'posix', 'Test only appropriate for '
'POSIX systems')
def test_cross_env_shebang(self):
# Construct an executable with a path that wouldn't ordinarily be a problem
self.maker.executable = '/path/to/python3'

# Set the cross-compiling marker attribute, then clean up.
try:
sys.cross_compiling = True
filenames = self.maker.make('script1.py')
finally:
delattr(sys, "cross_compiling")

with open(filenames[0], 'rb') as f:
first_line = f.readline()
second_line = f.readline()
third_line = f.readline()
self.assertEqual(first_line, b'#!/bin/sh\n')
self.assertEqual(second_line, b"'''exec' /path/to/python3 "
b'"$0" "$@"\n')
self.assertEqual(third_line, b"' '''\n")

def test_multiple(self):
specs = ('foo.py', 'script1.py', 'script2.py', 'script3.py', 'shell.sh', 'uwsgi_part')
files = self.maker.make_multiple(specs)
Expand Down

0 comments on commit a16423f

Please sign in to comment.