Update pybuffer.i library to use new-style Python buffer C API.

Removed old API altogether as Python 2.7 is now the minimum version
supported and it supports the new-style buffer API.
This commit is contained in:
William S Fulton 2018-08-12 15:02:28 +01:00
commit 4313c2c168
4 changed files with 18 additions and 33 deletions

View file

@ -7,6 +7,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.0.0 (in progress) Version 4.0.0 (in progress)
=========================== ===========================
2018-08-12: gmazzamuto
[Python] #1283 Update pybuffer.i library to use new-style Python buffer C API.
2018-08-12: brianhatwood,wsfulton 2018-08-12: brianhatwood,wsfulton
[Java] #1303 #1304 Fix crash in directors when using OUTPUT and INOUT typemaps in typemaps.i and [Java] #1303 #1304 Fix crash in directors when using OUTPUT and INOUT typemaps in typemaps.i and
passing NULL pointers in C++ to director method overloaded and implemented in Java. passing NULL pointers in C++ to director method overloaded and implemented in Java.

View file

@ -6272,8 +6272,8 @@ For detailed usage of function annotation, see
<p> <p>
Buffer protocols were revised in Python 3. SWIG also gains a series of SWIG has a series of
new typemaps to support buffer interfaces. These typemap macros are typemaps to support buffer interfaces. These typemap macros are
defined in <tt>pybuffer.i</tt>, which must be included in order to use them. defined in <tt>pybuffer.i</tt>, which must be included in order to use them.
By using these typemaps, your wrapped function will be able to By using these typemaps, your wrapped function will be able to
accept any Python object that exposes a suitable buffer interface. accept any Python object that exposes a suitable buffer interface.
@ -6398,7 +6398,7 @@ type.
This macro maps an object's buffer to a pointer <tt>parm</tt> and a This macro maps an object's buffer to a pointer <tt>parm</tt> and a
size <tt>size_parm</tt>. It is similar to size <tt>size_parm</tt>. It is similar to
<tt>%pybuffer_mutable_binary</tt>, except the <tt>%pybuffer_mutable_binary</tt>, except the
<tt>%pybuffer_binary</tt> an accept both mutable and immutable <tt>%pybuffer_binary</tt> can accept both mutable and immutable
buffers. As a result, the wrapped function should not modify the buffer. buffers. As a result, the wrapped function should not modify the buffer.
</p> </p>

View file

@ -1,6 +1,6 @@
%module python_pybuf %module python_pybuf
%include<pybuffer.i> %include <pybuffer.i>
%include<cstring.i> %include <cstring.i>
/*functions for the test case*/ /*functions for the test case*/
%pybuffer_mutable_binary(char *buf1, int len); %pybuffer_mutable_binary(char *buf1, int len);
%pybuffer_mutable_string(char *buf2); %pybuffer_mutable_string(char *buf2);

View file

@ -15,16 +15,12 @@
%define %pybuffer_mutable_binary(TYPEMAP, SIZE) %define %pybuffer_mutable_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) { %typemap(in) (TYPEMAP, SIZE) {
int res; Py_ssize_t size = 0; void *buf = 0; int res; Py_ssize_t size = 0; void *buf = 0;
%#if PY_VERSION_HEX < 0x03000000
res = PyObject_AsWriteBuffer($input, &buf, &size);
%#else
Py_buffer view; Py_buffer view;
res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE); res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE);
size = view.len; size = view.len;
buf = view.buf; buf = view.buf;
PyBuffer_Release(&view); PyBuffer_Release(&view);
%#endif if (res < 0) {
if (res<0) {
PyErr_Clear(); PyErr_Clear();
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
} }
@ -33,7 +29,7 @@
} }
%enddef %enddef
/* %pybuffer_mutable_string(TYPEMAP, SIZE) /* %pybuffer_mutable_string(TYPEMAP)
* *
* Macro for functions accept mutable zero terminated string pointer. * Macro for functions accept mutable zero terminated string pointer.
* This can be used for both input and output. For example: * This can be used for both input and output. For example:
@ -48,19 +44,14 @@
%define %pybuffer_mutable_string(TYPEMAP) %define %pybuffer_mutable_string(TYPEMAP)
%typemap(in) (TYPEMAP) { %typemap(in) (TYPEMAP) {
int res; Py_ssize_t size = 0; void *buf = 0; int res; void *buf = 0;
%#if PY_VERSION_HEX < 0x03000000
res = PyObject_AsWriteBuffer($input, &buf, &size);
%#else
Py_buffer view; Py_buffer view;
res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE); res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE);
size = view.len;
buf = view.buf; buf = view.buf;
PyBuffer_Release(&view); PyBuffer_Release(&view);
%#endif if (res < 0) {
if (res<0) {
PyErr_Clear(); PyErr_Clear();
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); %argument_fail(res, "(TYPEMAP)", $symname, $argnum);
} }
$1 = ($1_ltype) buf; $1 = ($1_ltype) buf;
} }
@ -83,16 +74,12 @@
%define %pybuffer_binary(TYPEMAP, SIZE) %define %pybuffer_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) { %typemap(in) (TYPEMAP, SIZE) {
int res; Py_ssize_t size = 0; const void *buf = 0; int res; Py_ssize_t size = 0; const void *buf = 0;
%#if PY_VERSION_HEX < 0x03000000
res = PyObject_AsReadBuffer($input, &buf, &size);
%#else
Py_buffer view; Py_buffer view;
res = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO); res = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO);
size = view.len; size = view.len;
buf = view.buf; buf = view.buf;
PyBuffer_Release(&view); PyBuffer_Release(&view);
%#endif if (res < 0) {
if (res<0) {
PyErr_Clear(); PyErr_Clear();
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
} }
@ -101,7 +88,7 @@
} }
%enddef %enddef
/* %pybuffer_string(TYPEMAP, SIZE) /* %pybuffer_string(TYPEMAP)
* *
* Macro for functions accept read only zero terminated string pointer. * Macro for functions accept read only zero terminated string pointer.
* This can be used for input. For example: * This can be used for input. For example:
@ -118,18 +105,13 @@
%define %pybuffer_string(TYPEMAP) %define %pybuffer_string(TYPEMAP)
%typemap(in) (TYPEMAP) { %typemap(in) (TYPEMAP) {
int res; Py_ssize_t size = 0; const void *buf = 0; int res; const void *buf = 0;
%#if PY_VERSION_HEX < 0x03000000
res = PyObject_AsReadBuffer($input, &buf, &size);
%#else
Py_buffer view; Py_buffer view;
res = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO); res = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO);
size = view.len;
buf = view.buf; buf = view.buf;
PyBuffer_Release(&view); PyBuffer_Release(&view);
%#endif if (res < 0) {
if (res<0) { %argument_fail(res, "(TYPEMAP)", $symname, $argnum);
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
} }
$1 = ($1_ltype) buf; $1 = ($1_ltype) buf;
} }