Fix get_wrapper_attr / set_wrapper_attr. (#1293)

Co-authored-by: Mark Towers <mark.m.towers@gmail.com>
This commit is contained in:
Alexis DUBURCQ
2025-01-17 23:46:45 +01:00
committed by GitHub
parent eaccbb57f1
commit 08a28d38da
2 changed files with 22 additions and 7 deletions

View File

@@ -432,18 +432,23 @@ class Wrapper(
name: The variable name
value: The new variable value
"""
sub_env = self.env
attr_set = False
sub_env = self
while attr_set is False and isinstance(sub_env, Wrapper):
# loop through all the wrappers, checking if it has the variable name then setting it
# otherwise stripping the wrapper to check the next.
# end when the core env is reached
while isinstance(sub_env, Wrapper):
if hasattr(sub_env, name):
setattr(sub_env, name, value)
attr_set = True
else:
sub_env = sub_env.env
return
if attr_set is False:
sub_env = sub_env.env
# check if the base environment has the wrapper, otherwise, we set it on the top (this) wrapper
if hasattr(sub_env, name):
setattr(sub_env, name, value)
else:
setattr(self, name, value)
def __str__(self):
"""Returns the wrapper name and the :attr:`env` representation string."""

View File

@@ -215,6 +215,16 @@ def test_get_set_wrapper_attr():
env.unwrapped._disable_render_order_enforcing
assert env.get_wrapper_attr("_disable_render_order_enforcing") is True
# Test with top-most wrapper
env.MY_ATTRIBUTE_1 = True
assert env.get_wrapper_attr("MY_ATTRIBUTE_1") is True
env.set_wrapper_attr("MY_ATTRIBUTE_1", False)
assert env.get_wrapper_attr("MY_ATTRIBUTE_1") is False
# Test with non-existing attribute
env.set_wrapper_attr("MY_ATTRIBUTE_2", True)
assert getattr(env, "MY_ATTRIBUTE_2") is True
class TestRandomSeeding:
@staticmethod