From e175125b75727971a72990fec6e96d63f21235de Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Tue, 14 Jan 2025 20:05:56 +0200 Subject: [PATCH 1/4] Remove `import re` from script template --- src/pip/_internal/operations/install/wheel.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/pip/_internal/operations/install/wheel.py b/src/pip/_internal/operations/install/wheel.py index aef42aa9eef..3bc8da42ddf 100644 --- a/src/pip/_internal/operations/install/wheel.py +++ b/src/pip/_internal/operations/install/wheel.py @@ -412,6 +412,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 = r"""# -*- coding: utf-8 -*- +import sys +from %(module)s import %(import_name)s +if __name__ == '__main__': + if sys.argv[0].endswith('-script.pyw'): + sys.argv[0] = sys.argv[0][: -11] + elif 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]: From 9b993f0084b5c04b946a36d6f844b50b7fa37f01 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Tue, 14 Jan 2025 20:20:54 +0200 Subject: [PATCH 2/4] Add NEWS --- news/13165.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/13165.feature.rst diff --git a/news/13165.feature.rst b/news/13165.feature.rst new file mode 100644 index 00000000000..e21ddac50ad --- /dev/null +++ b/news/13165.feature.rst @@ -0,0 +1 @@ +Speed up small CLI tools by removing ``import re`` from the executable template. From 1d49aa3b7fb930d5923f8c6d03fea51ad9f38aa1 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Wed, 15 Jan 2025 01:17:46 +0200 Subject: [PATCH 3/4] Address PR review --- src/pip/_internal/operations/install/wheel.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pip/_internal/operations/install/wheel.py b/src/pip/_internal/operations/install/wheel.py index 3bc8da42ddf..da3b5bd8125 100644 --- a/src/pip/_internal/operations/install/wheel.py +++ b/src/pip/_internal/operations/install/wheel.py @@ -418,10 +418,8 @@ class PipScriptMaker(ScriptMaker): import sys from %(module)s import %(import_name)s if __name__ == '__main__': - if sys.argv[0].endswith('-script.pyw'): - sys.argv[0] = sys.argv[0][: -11] - elif sys.argv[0].endswith('.exe'): - sys.argv[0] = sys.argv[0][: -4] + if sys.argv[0].endswith('.exe'): + sys.argv[0] = sys.argv[0][:-4] sys.exit(%(func)s()) """ From 88cb374fe9e32c1a66d65722952866e5582a5e3f Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Thu, 16 Jan 2025 08:27:15 +0200 Subject: [PATCH 4/4] Remove legacy file encoding declaration --- src/pip/_internal/operations/install/wheel.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pip/_internal/operations/install/wheel.py b/src/pip/_internal/operations/install/wheel.py index da3b5bd8125..0e94cb80678 100644 --- a/src/pip/_internal/operations/install/wheel.py +++ b/src/pip/_internal/operations/install/wheel.py @@ -414,8 +414,7 @@ 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 -*- -import sys + script_template = r"""import sys from %(module)s import %(import_name)s if __name__ == '__main__': if sys.argv[0].endswith('.exe'):