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

1
.gitignore vendored
View File

@@ -62,3 +62,4 @@ mydbstub.py*
ubuntu-xenial-16.04-cloudimg-console.log
ubuntu-bionic-18.04-cloudimg-console.log
.pytest_cache

View File

@@ -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()

View File

@@ -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)

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 @@
};
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------

View File

@@ -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):