From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dan.rpsys.net (5751f4a1.skybroadband.com [87.81.244.161]) by mail.openembedded.org (Postfix) with ESMTP id DC9966010B for ; Wed, 30 Mar 2016 19:52:59 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by dan.rpsys.net (8.14.4/8.14.4/Debian-4.1ubuntu1) with ESMTP id u2UJr0t2023281 for ; Wed, 30 Mar 2016 20:53:00 +0100 Received: from dan.rpsys.net ([127.0.0.1]) by localhost (dan.rpsys.net [127.0.0.1]) (amavisd-new, port 10024) with LMTP id imAJBUPkWsvL for ; Wed, 30 Mar 2016 20:53:00 +0100 (BST) Received: from hex ([192.168.3.34]) (authenticated bits=0) by dan.rpsys.net (8.14.4/8.14.4/Debian-4.1ubuntu1) with ESMTP id u2UJquBL023277 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 30 Mar 2016 20:52:57 +0100 Message-ID: <1459367576.21672.57.camel@linuxfoundation.org> From: Richard Purdie To: bitbake-devel Date: Wed, 30 Mar 2016 20:52:56 +0100 X-Mailer: Evolution 3.16.5-1ubuntu3.1 Mime-Version: 1.0 Subject: [PATCH] build/utils: Allow python functions to execute with real exception handling X-BeenThere: bitbake-devel@lists.openembedded.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Patches and discussion that advance bitbake development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Mar 2016 19:53:00 -0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit With the code as it stands today it not possible to execute a python function and get "normal" python exception handling behaviour. If a python function raises an exception, it forces a traceback to be printed and the exception becomes a FuncFailed exception. This adds in a parameter 'pythonexception' which allows standard python exceptions to be passed unchanged with no traceback. Ultimately we may want to change to this convention in various places but at least it means we can start to add sane functions now. Signed-off-by: Richard Purdie diff --git a/bitbake/lib/bb/build.py b/bitbake/lib/bb/build.py index 1d2df81..1d0dcde 100644 --- a/bitbake/lib/bb/build.py +++ b/bitbake/lib/bb/build.py @@ -156,7 +156,12 @@ class LogTee(object): def flush(self): self.outfile.flush() -def exec_func(func, d, dirs = None): +# +# pythonexception allows the python exceptions generated to be raised +# as the real exceptions (not FuncFailed) and without a backtrace at the +# origin of the failure. +# +def exec_func(func, d, dirs = None, pythonexception=False): """Execute a BB 'function'""" body = d.getVar(func, False) @@ -224,7 +229,7 @@ def exec_func(func, d, dirs = None): with bb.utils.fileslocked(lockfiles): if ispython: - exec_func_python(func, d, runfile, cwd=adir) + exec_func_python(func, d, runfile, cwd=adir, pythonexception=pythonexception) else: exec_func_shell(func, d, runfile, cwd=adir) @@ -232,7 +237,7 @@ _functionfmt = """ {function}(d) """ logformatter = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") -def exec_func_python(func, d, runfile, cwd=None): +def exec_func_python(func, d, runfile, cwd=None, pythonexception=False): """Execute a python BB 'function'""" code = _functionfmt.format(function=func) @@ -256,10 +261,12 @@ def exec_func_python(func, d, runfile, cwd=None): bb.methodpool.insert_method(func, text, fn, lineno - 1) comp = utils.better_compile(code, func, "exec_python_func() autogenerated") - utils.better_exec(comp, {"d": d}, code, "exec_python_func() autogenerated") + utils.better_exec(comp, {"d": d}, code, "exec_python_func() autogenerated", pythonexception=pythonexception) except (bb.parse.SkipRecipe, bb.build.FuncFailed): raise except: + if pythonexception: + raise raise FuncFailed(func, None) finally: bb.debug(2, "Python function %s finished" % func) diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 7ab8927..e9ad68f 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py @@ -372,7 +372,7 @@ def _print_exception(t, value, tb, realfile, text, context): finally: logger.error("\n".join(error)) -def better_exec(code, context, text = None, realfile = ""): +def better_exec(code, context, text = None, realfile = "", pythonexception=False): """ Similiar to better_compile, better_exec will print the lines that are responsible for the @@ -389,6 +389,8 @@ def better_exec(code, context, text = None, realfile = ""): # Error already shown so passthrough, no need for traceback raise except Exception as e: + if pythonexception: + raise (t, value, tb) = sys.exc_info() try: _print_exception(t, value, tb, realfile, text, context)