Skip to content

Remove import re from script template #13166

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/13165.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speed up small CLI tools by removing ``import re`` from the executable template.
14 changes: 14 additions & 0 deletions src/pip/_internal/operations/install/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
import shutil
import sys
import textwrap
import warnings
from base64 import urlsafe_b64encode
from email.message import Message
Expand Down Expand Up @@ -412,6 +413,19 @@ def _raise_for_invalid_entrypoint(specification: str) -> None:


class PipScriptMaker(ScriptMaker):
# Override distlib's default script template with one that
# doesn't import `re` module, allowing scripts to load faster.
script_template = textwrap.dedent(
"""\
import sys
from %(module)s import %(import_name)s
if __name__ == '__main__':
if sys.argv[0].endswith('.exe'):
sys.argv[0] = sys.argv[0][:-4]
sys.exit(%(func)s())
"""
)

def make(
self, specification: str, options: Optional[Dict[str, Any]] = None
) -> List[str]:
Expand Down