summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Watson <cjwatson@canonical.com>2022-08-07 13:44:07 +0100
committerColin Watson <cjwatson@canonical.com>2022-08-07 13:44:07 +0100
commit859456919f9df4acb5e902e2b88cc4e612a3024c (patch)
treeae6248ad179d6c19767091b824c4b089948ea98d
parent4d490966549c43e56a6a5aaf17955922ad15ad18 (diff)
downloadzope-tales-859456919f9df4acb5e902e2b88cc4e612a3024c.tar.gz
Fix "Invalid variable name" error for base of path
If you were aiming for this TAL fragment: <p class="error message" tal:condition="view/error_message" tal:content="structure view/error_message/escapedtext" /> ... but instead mistype it as this: <p class="error message" tal:condition="view/error_message" tal:content="structured view/error_message/escapedtext" /> ... then you get this very confusing exception: zope.tal.taldefs.TALError: Invalid variable name "escapedtext" in expression 'structured view/error_message/escapedtext', at ... But "escapedtext" is not the invalid name here; it's "structured view", because the typo of "structured" for "structure" means that `zope.tal` doesn't interpret that part of the attribute before passing it on to `zope.tales` to evaluate the path expression. Fix the incorrect error message.
-rw-r--r--CHANGES.rst3
-rw-r--r--src/zope/tales/expressions.py2
-rw-r--r--src/zope/tales/tests/test_expressions.py7
3 files changed, 10 insertions, 2 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 9bd9497..e7ec251 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -7,6 +7,9 @@
- Add support for Python 3.9, 3.10.
+- Fix error message raised if the first element of a path expression is not
+ a valid name.
+
5.1 (2020-07-06)
================
diff --git a/src/zope/tales/expressions.py b/src/zope/tales/expressions.py
index dd8980c..18db2b1 100644
--- a/src/zope/tales/expressions.py
+++ b/src/zope/tales/expressions.py
@@ -124,7 +124,7 @@ class SubPathExpr(object):
base = first[0]
if base and not _valid_name(base):
raise engine.getCompilerError()(
- 'Invalid variable name "%s"' % element)
+ 'Invalid variable name "%s"' % base)
self._base = base
compiledpath[0] = first[1:]
self._compiled_path = tuple(compiledpath)
diff --git a/src/zope/tales/tests/test_expressions.py b/src/zope/tales/tests/test_expressions.py
index 1653c29..9d62875 100644
--- a/src/zope/tales/tests/test_expressions.py
+++ b/src/zope/tales/tests/test_expressions.py
@@ -193,8 +193,13 @@ class TestParsedExpressions(ExpressionTestBase):
def test_dynamic_invalid_variable_name(self):
from zope.tales.tales import CompilerError
- with self.assertRaisesRegex(CompilerError, "Invalid variable name"):
+ with self.assertRaisesRegex(
+ CompilerError, 'Invalid variable name "123"'):
self.engine.compile('path:a/?123')
+ # Deliberate typo for the TAL construct "structure ...".
+ with self.assertRaisesRegex(
+ CompilerError, 'Invalid variable name "structured a"'):
+ self.engine.compile('structured a/b')
def testOldStyleClassIsCalled(self):
class AnOldStyleClass: