diff options
author | Mike Frysinger <vapier@google.com> | 2022-12-08 01:46:45 -0500 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2022-12-08 22:22:36 +0000 |
commit | 0ab6b1168811586b62d8d1c26bad118b0a4b3807 (patch) | |
tree | 31a34caaa840800b3f691cd6bd22419d14a93cfd | |
parent | a621254b263664efa308a645214d3d140e068676 (diff) | |
download | git-repo-0ab6b1168811586b62d8d1c26bad118b0a4b3807.tar.gz |
wrapper: switch to functools.lru_cache
No need to implement our own caching logic with newer Python.
Change-Id: Idc3243b8e22ff020817b0a4f18c9b86b1222d631
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/354357
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
-rw-r--r-- | tests/test_wrapper.py | 2 | ||||
-rw-r--r-- | wrapper.py | 19 |
2 files changed, 9 insertions, 12 deletions
diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index 2a0e542b..ef879a5d 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py | |||
@@ -38,7 +38,7 @@ class RepoWrapperTestCase(unittest.TestCase): | |||
38 | 38 | ||
39 | def setUp(self): | 39 | def setUp(self): |
40 | """Load the wrapper module every time.""" | 40 | """Load the wrapper module every time.""" |
41 | wrapper._wrapper_module = None | 41 | wrapper.Wrapper.cache_clear() |
42 | self.wrapper = wrapper.Wrapper() | 42 | self.wrapper = wrapper.Wrapper() |
43 | 43 | ||
44 | 44 | ||
@@ -12,6 +12,7 @@ | |||
12 | # See the License for the specific language governing permissions and | 12 | # See the License for the specific language governing permissions and |
13 | # limitations under the License. | 13 | # limitations under the License. |
14 | 14 | ||
15 | import functools | ||
15 | import importlib.machinery | 16 | import importlib.machinery |
16 | import importlib.util | 17 | import importlib.util |
17 | import os | 18 | import os |
@@ -21,15 +22,11 @@ def WrapperPath(): | |||
21 | return os.path.join(os.path.dirname(__file__), 'repo') | 22 | return os.path.join(os.path.dirname(__file__), 'repo') |
22 | 23 | ||
23 | 24 | ||
24 | _wrapper_module = None | 25 | @functools.lru_cache(maxsize=None) |
25 | |||
26 | |||
27 | def Wrapper(): | 26 | def Wrapper(): |
28 | global _wrapper_module | 27 | modname = 'wrapper' |
29 | if not _wrapper_module: | 28 | loader = importlib.machinery.SourceFileLoader(modname, WrapperPath()) |
30 | modname = 'wrapper' | 29 | spec = importlib.util.spec_from_loader(modname, loader) |
31 | loader = importlib.machinery.SourceFileLoader(modname, WrapperPath()) | 30 | module = importlib.util.module_from_spec(spec) |
32 | spec = importlib.util.spec_from_loader(modname, loader) | 31 | loader.exec_module(module) |
33 | _wrapper_module = importlib.util.module_from_spec(spec) | 32 | return module |
34 | loader.exec_module(_wrapper_module) | ||
35 | return _wrapper_module | ||