Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit06a26fd

Browse files
authored
gh-118761: Optimise import time forshlex (#132036)
1 parent984a314 commit06a26fd

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

‎Lib/shlex.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
# iterator interface by Gustavo Niemeyer, April 2003.
88
# changes to tokenize more like Posix shells by Vinay Sajip, July 2016.
99

10-
importos
11-
importre
1210
importsys
13-
fromcollectionsimportdeque
14-
1511
fromioimportStringIO
1612

1713
__all__= ["shlex","split","quote","join"]
@@ -20,6 +16,8 @@ class shlex:
2016
"A lexical analyzer class for simple shell-like syntaxes."
2117
def__init__(self,instream=None,infile=None,posix=False,
2218
punctuation_chars=False):
19+
fromcollectionsimportdeque# deferred import for performance
20+
2321
ifisinstance(instream,str):
2422
instream=StringIO(instream)
2523
ifinstreamisnotNone:
@@ -278,6 +276,7 @@ def read_token(self):
278276

279277
defsourcehook(self,newfile):
280278
"Hook called on a filename to be sourced."
279+
importos.path
281280
ifnewfile[0]=='"':
282281
newfile=newfile[1:-1]
283282
# This implements cpp-like semantics for relative-path inclusion.
@@ -318,13 +317,17 @@ def join(split_command):
318317
return' '.join(quote(arg)forarginsplit_command)
319318

320319

321-
_find_unsafe=re.compile(r'[^\w@%+=:,./-]',re.ASCII).search
322-
323320
defquote(s):
324321
"""Return a shell-escaped version of the string *s*."""
325322
ifnots:
326323
return"''"
327-
if_find_unsafe(s)isNone:
324+
325+
# Use bytes.translate() for performance
326+
safe_chars= (b'%+,-./0123456789:=@'
327+
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'
328+
b'abcdefghijklmnopqrstuvwxyz')
329+
# No quoting is needed if `s` is an ASCII string consisting only of `safe_chars`
330+
ifs.isascii()andnots.encode().translate(None,delete=safe_chars):
328331
returns
329332

330333
# use single quotes, and put single quotes into double quotes

‎Lib/test/test_shlex.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importshlex
44
importstring
55
importunittest
6+
fromtest.supportimportimport_helper
67

78

89
# The original test data set was from shellwords, by Hartmut Goebel.
@@ -363,6 +364,9 @@ def testPunctuationCharsReadOnly(self):
363364
withself.assertRaises(AttributeError):
364365
shlex_instance.punctuation_chars=False
365366

367+
deftest_lazy_imports(self):
368+
import_helper.ensure_lazy_imports('shlex', {'collections','re','os'})
369+
366370

367371
# Allow this test to be used with old shlex.py
368372
ifnotgetattr(shlex,"split",None):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve import times by up to 33x for the:mod:`shlex` module,
2+
and improve the performance of:func:`shlex.quote` by up to 12x.
3+
Patch by Adam Turner.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp