Fix context manager support for wx.TheClipboard.

Add some more clipboard tests.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@76572 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2014-05-18 03:25:31 +00:00
parent 2c72085b43
commit 5882e525e3
3 changed files with 47 additions and 5 deletions

View File

@@ -37,9 +37,6 @@ def run():
c.find('AddData.data').transfer = True
c.find('SetData.data').transfer = True
# context manager methods
c.addPyMethod('__enter__', '(self)', 'return self')
c.addPyMethod('__exit__', '(self, exc_type, exc_val, exc_tb)', 'self.Close()')
# TODO: This init wrapper class may be useful elsewhere...
module.addPyCode("""\
@@ -66,6 +63,15 @@ def run():
self._checkInstance()
return repr(self._instance)
# context manager methods
def __enter__(self):
self._checkInstance()
if not self.Open():
raise RuntimeError('Unable to open clipboard.')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.Close()
TheClipboard = _wxPyDelayedInitWrapper(Clipboard.Get)
""")

View File

@@ -20,6 +20,24 @@ class clipbrd_Tests(wtc.WidgetTestCase):
wx.TheClipboard.Close()
self.assertEqual(data2.GetText(), 'This is some data.')
self.assertEqual(data2.Text, 'This is some data.')
def test_clpbrd2(self):
# same, but with the context manager
# copy
data1 = wx.TextDataObject('This is some data.')
with wx.TheClipboard as cb:
cb.SetData(data1)
# paste
data2 = wx.TextDataObject()
with wx.TheClipboard as cb:
wx.TheClipboard.GetData(data2)
self.assertEqual(data2.GetText(), 'This is some data.')
self.assertEqual(data2.Text, 'This is some data.')
#---------------------------------------------------------------------------

View File

@@ -63,7 +63,7 @@ class DataObjTests(wtc.WidgetTestCase):
data._testGetAllFormats()
# TODO: Get this fixed! See https://groups.google.com/d/topic/wx-dev/wFxevpvbhvQ/discussion
@unittest.skipIf(sys.platform == 'darwin', 'Using wx.DF_TEXT currently fails on Mac')
#@unittest.skipIf(sys.platform == 'darwin', 'Using wx.DF_TEXT currently fails on Mac')
def test_DataObject(self):
class MyDataObject(wx.DataObject):
def __init__(self, value=''):
@@ -159,7 +159,7 @@ class DataObjTests(wtc.WidgetTestCase):
# TODO: Get this fixed! See https://groups.google.com/d/topic/wx-dev/wFxevpvbhvQ/discussion
@unittest.skipIf(sys.platform == 'darwin', 'Using wx.DF_TEXT currently fails on Mac')
#@unittest.skipIf(sys.platform == 'darwin', 'Using wx.DF_TEXT currently fails on Mac')
def test_DataObjectSimple2(self):
class MyDataObject(wx.DataObjectSimple):
def __init__(self, value=''):
@@ -275,6 +275,23 @@ class DataObjTests(wtc.WidgetTestCase):
self.assertAlmostEqual(do.TextLength, len(data), delta=1)
def test_TextDataObjectClipboard(self):
# copy
text = 'This is some MORE data.'
data1 = wx.TextDataObject(text)
if wx.TheClipboard.Open():
wx.TheClipboard.SetData(data1)
wx.TheClipboard.Close()
# paste
data2 = wx.TextDataObject()
if wx.TheClipboard.Open():
wx.TheClipboard.GetData(data2)
wx.TheClipboard.Close()
self.assertEqual(text, data2.Text)
def test_URLDataObject(self):
url = 'http://wxPython.org/'
do = wx.URLDataObject()
@@ -301,6 +318,7 @@ class DataObjTests(wtc.WidgetTestCase):
self.assertEqual(do.GetHTML(), data)
self.assertEqual(do.HTML, data)
#---------------------------------------------------------------------------