Commit 7f79fac5 by David Hotham Committed by GitHub

have subprocess deal with text (#8060)

cf python-poetry#7313, python-poetry#7643
parent f6913202
......@@ -14,8 +14,6 @@ from typing import Any
from virtualenv.seed.wheels.embed import get_embed_wheel
from poetry.utils._compat import decode
from poetry.utils._compat import encode
from poetry.utils.env.exceptions import EnvCommandError
from poetry.utils.env.site_packages import SitePackages
from poetry.utils.helpers import get_real_windows_path
......@@ -343,13 +341,14 @@ class Env:
try:
if input_:
output = subprocess.run(
output: str = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=stderr,
input=encode(input_),
input=input_,
check=True,
env=env,
text=True,
**kwargs,
).stdout
elif call:
......@@ -357,11 +356,13 @@ class Env:
subprocess.check_call(cmd, stderr=stderr, env=env, **kwargs)
output = ""
else:
output = subprocess.check_output(cmd, stderr=stderr, env=env, **kwargs)
output = subprocess.check_output(
cmd, stderr=stderr, env=env, text=True, **kwargs
)
except CalledProcessError as e:
raise EnvCommandError(e, input=input_)
return decode(output)
return output
def execute(self, bin: str, *args: str, **kwargs: Any) -> int:
command = self.get_command_from_bin(bin) + list(args)
......
......@@ -23,7 +23,6 @@ from poetry.core.constraints.version import parse_constraint
from poetry.toml.file import TOMLFile
from poetry.utils._compat import WINDOWS
from poetry.utils._compat import decode
from poetry.utils._compat import encode
from poetry.utils.env.exceptions import EnvCommandError
from poetry.utils.env.exceptions import IncorrectEnvError
......@@ -67,11 +66,9 @@ class EnvManager:
return None
try:
executable = decode(
subprocess.check_output(
[path_python, "-c", "import sys; print(sys.executable)"],
executable = subprocess.check_output(
[path_python, "-c", "import sys; print(sys.executable)"], text=True
).strip()
)
return Path(executable)
except CalledProcessError:
......@@ -115,11 +112,9 @@ class EnvManager:
executable = EnvManager._detect_active_python(io)
if executable:
python_patch = decode(
subprocess.check_output(
[executable, "-c", GET_PYTHON_VERSION_ONELINER],
python_patch = subprocess.check_output(
[executable, "-c", GET_PYTHON_VERSION_ONELINER], text=True
).strip()
)
version = ".".join(str(v) for v in python_patch.split(".")[:precision])
......@@ -150,10 +145,8 @@ class EnvManager:
raise PythonVersionNotFound(python)
try:
python_version_string = decode(
subprocess.check_output(
[python_path, "-c", GET_PYTHON_VERSION_ONELINER],
)
python_version_string = subprocess.check_output(
[python_path, "-c", GET_PYTHON_VERSION_ONELINER], text=True
)
except CalledProcessError as e:
raise EnvCommandError(e)
......@@ -334,10 +327,8 @@ class EnvManager:
if python_path.is_file():
# Validate env name if provided env is a full path to python
try:
env_dir = decode(
subprocess.check_output(
[python, "-c", GET_ENV_PATH_ONELINER],
)
env_dir = subprocess.check_output(
[python, "-c", GET_ENV_PATH_ONELINER], text=True
).strip("\n")
env_name = Path(env_dir).name
if not self.check_env_is_for_current_project(env_name, base_env_name):
......@@ -393,10 +384,8 @@ class EnvManager:
pass
try:
python_version_string = decode(
subprocess.check_output(
[python, "-c", GET_PYTHON_VERSION_ONELINER],
)
python_version_string = subprocess.check_output(
[python, "-c", GET_PYTHON_VERSION_ONELINER], text=True
)
except CalledProcessError as e:
raise EnvCommandError(e)
......@@ -485,11 +474,9 @@ class EnvManager:
python_patch = ".".join([str(v) for v in sys.version_info[:3]])
python_minor = ".".join([str(v) for v in sys.version_info[:2]])
if executable:
python_patch = decode(
subprocess.check_output(
[executable, "-c", GET_PYTHON_VERSION_ONELINER],
python_patch = subprocess.check_output(
[executable, "-c", GET_PYTHON_VERSION_ONELINER], text=True
).strip()
)
python_minor = ".".join(python_patch.split(".")[:2])
supported_python = self._poetry.package.python_constraint
......@@ -533,12 +520,11 @@ class EnvManager:
continue
try:
python_patch = decode(
subprocess.check_output(
python_patch = subprocess.check_output(
[python, "-c", GET_PYTHON_VERSION_ONELINER],
stderr=subprocess.STDOUT,
text=True,
).strip()
)
except CalledProcessError:
continue
......
......@@ -53,15 +53,12 @@ class SystemGit:
git_command = find_git_command()
env = os.environ.copy()
env["GIT_TERMINAL_PROMPT"] = "0"
return (
subprocess.check_output(
return subprocess.check_output(
git_command + list(args),
stderr=subprocess.STDOUT,
env=env,
)
.decode()
.strip()
)
text=True,
).strip()
@staticmethod
def _check_parameter(parameter: str) -> None:
......
......@@ -968,7 +968,7 @@ def test_env_has_symlinks_on_nix(tmp_path: Path, tmp_venv: VirtualEnv) -> None:
def test_run_with_input(tmp_path: Path, tmp_venv: VirtualEnv) -> None:
result = tmp_venv.run("python", "-", input_=MINIMAL_SCRIPT)
assert result == "Minimal Output" + os.linesep
assert result == "Minimal Output\n"
def test_run_with_input_non_zero_return(tmp_path: Path, tmp_venv: VirtualEnv) -> None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment