Skip to content

Commit 1b723f2

Browse files
authored
Revert "[lldb] Limit Py_buffer_RAII to SWIG < 4.1" (#167934)
Reverts #167808
1 parent 833ffa5 commit 1b723f2

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H
2+
#define LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H
3+
4+
#include <Python.h>
5+
6+
// Defined here instead of a .swig file because SWIG 2 doesn't support
7+
// explicit deleted functions.
8+
struct Py_buffer_RAII {
9+
Py_buffer buffer = {};
10+
Py_buffer_RAII(){};
11+
Py_buffer &operator=(const Py_buffer_RAII &) = delete;
12+
Py_buffer_RAII(const Py_buffer_RAII &) = delete;
13+
~Py_buffer_RAII() {
14+
if (buffer.obj)
15+
PyBuffer_Release(&buffer);
16+
}
17+
};
18+
19+
#endif // LLDB_BINDINGS_PYTHON_PYTHON_TYPEMAPS_H

lldb/bindings/python/python-typemaps.swig

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ 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+
915
%typemap(in) char ** {
1016
/* Check if is a list */
1117
if (PythonList::Check($input)) {
@@ -628,21 +634,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
628634
}
629635
}
630636

631-
#if SWIG_VERSION < 0x040100
632-
// The two pybuffer macros below are copied out of swig/Lib/python/pybuffer.i,
637+
// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i,
633638
// and fixed so they will not crash if PyObject_GetBuffer fails.
634639
// https://github.com/swig/swig/issues/1640
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-
};
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.
646643

647644
%define %pybuffer_mutable_binary(TYPEMAP, SIZE)
648645
%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
@@ -677,9 +674,6 @@ struct Py_buffer_RAII {
677674
$2 = ($2_ltype)(size / sizeof($*1_type));
678675
}
679676
%enddef
680-
#else
681-
%include <pybuffer.i>
682-
#endif
683677

684678
%pybuffer_binary(const uint8_t *buf, size_t num_bytes);
685679
%pybuffer_mutable_binary(uint8_t *buf, size_t num_bytes);

0 commit comments

Comments
 (0)