Skip to content

Commit

Permalink
[FIX] base: fix qweb ast validation for python 3.8.4
Browse files Browse the repository at this point in the history
As of Python 3.8.4, when `ast.Name` is instantiated with either `True`,
`False` and `None`, a ValueError is raised [1][2].
Because of that, QWeb views processing will crash.

Replacing `ast.Name` with `ast.Constant` is the proper way to
instantiate those constants, and is supported since Python 3.6.0.

This fix was tested on Python 3.6, 3.7 and 3.8, and is in 14.0 already.

It is now considered for backport in 13.0 because:
- 13.0 still represented 75% of the new installations as of
  September 2020, so if left unpatched it will generate a large number
  of obscure errors and support tickets for our users.
- Ubuntu 20.04 has started to deploy Python 3.8.5 via unattended
  upgrades in October 2020, replacing 3.8.2, and thus triggers the
  problem [4].

1: https://docs.python.org/3/whatsnew/changelog.html#id7
2: https://bugs.python.org/issue40870
3: https://packages.ubuntu.com/focal/python3.8

backport of d73b44a using ast.NameConstant (available in python with
python/cpython@442f209
and deprecated in python 3.8) because python 3.5 doesn't have ast.Constant.
closes odoo#59630

Signed-off-by: Olivier Dony (odo) <[email protected]>
  • Loading branch information
d-fence authored and qgroulard committed Nov 29, 2022
1 parent 3a8df9d commit 7a0df50
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions odoo/addons/base/models/ir_qweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,10 @@ def _compile_expr(self, expr):
def _get_attr_bool(self, attr, default=False):
if attr:
if attr is True:
return ast.Name(id='True', ctx=ast.Load())
return ast.NameConstant(True)
attr = attr.lower()
if attr in ('false', '0'):
return ast.Name(id='False', ctx=ast.Load())
return ast.NameConstant(False)
elif attr in ('true', '1'):
return ast.Name(id='True', ctx=ast.Load())
return ast.Name(id=str(attr if attr is False else default), ctx=ast.Load())
return ast.NameConstant(True)
return ast.NameConstant(attr if attr is False else bool(default))
8 changes: 4 additions & 4 deletions odoo/addons/base/models/qweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,12 +613,12 @@ def _if_content_is_not_Falsy(self, body, orelse):
ast.Compare(
left=ast.Name(id='content', ctx=ast.Load()),
ops=[ast.IsNot()],
comparators=[ast.Name(id='None', ctx=ast.Load())]
comparators=[ast.NameConstant(None)]
),
ast.Compare(
left=ast.Name(id='content', ctx=ast.Load()),
ops=[ast.IsNot()],
comparators=[ast.Name(id='False', ctx=ast.Load())]
comparators=[ast.NameConstant(False)]
)
]
),
Expand Down Expand Up @@ -1246,7 +1246,7 @@ def _compile_widget(self, el, expression, field_options):
keywords=[], starargs=None, kwargs=None
),
self._compile_expr0(expression),
ast.Name(id='None', ctx=ast.Load()),
ast.NameConstant(None),
], ctx=ast.Load())
)
]
Expand Down Expand Up @@ -1563,7 +1563,7 @@ def _compile_directive_call(self, el, options):
if isinstance(key, pycompat.string_types):
keys.append(ast.Str(s=key))
elif key is None:
keys.append(ast.Name(id='None', ctx=ast.Load()))
keys.append(ast.NameConstant(None))
values.append(ast.Str(s=value))

# {'nsmap': {None: 'xmlns def'}}
Expand Down

0 comments on commit 7a0df50

Please sign in to comment.