indygreg / python-build-standalone

Produce redistributable builds of Python
Mozilla Public License 2.0
2.14k stars 133 forks source link

Shebangs in (e.g.) `pydoc3` fail for paths with spaces on macOS #394

Open charliermarsh opened 3 hours ago

charliermarsh commented 3 hours ago

For example:

❯ "/Users/crmarsh/Library/Application Support/uv/python/cpython-3.13.0-macos-aarch64-none/bin/pydoc3"
/Users/crmarsh/Library/Application Support/uv/python/cpython-3.13.0-macos-aarch64-none/bin/pydoc3: line 2: /Users/crmarsh/Library
Support/uv/python/cpython-3.13.0-macos-aarch64-none/bin/python3.13: No such file or directory
/Users/crmarsh/Library/Application Support/uv/python/cpython-3.13.0-macos-aarch64-none/bin/pydoc3: line 2: exec: /Users/crmarsh/Library
Support/uv/python/cpython-3.13.0-macos-aarch64-none/bin/python3.13: cannot execute: No such file or directory

The script looks like:

#!/bin/sh
"exec" "$(dirname $0)/python3.13" "$0" "$@"

import pydoc
if __name__ == '__main__':
    pydoc.cli()

In uv, however, our relocatable shebang looks like:

#!/bin/sh
'''exec' "$(dirname -- "$(realpath -- "$0")")"/'python3' "$0" "$@"
' '''
# -*- coding: utf-8 -*-
import re
import sys
from black import patched_main
if __name__ == "__main__":
    sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0])
    sys.exit(patched_main())

(\ht to @paveldikov who authored it.)

charliermarsh commented 3 hours ago

Write-ups on uv shebang, which seems to be pretty robust: