]> Git Repo - binutils.git/blobdiff - gdb/python/py-lazy-string.c
update copyright year range in GDB files
[binutils.git] / gdb / python / py-lazy-string.c
index 23cf13a0577623cd67bfcc92231687a3318d6887..14575bff85ab9cb0b5c839d184e3a09fd74e1d7f 100644 (file)
@@ -1,6 +1,6 @@
 /* Python interface to lazy strings.
 
-   Copyright (C) 2010 Free Software Foundation, Inc.
+   Copyright (C) 2010-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -21,7 +21,6 @@
 #include "python-internal.h"
 #include "charset.h"
 #include "value.h"
-#include "exceptions.h"
 #include "valprint.h"
 #include "language.h"
 
@@ -46,14 +45,15 @@ typedef struct {
   struct type *type;
 } lazy_string_object;
 
-static PyTypeObject lazy_string_object_type;
+extern PyTypeObject lazy_string_object_type
+    CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("lazy_string_object");
 
 static PyObject *
 stpy_get_address (PyObject *self, void *closure)
 {
   lazy_string_object *self_string = (lazy_string_object *) self;
 
-  return PyLong_FromUnsignedLongLong (self_string->address);
+  return gdb_py_long_from_ulongest (self_string->address);
 }
 
 static PyObject *
@@ -83,7 +83,7 @@ stpy_get_length (PyObject *self, void *closure)
   return PyLong_FromLong (self_string->length);
 }
 
-PyObject *
+static PyObject *
 stpy_get_type (PyObject *self, void *closure)
 {
   lazy_string_object *str_obj = (lazy_string_object *) self;
@@ -95,16 +95,25 @@ static PyObject *
 stpy_convert_to_value  (PyObject *self, PyObject *args)
 {
   lazy_string_object *self_string = (lazy_string_object *) self;
-  struct value *val;
+  struct value *val = NULL;
 
   if (self_string->address == 0)
     {
-      PyErr_SetString (PyExc_MemoryError,
+      PyErr_SetString (gdbpy_gdb_memory_error,
                       _("Cannot create a value from NULL."));
       return NULL;
     }
 
-  val = value_at_lazy (self_string->type, self_string->address);
+  TRY
+    {
+      val = value_at_lazy (self_string->type, self_string->address);
+    }
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+  END_CATCH
+
   return value_to_value_object (val);
 }
 
@@ -124,7 +133,7 @@ gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
 
   if (address == 0 && length != 0)
     {
-      PyErr_SetString (PyExc_MemoryError,
+      PyErr_SetString (gdbpy_gdb_memory_error,
                       _("Cannot create a lazy string with address 0x0, " \
                         "and a non-zero length."));
       return NULL;
@@ -152,13 +161,14 @@ gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
   return (PyObject *) str_obj;
 }
 
-void
+int
 gdbpy_initialize_lazy_string (void)
 {
   if (PyType_Ready (&lazy_string_object_type) < 0)
-    return;
+    return -1;
 
   Py_INCREF (&lazy_string_object_type);
+  return 0;
 }
 
 /* Determine whether the printer object pointed to by OBJ is a
@@ -169,86 +179,26 @@ gdbpy_is_lazy_string (PyObject *result)
   return PyObject_TypeCheck (result, &lazy_string_object_type);
 }
 
-/* Extract and return the actual string from the lazy string object
-   STRING.  Addtionally, the string type is written to *STR_TYPE, the
-   string length is written to *LENGTH, and the string encoding is
-   written to *ENCODING.  On error, NULL is returned.  The caller is
-   responsible for freeing the returned buffer.  */
-gdb_byte *
-gdbpy_extract_lazy_string (PyObject *string, struct type **str_type,
-                    long *length, char **encoding)
-{
-  int width;
-  int bytes_read;
-  gdb_byte *buffer = NULL;
-  int errcode = 0;
-  CORE_ADDR addr;
-  struct gdbarch *gdbarch;
-  enum bfd_endian byte_order;
-  PyObject *py_len = NULL, *py_encoding = NULL; 
-  PyObject *py_addr = NULL, *py_type = NULL;
-  volatile struct gdb_exception except;
-
-  py_len = PyObject_GetAttrString (string, "length");
-  py_encoding = PyObject_GetAttrString (string, "encoding");
-  py_addr = PyObject_GetAttrString (string, "address");
-  py_type = PyObject_GetAttrString (string, "type");
-
-  /* A NULL encoding, length, address or type is not ok.  */
-  if (!py_len || !py_encoding || !py_addr || !py_type)
-    goto error;
-
-  *length = PyLong_AsLong (py_len);
-  addr = PyLong_AsUnsignedLongLong (py_addr);
-
-  /* If the user supplies Py_None an encoding, set encoding to NULL.
-     This will trigger the resulting LA_PRINT_CALL to automatically
-     select an encoding.  */
-  if (py_encoding == Py_None)
-    *encoding = NULL;
-  else
-    *encoding = xstrdup (PyString_AsString (py_encoding));
+/* Extract the parameters from the lazy string object STRING.
+   ENCODING will either be set to NULL, or will be allocated with
+   xmalloc, in which case the callers is responsible for freeing
+   it.  */
 
-  *str_type = type_object_to_type (py_type);
-  gdbarch = get_type_arch (*str_type);
-  byte_order = gdbarch_byte_order (gdbarch);
-  width = TYPE_LENGTH (*str_type);
+void
+gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
+                          struct type **str_type,
+                          long *length, char **encoding)
+{
+  lazy_string_object *lazy;
 
-  TRY_CATCH (except, RETURN_MASK_ALL)
-    {
-      errcode = read_string (addr, *length, width,
-                            *length, byte_order, &buffer,
-                            &bytes_read);
-    }
-  if (except.reason < 0)
-    {
-      PyErr_Format (except.reason == RETURN_QUIT                       \
-                   ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,     \
-                   "%s", except.message);                              \
-      goto error;
+  gdb_assert (gdbpy_is_lazy_string (string));
 
-    }
+  lazy = (lazy_string_object *) string;
 
-  if (errcode)
-    goto error;
-
-  *length = bytes_read / width;
-
-  Py_DECREF (py_encoding);
-  Py_DECREF (py_len);
-  Py_DECREF (py_addr);
-  Py_DECREF (py_type);
-  return buffer;
-
- error:
-  Py_XDECREF (py_encoding);
-  Py_XDECREF (py_len);
-  Py_XDECREF (py_addr);
-  Py_XDECREF (py_type);
-  xfree (buffer);
-  *length = 0;
-  *str_type = NULL;
-  return NULL;
+  *addr = lazy->address;
+  *str_type = lazy->type;
+  *length = lazy->length;
+  *encoding = lazy->encoding ? xstrdup (lazy->encoding) : NULL;
 }
 
 \f
@@ -268,9 +218,8 @@ static PyGetSetDef lazy_string_object_getset[] = {
   { NULL }  /* Sentinel */
 };
 
-static PyTypeObject lazy_string_object_type = {
-  PyObject_HEAD_INIT (NULL)
-  0,                             /*ob_size*/
+PyTypeObject lazy_string_object_type = {
+  PyVarObject_HEAD_INIT (NULL, 0)
   "gdb.LazyString",              /*tp_name*/
   sizeof (lazy_string_object),   /*tp_basicsize*/
   0,                             /*tp_itemsize*/
This page took 0.032815 seconds and 4 git commands to generate.