Return a shell-escaped version of the string *s*.
(s)
| 318 | |
| 319 | |
| 320 | def quote(s): |
| 321 | """Return a shell-escaped version of the string *s*.""" |
| 322 | if not s: |
| 323 | return "''" |
| 324 | |
| 325 | if not isinstance(s, str): |
| 326 | raise TypeError(f"expected string object, got {type(s).__name__!r}") |
| 327 | |
| 328 | # Use bytes.translate() for performance |
| 329 | safe_chars = (b'%+,-./0123456789:=@' |
| 330 | b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_' |
| 331 | 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): |
| 334 | return s |
| 335 | |
| 336 | # use single quotes, and put single quotes into double quotes |
| 337 | # the string $'b is then quoted as '$'"'"'b' |
| 338 | return "'" + s.replace("'", "'\"'\"'") + "'" |
| 339 | |
| 340 | |
| 341 | def _print_tokens(lexer): |