This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFA] Python: eliminate some code duplication in py-type.c


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.  */



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]