gdb/
[binutils-gdb.git] / gdb / python / python-value.c
index 420d26f163cd5b473e69ad20724c2b1c004bd7ad..bf07dc43d1901c4e0f7bcbfb2eac8ecf663b5a8d 100644 (file)
@@ -143,6 +143,40 @@ valpy_address (PyObject *self, PyObject *args)
   return value_to_value_object (res_val);
 }
 
+/* Implementation of gdb.Value.string ([encoding] [, errors]) -> string
+   Return Unicode string with value contents.  If ENCODING is not given,
+   the string is assumed to be encoded in the target's charset.  */
+static PyObject *
+valpy_string (PyObject *self, PyObject *args, PyObject *kw)
+{
+  int length, ret = 0;
+  gdb_byte *buffer;
+  struct value *value = ((value_object *) self)->value;
+  volatile struct gdb_exception except;
+  PyObject *unicode;
+  const char *encoding = NULL;
+  const char *errors = NULL;
+  const char *user_encoding = NULL;
+  const char *la_encoding = NULL;
+  static char *keywords[] = { "encoding", "errors" };
+
+  if (!PyArg_ParseTupleAndKeywords (args, kw, "|ss", keywords,
+                                   &user_encoding, &errors))
+    return NULL;
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      LA_GET_STRING (value, &buffer, &length, &la_encoding);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
+  unicode = PyUnicode_Decode (buffer, length, encoding, errors);
+  xfree (buffer);
+
+  return unicode;
+}
+
 static Py_ssize_t
 valpy_length (PyObject *self)
 {
@@ -238,6 +272,18 @@ valpy_str (PyObject *self)
   return result;
 }
 
+/* Implements gdb.Value.is_optimized_out.  */
+static PyObject *
+valpy_get_is_optimized_out (PyObject *self, void *closure)
+{
+  struct value *value = ((value_object *) self)->value;
+
+  if (value_optimized_out (value))
+    Py_RETURN_TRUE;
+
+  Py_RETURN_FALSE;
+}
+
 enum valpy_opcode
 {
   VALPY_ADD,
@@ -275,11 +321,11 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
         a gdb.Value object and need to convert it from python as well.  */
       arg1 = convert_value_from_python (self);
       if (arg1 == NULL)
-       return NULL;
+       break;
 
       arg2 = convert_value_from_python (other);
       if (arg2 == NULL)
-       return NULL;
+       break;
 
       switch (opcode)
        {
@@ -356,7 +402,7 @@ valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
     }
   GDB_PY_HANDLE_EXCEPTION (except);
 
-  return value_to_value_object (res_val);
+  return res_val ? value_to_value_object (res_val) : NULL;
 }
 
 static PyObject *
@@ -743,7 +789,7 @@ convert_value_from_python (PyObject *obj)
            }
        }
       else if (PyObject_TypeCheck (obj, &value_object_type))
-       value = ((value_object *) obj)->value;
+       value = value_copy (((value_object *) obj)->value);
       else
        PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s"),
                      PyString_AsString (PyObject_Str (obj)));
@@ -791,9 +837,19 @@ gdbpy_initialize_values (void)
   values_in_python = NULL;
 }
 
+static PyGetSetDef value_object_getset[] = {
+  { "is_optimized_out", valpy_get_is_optimized_out, NULL,
+    "Boolean telling whether the value is optimized out (i.e., not available).",
+    NULL },
+  {NULL}  /* Sentinel */
+};
+
 static PyMethodDef value_object_methods[] = {
   { "address", valpy_address, METH_NOARGS, "Return the address of the value." },
   { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
+  { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
+    "string ([encoding] [, errors]) -> string\n\
+Return Unicode string representation of the value." },
   {NULL}  /* Sentinel */
 };
 
@@ -860,7 +916,7 @@ PyTypeObject value_object_type = {
   0,                             /* tp_iternext */
   value_object_methods,                  /* tp_methods */
   0,                             /* tp_members */
-  0,                             /* tp_getset */
+  value_object_getset,           /* tp_getset */
   0,                             /* tp_base */
   0,                             /* tp_dict */
   0,                             /* tp_descr_get */