Skip to content

Commit f11f81d

Browse files
committed
run python scripts as "python -c text"
and not as "python -", and then passing the script as input
1 parent c41e213 commit f11f81d

File tree

5 files changed

+63
-155
lines changed

5 files changed

+63
-155
lines changed

src/poetry/inspection/info.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,7 @@ def get_pep517_metadata(path: Path) -> PackageInfo:
586586
"--no-input",
587587
*PEP517_META_BUILD_DEPS,
588588
)
589-
venv.run(
590-
"python",
591-
"-",
592-
input_=pep517_meta_build_script,
593-
)
589+
venv.run_python_script(pep517_meta_build_script)
594590
info = PackageInfo.from_metadata(dest_dir)
595591
except EnvCommandError as e:
596592
# something went wrong while attempting pep517 metadata build

src/poetry/utils/env/base_env.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ def run_python_script(self, content: str, **kwargs: Any) -> str:
327327
"-I",
328328
"-W",
329329
"ignore",
330-
"-",
331-
input_=content,
330+
"-c",
331+
content,
332332
stderr=subprocess.PIPE,
333333
**kwargs,
334334
)
@@ -338,23 +338,11 @@ def _run(self, cmd: list[str], **kwargs: Any) -> str:
338338
Run a command inside the Python environment.
339339
"""
340340
call = kwargs.pop("call", False)
341-
input_ = kwargs.pop("input_", None)
342341
env = kwargs.pop("env", dict(os.environ))
343342
stderr = kwargs.pop("stderr", subprocess.STDOUT)
344343

345344
try:
346-
if input_:
347-
output: str = subprocess.run(
348-
cmd,
349-
stdout=subprocess.PIPE,
350-
stderr=stderr,
351-
input=input_,
352-
check=True,
353-
env=env,
354-
text=True,
355-
**kwargs,
356-
).stdout
357-
elif call:
345+
if call:
358346
assert stderr != subprocess.PIPE
359347
subprocess.check_call(cmd, stderr=stderr, env=env, **kwargs)
360348
output = ""
@@ -363,7 +351,7 @@ def _run(self, cmd: list[str], **kwargs: Any) -> str:
363351
cmd, stderr=stderr, env=env, text=True, **kwargs
364352
)
365353
except CalledProcessError as e:
366-
raise EnvCommandError(e, input=input_)
354+
raise EnvCommandError(e)
367355

368356
return output
369357

src/poetry/utils/env/exceptions.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, env_name: str) -> None:
2020

2121

2222
class EnvCommandError(EnvError):
23-
def __init__(self, e: CalledProcessError, input: str | None = None) -> None:
23+
def __init__(self, e: CalledProcessError) -> None:
2424
self.e = e
2525

2626
message_parts = [
@@ -30,8 +30,6 @@ def __init__(self, e: CalledProcessError, input: str | None = None) -> None:
3030
message_parts.append(f"Output:\n{decode(e.output)}")
3131
if e.stderr:
3232
message_parts.append(f"Error output:\n{decode(e.stderr)}")
33-
if input:
34-
message_parts.append(f"Input:\n{input}")
3533
super().__init__("\n\n".join(message_parts))
3634

3735

tests/console/commands/env/helpers.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,28 @@ def check_output_wrapper(
2424
) -> Callable[[list[str], Any, Any], str]:
2525
def check_output(cmd: list[str], *args: Any, **kwargs: Any) -> str:
2626
# cmd is a list, like ["python", "-c", "do stuff"]
27-
python_cmd = cmd[2]
27+
python_cmd = cmd[-1]
28+
if "print(json.dumps(env))" in python_cmd:
29+
return (
30+
f'{{"version_info": [{version.major}, {version.minor},'
31+
f" {version.patch}]}}"
32+
)
33+
2834
if "sys.version_info[:3]" in python_cmd:
2935
return version.text
30-
elif "sys.version_info[:2]" in python_cmd:
36+
37+
if "sys.version_info[:2]" in python_cmd:
3138
return f"{version.major}.{version.minor}"
32-
elif "import sys; print(sys.executable)" in python_cmd:
39+
40+
if "import sys; print(sys.executable)" in python_cmd:
3341
executable = cmd[0]
3442
basename = os.path.basename(executable)
3543
return f"/usr/bin/{basename}"
36-
else:
37-
assert "import sys; print(sys.prefix)" in python_cmd
38-
return "/prefix"
44+
45+
if "print(sys.base_prefix)" in python_cmd:
46+
return "/usr"
47+
48+
assert "import sys; print(sys.prefix)" in python_cmd
49+
return "/prefix"
3950

4051
return check_output

0 commit comments

Comments
 (0)