Skip to content
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

Remove import re from script template #13166

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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.
11 changes: 11 additions & 0 deletions src/pip/_internal/operations/install/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ 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 = r"""# -*- coding: utf-8 -*-
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so.

I also wonder if it’d be worthwhile to conditionally use removesuffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given pip doesn't work with Python 2 anymore # -- coding: utf-8 -- can be removed right?

I think so too, I can remove it. My initial thought was to omit any fancy improvements from this PR, and simply replace the re.sub call with something that mimics it closely, so if distlib updates the template for some reason, it may be easier to track and copy the changes here.

I also wonder if it’d be worthwhile to conditionally use removesuffix.

It could be used, and could be used unconditionally, now that there is only one suffix to remove in this edit. Pip still supports Python 3.8 and removesuffix was added in 3.9, so we're not there yet.

One micro-optimization that can also be done is replace sys.exit with raise SystemExit to avoid one function call, but that may be a bit too much :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One micro-optimization that can also be done is replace sys.exit with raise SystemExit to avoid one function call, but that may be a bit too much :D

It appears to be to a C function though, so it may not be a clear win, especially given sys needs to be imported anyway.

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
Loading