chameleon icon indicating copy to clipboard operation
chameleon copied to clipboard

"chameleon.utils.Scope": "dict" methods unreliable in presence of "global" variables

Open d-maurer opened this issue 4 years ago • 4 comments

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")
>>> 

d-maurer avatar Mar 29 '20 06:03 d-maurer

I'm not sure I'm completely understanding what needs to be done to fix this.

malthe avatar Mar 29 '20 07:03 malthe

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 lists):

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 avatar Mar 29 '20 16:03 d-maurer

@d-maurer did you have a chance to look at the pull request that addresses this? ^^^

malthe avatar Apr 06 '22 05:04 malthe

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).

d-maurer avatar Apr 06 '22 05:04 d-maurer

This should be fixed with #306.

malthe avatar Dec 12 '23 17:12 malthe