diff options
author | Jason Kirtland <jek@discorporate.us> | 2007-04-25 18:09:25 +0000 |
---|---|---|
committer | Jason Kirtland <jek@discorporate.us> | 2007-04-25 18:09:25 +0000 |
commit | 7edf9a37210c9b41f9d0dc3038eb9faa62c82e42 (patch) | |
tree | fad9187153d220bbf7235f4139eb7ec6022128ee | |
parent | 2f547222da46b38df07dde08e60bc5efbb0afd79 (diff) | |
download | sqlalchemy-7edf9a37210c9b41f9d0dc3038eb9faa62c82e42.tar.gz |
- Always propagate constructor exceptions in mapped clases (applied patch in #528)
-rw-r--r-- | lib/sqlalchemy/orm/mapper.py | 11 | ||||
-rw-r--r-- | test/orm/mapper.py | 29 |
2 files changed, 36 insertions, 4 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 2bffef815..cbbe83999 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 d1c4cf546..d6563d014 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""" |