don't let bin2hex call strlen
[deliverable/binutils-gdb.git] / gdb / python / py-utils.c
index 890b65d346e197a511ba98c4d105e200889f2698..deca3e83cb5b7b6f42fc57e3b46db266b0c71798 100644 (file)
@@ -1,6 +1,6 @@
 /* General utility routines for GDB/Python.
 
-   Copyright (C) 2008-2013 Free Software Foundation, Inc.
+   Copyright (C) 2008-2014 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -31,12 +31,7 @@ py_decref (void *p)
 {
   PyObject *py = p;
 
-  /* Note that we need the extra braces in this 'if' to avoid a
-     warning from gcc.  */
-  if (py)
-    {
-      Py_DECREF (py);
-    }
+  Py_DECREF (py);
 }
 
 /* Return a new cleanup which will decrement the Python object's
@@ -76,7 +71,7 @@ make_cleanup_py_xdecref (PyObject *py)
 
    As an added bonus, the functions accepts a unicode string and returns it
    right away, so callers don't need to check which kind of string they've
-   got.  In Python 3, all strings are Unicode so this case is always the 
+   got.  In Python 3, all strings are Unicode so this case is always the
    one that applies.
 
    If the given object is not one of the mentioned string types, NULL is
@@ -221,7 +216,7 @@ python_string_to_host_string (PyObject *obj)
   if (str == NULL)
     return NULL;
 
-  result = unicode_to_encoded_string (str, host_charset ()); 
+  result = unicode_to_encoded_string (str, host_charset ());
   Py_DECREF (str);
   return result;
 }
@@ -293,10 +288,10 @@ gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue)
 }
 
 /* Convert a GDB exception to the appropriate Python exception.
-   
-   This sets the Python error indicator, and returns NULL.  */
 
-PyObject *
+   This sets the Python error indicator.  */
+
+void
 gdbpy_convert_exception (struct gdb_exception exception)
 {
   PyObject *exc_class;
@@ -308,44 +303,51 @@ gdbpy_convert_exception (struct gdb_exception exception)
   else
     exc_class = gdbpy_gdb_error;
 
-  return PyErr_Format (exc_class, "%s", exception.message);
+  PyErr_Format (exc_class, "%s", exception.message);
 }
 
 /* Converts OBJ to a CORE_ADDR value.
 
-   Returns 1 on success or 0 on failure, with a Python exception set.  This
-   function can also throw GDB exceptions.
+   Returns 0 on success or -1 on failure, with a Python exception set.
 */
 
 int
 get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
 {
   if (gdbpy_is_value_object (obj))
-    *addr = value_as_address (value_object_to_value (obj));
+    {
+      volatile struct gdb_exception except;
+
+      TRY_CATCH (except, RETURN_MASK_ALL)
+       {
+         *addr = value_as_address (value_object_to_value (obj));
+       }
+      GDB_PY_SET_HANDLE_EXCEPTION (except);
+    }
   else
     {
       PyObject *num = PyNumber_Long (obj);
       gdb_py_ulongest val;
 
       if (num == NULL)
-       return 0;
+       return -1;
 
       val = gdb_py_long_as_ulongest (num);
       Py_XDECREF (num);
       if (PyErr_Occurred ())
-       return 0;
+       return -1;
 
       if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
        {
          PyErr_SetString (PyExc_ValueError,
                           _("Overflow converting to address."));
-         return 0;
+         return -1;
        }
 
       *addr = val;
     }
 
-  return 1;
+  return 0;
 }
 
 /* Convert a LONGEST to the appropriate Python object -- either an
@@ -424,3 +426,19 @@ gdb_py_generic_dict (PyObject *self, void *closure)
   Py_INCREF (result);
   return result;
 }
+
+/* Like PyModule_AddObject, but does not steal a reference to
+   OBJECT.  */
+
+int
+gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object)
+{
+  int result;
+
+  Py_INCREF (object);
+  /* Python 2.4 did not have a 'const' here.  */
+  result = PyModule_AddObject (module, (char *) name, object);
+  if (result < 0)
+    Py_DECREF (object);
+  return result;
+}
This page took 0.027309 seconds and 4 git commands to generate.