From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11275 invoked by alias); 4 Dec 2009 15:25:25 -0000 Received: (qmail 11262 invoked by uid 22791); 4 Dec 2009 15:25:23 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 04 Dec 2009 15:25:18 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id nB4FPHPQ012980 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 4 Dec 2009 10:25:17 -0500 Received: from localhost.localdomain (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id nB4FPG1G018788 for ; Fri, 4 Dec 2009 10:25:17 -0500 Message-ID: <4B1929DC.7070809@redhat.com> Date: Fri, 04 Dec 2009 15:25:00 -0000 From: Phil Muldoon User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4pre) Gecko/20090922 Fedora/3.0-3.9.b4.fc12 Lightning/1.0pre Thunderbird/3.0b4 MIME-Version: 1.0 To: gdb-patches ml Subject: [python][patch] And range method to type Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes 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: 2009-12/txt/msg00052.txt.bz2 This patch adds a "range" method for GDB.Types. It only supports types that support a range, or the range type itself. Ok? Cheers, Phil -- ChangeLog 2009-12-04 Phil Muldoon * python/py-type.c (typy_range): New method. Testsuite ChangeLog 2009-12-04 Phil Muldoon * gdb.python/py-type.exp (test_range): New method. Test type range method. Doc ChangeLog 2009-12-04 Phil Muldoon * gdb.texinfo (Types In Python): Document range method. -- Index: doc/gdb.texinfo =================================================================== RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v retrieving revision 1.647 diff -u -r1.647 gdb.texinfo --- doc/gdb.texinfo 3 Dec 2009 21:19:48 -0000 1.647 +++ doc/gdb.texinfo 4 Dec 2009 15:15:21 -0000 @@ -19654,6 +19654,13 @@ @code{volatile}. @end defmethod +@defmethod Type range +Return a Python @code{Tuple} object that contains two elements. The +first element of the @code{Tuple} contains the low bound of the type; the +second element contains the high bound of the type. If the type does +not have a range, @value{GDBN} will throw a @code{RuntimeError} +@end defmethod + @defmethod Type reference Return a new @code{gdb.Type} object which represents a reference to this type. Index: python/py-type.c =================================================================== RCS file: /cvs/src/src/gdb/python/py-type.c,v retrieving revision 1.3 diff -u -r1.3 py-type.c --- python/py-type.c 3 Dec 2009 21:19:49 -0000 1.3 +++ python/py-type.c 4 Dec 2009 15:15:22 -0000 @@ -273,6 +273,44 @@ return type_to_type_object (type); } +/* Return the range of a type represented by SELF. The return type is + a tuple. The first element of the tuple contains the low bound, + while the second element of the tuple contains the high bound. */ +static PyObject * +typy_range (PyObject *self, PyObject *args) +{ + struct type *type = ((type_object *) self)->type; + PyObject *result, *low, *high; + + if (TYPE_CODE (type) != TYPE_CODE_ARRAY && + TYPE_CODE (type) != TYPE_CODE_STRING && + TYPE_CODE (type) != TYPE_CODE_RANGE) + { + PyErr_SetString (PyExc_RuntimeError, + "This type does not have a range."); + return NULL; + } + + switch (TYPE_CODE (type)) + { + case TYPE_CODE_ARRAY: + case TYPE_CODE_STRING: + low = PyLong_FromLong (TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type))); + high = PyLong_FromLong (TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type))); + break; + case TYPE_CODE_RANGE: + low = PyLong_FromLong (TYPE_LOW_BOUND (type)); + high = PyLong_FromLong (TYPE_HIGH_BOUND (type)); + break; + } + + result = PyTuple_New (2); + PyTuple_SetItem (result, 0, low); + PyTuple_SetItem (result, 1, high); + + return result; +} + /* Return a Type object which represents a reference to SELF. */ static PyObject * typy_reference (PyObject *self, PyObject *args) @@ -699,6 +737,9 @@ { "pointer", typy_pointer, METH_NOARGS, "pointer () -> Type\n\ Return a type of pointer to this type." }, + { "range", typy_range, METH_NOARGS, + "range () -> Tuple\n\ +Return a Tuple containing the lower and upper range for this type."}, { "reference", typy_reference, METH_NOARGS, "reference () -> Type\n\ Return a type of reference to this type." }, Index: testsuite/gdb.python/py-type.exp =================================================================== RCS file: /cvs/src/src/gdb/testsuite/gdb.python/py-type.exp,v retrieving revision 1.1 diff -u -r1.1 py-type.exp --- testsuite/gdb.python/py-type.exp 3 Dec 2009 21:19:49 -0000 1.1 +++ testsuite/gdb.python/py-type.exp 4 Dec 2009 15:15:24 -0000 @@ -102,6 +102,28 @@ gdb_test "python print fields\[1\].is_base_class" "False" "Check base class" } +proc test_range {} { + + # Test a valid range request. + gdb_py_test_silent_cmd "print ar" "print value" 1 + gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1 + gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length" + gdb_test "python print ar.type.range()\[0\]" "0" "Check low range" + gdb_test "python print ar.type.range()\[1\]" "1" "Check high range" + + # Test a range request on a ranged type. + gdb_py_test_silent_cmd "print ar" "print value" 1 + gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1 + gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1 + gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound" + gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound" + + # Test where a range does not exist. + gdb_py_test_silent_cmd "print st" "print value" 1 + gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1 + gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type." +} + # Perform C Tests. build_inferior "c" restart_gdb "break to inspect struct and array." @@ -112,3 +134,4 @@ restart_gdb "break to inspect struct and array." test_fields "c++" test_base_class +test_range