Skip to content

Commit

Permalink
Account for, and test, both pre and post 3009 behaviors
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
  • Loading branch information
s0undt3ch committed Dec 14, 2023
1 parent 260b5ed commit a4278fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
27 changes: 15 additions & 12 deletions salt/loader/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def __init__(
pack_self=None,
# Once we get rid of __utils__, the keyword arguments bellow should be removed
_ast_dunder_virtual_inspect=False,
_ast_dunder_virtual_deprecate_only=True,
_only_pack_properly_namespaced_functions=True,
): # pylint: disable=W0231
"""
Expand Down Expand Up @@ -278,6 +279,7 @@ def __init__(
self.loaded_base_name = loaded_base_name or LOADED_BASE_NAME
self.mod_type_check = mod_type_check or _mod_type
self._ast_dunder_virtual_inspect = _ast_dunder_virtual_inspect
self._ast_dunder_virtual_deprecate_only = _ast_dunder_virtual_deprecate_only
self._only_pack_properly_namespaced_functions = (
_only_pack_properly_namespaced_functions
)
Expand Down Expand Up @@ -700,18 +702,19 @@ def _load_module(self, name):
and not self._ast_dunder_virtual_defined(name, fpath)
):
func_decorator = _deprecated_dunder_utils_usage
# On 3010, un-commend the code below
# log.debug(
# "Not loading %r because it does not define a __virtual__ function",
# name,
# )
# return False

# On 3010, remove this code below
log.debug(
"Loading %r into __utils__ but this will stop working in Salt 3010",
name,
)
if self._ast_dunder_virtual_deprecate_only is True:
# Pre 3010 behavior
log.debug(
"Loading %r into __utils__ but this will stop working in Salt 3010",
name,
)
else:
# Post 3010 behavior
log.debug(
"Not loading %r because it does not define a __virtual__ function",
name,
)
return False

# if the fpath has `.cpython-3x` in it, but the running Py version
# is 3.y, the following will cause us to return immediately and we won't try to import this .pyc.
Expand Down
20 changes: 18 additions & 2 deletions tests/pytests/functional/loader/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ def loaded_base_name():
return random_string(f"{__name__}.", digits=False, uppercase=False)


def ast_dunder_virtual_deprecate_only_ids(value):
return f"ast_dunder_virtual_deprecate_only={value}"


@pytest.fixture(params=(True, False), ids=ast_dunder_virtual_deprecate_only_ids)
def ast_dunder_virtual_deprecate_only(request):
return request.param


@pytest.fixture
def loader(minion_opts, loaded_base_name):
def loader(request, minion_opts, loaded_base_name, ast_dunder_virtual_deprecate_only):
loader = salt.loader.utils(minion_opts, loaded_base_name=loaded_base_name)
loader._ast_dunder_virtual_deprecate_only = ast_dunder_virtual_deprecate_only
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Expand Down Expand Up @@ -46,4 +56,10 @@ def test_ast_inspect_loading(loader):
"process.",
)
for loaded_func in loaded_functions:
assert not loaded_func.startswith(modules_which_do_not_define_dunder_virtual)
if loader._ast_dunder_virtual_deprecate_only:
if loaded_func.startswith(modules_which_do_not_define_dunder_virtual):
assert hasattr(loader[loaded_func], "__wrapped__")
else:
assert not loaded_func.startswith(
modules_which_do_not_define_dunder_virtual
)

0 comments on commit a4278fe

Please sign in to comment.