Skip to content

Commit a5cd068

Browse files
authored
Merge pull request #13166 from hukkin/rm-re-import
Remove `import re` from script template
2 parents f51b434 + 530f7f1 commit a5cd068

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

news/13165.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up small CLI tools by removing ``import re`` from the executable template.

src/pip/_internal/operations/install/wheel.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import re
1313
import shutil
1414
import sys
15+
import textwrap
1516
import warnings
1617
from base64 import urlsafe_b64encode
1718
from collections.abc import Generator, Iterable, Iterator, Sequence
@@ -403,6 +404,19 @@ def _raise_for_invalid_entrypoint(specification: str) -> None:
403404

404405

405406
class PipScriptMaker(ScriptMaker):
407+
# Override distlib's default script template with one that
408+
# doesn't import `re` module, allowing scripts to load faster.
409+
script_template = textwrap.dedent(
410+
"""\
411+
import sys
412+
from %(module)s import %(import_name)s
413+
if __name__ == '__main__':
414+
if sys.argv[0].endswith('.exe'):
415+
sys.argv[0] = sys.argv[0][:-4]
416+
sys.exit(%(func)s())
417+
"""
418+
)
419+
406420
def make(
407421
self, specification: str, options: dict[str, Any] | None = None
408422
) -> list[str]:

0 commit comments

Comments
 (0)