Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-118761: Optimise import time forshlex#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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
1fd83f18811463bd6916a192329ecc130af4a640ceFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,25 +7,22 @@ | ||
| # iterator interface by Gustavo Niemeyer, April 2003. | ||
| # changes to tokenize more like Posix shells by Vinay Sajip, July 2016. | ||
JelleZijlstra marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
picnixz marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| __all__ = ["shlex", "split", "quote", "join"] | ||
| 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): | ||
| from io import StringIO # deferred import for performance | ||
| instream = StringIO(instream) | ||
| if instream is not None: | ||
| self.instream = instream | ||
| self.infile = infile | ||
| else: | ||
| import sys # deferred import for performance | ||
| self.instream = sys.stdin | ||
| self.infile = None | ||
| self.posix = posix | ||
| @@ -78,6 +75,7 @@ def push_token(self, tok): | ||
| def push_source(self, newstream, newfile=None): | ||
| "Push an input source onto the lexer's input source stack." | ||
| if isinstance(newstream, str): | ||
| from io import StringIO # deferred import for performance | ||
| newstream = StringIO(newstream) | ||
| self.filestack.appendleft((self.infile, self.instream, self.lineno)) | ||
| self.infile = newfile | ||
| @@ -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,7 +317,14 @@ def join(split_command): | ||
| return ' '.join(quote(arg) for arg in split_command) | ||
| def _find_unsafe(s, /): | ||
| # this function replaces itself with the compiled pattern on execution, | ||
| # to allow as deferred import of re for performance | ||
| global _find_unsafe | ||
| import re | ||
| _find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search | ||
AA-Turner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return _find_unsafe(s) | ||
| def quote(s): | ||
| """Return a shell-escaped version of the string *s*.""" | ||
| @@ -337,6 +343,7 @@ def _print_tokens(lexer): | ||
| print("Token: " + repr(tt)) | ||
| if __name__ == '__main__': | ||
| import sys # deferred import for performance | ||
| if len(sys.argv) == 1: | ||
| _print_tokens(shlex()) | ||
| else: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Improve import times by up to 33x for the :mod:`shlex` module. Patch by Adam | ||
| Turner. |
Uh oh!
There was an error while loading.Please reload this page.