From 169d7d81fe2934476d954028896c84e54d00c582 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Wed, 12 Nov 2025 18:12:27 -0800 Subject: [PATCH 1/2] [lldb] Limit Py_buffer_RAII to SWIG < 4.1 The bug [1] this is working around was fixed in SWIG 4.1. The workaround uses functions and constants that are not part of the limited API, which I'm trying to eliminate to make LLDB compatible with the Python Limited C API [2]. [1] https://github.com/swig/swig/issues/1640 [2] https://github.com/llvm/llvm-project/issues/151617 --- lldb/bindings/python/python-typemaps.h | 2 ++ lldb/bindings/python/python-typemaps.swig | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lldb/bindings/python/python-typemaps.h b/lldb/bindings/python/python-typemaps.h index 8a533e822988e..366ae5e6a6676 100644 --- a/lldb/bindings/python/python-typemaps.h +++ b/lldb/bindings/python/python-typemaps.h @@ -3,6 +3,7 @@ #include +#if SWIG_VERSION < 0x040100 // Defined here instead of a .swig file because SWIG 2 doesn't support // explicit deleted functions. struct Py_buffer_RAII { @@ -15,5 +16,6 @@ struct Py_buffer_RAII { PyBuffer_Release(&buffer); } }; +#endif #endif // LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H diff --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig index 4d3a95768f2f3..f86502c99c5c7 100644 --- a/lldb/bindings/python/python-typemaps.swig +++ b/lldb/bindings/python/python-typemaps.swig @@ -634,13 +634,10 @@ template <> bool SetNumberFromPyObject(double &number, PyObject *obj) { } } +#if SWIG_VERSION < 0x040100 // 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 -// -// 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) { int res; @@ -674,6 +671,9 @@ template <> bool SetNumberFromPyObject(double &number, PyObject *obj) { $2 = ($2_ltype)(size / sizeof($*1_type)); } %enddef +#else +%include +#endif %pybuffer_binary(const uint8_t *buf, size_t num_bytes); %pybuffer_mutable_binary(uint8_t *buf, size_t num_bytes); From 0df6a185bada666202b515a25d5d0415b2e0dd8f Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Thu, 13 Nov 2025 10:45:21 -0800 Subject: [PATCH 2/2] Eliminate python-typemaps.h --- lldb/bindings/python/python-typemaps.h | 21 --------------------- lldb/bindings/python/python-typemaps.swig | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 28 deletions(-) delete mode 100644 lldb/bindings/python/python-typemaps.h diff --git a/lldb/bindings/python/python-typemaps.h b/lldb/bindings/python/python-typemaps.h deleted file mode 100644 index 366ae5e6a6676..0000000000000 --- a/lldb/bindings/python/python-typemaps.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H -#define LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H - -#include - -#if SWIG_VERSION < 0x040100 -// 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 - -#endif // LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H diff --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig index f86502c99c5c7..841162beb4e30 100644 --- a/lldb/bindings/python/python-typemaps.swig +++ b/lldb/bindings/python/python-typemaps.swig @@ -6,12 +6,6 @@ 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)) { @@ -635,9 +629,21 @@ template <> bool SetNumberFromPyObject(double &number, PyObject *obj) { } #if SWIG_VERSION < 0x040100 -// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i, +// The two pybuffer macros below 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); + } +}; + %define %pybuffer_mutable_binary(TYPEMAP, SIZE) %typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) { int res;