From 568ae80d2ee7de0523f6fd6a7e78f37a6b40a191 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Wed, 7 Feb 2024 15:52:56 -0600 Subject: [PATCH] Use functools.lru_cache to cache extension point discovery --- colcon_core/extension_point.py | 4 ++++ test/test_extension_point.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/colcon_core/extension_point.py b/colcon_core/extension_point.py index c724d221..6d61e27d 100644 --- a/colcon_core/extension_point.py +++ b/colcon_core/extension_point.py @@ -3,6 +3,7 @@ # Licensed under the Apache License, Version 2.0 from collections import defaultdict +from functools import lru_cache import os import traceback @@ -46,6 +47,7 @@ def _get_unique_distributions(): yield dist +@lru_cache def get_all_extension_points(): """ Get all extension points related to `colcon` and any of its extensions. @@ -78,6 +80,7 @@ def get_all_extension_points(): return entry_points +@lru_cache def get_extension_points(group): """ Get the extension points for a specific group. @@ -136,6 +139,7 @@ def load_extension_points(group, *, excludes=None): return extension_types +@lru_cache def load_extension_point(name, value, group): """ Load the extension point. diff --git a/test/test_extension_point.py b/test/test_extension_point.py index 96e58a0d..72c37362 100644 --- a/test/test_extension_point.py +++ b/test/test_extension_point.py @@ -101,6 +101,7 @@ def test_extension_point_blocklist(): # successful loading of entry point not in blocklist load.reset_mock() + load_extension_point.cache_clear() with EnvironmentContext(COLCON_EXTENSION_BLOCKLIST=os.pathsep.join([ 'group1.extB', 'group2.extC']) ): @@ -109,6 +110,7 @@ def test_extension_point_blocklist(): # entry point in a blocked group can't be loaded load.reset_mock() + load_extension_point.cache_clear() with EnvironmentContext(COLCON_EXTENSION_BLOCKLIST='group1'): with pytest.raises(RuntimeError) as e: load_extension_point('extA', 'eA', 'group1') @@ -117,6 +119,7 @@ def test_extension_point_blocklist(): assert load.call_count == 0 # entry point listed in the blocklist can't be loaded + load_extension_point.cache_clear() with EnvironmentContext(COLCON_EXTENSION_BLOCKLIST=os.pathsep.join([ 'group1.extA', 'group1.extB']) ):