[SATYR PATCH 7/8] python: factor out stacktrace object creation

Martin Milata mmilata at redhat.com
Mon Aug 26 14:49:06 UTC 2013


The code is repeated at least two times and we'll need it once again.
It is still essentially the same for all four stacktrace types and could
probably be rewritten in a generic way but I'd rather not do it now for
the sake of size of this patchset.

Related to #86.

Signed-off-by: Martin Milata <mmilata at redhat.com>
---
 python/py_core_stacktrace.c   | 64 ++++++++++++++++++------------------------
 python/py_core_stacktrace.h   |  3 ++
 python/py_java_stacktrace.c   | 65 ++++++++++++++++++-------------------------
 python/py_java_stacktrace.h   |  3 ++
 python/py_koops_stacktrace.c  | 59 ++++++++++++++++++---------------------
 python/py_koops_stacktrace.h  |  3 ++
 python/py_python_stacktrace.c | 59 ++++++++++++++++++---------------------
 python/py_python_stacktrace.h |  3 ++
 8 files changed, 120 insertions(+), 139 deletions(-)

diff --git a/python/py_core_stacktrace.c b/python/py_core_stacktrace.c
index 976e994..936611b 100644
--- a/python/py_core_stacktrace.c
+++ b/python/py_core_stacktrace.c
@@ -111,48 +111,53 @@ PyTypeObject sr_py_core_stacktrace_type = {
     NULL,                           /* tp_weaklist */
 };
 
-/* constructor */
 PyObject *
-sr_py_core_stacktrace_new(PyTypeObject *object,
-                          PyObject *args,
-                          PyObject *kwds)
+core_stacktrace_to_python_obj(struct sr_core_stacktrace *stacktrace)
 {
-    struct sr_py_core_stacktrace *bo = (struct sr_py_core_stacktrace*)
-        PyObject_New(struct sr_py_core_stacktrace,
-                     &sr_py_core_stacktrace_type);
-
+    struct sr_py_core_stacktrace *bo = PyObject_New(struct sr_py_core_stacktrace,
+                                                    &sr_py_core_stacktrace_type);
     if (!bo)
         return PyErr_NoMemory();
 
     bo->thread_type = &sr_py_core_thread_type;
     bo->frame_type = &sr_py_core_frame_type;
 
+    bo->stacktrace = stacktrace;
+    bo->threads = threads_to_python_list((struct sr_stacktrace *)bo->stacktrace,
+                                         bo->thread_type, bo->frame_type);
+    if (!bo->threads)
+        return NULL;
+
+    return (PyObject *)bo;
+}
+
+/* constructor */
+PyObject *
+sr_py_core_stacktrace_new(PyTypeObject *object,
+                          PyObject *args,
+                          PyObject *kwds)
+{
     const char *str = NULL;
     if (!PyArg_ParseTuple(args, "|s", &str))
         return NULL;
 
+    struct sr_core_stacktrace *stacktrace;
+
     if (str)
     {
         char *error_msg;
-        bo->stacktrace = sr_core_stacktrace_from_json_text(str, &error_msg);
-        if (!bo->stacktrace)
+        stacktrace = sr_core_stacktrace_from_json_text(str, &error_msg);
+        if (!stacktrace)
         {
             PyErr_SetString(PyExc_ValueError, error_msg);
             free(error_msg);
             return NULL;
         }
-        bo->threads = threads_to_python_list((struct sr_stacktrace *)bo->stacktrace,
-                                             bo->thread_type, bo->frame_type);
-        if (!bo->threads)
-            return NULL;
     }
     else
-    {
-        bo->threads = PyList_New(0);
-        bo->stacktrace = sr_core_stacktrace_new();
-    }
+        stacktrace = sr_core_stacktrace_new();
 
-    return (PyObject *)bo;
+    return core_stacktrace_to_python_obj(stacktrace);
 }
 
 /* destructor */
@@ -189,24 +194,9 @@ sr_py_core_stacktrace_dup(PyObject *self, PyObject *args)
     if (threads_prepare_linked_list((struct sr_py_multi_stacktrace *)this) < 0)
         return NULL;
 
-    struct sr_py_core_stacktrace *bo = (struct sr_py_core_stacktrace*)
-        PyObject_New(struct sr_py_core_stacktrace,
-                     &sr_py_core_stacktrace_type);
-
-    if (!bo)
-        return PyErr_NoMemory();
-
-    bo->thread_type = &sr_py_core_thread_type;
-    bo->frame_type = &sr_py_core_frame_type;
-
-    bo->stacktrace = sr_core_stacktrace_dup(this->stacktrace);
-    if (!bo->stacktrace)
-        return NULL;
-
-    bo->threads = threads_to_python_list((struct sr_stacktrace *)bo->stacktrace,
-                                         bo->thread_type, bo->frame_type);
-    if (!bo->threads)
+    struct sr_core_stacktrace *stacktrace = sr_core_stacktrace_dup(this->stacktrace);
+    if (!stacktrace)
         return NULL;
 
-    return (PyObject*)bo;
+    return core_stacktrace_to_python_obj(stacktrace);
 }
diff --git a/python/py_core_stacktrace.h b/python/py_core_stacktrace.h
index 4cb00ad..f458a23 100644
--- a/python/py_core_stacktrace.h
+++ b/python/py_core_stacktrace.h
@@ -49,6 +49,9 @@ struct sr_py_core_stacktrace
     PyTypeObject *frame_type;
 };
 
+/* helpers */
+PyObject *core_stacktrace_to_python_obj(struct sr_core_stacktrace *stacktrace);
+
 /* constructor */
 PyObject *sr_py_core_stacktrace_new(PyTypeObject *object,
                                     PyObject *args,
diff --git a/python/py_java_stacktrace.c b/python/py_java_stacktrace.c
index fc5bde9..c175692 100644
--- a/python/py_java_stacktrace.c
+++ b/python/py_java_stacktrace.c
@@ -76,49 +76,53 @@ PyTypeObject sr_py_java_stacktrace_type = {
     NULL,                           /* tp_weaklist */
 };
 
-/* constructor */
 PyObject *
-sr_py_java_stacktrace_new(PyTypeObject *object,
-                          PyObject *args,
-                          PyObject *kwds)
+java_stacktrace_to_python_obj(struct sr_java_stacktrace *stacktrace)
 {
-    struct sr_py_java_stacktrace *bo = (struct sr_py_java_stacktrace*)
-        PyObject_New(struct sr_py_java_stacktrace,
-                     &sr_py_java_stacktrace_type);
-
+    struct sr_py_java_stacktrace *bo = PyObject_New(struct sr_py_java_stacktrace,
+                                                    &sr_py_java_stacktrace_type);
     if (!bo)
         return PyErr_NoMemory();
 
     bo->thread_type = &sr_py_java_thread_type;
     bo->frame_type = &sr_py_java_frame_type;
 
+    bo->stacktrace = stacktrace;
+    bo->threads = threads_to_python_list((struct sr_stacktrace *)bo->stacktrace,
+                                         bo->thread_type, bo->frame_type);
+    if (!bo->threads)
+        return NULL;
+
+    return (PyObject *)bo;
+}
+
+/* constructor */
+PyObject *
+sr_py_java_stacktrace_new(PyTypeObject *object,
+                          PyObject *args,
+                          PyObject *kwds)
+{
     const char *str = NULL;
     if (!PyArg_ParseTuple(args, "|s", &str))
         return NULL;
 
+    struct sr_java_stacktrace *stacktrace;
+
     if (str)
     {
-        /* ToDo parse */
         struct sr_location location;
         sr_location_init(&location);
-        bo->stacktrace = sr_java_stacktrace_parse(&str, &location);
-        if (!bo->stacktrace)
+        stacktrace = sr_java_stacktrace_parse(&str, &location);
+        if (!stacktrace)
         {
             PyErr_SetString(PyExc_ValueError, location.message);
             return NULL;
         }
-        bo->threads = threads_to_python_list((struct sr_stacktrace *)bo->stacktrace,
-                                             bo->thread_type, bo->frame_type);
-        if (!bo->threads)
-            return NULL;
     }
     else
-    {
-        bo->threads = PyList_New(0);
-        bo->stacktrace = sr_java_stacktrace_new();
-    }
+        stacktrace = sr_java_stacktrace_new();
 
-    return (PyObject *)bo;
+    return java_stacktrace_to_python_obj(stacktrace);
 }
 
 /* destructor */
@@ -155,24 +159,9 @@ sr_py_java_stacktrace_dup(PyObject *self, PyObject *args)
     if (threads_prepare_linked_list((struct sr_py_multi_stacktrace *)this) < 0)
         return NULL;
 
-    struct sr_py_java_stacktrace *bo = (struct sr_py_java_stacktrace*)
-        PyObject_New(struct sr_py_java_stacktrace,
-                     &sr_py_java_stacktrace_type);
-
-    if (!bo)
-        return PyErr_NoMemory();
-
-    bo->thread_type = &sr_py_java_thread_type;
-    bo->frame_type = &sr_py_java_frame_type;
-
-    bo->stacktrace = sr_java_stacktrace_dup(this->stacktrace);
-    if (!bo->stacktrace)
-        return NULL;
-
-    bo->threads = threads_to_python_list((struct sr_stacktrace *)bo->stacktrace,
-                                         bo->thread_type, bo->frame_type);
-    if (!bo->threads)
+    struct sr_java_stacktrace *stacktrace = sr_java_stacktrace_dup(this->stacktrace);
+    if (!stacktrace)
         return NULL;
 
-    return (PyObject*)bo;
+    return java_stacktrace_to_python_obj(stacktrace);
 }
diff --git a/python/py_java_stacktrace.h b/python/py_java_stacktrace.h
index 4802d57..9cbe80a 100644
--- a/python/py_java_stacktrace.h
+++ b/python/py_java_stacktrace.h
@@ -49,6 +49,9 @@ struct sr_py_java_stacktrace
     PyTypeObject *frame_type;
 };
 
+/* helpers */
+PyObject *java_stacktrace_to_python_obj(struct sr_java_stacktrace *stacktrace);
+
 /* constructor */
 PyObject *sr_py_java_stacktrace_new(PyTypeObject *object,
                                     PyObject *args,
diff --git a/python/py_koops_stacktrace.c b/python/py_koops_stacktrace.c
index 80bc104..561cbc7 100644
--- a/python/py_koops_stacktrace.c
+++ b/python/py_koops_stacktrace.c
@@ -157,45 +157,52 @@ sr_py_koops_stacktrace_set_taint_flags(PyObject *self, PyObject *rhs, void *data
     return -1;
 }
 
-/* constructor */
 PyObject *
-sr_py_koops_stacktrace_new(PyTypeObject *object,
-                           PyObject *args,
-                           PyObject *kwds)
+koops_stacktrace_to_python_obj(struct sr_koops_stacktrace *stacktrace)
 {
-    struct sr_py_koops_stacktrace *bo = (struct sr_py_koops_stacktrace*)
-        PyObject_New(struct sr_py_koops_stacktrace,
-                     &sr_py_koops_stacktrace_type);
-
+    struct sr_py_koops_stacktrace *bo = PyObject_New(struct sr_py_koops_stacktrace,
+                                                    &sr_py_koops_stacktrace_type);
     if (!bo)
         return PyErr_NoMemory();
 
     bo->frame_type = &sr_py_koops_frame_type;
 
+    bo->stacktrace = stacktrace;
+    bo->frames = frames_to_python_list((struct sr_thread *)bo->stacktrace,
+                                       bo->frame_type);
+    if (!bo->frames)
+        return NULL;
+
+    return (PyObject *)bo;
+}
+
+/* constructor */
+PyObject *
+sr_py_koops_stacktrace_new(PyTypeObject *object,
+                           PyObject *args,
+                           PyObject *kwds)
+{
     const char *str = NULL;
     if (!PyArg_ParseTuple(args, "|s", &str))
         return NULL;
 
+    struct sr_koops_stacktrace *stacktrace;
+
     if (str)
     {
         struct sr_location location;
         sr_location_init(&location);
-        bo->stacktrace = sr_koops_stacktrace_parse(&str, &location);
-        if (!bo->stacktrace)
+        stacktrace = sr_koops_stacktrace_parse(&str, &location);
+        if (!stacktrace)
         {
             PyErr_SetString(PyExc_ValueError, location.message);
             return NULL;
         }
-
-        bo->frames = frames_to_python_list((struct sr_thread *)bo->stacktrace, bo->frame_type);
     }
     else
-    {
-        bo->stacktrace = sr_koops_stacktrace_new();
-        bo->frames = PyList_New(0);
-    }
+        stacktrace = sr_koops_stacktrace_new();
 
-    return (PyObject *)bo;
+    return koops_stacktrace_to_python_obj(stacktrace);
 }
 
 /* destructor */
@@ -232,23 +239,11 @@ sr_py_koops_stacktrace_dup(PyObject *self, PyObject *args)
     if (frames_prepare_linked_list((struct sr_py_base_thread *)this) < 0)
         return NULL;
 
-    struct sr_py_koops_stacktrace *bo = (struct sr_py_koops_stacktrace*)
-        PyObject_New(struct sr_py_koops_stacktrace,
-                     &sr_py_koops_stacktrace_type);
-
-    if (!bo)
-        return PyErr_NoMemory();
-
-    bo->frame_type = &sr_py_koops_frame_type;
-    bo->stacktrace = sr_koops_stacktrace_dup(this->stacktrace);
-    if (!bo->stacktrace)
-        return NULL;
-
-    bo->frames = frames_to_python_list((struct sr_thread *)bo->stacktrace, bo->frame_type);
-    if (!bo->frames)
+    struct sr_koops_stacktrace *stacktrace = sr_koops_stacktrace_dup(this->stacktrace);
+    if (!stacktrace)
         return NULL;
 
-    return (PyObject*)bo;
+    return koops_stacktrace_to_python_obj(stacktrace);
 }
 
 PyObject *
diff --git a/python/py_koops_stacktrace.h b/python/py_koops_stacktrace.h
index 162ac5e..e72ab54 100644
--- a/python/py_koops_stacktrace.h
+++ b/python/py_koops_stacktrace.h
@@ -46,6 +46,9 @@ struct sr_py_koops_stacktrace
     PyObject *taint_flags;
 };
 
+/* helpers */
+PyObject *koops_stacktrace_to_python_obj(struct sr_koops_stacktrace *stacktrace);
+
 /* constructor */
 PyObject *sr_py_koops_stacktrace_new(PyTypeObject *object,
                                      PyObject *args,
diff --git a/python/py_python_stacktrace.c b/python/py_python_stacktrace.c
index 1b45e64..65cba81 100644
--- a/python/py_python_stacktrace.c
+++ b/python/py_python_stacktrace.c
@@ -105,45 +105,52 @@ PyTypeObject sr_py_python_stacktrace_type = {
     NULL,                           /* tp_weaklist */
 };
 
-/* constructor */
 PyObject *
-sr_py_python_stacktrace_new(PyTypeObject *object,
-                            PyObject *args,
-                            PyObject *kwds)
+python_stacktrace_to_python_obj(struct sr_python_stacktrace *stacktrace)
 {
-    struct sr_py_python_stacktrace *bo = (struct sr_py_python_stacktrace*)
-        PyObject_New(struct sr_py_python_stacktrace,
-                     &sr_py_python_stacktrace_type);
-
+    struct sr_py_python_stacktrace *bo = PyObject_New(struct sr_py_python_stacktrace,
+                                                      &sr_py_python_stacktrace_type);
     if (!bo)
         return PyErr_NoMemory();
 
     bo->frame_type = &sr_py_python_frame_type;
 
+    bo->stacktrace = stacktrace;
+    bo->frames = frames_to_python_list((struct sr_thread *)bo->stacktrace,
+                                       bo->frame_type);
+    if (!bo->frames)
+        return NULL;
+
+    return (PyObject *)bo;
+}
+
+/* constructor */
+PyObject *
+sr_py_python_stacktrace_new(PyTypeObject *object,
+                            PyObject *args,
+                            PyObject *kwds)
+{
     const char *str = NULL;
     if (!PyArg_ParseTuple(args, "|s", &str))
         return NULL;
 
+    struct sr_python_stacktrace *stacktrace;
+
     if (str)
     {
         struct sr_location location;
         sr_location_init(&location);
-        bo->stacktrace = sr_python_stacktrace_parse(&str, &location);
-        if (!bo->stacktrace)
+        stacktrace = sr_python_stacktrace_parse(&str, &location);
+        if (!stacktrace)
         {
             PyErr_SetString(PyExc_ValueError, location.message);
             return NULL;
         }
-
-        bo->frames = frames_to_python_list((struct sr_thread *)bo->stacktrace, bo->frame_type);
     }
     else
-    {
-        bo->stacktrace = sr_python_stacktrace_new();
-        bo->frames = PyList_New(0);
-    }
+        stacktrace = sr_python_stacktrace_new();
 
-    return (PyObject *)bo;
+    return python_stacktrace_to_python_obj(stacktrace);
 }
 
 /* destructor */
@@ -180,23 +187,11 @@ sr_py_python_stacktrace_dup(PyObject *self, PyObject *args)
     if (frames_prepare_linked_list((struct sr_py_base_thread *)this) < 0)
         return NULL;
 
-    struct sr_py_python_stacktrace *bo = (struct sr_py_python_stacktrace*)
-        PyObject_New(struct sr_py_python_stacktrace,
-                     &sr_py_python_stacktrace_type);
-
-    if (!bo)
-        return PyErr_NoMemory();
-
-    bo->frame_type = &sr_py_python_frame_type;
-    bo->stacktrace = sr_python_stacktrace_dup(this->stacktrace);
-    if (!bo->stacktrace)
-        return NULL;
-
-    bo->frames =  frames_to_python_list((struct sr_thread *)bo->stacktrace, bo->frame_type);
-    if (!bo->frames)
+    struct sr_python_stacktrace *stacktrace = sr_python_stacktrace_dup(this->stacktrace);
+    if (!stacktrace)
         return NULL;
 
-    return (PyObject*)bo;
+    return python_stacktrace_to_python_obj(stacktrace);
 }
 
 /*
diff --git a/python/py_python_stacktrace.h b/python/py_python_stacktrace.h
index faa5e06..a89ad8f 100644
--- a/python/py_python_stacktrace.h
+++ b/python/py_python_stacktrace.h
@@ -45,6 +45,9 @@ struct sr_py_python_stacktrace
     PyTypeObject *frame_type;
 };
 
+/* helpers */
+PyObject *python_stacktrace_to_python_obj(struct sr_python_stacktrace *stacktrace);
+
 /* constructor */
 PyObject *sr_py_python_stacktrace_new(PyTypeObject *object,
                                       PyObject *args,
-- 
1.8.3.1



More information about the Crash-catcher mailing list