public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* RFA: fix PR python/11915
@ 2010-08-20 22:32 Tom Tromey
  2010-08-21  7:54 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2010-08-20 22:32 UTC (permalink / raw)
  To: gdb-patches

This patch fixes PR python/11915.

It adds a new method, Type.array, so that users can create new array
types at runtime.

This needs a doc review.

Built & regtested on x86-64 (compile farm).

Tom

2010-08-20  Tom Tromey  <tromey@redhat.com>

	PR python/11915:
	* python/py-type.c (typy_array): New function.
	(type_object_methods): Add "array".

2010-08-20  Tom Tromey  <tromey@redhat.com>

	PR python/11915:
	* gdb.texinfo (Types In Python): Document array method.

2010-08-20  Tom Tromey  <tromey@redhat.com>

	PR python/11915:
	* gdb.python/py-type.exp (test_fields): Add tests for array.

 gdb/ChangeLog                        |    6 ++++
 gdb/doc/ChangeLog                    |    5 +++
 gdb/doc/gdb.texinfo                  |    9 ++++++
 gdb/python/py-type.c                 |   50 ++++++++++++++++++++++++++++++++++
 gdb/testsuite/ChangeLog              |    5 +++
 gdb/testsuite/gdb.python/py-type.exp |    5 +++
 6 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index ba1607c..628e147 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -20916,6 +20916,15 @@ but it can be @code{None} in some situations.
 @end table
 @end defmethod
 
+@defmethod Type array @var{n1} @r{[}@var{n2}@r{]}
+Return a new @code{gdb.Type} object which represents an array of this
+type.  If one argument is given, it is the inclusive upper bound of
+the array; in this case the lower bound is zero.  If two arguments are
+given, the first argument is the lower bound of the array, and the
+second argument is the upper bound of the array.  An array's length
+must not be negative.
+@end defmethod
+
 @defmethod Type const
 Return a new @code{gdb.Type} object which represents a
 @code{const}-qualified variant of this type.
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 529f301..3098248 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -262,6 +262,53 @@ typy_strip_typedefs (PyObject *self, PyObject *args)
   return type_to_type_object (check_typedef (type));
 }
 
+/* Return an array type.  */
+static PyObject *
+typy_array (PyObject *self, PyObject *args)
+{
+  int n1, n2;
+  PyObject *n2_obj = NULL;
+  struct type *array = NULL;
+  struct type *type = ((type_object *) self)->type;
+  volatile struct gdb_exception except;
+
+  if (! PyArg_ParseTuple (args, "i|O", &n1, &n2_obj))
+    return NULL;
+
+  if (n2_obj)
+    {
+      if (!PyInt_Check (n2_obj))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("Array bound must be an integer"));
+	  return NULL;
+	}
+      n2 = (int) PyInt_AsLong (n2_obj);
+      if (PyErr_Occurred ())
+	return NULL;
+    }
+  else
+    {
+      n2 = n1;
+      n1 = 0;
+    }
+
+  if (n2 < n1)
+    {
+      PyErr_SetString (PyExc_ValueError,
+		       _("Array length must not be negative"));
+      return NULL;
+    }
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      array = lookup_array_range_type (type, n1, n2);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  return type_to_type_object (array);
+}
+
 /* Return a Type object which represents a pointer to SELF.  */
 static PyObject *
 typy_pointer (PyObject *self, PyObject *args)
@@ -836,6 +883,9 @@ static PyGetSetDef type_object_getset[] =
 
 static PyMethodDef type_object_methods[] =
 {
+  { "array", typy_array, METH_VARARGS,
+    "array (N) -> Type\n\
+Return a type which represents an array of N objects of this type." },
   { "const", typy_const, METH_NOARGS,
     "const () -> Type\n\
 Return a const variant of this type." },
diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp
index 6e5bd0e..095711f 100644
--- a/gdb/testsuite/gdb.python/py-type.exp
+++ b/gdb/testsuite/gdb.python/py-type.exp
@@ -97,6 +97,11 @@ proc test_fields {lang} {
   gdb_test "python fields = ar.type.fields()"
   gdb_test "python print len(fields)" "1" "Check the number of fields"
   gdb_test "python print fields\[0\].type" "<range type>" "Check array field type"
+
+  gdb_test "python print ar\[0\].cast(ar\[0\].type.array(1))" \
+      ".1, 2." "cast to array with one argument"
+  gdb_test "python print ar\[0\].cast(ar\[0\].type.array(0, 1))" \
+      ".1, 2." "cast to array with two arguments"
 }
 
 proc test_base_class {} {

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: RFA: fix PR python/11915
  2010-08-20 22:32 RFA: fix PR python/11915 Tom Tromey
@ 2010-08-21  7:54 ` Eli Zaretskii
  2010-08-23 17:03   ` Tom Tromey
  0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2010-08-21  7:54 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> From: Tom Tromey <tromey@redhat.com>
> Date: Fri, 20 Aug 2010 16:32:22 -0600
> 
> This patch fixes PR python/11915.
> 
> It adds a new method, Type.array, so that users can create new array
> types at runtime.

Thanks.

> +@defmethod Type array @var{n1} @r{[}@var{n2}@r{]}
> +Return a new @code{gdb.Type} object which represents an array of this
> +type.  If one argument is given, it is the inclusive upper bound of
> +the array; in this case the lower bound is zero.  If two arguments are

"Inclusive upper bound"?  Does this mean that if the argument is N,
then the array will have N+1 members, from zero to N?  That sounds
against the intuition, doesn't it?

> +given, the first argument is the lower bound of the array, and the
> +second argument is the upper bound of the array.

Will the reader know whether negative arguments are allowed (provided
that the second is greater than the first)?  Or is it a good idea to
tell explicitly?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: RFA: fix PR python/11915
  2010-08-21  7:54 ` Eli Zaretskii
@ 2010-08-23 17:03   ` Tom Tromey
  2010-08-23 17:16     ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2010-08-23 17:03 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

>>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:

Eli> "Inclusive upper bound"?  Does this mean that if the argument is N,
Eli> then the array will have N+1 members, from zero to N?  That sounds
Eli> against the intuition, doesn't it?

Yes, but it is the only way to allow an array whose upper bound is
MAXINT.  I think it is also consistent with Type.range.

>> +given, the first argument is the lower bound of the array, and the
>> +second argument is the upper bound of the array.

Eli> Will the reader know whether negative arguments are allowed (provided
Eli> that the second is greater than the first)?  Or is it a good idea to
Eli> tell explicitly?

I added a note.  New patch appended.

Tom

2010-08-20  Tom Tromey  <tromey@redhat.com>

	PR python/11915:
	* python/py-type.c (typy_array): New function.
	(type_object_methods): Add "array".

2010-08-20  Tom Tromey  <tromey@redhat.com>

	PR python/11915:
	* gdb.texinfo (Types In Python): Document array method.

2010-08-20  Tom Tromey  <tromey@redhat.com>

	PR python/11915:
	* gdb.python/py-type.exp (test_fields): Add tests for array.

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index ba1607c..7750361 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -20916,6 +20916,15 @@ but it can be @code{None} in some situations.
 @end table
 @end defmethod
 
+@defmethod Type array @var{n1} @r{[}@var{n2}@r{]}
+Return a new @code{gdb.Type} object which represents an array of this
+type.  If one argument is given, it is the inclusive upper bound of
+the array; in this case the lower bound is zero.  If two arguments are
+given, the first argument is the lower bound of the array, and the
+second argument is the upper bound of the array.  An array's length
+must not be negative, but the bounds can be.
+@end defmethod
+
 @defmethod Type const
 Return a new @code{gdb.Type} object which represents a
 @code{const}-qualified variant of this type.
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 529f301..ea6c8e5 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -262,6 +262,54 @@ typy_strip_typedefs (PyObject *self, PyObject *args)
   return type_to_type_object (check_typedef (type));
 }
 
+/* Return an array type.  */
+
+static PyObject *
+typy_array (PyObject *self, PyObject *args)
+{
+  int n1, n2;
+  PyObject *n2_obj = NULL;
+  struct type *array = NULL;
+  struct type *type = ((type_object *) self)->type;
+  volatile struct gdb_exception except;
+
+  if (! PyArg_ParseTuple (args, "i|O", &n1, &n2_obj))
+    return NULL;
+
+  if (n2_obj)
+    {
+      if (!PyInt_Check (n2_obj))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("Array bound must be an integer"));
+	  return NULL;
+	}
+      n2 = (int) PyInt_AsLong (n2_obj);
+      if (PyErr_Occurred ())
+	return NULL;
+    }
+  else
+    {
+      n2 = n1;
+      n1 = 0;
+    }
+
+  if (n2 < n1)
+    {
+      PyErr_SetString (PyExc_ValueError,
+		       _("Array length must not be negative"));
+      return NULL;
+    }
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      array = lookup_array_range_type (type, n1, n2);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  return type_to_type_object (array);
+}
+
 /* Return a Type object which represents a pointer to SELF.  */
 static PyObject *
 typy_pointer (PyObject *self, PyObject *args)
@@ -836,6 +884,9 @@ static PyGetSetDef type_object_getset[] =
 
 static PyMethodDef type_object_methods[] =
 {
+  { "array", typy_array, METH_VARARGS,
+    "array (N) -> Type\n\
+Return a type which represents an array of N objects of this type." },
   { "const", typy_const, METH_NOARGS,
     "const () -> Type\n\
 Return a const variant of this type." },
diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp
index 6e5bd0e..095711f 100644
--- a/gdb/testsuite/gdb.python/py-type.exp
+++ b/gdb/testsuite/gdb.python/py-type.exp
@@ -97,6 +97,11 @@ proc test_fields {lang} {
   gdb_test "python fields = ar.type.fields()"
   gdb_test "python print len(fields)" "1" "Check the number of fields"
   gdb_test "python print fields\[0\].type" "<range type>" "Check array field type"
+
+  gdb_test "python print ar\[0\].cast(ar\[0\].type.array(1))" \
+      ".1, 2." "cast to array with one argument"
+  gdb_test "python print ar\[0\].cast(ar\[0\].type.array(0, 1))" \
+      ".1, 2." "cast to array with two arguments"
 }
 
 proc test_base_class {} {

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: RFA: fix PR python/11915
  2010-08-23 17:03   ` Tom Tromey
@ 2010-08-23 17:16     ` Eli Zaretskii
  0 siblings, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2010-08-23 17:16 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> From: Tom Tromey <tromey@redhat.com>
> Cc: gdb-patches@sourceware.org
> Date: Mon, 23 Aug 2010 11:03:30 -0600
> 
> >>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:
> 
> Eli> "Inclusive upper bound"?  Does this mean that if the argument is N,
> Eli> then the array will have N+1 members, from zero to N?  That sounds
> Eli> against the intuition, doesn't it?
> 
> Yes, but it is the only way to allow an array whose upper bound is
> MAXINT.  I think it is also consistent with Type.range.

It means I'd need to use an argument of zero to have an array of one
element.  It also means the size of an array whose upper bound is
MAXINT is MAXINT+1, which could overflow, no?

I'm fine with this if users will be, but it feels strange.

> >> +given, the first argument is the lower bound of the array, and the
> >> +second argument is the upper bound of the array.
> 
> Eli> Will the reader know whether negative arguments are allowed (provided
> Eli> that the second is greater than the first)?  Or is it a good idea to
> Eli> tell explicitly?
> 
> I added a note.  New patch appended.

Thanks, this version is fine with me.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-08-23 17:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-20 22:32 RFA: fix PR python/11915 Tom Tromey
2010-08-21  7:54 ` Eli Zaretskii
2010-08-23 17:03   ` Tom Tromey
2010-08-23 17:16     ` Eli Zaretskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).