Improve subproctools.quote.

This commit is contained in:
voussoir 2020-12-15 13:10:13 -08:00
parent f2456faf62
commit f72c68bab2

View file

@ -9,7 +9,8 @@ def quote(arg):
# separate for external applications. # separate for external applications.
# Ampersand, pipe, and caret are process flow and special escape. # Ampersand, pipe, and caret are process flow and special escape.
# Quotes inside quotes must be doubled up. # Quotes inside quotes must be doubled up.
if arg == '' or any((c.isspace() or c in arg) for c in [',', ';', '=', '&', '|', '^', '"']): badchars = [',', ';', '=', '&', '|', '^', '"']
if arg == '' or any(c.isspace() for c in arg) or any(c in arg for c in badchars):
arg = arg.replace('"', '""') arg = arg.replace('"', '""')
arg = f'"{arg}"' arg = f'"{arg}"'
return arg return arg
@ -17,7 +18,8 @@ def quote(arg):
# Semicolon is command delimiter. # Semicolon is command delimiter.
# Equals assigns shell variables. # Equals assigns shell variables.
# Quotes inside quotes must be escaped with backslash. # Quotes inside quotes must be escaped with backslash.
if arg == '' or any((c.isspace() or c in arg) for c in [' ', ';', '=', '&', '|']): badchars = [' ', ';', '=', '&', '|']
if arg == '' or any(c.isspace() for c in arg) or any(c in arg for c in badchars):
arg = arg.replace("'", "\\'") arg = arg.replace("'", "\\'")
arg = f"'{arg}'" arg = f"'{arg}'"
return arg return arg