[SATYR PATCHv3 2/6] python: base class for frames

Martin Milata mmilata at redhat.com
Mon Jun 24 09:47:30 UTC 2013


A BaseFrame class was added that is now superclass of all the frame
classes. It should contain methods common to all frame types (that can
take advantage of the type-agnostic C api).

Signed-off-by: Martin Milata <mmilata at redhat.com>
---
 python/Makefile.am       |   2 +
 python/py_base_frame.c   | 117 +++++++++++++++++++++++++++++++++++++++++++++++
 python/py_base_frame.h   |  51 +++++++++++++++++++++
 python/py_gdb_frame.c    |  14 ++----
 python/py_gdb_frame.h    |   8 ++--
 python/py_java_frame.c   |  14 ++----
 python/py_java_frame.h   |   8 ++--
 python/py_koops_frame.c  |  14 ++----
 python/py_koops_frame.h  |   8 ++--
 python/py_module.c       |  11 +++++
 python/py_python_frame.c |  14 ++----
 python/py_python_frame.h |   8 ++--
 12 files changed, 205 insertions(+), 64 deletions(-)
 create mode 100644 python/py_base_frame.c
 create mode 100644 python/py_base_frame.h

diff --git a/python/Makefile.am b/python/Makefile.am
index 8387aef..49ef936 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -8,6 +8,8 @@ clientexec_LTLIBRARIES = _satyr.la
 _satyr_la_SOURCES = \
     py_cluster.h \
     py_cluster.c \
+    py_base_frame.h \
+    py_base_frame.c \
     py_gdb_frame.h \
     py_gdb_frame.c \
     py_gdb_sharedlib.h \
diff --git a/python/py_base_frame.c b/python/py_base_frame.c
new file mode 100644
index 0000000..7c614c7
--- /dev/null
+++ b/python/py_base_frame.c
@@ -0,0 +1,117 @@
+/*
+    py_base_frame.c
+
+    Copyright (C) 2013 Red Hat, Inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+#include "py_common.h"
+#include "py_base_frame.h"
+
+#include <inttypes.h>
+
+#include "frame.h"
+#include "strbuf.h"
+
+#define frame_doc "satyr.BaseFrame - base class for call frames"
+
+#define f_short_string_doc "Usage: frame.short_string()\n" \
+                           "Returns: string - brief textual representation of the frame"
+
+static PyMethodDef
+frame_methods[] =
+{
+    /* methods */
+    { "short_string", sr_py_base_frame_short_string, METH_NOARGS, f_short_string_doc },
+    { NULL },
+};
+
+PyTypeObject
+sr_py_base_frame_type =
+{
+    PyObject_HEAD_INIT(NULL)
+    0,
+    "satyr.BaseFrame",          /* tp_name */
+    sizeof(struct sr_py_base_frame),        /* tp_basicsize */
+    0,                          /* tp_itemsize */
+    NULL,                       /* tp_dealloc */
+    NULL,                       /* tp_print */
+    NULL,                       /* tp_getattr */
+    NULL,                       /* tp_setattr */
+    (cmpfunc)sr_py_base_frame_cmp, /* tp_compare */
+    NULL,                       /* tp_repr */
+    NULL,                       /* tp_as_number */
+    NULL,                       /* tp_as_sequence */
+    NULL,                       /* tp_as_mapping */
+    NULL,                       /* tp_hash */
+    NULL,                       /* tp_call */
+    NULL,                       /* tp_str */
+    NULL,                       /* tp_getattro */
+    NULL,                       /* tp_setattro */
+    NULL,                       /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+    frame_doc,                  /* tp_doc */
+    NULL,                       /* tp_traverse */
+    NULL,                       /* tp_clear */
+    NULL,                       /* tp_richcompare */
+    0,                          /* tp_weaklistoffset */
+    NULL,                       /* tp_iter */
+    NULL,                       /* tp_iternext */
+    frame_methods,              /* tp_methods */
+    NULL,                       /* tp_members */
+    NULL,                       /* tp_getset */
+    NULL,                       /* tp_base */
+    NULL,                       /* tp_dict */
+    NULL,                       /* tp_descr_get */
+    NULL,                       /* tp_descr_set */
+    0,                          /* tp_dictoffset */
+    NULL,                       /* tp_init */ //TODO throw an exception?
+    NULL,                       /* tp_alloc */
+    NULL,                       /* tp_new */
+    NULL,                       /* tp_free */
+    NULL,                       /* tp_is_gc */
+    NULL,                       /* tp_bases */
+    NULL,                       /* tp_mro */
+    NULL,                       /* tp_cache */
+    NULL,                       /* tp_subclasses */
+    NULL,                       /* tp_weaklist */
+};
+
+/* methods */
+PyObject *
+sr_py_base_frame_short_string(PyObject *self, PyObject *args)
+{
+    struct sr_frame *frame = ((struct sr_py_base_frame*)self)->frame;
+    struct sr_strbuf *strbuf = sr_strbuf_new();
+
+    sr_frame_append_to_str(frame, strbuf);
+    char *str = sr_strbuf_free_nobuf(strbuf);
+
+    PyObject *result = PyString_FromString(str);
+    free(str);
+    return result;
+}
+
+int
+sr_py_base_frame_cmp(struct sr_py_base_frame *self, struct sr_py_base_frame *other)
+{
+    if (self->ob_type != other->ob_type)
+    {
+        /* distinct types must be unequal */
+        return normalize_cmp(self->ob_type - other->ob_type);
+    }
+
+    return normalize_cmp(sr_frame_cmp(self->frame, other->frame));
+}
diff --git a/python/py_base_frame.h b/python/py_base_frame.h
new file mode 100644
index 0000000..98e18f9
--- /dev/null
+++ b/python/py_base_frame.h
@@ -0,0 +1,51 @@
+/*
+    py_base_frame.h
+
+    Copyright (C) 2013 Red Hat, Inc.
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+#ifndef SATYR_PY_BASE_FRAME_H
+#define SATYR_PY_BASE_FRAME_H
+
+/**
+ * @file
+ * @brief Base class for call frames.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <Python.h>
+#include <structmember.h>
+
+extern PyTypeObject sr_py_base_frame_type;
+
+struct sr_py_base_frame
+{
+    PyObject_HEAD
+    struct sr_frame *frame;
+};
+
+int sr_py_base_frame_cmp(struct sr_py_base_frame *self, struct sr_py_base_frame *other);
+
+PyObject *sr_py_base_frame_short_string(PyObject *self, PyObject *args);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/python/py_gdb_frame.c b/python/py_gdb_frame.c
index e052294..839c8f0 100644
--- a/python/py_gdb_frame.c
+++ b/python/py_gdb_frame.c
@@ -19,6 +19,7 @@
 */
 #include "py_common.h"
 #include "py_gdb_frame.h"
+#include "py_base_frame.h"
 
 #include <inttypes.h>
 
@@ -134,7 +135,7 @@ sr_py_gdb_frame_type =
     NULL,                       /* tp_print */
     NULL,                       /* tp_getattr */
     NULL,                       /* tp_setattr */
-    sr_py_gdb_frame_cmp,        /* tp_compare */
+    NULL,                       /* tp_compare */
     NULL,                       /* tp_repr */
     NULL,                       /* tp_as_number */
     NULL,                       /* tp_as_sequence */
@@ -156,7 +157,7 @@ sr_py_gdb_frame_type =
     frame_methods,              /* tp_methods */
     NULL,                       /* tp_members */
     frame_getset,               /* tp_getset */
-    NULL,                       /* tp_base */
+    &sr_py_base_frame_type,     /* tp_base */
     NULL,                       /* tp_dict */
     NULL,                       /* tp_descr_get */
     NULL,                       /* tp_descr_set */
@@ -253,15 +254,6 @@ sr_py_gdb_frame_dup(PyObject *self, PyObject *args)
     return (PyObject*)fo;
 }
 
-int
-sr_py_gdb_frame_cmp(PyObject *self, PyObject *other)
-{
-    struct sr_gdb_frame *f1 = ((struct sr_py_gdb_frame*)self)->frame;
-    struct sr_gdb_frame *f2 = ((struct sr_py_gdb_frame*)other)->frame;
-
-    return normalize_cmp(sr_gdb_frame_cmp(f1, f2, true));
-}
-
 PyObject *
 sr_py_gdb_frame_calls_func(PyObject *self, PyObject *args)
 {
diff --git a/python/py_gdb_frame.h b/python/py_gdb_frame.h
index e0ac90c..d73ee51 100644
--- a/python/py_gdb_frame.h
+++ b/python/py_gdb_frame.h
@@ -34,6 +34,9 @@ extern "C" {
 
 PyTypeObject sr_py_gdb_frame_type;
 
+/* The beginning of this structure has to have the same layout as
+ * sr_py_base_frame.
+ */
 struct sr_py_gdb_frame
 {
     PyObject_HEAD
@@ -56,11 +59,6 @@ void sr_py_gdb_frame_free(PyObject *object);
  */
 PyObject *sr_py_gdb_frame_str(PyObject *self);
 
-/**
- * cmp
- */
-int sr_py_gdb_frame_cmp(PyObject *self, PyObject *other);
-
 /* methods */
 PyObject *sr_py_gdb_frame_dup(PyObject *self, PyObject *args);
 PyObject *sr_py_gdb_frame_calls_func(PyObject *self, PyObject *args);
diff --git a/python/py_java_frame.c b/python/py_java_frame.c
index ccbbb04..602cf73 100644
--- a/python/py_java_frame.c
+++ b/python/py_java_frame.c
@@ -18,6 +18,7 @@
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 #include "py_common.h"
+#include "py_base_frame.h"
 #include "py_java_frame.h"
 #include "location.h"
 #include "strbuf.h"
@@ -82,7 +83,7 @@ sr_py_java_frame_type =
     NULL,                       /* tp_print */
     NULL,                       /* tp_getattr */
     NULL,                       /* tp_setattr */
-    sr_py_java_frame_cmp,       /* tp_compare */
+    NULL,                       /* tp_compare */
     NULL,                       /* tp_repr */
     NULL,                       /* tp_as_number */
     NULL,                       /* tp_as_sequence */
@@ -104,7 +105,7 @@ sr_py_java_frame_type =
     frame_methods,              /* tp_methods */
     NULL,                       /* tp_members */
     frame_getset,               /* tp_getset */
-    NULL,                       /* tp_base */
+    &sr_py_base_frame_type,     /* tp_base */
     NULL,                       /* tp_dict */
     NULL,                       /* tp_descr_get */
     NULL,                       /* tp_descr_set */
@@ -220,12 +221,3 @@ sr_py_java_frame_dup(PyObject *self, PyObject *args)
 
     return (PyObject*)fo;
 }
-
-int
-sr_py_java_frame_cmp(PyObject *self, PyObject *other)
-{
-    struct sr_java_frame *f1 = ((struct sr_py_java_frame*)self)->frame;
-    struct sr_java_frame *f2 = ((struct sr_py_java_frame*)other)->frame;
-
-    return normalize_cmp(sr_java_frame_cmp(f1, f2));
-}
diff --git a/python/py_java_frame.h b/python/py_java_frame.h
index c71d655..9f30eb5 100644
--- a/python/py_java_frame.h
+++ b/python/py_java_frame.h
@@ -34,6 +34,9 @@ extern "C" {
 
 PyTypeObject sr_py_java_frame_type;
 
+/* The beginning of this structure has to have the same layout as
+ * sr_py_base_frame.
+ */
 struct sr_py_java_frame
 {
     PyObject_HEAD
@@ -56,11 +59,6 @@ void sr_py_java_frame_free(PyObject *object);
  */
 PyObject *sr_py_java_frame_str(PyObject *self);
 
-/**
- * cmp
- */
-int sr_py_java_frame_cmp(PyObject *self, PyObject *other);
-
 /* methods */
 PyObject *sr_py_java_frame_dup(PyObject *self, PyObject *args);
 
diff --git a/python/py_koops_frame.c b/python/py_koops_frame.c
index 092fb78..eae74e6 100644
--- a/python/py_koops_frame.c
+++ b/python/py_koops_frame.c
@@ -18,6 +18,7 @@
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 #include "py_common.h"
+#include "py_base_frame.h"
 #include "py_koops_frame.h"
 
 #include <inttypes.h>
@@ -93,7 +94,7 @@ sr_py_koops_frame_type =
     NULL,                       /* tp_print */
     NULL,                       /* tp_getattr */
     NULL,                       /* tp_setattr */
-    sr_py_koops_frame_cmp,      /* tp_compare */
+    NULL,                       /* tp_compare */
     NULL,                       /* tp_repr */
     NULL,                       /* tp_as_number */
     NULL,                       /* tp_as_sequence */
@@ -115,7 +116,7 @@ sr_py_koops_frame_type =
     frame_methods,              /* tp_methods */
     NULL,                       /* tp_members */
     frame_getset,               /* tp_getset */
-    NULL,                       /* tp_base */
+    &sr_py_base_frame_type,     /* tp_base */
     NULL,                       /* tp_dict */
     NULL,                       /* tp_descr_get */
     NULL,                       /* tp_descr_set */
@@ -228,12 +229,3 @@ sr_py_koops_frame_dup(PyObject *self, PyObject *args)
 
     return (PyObject*)fo;
 }
-
-int
-sr_py_koops_frame_cmp(PyObject *self, PyObject *other)
-{
-    struct sr_koops_frame *f1 = ((struct sr_py_koops_frame*)self)->frame;
-    struct sr_koops_frame *f2 = ((struct sr_py_koops_frame*)other)->frame;
-
-    return normalize_cmp(sr_koops_frame_cmp(f1, f2));
-}
diff --git a/python/py_koops_frame.h b/python/py_koops_frame.h
index ecbf874..81174de 100644
--- a/python/py_koops_frame.h
+++ b/python/py_koops_frame.h
@@ -34,6 +34,9 @@ extern "C" {
 
 PyTypeObject sr_py_koops_frame_type;
 
+/* The beginning of this structure has to have the same layout as
+ * sr_py_base_frame.
+ */
 struct sr_py_koops_frame
 {
     PyObject_HEAD
@@ -56,11 +59,6 @@ void sr_py_koops_frame_free(PyObject *object);
  */
 PyObject *sr_py_koops_frame_str(PyObject *self);
 
-/*
- * cmp
- */
-int sr_py_koops_frame_cmp(PyObject *self, PyObject *other);
-
 /* methods */
 PyObject *sr_py_koops_frame_dup(PyObject *self, PyObject *args);
 
diff --git a/python/py_module.c b/python/py_module.c
index 32fd313..4c7231e 100644
--- a/python/py_module.c
+++ b/python/py_module.c
@@ -1,5 +1,6 @@
 #include <Python.h>
 #include "py_cluster.h"
+#include "py_base_frame.h"
 #include "py_gdb_frame.h"
 #include "py_gdb_sharedlib.h"
 #include "py_gdb_stacktrace.h"
@@ -28,6 +29,12 @@ module_methods[]=
 PyMODINIT_FUNC
 init_satyr()
 {
+    if (PyType_Ready(&sr_py_base_frame_type) < 0)
+    {
+        puts("PyType_Ready(&sr_py_base_frame_type) < 0");
+        return;
+    }
+
     if (PyType_Ready(&sr_py_gdb_frame_type) < 0)
     {
         puts("PyType_Ready(&sr_py_gdb_frame_type) < 0");
@@ -114,6 +121,10 @@ init_satyr()
         return;
     }
 
+    Py_INCREF(&sr_py_base_frame_type);
+    PyModule_AddObject(module, "BaseFrame",
+                       (PyObject *)&sr_py_base_frame_type);
+
     Py_INCREF(&sr_py_gdb_frame_type);
     PyModule_AddObject(module, "GdbFrame",
                        (PyObject *)&sr_py_gdb_frame_type);
diff --git a/python/py_python_frame.c b/python/py_python_frame.c
index 3ba156d..4ff160a 100644
--- a/python/py_python_frame.c
+++ b/python/py_python_frame.c
@@ -18,6 +18,7 @@
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 #include "py_common.h"
+#include "py_base_frame.h"
 #include "py_python_frame.h"
 #include "location.h"
 #include "strbuf.h"
@@ -80,7 +81,7 @@ sr_py_python_frame_type =
     NULL,                       /* tp_print */
     NULL,                       /* tp_getattr */
     NULL,                       /* tp_setattr */
-    sr_py_python_frame_cmp,     /* tp_compare */
+    NULL,                       /* tp_compare */
     NULL,                       /* tp_repr */
     NULL,                       /* tp_as_number */
     NULL,                       /* tp_as_sequence */
@@ -102,7 +103,7 @@ sr_py_python_frame_type =
     frame_methods,              /* tp_methods */
     NULL,                       /* tp_members */
     frame_getset,               /* tp_getset */
-    NULL,                       /* tp_base */
+    &sr_py_base_frame_type,     /* tp_base */
     NULL,                       /* tp_dict */
     NULL,                       /* tp_descr_get */
     NULL,                       /* tp_descr_set */
@@ -207,12 +208,3 @@ sr_py_python_frame_dup(PyObject *self, PyObject *args)
 
     return (PyObject*)fo;
 }
-
-int
-sr_py_python_frame_cmp(PyObject *self, PyObject *other)
-{
-    struct sr_python_frame *f1 = ((struct sr_py_python_frame*)self)->frame;
-    struct sr_python_frame *f2 = ((struct sr_py_python_frame*)other)->frame;
-
-    return normalize_cmp(sr_python_frame_cmp(f1, f2));
-}
diff --git a/python/py_python_frame.h b/python/py_python_frame.h
index 736056b..6b4b63f 100644
--- a/python/py_python_frame.h
+++ b/python/py_python_frame.h
@@ -34,6 +34,9 @@ extern "C" {
 
 PyTypeObject sr_py_python_frame_type;
 
+/* The beginning of this structure has to have the same layout as
+ * sr_py_base_frame.
+ */
 struct sr_py_python_frame
 {
     PyObject_HEAD
@@ -56,11 +59,6 @@ void sr_py_python_frame_free(PyObject *object);
  */
 PyObject *sr_py_python_frame_str(PyObject *self);
 
-/**
- * cmp
- */
-int sr_py_python_frame_cmp(PyObject *self, PyObject *other);
-
 /* methods */
 PyObject *sr_py_python_frame_dup(PyObject *self, PyObject *args);
 
-- 
1.7.11.7



More information about the Crash-catcher mailing list