Merge branch 'this-n-that' into wxPy-4.0.x

(cherry picked from commit 2e5430d57f)
This commit is contained in:
Robin Dunn
2018-05-07 11:22:08 -07:00
parent e64deef9d2
commit 09de554e1b
6 changed files with 64 additions and 16 deletions

View File

@@ -14,6 +14,10 @@
#ifndef WXPYBUFFER_H
#define WXPYBUFFER_H
// TODO: When support for Python2 is dropped then look into changing this
// class such that it holds on to the Py_buffer view object for the lifetime of
// the wxPyBuffer object. This could help with cases where the buffer is
// attempted to be accessed when the source object has already been gc'd.
class wxPyBuffer
{
@@ -24,8 +28,20 @@ public:
// the Python buffer protocol. Raises a TypeError if the object can not
// be used as a buffer.
bool create(PyObject* obj) {
int rv = PyObject_AsReadBuffer(obj, (const void**)&m_ptr, &m_len);
return rv != -1;
#if PY_MAJOR_VERSION < 3
// Old buffer protocol
int rv = PyObject_AsReadBuffer(obj, (const void**)&m_ptr, &m_len);
return rv != -1;
#else
// New buffer protocol
Py_buffer view;
if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
return false;
m_ptr = view.buf;
m_len = view.len;
PyBuffer_Release(&view);
return true;
#endif
}

View File

@@ -24,7 +24,10 @@
// Code to test a PyObject for compatibility
if (!sipIsErr) {
if (PyObject_CheckBuffer(sipPy) // New buffer interface
|| PyObject_CheckReadBuffer(sipPy)) // or old buffer interface
#if PY_MAJOR_VERSION < 3
|| PyObject_CheckReadBuffer(sipPy) // or old buffer interface
#endif
)
return TRUE;
return FALSE;
}
@@ -110,4 +113,4 @@
};
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------