bitbake: event: Improve error message for eventhandler
v2 cover bitbake: event: Improve error message for eventhandler
From: Robert Yang <[email protected]>
The following changes since commit 2d8e0802313ddfc904344b3cff3e0396e0d17fe8:
oeqa/selftest/prservice: Improve test robustness (2023-12-24 10:39:07 +0000)
are available in the Git repository at:
https://github.com/robertlinux/yocto rbt/event
https://github.com/robertlinux/yocto/tree/rbt/event
Robert Yang (1):
bitbake: event: Improve error message for eventhandler
bitbake/lib/bb/event.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
Signature · 1 line
2.42.0
PATCH v2 1/1 bitbake: event: Improve error message for eventhandler
From: Robert Yang <[email protected]>
* In python 3.10.9, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
# SPDX-License-Identifier: MIT
^^^^^
SyntaxError: invalid syntax
This is hard to debug since the error line number 4 is incorrect, but nothing is wrong with the code in line 4.
* In python 3.8.10, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
an error line
^
SyntaxError: invalid syntax
* Now the error message and lineno is correct:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 256
an error line
^^^^^
SyntaxError: invalid syntax
* Fixed it via:
- Don't pass filename to compile() to make it can report error code correctly in both python 3.8.10 and 3.10.9.
- Fix the lineno offset and filename in traceback message.
--- bitbake/lib/bb/event.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
bitbake/lib/bb/event.py +18 -5
@@ -260,16 +260,29 @@ def register(name, handler, mask=None, filename=None, lineno=None, data=None): try: code = bb.methodpool.compile_cache(tmp) if not code: - if filename is None: - filename = "%s(e, d)" % name - code = compile(tmp, filename, "exec", ast.PyCF_ONLY_AST) + code = compile(tmp, name, "exec", ast.PyCF_ONLY_AST) if lineno is not None: ast.increment_lineno(code, lineno-1) - code = compile(code, filename, "exec") + code = compile(code, name, "exec") bb.methodpool.compile_cache_add(tmp, code) except SyntaxError: + exc_lines = traceback.format_exc(limit=0).splitlines() + # Fix lineno offset manually since compile() cannot handle it correctly + if filename and lineno: + line_first = exc_lines[0] + if 'File ' in line_first and ' line ' in line_first: + lineno_new = line_first.split(' line ')[-1] + if lineno_new: + lineno_new = int(lineno_new) + lineno_new += lineno - 1 + line_first = 'File "%s", line %d' % (filename, lineno_new) + # Fix filename + exc_lines[0] = line_first.replace(name, filename) + else: + logger.warning('Failed to fix lineno for %s:%s' % (filename, name)) + logger.error("Unable to register event handler '%s':\n%s", name, - ''.join(traceback.format_exc(limit=0))) + '\n'.join(exc_lines)) _handlers[name] = noop return env = {}
Signature · 1 line
2.42.0
Quoted 65 lines · Robert Yang via lists.openembedded.org
From: Robert Yang <[email protected]>
* In python 3.10.9, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
# SPDX-License-Identifier: MIT
^^^^^
SyntaxError: invalid syntaxThis is hard to debug since the error line number 4 is incorrect, but nothing is wrong with the code in line 4.
* In python 3.8.10, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
an error line
^
SyntaxError: invalid syntax* Now the error message and lineno is correct:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 256
an error line
^^^^^
SyntaxError: invalid syntax* Fixed it via:
- Don't pass filename tocompile()to make it can report error code correctly in both python 3.8.10 and 3.10.9.- Fix the lineno offset and filename in traceback message.
Signed-off-by Robert Yang <[email protected]>--- bitbake/lib/bb/event.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)bitbake/lib/bb/event.py +12
-4@@ -260,16 +260,29 @@ def register(name, handler, mask=None, filename=None, lineno=None, data=None): try: code = bb.methodpool.compile_cache(tmp) if not code: - if filename is None: - filename = "%s(e, d)" % name - code = compile(tmp, filename, "exec", ast.PyCF_ONLY_AST) + code = compile(tmp, name, "exec", ast.PyCF_ONLY_AST) if lineno is not None: ast.increment_lineno(code, lineno-1) - code = compile(code, filename, "exec") + code = compile(code, name, "exec") bb.methodpool.compile_cache_add(tmp, code) except SyntaxError: + exc_lines = traceback.format_exc(limit=0).splitlines() + # Fix lineno offset manually since compile() cannot handle it correctly + if filename and lineno: + line_first = exc_lines[0] + if 'File ' in line_first and ' line ' in line_first: + lineno_new = line_first.split(' line ')[-1] + if lineno_new: + lineno_new = int(lineno_new) + lineno_new += lineno - 1 + line_first = 'File "%s", line %d' % (filename, lineno_new)
bitbake/lib/bb/event.py def register(name, handler, mask=None, filename=None, lineno=None, data=None): · line 287
Quoted 10 lines
bitbake/lib/bb/event.py +6
-1+ # Fix filename + exc_lines[0] = line_first.replace(name, filename) + else: + logger.warning('Failed to fix lineno for %s:%s' % (filename, name)) + logger.error("Unable to register event handler '%s':\n%s", name, - ''.join(traceback.format_exc(limit=0))) + '\n'.join(exc_lines)) _handlers[name] = noop return
This is better but I'm still wondering if we can improve. The other trick we could use is to inject "\n" characters into the start of the string you're compiling so the line numbers really match up. Would that work better?
Cheers,
Richard
Hi RP,
Quoted 75 lines · Richard Purdie
Quoted 73 lines · Robert Yang via lists.openembedded.org
From: Robert Yang <[email protected]>
* In python 3.10.9, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
# SPDX-License-Identifier: MIT
^^^^^
SyntaxError: invalid syntaxThis is hard to debug since the error line number 4 is incorrect, but nothing is wrong with the code in line 4.
* In python 3.8.10, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
an error line
^
SyntaxError: invalid syntax* Now the error message and lineno is correct:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 256
an error line
^^^^^
SyntaxError: invalid syntax* Fixed it via:
- Don't pass filename tocompile()to make it can report error code correctly in both python 3.8.10 and 3.10.9.- Fix the lineno offset and filename in traceback message.
Signed-off-by Robert Yang <[email protected]>--- bitbake/lib/bb/event.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)bitbake/lib/bb/event.py +18
-5@@ -260,16 +260,29 @@ def register(name, handler, mask=None, filename=None, lineno=None, data=None): try: code = bb.methodpool.compile_cache(tmp) if not code: - if filename is None: - filename = "%s(e, d)" % name - code = compile(tmp, filename, "exec", ast.PyCF_ONLY_AST) + code = compile(tmp, name, "exec", ast.PyCF_ONLY_AST) if lineno is not None: ast.increment_lineno(code, lineno-1) - code = compile(code, filename, "exec") + code = compile(code, name, "exec") bb.methodpool.compile_cache_add(tmp, code) except SyntaxError: + exc_lines = traceback.format_exc(limit=0).splitlines() + # Fix lineno offset manually since compile() cannot handle it correctly + if filename and lineno: + line_first = exc_lines[0] + if 'File ' in line_first and ' line ' in line_first: + lineno_new = line_first.split(' line ')[-1] + if lineno_new: + lineno_new = int(lineno_new) + lineno_new += lineno - 1 + line_first = 'File "%s", line %d' % (filename, lineno_new) + # Fix filename + exc_lines[0] = line_first.replace(name, filename) + else: + logger.warning('Failed to fix lineno for %s:%s' % (filename, name)) + logger.error("Unable to register event handler '%s':\n%s", name, - ''.join(traceback.format_exc(limit=0))) + '\n'.join(exc_lines))
Quoted 8 lines
bitbake/lib/bb/event.py def register(name, handler, mask=None, filename=None, lineno=None, data=None): · line 287
Quoted 2 lines
bitbake/lib/bb/event.py +0
-0_handlers[name] = noop returnThis is better but I'm still wondering if we can improve. The other trick we could use is to inject "\n" characters into the start of the string you're compiling so the line numbers really match up. Would that work better?
If you meant the following line:
if 'File ' in line_first and ' line ' in line_first:
I can use re.match() to improve it and send a V3.
// Robert
Quoted 5 lines
Cheers,
Richard
Quoted 87 lines · Robert Yang
Hi RP,
Quoted 75 lines · Richard Purdie
Quoted 73 lines · Robert Yang via lists.openembedded.org
From: Robert Yang <[email protected]>
* In python 3.10.9, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
# SPDX-License-Identifier: MIT
^^^^^
SyntaxError: invalid syntaxThis is hard to debug since the error line number 4 is incorrect, but nothing is wrong with the code in line 4.
* In python 3.8.10, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
an error line
^
SyntaxError: invalid syntax* Now the error message and lineno is correct:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 256
an error line
^^^^^
SyntaxError: invalid syntax* Fixed it via:
- Don't pass filename tocompile()to make it can report error code correctly in both python 3.8.10 and 3.10.9.- Fix the lineno offset and filename in traceback message.
Signed-off-by Robert Yang <[email protected]>--- bitbake/lib/bb/event.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)bitbake/lib/bb/event.py +18
-5@@ -260,16 +260,29 @@ def register(name, handler, mask=None, filename=None, lineno=None, data=None): try: code = bb.methodpool.compile_cache(tmp) if not code: - if filename is None: - filename = "%s(e, d)" % name - code = compile(tmp, filename, "exec", ast.PyCF_ONLY_AST) + code = compile(tmp, name, "exec", ast.PyCF_ONLY_AST) if lineno is not None: ast.increment_lineno(code, lineno-1) - code = compile(code, filename, "exec") + code = compile(code, name, "exec") bb.methodpool.compile_cache_add(tmp, code) except SyntaxError: + exc_lines = traceback.format_exc(limit=0).splitlines() + # Fix lineno offset manually since compile() cannot handle it correctly + if filename and lineno: + line_first = exc_lines[0] + if 'File ' in line_first and ' line ' in line_first: + lineno_new = line_first.split(' line ')[-1] + if lineno_new: + lineno_new = int(lineno_new) + lineno_new += lineno - 1 + line_first = 'File "%s", line %d' % (filename, lineno_new) + # Fix filename + exc_lines[0] = line_first.replace(name, filename) + else: + logger.warning('Failed to fix lineno for %s:%s' % (filename, name)) + logger.error("Unable to register event handler '%s':\n%s", name, - ''.join(traceback.format_exc(limit=0))) + '\n'.join(exc_lines))Quoted 8 lines
bitbake/lib/bb/event.py def register(name, handler, mask=None, filename=None, lineno=None, data=None): · line 287
Quoted 2 lines
bitbake/lib/bb/event.py +0
-0_handlers[name] = noop returnThis is better but I'm still wondering if we can improve. The other trick we could use is to inject "\n" characters into the start of the string you're compiling so the line numbers really match up. Would that work better?
Quoted 5 lines
If you meant the following line:
if 'File ' in
line_firstand ' line ' inline_first:I can use
re.match()to improve it and send a V3.
I mean something like:
tmp = "\n" * (lineno-1) + tmp
which would increase the line numbers on the string to match the needed line numbers before we compile it.
Cheers,
Richard
Quoted 92 lines · Richard Purdie
Quoted 91 lines · Robert Yang
Hi RP,
Quoted 75 lines · Richard Purdie
Quoted 73 lines · Robert Yang via lists.openembedded.org
From: Robert Yang <[email protected]>
* In python 3.10.9, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
# SPDX-License-Identifier: MIT
^^^^^
SyntaxError: invalid syntaxThis is hard to debug since the error line number 4 is incorrect, but nothing is wrong with the code in line 4.
* In python 3.8.10, the error message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 4
an error line
^
SyntaxError: invalid syntax* Now the error message and lineno is correct:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 256
an error line
^^^^^
SyntaxError: invalid syntax* Fixed it via:
- Don't pass filename tocompile()to make it can report error code correctlyin both python 3.8.10 and 3.10.9.- Fix the lineno offset and filename in traceback message.
Signed-off-by Robert Yang <[email protected]>--- bitbake/lib/bb/event.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)bitbake/lib/bb/event.py +18
-5@@ -260,16 +260,29 @@ def register(name, handler, mask=None, filename=None, lineno=None, data=None): try: code = bb.methodpool.compile_cache(tmp) if not code: - if filename is None: - filename = "%s(e, d)" % name - code = compile(tmp, filename, "exec", ast.PyCF_ONLY_AST) + code = compile(tmp, name, "exec", ast.PyCF_ONLY_AST) if lineno is not None: ast.increment_lineno(code, lineno-1) - code = compile(code, filename, "exec") + code = compile(code, name, "exec") bb.methodpool.compile_cache_add(tmp, code) except SyntaxError: + exc_lines = traceback.format_exc(limit=0).splitlines() + # Fix lineno offset manually since compile() cannot handle it correctly + if filename and lineno: + line_first = exc_lines[0] + if 'File ' in line_first and ' line ' in line_first: + lineno_new = line_first.split(' line ')[-1] + if lineno_new: + lineno_new = int(lineno_new) + lineno_new += lineno - 1 + line_first = 'File "%s", line %d' % (filename, lineno_new) + # Fix filename + exc_lines[0] = line_first.replace(name, filename) + else: + logger.warning('Failed to fix lineno for %s:%s' % (filename, name)) + logger.error("Unable to register event handler '%s':\n%s", name, - ''.join(traceback.format_exc(limit=0))) + '\n'.join(exc_lines))Quoted 8 lines
bitbake/lib/bb/event.py def register(name, handler, mask=None, filename=None, lineno=None, data=None): · line 287
Quoted 2 lines
bitbake/lib/bb/event.py +0
-0_handlers[name] = noop returnThis is better but I'm still wondering if we can improve. The other trick we could use is to inject "\n" characters into the start of the string you're compiling so the line numbers really match up. Would that work better?
If you meant the following line:
if 'File ' in
line_firstand ' line ' inline_first:
Quoted 8 lines
Quoted 1 line
I can use
re.match()to improve it and send a V3.I mean something like:
tmp = "\n" * (lineno-1) + tmp
which would increase the line numbers on the string to match the needed line numbers before we compile it.
Cool, this is very simple and works well, V3 is coming.
// Robert
Quoted 4 lines
Cheers,
Richard