Skip to content

Commit 3732168

Browse files
itamarojezdezclaude
committed
fixup.py: handle "#!" lines that can't hold the tree's location
A "#!" line can't name an interpreter whose path has spaces, and its length is limited. fixup.py wrote the tree's location into the installed Python scripts regardless, so `idle3`, `pydoc3`, `python-config.py` and (on macOS) `python3-config` didn't run from such a tree. When a plain "#!" line wouldn't work, make the script run through /bin/sh, with a first statement that the shell reads as `exec` of the interpreter and Python reads as a string. Other scripts keep an ordinary "#!" line. This is the approach of #39, moved to fixup.py. Co-authored-by: Jannis Leidel <jannis@leidel.info> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 99d49aa commit 3732168

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Replacing the placeholder is a separate step, done at install time. Once the tre
5353
python3 ../prebuilt-cpython/fixup.py dist/
5454
```
5555

56-
`fixup.py` replaces the placeholder with the tree's location in text files only and lists the binary files that still contain it. It never modifies a binary, so signatures stay valid. It fails if no text file in the tree has the placeholder, which is the case for a tree it has already been run on.
56+
`fixup.py` replaces the placeholder with the tree's location in text files only and lists the binary files that still contain it. It never modifies a binary, so signatures stay valid. A `#!` line can't hold a path with spaces and is limited in length, so where the tree's location doesn't fit in one, the installed Python scripts are run through `/bin/sh` instead. It fails if no text file in the tree has the placeholder, which is the case for a tree it has already been run on.
5757

5858
`gather --relative-rpath` also uses `patchelf` to give the installed ELF files an `$ORIGIN`-relative RPATH, so they load libraries from inside the tree wherever it ends up; this follows what python-build-standalone does at build time. It's Linux only.
5959

‎fixup.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,32 @@
2020
import argparse
2121
import os
2222
import pathlib
23+
import re
2324
import sys
2425

2526
PLACEHOLDER = "/the/knights/who/say/ni".ljust(255, "i")
27+
# The first line of the Python scripts that CPython installs.
28+
SHEBANG = re.compile(
29+
rb"#!(%s/bin/python[^/\s]*)\n" % re.escape(os.fsencode(PLACEHOLDER))
30+
)
31+
# Linux before 5.1 truncates a longer "#!" line.
32+
SHEBANG_LIMIT = 127
33+
34+
35+
def fix_shebang(contents: bytes, placeholder: bytes, prefix: bytes) -> bytes:
36+
"""Replace a "#!" line that wouldn't work with the new prefix.
37+
38+
A "#!" line can't name an interpreter whose path has spaces, and its
39+
length is limited. Such a script is run by /bin/sh instead, with a
40+
first statement that the shell reads as `exec` and Python as a string.
41+
"""
42+
match = SHEBANG.match(contents)
43+
if match is None:
44+
return contents
45+
python = match[1].replace(placeholder, prefix)
46+
if not re.search(rb"\s", python) and len(python) + 2 <= SHEBANG_LIMIT:
47+
return contents
48+
return b"#!/bin/sh\n'exec' '%s' \"$0\" \"$@\"\n" % python + contents[match.end() :]
2649

2750

2851
def fixup(tree: pathlib.Path) -> tuple[list[pathlib.Path], list[pathlib.Path]]:
@@ -57,6 +80,7 @@ def fixup(tree: pathlib.Path) -> tuple[list[pathlib.Path], list[pathlib.Path]]:
5780
"it has already been fixed up or wasn't built by relocatable.py"
5881
)
5982
for path, contents in text.items():
83+
contents = fix_shebang(contents, placeholder, prefix)
6084
path.write_bytes(contents.replace(placeholder, prefix))
6185
return sorted(text), sorted(binary)
6286

0 commit comments

Comments
 (0)