[oe-commits] [bitbake] 03/04: build/utils: Allow python functions to execute with real exception handling

git at git.openembedded.org git at git.openembedded.org
Wed Mar 30 20:25:04 UTC 2016


rpurdie pushed a commit to branch master-next
in repository bitbake.

commit 85cf22fd0ed26bb7dc7738ef2a10441891f38ae2
Author: Richard Purdie <richard.purdie at linuxfoundation.org>
AuthorDate: Wed Mar 30 20:52:56 2016 +0100

    build/utils: Allow python functions to execute with real exception handling
    
    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 <richard.purdie at linuxfoundation.org>
---
 lib/bb/build.py | 15 +++++++++++----
 lib/bb/utils.py |  4 +++-
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/lib/bb/build.py b/lib/bb/build.py
index d20ee06..db5072c 100644
--- a/lib/bb/build.py
+++ b/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/lib/bb/utils.py b/lib/bb/utils.py
index 7ab8927..e9ad68f 100644
--- a/lib/bb/utils.py
+++ b/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 = "<code>"):
+def better_exec(code, context, text = None, realfile = "<code>", 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 = "<code>"):
         # 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)

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Openembedded-commits mailing list