mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2026-01-04 19:10:09 +01:00
Add context manager methods to wx.DC
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user