Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have subprocess deal with text #8060

Merged
merged 1 commit into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

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
Expand Down Expand Up @@ -343,25 +341,28 @@ def _run(self, cmd: list[str], **kwargs: Any) -> str:

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:
assert stderr != subprocess.PIPE
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)
Expand Down
54 changes: 20 additions & 34 deletions src/poetry/utils/env/env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

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
Expand Down Expand Up @@ -67,11 +66,9 @@ def _full_python_path(python: str) -> Path | None:
return None

try:
executable = decode(
subprocess.check_output(
[path_python, "-c", "import sys; print(sys.executable)"],
).strip()
)
executable = subprocess.check_output(
[path_python, "-c", "import sys; print(sys.executable)"], text=True
).strip()
return Path(executable)

except CalledProcessError:
Expand Down Expand Up @@ -115,11 +112,9 @@ def get_python_version(
executable = EnvManager._detect_active_python(io)

if executable:
python_patch = decode(
subprocess.check_output(
[executable, "-c", GET_PYTHON_VERSION_ONELINER],
).strip()
)
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])

Expand Down Expand Up @@ -150,10 +145,8 @@ def activate(self, python: str) -> Env:
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)
Expand Down Expand Up @@ -334,10 +327,8 @@ def remove(self, python: str) -> Env:
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):
Expand Down Expand Up @@ -393,10 +384,8 @@ def remove(self, python: str) -> Env:
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)
Expand Down Expand Up @@ -485,11 +474,9 @@ def create_venv(
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],
).strip()
)
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
Expand Down Expand Up @@ -533,12 +520,11 @@ def create_venv(
continue

try:
python_patch = decode(
subprocess.check_output(
[python, "-c", GET_PYTHON_VERSION_ONELINER],
stderr=subprocess.STDOUT,
).strip()
)
python_patch = subprocess.check_output(
[python, "-c", GET_PYTHON_VERSION_ONELINER],
stderr=subprocess.STDOUT,
text=True,
).strip()
except CalledProcessError:
continue

Expand Down
15 changes: 6 additions & 9 deletions src/poetry/vcs/git/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@ def run(*args: Any, **kwargs: Any) -> str:
git_command = find_git_command()
env = os.environ.copy()
env["GIT_TERMINAL_PROMPT"] = "0"
return (
subprocess.check_output(
git_command + list(args),
stderr=subprocess.STDOUT,
env=env,
)
.decode()
.strip()
)
return subprocess.check_output(
git_command + list(args),
stderr=subprocess.STDOUT,
env=env,
text=True,
).strip()

@staticmethod
def _check_parameter(parameter: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down