Skip to content

Commit 8d689ae

Browse files
itamaroclaude
andcommitted
gh-119696: Find the interpreter relative to installed scripts with --enable-relocatable
The installed idle3, pydoc3 and python-config Python scripts start with "#!" and the absolute path of the interpreter. That path is wrong once the installation is moved, and a "#!" line can't hold a path that has spaces or is too long. With --enable-relocatable, start them with a /bin/sh wrapper that runs the interpreter by its location relative to the script. python-config.py in the config directory gets the same wrapper, when the bin directory is "bin" in the prefix. Where the realpath command is available, the wrapper also follows a symlink to the script. Without the option the generated scripts are unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 6e8fc18 commit 8d689ae

6 files changed

Lines changed: 62 additions & 12 deletions

File tree

‎Lib/test/test_sysconfig.py‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,27 @@ def test_pkgconfig_prefix(self):
731731
self.assertEqual(os.path.realpath(resolved),
732732
os.path.realpath(sys.base_prefix))
733733

734+
@unittest.skipIf(os.name != 'posix', 'scripts are only installed on POSIX')
735+
@unittest.skipIf(is_wasi or is_android or is_apple_mobile,
736+
'no installed scripts on this platform')
737+
@unittest.skipIf(get_config_var('RELOCATABLE') != 'yes',
738+
'requires --enable-relocatable')
739+
@unittest.skipIf(is_python_build(),
740+
'scripts are only installed by make install')
741+
@requires_subprocess()
742+
def test_relocatable_script(self):
743+
script = os.path.join(sys._stdlib_dir,
744+
os.path.basename(get_config_var('LIBPL')),
745+
'python-config.py')
746+
if not os.path.exists(script):
747+
self.skipTest(f'{script} not found')
748+
with open(script, encoding='utf-8') as f:
749+
self.assertNotIn(get_config_var('EXENAME'), f.readline())
750+
proc = subprocess.run([script, '--prefix'],
751+
capture_output=True, text=True, check=True)
752+
self.assertEqual(os.path.realpath(proc.stdout.rstrip('\n')),
753+
os.path.realpath(sys.base_prefix))
754+
734755
@unittest.skipIf(os.name != 'posix', '_sysconfig-vars JSON file is only available on POSIX')
735756
@unittest.skipIf(is_wasi, "_sysconfig-vars JSON file currently isn't available on WASI")
736757
@unittest.skipIf(is_android or is_apple_mobile, 'Android and iOS change the prefix')
@@ -808,12 +829,12 @@ def test_python_config_sh_prefix_special_characters(self):
808829
f'python{ldversion}-config')
809830
if not os.path.isfile(script):
810831
self.skipTest(f'{script!r} does not exist')
811-
with open(script, encoding='utf-8') as f:
812-
if f.readline().rstrip() != '#!/bin/sh':
813-
self.skipTest('python-config is not the shell script variant')
814-
815832
with open(script, encoding='utf-8') as f:
816833
script_text = f.read()
834+
# With --enable-relocatable, the Python variant is run by /bin/sh too.
835+
if (not script_text.startswith('#!/bin/sh\n')
836+
or script_text.startswith("#!/bin/sh\n'''exec'")):
837+
self.skipTest('python-config is not the shell script variant')
817838
makefile_vars = _parse_makefile(sysconfig.get_makefile_filename())
818839
configured_prefix = makefile_vars['prefix']
819840

‎Makefile.pre.in‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,19 +2929,38 @@ libinstall: all $(srcdir)/Modules/xxmodule.c
29292929
-o 0 -o 1 -o 2 $(COMPILEALL_OPTS) -d $(LIBDEST)/site-packages -f \
29302930
-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
29312931

2932+
# The first lines of an installed Python script. With --enable-relocatable,
2933+
# the script finds the interpreter relative to its own location.
2934+
2935+
# For a script in $(BINDIR).
2936+
BIN_SCRIPT_HEADER= \
2937+
if test "$(RELOCATABLE)" = yes; then \
2938+
sed "s,@PYTHON@,python$(LDVERSION)$(EXE)," $(srcdir)/Misc/relocatable-script-header.in; \
2939+
else \
2940+
printf '%s\n' '\#!$(EXENAME)'; \
2941+
fi
2942+
2943+
# For python-config.py in $(LIBPL), three directories below $(prefix).
2944+
LIBPL_SCRIPT_HEADER= \
2945+
if test "$(RELOCATABLE)" = yes && test '$(BINDIR)' = '$(prefix)/bin'; then \
2946+
sed "s,@PYTHON@,../../../bin/python$(LDVERSION)$(EXE)," $(srcdir)/Misc/relocatable-script-header.in; \
2947+
else \
2948+
printf '%s\n' '\#!$(EXENAME)'; \
2949+
fi
2950+
29322951
# bpo-21536: Misc/python-config.sh is generated in the build directory
29332952
# from $(srcdir)Misc/python-config.sh.in.
2934-
python-config: $(srcdir)/Misc/python-config.in Misc/python-config.sh
2953+
python-config: $(srcdir)/Misc/python-config.in Misc/python-config.sh $(srcdir)/Misc/relocatable-script-header.in
29352954
@ # Substitution happens here, as the completely-expanded BINDIR
29362955
@ # is not available in configure
2937-
sed -e "s,@EXENAME@,$(EXENAME)," < $(srcdir)/Misc/python-config.in >python-config.py
2956+
($(LIBPL_SCRIPT_HEADER); cat $(srcdir)/Misc/python-config.in) >python-config.py
29382957
@ # Replace makefile compat. variable references with shell script compat. ones; $(VAR) -> ${VAR}
29392958
LC_ALL=C sed -e 's,\$$(\([A-Za-z0-9_]*\)),\$$\{\1\},g' < Misc/python-config.sh >python-config
29402959
@ # On Darwin, always use the python version of the script, the shell
29412960
@ # version doesn't use the compiler customizations that are provided
29422961
@ # in python (_osx_support.py).
29432962
@if test `uname -s` = Darwin; then \
2944-
cp python-config.py python-config; \
2963+
($(BIN_SCRIPT_HEADER); cat $(srcdir)/Misc/python-config.in) >python-config; \
29452964
fi
29462965

29472966
# With --enable-relocatable, the installed pkg-config files define prefix
@@ -2972,14 +2991,14 @@ BUILD_SCRIPTS_DIR=build/scripts-$(VERSION)
29722991
SCRIPT_IDLE=$(BUILD_SCRIPTS_DIR)/idle$(VERSION)
29732992
SCRIPT_PYDOC=$(BUILD_SCRIPTS_DIR)/pydoc$(VERSION)
29742993

2975-
$(SCRIPT_IDLE): $(srcdir)/Tools/scripts/idle3
2994+
$(SCRIPT_IDLE): $(srcdir)/Tools/scripts/idle3 $(srcdir)/Misc/relocatable-script-header.in
29762995
@$(MKDIR_P) $(BUILD_SCRIPTS_DIR)
2977-
sed -e "s,/usr/bin/env python3,$(EXENAME)," < $(srcdir)/Tools/scripts/idle3 > $@
2996+
($(BIN_SCRIPT_HEADER); sed 1d $(srcdir)/Tools/scripts/idle3) > $@
29782997
@chmod +x $@
29792998

2980-
$(SCRIPT_PYDOC): $(srcdir)/Tools/scripts/pydoc3
2999+
$(SCRIPT_PYDOC): $(srcdir)/Tools/scripts/pydoc3 $(srcdir)/Misc/relocatable-script-header.in
29813000
@$(MKDIR_P) $(BUILD_SCRIPTS_DIR)
2982-
sed -e "s,/usr/bin/env python3,$(EXENAME)," < $(srcdir)/Tools/scripts/pydoc3 > $@
3001+
($(BIN_SCRIPT_HEADER); sed 1d $(srcdir)/Tools/scripts/pydoc3) > $@
29833002
@chmod +x $@
29843003

29853004
.PHONY: scripts
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
With ``--enable-relocatable``, the installed ``idle3``, ``pydoc3`` and
2+
``python-config`` Python scripts no longer start with the absolute path of the
3+
interpreter. They run the interpreter by its location relative to the script,
4+
which also works when that path contains spaces or is too long for a ``#!``
5+
line.

‎Misc/README‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ python.pc.in Package configuration info template for pkg-config
1616
README The file you're reading now
1717
README.AIX Information about using Python on AIX
1818
README.valgrind Information for Valgrind users, see valgrind-python.supp
19+
relocatable-script-header.in
20+
First lines of installed Python scripts with
21+
--enable-relocatable
1922
SpecialBuilds.txt Describes extra symbols you can set for debug builds
2023
svnmap.txt Map of old SVN revs and branches to hg changeset ids,
2124
help history-digging

‎Misc/python-config.in‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!@EXENAME@
21
# -*- python -*-
32

43
# Keep this script in sync with python-config.sh.in

‎Misc/relocatable-script-header.in‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
'''exec' "$(dirname "$(realpath "$0" 2>/dev/null || printf %s "$0")")/@PYTHON@" "$0" "$@"
3+
' '''

0 commit comments

Comments
 (0)