Skip to content

Commit

Permalink
Merge 49037e8 into b541ca9
Browse files Browse the repository at this point in the history
  • Loading branch information
d-maurer committed Aug 24, 2022
2 parents b541ca9 + 49037e8 commit 7b64762
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Expand Up @@ -5,6 +5,10 @@
5.2.0 (unreleased)
==================

- ``tales.Context.getValue`` now returns the innermost (not outermost)
value for the given variable. Fixes
`#19 <https://github.com/zopefoundation/zope.tales/issues/19>`_.

- Add support for Python 3.9, 3.10.

- Fix error message raised if the first element of a path expression is not
Expand Down
16 changes: 10 additions & 6 deletions src/zope/tales/tales.py
Expand Up @@ -744,12 +744,16 @@ def setGlobal(self, name, value):
vars[name] = value

def getValue(self, name, default=None):
value = default
for vars in self._vars_stack:
value = vars.get(name, default)
if value is not default:
break
return value
"""return the current value of variable *name* or *default*."""
# ``beginScope`` puts a copy of all variables into ``vars``
# and pushes it onto ``_vars_stack``,
# ``endScope`` pops the last element of ``vars_stack`` into ``vars``,
# ``setGlobal`` updates all variable bindings in ``_vars_stack``
# (and thereby, implicitly, ``vars``).
# Consequently, the current value of a variable can
# always be found in ``vars``
# (no need to iterate over ``_vars_stack``).
return self.vars.get(name, default)

def setRepeat(self, name, expr):
expr = self.evaluate(expr)
Expand Down
8 changes: 7 additions & 1 deletion src/zope/tales/tests/test_tales.py
Expand Up @@ -134,21 +134,27 @@ def testVariables(self):

c = ctxt.vars
self.assertEqual(c['v1'], 1, 'Variable "v1"')
self.assertEqual(ctxt.getValue('v1'), 1, 'Variable "v1"')

ctxt.beginScope()
ctxt.setLocal('v1', 3)
ctxt.setGlobal('g', 1)

c = ctxt.vars
self.assertEqual(c['v1'], 3, 'Inner scope')
self.assertEqual(ctxt.getValue('v1'), 3, 'Inner scope')
self.assertEqual(c['v2'], 2, 'Outer scope')
self.assertEqual(ctxt.getValue('v2'), 2, 'Outer scope')
self.assertEqual(c['g'], 1, 'Global')
self.assertEqual(ctxt.getValue('g'), 1, 'Global')

ctxt.endScope()

c = ctxt.vars
self.assertEqual(c['v1'], 1, "Uncovered local")
self.assertEqual(ctxt.getValue('v1'), 1, "Uncovered local")
self.assertEqual(c['g'], 1, "Global from inner scope")
self.assertEqual(ctxt.getValue('g'), 1, "Global from inner scope")

ctxt.endScope()

Expand Down Expand Up @@ -220,7 +226,7 @@ def test_getValue_nested(self):
self.context.vars['it'] = 1
self.context.beginScope()
self.context.vars['it'] = 2
self.assertEqual(self.context.getValue('it'), 1)
self.assertEqual(self.context.getValue('it'), 2)

def test_evaluate_boolean(self):
# Make sure it always returns a regular bool, no matter
Expand Down

0 comments on commit 7b64762

Please sign in to comment.