]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
merged r6340 of trunk, for [ticket:1526]
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 12 Sep 2009 20:03:08 +0000 (20:03 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 12 Sep 2009 20:03:08 +0000 (20:03 +0000)
CHANGES
lib/sqlalchemy/orm/attributes.py
test/orm/test_attributes.py

diff --git a/CHANGES b/CHANGES
index fd04e91d2123bc35d4fd0de4b807cd4773009293..2ed260a4c10868befba3567953ce6cf032f442c9 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -13,7 +13,12 @@ CHANGES
     - Fixed bug which disallowed one side of a many-to-many 
       bidirectional reference to declare itself as "viewonly"
       [ticket:1507]
-
+    
+    - Added an assertion that prevents a @validates function
+      or other AttributeExtension from loading an unloaded
+      collection such that internal state may be corrupted.
+      [ticket:1526]
+      
     - Fixed bug which prevented two entities from mutually
       replacing each other's primary key values within a single
       flush() for some orderings of operations.  [ticket:1519]
index 46e9b00de2bbc389e87a1f7a0b552082a478aa49..b4463972d9451dccbfa4c12c096593b199c29646 100644 (file)
@@ -707,6 +707,7 @@ class CollectionAttributeImpl(AttributeImpl):
         collection = self.get_collection(state, dict_, passive=passive)
         if collection is PASSIVE_NORESULT:
             value = self.fire_append_event(state, dict_, value, initiator)
+            assert self.key not in dict_, "Collection was loaded during event handling."
             state.get_pending(self.key).append(value)
         else:
             collection.append_with_event(value, initiator)
@@ -718,6 +719,7 @@ class CollectionAttributeImpl(AttributeImpl):
         collection = self.get_collection(state, state.dict, passive=passive)
         if collection is PASSIVE_NORESULT:
             self.fire_remove_event(state, dict_, value, initiator)
+            assert self.key not in dict_, "Collection was loaded during event handling."
             state.get_pending(self.key).remove(value)
         else:
             collection.remove_with_event(value, initiator)
index ca8cef3ad85a72a77c2ddc5d760e2c53d0b77a1b..1d92c3fb2743ec24af3918af568cb0ee42867086 100644 (file)
@@ -4,7 +4,7 @@ from sqlalchemy.orm.collections import collection
 from sqlalchemy.orm.interfaces import AttributeExtension
 from sqlalchemy import exc as sa_exc
 from sqlalchemy.test import *
-from sqlalchemy.test.testing import eq_
+from sqlalchemy.test.testing import eq_, assert_raises
 from test.orm import _base
 import gc
 
@@ -215,7 +215,45 @@ class AttributesTest(_base.ORMTest):
         a.email_address = 'foo@bar.com'
         u.addresses.append(a)
         self.assert_(u.user_id == 7 and u.user_name == 'heythere' and u.addresses[0].email_address == 'lala@123.com' and u.addresses[1].email_address == 'foo@bar.com')
+    
+    def test_extension_lazyload_assertion(self):
+        class Foo(_base.BasicEntity):
+            pass
+        class Bar(_base.BasicEntity):
+            pass
+
+        class ReceiveEvents(AttributeExtension):
+            def append(self, state, child, initiator):
+                state.obj().bars
+                return child
+
+            def remove(self, state, child, initiator):
+                state.obj().bars
+                return child
+
+            def set(self, state, child, oldchild, initiator):
+                return child
 
+        attributes.register_class(Foo)
+        attributes.register_class(Bar)
+
+        bar1, bar2, bar3 = [Bar(id=1), Bar(id=2), Bar(id=3)]
+        def func1():
+            return [bar1, bar2, bar3]
+
+        attributes.register_attribute(Foo, 'bars', uselist=True, callable_=lambda o:func1, useobject=True, extension=[ReceiveEvents()])
+        attributes.register_attribute(Bar, 'foos', uselist=True, useobject=True, extension=[attributes.GenericBackrefExtension('bars')])
+
+        x = Foo()
+        assert_raises(AssertionError, Bar(id=4).foos.append, x)
+        
+        x.bars
+        b = Bar(id=4)
+        b.foos.append(x)
+        attributes.instance_state(x).expire_attributes(['bars'])
+        assert_raises(AssertionError, b.foos.remove, x)
+        
+        
     def test_scalar_listener(self):
         # listeners on ScalarAttributeImpl and MutableScalarAttributeImpl aren't used normally.
         # test that they work for the benefit of user extensions