gh-119670: Add 'always' keyword argument to shlex.quote#119674
gh-119670: Add 'always' keyword argument to shlex.quote#119674jb2170 wants to merge 11 commits intopython:mainfrom
shlex.quote#119674Conversation
Removes early return for the special case of an empty string
vstinner
left a comment
There was a problem hiding this comment.
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.
picnixz
left a comment
There was a problem hiding this comment.
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.
| :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*. |
| * 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.
* 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`.)| 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.
| 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
|
|
I only asked about the parameter name |
|
Could you:
|
|
It seems like @jb2170 is inactive for one year. Sadly, I suggest closing the PR and the related issue. |
|
I'm still here, I'll give it a second look 😅 |
|
Ping @jb2170. |
|
This PR is stale because it has been open for 30 days with no activity. |
|
I'll do a fresh rebase / branch / PR since this PR is very much behind It's coming along on my branch shlex-quote-force. I suggest I want to talk about this behavior of I'll check my work and open a PR for shlex-quote-force later today. |
|
Superseded by #148846 |
Addresses #119670
📚 Documentation preview 📚: https://cpython-previews--119674.org.readthedocs.build/