Skip to contentSkip to conversation

bitbake: event: Improve error message for eventhandler

PATCH Superseded [email protected] 2 participants 4 messages Dec 21, 2023 – Dec 26, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/1 bitbake: event: Improve error message for eventhandler

In reply to [email protected]

From: Robert Yang <[email protected]>

* Before the erorr 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, and nothing is wrong with the code in line 4.

* Now the error message is:
ERROR: Unable to register event handler '/path/to/poky/meta/classes-global/base.bbclass':
File "defaultbase_eventhandler", line 4
an error line
^^^^^
SyntaxError: invalid syntax

This is much easier to debug.

The filename and code doesn't match since the file usually contains more code than eventhandler, don't pass filename to compile() can fix the problem.

---
 bitbake/lib/bb/event.py | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
bitbake/lib/bb/event.py +7 -5
index f8acacd80d1..2fbf1188d45 100644
@@ -260,15 +260,17 @@ 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:
-                logger.error("Unable to register event handler '%s':\n%s", name,
+                if filename:
+                    err_msg= filename
+                else:
+                    err_msg = name
+                logger.error("Unable to register event handler '%s':\n%s", err_msg,
                              ''.join(traceback.format_exc(limit=0)))
                 _handlers[name] = noop
                 return
Signature · 1 line
2.42.0
#0 lore raw

cover bitbake: event: Improve error message for eventhandler

From: Robert Yang <[email protected]>

The following changes since commit baa6b64a946623db4db2d29f29f35ad0fec2b4d7:

poky.conf: update SANITY_TESTED_DISTROS to match autobuilder (2023-12-20 23:36:00 +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 | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
Signature · 1 line
2.42.0
#2 lore raw
In reply to [email protected]
Quoted 19 lines · Robert Yang via lists.openembedded.org

From: Robert Yang <[email protected]>

* Before the erorr 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, and nothing is wrong with the code in line 4.

* Now the error message is:
ERROR: Unable to register event handler '/path/to/poky/meta/classes-global/base.bbclass':
File "defaultbase_eventhandler", line 4
an error line
^^^^^
SyntaxError: invalid syntax

Quoted 5 lines

This is much easier to debug.

The filename and code doesn't match since the file usually contains more code than eventhandler, don't pass filename to compile() can fix the problem.

Sorry, but I really don't like this change. It is now giving a filename as an event handler and an event handler name as a file. To some, that is going to be even more confusing.

What really needs to happen here is that the line number needs to be fixed with the offset in the file. You'll see the code already does:

ast.increment_lineno(code, lineno-1)

so before printing the traceback, the code probably needs to account for that offset in the in the exception handling too.

Cheers,

Richard

#3 lore raw

Hi RP,

Quoted 30 lines · Richard Purdie
Quoted 19 lines · Robert Yang via lists.openembedded.org

From: Robert Yang <[email protected]>

* Before the erorr 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, and nothing is wrong with the code in line 4.

* Now the error message is:
ERROR: Unable to register event handler '/path/to/poky/meta/classes-global/base.bbclass':
File "defaultbase_eventhandler", line 4
an error line
^^^^^
SyntaxError: invalid syntax

Quoted 5 lines

This is much easier to debug.

The filename and code doesn't match since the file usually contains more code than eventhandler, don't pass filename to compile() can fix the problem.

Sorry, but I really don't like this change. It is now giving a filename as an event handler and an event handler name as a file. To some, that is going to be even more confusing.

Quoted 7 lines

What really needs to happen here is that the line number needs to be fixed with the offset in the file. You'll see the code already does:

ast.increment_lineno(code, lineno-1)

so before printing the traceback, the code probably needs to account for that offset in the in the exception handling too.

After more investigations, the compile() or ast.parse() can't accept a lineno
argument as offset, and the ast.increment_lineno(code, lineno-1) can't work
since 'code' is None when there is an exception. And we can't fix traceback.tb_lineno since the tb_lineno is for event.py, not for 'code' in compile(), it seems that we have to fix the error message in traceback.

And python 3.10.9 is worse than 3.8.10:

* In python 3.8.10, the error message is:
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

We can see the error code line (an error line)

* But in python3 3.10.9, we can't see the error code:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/buildarea1/lyang1/poky/meta/classes-global/base.bbclass", line 4
# SPDX-License-Identifier: MIT
^^^^^
SyntaxError: invalid syntax

I think that we can fix it in the following ways:
1) Don't pass file name to compile() to make it can report error code correctly in both python 3.8.10 and 3.10.9.

2) Fix the lineno offset and filename in traceback message.

Then the lineno and filename will be correct for both python versions:

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

I will send a V2 for it.

// Robert

Quoted 5 lines

Cheers,

Richard