From: Doug Evans <dje@google.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] Remove duplicate code to init python objfile/progspace objects
Date: Thu, 09 Oct 2014 15:54:00 -0000 [thread overview]
Message-ID: <yjt2a955qlvw.fsf@ruffy.mtv.corp.google.com> (raw)
Hi.
This patch just removes some duplicate code.
2014-10-09 Doug Evans <dje@google.com>
* py-objfile.c (objfpy_initialize): New function.
(objfpy_new, objfile_to_objfile_object): Call it.
* py-progspace.c (pspy_initialize): New function.
(pspy_new, pspace_to_pspace_object): Call it.
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index 5d00d62..df29691 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -74,6 +74,33 @@ objfpy_dealloc (PyObject *o)
Py_TYPE (self)->tp_free (self);
}
+/* Initialize an objfile_object.
+ The result is a boolean indicating success. */
+
+static int
+objfpy_initialize (objfile_object *self)
+{
+ self->objfile = NULL;
+
+ self->printers = PyList_New (0);
+ if (self->printers == NULL)
+ return 0;
+
+ self->frame_filters = PyDict_New ();
+ if (self->frame_filters == NULL)
+ return 0;
+
+ self->type_printers = PyList_New (0);
+ if (self->type_printers == NULL)
+ return 0;
+
+ self->xmethods = PyList_New (0);
+ if (self->xmethods == NULL)
+ return 0;
+
+ return 1;
+}
+
static PyObject *
objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
{
@@ -81,36 +108,13 @@ objfpy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
if (self)
{
- self->objfile = NULL;
-
- self->printers = PyList_New (0);
- if (!self->printers)
- {
- Py_DECREF (self);
- return NULL;
- }
-
- self->frame_filters = PyDict_New ();
- if (!self->frame_filters)
- {
- Py_DECREF (self);
- return NULL;
- }
-
- self->type_printers = PyList_New (0);
- if (!self->type_printers)
- {
- Py_DECREF (self);
- return NULL;
- }
-
- self->xmethods = PyList_New (0);
- if (self->xmethods == NULL)
+ if (!objfpy_initialize (self))
{
Py_DECREF (self);
return NULL;
}
}
+
return (PyObject *) self;
}
@@ -280,6 +284,7 @@ py_free_objfile (struct objfile *objfile, void *datum)
representing OBJFILE. If the object has already been created,
return it. Otherwise, create it. Return NULL and set the Python
error on failure. */
+
PyObject *
objfile_to_objfile_object (struct objfile *objfile)
{
@@ -291,36 +296,13 @@ objfile_to_objfile_object (struct objfile *objfile)
object = PyObject_New (objfile_object, &objfile_object_type);
if (object)
{
- object->objfile = objfile;
-
- object->printers = PyList_New (0);
- if (!object->printers)
- {
- Py_DECREF (object);
- return NULL;
- }
-
- object->frame_filters = PyDict_New ();
- if (!object->frame_filters)
- {
- Py_DECREF (object);
- return NULL;
- }
-
- object->type_printers = PyList_New (0);
- if (!object->type_printers)
- {
- Py_DECREF (object);
- return NULL;
- }
-
- object->xmethods = PyList_New (0);
- if (object->xmethods == NULL)
+ if (!objfpy_initialize (object))
{
Py_DECREF (object);
return NULL;
}
+ object->objfile = objfile;
set_objfile_data (objfile, objfpy_objfile_data_key, object);
}
}
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index b0092c5..4280032 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -82,6 +82,33 @@ pspy_dealloc (PyObject *self)
Py_TYPE (self)->tp_free (self);
}
+/* Initialize a pspace_object.
+ The result is a boolean indicating success. */
+
+static int
+pspy_initialize (pspace_object *self)
+{
+ self->pspace = NULL;
+
+ self->printers = PyList_New (0);
+ if (self->printers == NULL)
+ return 0;
+
+ self->frame_filters = PyDict_New ();
+ if (self->frame_filters == NULL)
+ return 0;
+
+ self->type_printers = PyList_New (0);
+ if (self->type_printers == NULL)
+ return 0;
+
+ self->xmethods = PyList_New (0);
+ if (self->xmethods == NULL)
+ return 0;
+
+ return 1;
+}
+
static PyObject *
pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
{
@@ -89,36 +116,13 @@ pspy_new (PyTypeObject *type, PyObject *args, PyObject *keywords)
if (self)
{
- self->pspace = NULL;
-
- self->printers = PyList_New (0);
- if (!self->printers)
- {
- Py_DECREF (self);
- return NULL;
- }
-
- self->frame_filters = PyDict_New ();
- if (!self->frame_filters)
- {
- Py_DECREF (self);
- return NULL;
- }
-
- self->type_printers = PyList_New (0);
- if (!self->type_printers)
- {
- Py_DECREF (self);
- return NULL;
- }
-
- self->xmethods = PyList_New (0);
- if (self->xmethods == NULL)
+ if (!pspy_initialize (self))
{
Py_DECREF (self);
return NULL;
}
}
+
return (PyObject *) self;
}
@@ -296,36 +300,13 @@ pspace_to_pspace_object (struct program_space *pspace)
object = PyObject_New (pspace_object, &pspace_object_type);
if (object)
{
- object->pspace = pspace;
-
- object->printers = PyList_New (0);
- if (!object->printers)
- {
- Py_DECREF (object);
- return NULL;
- }
-
- object->frame_filters = PyDict_New ();
- if (!object->frame_filters)
- {
- Py_DECREF (object);
- return NULL;
- }
-
- object->type_printers = PyList_New (0);
- if (!object->type_printers)
- {
- Py_DECREF (object);
- return NULL;
- }
-
- object->xmethods = PyList_New (0);
- if (object->xmethods == NULL)
+ if (!pspy_initialize (object))
{
Py_DECREF (object);
return NULL;
}
+ object->pspace = pspace;
set_program_space_data (pspace, pspy_pspace_data_key, object);
}
}
next reply other threads:[~2014-10-09 15:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-09 15:54 Doug Evans [this message]
2014-10-13 20:01 ` Doug Evans
2014-10-13 20:21 ` Phil Muldoon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=yjt2a955qlvw.fsf@ruffy.mtv.corp.google.com \
--to=dje@google.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox