bitbake/parser,build,utils:
authorHolger Hans Peter Freyther <zecke@selfish.org>
Sun, 26 Feb 2006 11:07:47 +0000 (11:07 +0000)
committerHolger Hans Peter Freyther <zecke@selfish.org>
Sun, 26 Feb 2006 11:07:47 +0000 (11:07 +0000)
    We compile strings into code. In case of erros the traceback
    is not useful at all. It doesn't print the function name,
    accurate line number etc.
    Introduce a better_compile method that excepts the Error
    and prints the surrounding lines of code.
    We make use of it on the parsing level and execution level
    of python methods.

lib/bb/build.py
lib/bb/parse/parse_py/BBHandler.py
lib/bb/utils.py

index 599b45d..5a72be7 100644 (file)
@@ -25,7 +25,7 @@ You should have received a copy of the GNU General Public License along with
 Based on functions from the base bb module, Copyright 2003 Holger Schurig
 """
 
-from bb import debug, data, fetch, fatal, error, note, event, mkdirhier
+from bb import debug, data, fetch, fatal, error, note, event, mkdirhier, utils
 import bb, os
 
 # data holds flags and function name for a given task
@@ -122,8 +122,9 @@ def exec_func_python(func, d):
     """Execute a python BB 'function'"""
     import re, os
 
-    tmp = "def " + func + "():\n%s" % data.getVar(func, d)
-    comp = compile(tmp + '\n' + func + '()', bb.data.getVar('FILE', d, 1) + ':' + func, "exec")
+    tmp  = "def " + func + "():\n%s" % data.getVar(func, d)
+    tmp += '\n' + func + '()'
+    comp = utils.better_compile(tmp, func, bb.data.getVar('FILE', d, 1) )
     prevdir = os.getcwd()
     g = {} # globals
     g['bb'] = bb
index fac3e85..d083bb3 100644 (file)
@@ -22,7 +22,7 @@
    Place, Suite 330, Boston, MA 02111-1307 USA.""" 
 
 import re, bb, os, sys
-import bb.fetch, bb.build
+import bb.fetch, bb.build, bb.utils
 from bb import debug, data, fetch, fatal
 
 from ConfHandler import include, localpath, obtain, init
@@ -206,7 +206,7 @@ def feeder(lineno, s, fn, d):
             return
         else:
             text = '\n'.join(__body__)
-            comp = compile(text, "<bb>", "exec")
+            comp = bb.utils.better_compile(text, "<bb>", fn )
             exec comp in __builtins__
             __body__ = []
             __inpython__ = False
index 2f53c65..31dc4e3 100644 (file)
@@ -93,3 +93,26 @@ def explode_deps(s):
             # Ignore version
             #r[-1] += ' ' + ' '.join(j)
     return r
+
+
+def better_compile(text, file, realfile):
+    try:
+        return compile(text, file, "exec")
+    except Exception, e:
+        import bb,sys
+
+        # split the text into lines again
+        body = text.split('\n')
+        bb.error("Error in compiling: ", realfile)
+        bb.error("The lines resulting into thiis error were:")
+        bb.error("\t%d:%s:'%s'" % (e.lineno, e.__class__.__name__, body[e.lineno-1]))
+        # print the environment of the method
+        bb.error("Printing the environment of the function")
+        min_line = max(1,e.lineno-4)
+        max_line = min(e.lineno+4,len(body))
+        for i in range(min_line,max_line+1):
+            bb.error("\t%.4d:%s" % (i, body[i-1]) )
+
+        # exit now
+        sys.exit(1)
+