diff mbox series

[v3,1/1] bitbake: event: Inject empty lines to make code match lineno in filename

Message ID b64c2496383eb263e6fd2d636ec7a2d88b424db6.1703829627.git.liezhi.yang@windriver.com
State Accepted, archived
Commit c212933d9c786806852c87f188250a4f0a14c048
Headers show
Series [v3,1/1] bitbake: event: Inject empty lines to make code match lineno in filename | expand

Commit Message

Robert Yang Dec. 29, 2023, 6:06 a.m. UTC
From: Robert Yang <liezhi.yang@windriver.com>

So that we can get the correct error messages.

* 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.

* 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

And no impact on parsing time:
* Before:
$ rm -fr cache tmp; time bitbake -p
real    0m27.254s

* Now:
$ rm -fr cache tmp; time bitbake -p
real    0m27.200s

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
---
 bitbake/lib/bb/event.py | 2 ++
 1 file changed, 2 insertions(+)

Comments

Richard Purdie Dec. 29, 2023, 11:16 a.m. UTC | #1
On Thu, 2023-12-28 at 22:06 -0800, Robert Yang via
lists.openembedded.org wrote:
> From: Robert Yang <liezhi.yang@windriver.com>
> 
> So that we can get the correct error messages.
> 
> * 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.
> 
> * 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
> 
> And no impact on parsing time:
> * Before:
> $ rm -fr cache tmp; time bitbake -p
> real    0m27.254s
> 
> * Now:
> $ rm -fr cache tmp; time bitbake -p
> real    0m27.200s
> 
> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
> ---
>  bitbake/lib/bb/event.py | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/bitbake/lib/bb/event.py b/bitbake/lib/bb/event.py
> index f8acacd80d1..7722258b7e8 100644
> --- a/bitbake/lib/bb/event.py
> +++ b/bitbake/lib/bb/event.py
> @@ -257,6 +257,8 @@ def register(name, handler, mask=None, filename=None, lineno=None, data=None):
>          # handle string containing python code
>          if isinstance(handler, str):
>              tmp = "def %s(e, d):\n%s" % (name, handler)
> +            # Inject empty lines to make code match lineno in filename
> +            tmp = "\n" * (lineno-1) + tmp
>              try:
>                  code = bb.methodpool.compile_cache(tmp)
>                  if not code:

Looks good but don't you need remove the ast line number adjustment a
few lines later as well?

Should we put some tests into bitbake-selftest for this?

Cheers,

Richard
Alexandre Belloni Dec. 31, 2023, 8:28 p.m. UTC | #2
This causes bitbake-selftest failures:

======================================================================
ERROR: test_register_from_string (bb.tests.event.EventHandlingTest)
Test register method receiving code in string
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/pokybuild/yocto-worker/oe-selftest-debian/build/bitbake/lib/bb/tests/event.py", line 121, in test_register_from_string
    result = bb.event.register("string_handler", "    return True")
  File "/home/pokybuild/yocto-worker/oe-selftest-debian/build/bitbake/lib/bb/event.py", line 261, in register
    tmp = "\n" * (lineno-1) + tmp
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

https://autobuilder.yoctoproject.org/typhoon/#/builders/80/builds/6202/steps/11/logs/stdio

On 28/12/2023 22:06:42-0800, Robert Yang via lists.openembedded.org wrote:
> From: Robert Yang <liezhi.yang@windriver.com>
> 
> So that we can get the correct error messages.
> 
> * 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.
> 
> * 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
> 
> And no impact on parsing time:
> * Before:
> $ rm -fr cache tmp; time bitbake -p
> real    0m27.254s
> 
> * Now:
> $ rm -fr cache tmp; time bitbake -p
> real    0m27.200s
> 
> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
> ---
>  bitbake/lib/bb/event.py | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/bitbake/lib/bb/event.py b/bitbake/lib/bb/event.py
> index f8acacd80d1..7722258b7e8 100644
> --- a/bitbake/lib/bb/event.py
> +++ b/bitbake/lib/bb/event.py
> @@ -257,6 +257,8 @@ def register(name, handler, mask=None, filename=None, lineno=None, data=None):
>          # handle string containing python code
>          if isinstance(handler, str):
>              tmp = "def %s(e, d):\n%s" % (name, handler)
> +            # Inject empty lines to make code match lineno in filename
> +            tmp = "\n" * (lineno-1) + tmp
>              try:
>                  code = bb.methodpool.compile_cache(tmp)
>                  if not code:
> -- 
> 2.42.0
> 

> 
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#15717): https://lists.openembedded.org/g/bitbake-devel/message/15717
> Mute This Topic: https://lists.openembedded.org/mt/103413642/3617179
> Group Owner: bitbake-devel+owner@lists.openembedded.org
> Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [alexandre.belloni@bootlin.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>
Robert Yang Jan. 8, 2024, 10:23 a.m. UTC | #3
On 12/29/23 7:16 PM, Richard Purdie wrote:
> On Thu, 2023-12-28 at 22:06 -0800, Robert Yang via
> lists.openembedded.org wrote:
>> From: Robert Yang <liezhi.yang@windriver.com>
>>
>> So that we can get the correct error messages.
>>
>> * 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.
>>
>> * 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
>>
>> And no impact on parsing time:
>> * Before:
>> $ rm -fr cache tmp; time bitbake -p
>> real    0m27.254s
>>
>> * Now:
>> $ rm -fr cache tmp; time bitbake -p
>> real    0m27.200s
>>
>> Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
>> ---
>>   bitbake/lib/bb/event.py | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/bitbake/lib/bb/event.py b/bitbake/lib/bb/event.py
>> index f8acacd80d1..7722258b7e8 100644
>> --- a/bitbake/lib/bb/event.py
>> +++ b/bitbake/lib/bb/event.py
>> @@ -257,6 +257,8 @@ def register(name, handler, mask=None, filename=None, lineno=None, data=None):
>>           # handle string containing python code
>>           if isinstance(handler, str):
>>               tmp = "def %s(e, d):\n%s" % (name, handler)
>> +            # Inject empty lines to make code match lineno in filename
>> +            tmp = "\n" * (lineno-1) + tmp
>>               try:
>>                   code = bb.methodpool.compile_cache(tmp)
>>                   if not code:
> 
> Looks good but don't you need remove the ast line number adjustment a
> few lines later as well?

Yes, you're right, the code has incremented the lineno by '\n', so the
following line isn't needed:

ast.increment_lineno(code, lineno-1)

Now the patch is: (Check lineno is not None to fix bitbake-selftest error)
--- a/bitbake/lib/bb/event.py
+++ b/bitbake/lib/bb/event.py
@@ -257,14 +257,15 @@ def register(name, handler, mask=None, filename=None, 
lineno=None, data=None):
          # handle string containing python code
          if isinstance(handler, str):
              tmp = "def %s(e, d):\n%s" % (name, handler)
+            # Inject empty lines to make code match lineno in filename
+            if lineno is not None:
+                tmp = "\n" * (lineno-1) + tmp
              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)
-                    if lineno is not None:
-                        ast.increment_lineno(code, lineno-1)
                      code = compile(code, filename, "exec")
                      bb.methodpool.compile_cache_add(tmp, code)
              except SyntaxError:

I will send a V4 add with a testcase for it.

// Robert

> 
> Should we put some tests into bitbake-selftest for this?
> 
> Cheers,
> 
> Richard
diff mbox series

Patch

diff --git a/bitbake/lib/bb/event.py b/bitbake/lib/bb/event.py
index f8acacd80d1..7722258b7e8 100644
--- a/bitbake/lib/bb/event.py
+++ b/bitbake/lib/bb/event.py
@@ -257,6 +257,8 @@  def register(name, handler, mask=None, filename=None, lineno=None, data=None):
         # handle string containing python code
         if isinstance(handler, str):
             tmp = "def %s(e, d):\n%s" % (name, handler)
+            # Inject empty lines to make code match lineno in filename
+            tmp = "\n" * (lineno-1) + tmp
             try:
                 code = bb.methodpool.compile_cache(tmp)
                 if not code: