Instead of using copy.copy to implement Clone, create the new instance with __new__

This commit is contained in:
Robin Dunn
2017-01-27 20:44:09 -08:00
parent 968784d63d
commit 360e3e6b2f
2 changed files with 20 additions and 30 deletions

View File

@@ -11,6 +11,8 @@ import etgtools
import etgtools.tweaker_tools as tools
from etgtools.extractors import ClassDef, MethodDef, ParamDef
import copy
PACKAGE = "wx"
MODULE = "_core"
NAME = "pyevent" # Base name of the file to generate to for this script
@@ -73,25 +75,14 @@ def run():
Through the magic of Python this implementation should work for
this and all derived classes.""",
body="""\
# Create a new instance
import copy
clone = copy.copy(self)
# and then invoke the C++ copy constructor to copy the C++ bits too.
# Create a new instance of the same type as this instance and
# then invoke the C++ copy constructor to copy the C++ parts and
# any custom attributes.
clone = wx.PyEvent.__new__(self.__class__)
wx.PyEvent.__init__(clone, self)
return clone
""")
cls.addPyMethod("__setstate__", "(self, state)",
doc = """Sets internal state of PyEvent object. This allows PyEvent to be reconstructed as part of the pickling process""",
body = """\
self.__dict__.update(state)
""")
cls.addPyMethod("__getstate__", "(self)",
doc = "Returns this object's internal state. This is used as part of the pickling process",
body = """\
return self.__dict__
""")
module.addItem(cls)
cls.addCppCode("IMPLEMENT_DYNAMIC_CLASS(wxPyEvent, wxEvent);")
@@ -142,25 +133,14 @@ def run():
Through the magic of Python this implementation should work for
this and all derived classes.""",
body="""\
# Create a new instance
import copy
clone = copy.copy(self)
# and then invoke the C++ copy constructor to copy the C++ bits too.
# Create a new instance of the same type as this instance and
# then invoke the C++ copy constructor to copy the C++ parts and
# any custom attributes.
clone = wx.PyCommandEvent.__new__(self.__class__)
wx.PyCommandEvent.__init__(clone, self)
return clone
""")
cls.addPyMethod("__setstate__", "(self, state)",
doc = """Sets internal state of PyEvent object. This allows PyEvent to be reconstructed as part of the pickling process""",
body = """\
self.__dict__.update(state)
""")
cls.addPyMethod("__getstate__", "(self)",
doc = "Returns this object's internal state. This is used as part of the pickling process",
body = """\
return self.__dict__
""")
module.addItem(cls)
cls.addCppCode("IMPLEMENT_DYNAMIC_CLASS(wxPyCommandEvent, wxCommandEvent);")

View File

@@ -60,6 +60,16 @@ class PyEvents(unittest.TestCase):
self.assertTrue(evt1.attr == evt2.attr)
def test_CppCloneDerived(self):
# test what happens when Clone is called from C++
if hasattr(wx, 'testCppClone'):
evt1 = MyPyEvent()
evt1.attr = 'testCppClone'
evt2 = wx.testCppClone(evt1)
self.assertTrue(evt1.attr == evt2.attr)
self.assertTrue(isinstance(evt2, MyPyEvent))
@unittest.skip('not testing refcounts for now, needs checking...')
def test_CppCloneRefCounts(self):
# Since we're doing some funky stuff under the covers with Clone, make