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