Skip to content

Commit ac2d3d1

Browse files
authored
[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] swig/swig#1640 [2] #151617
1 parent 1a86f0a commit ac2d3d1

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

lldb/bindings/python/python-typemaps.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

lldb/bindings/python/python-typemaps.swig

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ AND call SWIG_fail at the same time, because it will result in a double free.
66
77
*/
88

9-
%inline %{
10-
11-
#include "../bindings/python/python-typemaps.h"
12-
13-
%}
14-
159
%typemap(in) char ** {
1610
/* Check if is a list */
1711
if (PythonList::Check($input)) {
@@ -634,12 +628,21 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
634628
}
635629
}
636630

637-
// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i,
631+
#if SWIG_VERSION < 0x040100
632+
// The two pybuffer macros below are copied out of swig/Lib/python/pybuffer.i,
638633
// and fixed so they will not crash if PyObject_GetBuffer fails.
639634
// https://github.com/swig/swig/issues/1640
640-
//
641-
// I've also moved the call to PyBuffer_Release to the end of the SWIG wrapper,
642-
// doing it right away is not legal according to the python buffer protocol.
635+
636+
struct Py_buffer_RAII {
637+
Py_buffer buffer = {};
638+
Py_buffer_RAII(){};
639+
Py_buffer &operator=(const Py_buffer_RAII &) = delete;
640+
Py_buffer_RAII(const Py_buffer_RAII &) = delete;
641+
~Py_buffer_RAII() {
642+
if (buffer.obj)
643+
PyBuffer_Release(&buffer);
644+
}
645+
};
643646

644647
%define %pybuffer_mutable_binary(TYPEMAP, SIZE)
645648
%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
@@ -674,6 +677,9 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
674677
$2 = ($2_ltype)(size / sizeof($*1_type));
675678
}
676679
%enddef
680+
#else
681+
%include <pybuffer.i>
682+
#endif
677683

678684
%pybuffer_binary(const uint8_t *buf, size_t num_bytes);
679685
%pybuffer_mutable_binary(uint8_t *buf, size_t num_bytes);

0 commit comments

Comments
 (0)