chameleon
chameleon copied to clipboard
"chameleon.utils.Scope": "dict" methods unreliable in presence of "global" variables
Working on the integration of zope.tales
expressions with chameleon
, I noticed that some expressions use Scope.vars.get
. Like other dict
methods, this method knows nothing about _root
and therefore may miss (some) variables defined with Scope.set_global
.
>>> from chameleon.utils import Scope
>>> t = Scope()
>>> l = t.copy()
>>> l.setGlobal("a", "A")
>>> l.get("a")
>>>
I'm not sure I'm completely understanding what needs to be done to fix this.
Malthe Borch wrote at 2020-3-29 00:13 -0700:
I'm not sure I'm completely understanding what needs to be done to fix this.
One possibility:
def get(self, key, default=None):
try: return self[key]
except NameError: return default
def __contains__(self, key):
try: self[key]
except NameError: return False
else: return True
I am not sure whether the other methods are important. If so,
keys
could be defined by (for Python 3; for Python 2, the yield from
must be replaced and super
needs parameters and maybe, we
want to ensure that the methods return list
s):
def keys(self):
yield from super().keys()
if hasattr(self, "_root"):
for k in self._root.keys():
if not super().__contains__(k): yield k
values
and items
could by defined in terms of keys
and
__getitem__
.
@d-maurer did you have a chance to look at the pull request that addresses this? ^^^
Malthe Borch wrote at 2022-4-5 22:19 -0700: @.*** did you have a chance to look at the pull request that addresses this? ^^^
I missed that you would like me to review.
It can still take some time (I have other tasks at the moment).
This should be fixed with #306.