Add context manager methods to wx.DC

This commit is contained in:
Robin Dunn
2018-01-29 12:23:57 -08:00
parent bcb3444f24
commit d330ad1b40
3 changed files with 22 additions and 0 deletions

View File

@@ -210,6 +210,12 @@ Changes in this release include the following:
point values to integers, and a couple other possible incorrect
conversions. (#536)
* Added context manager methods to wx.DC that explicitly destroys the C++
part of the DC upon exit. Using DCs as context managers is not required, but
can be handy in the rare cases where something holds on to a DC for too
long, perhaps unintentionally. (#680)

View File

@@ -264,6 +264,10 @@ def run():
c.addPyCode('DC.GetCGContext = wx.deprecated(DC.GetCGContext, "Use GetHandle instead.")')
c.addPyCode('DC.GetGdkDrawable = wx.deprecated(DC.GetGdkDrawable, "Use GetHandle instead.")')
# context manager methods
c.addPyMethod('__enter__', '(self)', 'return self')
c.addPyMethod('__exit__', '(self, exc_type, exc_val, exc_tb)', 'self.Destroy()')
# This file contains implementations of functions for quickly drawing
# lists of items on the DC. They are called from the CppMethods defined

View File

@@ -175,6 +175,18 @@ class dc_Tests(wtc.WidgetTestCase):
dc.DrawLines( [(15,15), (35,15), (35,35), (35,15), (15,15)] )
def test_dcContextManager(self):
import wx.siplib
with wx.ClientDC(self.frame) as dc:
dc.DrawLine(0,0,100,100)
# check ownership
assert wx.siplib.ispyowned(dc)
# check the DC's ownership has changed
assert not wx.siplib.ispyowned(dc)
#---------------------------------------------------------------------------