def test_normal_setstate(self): """ Validates that only existing setstate methods are called when there are no registered state functions in the class chain. """ # Validate that unpickling the first class gives us an instance of # the third class with the appropriate attribute values. It will have # the default Foo values (because there is no state function to move # them) and also the default Baz values (since they inherit the # trait defaults because nothing overwrote the values.) start = Foo() end = sweet_pickle.loads(sweet_pickle.dumps(start)) self.assertEqual(True, isinstance(end, Baz)) self._assertAttributes(end, 1, (False, 1, 1, 'foo')) self._assertAttributes(end, 2, None) self._assertAttributes(end, 3, (False, 3, 3, 'baz')) # Validate that unpickling the second class gives us an instance of # the third class with the appropriate attribute values. It will have # only the Baz attributes with the Bar values (since the __setstate__ # on Baz converted the Bar attributes to Baz attributes.) start = Bar() end = sweet_pickle.loads(sweet_pickle.dumps(start)) self.assertEqual(True, isinstance(end, Baz)) self._assertAttributes(end, 2, None) self._assertAttributes(end, 3, (True, 2, 2, 'bar'))
def test_generic(): print('\nRunning generic test...') a = A() b = B() a.x = random.randint(1, 100) b.set_a(a) a.set_b(b) value = a.x # This will fail, even though we have a __setstate__ method. s = pickle.dumps(a) new_a = pickle.loads(s) try: print('\ta.x: %s' % new_a.x) print('\ta.b_ref.y: %s' % new_a.b_ref.y) except Exception as msg: print('\t%s' % 'Expected Error'.center(75, '*')) print('\t%s' % msg) print('\t%s' % ('*' * 75)) # This will work! s = pickle.dumps(a) new_a = sweet_pickle.loads(s) assert new_a.x == new_a.b_ref.y == value print('Generic test succesfull.\n\n')
def test_unpickled_class_mapping(self): # Add the mappings to the registry self.registry.add_mapping_to_class(Foo.__module__, Foo.__name__, Bar) self.registry.add_mapping_to_class(Bar.__module__, Bar.__name__, Baz) # Validate that unpickling the first class gives us an instance of # the third class. start = Foo() end = sweet_pickle.loads(sweet_pickle.dumps(start)) self.assertEqual(True, isinstance(end, Baz)) # Validate that unpickling the second class gives us an instance of # the third class. start = Bar() end = sweet_pickle.loads(sweet_pickle.dumps(start)) self.assertEqual(True, isinstance(end, Baz))
def test_unpickled_chain_functionality(self): """ Validates that the registered state functions are used when unpickling. """ # Add the state function to the registry self.registry.add_state_function_for_class(Bar, 2, bar_state_function) # Validate that unpickling the first class gives us an instance of # the third class with the appropriate attribute values. start = Foo() end = sweet_pickle.loads(sweet_pickle.dumps(start)) self.assertEqual(True, isinstance(end, Baz)) self._assertAttributes(end, 1, None) self._assertAttributes(end, 2, None) self._assertAttributes(end, 3, (False, 1, 1, 'foo')) # Validate that unpickling the second class gives us an instance of # the third class. start = Bar() end = sweet_pickle.loads(sweet_pickle.dumps(start)) self.assertEqual(True, isinstance(end, Baz)) self._assertAttributes(end, 2, None) self._assertAttributes(end, 3, (True, 2, 2, 'bar'))
def test_toy_app(): print('\nRunning toy app test...') a = Application() a.finder.find() a.get() s = pickle.dumps(a) b = pickle.loads(s) # Won't work. try: b.get() except Exception as msg: print('\t%s' % 'Expected Error'.center(75, '*')) print('\t%s' % msg) print('\t%s' % ('*' * 75)) # Works fine. c = sweet_pickle.loads(s) c.get() print('Toy app test succesfull.\n\n')
value = a.x # This will fail, even though we have a __setstate__ method. s = pickle.dumps(a) new_a = pickle.loads(s) try: print '\ta.x: %s' % new_a.x print '\ta.b_ref.y: %s' % new_a.b_ref.y except Exception, msg: print '\t%s' % 'Expected Error'.center(75,'*') print '\t%s' % msg print '\t%s' % ('*'*75) # This will work! s = pickle.dumps(a) new_a = sweet_pickle.loads(s) assert new_a.x == new_a.b_ref.y == value print 'Generic test succesfull.\n\n' ######################################## # Usecase2: Toy Application import re class StringFinder(object): def __init__(self, source, pattern): self.pattern = pattern self.source = source self.data = [] def __getstate__(self):
def fn(o): sweet_pickle.loads(sweet_pickle.dumps(o))
value = a.x # This will fail, even though we have a __setstate__ method. s = pickle.dumps(a) new_a = pickle.loads(s) try: print '\ta.x: %s' % new_a.x print '\ta.b_ref.y: %s' % new_a.b_ref.y except Exception, msg: print '\t%s' % 'Expected Error'.center(75, '*') print '\t%s' % msg print '\t%s' % ('*' * 75) # This will work! s = pickle.dumps(a) new_a = sweet_pickle.loads(s) assert new_a.x == new_a.b_ref.y == value print 'Generic test succesfull.\n\n' ######################################## # Usecase2: Toy Application import re class StringFinder(object): def __init__(self, source, pattern): self.pattern = pattern self.source = source self.data = []