There is currently one remaining failure with Python 3.10.0a4 (see https://bugzilla.redhat.com/show_bug.cgi?id=1907442):
================================================= FAILURES =================================================
_______________________________________ test_undefined_native_return _______________________________________
env = <jinja2.nativetypes.NativeEnvironment object at 0x7ff4469a0d00>
def test_undefined_native_return(env):
t = env.from_string("{{ missing }}")
> assert isinstance(t.render(), Undefined)
tests/test_nativetypes.py:23:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
src/jinja2/nativetypes.py:92: in render
return self.environment.handle_exception()
src/jinja2/environment.py:790: in handle_exception
raise rewrite_traceback_stack(source=source)
src/jinja2/nativetypes.py:33: in native_concat
return literal_eval(raw)
/usr/lib64/python3.10/ast.py:108: in literal_eval
return _convert(node_or_string)
/usr/lib64/python3.10/ast.py:107: in _convert
return _convert_signed_num(node)
/usr/lib64/python3.10/ast.py:81: in _convert_signed_num
return _convert_num(node)
/usr/lib64/python3.10/ast.py:72: in _convert_num
_raise_malformed_node(node)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
node = Undefined
def _raise_malformed_node(node):
msg = "malformed node or string"
> if lno := getattr(node, 'lineno', None):
E jinja2.exceptions.UndefinedError: 'missing' is undefined
/usr/lib64/python3.10/ast.py:67: UndefinedError
Environment:
- Python version: 3.10.0a4
- Jinja version: 2.11.2
The failure is caused by a change introduced in python/cpython#23677 (see also https://bugs.python.org/issue28964): Lib/ast.py now calls gettattr(node, 'lineno', None) in _raise_malformed_node(node).
Now, the test case in question renders "{{ missing }}" and expects it to return a Jinja Undefined object. However, the Jinja Undefined object raises UndefinedError in __getattr__(), so render() will also raise instead of returning Undefined.
This minimal patch fixes the issue:
diff --git a/src/jinja2/runtime.py b/src/jinja2/runtime.py
index 950f620..316c901 100644
--- a/src/jinja2/runtime.py
+++ b/src/jinja2/runtime.py
@@ -733,6 +733,8 @@ class Undefined:
def __getattr__(self, name):
if name[:2] == "__":
raise AttributeError(name)
+ if name == 'lineno': # Python 3.10
+ raise AttributeError(name)
return self._fail_with_undefined_error()
__add__ = __radd__ = __sub__ = __rsub__ = _fail_with_undefined_error
Would this be acceptable as a PR, or is there a better solution?
There is currently one remaining failure with Python 3.10.0a4 (see https://bugzilla.redhat.com/show_bug.cgi?id=1907442):
Environment:
The failure is caused by a change introduced in python/cpython#23677 (see also https://bugs.python.org/issue28964): Lib/ast.py now calls
gettattr(node, 'lineno', None)in_raise_malformed_node(node).Now, the test case in question renders "{{ missing }}" and expects it to return a Jinja
Undefinedobject. However, the JinjaUndefinedobject raisesUndefinedErrorin__getattr__(), sorender()will also raise instead of returningUndefined.This minimal patch fixes the issue:
Would this be acceptable as a PR, or is there a better solution?