Skip to content

Commit

Permalink
Escape with double quotes instead of single quotes
Browse files Browse the repository at this point in the history
This takes us away from the standard shlex implementation. It is still
not what you want on Windows (backslashes will not be understood),
however it should do what you want in more situations (for example it
supports spaces).
  • Loading branch information
remram44 committed Feb 28, 2020
1 parent 549eb60 commit 67c5cab
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@
import types


# this is quote from the shlex module, added in py3.3
_find_unsafe = re.compile(br'[^\w@%+=:,./~-]').search
safe_shell_chars = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"-+=/:.,%_")


def _sh_quote(s):
"""Return a shell-escaped version of the string `s`."""
if not s:
return b""
if _find_unsafe(s) is None:
r"""Given bl"a, returns "bl\\"a".
"""
if isinstance(s, bytes):
s = s.decode('utf-8')
if not s or any(c not in safe_shell_chars for c in s):
return '"%s"' % (s.replace('\\', '\\\\')
.replace('"', '\\"')
.replace('`', '\\`')
.replace('$', '\\$'))
else:
return s

# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return b"'" + s.replace(b"'", b"'\"'\"'") + b"'"


# Unicode conversion functions; assume UTF-8

Expand Down

0 comments on commit 67c5cab

Please sign in to comment.