From 885aef7b1415eda61beca64484b090651e4c0e7b Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 8 Sep 2023 14:16:31 +0200 Subject: [PATCH] [ADD] base: support for breakpoint() in qweb The breakpoint() builtin was added in [Python 3.7] ([PEP 553]). It is conveniently everything-agnostic, can be configured (including disabled) via an envvar (`PYTHONBREAKPOINT`) and code (`sys.breakpointhook`), and defaults to pdb (`pdb.set_trace`). This is more flexible as it allows using arbitrary callables without special casing, which mostly allows using less common debuggers (e.g. IDE debug servers / hooks). As using an empty string for the debugger name was not allowed previously, co-opt that to invoke `breakpoint`. And deprecate the old style. [Python 3.7]: https://docs.python.org/3/whatsnew/3.7.html#pep-553-built-in-breakpoint [PEP 553]: https://peps.python.org/pep-0553/ closes odoo/odoo#134842 Related: odoo/documentation#5800 Signed-off-by: Xavier Morel (xmo) --- odoo/addons/base/models/ir_qweb.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/odoo/addons/base/models/ir_qweb.py b/odoo/addons/base/models/ir_qweb.py index cb8794a910f..0343ac3bc47 100644 --- a/odoo/addons/base/models/ir_qweb.py +++ b/odoo/addons/base/models/ir_qweb.py @@ -123,15 +123,21 @@ Directives ``t-debug`` ~~~~~~~~~~~ -**Values**: ``pdb``, ``ipdb``, ``pudb``, ``wdb`` +**Values**: `''` (empty string), ``pdb``, ``ipdb``, ``pudb``, ``wdb`` -Activate the choosed debugger. +Triggers a debugger breakpoint at that location. With an empty value, calls the +``breakpoint`` builtin invoking whichever breakpoint hook has been set up, +otherwise triggers a breakpoint uses the corresponding debugger. When dev mode is enabled this allows python developers to have access to the state of variables being rendered. The code generated by the QWeb engine is not accessible, only the variables (values, self) can be analyzed or the methods that called the QWeb rendering. +.. warning:: using a non-empty string is deprecated since 17.0, configure your + preferred debugger via ``PYTHONBREAKPOINT`` or + ``sys.setbreakpointhook``. + ``t-if`` ~~~~~~~~ **Values**: python expression @@ -370,6 +376,7 @@ import time import token import tokenize import traceback +import warnings import werkzeug from markupsafe import Markup, escape @@ -2354,7 +2361,16 @@ class IrQWeb(models.AbstractModel): def _debug_trace(self, debugger, values): """Method called at running time to load debugger.""" - if debugger in SUPPORTED_DEBUGGER: + if not debugger: + breakpoint() + elif debugger in SUPPORTED_DEBUGGER: + warnings.warn( + "Using t-debug with an explicit debugger is deprecated " + "since Odoo 17.0, keep the value empty and configure the " + "``breakpoint`` builtin instead.", + category=DeprecationWarning, + stacklevel=2, + ) __import__(debugger).set_trace() else: raise ValueError(f"unsupported t-debug value: {debugger}")