summaryrefslogtreecommitdiffstats
path: root/lib/bb/codeparser.py
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2016-01-04 17:31:19 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-01-05 14:06:27 +0000
commit5796ed550d127853808f38257f8dcc8c1cf59342 (patch)
tree5a30db899f20afcbd7cd118d725516ecc49dc8a6 /lib/bb/codeparser.py
parent98d7002d1dca4b62042e1589fd5b9b3805d57f7a (diff)
downloadbitbake-contrib-5796ed550d127853808f38257f8dcc8c1cf59342.tar.gz
codeparser: Add support for correct linenumbers
Currently, if there is something like a python indentation error in a python function, the linenumbers and even file aren't reported correctly. This allows lineno and filename parameters to be passed in to correct this. The lack of a lineno parameter to python's compile() function is worked around by using empty linefeeds. Ugly, but effective and with minimal performance overhead. Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'lib/bb/codeparser.py')
-rw-r--r--lib/bb/codeparser.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/bb/codeparser.py b/lib/bb/codeparser.py
index 6c9a42dc3..577b4271c 100644
--- a/lib/bb/codeparser.py
+++ b/lib/bb/codeparser.py
@@ -28,6 +28,10 @@ def check_indent(codestr):
return codestr
if codestr[i-1] == "\t" or codestr[i-1] == " ":
+ if codestr[0] == "\n":
+ # Since we're adding a line, we need to remove one line of any empty padding
+ # to ensure line numbers are correct
+ codestr = codestr[1:]
return "if 1:\n" + codestr
return codestr
@@ -248,7 +252,7 @@ class PythonParser():
self.unhandled_message = "in call of %s, argument '%s' is not a string literal"
self.unhandled_message = "while parsing %s, %s" % (name, self.unhandled_message)
- def parse_python(self, node):
+ def parse_python(self, node, lineno=0, filename="<string>"):
if not node or not node.strip():
return
@@ -270,7 +274,9 @@ class PythonParser():
self.contains[i] = set(codeparsercache.pythoncacheextras[h].contains[i])
return
- code = compile(check_indent(str(node)), "<string>", "exec",
+ # We can't add to the linenumbers for compile, we can pad to the correct number of blank lines though
+ node = "\n" * int(lineno) + node
+ code = compile(check_indent(str(node)), filename, "exec",
ast.PyCF_ONLY_AST)
for n in ast.walk(code):