]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Always propagate constructor exceptions in mapped clases (applied patch in #528)
authorJason Kirtland <jek@discorporate.us>
Wed, 25 Apr 2007 18:09:25 +0000 (18:09 +0000)
committerJason Kirtland <jek@discorporate.us>
Wed, 25 Apr 2007 18:09:25 +0000 (18:09 +0000)
lib/sqlalchemy/orm/mapper.py
test/orm/mapper.py

index 2bffef815c201e60d7bbc96ce329c0950dd16a05..cbbe839990eaf782f3a287db10de1198a30a29e4 100644 (file)
@@ -670,10 +670,13 @@ class Mapper(object):
             if oldinit is not None:
                 try:
                     oldinit(self, *args, **kwargs)
-                except:
-                    if session is not None:
-                        session.expunge(self)
-                    raise
+                except Exception, e:
+                    try:
+                        if session is not None:
+                            session.expunge(self)
+                    except:
+                        pass # raise original exception instead
+                    raise e
         # override oldinit, insuring that its not already a Mapper-decorated init method
         if oldinit is None or not hasattr(oldinit, '_sa_mapper_init'):
             init._sa_mapper_init = True
index d1c4cf5463a4d92a63e1fe8c5451f28326dc6b98..d6563d014b96fb118c497f68f83adc816d8c050b 100644 (file)
@@ -154,6 +154,35 @@ class MapperTest(MapperSuperTest):
             assert False
         except TypeError, e:
             pass
+
+    def testconstructorexceptions(self):
+        """test that exceptions raised raised in the mapped class are not masked by sa decorations""" 
+        ex = AssertionError('oops')
+        sess = create_session()
+
+        class Foo(object):
+            def __init__(self):
+                raise ex
+        mapper(Foo, users)
+        
+        try:
+            Foo()
+            assert False
+        except Exception, e:
+            assert e is ex
+
+        class Bar(object):
+            def __init__(self):
+                object_session(self).expunge(self)
+                raise ex
+
+        mapper(Bar, orders)
+
+        try:
+            Bar(_sa_session=sess)
+            assert False
+        except Exception, e:
+            assert e is ex
             
     def testrefresh_lazy(self):
         """test that when a lazy loader is set as a trigger on an object's attribute (at the attribute level, not the class level), a refresh() operation doesnt fire the lazy loader or create any problems"""