def handle_children_state(children, kids): """Given a list of children (as `children`) of a particular object and their states in the `kids` argument, this function sets up the children by removing unnecessary ones, fixing existing ones and adding new children if necessary (depending on the state). """ # Make a copy of the list so adding/removing does not trigger events # each time. m_children = list(children) n_child, n_kid = len(m_children), len(kids) # Remove extra children we have. for i in range(n_child - n_kid): m_children.pop() # Now check existing children deleting existing ones and # creating new ones if needed. for i in range(n_child): child, kid = m_children[i], kids[i] md = kid.__metadata__ if (child.__module__ != md['module']) \ or (child.__class__.__name__ != md['class_name']): m_children[i] = create_instance(kid) # Add any extra kids. for i in range(n_kid - n_child): child = create_instance(kids[n_child + i]) m_children.append(child) # Now set the children in one shot. children[:] = m_children
def test_update(self): """Test if update method calls the handlers in order.""" registry = version_registry.registry # First an elementary test. c = Classic() state = state_pickler.get_state(c) h = Handler() registry.register('Classic', __name__, h.upgrade) c1 = state_pickler.create_instance(state) state_pickler.set_state(c1, state) self.assertEqual(h.calls, [('upgrade', state, 0)]) # Remove the handler. registry.unregister('Classic', __name__) # Now check to see if this works for inheritance trees. t = Test() state = state_pickler.get_state(t) h = Handler() registry.register('Classic', __name__, h.upgrade) registry.register('New', __name__, h.upgrade) registry.register('Test', __name__, h.upgrade1) t1 = state_pickler.create_instance(state) state_pickler.set_state(t1, state) # This should call New handler, then the Test and then # Classic. self.assertEqual(h.calls, [('upgrade', state, 0), ('upgrade1', state, 1), ('upgrade', state.a, 0)])
def test_state_setter(self): """Test some of the features of the set_state method.""" t = TestClassic() self.set_object(t) # Get the saved state. res = state_pickler.get_state(t) # Now create a new instance and test the setter. t1 = state_pickler.create_instance(res) keys = ['c', 'b', 'f', 'i', 'tuple', 'list', 'l', 'numeric', 'n', 's', 'u', 'pure_list', 'inst', 'ref', 'dict'] ignore = list(keys) ignore.remove('b') first = ['b'] last = [] state_pickler.set_state(t1, res, ignore=ignore, first=first, last=last) # Only 'b' should have been set. self.assertEqual(t1.b, True) # Rest are unchanged. self.assertEqual(t1.i, 7) self.assertEqual(t1.s, 'String') self.assertEqual(t1.u, u'Unicode') self.assertEqual(t1.inst.a, 'a') self.assertEqual(t1.list[0], 1) self.assertEqual(t1.tuple[-1].a, 'a') self.assertEqual(t1.dict['a'], 1) # Check if last works. last = ignore ignore = [] first = [] state_pickler.set_state(t1, res, ignore=ignore, first=first, last=last) # Check everything. self.verify_unpickled(t1, res)
def test_state_setter_classic(self): """Test if classic classes' state can be set.""" t = TestClassic() self.set_object(t) # Get the pickled state. res = state_pickler.get_state(t) # Now create a new instance and set its state. t1 = state_pickler.create_instance(res) state_pickler.set_state(t1, res) # Check each attribute. self.verify_unpickled(t1, res)
def test_state_setter(self): """Test some of the features of the set_state method.""" t = TestClassic() self.set_object(t) # Get the saved state. res = state_pickler.get_state(t) # Now create a new instance and test the setter. t1 = state_pickler.create_instance(res) keys = [ "c", "b", "f", "i", "tuple", "list", "longi", "numeric", "n", "s", "u", "pure_list", "inst", "ref", "dict", ] ignore = list(keys) ignore.remove("b") first = ["b"] last = [] state_pickler.set_state(t1, res, ignore=ignore, first=first, last=last) # Only 'b' should have been set. self.assertTrue(t1.b) # Rest are unchanged. self.assertEqual(t1.i, 7) self.assertEqual(t1.s, "String") self.assertEqual(t1.u, "Unicode") self.assertEqual(t1.inst.a, "a") self.assertEqual(t1.list[0], 1) self.assertEqual(t1.tuple[-1].a, "a") self.assertEqual(t1.dict["a"], 1) # Check if last works. last = ignore ignore = [] first = [] state_pickler.set_state(t1, res, ignore=ignore, first=first, last=last) # Check everything. self.verify_unpickled(t1, res)
def test_state_setter(self): """Test some of the features of the set_state method.""" t = TestClassic() self.set_object(t) # Get the saved state. res = state_pickler.get_state(t) # Now create a new instance and test the setter. t1 = state_pickler.create_instance(res) keys = [ 'c', 'b', 'f', 'i', 'tuple', 'list', 'l', 'numeric', 'n', 's', 'u', 'pure_list', 'inst', 'ref', 'dict' ] ignore = list(keys) ignore.remove('b') first = ['b'] last = [] state_pickler.set_state(t1, res, ignore=ignore, first=first, last=last) # Only 'b' should have been set. self.assertEqual(t1.b, True) # Rest are unchanged. self.assertEqual(t1.i, 7) self.assertEqual(t1.s, 'String') self.assertEqual(t1.u, u'Unicode') self.assertEqual(t1.inst.a, 'a') self.assertEqual(t1.list[0], 1) self.assertEqual(t1.tuple[-1].a, 'a') self.assertEqual(t1.dict['a'], 1) # Check if last works. last = ignore ignore = [] first = [] state_pickler.set_state(t1, res, ignore=ignore, first=first, last=last) # Check everything. self.verify_unpickled(t1, res)