Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-118761: Optimise import time for shlex#132036
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits Select commit Hold shift + click to select a range
1fd83f1 Optimise import time for ``shlex``
AA-Turner 8811463 Revert deferral of sys and io
AA-Turner bd6916a Switch to bytes.translate() based approach
AA-Turner 192329e Update Lib/shlex.py
AA-Turner cc130af Merge branch 'main' into opt-shlex
AA-Turner 4a640ce Add test_lazy_imports
AA-Turner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,11 +7,7 @@ | ||
| # iterator interface by Gustavo Niemeyer, April 2003. | ||
| # changes to tokenize more like Posix shells by Vinay Sajip, July 2016. | ||
| import os | ||
| import re | ||
| import sys | ||
| from collections import deque | ||
picnixz marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| from io import StringIO | ||
| __all__ = ["shlex", "split", "quote", "join"] | ||
| @@ -20,6 +16,8 @@ class shlex: | ||
| "A lexical analyzer class for simple shell-like syntaxes." | ||
| def __init__(self, instream=None, infile=None, posix=False, | ||
| punctuation_chars=False): | ||
| from collections import deque # deferred import for performance | ||
| if isinstance(instream, str): | ||
| instream = StringIO(instream) | ||
| if instream is not None: | ||
| @@ -278,6 +276,7 @@ def read_token(self): | ||
| def sourcehook(self, newfile): | ||
| "Hook called on a filename to be sourced." | ||
| import os.path | ||
| if newfile[0] == '"': | ||
| newfile = newfile[1:-1] | ||
| # This implements cpp-like semantics for relative-path inclusion. | ||
| @@ -318,13 +317,17 @@ def join(split_command): | ||
| return ' '.join(quote(arg) for arg in split_command) | ||
| _find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search | ||
| def quote(s): | ||
| """Return a shell-escaped version of the string *s*.""" | ||
| if not s: | ||
| return "''" | ||
| if _find_unsafe(s) is None: | ||
| # Use bytes.translate() for performance | ||
| 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): | ||
| return s | ||
| # use single quotes, and put single quotes into double quotes | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions 3 Misc/NEWS.d/next/Library/2025-04-03-00-56-48.gh-issue-118761.Vb0S1B.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Improve import times by up to 33x for the :mod:`shlex` module, | ||
| and improve the performance of :func:`shlex.quote` by up to 12x. | ||
| Patch by Adam Turner. |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.