diff options
author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2016-03-30 20:52:56 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2016-03-31 09:13:04 +0100 |
commit | d73da22b0e332411ed5552d36910fe15f92c8fac (patch) | |
tree | a2a0961cc5ac0e663fda21ae9c9b3cc80e340a92 | |
parent | 672c07de4a96eb67eaafba0873eced44ec9ae1a6 (diff) | |
download | poky-d73da22b0e332411ed5552d36910fe15f92c8fac.tar.gz |
bitbake: 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.
(Bitbake rev: 85cf22fd0ed26bb7dc7738ef2a10441891f38ae2)
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r-- | bitbake/lib/bb/build.py | 15 | ||||
-rw-r--r-- | bitbake/lib/bb/utils.py | 4 |
2 files changed, 14 insertions, 5 deletions
diff --git a/bitbake/lib/bb/build.py b/bitbake/lib/bb/build.py index d20ee065b2..db5072cb4d 100644 --- a/bitbake/lib/bb/build.py +++ b/bitbake/lib/bb/build.py | |||
@@ -156,7 +156,12 @@ class LogTee(object): | |||
156 | def flush(self): | 156 | def flush(self): |
157 | self.outfile.flush() | 157 | self.outfile.flush() |
158 | 158 | ||
159 | def exec_func(func, d, dirs = None): | 159 | # |
160 | # pythonexception allows the python exceptions generated to be raised | ||
161 | # as the real exceptions (not FuncFailed) and without a backtrace at the | ||
162 | # origin of the failure. | ||
163 | # | ||
164 | def exec_func(func, d, dirs = None, pythonexception=False): | ||
160 | """Execute a BB 'function'""" | 165 | """Execute a BB 'function'""" |
161 | 166 | ||
162 | body = d.getVar(func, False) | 167 | body = d.getVar(func, False) |
@@ -224,7 +229,7 @@ def exec_func(func, d, dirs = None): | |||
224 | 229 | ||
225 | with bb.utils.fileslocked(lockfiles): | 230 | with bb.utils.fileslocked(lockfiles): |
226 | if ispython: | 231 | if ispython: |
227 | exec_func_python(func, d, runfile, cwd=adir) | 232 | exec_func_python(func, d, runfile, cwd=adir, pythonexception=pythonexception) |
228 | else: | 233 | else: |
229 | exec_func_shell(func, d, runfile, cwd=adir) | 234 | exec_func_shell(func, d, runfile, cwd=adir) |
230 | 235 | ||
@@ -232,7 +237,7 @@ _functionfmt = """ | |||
232 | {function}(d) | 237 | {function}(d) |
233 | """ | 238 | """ |
234 | logformatter = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") | 239 | logformatter = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") |
235 | def exec_func_python(func, d, runfile, cwd=None): | 240 | def exec_func_python(func, d, runfile, cwd=None, pythonexception=False): |
236 | """Execute a python BB 'function'""" | 241 | """Execute a python BB 'function'""" |
237 | 242 | ||
238 | code = _functionfmt.format(function=func) | 243 | code = _functionfmt.format(function=func) |
@@ -256,10 +261,12 @@ def exec_func_python(func, d, runfile, cwd=None): | |||
256 | bb.methodpool.insert_method(func, text, fn, lineno - 1) | 261 | bb.methodpool.insert_method(func, text, fn, lineno - 1) |
257 | 262 | ||
258 | comp = utils.better_compile(code, func, "exec_python_func() autogenerated") | 263 | comp = utils.better_compile(code, func, "exec_python_func() autogenerated") |
259 | utils.better_exec(comp, {"d": d}, code, "exec_python_func() autogenerated") | 264 | utils.better_exec(comp, {"d": d}, code, "exec_python_func() autogenerated", pythonexception=pythonexception) |
260 | except (bb.parse.SkipRecipe, bb.build.FuncFailed): | 265 | except (bb.parse.SkipRecipe, bb.build.FuncFailed): |
261 | raise | 266 | raise |
262 | except: | 267 | except: |
268 | if pythonexception: | ||
269 | raise | ||
263 | raise FuncFailed(func, None) | 270 | raise FuncFailed(func, None) |
264 | finally: | 271 | finally: |
265 | bb.debug(2, "Python function %s finished" % func) | 272 | bb.debug(2, "Python function %s finished" % func) |
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 7ab8927608..e9ad68f2d7 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): | |||
372 | finally: | 372 | finally: |
373 | logger.error("\n".join(error)) | 373 | logger.error("\n".join(error)) |
374 | 374 | ||
375 | def better_exec(code, context, text = None, realfile = "<code>"): | 375 | def better_exec(code, context, text = None, realfile = "<code>", pythonexception=False): |
376 | """ | 376 | """ |
377 | Similiar to better_compile, better_exec will | 377 | Similiar to better_compile, better_exec will |
378 | print the lines that are responsible for the | 378 | print the lines that are responsible for the |
@@ -389,6 +389,8 @@ def better_exec(code, context, text = None, realfile = "<code>"): | |||
389 | # Error already shown so passthrough, no need for traceback | 389 | # Error already shown so passthrough, no need for traceback |
390 | raise | 390 | raise |
391 | except Exception as e: | 391 | except Exception as e: |
392 | if pythonexception: | ||
393 | raise | ||
392 | (t, value, tb) = sys.exc_info() | 394 | (t, value, tb) = sys.exc_info() |
393 | try: | 395 | try: |
394 | _print_exception(t, value, tb, realfile, text, context) | 396 | _print_exception(t, value, tb, realfile, text, context) |