pybuffer: don't use the old deprecated buffer protocol for Python 3
This commit is contained in:
parent
5a8c4fde4c
commit
3594e54f7e
1 changed files with 40 additions and 8 deletions
|
|
@ -13,9 +13,17 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
%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);
|
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();
|
PyErr_Clear();
|
||||||
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
|
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
|
||||||
|
|
@ -39,9 +47,17 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
%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; Py_ssize_t size = 0; void *buf = 0;
|
||||||
|
%#if PY_VERSION_HEX < 0x03000000
|
||||||
res = PyObject_AsWriteBuffer($input, &buf, &size);
|
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();
|
PyErr_Clear();
|
||||||
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
|
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
|
||||||
|
|
@ -65,9 +81,17 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
%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);
|
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();
|
PyErr_Clear();
|
||||||
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
|
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
|
||||||
|
|
@ -93,9 +117,17 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
%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; Py_ssize_t size = 0; const void *buf = 0;
|
||||||
|
%#if PY_VERSION_HEX < 0x03000000
|
||||||
res = PyObject_AsReadBuffer($input, &buf, &size);
|
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) {
|
||||||
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
|
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue