Jan Kratochvil 23eda4b
http://sourceware.org/ml/gdb-patches/2010-10/msg00175.html
Jan Kratochvil 23eda4b
Subject: Re: [patch] python: save/restore/fix error state
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
On Fri, 08 Oct 2010 22:08:27 +0200, Doug Evans wrote:
Jan Kratochvil 23eda4b
> The comment above this code says:
Jan Kratochvil 23eda4b
> 
Jan Kratochvil 23eda4b
>   /* Note: If an exception occurs python will print the traceback and
Jan Kratochvil 23eda4b
>      clear the error indicator.  */
Jan Kratochvil 23eda4b
> 
Jan Kratochvil 23eda4b
> ISTM that either this comment is wrong or the above patch is wrong, or
Jan Kratochvil 23eda4b
> some combination thereof.
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
This comment is right, Python has no bug there, other gdb/python/ code has
Jan Kratochvil 23eda4b
bugs.  That is some gdb/python/ code returned success value while still
Jan Kratochvil 23eda4b
leaving the python exception set.  Such case is undefined and unchecked by
Jan Kratochvil 23eda4b
Python.  Python error state does not follow the errno POSIX semantics
Jan Kratochvil 23eda4b
	The setting of errno after a successful call to a function is
Jan Kratochvil 23eda4b
	unspecified [...].
Jan Kratochvil 23eda4b
as Python requires instead:
Jan Kratochvil 23eda4b
	The Python error state after a successful call to a function must be
Jan Kratochvil 23eda4b
	cleared.
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
I have checked callers of these functions, as a closure on the callers of the
Jan Kratochvil 23eda4b
top py-utils.c functions.  That does not mean this Python error state handling
Jan Kratochvil 23eda4b
verification is complete:
Jan Kratochvil 23eda4b
	python_string_to_unicode unicode_to_encoded_string
Jan Kratochvil 23eda4b
	unicode_to_encoded_python_string unicode_to_target_string
Jan Kratochvil 23eda4b
	unicode_to_target_python_string python_string_to_target_string
Jan Kratochvil 23eda4b
	python_string_to_target_python_string python_string_to_host_string
Jan Kratochvil 23eda4b
	target_string_to_unicode gdbpy_obj_to_string gdbpy_exception_to_string
Jan Kratochvil 23eda4b
	get_addr_from_python convert_value_from_python frapy_read_var
Jan Kratochvil 23eda4b
	gdbpy_get_display_hint valpy_getitem FIXME set_parameter_value
Jan Kratochvil 23eda4b
	compute_enum_values infpy_read_memory infpy_write_memory
Jan Kratochvil 23eda4b
	infpy_search_memory valpy_new valpy_call valpy_binop valpy_richcompare
Jan Kratochvil 23eda4b
	pretty_print_one_value set_attr parmpy_init
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
Therefore I have removed the check after PyRun_SimpleFile.
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
No regressions on {x86_64,x86_64-m32,i686}-fedora14snapshot-linux-gnu.
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
Thanks,
Jan Kratochvil 23eda4b
Jan
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
gdb/
Jan Kratochvil 23eda4b
2010-10-09  Jan Kratochvil  <jan.kratochvil@redhat.com>
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
	* python/py-breakpoint.c (bppy_set_condition): New comment.
Jan Kratochvil 23eda4b
	* python/py-cmd.c (cmdpy_function): Call also gdbpy_print_stack for
Jan Kratochvil 23eda4b
	failed PyUnicode_Decode.
Jan Kratochvil 23eda4b
	(cmdpy_completer): Skip element for failed
Jan Kratochvil 23eda4b
	python_string_to_host_string.
Jan Kratochvil 23eda4b
	(cmdpy_init): Return -1 on failed python_string_to_host_string.
Jan Kratochvil 23eda4b
	* python/py-frame.c (frapy_read_var): Extend the function comment.
Jan Kratochvil 23eda4b
	* python/py-function.c (fnpy_init): Return -1 on failed
Jan Kratochvil 23eda4b
	python_string_to_host_string.
Jan Kratochvil 23eda4b
	* python/py-inferior.c (infpy_read_memory, infpy_write_memory): Extend
Jan Kratochvil 23eda4b
	the function comment.
Jan Kratochvil 23eda4b
	(infpy_search_memory): Extend the function comment.  Remove the
Jan Kratochvil 23eda4b
	PyErr_SetString call on already set error state.
Jan Kratochvil 23eda4b
	* python/py-param.c (set_parameter_value): Extend the function
Jan Kratochvil 23eda4b
	comment.  Return -1 on failed python_string_to_host_string, twice.
Jan Kratochvil 23eda4b
	(set_attr): Extend the function comment.
Jan Kratochvil 23eda4b
	(compute_enum_values): Extend the function comment.  New variable
Jan Kratochvil 23eda4b
	back_to.  Protect self->enumeration by BACK_TO cleanups.  Return 0 on
Jan Kratochvil 23eda4b
	failed python_string_to_host_string.
Jan Kratochvil 23eda4b
	(get_doc_string): Call gdbpy_print_stack on failed
Jan Kratochvil 23eda4b
	python_string_to_host_string.
Jan Kratochvil 23eda4b
	(parmpy_init): Extend the function comment.
Jan Kratochvil 23eda4b
	* python/py-prettyprint.c (pretty_print_one_value): Likewise.
Jan Kratochvil 23eda4b
	(gdbpy_get_display_hint, print_children): Call gdbpy_print_stack on
Jan Kratochvil 23eda4b
	failed python_string_to_host_string.
Jan Kratochvil 23eda4b
	* python/py-value.c (valpy_new, valpy_getitem, valpy_call)
Jan Kratochvil 23eda4b
	(valpy_binop, valpy_richcompare): Extend the function comment.
Jan Kratochvil 23eda4b
	* python/python.c
Jan Kratochvil 23eda4b
	(struct python_env) <error_type, error_value, error_traceback>: New
Jan Kratochvil 23eda4b
	fields.
Jan Kratochvil 23eda4b
	(restore_python_env): Handle PyErr_Occurred.  Call PyErr_Restore.
Jan Kratochvil 23eda4b
	(ensure_python_env): Call PyErr_Fetch.
Jan Kratochvil 23eda4b
	* varobj.c (update_dynamic_varobj_children): Call gdbpy_print_stack on
Jan Kratochvil 23eda4b
	failed convert_value_from_python.
Jan Kratochvil 23eda4b
	(value_get_print_value): Call gdbpy_print_stack on failed
Jan Kratochvil 23eda4b
	python_string_to_target_python_string.
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
gdb/testsuite/
Jan Kratochvil 23eda4b
2010-10-09  Jan Kratochvil  <jan.kratochvil@redhat.com>
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
	* gdb.python/py-error.exp: New file.
Jan Kratochvil 23eda4b
	* gdb.python/py-error.py: New file.
Jan Kratochvil 23eda4b
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/py-breakpoint.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/py-breakpoint.c	2010-10-12 18:27:54.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/py-breakpoint.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -420,6 +420,9 @@ bppy_get_condition (PyObject *self, void
Jan Kratochvil 23eda4b
   return PyString_Decode (str, strlen (str), host_charset (), NULL);
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
+/* Returns 0 on success.  Returns -1 on error, with a python exception set.
Jan Kratochvil 23eda4b
+   */
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
 static int
Jan Kratochvil 23eda4b
 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/py-cmd.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/py-cmd.c	2010-10-12 18:27:54.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/py-cmd.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -138,7 +138,10 @@ cmdpy_function (struct cmd_list_element 
Jan Kratochvil 23eda4b
     args = "";
Jan Kratochvil 23eda4b
   argobj = PyUnicode_Decode (args, strlen (args), host_charset (), NULL);
Jan Kratochvil 23eda4b
   if (! argobj)
Jan Kratochvil 23eda4b
-    error (_("Could not convert arguments to Python string."));
Jan Kratochvil 23eda4b
+    {
Jan Kratochvil 23eda4b
+      gdbpy_print_stack ();
Jan Kratochvil 23eda4b
+      error (_("Could not convert arguments to Python string."));
Jan Kratochvil 23eda4b
+    }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
   ttyobj = from_tty ? Py_True : Py_False;
Jan Kratochvil 23eda4b
   Py_INCREF (ttyobj);
Jan Kratochvil 23eda4b
@@ -255,6 +258,12 @@ cmdpy_completer (struct cmd_list_element
Jan Kratochvil 23eda4b
 	      continue;
Jan Kratochvil 23eda4b
 	    }
Jan Kratochvil 23eda4b
 	  result[out] = python_string_to_host_string (elt);
Jan Kratochvil 23eda4b
+	  if (result[out] == NULL)
Jan Kratochvil 23eda4b
+	    {
Jan Kratochvil 23eda4b
+	      /* Skip problem elements.  */
Jan Kratochvil 23eda4b
+	      PyErr_Clear ();
Jan Kratochvil 23eda4b
+	      continue;
Jan Kratochvil 23eda4b
+	    }
Jan Kratochvil 23eda4b
 	  ++out;
Jan Kratochvil 23eda4b
 	}
Jan Kratochvil 23eda4b
       result[out] = NULL;
Jan Kratochvil 23eda4b
@@ -465,7 +474,15 @@ cmdpy_init (PyObject *self, PyObject *ar
Jan Kratochvil 23eda4b
       PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
       if (ds_obj && gdbpy_is_string (ds_obj))
Jan Kratochvil 23eda4b
-	docstring = python_string_to_host_string (ds_obj);
Jan Kratochvil 23eda4b
+	{
Jan Kratochvil 23eda4b
+	  docstring = python_string_to_host_string (ds_obj);
Jan Kratochvil 23eda4b
+	  if (docstring == NULL)
Jan Kratochvil 23eda4b
+	    {
Jan Kratochvil 23eda4b
+	      xfree (cmd_name);
Jan Kratochvil 23eda4b
+	      xfree (pfx_name);
Jan Kratochvil 23eda4b
+	      return -1;
Jan Kratochvil 23eda4b
+	    }
Jan Kratochvil 23eda4b
+	}
Jan Kratochvil 23eda4b
     }
Jan Kratochvil 23eda4b
   if (! docstring)
Jan Kratochvil 23eda4b
     docstring = xstrdup (_("This command is not documented."));
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/py-frame.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/py-frame.c	2010-06-28 23:16:03.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/py-frame.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -385,7 +385,8 @@ frapy_find_sal (PyObject *self, PyObject
Jan Kratochvil 23eda4b
    start the search from that block, otherwise search from the frame's
Jan Kratochvil 23eda4b
    current block (determined by examining the resume address of the
Jan Kratochvil 23eda4b
    frame).  The variable argument must be a string or an instance of a
Jan Kratochvil 23eda4b
-   gdb.Symbol.  The block argument must be an instance of gdb.Block.  */
Jan Kratochvil 23eda4b
+   gdb.Symbol.  The block argument must be an instance of gdb.Block.  Returns
Jan Kratochvil 23eda4b
+   NULL on error, with a python exception set.  */
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 frapy_read_var (PyObject *self, PyObject *args)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/py-function.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/py-function.c	2010-05-17 23:23:25.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/py-function.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -113,7 +113,14 @@ fnpy_init (PyObject *self, PyObject *arg
Jan Kratochvil 23eda4b
     {
Jan Kratochvil 23eda4b
       PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
Jan Kratochvil 23eda4b
       if (ds_obj && gdbpy_is_string (ds_obj))
Jan Kratochvil 23eda4b
-	docstring = python_string_to_host_string (ds_obj);
Jan Kratochvil 23eda4b
+	{
Jan Kratochvil 23eda4b
+	  docstring = python_string_to_host_string (ds_obj);
Jan Kratochvil 23eda4b
+	  if (docstring == NULL)
Jan Kratochvil 23eda4b
+	    {
Jan Kratochvil 23eda4b
+	      Py_DECREF (self);
Jan Kratochvil 23eda4b
+	      return -1;
Jan Kratochvil 23eda4b
+	    }
Jan Kratochvil 23eda4b
+	}
Jan Kratochvil 23eda4b
     }
Jan Kratochvil 23eda4b
   if (! docstring)
Jan Kratochvil 23eda4b
     docstring = xstrdup (_("This function is not documented."));
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/py-inferior.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/py-inferior.c	2010-06-28 23:16:03.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/py-inferior.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -293,7 +293,8 @@ gdbpy_inferiors (PyObject *unused, PyObj
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
 /* Implementation of gdb.read_memory (address, length).
Jan Kratochvil 23eda4b
    Returns a Python buffer object with LENGTH bytes of the inferior's
Jan Kratochvil 23eda4b
-   memory at ADDRESS.  Both arguments are integers.  */
Jan Kratochvil 23eda4b
+   memory at ADDRESS.  Both arguments are integers.  Returns NULL on error,
Jan Kratochvil 23eda4b
+   with a python exception set.  */
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
@@ -361,7 +362,8 @@ infpy_read_memory (PyObject *self, PyObj
Jan Kratochvil 23eda4b
    Writes the contents of BUFFER (a Python object supporting the read
Jan Kratochvil 23eda4b
    buffer protocol) at ADDRESS in the inferior's memory.  Write LENGTH
Jan Kratochvil 23eda4b
    bytes from BUFFER, or its entire contents if the argument is not
Jan Kratochvil 23eda4b
-   provided.  The function returns nothing.  */
Jan Kratochvil 23eda4b
+   provided.  The function returns nothing.  Returns NULL on error, with
Jan Kratochvil 23eda4b
+   a python exception set.  */
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
@@ -473,7 +475,8 @@ get_char_buffer (PyObject *self, Py_ssiz
Jan Kratochvil 23eda4b
    search from ADDRESS.  PATTERN is the pattern to search for (and
Jan Kratochvil 23eda4b
    must be a Python object supporting the buffer protocol).
Jan Kratochvil 23eda4b
    Returns a Python Long object holding the address where the pattern
Jan Kratochvil 23eda4b
-   was located, or if the pattern was not found, returns None.  */
Jan Kratochvil 23eda4b
+   was located, or if the pattern was not found, returns None.  Returns NULL
Jan Kratochvil 23eda4b
+   on error, with a python exception set.  */
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
@@ -511,12 +514,7 @@ infpy_search_memory (PyObject *self, PyO
Jan Kratochvil 23eda4b
 	}
Jan Kratochvil 23eda4b
     }
Jan Kratochvil 23eda4b
   else
Jan Kratochvil 23eda4b
-    {
Jan Kratochvil 23eda4b
-      PyErr_SetString (PyExc_RuntimeError,
Jan Kratochvil 23eda4b
-		       _("Cannot get search address/range from Python."));
Jan Kratochvil 23eda4b
-
Jan Kratochvil 23eda4b
-      return NULL;
Jan Kratochvil 23eda4b
-    }
Jan Kratochvil 23eda4b
+    return NULL;
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
   if (!PyObject_CheckReadBuffer (pattern))
Jan Kratochvil 23eda4b
     {
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/py-param.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/py-param.c	2010-05-17 23:23:25.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/py-param.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -110,8 +110,8 @@ get_attr (PyObject *obj, PyObject *attr_
Jan Kratochvil 23eda4b
   return PyObject_GenericGetAttr (obj, attr_name);
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
-/* Set a parameter value from a Python value.  Return 0 on success, -1
Jan Kratochvil 23eda4b
-   on failure.  */
Jan Kratochvil 23eda4b
+/* Set a parameter value from a Python value.  Return 0 on success.  Returns
Jan Kratochvil 23eda4b
+   -1 on error, with a python exception set.  */
Jan Kratochvil 23eda4b
 static int
Jan Kratochvil 23eda4b
 set_parameter_value (parmpy_object *self, PyObject *value)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
@@ -142,7 +142,11 @@ set_parameter_value (parmpy_object *self
Jan Kratochvil 23eda4b
 	    self->value.stringval = NULL;
Jan Kratochvil 23eda4b
 	}
Jan Kratochvil 23eda4b
       else
Jan Kratochvil 23eda4b
-	self->value.stringval = python_string_to_host_string (value);
Jan Kratochvil 23eda4b
+	{
Jan Kratochvil 23eda4b
+	  self->value.stringval = python_string_to_host_string (value);
Jan Kratochvil 23eda4b
+	  if (self->value.stringval == NULL)
Jan Kratochvil 23eda4b
+	    return -1;
Jan Kratochvil 23eda4b
+	}
Jan Kratochvil 23eda4b
       break;
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
     case var_enum:
Jan Kratochvil 23eda4b
@@ -158,6 +162,8 @@ set_parameter_value (parmpy_object *self
Jan Kratochvil 23eda4b
 	  }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
 	str = python_string_to_host_string (value);
Jan Kratochvil 23eda4b
+	if (str == NULL)
Jan Kratochvil 23eda4b
+	  return -1;
Jan Kratochvil 23eda4b
 	for (i = 0; self->enumeration[i]; ++i)
Jan Kratochvil 23eda4b
 	  if (! strcmp (self->enumeration[i], str))
Jan Kratochvil 23eda4b
 	    break;
Jan Kratochvil 23eda4b
@@ -258,7 +264,7 @@ set_parameter_value (parmpy_object *self
Jan Kratochvil 23eda4b
   return 0;
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
-/* Set an attribute.  */
Jan Kratochvil 23eda4b
+/* Set an attribute.  Returns -1 on error, with a python exception set.  */
Jan Kratochvil 23eda4b
 static int
Jan Kratochvil 23eda4b
 set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
@@ -358,12 +364,13 @@ add_setshow_generic (int parmclass, enum
Jan Kratochvil 23eda4b
     }
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
-/* A helper which computes enum values.  Returns 1 on success, 0 on
Jan Kratochvil 23eda4b
-   error.  */
Jan Kratochvil 23eda4b
+/* A helper which computes enum values.  Returns 1 on success.  Returns 0 on
Jan Kratochvil 23eda4b
+   error, with a python exception set.  */
Jan Kratochvil 23eda4b
 static int
Jan Kratochvil 23eda4b
 compute_enum_values (parmpy_object *self, PyObject *enum_values)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
   Py_ssize_t size, i;
Jan Kratochvil 23eda4b
+  struct cleanup *back_to;
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
   if (! enum_values)
Jan Kratochvil 23eda4b
     {
Jan Kratochvil 23eda4b
@@ -390,6 +397,7 @@ compute_enum_values (parmpy_object *self
Jan Kratochvil 23eda4b
     }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
   self->enumeration = xmalloc ((size + 1) * sizeof (char *));
Jan Kratochvil 23eda4b
+  back_to = make_cleanup (free_current_contents, &self->enumeration);
Jan Kratochvil 23eda4b
   memset (self->enumeration, 0, (size + 1) * sizeof (char *));
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
   for (i = 0; i < size; ++i)
Jan Kratochvil 23eda4b
@@ -397,16 +405,27 @@ compute_enum_values (parmpy_object *self
Jan Kratochvil 23eda4b
       PyObject *item = PySequence_GetItem (enum_values, i);
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
       if (! item)
Jan Kratochvil 23eda4b
-	return 0;
Jan Kratochvil 23eda4b
+	{
Jan Kratochvil 23eda4b
+	  do_cleanups (back_to);
Jan Kratochvil 23eda4b
+	  return 0;
Jan Kratochvil 23eda4b
+	}
Jan Kratochvil 23eda4b
       if (! gdbpy_is_string (item))
Jan Kratochvil 23eda4b
 	{
Jan Kratochvil 23eda4b
+	  do_cleanups (back_to);
Jan Kratochvil 23eda4b
 	  PyErr_SetString (PyExc_RuntimeError, 
Jan Kratochvil 23eda4b
 			   _("The enumeration item not a string."));
Jan Kratochvil 23eda4b
 	  return 0;
Jan Kratochvil 23eda4b
 	}
Jan Kratochvil 23eda4b
       self->enumeration[i] = python_string_to_host_string (item);
Jan Kratochvil 23eda4b
+      if (self->enumeration[i] == NULL)
Jan Kratochvil 23eda4b
+	{
Jan Kratochvil 23eda4b
+	  do_cleanups (back_to);
Jan Kratochvil 23eda4b
+	  return 0;
Jan Kratochvil 23eda4b
+	}
Jan Kratochvil 23eda4b
+      make_cleanup (xfree, (char *) self->enumeration[i]);
Jan Kratochvil 23eda4b
     }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
+  discard_cleanups (back_to);
Jan Kratochvil 23eda4b
   return 1;
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
@@ -422,7 +441,11 @@ get_doc_string (PyObject *object, PyObje
Jan Kratochvil 23eda4b
       PyObject *ds_obj = PyObject_GetAttr (object, attr);
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
       if (ds_obj && gdbpy_is_string (ds_obj))
Jan Kratochvil 23eda4b
-	result = python_string_to_host_string (ds_obj);
Jan Kratochvil 23eda4b
+	{
Jan Kratochvil 23eda4b
+	  result = python_string_to_host_string (ds_obj);
Jan Kratochvil 23eda4b
+	  if (result == NULL)
Jan Kratochvil 23eda4b
+	    gdbpy_print_stack ();
Jan Kratochvil 23eda4b
+	}
Jan Kratochvil 23eda4b
     }
Jan Kratochvil 23eda4b
   if (! result)
Jan Kratochvil 23eda4b
     result = xstrdup (_("This command is not documented."));
Jan Kratochvil 23eda4b
@@ -449,8 +472,9 @@ get_doc_string (PyObject *object, PyObje
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
    The documentation for the parameter is taken from the doc string
Jan Kratochvil 23eda4b
    for the python class.
Jan Kratochvil 23eda4b
-   
Jan Kratochvil 23eda4b
-*/
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+   Returns -1 on error, with a python exception set.  */
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
 static int
Jan Kratochvil 23eda4b
 parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/py-prettyprint.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/py-prettyprint.c	2010-10-12 18:27:57.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/py-prettyprint.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -185,8 +185,8 @@ find_pretty_printer (PyObject *value)
Jan Kratochvil 23eda4b
    is returned.  If the function returns Py_NONE that means the pretty
Jan Kratochvil 23eda4b
    printer returned the Python None as a value.  Otherwise, if the
Jan Kratochvil 23eda4b
    function returns a value,  *OUT_VALUE is set to the value, and NULL
Jan Kratochvil 23eda4b
-   is returned.  On error, *OUT_VALUE is set to NULL, and NULL is
Jan Kratochvil 23eda4b
-   returned.  */
Jan Kratochvil 23eda4b
+   is returned.  On error, *OUT_VALUE is set to NULL, NULL is
Jan Kratochvil 23eda4b
+   returned, with a python exception set.  */
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 pretty_print_one_value (PyObject *printer, struct value **out_value)
Jan Kratochvil 23eda4b
@@ -232,7 +232,11 @@ gdbpy_get_display_hint (PyObject *printe
Jan Kratochvil 23eda4b
   if (hint)
Jan Kratochvil 23eda4b
     {
Jan Kratochvil 23eda4b
       if (gdbpy_is_string (hint))
Jan Kratochvil 23eda4b
-	result = python_string_to_host_string (hint);
Jan Kratochvil 23eda4b
+	{
Jan Kratochvil 23eda4b
+	  result = python_string_to_host_string (hint);
Jan Kratochvil 23eda4b
+	  if (result == NULL)
Jan Kratochvil 23eda4b
+	    gdbpy_print_stack ();
Jan Kratochvil 23eda4b
+	}
Jan Kratochvil 23eda4b
       Py_DECREF (hint);
Jan Kratochvil 23eda4b
     }
Jan Kratochvil 23eda4b
   else
Jan Kratochvil 23eda4b
@@ -574,7 +578,10 @@ print_children (PyObject *printer, const
Jan Kratochvil 23eda4b
 	  else
Jan Kratochvil 23eda4b
 	    {
Jan Kratochvil 23eda4b
 	      output = python_string_to_host_string (py_v);
Jan Kratochvil 23eda4b
-	      fputs_filtered (output, stream);
Jan Kratochvil 23eda4b
+	      if (!output)
Jan Kratochvil 23eda4b
+		gdbpy_print_stack ();
Jan Kratochvil 23eda4b
+	      else
Jan Kratochvil 23eda4b
+		fputs_filtered (output, stream);
Jan Kratochvil 23eda4b
 	      xfree (output);
Jan Kratochvil 23eda4b
 	    }
Jan Kratochvil 23eda4b
 	}
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/py-value.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/py-value.c	2010-10-12 18:27:54.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/py-value.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -114,7 +114,8 @@ note_value (value_object *value_obj)
Jan Kratochvil 23eda4b
   values_in_python = value_obj;
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
-/* Called when a new gdb.Value object needs to be allocated.  */
Jan Kratochvil 23eda4b
+/* Called when a new gdb.Value object needs to be allocated.  Returns NULL on
Jan Kratochvil 23eda4b
+   error, with a python exception set.  */
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
@@ -334,7 +335,7 @@ valpy_length (PyObject *self)
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
 /* Given string name of an element inside structure, return its value
Jan Kratochvil 23eda4b
-   object.  */
Jan Kratochvil 23eda4b
+   object.  Returns NULL on error, with a python exception set.  */
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 valpy_getitem (PyObject *self, PyObject *key)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
@@ -468,7 +469,8 @@ enum valpy_opcode
Jan Kratochvil 23eda4b
   ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
 /* Returns a value object which is the result of applying the operation
Jan Kratochvil 23eda4b
-   specified by OPCODE to the given arguments.  */
Jan Kratochvil 23eda4b
+   specified by OPCODE to the given arguments.  Returns NULL on error, with
Jan Kratochvil 23eda4b
+   a python exception set.  */
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
@@ -723,7 +725,8 @@ valpy_xor (PyObject *self, PyObject *oth
Jan Kratochvil 23eda4b
   return valpy_binop (VALPY_BITXOR, self, other);
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
-/* Implements comparison operations for value objects.  */
Jan Kratochvil 23eda4b
+/* Implements comparison operations for value objects.  Returns NULL on error,
Jan Kratochvil 23eda4b
+   with a python exception set.  */
Jan Kratochvil 23eda4b
 static PyObject *
Jan Kratochvil 23eda4b
 valpy_richcompare (PyObject *self, PyObject *other, int op)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/python/python.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/python/python.c	2010-10-12 18:27:54.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/python/python.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -82,6 +82,7 @@ struct python_env
Jan Kratochvil 23eda4b
   PyGILState_STATE state;
Jan Kratochvil 23eda4b
   struct gdbarch *gdbarch;
Jan Kratochvil 23eda4b
   const struct language_defn *language;
Jan Kratochvil 23eda4b
+  PyObject *error_type, *error_value, *error_traceback;
Jan Kratochvil 23eda4b
 };
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
 static void
Jan Kratochvil 23eda4b
@@ -89,6 +90,16 @@ restore_python_env (void *p)
Jan Kratochvil 23eda4b
 {
Jan Kratochvil 23eda4b
   struct python_env *env = (struct python_env *)p;
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
+  /* Leftover Python error is forbidden by Python Exception Handling.  */
Jan Kratochvil 23eda4b
+  if (PyErr_Occurred ())
Jan Kratochvil 23eda4b
+    {
Jan Kratochvil 23eda4b
+      /* This order is similar to the one calling error afterwards. */
Jan Kratochvil 23eda4b
+      gdbpy_print_stack ();
Jan Kratochvil 23eda4b
+      warning (_("internal error: Unhandled Python exception"));
Jan Kratochvil 23eda4b
+    }
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+  PyErr_Restore (env->error_type, env->error_value, env->error_traceback);
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
   PyGILState_Release (env->state);
Jan Kratochvil 23eda4b
   python_gdbarch = env->gdbarch;
Jan Kratochvil 23eda4b
   python_language = env->language;
Jan Kratochvil 23eda4b
@@ -111,6 +122,9 @@ ensure_python_env (struct gdbarch *gdbar
Jan Kratochvil 23eda4b
   python_gdbarch = gdbarch;
Jan Kratochvil 23eda4b
   python_language = language;
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
+  /* Save it and ensure ! PyErr_Occurred () afterwards.  */
Jan Kratochvil 23eda4b
+  PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback);
Jan Kratochvil 23eda4b
+  
Jan Kratochvil 23eda4b
   return make_cleanup (restore_python_env, env);
Jan Kratochvil 23eda4b
 }
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/testsuite/gdb.python/py-error.exp
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/testsuite/gdb.python/py-error.exp	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -0,0 +1,56 @@
Jan Kratochvil 23eda4b
+# Copyright (C) 2010 Free Software Foundation, Inc.
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+# This program is free software; you can redistribute it and/or modify
Jan Kratochvil 23eda4b
+# it under the terms of the GNU General Public License as published by
Jan Kratochvil 23eda4b
+# the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil 23eda4b
+# (at your option) any later version.
Jan Kratochvil 23eda4b
+#
Jan Kratochvil 23eda4b
+# This program is distributed in the hope that it will be useful,
Jan Kratochvil 23eda4b
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil 23eda4b
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil 23eda4b
+# GNU General Public License for more details.
Jan Kratochvil 23eda4b
+#
Jan Kratochvil 23eda4b
+# You should have received a copy of the GNU General Public License
Jan Kratochvil 23eda4b
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+# Test error while loading *-gdb.py.  IBM1047 is chosen as possibly supported
Jan Kratochvil 23eda4b
+# by glibc but unsupported by Python
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+set testfile "py-error"
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+load_lib gdb-python.exp
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+# Start with a fresh gdb.
Jan Kratochvil 23eda4b
+gdb_exit
Jan Kratochvil 23eda4b
+gdb_start
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+# Skip all tests if Python scripting is not enabled.
Jan Kratochvil 23eda4b
+if { [skip_python_tests] } { continue }
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+set charset "IBM1047"
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+set test2 "main reached"
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+set test "set host-charset $charset"
Jan Kratochvil 23eda4b
+set test_regex [string_to_regexp $test]
Jan Kratochvil 23eda4b
+gdb_test_multiple $test $test {
Jan Kratochvil 23eda4b
+    -re "^$test_regex\r\n$gdb_prompt $" {
Jan Kratochvil 23eda4b
+	pass $test
Jan Kratochvil 23eda4b
+    }
Jan Kratochvil 23eda4b
+    -re "^$test_regex\r\nUndefined item: \"$charset\"\\.\r\n$gdb_prompt $" {
Jan Kratochvil 23eda4b
+	xfail $test
Jan Kratochvil 23eda4b
+	untested $test2
Jan Kratochvil 23eda4b
+	set test2 ""
Jan Kratochvil 23eda4b
+    }
Jan Kratochvil 23eda4b
+}
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+if {$test2 == ""} {
Jan Kratochvil 23eda4b
+    return 0
Jan Kratochvil 23eda4b
+}
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+set remote_python_file [remote_download host ${srcdir}/${subdir}/${testfile}.py]
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+# argc=LookupError: unknown encoding: IBM1047
Jan Kratochvil 23eda4b
+gdb_test "source $remote_python_file" "Traceback.*ClassName.*\r\nLookupError: unknown encoding: $charset" $test2
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+gdb_test "p 1" " = 1" "no delayed error"
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/testsuite/gdb.python/py-error.py
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/testsuite/gdb.python/py-error.py	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -0,0 +1,25 @@
Jan Kratochvil 23eda4b
+# Copyright (C) 2010 Free Software Foundation, Inc.
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+# This program is free software; you can redistribute it and/or modify
Jan Kratochvil 23eda4b
+# it under the terms of the GNU General Public License as published by
Jan Kratochvil 23eda4b
+# the Free Software Foundation; either version 3 of the License, or
Jan Kratochvil 23eda4b
+# (at your option) any later version.
Jan Kratochvil 23eda4b
+#
Jan Kratochvil 23eda4b
+# This program is distributed in the hope that it will be useful,
Jan Kratochvil 23eda4b
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
Jan Kratochvil 23eda4b
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jan Kratochvil 23eda4b
+# GNU General Public License for more details.
Jan Kratochvil 23eda4b
+#
Jan Kratochvil 23eda4b
+# You should have received a copy of the GNU General Public License
Jan Kratochvil 23eda4b
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+import gdb
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+class ClassName(gdb.Command):
Jan Kratochvil 23eda4b
+    'a'
Jan Kratochvil 23eda4b
+    def __init__(self):
Jan Kratochvil 23eda4b
+        gdb.Command.__init__ (self, "ClassName", gdb.COMMAND_DATA, prefix=True)
Jan Kratochvil 23eda4b
+    def invoke(self, args, from_tty):
Jan Kratochvil 23eda4b
+        print
Jan Kratochvil 23eda4b
+
Jan Kratochvil 23eda4b
+ClassName()
Jan Kratochvil 23eda4b
Index: gdb-7.2/gdb/varobj.c
Jan Kratochvil 23eda4b
===================================================================
Jan Kratochvil 23eda4b
--- gdb-7.2.orig/gdb/varobj.c	2010-10-12 18:27:54.000000000 +0200
Jan Kratochvil 23eda4b
+++ gdb-7.2/gdb/varobj.c	2010-10-12 18:28:58.000000000 +0200
Jan Kratochvil 23eda4b
@@ -1054,6 +1054,8 @@ update_dynamic_varobj_children (struct v
Jan Kratochvil 23eda4b
 	    error (_("Invalid item from the child list"));
Jan Kratochvil 23eda4b
 
Jan Kratochvil 23eda4b
 	  v = convert_value_from_python (py_v);
Jan Kratochvil 23eda4b
+	  if (v == NULL)
Jan Kratochvil 23eda4b
+	    gdbpy_print_stack ();
Jan Kratochvil 23eda4b
 	  install_dynamic_child (var, can_mention ? changed : NULL,
Jan Kratochvil 23eda4b
 				 can_mention ? new : NULL,
Jan Kratochvil 23eda4b
 				 can_mention ? unchanged : NULL,
Jan Kratochvil 23eda4b
@@ -2542,6 +2544,8 @@ value_get_print_value (struct value *val
Jan Kratochvil 23eda4b
 			type = builtin_type (gdbarch)->builtin_char;
Jan Kratochvil 23eda4b
 			Py_DECREF (py_str);
Jan Kratochvil 23eda4b
 		      }
Jan Kratochvil 23eda4b
+		    else
Jan Kratochvil 23eda4b
+		      gdbpy_print_stack ();
Jan Kratochvil 23eda4b
 		  }
Jan Kratochvil 23eda4b
 		Py_DECREF (output);
Jan Kratochvil 23eda4b
 	      }