Skip to content

Commit cf6daed

Browse files
committed
Allow NamedLoaderContexts to be returned from loader
1 parent c1a5aae commit cf6daed

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
From 1be3f92ef3bf14e47340e2e075291204b3e75e98 Mon Sep 17 00:00:00 2001
2+
From: Victor Zhestkov <[email protected]>
3+
Date: Wed, 25 Sep 2024 14:07:42 +0300
4+
Subject: [PATCH] Allow NamedLoaderContexts to be returned from loader
5+
6+
It is useful in some cases to return NamedLoaderContexts from loaded
7+
functions. Instead of choking or requireing implimenters to call the
8+
context's value() method before being de-scoped, detect when a
9+
NamedLoaderContext has been returned and return the value from the
10+
current context.
11+
12+
Co-authored-by: Daniel A. Wozniak <[email protected]>
13+
---
14+
salt/loader/lazy.py | 5 ++++-
15+
tests/pytests/integration/modules/test_config.py | 8 ++++++++
16+
tests/pytests/unit/loader/test_loader.py | 13 +++++++++++++
17+
3 files changed, 25 insertions(+), 1 deletion(-)
18+
create mode 100644 tests/pytests/integration/modules/test_config.py
19+
20+
diff --git a/salt/loader/lazy.py b/salt/loader/lazy.py
21+
index 5de995d446..b7fd97f0e1 100644
22+
--- a/salt/loader/lazy.py
23+
+++ b/salt/loader/lazy.py
24+
@@ -1246,7 +1246,10 @@ class LazyLoader(salt.utils.lazy.LazyDict):
25+
self.parent_loader = current_loader
26+
token = salt.loader.context.loader_ctxvar.set(self)
27+
try:
28+
- return _func_or_method(*args, **kwargs)
29+
+ ret = _func_or_method(*args, **kwargs)
30+
+ if isinstance(ret, salt.loader.context.NamedLoaderContext):
31+
+ ret = ret.value()
32+
+ return ret
33+
finally:
34+
self.parent_loader = None
35+
salt.loader.context.loader_ctxvar.reset(token)
36+
diff --git a/tests/pytests/integration/modules/test_config.py b/tests/pytests/integration/modules/test_config.py
37+
new file mode 100644
38+
index 0000000000..afdf470605
39+
--- /dev/null
40+
+++ b/tests/pytests/integration/modules/test_config.py
41+
@@ -0,0 +1,8 @@
42+
+import pytest
43+
+
44+
+
45+
+@pytest.mark.slow_test
46+
+def test_config_items(salt_cli, salt_minion):
47+
+ ret = salt_cli.run("config.items", minion_tgt=salt_minion.id)
48+
+ assert ret.returncode == 0
49+
+ assert isinstance(ret.data, dict)
50+
diff --git a/tests/pytests/unit/loader/test_loader.py b/tests/pytests/unit/loader/test_loader.py
51+
index 86348749db..aba605f42a 100644
52+
--- a/tests/pytests/unit/loader/test_loader.py
53+
+++ b/tests/pytests/unit/loader/test_loader.py
54+
@@ -62,3 +62,16 @@ def test_raw_mod_functions():
55+
ret = salt.loader.raw_mod(opts, "grains", "get")
56+
for k, v in ret.items():
57+
assert isinstance(v, salt.loader.lazy.LoadedFunc)
58+
+
59+
+
60+
+def test_return_named_context_from_loaded_func(tmp_path):
61+
+ opts = {
62+
+ "optimization_order": [0],
63+
+ }
64+
+ contents = """
65+
+ def foobar():
66+
+ return __test__
67+
+ """
68+
+ with pytest.helpers.temp_file("mymod.py", contents, directory=tmp_path):
69+
+ loader = salt.loader.LazyLoader([tmp_path], opts, pack={"__test__": "meh"})
70+
+ assert loader["mymod.foobar"]() == "meh"
71+
--
72+
2.46.1
73+

salt/salt.spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ Patch138: prevent-using-syncwrapper-with-no-reason.patch
442442
Patch139: use-cachedir-for-extension_modules-in-salt-call-bsc-.patch
443443
# PATCH-FIX_OPENSUSE: https://github.com/openSUSE/salt/pull/680
444444
Patch140: revert-the-change-making-reactor-less-blocking-bsc-1.patch
445+
# PATCH-FIX_UPSTREAM: https://github.com/saltstack/salt/pull/66649
446+
Patch141: allow-namedloadercontexts-to-be-returned-from-loader.patch
445447

446448
### IMPORTANT: The line below is used as a snippet marker. Do not touch it.
447449
### SALT PATCHES LIST END

0 commit comments

Comments
 (0)