From 5a958ff03b3dfe3adecf8dda0084cbda3851be82 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sat, 27 Apr 2013 02:49:08 +0000 Subject: [PATCH] Small tweak and tests to allow using objects supporting only the old buffer interface to be used as buffer sources. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@73872 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/MigrationGuide.txt | 3 +-- src/wxpybuffer.h | 1 - src/wxpybuffer.sip | 3 ++- unittests/test_bitmap.py | 25 +++++++++++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/docs/MigrationGuide.txt b/docs/MigrationGuide.txt index df593e44..2c07f9b4 100644 --- a/docs/MigrationGuide.txt +++ b/docs/MigrationGuide.txt @@ -290,8 +290,7 @@ methods which accept any object supporting the buffer protocol. These are methods which allow you to set the raw RGB or Alpha data in the image in one step. As a consequence of using the new APIs the objects passed must also implement the new buffer interface in order to be -compatible. Apparently arrays from the stock array module do not -support the new protocol, but everything else I've tried so far do. +compatible. GetData and GetAlpha now return a copy of the image data as a bytearray object instead of a string object. This means that since diff --git a/src/wxpybuffer.h b/src/wxpybuffer.h index e69ad84e..3d2899f0 100644 --- a/src/wxpybuffer.h +++ b/src/wxpybuffer.h @@ -52,7 +52,6 @@ public: return ptr; } - void* m_ptr; Py_ssize_t m_len; }; diff --git a/src/wxpybuffer.sip b/src/wxpybuffer.sip index 8a82ccc1..53c90cb7 100644 --- a/src/wxpybuffer.sip +++ b/src/wxpybuffer.sip @@ -23,7 +23,8 @@ %ConvertToTypeCode // Code to test a PyObject for compatibility if (!sipIsErr) { - if (PyObject_CheckBuffer(sipPy)) + if (PyObject_CheckBuffer(sipPy) // New buffer interface + || PyObject_CheckReadBuffer(sipPy)) // or old buffer interface return TRUE; return FALSE; } diff --git a/unittests/test_bitmap.py b/unittests/test_bitmap.py index e6700046..0bc5cb59 100644 --- a/unittests/test_bitmap.py +++ b/unittests/test_bitmap.py @@ -174,6 +174,31 @@ class BitmapTests(wtc.WidgetTestCase): self.assertTrue(bmp.IsOk()) + def test_bitmapBufferFactory4(self): + # The array.array object does not support the new buffer protocol, + # (at least as of 2.7) so this test shows if we can support the old + # one too. + import array + w = h = 10 + buf = array.array('B', [123] * (w*h*4)) + bmp = wx.Bitmap.FromBufferRGBA(w, h, buf) + self.assertTrue(bmp.IsOk()) + + + def test_bitmapBufferFactory5(self): + # The array.array object does not support the new buffer protocol, + # (at least as of 2.7) so this test shows if we can support the old + # one too. + import array + w = h = 10 + buf = array.array('B', [10, 20, 30] * (w*h)) + bmp = wx.Bitmap.FromBuffer(w, h, buf) + self.assertTrue(bmp.IsOk()) + img = bmp.ConvertToImage() + self.assertEqual( (img.GetRed(1,2), img.GetGreen(1,2), img.GetBlue(1,2)), + (10,20,30) ) + + def test_bitmapEmptyFactory1(self): w = h = 10 bmp = wx.Bitmap.FromRGBA(w, h)