From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27117 invoked by alias); 29 Sep 2011 14:50:29 -0000 Received: (qmail 27107 invoked by uid 22791); 29 Sep 2011 14:50:28 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from qmta09.emeryville.ca.mail.comcast.net (HELO qmta09.emeryville.ca.mail.comcast.net) (76.96.30.96) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 29 Sep 2011 14:50:12 +0000 Received: from omta20.emeryville.ca.mail.comcast.net ([76.96.30.87]) by qmta09.emeryville.ca.mail.comcast.net with comcast id edSb1h0041smiN4A9eq5sh; Thu, 29 Sep 2011 14:50:05 +0000 Received: from [192.168.10.125] ([75.68.58.5]) by omta20.emeryville.ca.mail.comcast.net with comcast id eenV1h00u06m2QB8genWvo; Thu, 29 Sep 2011 14:47:30 +0000 From: Paul Koning Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: [RFA] Python: eliminate some code duplication in py-type.c Date: Thu, 29 Sep 2011 15:10:00 -0000 Message-Id: <4AAB6A5B-1256-4CF6-8568-FF067ED2D2F7@comcast.net> To: gdb-patches@sourceware.org Mime-Version: 1.0 (Apple Message framework v1084) Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-09/txt/msg00547.txt.bz2 My previous change to add mapping support to gdb.Type resulted in two copie= s of the code that walks through the fields of a structure: one for the met= hods that get a list of fields (like gdb.Type.fields()) and one for the ite= rator. 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 buil= d 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 * python/py-type.c (typy_make_iter): Add forward declaration. (typy_fields_items): Use the gdb.Type iterator. Index: python/py-type.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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; }; =20 +/* Forward declarations. */ +static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind= ); + #define ENTRY(X) { X, #X } =20 static struct pyty_code pyty_codes[] =3D @@ -290,40 +293,15 @@ static PyObject * typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind) { - PyObject *result =3D NULL, *item =3D NULL; - int i; - struct type *type =3D ((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 =3D PyList_New (0); - if (result =3D=3D NULL) - return NULL; + PyObject *result =3D NULL, *iter =3D NULL; =20=20=20 - for (i =3D 0; i < TYPE_NFIELDS (type); ++i) - { - item =3D make_fielditem (type, i, kind); - if (!item) - goto fail; - if (PyList_Append (result, item)) - goto fail; - Py_DECREF (item); - } - + iter =3D typy_make_iter (self, kind); + if (iter =3D=3D NULL) + return NULL; +=20=20=20=20 + result =3D PySequence_List (iter); + Py_DECREF (iter); return result; - - fail: - Py_XDECREF (item); - Py_XDECREF (result); - return NULL; } =20 /* Return a sequence of all fields. Each field is a gdb.Field object. */