Skip to content

Commit

Permalink
Use functools.lru_cache to cache extension point discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
cottsay committed Feb 7, 2024
1 parent 9854d54 commit 568ae80
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions colcon_core/extension_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions test/test_extension_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
):
Expand All @@ -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')
Expand All @@ -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'])
):
Expand Down

0 comments on commit 568ae80

Please sign in to comment.