Commits

jason kirtland  committed 9492207

- Always propagate constructor exceptions in mapped clases (applied patch in #528)

  • Participants
  • Parent commits 1d8aaed

Comments (0)

Files changed (2)

File lib/sqlalchemy/orm/mapper.py

             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

File test/orm/mapper.py

             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"""