Skip to content

Commit

Permalink
Use shlex.quote instead of deprecated pipes.quote (#2351)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzymadness authored Jun 7, 2022
1 parent a8fd40f commit a70b019
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2351.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use ``shlex.quote`` instead of deprecated ``pipes.quote`` in Python 3. - by :user:`frenzymadness`.
8 changes: 6 additions & 2 deletions src/virtualenv/discovery/cached_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import logging
import os
import pipes
import sys
from collections import OrderedDict

Expand All @@ -19,6 +18,11 @@
from virtualenv.util.six import ensure_text
from virtualenv.util.subprocess import Popen, subprocess

if PY2:
from pipes import quote
else:
from shlex import quote

_CACHE = OrderedDict()
_CACHE[Path(sys.executable)] = PythonInfo()

Expand Down Expand Up @@ -126,7 +130,7 @@ def __repr__(self):
def e(v):
return v.decode("utf-8") if isinstance(v, bytes) else v

cmd_repr = e(" ").join(pipes.quote(e(c)) for c in self.cmd)
cmd_repr = e(" ").join(quote(e(c)) for c in self.cmd)
if self.env is not None:
cmd_repr += e(" env of {!r}").format(self.env)
if PY2:
Expand Down
8 changes: 6 additions & 2 deletions tasks/make_zipapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io
import json
import os
import pipes
import shutil
import subprocess
import sys
Expand All @@ -18,6 +17,11 @@
from packaging.markers import Marker
from packaging.requirements import Requirement

if sys.version_info[0] == 2:
from pipes import quote
else:
from shlex import quote

HERE = Path(__file__).parent.absolute()

VERSIONS = ["3.{}".format(i) for i in range(10, 4, -1)] + ["2.7"]
Expand Down Expand Up @@ -227,7 +231,7 @@ def run_suppress_output(cmd, stop_print_on_fail=False):
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
out, err = process.communicate()
if stop_print_on_fail and process.returncode != 0:
print("exit with {} of {}".format(process.returncode, " ".join(pipes.quote(i) for i in cmd)), file=sys.stdout)
print("exit with {} of {}".format(process.returncode, " ".join(quote(i) for i in cmd)), file=sys.stdout)
if out:
print(out, file=sys.stdout)
if err:
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/activation/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import absolute_import, unicode_literals

import os
import pipes
import re
import shutil
import subprocess
Expand All @@ -11,12 +10,17 @@
import pytest
import six

from virtualenv.info import IS_PYPY, WIN_CPYTHON_2
from virtualenv.info import IS_PYPY, PY2, WIN_CPYTHON_2
from virtualenv.run import cli_run
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_str, ensure_text
from virtualenv.util.subprocess import Popen

if PY2:
from pipes import quote
else:
from shlex import quote


class ActivationTester(object):
def __init__(self, of_class, session, cmd, activate_script, extension):
Expand Down Expand Up @@ -157,7 +161,7 @@ def assert_output(self, out, raw, tmp_path):
assert out[-1] == "None", raw

def quote(self, s):
return pipes.quote(s)
return quote(s)

def python_cmd(self, cmd):
return "{} -c {}".format(os.path.basename(sys.executable), self.quote(cmd))
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/activation/test_batch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import absolute_import, unicode_literals

import pipes

from virtualenv.activation import BatchActivator
from virtualenv.info import PY2

if PY2:
from pipes import quote
else:
from shlex import quote


def test_batch(activation_tester_class, activation_tester, tmp_path, activation_python):
Expand All @@ -25,7 +29,7 @@ def _get_test_lines(self, activate_script):

def quote(self, s):
"""double quotes needs to be single, and single need to be double"""
return "".join(("'" if c == '"' else ('"' if c == "'" else c)) for c in pipes.quote(s))
return "".join(("'" if c == '"' else ('"' if c == "'" else c)) for c in quote(s))

def print_prompt(self):
return "echo %PROMPT%"
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/activation/test_powershell.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from __future__ import absolute_import, unicode_literals

import pipes
import sys

import pytest

from virtualenv.activation import PowerShellActivator
from virtualenv.info import PY2

if PY2:
from pipes import quote
else:
from shlex import quote


@pytest.mark.slow
Expand All @@ -23,7 +28,7 @@ def __init__(self, session):

def quote(self, s):
"""powershell double double quote needed for quotes within single quotes"""
return pipes.quote(s).replace('"', '""')
return quote(s).replace('"', '""')

def _get_test_lines(self, activate_script):
# for BATCH utf-8 support need change the character code page to 650001
Expand Down

0 comments on commit a70b019

Please sign in to comment.