Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [python] [patch] PR 13624
@ 2011-10-06 12:49 Phil Muldoon
  2011-10-06 13:23 ` Paul_Koning
  2011-10-06 13:51 ` Tom Tromey
  0 siblings, 2 replies; 6+ messages in thread
From: Phil Muldoon @ 2011-10-06 12:49 UTC (permalink / raw)
  To: gdb-patches


The patch address PR 13624 which noted that in some instances we were
not catching GDB exceptions.  I checked every function and I found two
GDB cases, and one Python case.

OK?

Cheers,

Phil

--

2011-10-06  Phil Muldoon  <pmuldoon@redhat.com>

	* python/py-value.c (valpy_call): Check that arguments are
	a tuple.
	(valpy_nonzero): Catch GDB exceptions.
	(valpy_absolute): Ditto.
--

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 252d9b9..48bbb0a 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -519,6 +519,13 @@ valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
       return NULL;
     }
 
+  if (! PyTuple_Check (args))
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+		       _("Inferior arguments must be provided in a tuple."));
+      return NULL;
+    }
+
   args_count = PyTuple_Size (args);
   if (args_count > 0)
     {
@@ -792,33 +799,53 @@ static PyObject *
 valpy_absolute (PyObject *self)
 {
   struct value *value = ((value_object *) self)->value;
+  volatile struct gdb_exception except;
+  int isabs = 1;
 
-  if (value_less (value, value_zero (value_type (value), not_lval)))
-    return valpy_negative (self);
-  else
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      if (value_less (value, value_zero (value_type (value), not_lval)))
+	isabs = 0;
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  if (isabs)
     return valpy_positive (self);
+  else
+    return valpy_negative (self);
 }
 
 /* Implements boolean evaluation of gdb.Value.  */
 static int
 valpy_nonzero (PyObject *self)
 {
+  volatile struct gdb_exception except;
   value_object *self_value = (value_object *) self;
   struct type *type;
+  int nonzero = 0; /* Appease GCC warning.  */
 
   type = check_typedef (value_type (self_value->value));
 
-  if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
-    return !!value_as_long (self_value->value);
-  else if (TYPE_CODE (type) == TYPE_CODE_FLT)
-    return value_as_double (self_value->value) != 0;
-  else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
-    return !decimal_is_zero (value_contents (self_value->value),
-			     TYPE_LENGTH (type),
-			     gdbarch_byte_order (get_type_arch (type)));
-  else
-    /* All other values are True.  */
-    return 1;
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
+	nonzero = !!value_as_long (self_value->value);
+      else if (TYPE_CODE (type) == TYPE_CODE_FLT)
+	nonzero = value_as_double (self_value->value) != 0;
+      else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
+	nonzero = !decimal_is_zero (value_contents (self_value->value),
+				 TYPE_LENGTH (type),
+				 gdbarch_byte_order (get_type_arch (type)));
+      else
+	/* All other values are True.  */
+	nonzero = 1;
+    }
+  /* This is not documented in the Python documentation, but if this
+     function fails, return -1 as slot_nb_nonzero does (the default
+     Python nonzero function).  */
+  GDB_PY_SET_HANDLE_EXCEPTION (except);
+
+  return nonzero;
 }
 
 /* Implements ~ for value objects.  */


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

* RE: [python] [patch] PR 13624
  2011-10-06 12:49 [python] [patch] PR 13624 Phil Muldoon
@ 2011-10-06 13:23 ` Paul_Koning
  2011-10-06 13:51 ` Tom Tromey
  1 sibling, 0 replies; 6+ messages in thread
From: Paul_Koning @ 2011-10-06 13:23 UTC (permalink / raw)
  To: pmuldoon, gdb-patches

Could you make the new exception be a more explicit Pythonic one?  TypeError seems like the natural fit.

	paul

-----Original Message-----
From: gdb-patches-owner@sourceware.org [mailto:gdb-patches-owner@sourceware.org] On Behalf Of Phil Muldoon
Sent: Thursday, October 06, 2011 8:49 AM
To: gdb-patches@sourceware.org
Subject: [python] [patch] PR 13624


The patch address PR 13624 which noted that in some instances we were not catching GDB exceptions.  I checked every function and I found two GDB cases, and one Python case.

OK?

Cheers,

Phil

--

2011-10-06  Phil Muldoon  <pmuldoon@redhat.com>

	* python/py-value.c (valpy_call): Check that arguments are
	a tuple.
	(valpy_nonzero): Catch GDB exceptions.
	(valpy_absolute): Ditto.
--

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 252d9b9..48bbb0a 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -519,6 +519,13 @@ valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
       return NULL;
     }
 
+  if (! PyTuple_Check (args))
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+		       _("Inferior arguments must be provided in a tuple."));
+      return NULL;
+    }
+
...


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

* Re: [python] [patch] PR 13624
  2011-10-06 12:49 [python] [patch] PR 13624 Phil Muldoon
  2011-10-06 13:23 ` Paul_Koning
@ 2011-10-06 13:51 ` Tom Tromey
  2011-10-06 15:43   ` Phil Muldoon
  1 sibling, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2011-10-06 13:51 UTC (permalink / raw)
  To: pmuldoon; +Cc: gdb-patches

>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:

Phil> The patch address PR 13624 which noted that in some instances we were
Phil> not catching GDB exceptions.  I checked every function and I found two
Phil> GDB cases, and one Python case.

Phil> OK?

Almost.

Phil> +      PyErr_SetString (PyExc_RuntimeError,
Phil> +		       _("Inferior arguments must be provided in a tuple."));

I think this should use PyExc_TypeError.

Phil>    type = check_typedef (value_type (self_value->value));

This line should also be in the TRY_CATCH.

valpy_int is also missing some exception handling around CHECK_TYPEDEF.
Also is_intlike has to be dealt with somehow (maybe by removing the
CHECK_TYPEDEF and pushing it to the callers).
valpy_float has an unprotected CHECK_TYPEDEF.
valpy_lazy_string has an unprotected call to value_ind.
valpy_get_is_optimized_out has an unprotected call to
value_optimized_out; this one might not matter, but it is harmless to be
defensive.

Tom


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

* Re: [python] [patch] PR 13624
  2011-10-06 13:51 ` Tom Tromey
@ 2011-10-06 15:43   ` Phil Muldoon
  2011-10-06 17:44     ` Tom Tromey
  0 siblings, 1 reply; 6+ messages in thread
From: Phil Muldoon @ 2011-10-06 15:43 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

Tom Tromey <tromey@redhat.com> writes:

>>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
>
> Phil> The patch address PR 13624 which noted that in some instances we were
> Phil> not catching GDB exceptions.  I checked every function and I found two
> Phil> GDB cases, and one Python case.
>
> Phil> OK?
>
> Almost.

Thanks.  New patch attached:

--

2011-10-06  Phil Muldoon  <pmuldoon@redhat.com>

	* python/py-value.c (valpy_call): Check that arguments are
	a tuple.
        (is_intlike): Remove call to CHECK_TYPEDEF.
	(valpy_nonzero): Catch GDB exceptions.
	(valpy_absolute): Ditto.
        (valpy_lazy_string): Ditto.
        (valpy_call): Ditto.
        (valpy_get_is_optimized_out): Ditto.
        (valpy_long): Ditto.
        (valpy_float): Ditto.
        (valpy_int): Call CHECK_TYPEDEF.  Catch GDB exceptions.
        (valpy_richcompare): Ditto.

--
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 252d9b9..79de586 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -312,13 +312,18 @@ valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
   const char *user_encoding = NULL;
   static char *keywords[] = { "encoding", "length", NULL };
   PyObject *str_obj;
+  volatile struct gdb_exception except;
 
   if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords,
 				    &user_encoding, &length))
     return NULL;
 
-  if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
-    value = value_ind (value);
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
+	value = value_ind (value);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
 
   str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
 					     user_encoding,
@@ -510,7 +515,13 @@ valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
   volatile struct gdb_exception except;
   struct value *function = ((value_object *) self)->value;
   struct value **vargs = NULL;
-  struct type *ftype = check_typedef (value_type (function));
+  struct type *ftype;
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      ftype = check_typedef (value_type (function));
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
 
   if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
     {
@@ -519,6 +530,13 @@ valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
       return NULL;
     }
 
+  if (! PyTuple_Check (args))
+    {
+      PyErr_SetString (PyExc_TypeError,
+		       _("Inferior arguments must be provided in a tuple."));
+      return NULL;
+    }
+
   args_count = PyTuple_Size (args);
   if (args_count > 0)
     {
@@ -584,8 +602,16 @@ static PyObject *
 valpy_get_is_optimized_out (PyObject *self, void *closure)
 {
   struct value *value = ((value_object *) self)->value;
+  int opt = 0;
+  volatile struct gdb_exception except;
 
-  if (value_optimized_out (value))
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      opt = value_optimized_out (value);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  if (opt)
     Py_RETURN_TRUE;
 
   Py_RETURN_FALSE;
@@ -792,33 +818,53 @@ static PyObject *
 valpy_absolute (PyObject *self)
 {
   struct value *value = ((value_object *) self)->value;
+  volatile struct gdb_exception except;
+  int isabs = 1;
 
-  if (value_less (value, value_zero (value_type (value), not_lval)))
-    return valpy_negative (self);
-  else
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      if (value_less (value, value_zero (value_type (value), not_lval)))
+	isabs = 0;
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  if (isabs)
     return valpy_positive (self);
+  else
+    return valpy_negative (self);
 }
 
 /* Implements boolean evaluation of gdb.Value.  */
 static int
 valpy_nonzero (PyObject *self)
 {
+  volatile struct gdb_exception except;
   value_object *self_value = (value_object *) self;
   struct type *type;
+  int nonzero = 0; /* Appease GCC warning.  */
 
   type = check_typedef (value_type (self_value->value));
 
-  if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
-    return !!value_as_long (self_value->value);
-  else if (TYPE_CODE (type) == TYPE_CODE_FLT)
-    return value_as_double (self_value->value) != 0;
-  else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
-    return !decimal_is_zero (value_contents (self_value->value),
-			     TYPE_LENGTH (type),
-			     gdbarch_byte_order (get_type_arch (type)));
-  else
-    /* All other values are True.  */
-    return 1;
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
+	nonzero = !!value_as_long (self_value->value);
+      else if (TYPE_CODE (type) == TYPE_CODE_FLT)
+	nonzero = value_as_double (self_value->value) != 0;
+      else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
+	nonzero = !decimal_is_zero (value_contents (self_value->value),
+				 TYPE_LENGTH (type),
+				 gdbarch_byte_order (get_type_arch (type)));
+      else
+	/* All other values are True.  */
+	nonzero = 1;
+    }
+  /* This is not documented in the Python documentation, but if this
+     function fails, return -1 as slot_nb_nonzero does (the default
+     Python nonzero function).  */
+  GDB_PY_SET_HANDLE_EXCEPTION (except);
+
+  return nonzero;
 }
 
 /* Implements ~ for value objects.  */
@@ -954,7 +1000,6 @@ valpy_richcompare (PyObject *self, PyObject *other, int op)
 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
@@ -971,16 +1016,12 @@ valpy_int (PyObject *self)
   LONGEST l = 0;
   volatile struct gdb_exception except;
 
-  CHECK_TYPEDEF (type);
-  if (!is_intlike (type, 0))
-    {
-      PyErr_SetString (PyExc_RuntimeError, 
-		       _("Cannot convert value to int."));
-      return NULL;
-    }
-
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
+      CHECK_TYPEDEF (type);
+      if (!is_intlike (type, 0))
+	error( _("Cannot convert value to int."));
+
       l = value_as_long (value);
     }
   GDB_PY_HANDLE_EXCEPTION (except);
@@ -997,15 +1038,13 @@ valpy_long (PyObject *self)
   LONGEST l = 0;
   volatile struct gdb_exception except;
 
-  if (!is_intlike (type, 1))
-    {
-      PyErr_SetString (PyExc_RuntimeError, 
-		       _("Cannot convert value to long."));
-      return NULL;
-    }
-
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
+      CHECK_TYPEDEF (type);
+
+      if (!is_intlike (type, 1))
+	error (_("Cannot convert value to long."));
+
       l = value_as_long (value);
     }
   GDB_PY_HANDLE_EXCEPTION (except);
@@ -1022,16 +1061,13 @@ valpy_float (PyObject *self)
   double d = 0;
   volatile struct gdb_exception except;
 
-  CHECK_TYPEDEF (type);
-  if (TYPE_CODE (type) != TYPE_CODE_FLT)
-    {
-      PyErr_SetString (PyExc_RuntimeError, 
-		       _("Cannot convert value to float."));
-      return NULL;
-    }
-
   TRY_CATCH (except, RETURN_MASK_ALL)
     {
+      CHECK_TYPEDEF (type);
+
+      if (TYPE_CODE (type) != TYPE_CODE_FLT)
+	error(_("Cannot convert value to float."));
+
       d = value_as_double (value);
     }
   GDB_PY_HANDLE_EXCEPTION (except);


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

* Re: [python] [patch] PR 13624
  2011-10-06 15:43   ` Phil Muldoon
@ 2011-10-06 17:44     ` Tom Tromey
  2011-10-07 13:47       ` Phil Muldoon
  0 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2011-10-06 17:44 UTC (permalink / raw)
  To: pmuldoon; +Cc: gdb-patches

>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:

Phil> Thanks.  New patch attached:

Thanks.  One nit.

Phil> +	error(_("Cannot convert value to float."));

Missing space.

Ok with that change.

Tom


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

* Re: [python] [patch] PR 13624
  2011-10-06 17:44     ` Tom Tromey
@ 2011-10-07 13:47       ` Phil Muldoon
  0 siblings, 0 replies; 6+ messages in thread
From: Phil Muldoon @ 2011-10-07 13:47 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

Tom Tromey <tromey@redhat.com> writes:

>>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
>
> Phil> Thanks.  New patch attached:
>
> Thanks.  One nit.
>
> Phil> +	error(_("Cannot convert value to float."));
>
> Missing space.
>
> Ok with that change.

So committed, thanks.

Cheers,

Phil


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

end of thread, other threads:[~2011-10-07 13:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-06 12:49 [python] [patch] PR 13624 Phil Muldoon
2011-10-06 13:23 ` Paul_Koning
2011-10-06 13:51 ` Tom Tromey
2011-10-06 15:43   ` Phil Muldoon
2011-10-06 17:44     ` Tom Tromey
2011-10-07 13:47       ` Phil Muldoon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox