Blame SOURCES/satyr-0.13-python-add-Ruby-support.patch

f41043
From b0c2427cdc59be0bb1fa76e928b17d9aa1a0d4eb Mon Sep 17 00:00:00 2001
f41043
From: Martin Milata <mmilata@redhat.com>
f41043
Date: Wed, 28 Jan 2015 17:31:41 +0100
f41043
Subject: [PATCH] python: add Ruby support
f41043
f41043
Related: #1334604
f41043
f41043
Signed-off-by: Martin Milata <mmilata@redhat.com>
f41043
f41043
Conflicts:
f41043
	tests/Makefile.am
f41043
---
f41043
 python/Makefile.am          |   4 +
f41043
 python/py_module.c          |  22 +++++
f41043
 python/py_report.c          |   9 ++
f41043
 python/py_ruby_frame.c      | 227 ++++++++++++++++++++++++++++++++++++++++++++
f41043
 python/py_ruby_frame.h      |  69 ++++++++++++++
f41043
 python/py_ruby_stacktrace.c | 182 +++++++++++++++++++++++++++++++++++
f41043
 python/py_ruby_stacktrace.h |  71 ++++++++++++++
f41043
 tests/Makefile.am           |  11 ++-
f41043
 tests/python/ruby.py        | 174 +++++++++++++++++++++++++++++++++
f41043
 tests/python_bindings.at    |   1 +
f41043
 10 files changed, 769 insertions(+), 1 deletion(-)
f41043
 create mode 100644 python/py_ruby_frame.c
f41043
 create mode 100644 python/py_ruby_frame.h
f41043
 create mode 100644 python/py_ruby_stacktrace.c
f41043
 create mode 100644 python/py_ruby_stacktrace.h
f41043
 create mode 100755 tests/python/ruby.py
f41043
f41043
diff --git a/python/Makefile.am b/python/Makefile.am
f41043
index 83273a2..9380c12 100644
f41043
--- a/python/Makefile.am
f41043
+++ b/python/Makefile.am
f41043
@@ -44,6 +44,10 @@ _satyr_la_SOURCES = \
f41043
     py_java_thread.c \
f41043
     py_java_stacktrace.h \
f41043
     py_java_stacktrace.c \
f41043
+    py_ruby_frame.h \
f41043
+    py_ruby_frame.c \
f41043
+    py_ruby_stacktrace.h \
f41043
+    py_ruby_stacktrace.c \
f41043
     py_rpm_package.h \
f41043
     py_rpm_package.c \
f41043
     py_metrics.h \
f41043
diff --git a/python/py_module.c b/python/py_module.c
f41043
index 83697ef..144465b 100644
f41043
--- a/python/py_module.c
f41043
+++ b/python/py_module.c
f41043
@@ -17,6 +17,8 @@
f41043
 #include "py_core_frame.h"
f41043
 #include "py_core_thread.h"
f41043
 #include "py_core_stacktrace.h"
f41043
+#include "py_ruby_frame.h"
f41043
+#include "py_ruby_stacktrace.h"
f41043
 #include "py_rpm_package.h"
f41043
 #include "py_metrics.h"
f41043
 #include "py_operating_system.h"
f41043
@@ -187,6 +189,18 @@ init_satyr()
f41043
         return;
f41043
     }
f41043
 
f41043
+    if (PyType_Ready(&sr_py_ruby_frame_type) < 0)
f41043
+    {
f41043
+        puts("PyType_Ready(&sr_py_ruby_frame_type) < 0");
f41043
+        return;
f41043
+    }
f41043
+
f41043
+    if (PyType_Ready(&sr_py_ruby_stacktrace_type) < 0)
f41043
+    {
f41043
+        puts("PyType_Ready(&sr_py_ruby_stacktrace_type) < 0");
f41043
+        return;
f41043
+    }
f41043
+
f41043
     if (PyType_Ready(&sr_py_operating_system_type) < 0)
f41043
     {
f41043
         puts("PyType_Ready(&sr_py_operating_system_type) < 0");
f41043
@@ -301,6 +315,14 @@ init_satyr()
f41043
     PyModule_AddObject(module, "JavaStacktrace",
f41043
                        (PyObject *)&sr_py_java_stacktrace_type);
f41043
 
f41043
+    Py_INCREF(&sr_py_ruby_frame_type);
f41043
+    PyModule_AddObject(module, "RubyFrame",
f41043
+                       (PyObject *)&sr_py_ruby_frame_type);
f41043
+
f41043
+    Py_INCREF(&sr_py_ruby_stacktrace_type);
f41043
+    PyModule_AddObject(module, "RubyStacktrace",
f41043
+                       (PyObject *)&sr_py_ruby_stacktrace_type);
f41043
+
f41043
     Py_INCREF(&sr_py_core_frame_type);
f41043
     PyModule_AddObject(module, "CoreFrame",
f41043
                        (PyObject *)&sr_py_core_frame_type);
f41043
diff --git a/python/py_report.c b/python/py_report.c
f41043
index 8aa35dd..74cd9dd 100644
f41043
--- a/python/py_report.c
f41043
+++ b/python/py_report.c
f41043
@@ -28,6 +28,7 @@
f41043
 #include "py_python_stacktrace.h"
f41043
 #include "py_koops_stacktrace.h"
f41043
 #include "py_java_stacktrace.h"
f41043
+#include "py_ruby_stacktrace.h"
f41043
 
f41043
 #include "report.h"
f41043
 #include "operating_system.h"
f41043
@@ -284,6 +285,10 @@ report_prepare_subobjects(struct sr_py_report *report)
f41043
             if (stacktrace_prepare(report, &sr_py_koops_stacktrace_type, false) < 0)
f41043
                 return -1;
f41043
             break;
f41043
+        case SR_REPORT_RUBY:
f41043
+            if (stacktrace_prepare(report, &sr_py_ruby_stacktrace_type, false) < 0)
f41043
+                return -1;
f41043
+            break;
f41043
         default:
f41043
             report->report->stacktrace = NULL;
f41043
             break;
f41043
@@ -347,6 +352,10 @@ report_to_python_obj(struct sr_report *report)
f41043
             ro->stacktrace = koops_stacktrace_to_python_obj(
f41043
                     (struct sr_koops_stacktrace *)report->stacktrace);
f41043
             break;
f41043
+        case SR_REPORT_RUBY:
f41043
+            ro->stacktrace = ruby_stacktrace_to_python_obj(
f41043
+                    (struct sr_ruby_stacktrace *)report->stacktrace);
f41043
+            break;
f41043
         default:
f41043
             Py_INCREF(Py_None);
f41043
             ro->stacktrace = Py_None;
f41043
diff --git a/python/py_ruby_frame.c b/python/py_ruby_frame.c
f41043
new file mode 100644
f41043
index 0000000..9ed45ad
f41043
--- /dev/null
f41043
+++ b/python/py_ruby_frame.c
f41043
@@ -0,0 +1,227 @@
f41043
+/*
f41043
+    py_ruby_frame.c
f41043
+
f41043
+    Copyright (C) 2015  Red Hat, Inc.
f41043
+
f41043
+    This program is free software; you can redistribute it and/or modify
f41043
+    it under the terms of the GNU General Public License as published by
f41043
+    the Free Software Foundation; either version 2 of the License, or
f41043
+    (at your option) any later version.
f41043
+
f41043
+    This program is distributed in the hope that it will be useful,
f41043
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
f41043
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f41043
+    GNU General Public License for more details.
f41043
+
f41043
+    You should have received a copy of the GNU General Public License along
f41043
+    with this program; if not, write to the Free Software Foundation, Inc.,
f41043
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
f41043
+*/
f41043
+#include "py_common.h"
f41043
+#include "py_base_frame.h"
f41043
+#include "py_ruby_frame.h"
f41043
+#include "location.h"
f41043
+#include "strbuf.h"
f41043
+#include "utils.h"
f41043
+#include "ruby/frame.h"
f41043
+
f41043
+#define frame_doc "satyr.RubyFrame - class representing a ruby frame\n\n" \
f41043
+                  "Usage:\n\n" \
f41043
+                  "satyr.RubyFrame() - creates an empty frame\n\n" \
f41043
+                  "satyr.RubyFrame(str) - parses str and fills the frame object"
f41043
+
f41043
+#define f_dup_doc "Usage: frame.dup()\n\n" \
f41043
+                  "Returns: satyr.RubyFrame - a new clone of frame\n\n" \
f41043
+                  "Clones the frame object. All new structures are independent " \
f41043
+                  "of the original object."
f41043
+
f41043
+
f41043
+static PyMethodDef
f41043
+frame_methods[] =
f41043
+{
f41043
+    /* methods */
f41043
+    { "dup", sr_py_ruby_frame_dup, METH_NOARGS,  f_dup_doc },
f41043
+    { NULL },
f41043
+};
f41043
+
f41043
+/* See python/py_common.h and python/py_gdb_frame.c for generic getters/setters documentation. */
f41043
+#define GSOFF_PY_STRUCT sr_py_ruby_frame
f41043
+#define GSOFF_PY_MEMBER frame
f41043
+#define GSOFF_C_STRUCT sr_ruby_frame
f41043
+GSOFF_START
f41043
+GSOFF_MEMBER(file_name),
f41043
+GSOFF_MEMBER(file_line),
f41043
+GSOFF_MEMBER(special_function),
f41043
+GSOFF_MEMBER(function_name),
f41043
+GSOFF_MEMBER(block_level),
f41043
+GSOFF_MEMBER(rescue_level)
f41043
+GSOFF_END
f41043
+
f41043
+static PyGetSetDef
f41043
+frame_getset[] =
f41043
+{
f41043
+    SR_ATTRIBUTE_STRING(file_name,        "Source file name (string)"                              ),
f41043
+    SR_ATTRIBUTE_UINT32(file_line,        "Source line number (positive integer)"                  ),
f41043
+    SR_ATTRIBUTE_BOOL  (special_function, "True if the frame doesn't belong to an actual function" ),
f41043
+    SR_ATTRIBUTE_STRING(function_name,    "Function name (string)"                                 ),
f41043
+    SR_ATTRIBUTE_UINT32(block_level,      "Block nesting level (integer)"                          ),
f41043
+    SR_ATTRIBUTE_UINT32(rescue_level,     "Rescue block nesting level (integer)"                   ),
f41043
+    { NULL },
f41043
+};
f41043
+
f41043
+PyTypeObject
f41043
+sr_py_ruby_frame_type =
f41043
+{
f41043
+    PyObject_HEAD_INIT(NULL)
f41043
+    0,
f41043
+    "satyr.RubyFrame",          /* tp_name */
f41043
+    sizeof(struct sr_py_ruby_frame), /* tp_basicsize */
f41043
+    0,                          /* tp_itemsize */
f41043
+    sr_py_ruby_frame_free,      /* tp_dealloc */
f41043
+    NULL,                       /* tp_print */
f41043
+    NULL,                       /* tp_getattr */
f41043
+    NULL,                       /* tp_setattr */
f41043
+    NULL,                       /* tp_compare */
f41043
+    NULL,                       /* tp_repr */
f41043
+    NULL,                       /* tp_as_number */
f41043
+    NULL,                       /* tp_as_sequence */
f41043
+    NULL,                       /* tp_as_mapping */
f41043
+    NULL,                       /* tp_hash */
f41043
+    NULL,                       /* tp_call */
f41043
+    sr_py_ruby_frame_str,       /* tp_str */
f41043
+    NULL,                       /* tp_getattro */
f41043
+    NULL,                       /* tp_setattro */
f41043
+    NULL,                       /* tp_as_buffer */
f41043
+    Py_TPFLAGS_DEFAULT,         /* tp_flags */
f41043
+    frame_doc,                  /* tp_doc */
f41043
+    NULL,                       /* tp_traverse */
f41043
+    NULL,                       /* tp_clear */
f41043
+    NULL,                       /* tp_richcompare */
f41043
+    0,                          /* tp_weaklistoffset */
f41043
+    NULL,                       /* tp_iter */
f41043
+    NULL,                       /* tp_iternext */
f41043
+    frame_methods,              /* tp_methods */
f41043
+    NULL,                       /* tp_members */
f41043
+    frame_getset,               /* tp_getset */
f41043
+    &sr_py_base_frame_type,     /* tp_base */
f41043
+    NULL,                       /* tp_dict */
f41043
+    NULL,                       /* tp_descr_get */
f41043
+    NULL,                       /* tp_descr_set */
f41043
+    0,                          /* tp_dictoffset */
f41043
+    NULL,                       /* tp_init */
f41043
+    NULL,                       /* tp_alloc */
f41043
+    sr_py_ruby_frame_new,       /* tp_new */
f41043
+    NULL,                       /* tp_free */
f41043
+    NULL,                       /* tp_is_gc */
f41043
+    NULL,                       /* tp_bases */
f41043
+    NULL,                       /* tp_mro */
f41043
+    NULL,                       /* tp_cache */
f41043
+    NULL,                       /* tp_subclasses */
f41043
+    NULL,                       /* tp_weaklist */
f41043
+};
f41043
+
f41043
+/* constructor */
f41043
+PyObject *
f41043
+sr_py_ruby_frame_new(PyTypeObject *object, PyObject *args, PyObject *kwds)
f41043
+{
f41043
+    struct sr_py_ruby_frame *fo = (struct sr_py_ruby_frame*)
f41043
+        PyObject_New(struct sr_py_ruby_frame, &sr_py_ruby_frame_type);
f41043
+
f41043
+    if (!fo)
f41043
+        return PyErr_NoMemory();
f41043
+
f41043
+    const char *str = NULL;
f41043
+    if (!PyArg_ParseTuple(args, "|s", &str))
f41043
+        return NULL;
f41043
+
f41043
+    if (str)
f41043
+    {
f41043
+        struct sr_location location;
f41043
+        sr_location_init(&location);
f41043
+        fo->frame = sr_ruby_frame_parse(&str, &location);
f41043
+
f41043
+        if (!fo->frame)
f41043
+        {
f41043
+            PyErr_SetString(PyExc_ValueError, location.message);
f41043
+            return NULL;
f41043
+        }
f41043
+    }
f41043
+    else
f41043
+        fo->frame = sr_ruby_frame_new();
f41043
+
f41043
+    return (PyObject*)fo;
f41043
+}
f41043
+
f41043
+/* destructor */
f41043
+void
f41043
+sr_py_ruby_frame_free(PyObject *object)
f41043
+{
f41043
+    struct sr_py_ruby_frame *this = (struct sr_py_ruby_frame*)object;
f41043
+    sr_ruby_frame_free(this->frame);
f41043
+    PyObject_Del(object);
f41043
+}
f41043
+
f41043
+/* str */
f41043
+PyObject *
f41043
+sr_py_ruby_frame_str(PyObject *self)
f41043
+{
f41043
+    struct sr_py_ruby_frame *this = (struct sr_py_ruby_frame*)self;
f41043
+    struct sr_strbuf *buf = sr_strbuf_new();
f41043
+
f41043
+
f41043
+    if (this->frame->file_name)
f41043
+    {
f41043
+        sr_strbuf_append_str(buf, this->frame->file_name);
f41043
+    }
f41043
+
f41043
+    if (this->frame->file_line)
f41043
+    {
f41043
+        sr_strbuf_append_strf(buf, ":%d", this->frame->file_line);
f41043
+    }
f41043
+
f41043
+    if (this->frame->function_name)
f41043
+    {
f41043
+        sr_strbuf_append_str(buf, ":in `");
f41043
+
f41043
+        int i;
f41043
+        for (i = 0; i < this->frame->rescue_level; i++)
f41043
+        {
f41043
+            sr_strbuf_append_str(buf, "rescue in ");
f41043
+        }
f41043
+
f41043
+        if (this->frame->block_level == 1)
f41043
+        {
f41043
+            sr_strbuf_append_str(buf, "block in ");
f41043
+        }
f41043
+        else if (this->frame->block_level > 1)
f41043
+        {
f41043
+            sr_strbuf_append_strf(buf, "block (%d levels) in ", this->frame->block_level);
f41043
+        }
f41043
+
f41043
+        sr_strbuf_append_strf(buf, "%s%s%s'",
f41043
+                              (this->frame->special_function ? "<" : ""),
f41043
+                              this->frame->function_name,
f41043
+                              (this->frame->special_function ? ">" : ""));
f41043
+    }
f41043
+
f41043
+    char *str = sr_strbuf_free_nobuf(buf);
f41043
+    PyObject *result = Py_BuildValue("s", str);
f41043
+    free(str);
f41043
+    return result;
f41043
+}
f41043
+
f41043
+/* methods */
f41043
+PyObject *
f41043
+sr_py_ruby_frame_dup(PyObject *self, PyObject *args)
f41043
+{
f41043
+    struct sr_py_ruby_frame *this = (struct sr_py_ruby_frame*)self;
f41043
+    struct sr_py_ruby_frame *fo = (struct sr_py_ruby_frame*)
f41043
+        PyObject_New(struct sr_py_ruby_frame, &sr_py_ruby_frame_type);
f41043
+
f41043
+    if (!fo)
f41043
+        return PyErr_NoMemory();
f41043
+
f41043
+    fo->frame = sr_ruby_frame_dup(this->frame, false);
f41043
+
f41043
+    return (PyObject*)fo;
f41043
+}
f41043
diff --git a/python/py_ruby_frame.h b/python/py_ruby_frame.h
f41043
new file mode 100644
f41043
index 0000000..7f460ff
f41043
--- /dev/null
f41043
+++ b/python/py_ruby_frame.h
f41043
@@ -0,0 +1,69 @@
f41043
+/*
f41043
+    py_ruby_frame.h
f41043
+
f41043
+    Copyright (C) 2015  Red Hat, Inc.
f41043
+
f41043
+    This program is free software; you can redistribute it and/or modify
f41043
+    it under the terms of the GNU General Public License as published by
f41043
+    the Free Software Foundation; either version 2 of the License, or
f41043
+    (at your option) any later version.
f41043
+
f41043
+    This program is distributed in the hope that it will be useful,
f41043
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
f41043
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f41043
+    GNU General Public License for more details.
f41043
+
f41043
+    You should have received a copy of the GNU General Public License along
f41043
+    with this program; if not, write to the Free Software Foundation, Inc.,
f41043
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
f41043
+*/
f41043
+#ifndef SATYR_PY_RUBY_FRAME_H
f41043
+#define SATYR_PY_RUBY_FRAME_H
f41043
+
f41043
+/**
f41043
+ * @file
f41043
+ * @brief Python bindings for RUBY frame.
f41043
+ */
f41043
+
f41043
+#ifdef __cplusplus
f41043
+extern "C" {
f41043
+#endif
f41043
+
f41043
+#include <Python.h>
f41043
+#include <structmember.h>
f41043
+
f41043
+PyTypeObject sr_py_ruby_frame_type;
f41043
+
f41043
+/* The beginning of this structure has to have the same layout as
f41043
+ * sr_py_base_frame.
f41043
+ */
f41043
+struct sr_py_ruby_frame
f41043
+{
f41043
+    PyObject_HEAD
f41043
+    struct sr_ruby_frame *frame;
f41043
+};
f41043
+
f41043
+/**
f41043
+ * Constructor.
f41043
+ */
f41043
+PyObject *sr_py_ruby_frame_new(PyTypeObject *object,
f41043
+                               PyObject *args, PyObject *kwds);
f41043
+
f41043
+/**
f41043
+ * Destructor.
f41043
+ */
f41043
+void sr_py_ruby_frame_free(PyObject *object);
f41043
+
f41043
+/**
f41043
+ * str
f41043
+ */
f41043
+PyObject *sr_py_ruby_frame_str(PyObject *self);
f41043
+
f41043
+/* methods */
f41043
+PyObject *sr_py_ruby_frame_dup(PyObject *self, PyObject *args);
f41043
+
f41043
+#ifdef __cplusplus
f41043
+}
f41043
+#endif
f41043
+
f41043
+#endif
f41043
diff --git a/python/py_ruby_stacktrace.c b/python/py_ruby_stacktrace.c
f41043
new file mode 100644
f41043
index 0000000..97fbb44
f41043
--- /dev/null
f41043
+++ b/python/py_ruby_stacktrace.c
f41043
@@ -0,0 +1,182 @@
f41043
+#include "py_common.h"
f41043
+#include "py_ruby_frame.h"
f41043
+#include "py_ruby_stacktrace.h"
f41043
+#include "py_base_stacktrace.h"
f41043
+#include "utils.h"
f41043
+#include "strbuf.h"
f41043
+#include "ruby/frame.h"
f41043
+#include "ruby/stacktrace.h"
f41043
+#include "location.h"
f41043
+#include "stacktrace.h"
f41043
+
f41043
+#define stacktrace_doc "satyr.RubyStacktrace - class representing a ruby stacktrace\n\n" \
f41043
+                       "Usage:\n\n" \
f41043
+                       "satyr.RubyStacktrace() - creates an empty ruby stacktrace\n\n" \
f41043
+                       "satyr.RubyStacktrace(str) - parses str and fills the ruby stacktrace object"
f41043
+
f41043
+#define f_dup_doc "Usage: stacktrace.dup()\n\n" \
f41043
+                  "Returns: satyr.RubyStacktrace - a new clone of ruby stacktrace\n\n" \
f41043
+                  "Clones the RubyStacktrace object. All new structures are independent " \
f41043
+                  "of the original object."
f41043
+
f41043
+
f41043
+static PyMethodDef
f41043
+ruby_stacktrace_methods[] =
f41043
+{
f41043
+    /* methods */
f41043
+    { "dup", sr_py_ruby_stacktrace_dup, METH_NOARGS, f_dup_doc },
f41043
+    { NULL },
f41043
+};
f41043
+
f41043
+/* See python/py_common.h and python/py_gdb_frame.c for generic getters/setters documentation. */
f41043
+#define GSOFF_PY_STRUCT sr_py_ruby_stacktrace
f41043
+#define GSOFF_PY_MEMBER stacktrace
f41043
+#define GSOFF_C_STRUCT sr_ruby_stacktrace
f41043
+GSOFF_START
f41043
+GSOFF_MEMBER(exception_name)
f41043
+GSOFF_END
f41043
+
f41043
+static PyGetSetDef
f41043
+ruby_stacktrace_getset[] =
f41043
+{
f41043
+    SR_ATTRIBUTE_STRING(exception_name, "Exception type (string)"              ),
f41043
+    { NULL },
f41043
+};
f41043
+
f41043
+PyTypeObject sr_py_ruby_stacktrace_type = {
f41043
+    PyObject_HEAD_INIT(NULL)
f41043
+    0,
f41043
+    "satyr.RubyStacktrace",         /* tp_name */
f41043
+    sizeof(struct sr_py_ruby_stacktrace), /* tp_basicsize */
f41043
+    0,                              /* tp_itemsize */
f41043
+    sr_py_ruby_stacktrace_free,     /* tp_dealloc */
f41043
+    NULL,                           /* tp_print */
f41043
+    NULL,                           /* tp_getattr */
f41043
+    NULL,                           /* tp_setattr */
f41043
+    NULL,                           /* tp_compare */
f41043
+    NULL,                           /* tp_repr */
f41043
+    NULL,                           /* tp_as_number */
f41043
+    NULL,                           /* tp_as_sequence */
f41043
+    NULL,                           /* tp_as_mapping */
f41043
+    NULL,                           /* tp_hash */
f41043
+    NULL,                           /* tp_call */
f41043
+    sr_py_ruby_stacktrace_str,      /* tp_str */
f41043
+    NULL,                           /* tp_getattro */
f41043
+    NULL,                           /* tp_setattro */
f41043
+    NULL,                           /* tp_as_buffer */
f41043
+    Py_TPFLAGS_DEFAULT,             /* tp_flags */
f41043
+    stacktrace_doc,                 /* tp_doc */
f41043
+    NULL,                           /* tp_traverse */
f41043
+    NULL,                           /* tp_clear */
f41043
+    NULL,                           /* tp_richcompare */
f41043
+    0,                              /* tp_weaklistoffset */
f41043
+    NULL,                           /* tp_iter */
f41043
+    NULL,                           /* tp_iternext */
f41043
+    ruby_stacktrace_methods,        /* tp_methods */
f41043
+    NULL,                           /* tp_members */
f41043
+    ruby_stacktrace_getset,         /* tp_getset */
f41043
+    &sr_py_single_stacktrace_type,  /* tp_base */
f41043
+    NULL,                           /* tp_dict */
f41043
+    NULL,                           /* tp_descr_get */
f41043
+    NULL,                           /* tp_descr_set */
f41043
+    0,                              /* tp_dictoffset */
f41043
+    NULL,                           /* tp_init */
f41043
+    NULL,                           /* tp_alloc */
f41043
+    sr_py_ruby_stacktrace_new,      /* tp_new */
f41043
+    NULL,                           /* tp_free */
f41043
+    NULL,                           /* tp_is_gc */
f41043
+    NULL,                           /* tp_bases */
f41043
+    NULL,                           /* tp_mro */
f41043
+    NULL,                           /* tp_cache */
f41043
+    NULL,                           /* tp_subclasses */
f41043
+    NULL,                           /* tp_weaklist */
f41043
+};
f41043
+
f41043
+PyObject *
f41043
+ruby_stacktrace_to_python_obj(struct sr_ruby_stacktrace *stacktrace)
f41043
+{
f41043
+    struct sr_py_ruby_stacktrace *bo = PyObject_New(struct sr_py_ruby_stacktrace,
f41043
+                                                    &sr_py_ruby_stacktrace_type);
f41043
+    if (!bo)
f41043
+        return PyErr_NoMemory();
f41043
+
f41043
+    bo->frame_type = &sr_py_ruby_frame_type;
f41043
+
f41043
+    bo->stacktrace = stacktrace;
f41043
+    bo->frames = frames_to_python_list((struct sr_thread *)bo->stacktrace,
f41043
+                                       bo->frame_type);
f41043
+    if (!bo->frames)
f41043
+        return NULL;
f41043
+
f41043
+    return (PyObject *)bo;
f41043
+}
f41043
+
f41043
+/* constructor */
f41043
+PyObject *
f41043
+sr_py_ruby_stacktrace_new(PyTypeObject *object,
f41043
+                            PyObject *args,
f41043
+                            PyObject *kwds)
f41043
+{
f41043
+    const char *str = NULL;
f41043
+    if (!PyArg_ParseTuple(args, "|s", &str))
f41043
+        return NULL;
f41043
+
f41043
+    struct sr_ruby_stacktrace *stacktrace;
f41043
+
f41043
+    if (str)
f41043
+    {
f41043
+        struct sr_location location;
f41043
+        sr_location_init(&location);
f41043
+        stacktrace = sr_ruby_stacktrace_parse(&str, &location);
f41043
+        if (!stacktrace)
f41043
+        {
f41043
+            PyErr_SetString(PyExc_ValueError, location.message);
f41043
+            return NULL;
f41043
+        }
f41043
+    }
f41043
+    else
f41043
+        stacktrace = sr_ruby_stacktrace_new();
f41043
+
f41043
+    return ruby_stacktrace_to_python_obj(stacktrace);
f41043
+}
f41043
+
f41043
+/* destructor */
f41043
+void
f41043
+sr_py_ruby_stacktrace_free(PyObject *object)
f41043
+{
f41043
+    struct sr_py_ruby_stacktrace *this = (struct sr_py_ruby_stacktrace*)object;
f41043
+    /* the list will decref all of its elements */
f41043
+    Py_DECREF(this->frames);
f41043
+    this->stacktrace->frames = NULL;
f41043
+    sr_ruby_stacktrace_free(this->stacktrace);
f41043
+    PyObject_Del(object);
f41043
+}
f41043
+
f41043
+/* str */
f41043
+PyObject *
f41043
+sr_py_ruby_stacktrace_str(PyObject *self)
f41043
+{
f41043
+    struct sr_py_ruby_stacktrace *this = (struct sr_py_ruby_stacktrace *)self;
f41043
+    struct sr_strbuf *buf = sr_strbuf_new();
f41043
+    sr_strbuf_append_strf(buf, "Ruby stacktrace with %zd frames",
f41043
+                         (ssize_t)(PyList_Size(this->frames)));
f41043
+    char *str = sr_strbuf_free_nobuf(buf);
f41043
+    PyObject *result = Py_BuildValue("s", str);
f41043
+    free(str);
f41043
+    return result;
f41043
+}
f41043
+
f41043
+/* methods */
f41043
+PyObject *
f41043
+sr_py_ruby_stacktrace_dup(PyObject *self, PyObject *args)
f41043
+{
f41043
+    struct sr_py_ruby_stacktrace *this = (struct sr_py_ruby_stacktrace*)self;
f41043
+    if (frames_prepare_linked_list((struct sr_py_base_thread *)this) < 0)
f41043
+        return NULL;
f41043
+
f41043
+    struct sr_ruby_stacktrace *stacktrace = sr_ruby_stacktrace_dup(this->stacktrace);
f41043
+    if (!stacktrace)
f41043
+        return NULL;
f41043
+
f41043
+    return ruby_stacktrace_to_python_obj(stacktrace);
f41043
+}
f41043
diff --git a/python/py_ruby_stacktrace.h b/python/py_ruby_stacktrace.h
f41043
new file mode 100644
f41043
index 0000000..f4eebce
f41043
--- /dev/null
f41043
+++ b/python/py_ruby_stacktrace.h
f41043
@@ -0,0 +1,71 @@
f41043
+/*
f41043
+    py_ruby_stacktrace.h
f41043
+
f41043
+    Copyright (C) 2015  Red Hat, Inc.
f41043
+
f41043
+    This program is free software; you can redistribute it and/or modify
f41043
+    it under the terms of the GNU General Public License as published by
f41043
+    the Free Software Foundation; either version 2 of the License, or
f41043
+    (at your option) any later version.
f41043
+
f41043
+    This program is distributed in the hope that it will be useful,
f41043
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
f41043
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f41043
+    GNU General Public License for more details.
f41043
+
f41043
+    You should have received a copy of the GNU General Public License along
f41043
+    with this program; if not, write to the Free Software Foundation, Inc.,
f41043
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
f41043
+*/
f41043
+#ifndef SATYR_PY_RUBY_STACKTRACE_H
f41043
+#define SATYR_PY_RUBY_STACKTRACE_H
f41043
+
f41043
+/**
f41043
+ * @file
f41043
+ * @brief Python bindings for Ruby stack trace.
f41043
+ */
f41043
+
f41043
+#ifdef __cplusplus
f41043
+extern "C" {
f41043
+#endif
f41043
+
f41043
+#include <Python.h>
f41043
+#include <structmember.h>
f41043
+
f41043
+PyTypeObject sr_py_ruby_stacktrace_type;
f41043
+
f41043
+/* The beginning of this structure has to have the same layout as
f41043
+ * sr_py_base_thread.
f41043
+ */
f41043
+struct sr_py_ruby_stacktrace
f41043
+{
f41043
+    PyObject_HEAD
f41043
+    struct sr_ruby_stacktrace *stacktrace;
f41043
+    PyObject *frames;
f41043
+    PyTypeObject *frame_type;
f41043
+};
f41043
+
f41043
+/* helpers */
f41043
+PyObject *ruby_stacktrace_to_python_obj(struct sr_ruby_stacktrace *stacktrace);
f41043
+
f41043
+/* constructor */
f41043
+PyObject *sr_py_ruby_stacktrace_new(PyTypeObject *object,
f41043
+                                    PyObject *args,
f41043
+                                    PyObject *kwds);
f41043
+
f41043
+/* destructor */
f41043
+void sr_py_ruby_stacktrace_free(PyObject *object);
f41043
+
f41043
+/* str */
f41043
+PyObject *sr_py_ruby_stacktrace_str(PyObject *self);
f41043
+
f41043
+/* methods */
f41043
+PyObject *sr_py_ruby_stacktrace_dup(PyObject *self, PyObject *args);
f41043
+PyObject *sr_py_ruby_stacktrace_normalize(PyObject *self, PyObject *args);
f41043
+
f41043
+
f41043
+#ifdef __cplusplus
f41043
+}
f41043
+#endif
f41043
+
f41043
+#endif
f41043
diff --git a/tests/Makefile.am b/tests/Makefile.am
f41043
index 0eb33a5..b261880 100644
f41043
--- a/tests/Makefile.am
f41043
+++ b/tests/Makefile.am
f41043
@@ -4,7 +4,16 @@ EXTRA_DIST = gdb_stacktraces \
f41043
              json_files \
f41043
              kerneloopses \
f41043
              programs \
f41043
-             python \
f41043
+             python/core.py \
f41043
+             python/gdb.py \
f41043
+             python/java.py \
f41043
+             python/koops.py \
f41043
+             python/metrics.py \
f41043
+             python/misc.py \
f41043
+             python/python.py \
f41043
+             python/report.py \
f41043
+             python/ruby.py \
f41043
+             python/test_helpers.py \
f41043
              python_stacktraces
f41043
 
f41043
 ## ------------ ##
f41043
diff --git a/tests/python/ruby.py b/tests/python/ruby.py
f41043
new file mode 100755
f41043
index 0000000..8a20593
f41043
--- /dev/null
f41043
+++ b/tests/python/ruby.py
f41043
@@ -0,0 +1,174 @@
f41043
+#!/usr/bin/env python
f41043
+
f41043
+import unittest
f41043
+from test_helpers import *
f41043
+
f41043
+path = '../ruby_stacktraces/ruby-01'
f41043
+contents = load_input_contents(path)
f41043
+frames_expected = 21
f41043
+expected_short_text = '''#1 rescue in block (2 levels) in func in /usr/share/ruby/vendor_ruby/will_crash.rb:13
f41043
+#2 block (2 levels) in func in /usr/share/ruby/vendor_ruby/will_crash.rb:10
f41043
+#3 times in /usr/share/ruby/vendor_ruby/will_crash.rb:9
f41043
+#4 block in func in /usr/share/ruby/vendor_ruby/will_crash.rb:9
f41043
+#5 times in /usr/share/ruby/vendor_ruby/will_crash.rb:8
f41043
+#6 func in /usr/share/ruby/vendor_ruby/will_crash.rb:8
f41043
+'''
f41043
+
f41043
+class TestRubyStacktrace(BindingsTestCase):
f41043
+    def setUp(self):
f41043
+        self.trace = satyr.RubyStacktrace(contents)
f41043
+
f41043
+    def test_correct_frame_count(self):
f41043
+        self.assertEqual(frame_count(self.trace), frames_expected)
f41043
+
f41043
+    def test_dup(self):
f41043
+        dup = self.trace.dup()
f41043
+        self.assertNotEqual(id(dup.frames), id(self.trace.frames))
f41043
+        self.assertTrue(all(map(lambda t1, t2: t1.equals(t2), dup.frames, self.trace.frames)))
f41043
+
f41043
+        dup.frames = dup.frames[:5]
f41043
+        dup2 = dup.dup()
f41043
+        self.assertEqual(len(dup.frames), len(dup2.frames))
f41043
+        self.assertNotEqual(id(dup.frames), id(dup2.frames))
f41043
+
f41043
+    def test_prepare_linked_list(self):
f41043
+        dup = self.trace.dup()
f41043
+        dup.frames = dup.frames[:5]
f41043
+        dup2 = dup.dup()
f41043
+        self.assertTrue(len(dup2.frames) <= 5)
f41043
+
f41043
+    def test_str(self):
f41043
+        out = str(self.trace)
f41043
+        self.assertTrue(('Ruby stacktrace with %d frames' % frames_expected) in out)
f41043
+
f41043
+    def test_getset(self):
f41043
+        self.assertGetSetCorrect(self.trace, 'exception_name', 'Wrap::MyException', 'WhateverException')
f41043
+
f41043
+    def test_special_functions(self):
f41043
+        trace = load_input_contents('../ruby_stacktraces/ruby-01')
f41043
+        trace = satyr.RubyStacktrace(trace)
f41043
+
f41043
+        f = trace.frames[0]
f41043
+        self.assertEqual(f.file_name, '/usr/share/ruby/vendor_ruby/will_crash.rb')
f41043
+        self.assertEqual(f.function_name, 'func')
f41043
+        self.assertFalse(f.special_function)
f41043
+        self.assertEqual(f.block_level, 2)
f41043
+        self.assertEqual(f.rescue_level, 1)
f41043
+
f41043
+        f = trace.frames[-1]
f41043
+        self.assertEqual(f.file_name, '/usr/bin/will_ruby_raise')
f41043
+        self.assertEqual(f.function_name, 'main')
f41043
+        self.assertTrue(f.special_function)
f41043
+        self.assertEqual(f.block_level, 0)
f41043
+        self.assertEqual(f.rescue_level, 0)
f41043
+
f41043
+    def test_to_short_text(self):
f41043
+        self.assertEqual(self.trace.to_short_text(6), expected_short_text)
f41043
+
f41043
+    def test_bthash(self):
f41043
+        self.assertEqual(self.trace.get_bthash(), '6124e03542a0381b14ebaf2c5b5e9f467ebba33b')
f41043
+
f41043
+    def test_duphash(self):
f41043
+        expected_plain = '''Thread
f41043
+/usr/share/ruby/vendor_ruby/will_crash.rb:13
f41043
+/usr/share/ruby/vendor_ruby/will_crash.rb:10
f41043
+/usr/share/ruby/vendor_ruby/will_crash.rb:9
f41043
+'''
f41043
+        self.assertEqual(self.trace.get_duphash(flags=satyr.DUPHASH_NOHASH, frames=3), expected_plain)
f41043
+        self.assertEqual(self.trace.get_duphash(), 'c877cda04fbce8d51c4d9b1d628b0f618677607e')
f41043
+
f41043
+    def test_crash_thread(self):
f41043
+        self.assertTrue(self.trace.crash_thread is self.trace)
f41043
+
f41043
+    def test_from_json(self):
f41043
+        trace = satyr.RubyStacktrace.from_json('{}')
f41043
+        self.assertEqual(trace.frames, [])
f41043
+
f41043
+        json_text = '''
f41043
+        {
f41043
+            "exception_name": "NotImplementedError",
f41043
+            "stacktrace": [
f41043
+                {
f41043
+                    "file_name": "/usr/share/foobar/mod/file.rb",
f41043
+                    "function_name": "do_nothing",
f41043
+                    "file_line": 42,
f41043
+                },
f41043
+                {
f41043
+                    "special_function": "top (required)",
f41043
+                    "file_line": 451
f41043
+                },
f41043
+                {
f41043
+                    "unknown_key": 19876543,
f41043
+                    "block_level": 42,
f41043
+                    "rescue_level": 6
f41043
+                }
f41043
+            ]
f41043
+        }
f41043
+'''
f41043
+        trace = satyr.RubyStacktrace.from_json(json_text)
f41043
+        self.assertEqual(trace.exception_name, 'NotImplementedError')
f41043
+        self.assertEqual(len(trace.frames), 3)
f41043
+
f41043
+        self.assertEqual(trace.frames[0].file_name, '/usr/share/foobar/mod/file.rb')
f41043
+        self.assertEqual(trace.frames[0].function_name, 'do_nothing')
f41043
+        self.assertEqual(trace.frames[0].file_line, 42)
f41043
+        self.assertFalse(trace.frames[0].special_function)
f41043
+        self.assertEqual(trace.frames[0].block_level, 0)
f41043
+        self.assertEqual(trace.frames[0].rescue_level, 0)
f41043
+
f41043
+        self.assertEqual(trace.frames[1].file_name, None)
f41043
+        self.assertEqual(trace.frames[1].function_name, 'top (required)')
f41043
+        self.assertEqual(trace.frames[1].file_line, 451)
f41043
+        self.assertTrue(trace.frames[1].special_function)
f41043
+        self.assertEqual(trace.frames[1].block_level, 0)
f41043
+        self.assertEqual(trace.frames[1].rescue_level, 0)
f41043
+
f41043
+        self.assertEqual(trace.frames[2].file_name, None)
f41043
+        self.assertEqual(trace.frames[2].function_name, None)
f41043
+        self.assertEqual(trace.frames[2].file_line, 0)
f41043
+        self.assertFalse(trace.frames[2].special_function)
f41043
+        self.assertEqual(trace.frames[2].block_level, 42)
f41043
+        self.assertEqual(trace.frames[2].rescue_level, 6)
f41043
+
f41043
+    def test_hash(self):
f41043
+        self.assertHashable(self.trace)
f41043
+
f41043
+
f41043
+class TestRubyFrame(BindingsTestCase):
f41043
+    def setUp(self):
f41043
+        self.frame = satyr.RubyStacktrace(contents).frames[1]
f41043
+
f41043
+    def test_str(self):
f41043
+        out = str(self.frame)
f41043
+        self.assertEqual(out, "/usr/share/ruby/vendor_ruby/will_crash.rb:10:in `block (2 levels) in func'")
f41043
+
f41043
+    def test_dup(self):
f41043
+        dup = self.frame.dup()
f41043
+        self.assertEqual(dup.function_name,
f41043
+            self.frame.function_name)
f41043
+
f41043
+        dup.function_name = 'other'
f41043
+        self.assertNotEqual(dup.function_name,
f41043
+            self.frame.function_name)
f41043
+
f41043
+    def test_cmp(self):
f41043
+        dup = self.frame.dup()
f41043
+        self.assertTrue(dup.equals(dup))
f41043
+        self.assertTrue(dup.equals(self.frame))
f41043
+        self.assertNotEqual(id(dup), id(self.frame))
f41043
+        dup.function_name = 'another'
f41043
+        self.assertFalse(dup.equals(self.frame))
f41043
+
f41043
+    def test_getset(self):
f41043
+        self.assertGetSetCorrect(self.frame, 'file_name', '/usr/share/ruby/vendor_ruby/will_crash.rb', 'java.rs')
f41043
+        self.assertGetSetCorrect(self.frame, 'file_line', 10, 6667)
f41043
+        self.assertGetSetCorrect(self.frame, 'special_function', False, True)
f41043
+        self.assertGetSetCorrect(self.frame, 'function_name', 'func', 'iiiiii')
f41043
+        self.assertGetSetCorrect(self.frame, 'block_level', 2, 666)
f41043
+        self.assertGetSetCorrect(self.frame, 'rescue_level', 0, 9000)
f41043
+
f41043
+    def test_hash(self):
f41043
+        self.assertHashable(self.frame)
f41043
+
f41043
+if __name__ == '__main__':
f41043
+    unittest.main()
f41043
diff --git a/tests/python_bindings.at b/tests/python_bindings.at
f41043
index a5e19c3..906f561 100644
f41043
--- a/tests/python_bindings.at
f41043
+++ b/tests/python_bindings.at
f41043
@@ -12,5 +12,6 @@ AT_TEST_PYTHON([koops])
f41043
 AT_TEST_PYTHON([python])
f41043
 AT_TEST_PYTHON([java])
f41043
 AT_TEST_PYTHON([core])
f41043
+AT_TEST_PYTHON([ruby])
f41043
 AT_TEST_PYTHON([metrics])
f41043
 AT_TEST_PYTHON([report])
f41043
-- 
f41043
1.8.3.1
f41043