mfabik / rpms / satyr

Forked from rpms/satyr 3 years ago
Clone

Blame SOURCES/satyr-0.13-python-exceptions.patch

f41043
From 7fa846eea1f9f1a541ec061f272d4f82d4aee428 Mon Sep 17 00:00:00 2001
f41043
From: Jakub Filak <jfilak@redhat.com>
f41043
Date: Wed, 25 Jun 2014 17:28:00 +0200
f41043
Subject: [SATYR PATCH 3/6] Fix parsing of invalid syntax Python exceptions
f41043
f41043
Closes #168
f41043
f41043
Signed-off-by: Jakub Filak <jfilak@redhat.com>
f41043
f41043
mmilata: commit msg typo
f41043
---
f41043
 lib/python_frame.c                 | 56 +++++++++++++++++++------------
f41043
 lib/python_stacktrace.c            | 69 ++++++++++++++++++++++++++++++++++----
f41043
 tests/python/python.py             | 38 +++++++++++++++++++++
f41043
 tests/python_stacktraces/python-04 |  6 ++++
f41043
 tests/python_stacktraces/python-05 | 16 +++++++++
f41043
 5 files changed, 157 insertions(+), 28 deletions(-)
f41043
 create mode 100644 tests/python_stacktraces/python-04
f41043
 create mode 100644 tests/python_stacktraces/python-05
f41043
f41043
diff --git a/lib/python_frame.c b/lib/python_frame.c
f41043
index b62628f..b0540fe 100644
f41043
--- a/lib/python_frame.c
f41043
+++ b/lib/python_frame.c
f41043
@@ -260,33 +260,45 @@ sr_python_frame_parse(const char **input,
f41043
 
f41043
     if (0 == sr_skip_string(&local_input, ", in "))
f41043
     {
f41043
-        location->message = sr_asprintf("Function name separator not found.");
f41043
-        return NULL;
f41043
-    }
f41043
-
f41043
-    location->column += strlen(", in ");
f41043
+        if (local_input[0] != '\n')
f41043
+        {
f41043
+            location->message = sr_asprintf("Function name separator not found.");
f41043
+            return NULL;
f41043
+        }
f41043
 
f41043
-    /* Parse function name */
f41043
-    if (!sr_parse_char_cspan(&local_input, "\n", &frame->function_name))
f41043
+        /* The last frame of SyntaxError stack trace does not have
f41043
+         * function name on its line. For the sake of simplicity, we will
f41043
+         * believe that we are dealing with such a frame now.
f41043
+         */
f41043
+        frame->function_name = sr_strdup("syntax");
f41043
+        frame->special_function = true;
f41043
+    }
f41043
+    else
f41043
     {
f41043
-        sr_python_frame_free(frame);
f41043
-        location->message = sr_asprintf("Unable to find the newline character "
f41043
-                "identifying the end of function name.");
f41043
+        location->column += strlen(", in ");
f41043
 
f41043
-        return NULL;
f41043
-    }
f41043
+        /* Parse function name */
f41043
+        if (!sr_parse_char_cspan(&local_input, "\n", &frame->function_name))
f41043
+        {
f41043
+            sr_python_frame_free(frame);
f41043
+            location->message = sr_asprintf("Unable to find the newline character "
f41043
+                    "identifying the end of function name.");
f41043
 
f41043
-    location->column += strlen(frame->function_name);
f41043
+            return NULL;
f41043
+        }
f41043
 
f41043
-    if (strlen(frame->function_name) > 0 &&
f41043
-        frame->function_name[0] == '<' &&
f41043
-        frame->function_name[strlen(frame->function_name)-1] == '>')
f41043
-    {
f41043
-        frame->special_function = true;
f41043
-        frame->function_name[strlen(frame->function_name)-1] = '\0';
f41043
-        char *inside = sr_strdup(frame->function_name + 1);
f41043
-        free(frame->function_name);
f41043
-        frame->function_name = inside;
f41043
+        location->column += strlen(frame->function_name);
f41043
+
f41043
+        if (strlen(frame->function_name) > 0 &&
f41043
+            frame->function_name[0] == '<' &&
f41043
+            frame->function_name[strlen(frame->function_name)-1] == '>')
f41043
+        {
f41043
+            frame->special_function = true;
f41043
+            frame->function_name[strlen(frame->function_name)-1] = '\0';
f41043
+            char *inside = sr_strdup(frame->function_name + 1);
f41043
+            free(frame->function_name);
f41043
+            frame->function_name = inside;
f41043
+        }
f41043
     }
f41043
 
f41043
     sr_skip_char(&local_input, '\n');
f41043
diff --git a/lib/python_stacktrace.c b/lib/python_stacktrace.c
f41043
index 3dc0a75..99aa52c 100644
f41043
--- a/lib/python_stacktrace.c
f41043
+++ b/lib/python_stacktrace.c
f41043
@@ -29,6 +29,7 @@
f41043
 #include "generic_thread.h"
f41043
 #include "internal_utils.h"
f41043
 #include <stdio.h>
f41043
+#include <stdlib.h>
f41043
 #include <string.h>
f41043
 #include <inttypes.h>
f41043
 
f41043
@@ -137,13 +138,48 @@ sr_python_stacktrace_parse(const char **input,
f41043
 
f41043
     if (!local_input)
f41043
     {
f41043
-        location->message = "Traceback header not found.";
f41043
-        return NULL;
f41043
-    }
f41043
+        /* SyntaxError stack trace of an exception thrown in the executed file
f41043
+         * conforms to the following template:
f41043
+         * invalid syntax ($file, line $number)
f41043
+         *
f41043
+         *    File "$file", line $number
f41043
+         *       $code
f41043
+         *         ^
f41043
+         * SyntaxError: invalid syntax
f41043
+         *
f41043
+         * for exceptions thrown from imported files, the stack trace has the
f41043
+         * regular form, except the last frame has no function name and is
f41043
+         * followed by the pointer line (^).
f41043
+         */
f41043
+        HEADER = "invalid syntax (",
f41043
+        local_input = sr_strstr_location(*input,
f41043
+                                     HEADER,
f41043
+                                     &location->line,
f41043
+                                     &location->column);
f41043
 
f41043
-    local_input += strlen(HEADER);
f41043
-    location->line += 2;
f41043
-    location->column = 0;
f41043
+        if (!local_input)
f41043
+        {
f41043
+            location->message = "Traceback header not found.";
f41043
+            return NULL;
f41043
+        }
f41043
+
f41043
+        local_input = sr_strstr_location(local_input,
f41043
+                           "  File \"",
f41043
+                           &location->line,
f41043
+                           &location->column);
f41043
+
f41043
+        if (!local_input)
f41043
+        {
f41043
+            location->message = "Frame with invalid line not found.";
f41043
+            return NULL;
f41043
+        }
f41043
+    }
f41043
+    else
f41043
+    {
f41043
+        local_input += strlen(HEADER);
f41043
+        location->line += 2;
f41043
+        location->column = 0;
f41043
+    }
f41043
 
f41043
     struct sr_python_stacktrace *stacktrace = sr_python_stacktrace_new();
f41043
 
f41043
@@ -173,6 +209,27 @@ sr_python_stacktrace_parse(const char **input,
f41043
         return NULL;
f41043
     }
f41043
 
f41043
+    bool invalid_syntax_pointer = true;
f41043
+    const char *tmp_input = local_input;
f41043
+    while (*tmp_input != '\n' && *tmp_input != '\0')
f41043
+    {
f41043
+        if (*tmp_input != ' ' && *tmp_input != '^')
f41043
+        {
f41043
+            invalid_syntax_pointer = false;
f41043
+            break;
f41043
+        }
f41043
+        ++tmp_input;
f41043
+    }
f41043
+
f41043
+    if (invalid_syntax_pointer)
f41043
+    {
f41043
+        /* Skip line "   ^" pointing to the invalid code */
f41043
+        sr_skip_char_cspan(&local_input, "\n");
f41043
+        ++local_input;
f41043
+        ++location->line;
f41043
+        location->column = 1;
f41043
+    }
f41043
+
f41043
     /* Parse exception name. */
f41043
     sr_parse_char_cspan(&local_input,
f41043
                         ":\n",
f41043
diff --git a/tests/python/python.py b/tests/python/python.py
f41043
index 146ca01..9044200 100755
f41043
--- a/tests/python/python.py
f41043
+++ b/tests/python/python.py
f41043
@@ -143,6 +143,44 @@ class TestPythonStacktrace(BindingsTestCase):
f41043
     def test_hash(self):
f41043
         self.assertHashable(self.trace)
f41043
 
f41043
+    def test_invalid_syntax_current_file(self):
f41043
+        trace = load_input_contents('../python_stacktraces/python-04')
f41043
+        trace = satyr.PythonStacktrace(trace)
f41043
+
f41043
+        self.assertEqual(len(trace.frames), 1)
f41043
+        self.assertEqual(trace.exception_name, 'SyntaxError')
f41043
+
f41043
+        f = trace.frames[0]
f41043
+        self.assertEqual(f.file_name, '/usr/bin/python3-mako-render')
f41043
+        self.assertEqual(f.function_name, "syntax")
f41043
+        self.assertEqual(f.file_line, 43)
f41043
+        self.assertEqual(f.line_contents, 'print render(data, kw)')
f41043
+        self.assertTrue(f.special_function)
f41043
+        self.assertFalse(f.special_file)
f41043
+
f41043
+    def test_invalid_syntax_imported_file(self):
f41043
+        trace = load_input_contents('../python_stacktraces/python-05')
f41043
+        trace = satyr.PythonStacktrace(trace)
f41043
+
f41043
+        self.assertEqual(len(trace.frames), 2)
f41043
+        self.assertEqual(trace.exception_name, 'SyntaxError')
f41043
+
f41043
+        f = trace.frames[1]
f41043
+        self.assertEqual(f.file_name, '/usr/bin/will_python_raise')
f41043
+        self.assertEqual(f.function_name, "module")
f41043
+        self.assertEqual(f.file_line, 2)
f41043
+        self.assertEqual(f.line_contents, 'import report')
f41043
+        self.assertTrue(f.special_function)
f41043
+        self.assertFalse(f.special_file)
f41043
+
f41043
+        f = trace.frames[0]
f41043
+        self.assertEqual(f.file_name, '/usr/lib64/python2.7/site-packages/report/__init__.py')
f41043
+        self.assertEqual(f.function_name, "syntax")
f41043
+        self.assertEqual(f.file_line, 15)
f41043
+        self.assertEqual(f.line_contents, 'def foo(:')
f41043
+        self.assertTrue(f.special_function)
f41043
+        self.assertFalse(f.special_file)
f41043
+
f41043
 class TestPythonFrame(BindingsTestCase):
f41043
     def setUp(self):
f41043
         self.frame = satyr.PythonStacktrace(contents).frames[-1]
f41043
diff --git a/tests/python_stacktraces/python-04 b/tests/python_stacktraces/python-04
f41043
new file mode 100644
f41043
index 0000000..91c3a3d
f41043
--- /dev/null
f41043
+++ b/tests/python_stacktraces/python-04
f41043
@@ -0,0 +1,6 @@
f41043
+invalid syntax (python3-mako-render, line 43)
f41043
+
f41043
+  File "/usr/bin/python3-mako-render", line 43
f41043
+    print render(data, kw)
f41043
+               ^
f41043
+SyntaxError: invalid syntax
f41043
diff --git a/tests/python_stacktraces/python-05 b/tests/python_stacktraces/python-05
f41043
new file mode 100644
f41043
index 0000000..79d1b8d
f41043
--- /dev/null
f41043
+++ b/tests/python_stacktraces/python-05
f41043
@@ -0,0 +1,16 @@
f41043
+will_python_raise:2:<module>:  File "/usr/lib64/python2.7/site-packages/report/__init__.py", line 15
f41043
+
f41043
+Traceback (most recent call last):
f41043
+  File "/usr/bin/will_python_raise", line 2, in <module>
f41043
+    import report
f41043
+  File "/usr/lib64/python2.7/site-packages/report/__init__.py", line 15
f41043
+    def foo(:
f41043
+            ^
f41043
+SyntaxError: invalid syntax
f41043
+
f41043
+Local variables in innermost frame:
f41043
+__builtins__: <module '__builtin__' (built-in)>
f41043
+__name__: '__main__'
f41043
+__file__: '/usr/bin/will_python_raise'
f41043
+__doc__: None
f41043
+__package__: None
f41043
-- 
f41043
1.9.3
f41043