Mike Bayer avatar Mike Bayer committed e20cade

- Allowed pickling of PropertyOption objects constructed with
instrumented descriptors; previously, pickle errors would occur
when pickling an object which was loaded with a descriptor-based
option, such as query.options(eagerload(MyClass.foo)).

Comments (0)

Files changed (3)

       be assigned to a pending parent instance, otherwise modified
       events would not be fired correctly.  Set collection is now
       compatible with merge(), fixes [ticket:1352].
-
+    
+    - Allowed pickling of PropertyOption objects constructed with
+      instrumented descriptors; previously, pickle errors would occur 
+      when pickling an object which was loaded with a descriptor-based 
+      option, such as query.options(eagerload(MyClass.foo)).
+      
     - Lazy loader will not use get() if the "lazy load" SQL clause
       matches the clause used by get(), but contains some parameters
       hardcoded.  Previously the lazy strategy would fail with the

lib/sqlalchemy/orm/interfaces.py

             else:
                 return None
 
+    def __getstate__(self):
+        d = self.__dict__.copy()
+        d['key'] = ret = []
+        for token in util.to_list(self.key):
+            if isinstance(token, PropComparator):
+                ret.append((token.mapper.class_, token.key))
+            else:
+                ret.append(token)
+        return d
+
+    def __setstate__(self, state):
+        ret = []
+        for key in state['key']:
+            if isinstance(key, tuple):
+                cls, propkey = key
+                ret.append(getattr(cls, propkey))
+            else:
+                ret.append(key)
+        state['key'] = tuple(ret)
+        self.__dict__ = state
+
     def __get_paths(self, query, raiseerr):
         path = None
         entity = None

test/orm/pickled.py

         self.assertEquals(ad.email_address, 'ed@bar.com')
         self.assertEquals(u2, User(name='ed', addresses=[Address(email_address='ed@bar.com')]))
 
+    @testing.resolve_artifact_names
+    def test_options_with_descriptors(self):
+        mapper(User, users, properties={
+            'addresses':relation(Address, backref="user")
+        })
+        mapper(Address, addresses)
+        sess = create_session()
+        u1 = User(name='ed')
+        u1.addresses.append(Address(email_address='ed@bar.com'))
+        sess.add(u1)
+        sess.flush()
+        sess.expunge_all()
 
+        for opt in [
+            sa.orm.eagerload(User.addresses),
+            sa.orm.eagerload("addresses"),
+            sa.orm.defer("name"),
+            sa.orm.defer(User.name),
+            sa.orm.defer([User.name]),
+            sa.orm.eagerload("addresses", User.addresses),
+            sa.orm.eagerload(["addresses", User.addresses]),
+        ]:
+            opt2 = pickle.loads(pickle.dumps(opt))
+            self.assertEquals(opt.key, opt2.key)
+        
+        u1 = sess.query(User).options(opt).first()
+        
+        u2 = pickle.loads(pickle.dumps(u1))
+        
+        
 class PolymorphicDeferredTest(_base.MappedTest):
     def define_tables(self, metadata):
         Table('users', metadata,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.