Use shlex.quote for quoting shell arguments
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John T. Wodder II · committed by David Lord · parent 59a4530 · Browse files · 4 files·+21 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+21 added−15 removed
- tests:+1 added−1 removed
- other:+20 added−14 removed
Provenance
- Commit
e798f64- Tree
0477bfa- Author
John T. Wodder II <git@varonathe.org>- Committer
David Lord <davidism@gmail.com>- Parent
59a4530
4 files changed+21 added−15 removed
modified:click/_compat.py+4 added
160 unmodified lines161 int_types = (int, long)162 iteritems = lambda x: x.iteritems()163 range_type = xrange164 165+ from pipes import quote as shlex_quote166+167 def is_bytes(x):168 return isinstance(x, (buffer, bytearray))169 170 _identifier_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')98 unmodified lines269 range_type = range270 isidentifier = lambda x: x.isidentifier()271 iteritems = lambda x: iter(x.items())272 273+ from shlex import quote as shlex_quote274+275 def is_bytes(x):276 return isinstance(x, (bytes, memoryview, bytearray))277 278 def _is_binary_reader(stream, default=False):496 unmodified linesmodified:click/_termui_impl.py+14 added-14 removed
14 unmodified lines15 import sys16 import time17 import math18 import contextlib19-from ._compat import _default_text_stdout, range_type, PY2, isatty, \20- open_stream, strip_ansi, term_len, get_best_encoding, WIN, int_types, \21- CYGWIN19+from ._compat import _default_text_stdout, range_type, isatty, \20+ open_stream, shlex_quote, strip_ansi, term_len, get_best_encoding, WIN, \21+ int_types, CYGWIN22 from .utils import echo23 from .exceptions import ClickException24 25 301 unmodified lines327 import tempfile328 fd, filename = tempfile.mkstemp()329 os.close(fd)330 try:331- if hasattr(os, 'system') and os.system('more "%s"' % filename) == 0:331+ if hasattr(os, 'system') and os.system("more %s" % shlex_quote(filename)) == 0:332 return _pipepager(generator, 'more', color)333 return _nullpager(stdout, generator, color)334 finally:335 os.unlink(filename)59 unmodified lines395 encoding = get_best_encoding(sys.stdout)396 with open_stream(filename, 'wb')[0] as f:397 f.write(text.encode(encoding))398 try:399- os.system(cmd + ' "' + filename + '"')399+ os.system("%s %s" % (shlex_quote(cmd), shlex_quote(filename)))400 finally:401 os.unlink(filename)402 403 36 unmodified lines440 environ.update(self.env)441 else:442 environ = None443 try:444- c = subprocess.Popen('%s "%s"' % (editor, filename),445- env=environ, shell=True)444+ c = subprocess.Popen(445+ "%s %s" % (shlex_quote(editor), shlex_quote(filename)),446+ env=environ,447+ shell=True448+ )449 exit_code = c.wait()450 if exit_code != 0:451 raise ClickException('%s: Editing failed!' % editor)452 except OSError as e:62 unmodified lines515 null.close()516 elif WIN:517 if locate:518 url = _unquote_file(url)516- args = 'explorer /select,"%s"' % _unquote_file(517- url.replace('"', ''))519+ args = "explorer /select,%s" % (shlex_quote(url),)520 else:519- args = 'start %s "" "%s"' % (520- wait and '/WAIT' or '', url.replace('"', ''))521+ args = 'start %s "" %s' % ("/WAIT" if wait else "", shlex_quote(url))522 return os.system(args)523 elif CYGWIN:524 if locate:525 url = _unquote_file(url)525- args = 'cygstart "%s"' % (os.path.dirname(url).replace('"', ''))526+ args = "cygstart %s" % (shlex_quote(os.path.dirname(url)),)527 else:527- args = 'cygstart %s "%s"' % (528- wait and '-w' or '', url.replace('"', ''))528+ args = "cygstart %s %s" % ("-w" if wait else "", shlex_quote(url))529 return os.system(args)530 531 try:532 if locate:106 unmodified linesmodified:tests/test_imports.py+1 added-1 removed
30 unmodified lines31 32 ALLOWED_IMPORTS = set([33 'weakref', 'os', 'struct', 'collections', 'sys', 'contextlib',34 'functools', 'stat', 're', 'codecs', 'inspect', 'itertools', 'io',35- 'threading', 'colorama', 'errno', 'fcntl', 'datetime'35+ 'threading', 'colorama', 'errno', 'fcntl', 'datetime', 'pipes', 'shlex'36 ])37 38 if WIN:39 ALLOWED_IMPORTS.update(['ctypes', 'ctypes.wintypes', 'msvcrt', 'time',16 unmodified linesmodified:CHANGES.rst+2 added
53 unmodified lines54 - Option help text that is wrapped adds an extra newline at the end to55 distinguish it from the next option. :issue:`1075`56 - Consider ``sensible-editor`` when determining the editor to use for57 ``click.edit()``. :pr:`1469`58+- Arguments to system calls such as the executable path passed to59+ ``click.edit`` can contains spaces. :pr:`1470`60 61 62 Version 7.063 -----------540 unmodified lines