This is the mail archive of the archer@sourceware.org mailing list for the Archer 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]

[python] fix enum constant conversion


This patch fixes Value to correctly convert enum constants via the
int() and long() built-in functions.

This is the bug that Graydon reported the other day.

Tom

2008-12-01  Tom Tromey  <tromey@redhat.com>

	* python/python-value.c (is_intlike): New function.
	(valpy_int): Use it.
	(valpy_long): Likewise.

2008-12-01  Tom Tromey  <tromey@redhat.com>

	* gdb.python/python-value.exp (test_value_numeric_ops): Add
	test for conversion of enum constant.
	* gdb.python/python-value.c (enum e): New type.
	(evalue): New global.

diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c
index 2a6af14..5a7fae0 100644
--- a/gdb/python/python-value.c
+++ b/gdb/python/python-value.c
@@ -651,6 +651,18 @@ valpy_richcompare (PyObject *self, PyObject *other, int op)
   Py_RETURN_FALSE;
 }
 
+/* Helper function to determine if a type is "int-like".  */
+static int
+is_intlike (struct type *type, int ptr_ok)
+{
+  CHECK_TYPEDEF (type);
+  return (TYPE_CODE (type) == TYPE_CODE_INT
+	  || TYPE_CODE (type) == TYPE_CODE_ENUM
+	  || TYPE_CODE (type) == TYPE_CODE_BOOL
+	  || TYPE_CODE (type) == TYPE_CODE_CHAR
+	  || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
+}
+
 /* Implements conversion to int.  */
 static PyObject *
 valpy_int (PyObject *self)
@@ -661,7 +673,7 @@ valpy_int (PyObject *self)
   volatile struct gdb_exception except;
 
   CHECK_TYPEDEF (type);
-  if (TYPE_CODE (type) != TYPE_CODE_INT)
+  if (!is_intlike (type, 0))
     {
       PyErr_SetString (PyExc_RuntimeError, "cannot convert value to int");
       return NULL;
@@ -685,8 +697,7 @@ valpy_long (PyObject *self)
   LONGEST l = 0;
   volatile struct gdb_exception except;
 
-  CHECK_TYPEDEF (type);
-  if (TYPE_CODE (type) != TYPE_CODE_INT && TYPE_CODE (type) != TYPE_CODE_PTR)
+  if (!is_intlike (type, 1))
     {
       PyErr_SetString (PyExc_RuntimeError, "cannot convert value to long");
       return NULL;
diff --git a/gdb/testsuite/gdb.python/python-value.c b/gdb/testsuite/gdb.python/python-value.c
index 95d9e03..019b8f8 100644
--- a/gdb/testsuite/gdb.python/python-value.c
+++ b/gdb/testsuite/gdb.python/python-value.c
@@ -27,8 +27,16 @@ union u
   float b;
 };
 
+enum e
+  {
+    ONE = 1,
+    TWO = 2
+  };
+
 typedef struct s *PTR;
 
+enum e evalue = TWO;
+
 int
 main (int argc, char *argv[])
 {
diff --git a/gdb/testsuite/gdb.python/python-value.exp b/gdb/testsuite/gdb.python/python-value.exp
index a34af9b..9f76906 100644
--- a/gdb/testsuite/gdb.python/python-value.exp
+++ b/gdb/testsuite/gdb.python/python-value.exp
@@ -111,6 +111,11 @@ proc test_value_numeric_ops {} {
   gdb_test "python print 'result = ' + str(1-i)" " = -4" "subtract python integer from integer value"
   gdb_test "python print 'result = ' + str(1.5+f)" " = 2.75" "add python float with double value"
 
+  # Conversion test.
+  gdb_test "print evalue" " = TWO"
+  gdb_test "python evalue = gdb.get_value_from_history (0)" ""
+  gdb_test "python print int (evalue)" "2"
+
   # Test pointer arithmethic
 
   # First, obtain the pointers


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