From 2b700a51ebb35f2126a69bfc0cad56a489e3831a Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 14 Mar 2022 12:44:07 -0400 Subject: [PATCH] fix 20ms startup penalty added by virtualenv --- docs/changelog/2317.bugfix.rst | 1 + docs/changelog/examples.rst | 6 +++--- src/virtualenv/create/via_global_ref/_virtualenv.py | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) create mode 100644 docs/changelog/2317.bugfix.rst diff --git a/docs/changelog/2317.bugfix.rst b/docs/changelog/2317.bugfix.rst new file mode 100644 index 000000000..6d71302d2 --- /dev/null +++ b/docs/changelog/2317.bugfix.rst @@ -0,0 +1 @@ +Improve performance of python startup inside created virtualenvs - by :user:`asottile`. diff --git a/docs/changelog/examples.rst b/docs/changelog/examples.rst index 575339553..eda79eb70 100644 --- a/docs/changelog/examples.rst +++ b/docs/changelog/examples.rst @@ -2,17 +2,17 @@ file ``544.doc.rst``:: - explain everything much better - by ``passionate_technicalwriter`` + explain everything much better - by :user:`passionate_technicalwriter`. file ``544.feature.rst``:: - ``tox --version`` now shows information about all registered plugins - by ``obestwalter`` + ``tox --version`` now shows information about all registered plugins - by :user:`obestwalter`. file ``571.bugfix.rst``:: ``skip_install`` overrides ``usedevelop`` (``usedevelop`` is an option to choose the installation type if the package is installed and ``skip_install`` determines if it should be - installed at all) - by ``ferdonline`` + installed at all) - by :user:`ferdonline`. .. see pyproject.toml for all available categories diff --git a/src/virtualenv/create/via_global_ref/_virtualenv.py b/src/virtualenv/create/via_global_ref/_virtualenv.py index da98b827a..6c51ca0d9 100644 --- a/src/virtualenv/create/via_global_ref/_virtualenv.py +++ b/src/virtualenv/create/via_global_ref/_virtualenv.py @@ -37,11 +37,8 @@ def parse_config_files(self, *args, **kwargs): _DISTUTILS_PATCH = "distutils.dist", "setuptools.dist" if sys.version_info > (3, 4): # https://docs.python.org/3/library/importlib.html#setting-up-an-importer - from functools import partial - from importlib.abc import MetaPathFinder - from importlib.util import find_spec - class _Finder(MetaPathFinder): + class _Finder: """A meta path finder that allows patching the imported distutils modules""" fullname = None @@ -65,6 +62,9 @@ def find_spec(self, fullname, path, target=None): # https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe self.lock.append(lock) + from functools import partial + from importlib.util import find_spec + with self.lock[0]: self.fullname = fullname try: