diff --git a/CHANGES.current b/CHANGES.current
index 85d1ffe54..1ced20389 100644
--- a/CHANGES.current
+++ b/CHANGES.current
@@ -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)
===========================
+2018-08-12: gmazzamuto
+ [Python] #1283 Update pybuffer.i library to use new-style Python buffer C API.
+
2018-08-12: brianhatwood,wsfulton
[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.
diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html
index 1dd84039a..a6f139e8b 100644
--- a/Doc/Manual/Python.html
+++ b/Doc/Manual/Python.html
@@ -6272,8 +6272,8 @@ For detailed usage of function annotation, see
-Buffer protocols were revised in Python 3. SWIG also gains a series of
-new typemaps to support buffer interfaces. These typemap macros are
+SWIG has a series of
+typemaps to support buffer interfaces. These typemap macros are
defined in pybuffer.i, which must be included in order to use them.
By using these typemaps, your wrapped function will be able to
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 parm and a
size size_parm. It is similar to
%pybuffer_mutable_binary, except the
-%pybuffer_binary an accept both mutable and immutable
+%pybuffer_binary can accept both mutable and immutable
buffers. As a result, the wrapped function should not modify the buffer.
diff --git a/Examples/test-suite/python_pybuf.i b/Examples/test-suite/python_pybuf.i
index 8e1302582..5bdc98920 100644
--- a/Examples/test-suite/python_pybuf.i
+++ b/Examples/test-suite/python_pybuf.i
@@ -1,6 +1,6 @@
%module python_pybuf
-%include
-%include
+%include
+%include
/*functions for the test case*/
%pybuffer_mutable_binary(char *buf1, int len);
%pybuffer_mutable_string(char *buf2);
diff --git a/Lib/python/pybuffer.i b/Lib/python/pybuffer.i
index f4e08716e..577eb69c8 100644
--- a/Lib/python/pybuffer.i
+++ b/Lib/python/pybuffer.i
@@ -15,16 +15,12 @@
%define %pybuffer_mutable_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) {
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;
res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE);
size = view.len;
buf = view.buf;
PyBuffer_Release(&view);
-%#endif
- if (res<0) {
+ if (res < 0) {
PyErr_Clear();
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
}
@@ -33,7 +29,7 @@
}
%enddef
-/* %pybuffer_mutable_string(TYPEMAP, SIZE)
+/* %pybuffer_mutable_string(TYPEMAP)
*
* Macro for functions accept mutable zero terminated string pointer.
* This can be used for both input and output. For example:
@@ -48,19 +44,14 @@
%define %pybuffer_mutable_string(TYPEMAP)
%typemap(in) (TYPEMAP) {
- int res; Py_ssize_t size = 0; void *buf = 0;
-%#if PY_VERSION_HEX < 0x03000000
- res = PyObject_AsWriteBuffer($input, &buf, &size);
-%#else
+ int res; void *buf = 0;
Py_buffer view;
res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE);
- size = view.len;
buf = view.buf;
PyBuffer_Release(&view);
-%#endif
- if (res<0) {
+ if (res < 0) {
PyErr_Clear();
- %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
+ %argument_fail(res, "(TYPEMAP)", $symname, $argnum);
}
$1 = ($1_ltype) buf;
}
@@ -83,16 +74,12 @@
%define %pybuffer_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) {
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;
res = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO);
size = view.len;
buf = view.buf;
PyBuffer_Release(&view);
-%#endif
- if (res<0) {
+ if (res < 0) {
PyErr_Clear();
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
}
@@ -101,7 +88,7 @@
}
%enddef
-/* %pybuffer_string(TYPEMAP, SIZE)
+/* %pybuffer_string(TYPEMAP)
*
* Macro for functions accept read only zero terminated string pointer.
* This can be used for input. For example:
@@ -118,18 +105,13 @@
%define %pybuffer_string(TYPEMAP)
%typemap(in) (TYPEMAP) {
- int res; Py_ssize_t size = 0; const void *buf = 0;
-%#if PY_VERSION_HEX < 0x03000000
- res = PyObject_AsReadBuffer($input, &buf, &size);
-%#else
+ int res; const void *buf = 0;
Py_buffer view;
res = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO);
- size = view.len;
buf = view.buf;
PyBuffer_Release(&view);
-%#endif
- if (res<0) {
- %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
+ if (res < 0) {
+ %argument_fail(res, "(TYPEMAP)", $symname, $argnum);
}
$1 = ($1_ltype) buf;
}