Skip to content

Commit a16423f

Browse files
authored
Add handling for cross-compilation environments. (#231)
1 parent cbd4ae1 commit a16423f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

distlib/scripts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ def _build_shebang(self, executable, post_interp):
157157
"""
158158
if os.name != 'posix':
159159
simple_shebang = True
160+
elif getattr(sys, "cross_compiling", False):
161+
# In a cross-compiling environment, the shebang will likely be a
162+
# script; this *must* be invoked with the "safe" version of the
163+
# shebang, or else using os.exec() to run the entry script will
164+
# fail, raising "OSError 8 [Errno 8] Exec format error".
165+
simple_shebang = False
160166
else:
161167
# Add 3 for '#!' prefix and newline suffix.
162168
shebang_length = len(executable) + len(post_interp) + 3

tests/test_scripts.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ def test_custom_shebang(self):
121121
actual = os.path.realpath(stdout.strip())
122122
self.assertEqual(actual, expected.encode('utf-8'))
123123

124+
@unittest.skipIf(os.name != 'posix', 'Test only appropriate for '
125+
'POSIX systems')
126+
def test_cross_env_shebang(self):
127+
# Construct an executable with a path that wouldn't ordinarily be a problem
128+
self.maker.executable = '/path/to/python3'
129+
130+
# Set the cross-compiling marker attribute, then clean up.
131+
try:
132+
sys.cross_compiling = True
133+
filenames = self.maker.make('script1.py')
134+
finally:
135+
delattr(sys, "cross_compiling")
136+
137+
with open(filenames[0], 'rb') as f:
138+
first_line = f.readline()
139+
second_line = f.readline()
140+
third_line = f.readline()
141+
self.assertEqual(first_line, b'#!/bin/sh\n')
142+
self.assertEqual(second_line, b"'''exec' /path/to/python3 "
143+
b'"$0" "$@"\n')
144+
self.assertEqual(third_line, b"' '''\n")
145+
124146
def test_multiple(self):
125147
specs = ('foo.py', 'script1.py', 'script2.py', 'script3.py', 'shell.sh', 'uwsgi_part')
126148
files = self.maker.make_multiple(specs)

0 commit comments

Comments
 (0)