-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
gh-119670: Add 'always' keyword argument to shlex.quote
#119674
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?
Conversation
Removes early return for the special case of an empty string
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.
Thanks for your interesting change.
Please add tests to Lib/test/test_shlex.py. Also, you can copy the NEWS entry in Doc/whatsnew/3.14.rst, in a new "shlex" sub-section of Improved Modules.
The first assertTrue test checks all strings now start with a single quote, ie they have actually been escaped The second test of assertFalse checks that not all the strings have been escaped when always=False (the default value), since the first, third, and fifth string do not need escaping unless requested.
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.
LGTM
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.
Personally, I'd prefer "force" rather than "always" but that's bike-shedding (using "force" is more common when you want to force a behaviour I think).
:func:`shlex.quote` now has an *always* keyword argument for forcing | ||
the escaping of the string passed to it |
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.
:func:`shlex.quote` now has an *always* keyword argument for forcing | |
the escaping of the string passed to it | |
Allow :func:`shlex.quote` to unconditionally escape its input | |
via the keyword-only argument *always*. |
Doc/whatsnew/3.14.rst
Outdated
* Add keyword-only argument of *always* to :func:`shlex.quote` for forcing | ||
the escaping of the string passed to it. |
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.
* Allow :func:`shlex.quote` to unconditionally escape its input
via ``shlex.quote(string, always=True)``.
(Contributed by YOUR_NAME_OR_GITHUB_NICKNAME in :gh:`119670`.)
Doc/library/shlex.rst
Outdated
If the *always* keyword argument is set to ``True`` then the string *s* | ||
will always be escaped, even in the absence of special characters. This is | ||
nice for uniformity, for example when escaping a list of strings. |
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.
If the *always* keyword argument is set to ``True`` then the string *s* | |
will always be escaped, even in the absence of special characters. This is | |
nice for uniformity, for example when escaping a list of strings. | |
If *always* is ``True``, then the string *s* is | |
unconditionally escaped, even in the absence of | |
special characters. This is typically useful for | |
uniformly escaping a list of strings: |
Oh, I also like |
I could be swayed;
Changing the behaviour to always quote its input would be ideal in my eyes! It requires tweaking a couple of the tests in At this point however I question "who would ever want def quote(s):
return "'" + s.replace("'", "'\"'\"'") + "'" This simplifies Perhaps my main issue is that
|
(docs test failed but no new documentation needed) |
I only asked about the parameter name |
Could you:
|
Addresses #119670
📚 Documentation preview 📚: https://cpython-previews--119674.org.readthedocs.build/