[lldb] Limit Py_buffer_RAII to SWIG < 4.1 (#167808)

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
This commit is contained in:
Jonas Devlieghere
2025-11-13 11:07:21 -08:00
committed by GitHub
parent 1a86f0aae7
commit ac2d3d1da6
2 changed files with 16 additions and 29 deletions

View File

@@ -1,19 +0,0 @@
#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

View File

@@ -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)) {
@@ -634,12 +628,21 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
}
}
// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i,
#if SWIG_VERSION < 0x040100
// 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
//
// 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.
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) {
@@ -674,6 +677,9 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
$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);