1. Daniel Miller
  2. sqlalchemy

Commits

Mike Bayer  committed aaaf7ea

added teardown_instance() to complement setup_instance().
Based on the instance/class agnostic behavior of ClassManager, this might be the best we can
do regarding [ticket:860]

  • Participants
  • Parent commits ed43ab8
  • Branches default

Comments (0)

Files changed (3)

File lib/sqlalchemy/orm/attributes.py

View file
 
     def setup_instance(self, instance, state=None):
         setattr(instance, self.STATE_ATTR, state or self.instance_state_factory(instance, self))
+    
+    def teardown_instance(self, instance):
+        delattr(instance, self.STATE_ATTR)
         
     def _new_state_if_none(self, instance):
         """Install a default InstanceState if none is present.
         state.dict = self._adapted.get_instance_dict(self.class_, instance)
         return state
 
+    def teardown_instance(self, instance):
+        self._adapted.remove_state(self.class_, instance)
+
     def state_of(self, instance):
         if hasattr(self._adapted, 'state_of'):
             return self._adapted.state_of(self.class_, instance)

File lib/sqlalchemy/orm/interfaces.py

View file
     def install_state(self, class_, instance, state):
         setattr(instance, '_default_state', state)
 
+    def remove_state(self, class_, instance):
+        delattr(instance, '_default_state', state)
+
     def state_getter(self, class_):
         return lambda instance: getattr(instance, '_default_state')

File test/orm/pickled.py

View file
 import pickle
 from testlib import sa, testing
 from testlib.sa import Table, Column, Integer, String, ForeignKey
-from testlib.sa.orm import mapper, relation, create_session
+from testlib.sa.orm import mapper, relation, create_session, attributes
 from orm import _base, _fixtures
 
 
         assert 'email_address' not in eu2.__dict__
         self.assertEquals(eu2.email_address, 'foo@bar.com')
 
+class CustomSetupTeardowntest(_fixtures.FixtureTest):
+    @testing.resolve_artifact_names
+    def test_rebuild_state(self):
+        """not much of a 'test', but illustrate how to 
+        remove instance-level state before pickling.
+        
+        """
+        mapper(User, users)
 
+        u1 = User()
+        attributes.manager_of_class(User).teardown_instance(u1)
+        assert not u1.__dict__
+        u2 = pickle.loads(pickle.dumps(u1))
+        attributes.manager_of_class(User).setup_instance(u2)
+        assert attributes.instance_state(u2)
+    
 if __name__ == '__main__':
     testenv.main()