Add __index__ to wx.WindowID, and __bool__ to wx.Region

(cherry picked from commit 9096426603)
This commit is contained in:
Robin Dunn
2020-02-26 11:10:27 -08:00
committed by Scott Talbert
parent 7c43dd1ab6
commit d453e92a73
3 changed files with 41 additions and 19 deletions

View File

@@ -88,11 +88,12 @@ def run():
c.mustHaveApp()
c.find('operator++').ignore()
# SIP maps operator bool() to __int__, but Classic used __nonzero__. Does
# it make any difference either way?
c.find('operator bool').ignore()
c.addCppMethod('int', '__nonzero__', '()', 'return (int)self->operator bool();',
'Returns true while there are still rectangles available in the iteration.')
c.addCppMethod('int', '__bool__', '()', 'return (int)self->operator bool();',
'Returns true while there are still rectangles available in the iteration.')
c.addCppMethod('void', 'Next', '()', 'self->operator++();',
'Move the iterator to the next rectangle in the region.')

View File

@@ -73,10 +73,12 @@ def run():
""")
klass.addCppMethod('int', '__int__', '()',
doc="Alias for GetValue allowing the IDRef to be passed as the WindowID parameter when creating widgets or etc.",
body="""\
return self->GetValue();
""")
doc="Alias for GetValue allowing the IDRef to be passed as the WindowID parameter when creating widgets or other places an integer type is needed.",
body="return self->GetValue();")
klass.addCppMethod('int', '__index__', '()',
doc="See :meth:`__int__`",
body="return self->GetValue();")
klass.addCppMethod('bool', '__eq__', '(wxWindowID id)', "return self->GetValue() == id;")
klass.addCppMethod('bool', '__ne__', '(wxWindowID id)', "return self->GetValue() != id;")

View File

@@ -75,6 +75,7 @@ class IdManagerTest(wtc.WidgetTestCase):
val = ref1 <= ref2
assert type(val) == bool
def test_WindowIDRef02(self):
d = {wx.NewIdRef(): 'one',
wx.NewIdRef(): 'two'}
@@ -82,6 +83,24 @@ class IdManagerTest(wtc.WidgetTestCase):
for k in keys:
val = d[k]
def test_WindowIDRef03(self):
# Ensure wx.WindowIDRef can be converted to int without warning when
# making a call to warrped method. In Py3.8+ this means there needs to
# be an __index__ method.
# Turn warnings into exceptions so this test will fail if there is
# a warning
import warnings
warnings.simplefilter('error')
wid = wx.NewIdRef()
assert isinstance(wid, wx.WindowIDRef)
b = wx.Button(self.frame, wid, 'button')
assert b.GetId() == wid.GetId()
#---------------------------------------------------------------------------