Commit 9315887 1 parent 399c879 commit 9315887 Copy full SHA for 9315887
File tree 2 files changed +11
-4
lines changed
2 files changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -55,11 +55,18 @@ def cmdify(self):
55
55
56
56
The result is then quoted into a pair of double quotes to be grouped.
57
57
58
+ An argument is intentionally not quoted if it does not contain
59
+ whitespaces. This is done to be compatible with Windows built-in
60
+ commands that don't work well with quotes, e.g. everything with `echo`,
61
+ and DOS-style (forward slash) switches.
62
+
58
63
The intended use of this function is to pre-process an argument list
59
64
before passing it into ``subprocess.Popen(..., shell=True)``.
60
65
61
66
See also: https://docs.python.org/3/library/subprocess.html#converting-argument-sequence
62
67
"""
63
68
return " " .join (
64
- '"{0}"' .format (re .sub (r'(\\*)"' , r'\1\1\\"' , arg )) for arg in self ._parts
69
+ arg if not next (re .finditer (r'\s' , arg ), None )
70
+ else '"{0}"' .format (re .sub (r'(\\*)"' , r'\1\1\\"' , arg ))
71
+ for arg in self ._parts
65
72
)
Original file line number Diff line number Diff line change @@ -30,9 +30,9 @@ def test_extend():
30
30
@pytest .mark .run
31
31
@pytest .mark .script
32
32
def test_cmdify ():
33
- script = Script ('python' , ['-c' , "print('hello')" ])
33
+ script = Script ('python' , ['-c' , "print('hello world ')" ])
34
34
cmd = script .cmdify ()
35
- assert cmd == '" python" "-c" "print(\' hello\' )"' , script
35
+ assert cmd == 'python -c "print(\' hello world \' )"' , script
36
36
37
37
38
38
@pytest .mark .run
@@ -44,6 +44,6 @@ def test_cmdify_complex():
44
44
]))
45
45
assert script .cmdify () == ' ' .join ([
46
46
'"C:\\ Program Files\\ Python36\\ python.exe"' ,
47
- '"-c" ' ,
47
+ '-c ' ,
48
48
""" "print(\' Double quote: \\ \" \' )" """ .strip (),
49
49
]), script
You can’t perform that action at this time.
0 commit comments