Skip to content

Conversation

@JDevlieghere
Copy link
Member

Reverts #167808

@llvmbot llvmbot added the lldb label Nov 13, 2025
@JDevlieghere JDevlieghere merged commit 1b723f2 into main Nov 13, 2025
6 of 11 checks passed
@JDevlieghere JDevlieghere deleted the revert-167808-Py_buffer_RAII-workaround branch November 13, 2025 19:24
@llvmbot
Copy link
Member

llvmbot commented Nov 13, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

Reverts llvm/llvm-project#167808


Full diff: https://github.com/llvm/llvm-project/pull/167934.diff

2 Files Affected:

  • (added) lldb/bindings/python/python-typemaps.h (+19)
  • (modified) lldb/bindings/python/python-typemaps.swig (+10-16)
diff --git a/lldb/bindings/python/python-typemaps.h b/lldb/bindings/python/python-typemaps.h
new file mode 100644
index 0000000000000..8a533e822988e
--- /dev/null
+++ b/lldb/bindings/python/python-typemaps.h
@@ -0,0 +1,19 @@
+#ifndef LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H
+#define LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H
+
+#include <Python.h>
+
+// Defined here instead of a .swig file because SWIG 2 doesn't support
+// explicit deleted functions.
+struct Py_buffer_RAII {
+  Py_buffer buffer = {};
+  Py_buffer_RAII(){};
+  Py_buffer &operator=(const Py_buffer_RAII &) = delete;
+  Py_buffer_RAII(const Py_buffer_RAII &) = delete;
+  ~Py_buffer_RAII() {
+    if (buffer.obj)
+      PyBuffer_Release(&buffer);
+  }
+};
+
+#endif // LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H
diff --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig
index 841162beb4e30..4d3a95768f2f3 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -6,6 +6,12 @@ AND call SWIG_fail at the same time, because it will result in a double free.
 
 */
 
+%inline %{
+
+#include "../bindings/python/python-typemaps.h"
+
+%}
+
 %typemap(in) char ** {
   /* Check if is a list  */
   if (PythonList::Check($input)) {
@@ -628,21 +634,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
   }
 }
 
-#if SWIG_VERSION < 0x040100
-// The two pybuffer macros below are copied out of swig/Lib/python/pybuffer.i,
+// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i,
 // and fixed so they will not crash if PyObject_GetBuffer fails.
 // https://github.com/swig/swig/issues/1640
-
-struct Py_buffer_RAII {
-  Py_buffer buffer = {};
-  Py_buffer_RAII(){};
-  Py_buffer &operator=(const Py_buffer_RAII &) = delete;
-  Py_buffer_RAII(const Py_buffer_RAII &) = delete;
-  ~Py_buffer_RAII() {
-    if (buffer.obj)
-      PyBuffer_Release(&buffer);
-  }
-};
+//
+// I've also moved the call to PyBuffer_Release to the end of the SWIG wrapper,
+// doing it right away is not legal according to the python buffer protocol.
 
 %define %pybuffer_mutable_binary(TYPEMAP, SIZE)
 %typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
@@ -677,9 +674,6 @@ struct Py_buffer_RAII {
   $2 = ($2_ltype)(size / sizeof($*1_type));
 }
 %enddef
-#else
-%include <pybuffer.i>
-#endif
 
 %pybuffer_binary(const uint8_t *buf, size_t num_bytes);
 %pybuffer_mutable_binary(uint8_t *buf, size_t num_bytes);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants