Skip to content

Commit 77bb52d

Browse files
Make InterpreterOptions configuration option a list (#259)
There are problems associated with `InterpreterOptions` being a string, chiefly that it isn't safe to assume where individual Python interpreter command line arguments begin and end simply by splitting the string on space characters. This isn't so much of a concern while the value of `InterpreterOptions` is just appended to the main .pex shebang in please_pex, but it'll become more of a concern when we instead rely on a native-code .pex preamble to invoke the interpreter because we'll be passing arguments variadically to `execvp(3)`. Make `InterpreterOptions` a repeatable configuration option and pass each value to please_pex as a separate `--interpreter_options` command line option (which itself also becomes repeatable). This isn't a breaking change because the values passed via `--interpreter_options` are for now ultimately just being stringified via a space-delimited join and appended to the .pex shebang, but the distinction will become meaningful when we implement a native-code .pex preamble.
1 parent 2c54638 commit 77bb52d

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

.plzconfig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ Help = A path or build label for the pex tool, which is used to create .pex file
4242

4343
[PluginConfig "interpreter_options"]
4444
ConfigKey = InterpreterOptions
45-
DefaultValue = ""
46-
Help = Any additional flags to pass to the python interpreter
45+
Optional = true
46+
Repeatable = true
47+
Help = Additional command line arguments to pass to the Python interpreter.
4748

4849
[PluginConfig "test_runner"]
4950
ConfigKey = TestRunner

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ The available configuration options are:
6262

6363
```ini
6464
[Plugin "python"]
65-
InterpreterOptions = -b -s
65+
InterpreterOptions = -b
66+
InterpreterOptions = -s
6667
DefaultInterpreter = python3
6768
PexTool = //tools/please_pex
6869
TestRunner = unittest

build_defs/python.build_defs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ def python_binary(name:str, main:str, srcs:list=[], resources:list=[], out:str=N
147147

148148
shebang = shebang or _interpreter_cmd(interpreter)
149149
zipsafe_flag = '' if zip_safe is False else '--zip_safe'
150-
cmd = '$TOOLS_PEX -s "%s" -m "%s" %s --interpreter_options="%s"' % (
151-
shebang, module_dir, zipsafe_flag, CONFIG.PYTHON.INTERPRETER_OPTIONS)
150+
interpreter_opts = _interpreter_opts(CONFIG.PYTHON.INTERPRETER_OPTIONS)
151+
cmd = '$TOOLS_PEX -s "%s" -m "%s" %s %s' % (
152+
shebang, module_dir, zipsafe_flag, interpreter_opts)
152153

153154
# If content hashing feature flag is enabled, we use the hash of the built
154155
# dependencies instead of the RULE_HASH as the base of the pex extraction
@@ -270,7 +271,8 @@ def python_test(name:str, srcs:list, data:list|dict=[], resources:list=[], deps:
270271
"""
271272
test_runner = test_runner or CONFIG.PYTHON.TEST_RUNNER
272273
shebang = shebang or _interpreter_cmd(interpreter)
273-
cmd = f'$TOOLS_PEX -t -s "{shebang}" -m "{module_dir}" -r "{test_runner}" --zip_safe --add_test_runner_deps --interpreter_options="{CONFIG.PYTHON.INTERPRETER_OPTIONS}" --stamp="$RULE_HASH"'
274+
interpreter_opts = _interpreter_opts(CONFIG.PYTHON.INTERPRETER_OPTIONS)
275+
cmd = f'$TOOLS_PEX -t -s "{shebang}" -m "{module_dir}" -r "{test_runner}" --zip_safe --add_test_runner_deps {interpreter_opts} --stamp="$RULE_HASH"'
274276
if site:
275277
cmd += ' -S'
276278

@@ -746,6 +748,10 @@ def _interpreter_cmd(interpreter:str):
746748
return f'$(out {interpreter})' if looks_like_build_label(interpreter) else interpreter
747749

748750

751+
def _interpreter_opts(opts:list):
752+
return " ".join([f'--interpreter_options="{o}"' for o in opts])
753+
754+
749755
def _patch_cmd(patch):
750756
"""Returns a command for the given patch or patches, with OS-specific tweaks where needed."""
751757
patches = [patch] if isinstance(patch, str) else patch

tools/please_pex/pex/pex.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Writer struct {
4747
}
4848

4949
// NewWriter constructs a new Writer.
50-
func NewWriter(entryPoint, interpreter, options, stamp string, zipSafe, noSite bool) *Writer {
50+
func NewWriter(entryPoint, interpreter string, options []string, stamp string, zipSafe, noSite bool) *Writer {
5151
pw := &Writer{
5252
zipSafe: zipSafe,
5353
noSite: noSite,
@@ -59,8 +59,8 @@ func NewWriter(entryPoint, interpreter, options, stamp string, zipSafe, noSite b
5959
}
6060

6161
// SetShebang sets the leading shebang that will be written to the file.
62-
func (pw *Writer) SetShebang(shebang string, options string) {
63-
shebang = strings.TrimSpace(fmt.Sprintf("%s %s", shebang, options))
62+
func (pw *Writer) SetShebang(shebang string, options []string) {
63+
shebang = strings.TrimSpace(fmt.Sprintf("%s %s", shebang, strings.Join(options, " ")))
6464
if !path.IsAbs(shebang) {
6565
shebang = "/usr/bin/env " + shebang
6666
}

tools/please_pex/pex_main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var opts = struct {
2222
TestRunner string `short:"r" long:"test_runner" default:"unittest" description:"Test runner to use"`
2323
Shebang string `short:"s" long:"shebang" description:"Explicitly set shebang to this"`
2424
Stamp string `long:"stamp" description:"Unique value used to derive cache directory for pex"`
25-
InterpreterOptions string `long:"interpreter_options" description:"Options-string to pass to the python interpreter"`
25+
InterpreterOptions []string `long:"interpreter_options" description:"Additional command line arguments to pass to the Python interpreter"`
2626
Test bool `short:"t" long:"test" description:"True if we're to build a test"`
2727
Debug pex.Debugger `short:"d" long:"debug" optional:"true" optional-value:"pdb" choice:"pdb" choice:"debugpy" description:"Debugger to generate a debugging pex"`
2828
Site bool `short:"S" long:"site" description:"Allow the pex to import site at startup"`

0 commit comments

Comments
 (0)