From: Paul Koning <paulkoning@comcast.net>
To: gdb-patches@sourceware.org
Subject: [RFA] Python: eliminate some code duplication in py-type.c
Date: Thu, 29 Sep 2011 15:10:00 -0000 [thread overview]
Message-ID: <4AAB6A5B-1256-4CF6-8568-FF067ED2D2F7@comcast.net> (raw)
My previous change to add mapping support to gdb.Type resulted in two copies of the code that walks through the fields of a structure: one for the methods that get a list of fields (like gdb.Type.fields()) and one for the iterator. That makes extra work for any future changes to that code, like the one proposed by Yi Lu.
The attached patch eliminates the duplication: it uses the iterator to build the list, instead of having a second copy of the code.
Built for i386-linux, no testsuite regressions... ok to commit?
paul
ChangeLog:
2011-09-29 Paul Koning <paul_koning@dell.com>
* python/py-type.c (typy_make_iter): Add forward declaration.
(typy_fields_items): Use the gdb.Type iterator.
Index: python/py-type.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-type.c,v
retrieving revision 1.22
diff -u -r1.22 py-type.c
--- python/py-type.c 28 Sep 2011 20:04:52 -0000 1.22
+++ python/py-type.c 29 Sep 2011 14:42:02 -0000
@@ -77,6 +77,9 @@
const char *name;
};
+/* Forward declarations. */
+static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind);
+
#define ENTRY(X) { X, #X }
static struct pyty_code pyty_codes[] =
@@ -290,40 +293,15 @@
static PyObject *
typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
{
- PyObject *result = NULL, *item = NULL;
- int i;
- struct type *type = ((type_object *) self)->type;
- volatile struct gdb_exception except;
-
- TRY_CATCH (except, RETURN_MASK_ALL)
- {
- CHECK_TYPEDEF (type);
- }
- GDB_PY_HANDLE_EXCEPTION (except);
-
- /* We would like to make a tuple here, make fields immutable, and
- then memoize the result (and perhaps make Field.type() lazy).
- However, that can lead to cycles. */
- result = PyList_New (0);
- if (result == NULL)
- return NULL;
+ PyObject *result = NULL, *iter = NULL;
- for (i = 0; i < TYPE_NFIELDS (type); ++i)
- {
- item = make_fielditem (type, i, kind);
- if (!item)
- goto fail;
- if (PyList_Append (result, item))
- goto fail;
- Py_DECREF (item);
- }
-
+ iter = typy_make_iter (self, kind);
+ if (iter == NULL)
+ return NULL;
+
+ result = PySequence_List (iter);
+ Py_DECREF (iter);
return result;
-
- fail:
- Py_XDECREF (item);
- Py_XDECREF (result);
- return NULL;
}
/* Return a sequence of all fields. Each field is a gdb.Field object. */
next reply other threads:[~2011-09-29 14:50 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-09-29 15:10 Paul Koning [this message]
2011-10-04 16:15 ` Tom Tromey
2011-10-04 16:21 ` Paul Koning
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=4AAB6A5B-1256-4CF6-8568-FF067ED2D2F7@comcast.net \
--to=paulkoning@comcast.net \
--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