[Qemu-devel] [PATCH 1/3] tracetool: prefix parse errors with line numbers

Stefan Hajnoczi posted 3 patches 8 years, 1 month ago
[Qemu-devel] [PATCH 1/3] tracetool: prefix parse errors with line numbers
Posted by Stefan Hajnoczi 8 years, 1 month ago
Include the file line number in the message that is printed when
trace-events parse errors are raised.

Suggested-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 scripts/tracetool/__init__.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/scripts/tracetool/__init__.py b/scripts/tracetool/__init__.py
index 0670ec17d5..fbccaaa0cf 100644
--- a/scripts/tracetool/__init__.py
+++ b/scripts/tracetool/__init__.py
@@ -300,13 +300,18 @@ def read_events(fobj):
     """
 
     events = []
-    for line in fobj:
+    for lineno, line in enumerate(fobj):
         if not line.strip():
             continue
         if line.lstrip().startswith('#'):
             continue
 
-        event = Event.build(line)
+        try:
+            event = Event.build(line)
+        except ValueError as e:
+            arg0 = 'Error on line %d: %s' % (lineno + 1, e.args[0])
+            e.args = (arg0,) + e.args[1:]
+            raise
 
         # transform TCG-enabled events
         if "tcg" not in event.properties:
-- 
2.14.3


Re: [Qemu-devel] [PATCH 1/3] tracetool: prefix parse errors with line numbers
Posted by Eric Blake 8 years, 1 month ago
On 01/10/2018 02:25 PM, Stefan Hajnoczi wrote:
> Include the file line number in the message that is printed when
> trace-events parse errors are raised.
> 
> Suggested-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>  scripts/tracetool/__init__.py | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/scripts/tracetool/__init__.py b/scripts/tracetool/__init__.py
> index 0670ec17d5..fbccaaa0cf 100644
> --- a/scripts/tracetool/__init__.py
> +++ b/scripts/tracetool/__init__.py
> @@ -300,13 +300,18 @@ def read_events(fobj):
>      """
>  
>      events = []
> -    for line in fobj:
> +    for lineno, line in enumerate(fobj):

If you use enumerate(fobj, 1) here...

>          if not line.strip():
>              continue
>          if line.lstrip().startswith('#'):
>              continue
>  
> -        event = Event.build(line)
> +        try:
> +            event = Event.build(line)
> +        except ValueError as e:
> +            arg0 = 'Error on line %d: %s' % (lineno + 1, e.args[0])

you could avoid the +1 here.

Up to you; either way, the patch is an improvement.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org