-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-119670: Add force keyword only argument to shlex.quote
#148846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ddbe3a5
0e722d6
fd4af18
78b6f3b
762999d
2a301a5
f470aa1
5450159
bcd8b63
d000ccd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,12 +44,15 @@ The :mod:`!shlex` module defines the following functions: | |||||
| .. versionadded:: 3.8 | ||||||
|
|
||||||
|
|
||||||
| .. function:: quote(s) | ||||||
| .. function:: quote(s, *, force=False) | ||||||
|
|
||||||
| Return a shell-escaped version of the string *s*. The returned value is a | ||||||
| string that can safely be used as one token in a shell command line, for | ||||||
| cases where you cannot use a list. | ||||||
|
|
||||||
| If *force* is :const:`True` then *s* will be quoted even if it is already | ||||||
| safe for a shell without being quoted. | ||||||
|
|
||||||
| .. _shlex-quote-warning: | ||||||
|
|
||||||
| .. warning:: | ||||||
|
|
@@ -91,8 +94,23 @@ The :mod:`!shlex` module defines the following functions: | |||||
| >>> command | ||||||
| ['ls', '-l', 'somefile; rm -rf ~'] | ||||||
|
|
||||||
| The *force* keyword can be used to produce consistent behavior when | ||||||
| escaping multiple strings: | ||||||
|
|
||||||
| >>> from shlex import quote | ||||||
| >>> filenames = ['my first file', 'file2', 'file 3'] | ||||||
| >>> filenames_some_escaped = [quote(f, force=False) for f in filenames] | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| >>> filenames_some_escaped | ||||||
| ["'my first file'", 'file2', "'file 3'"] | ||||||
| >>> filenames_all_escaped = [quote(f, force=True) for f in filenames] | ||||||
| >>> filenames_all_escaped | ||||||
| ["'my first file'", "'file2'", "'file 3'"] | ||||||
|
|
||||||
| .. versionadded:: 3.3 | ||||||
|
|
||||||
| .. versionchanged:: next | ||||||
| The *force* keyword was added. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| The :mod:`!shlex` module defines the following class: | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1739,6 +1739,15 @@ New deprecations | |||||||||||
| Hugo van Kemenade in :gh:`148100`.) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| * :mod:`shlex`: | ||||||||||||
|
|
||||||||||||
| * :func:`shlex.quote` has a new keyword-only parameter *force* that ensures | ||||||||||||
| a string will always be quoted, even if it is already safe for a shell | ||||||||||||
| without being quoted. | ||||||||||||
|
Comment on lines
+1744
to
+1746
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| (Contributed by Jay Berry in :gh:`148846`.) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| * :mod:`struct`: | ||||||||||||
|
|
||||||||||||
| * Calling the ``Struct.__new__()`` without required argument now is | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -317,8 +317,12 @@ def join(split_command): | |||||
| return ' '.join(quote(arg) for arg in split_command) | ||||||
|
|
||||||
|
|
||||||
| def quote(s): | ||||||
| """Return a shell-escaped version of the string *s*.""" | ||||||
| def quote(s, *, force=False): | ||||||
| """Return a shell-escaped version of the string *s*. | ||||||
|
|
||||||
| If *force* is *True* then *s* will be quoted even if it is | ||||||
| already safe for a shell without being quoted. | ||||||
|
Comment on lines
+323
to
+324
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the docstring with the updated doc I suggested as well. |
||||||
| """ | ||||||
| if not s: | ||||||
| return "''" | ||||||
|
|
||||||
|
|
@@ -329,8 +333,10 @@ def quote(s): | |||||
| safe_chars = (b'%+,-./0123456789:=@' | ||||||
| b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_' | ||||||
| b'abcdefghijklmnopqrstuvwxyz') | ||||||
| # No quoting is needed if `s` is an ASCII string consisting only of `safe_chars` | ||||||
| if s.isascii() and not s.encode().translate(None, delete=safe_chars): | ||||||
| if (not force | ||||||
| and s.isascii() and not s.encode().translate(None, delete=safe_chars)): | ||||||
| # No quoting is needed if we're not forcing quoting | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| # and `s` is an ASCII string consisting only of `safe_chars` | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return s | ||||||
|
|
||||||
| # use single quotes, and put single quotes into double quotes | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -342,6 +342,13 @@ def testQuote(self): | |
| self.assertRaises(TypeError, shlex.quote, 42) | ||
| self.assertRaises(TypeError, shlex.quote, b"abc") | ||
|
|
||
| def testForceQuote(self): | ||
| self.assertEqual(shlex.quote("spam"), "spam") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some tests where the input has quotes inside. |
||
| self.assertEqual(shlex.quote("spam", force=False), "spam") | ||
| self.assertEqual(shlex.quote("spam", force=True), "'spam'") | ||
| self.assertEqual(shlex.quote("spam eggs", force=False), "'spam eggs'") | ||
| self.assertEqual(shlex.quote("spam eggs", force=True), "'spam eggs'") | ||
|
|
||
| def testJoin(self): | ||
| for split_command, command in [ | ||
| (['a ', 'b'], "'a ' b"), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add *force* keyword only argument to :func:`shlex.quote` to always quote the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the NEWS entry with the updated What's New as well. |
||
| string passed to it, even if it is already safe for a shell without being | ||
| quoted. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.