Skip to content

Commit

Permalink
run python scripts as "python -c text" (python-poetry#8565)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and MrGreenTea committed Dec 18, 2023
1 parent 6f31547 commit 1c62aa2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 155 deletions.
6 changes: 1 addition & 5 deletions src/poetry/inspection/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,7 @@ def get_pep517_metadata(path: Path) -> PackageInfo:
"--no-input",
*PEP517_META_BUILD_DEPS,
)
venv.run(
"python",
"-",
input_=pep517_meta_build_script,
)
venv.run_python_script(pep517_meta_build_script)
info = PackageInfo.from_metadata(dest_dir)
except EnvCommandError as e:
# something went wrong while attempting pep517 metadata build
Expand Down
20 changes: 4 additions & 16 deletions src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ def run_python_script(self, content: str, **kwargs: Any) -> str:
"-I",
"-W",
"ignore",
"-",
input_=content,
"-c",
content,
stderr=subprocess.PIPE,
**kwargs,
)
Expand All @@ -338,23 +338,11 @@ def _run(self, cmd: list[str], **kwargs: Any) -> str:
Run a command inside the Python environment.
"""
call = kwargs.pop("call", False)
input_ = kwargs.pop("input_", None)
env = kwargs.pop("env", dict(os.environ))
stderr = kwargs.pop("stderr", subprocess.STDOUT)

try:
if input_:
output: str = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=stderr,
input=input_,
check=True,
env=env,
text=True,
**kwargs,
).stdout
elif call:
if call:
assert stderr != subprocess.PIPE
subprocess.check_call(cmd, stderr=stderr, env=env, **kwargs)
output = ""
Expand All @@ -363,7 +351,7 @@ def _run(self, cmd: list[str], **kwargs: Any) -> str:
cmd, stderr=stderr, env=env, text=True, **kwargs
)
except CalledProcessError as e:
raise EnvCommandError(e, input=input_)
raise EnvCommandError(e)

return output

Expand Down
4 changes: 1 addition & 3 deletions src/poetry/utils/env/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, env_name: str) -> None:


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

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


Expand Down
23 changes: 17 additions & 6 deletions tests/console/commands/env/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,28 @@ def check_output_wrapper(
) -> Callable[[list[str], Any, Any], str]:
def check_output(cmd: list[str], *args: Any, **kwargs: Any) -> str:
# cmd is a list, like ["python", "-c", "do stuff"]
python_cmd = cmd[2]
python_cmd = cmd[-1]
if "print(json.dumps(env))" in python_cmd:
return (
f'{{"version_info": [{version.major}, {version.minor},'
f" {version.patch}]}}"
)

if "sys.version_info[:3]" in python_cmd:
return version.text
elif "sys.version_info[:2]" in python_cmd:

if "sys.version_info[:2]" in python_cmd:
return f"{version.major}.{version.minor}"
elif "import sys; print(sys.executable)" in python_cmd:

if "import sys; print(sys.executable)" in python_cmd:
executable = cmd[0]
basename = os.path.basename(executable)
return f"/usr/bin/{basename}"
else:
assert "import sys; print(sys.prefix)" in python_cmd
return "/prefix"

if "print(sys.base_prefix)" in python_cmd:
return "/usr"

assert "import sys; print(sys.prefix)" in python_cmd
return "/prefix"

return check_output
Loading

0 comments on commit 1c62aa2

Please sign in to comment.