From a5eeca0a5c425bd17f1d4dbf0633e1a4311cda71 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Wed, 27 Sep 2017 19:17:19 -0700 Subject: [PATCH] Return None when trying to make a memoryview buffer with a NULL pointer --- src/wxpy_api.sip | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/wxpy_api.sip b/src/wxpy_api.sip index d97d7861..fdc14ff7 100644 --- a/src/wxpy_api.sip +++ b/src/wxpy_api.sip @@ -453,12 +453,17 @@ bool i_wxPyCheckForApp(bool raiseException) { PyObject* i_wxPyMakeBuffer(void* ptr, Py_ssize_t len, bool readOnly=false) { // GIL should already be held - Py_buffer view; - int flags = PyBUF_FORMAT|PyBUF_ND; - if (!readOnly) - flags |= PyBUF_WRITABLE; - PyBuffer_FillInfo(&view, NULL, ptr, len, readOnly ? 1:0, flags); - return PyMemoryView_FromBuffer(&view); + if (ptr && len) { + Py_buffer view; + int flags = PyBUF_FORMAT|PyBUF_ND; + if (!readOnly) + flags |= PyBUF_WRITABLE; + PyBuffer_FillInfo(&view, NULL, ptr, len, readOnly ? 1:0, flags); + return PyMemoryView_FromBuffer(&view); + } else { + Py_INCREF(Py_None); return Py_None; + // return PyBytes_FromString(""); TODO: None or an empty string? + } // // TODO: Consider using a sip.array object instead, like this: // // Create a sip.array of bytes, and then convert to a memoryview which is