From 5882e525e3cb2800944ac3c5ea0587b926e3e19e Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sun, 18 May 2014 03:25:31 +0000 Subject: [PATCH] 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 --- etg/clipbrd.py | 12 +++++++++--- unittests/test_clipbrd.py | 18 ++++++++++++++++++ unittests/test_dataobj.py | 22 ++++++++++++++++++++-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/etg/clipbrd.py b/etg/clipbrd.py index b6241a2a..02f84618 100644 --- a/etg/clipbrd.py +++ b/etg/clipbrd.py @@ -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) """) diff --git a/unittests/test_clipbrd.py b/unittests/test_clipbrd.py index e631f12f..83d41315 100644 --- a/unittests/test_clipbrd.py +++ b/unittests/test_clipbrd.py @@ -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.') #--------------------------------------------------------------------------- diff --git a/unittests/test_dataobj.py b/unittests/test_dataobj.py index 9940546e..21a776c1 100644 --- a/unittests/test_dataobj.py +++ b/unittests/test_dataobj.py @@ -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) + #---------------------------------------------------------------------------