From: Jason Kirtland Date: Wed, 25 Apr 2007 18:09:25 +0000 (+0000) Subject: - Always propagate constructor exceptions in mapped clases (applied patch in #528) X-Git-Tag: rel_0_3_7~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7edf9a37210c9b41f9d0dc3038eb9faa62c82e42;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Always propagate constructor exceptions in mapped clases (applied patch in #528) --- diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 2bffef815c..cbbe839990 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -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 diff --git a/test/orm/mapper.py b/test/orm/mapper.py index d1c4cf5463..d6563d014b 100644 --- a/test/orm/mapper.py +++ b/test/orm/mapper.py @@ -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"""