Skip to content

Commit ddbe3a5

Browse files
committed
shlex: Implement force parameter behavior for shlex.quote
1 parent d206d42 commit ddbe3a5

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

Lib/shlex.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,12 @@ def join(split_command):
317317
return ' '.join(quote(arg) for arg in split_command)
318318

319319

320-
def quote(s):
321-
"""Return a shell-escaped version of the string *s*."""
320+
def quote(s, force=False):
321+
"""Return a shell-escaped version of the string *s*.
322+
323+
If *force* is *True* then *s* will be quoted even if it is
324+
already safe for a shell without being quoted.
325+
"""
322326
if not s:
323327
return "''"
324328

@@ -329,8 +333,11 @@ def quote(s):
329333
safe_chars = (b'%+,-./0123456789:=@'
330334
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'
331335
b'abcdefghijklmnopqrstuvwxyz')
332-
# No quoting is needed if `s` is an ASCII string consisting only of `safe_chars`
333-
if s.isascii() and not s.encode().translate(None, delete=safe_chars):
336+
if (not force
337+
and s.isascii() and not s.encode().translate(None, delete=safe_chars)
338+
):
339+
# No quoting is needed if we're not forcing quoting
340+
# and `s` is an ASCII string consisting only of `safe_chars`
334341
return s
335342

336343
# use single quotes, and put single quotes into double quotes

0 commit comments

Comments
 (0)