($INBOX_DIR/description missing)
 help / color / mirror / Atom feed
From: Robert Yang <liezhi.yang@windriver.com>
To: Richard Purdie <richard.purdie@linuxfoundation.org>,
	bitbake-devel@lists.openembedded.org
Subject: Re: [bitbake-devel] [PATCH v3 1/1] bitbake: event: Inject empty lines to make code match lineno in filename
Date: Mon, 8 Jan 2024 18:23:17 +0800	[thread overview]
Message-ID: <c109be1f-43b5-4078-9218-60e0c0db8c6b@windriver.com> (raw)
In-Reply-To: <35a13b43c34c7159be6ddc3e00266e01d7ba712a.camel@linuxfoundation.org>



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


  reply	other threads:[~2024-01-08 10:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-29  6:06 [PATCH v3 0/1] bitbake: event: Inject empty lines to make code match lineno in filename liezhi.yang
2023-12-29  6:06 ` [PATCH v3 1/1] " liezhi.yang
2023-12-29 11:16   ` [bitbake-devel] " Richard Purdie
2024-01-08 10:23     ` Robert Yang [this message]
2023-12-31 20:28   ` Alexandre Belloni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c109be1f-43b5-4078-9218-60e0c0db8c6b@windriver.com \
    --to=liezhi.yang@windriver.com \
    --cc=bitbake-devel@lists.openembedded.org \
    --cc=richard.purdie@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).