From 09de554e1b38e7968bb14dc85bfcd9560fdf21f5 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Mon, 7 May 2018 11:22:08 -0700 Subject: [PATCH] Merge branch 'this-n-that' into wxPy-4.0.x (cherry picked from commit 2e5430d57f9ff0bfe195e251f3c27712e27be2bd) --- .gitignore | 1 + demo/__main__.py | 6 ++++++ etg/_msw.py | 5 +++++ src/wxpybuffer.h | 20 ++++++++++++++++++-- src/wxpybuffer.sip | 7 +++++-- unittests/test_image.py | 41 +++++++++++++++++++++++++++++------------ 6 files changed, 64 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 3b255eb1..b690e33c 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ mydbstub.py* ubuntu-xenial-16.04-cloudimg-console.log ubuntu-bionic-18.04-cloudimg-console.log +.pytest_cache diff --git a/demo/__main__.py b/demo/__main__.py index 99f75a1a..bb780ae2 100755 --- a/demo/__main__.py +++ b/demo/__main__.py @@ -1,4 +1,10 @@ #!/usr/bin/env python +import sys +import os import Main + +demoDir = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, demoDir) +os.chdir(demoDir) Main.main() diff --git a/etg/_msw.py b/etg/_msw.py index 5681b02a..20d3ff3a 100644 --- a/etg/_msw.py +++ b/etg/_msw.py @@ -58,6 +58,11 @@ def run(): module.addPyCode('import wx', order=10) module.addInclude(INCLUDES) + module.addPyCode("""\ + Metafile = wx.deprecated(wx.Metafile, 'Metafile has moved to the core wx module.') + MetafileDC = wx.deprecated(wx.MetafileDC, 'MetafileDC has moved to the core wx module.') + """) + # ----------------------------------------------------------------- # ----------------------------------------------------------------- tools.doCommonTweaks(module) diff --git a/src/wxpybuffer.h b/src/wxpybuffer.h index 973ed910..1b119920 100644 --- a/src/wxpybuffer.h +++ b/src/wxpybuffer.h @@ -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 } diff --git a/src/wxpybuffer.sip b/src/wxpybuffer.sip index 1e571b57..0a47621d 100644 --- a/src/wxpybuffer.sip +++ b/src/wxpybuffer.sip @@ -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 @@ }; -//-------------------------------------------------------------------------- \ No newline at end of file +//-------------------------------------------------------------------------- diff --git a/unittests/test_image.py b/unittests/test_image.py index f06f5bac..767b36a7 100644 --- a/unittests/test_image.py +++ b/unittests/test_image.py @@ -136,12 +136,21 @@ class image_Tests(wtc.WidgetTestCase): self.assertTrue(img.IsOk()) data = img.GetDataBuffer() self.assertTrue(isinstance(data, memoryview)) - data[0] = 1 - data[1] = 2 - data[2] = 3 - self.assertEqual(1, img.GetRed(0,0)) - self.assertEqual(2, img.GetGreen(0,0)) - self.assertEqual(3, img.GetBlue(0,0)) + if six.PY2: + data[0] = b'1' + data[1] = b'2' + data[2] = b'3' + self.assertEqual(ord('1'), img.GetRed(0,0)) + self.assertEqual(ord('2'), img.GetGreen(0,0)) + self.assertEqual(ord('3'), img.GetBlue(0,0)) + else: + data[0] = 1 + data[1] = 2 + data[2] = 3 + self.assertEqual(1, img.GetRed(0,0)) + self.assertEqual(2, img.GetGreen(0,0)) + self.assertEqual(3, img.GetBlue(0,0)) + def test_imageGetAlphaDataBuffer(self): w = h = 10 @@ -150,12 +159,20 @@ class image_Tests(wtc.WidgetTestCase): self.assertTrue(img.IsOk()) data = img.GetAlphaBuffer() self.assertTrue(isinstance(data, memoryview)) - data[0] = 1 - data[1] = 2 - data[2] = 3 - self.assertEqual(1, img.GetAlpha(0,0)) - self.assertEqual(2, img.GetAlpha(1,0)) - self.assertEqual(3, img.GetAlpha(2,0)) + if six.PY2: + data[0] = b'1' + data[1] = b'2' + data[2] = b'3' + self.assertEqual(ord('1'), img.GetAlpha(0,0)) + self.assertEqual(ord('2'), img.GetAlpha(1,0)) + self.assertEqual(ord('3'), img.GetAlpha(2,0)) + else: + data[0] = 1 + data[1] = 2 + data[2] = 3 + self.assertEqual(1, img.GetAlpha(0,0)) + self.assertEqual(2, img.GetAlpha(1,0)) + self.assertEqual(3, img.GetAlpha(2,0)) def test_imageSetDataBuffer1(self):