Define unique_ptr specialization for Py_buffer.
authorKevin Buettner <kevinb@redhat.com>
Sun, 17 Feb 2019 00:33:47 +0000 (17:33 -0700)
committerKevin Buettner <kevinb@redhat.com>
Tue, 26 Feb 2019 17:21:22 +0000 (10:21 -0700)
This patch causes PyBuffer_Release() to be called when the associated
buffer goes out of scope.  I've been using it as follows:

 ...
 Py_buffer_up buffer_up;
 Py_buffer py_buf;

 if (PyObject_CheckBuffer (obj)
     && PyObject_GetBuffer (obj, &py_buf, PyBUF_SIMPLE) == 0)
   {
      /* Got a buffer, py_buf, out of obj.  Cause it to released
         when it goes out of scope.  */
     buffer_up.reset (&py_buf);
   }
   ...

This snippet of code was taken directly from an upcoming patch to
python-value.c.

gdb/ChangeLog:

* python/python-internal.h (Py_buffer_deleter): New struct.
(Py_buffer_up): New typedef.

gdb/ChangeLog
gdb/python/python-internal.h

index 879ad6a97079d7560ae7ccfb0c5044d6cda3f88e..f311a24618b0390754a156e82e2c79368ece367b 100644 (file)
@@ -1,3 +1,8 @@
+2019-02-26  Kevin Buettner  <kevinb@redhat.com>
+
+       * python/python-internal.h (Py_buffer_deleter): New struct.
+       (Py_buffer_up): New typedef.
+
 2019-02-25  John Baldwin  <jhb@FreeBSD.org>
 
        * dwarf2read.c (dwarf2_get_dwz_file): Reset dwz_bfd to nullptr
index 3cb9ebc1eecb4ccb10af791627edd6f4cb3ee022..d11af83c8e557a1c0fd347b955c0e12e1f3291a8 100644 (file)
@@ -801,4 +801,17 @@ struct varobj;
 struct varobj_iter *py_varobj_get_iterator (struct varobj *var,
                                            PyObject *printer);
 
+/* Deleter for Py_buffer unique_ptr specialization.  */
+
+struct Py_buffer_deleter
+{
+  void operator() (Py_buffer *b) const
+  {
+    PyBuffer_Release (b);
+  }
+};
+
+/* A unique_ptr specialization for Py_buffer.  */
+typedef std::unique_ptr<Py_buffer, Py_buffer_deleter> Py_buffer_up;
+
 #endif /* PYTHON_PYTHON_INTERNAL_H */