From: Mike Bayer Date: Sat, 30 Jun 2007 16:57:20 +0000 (+0000) Subject: improved handling of exceptions upon __init__(): will preserve the stack X-Git-Tag: rel_0_3_9~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8c03fdf5f4b3b890d477bde1b4f7080b22b55447;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git improved handling of exceptions upon __init__(): will preserve the stack trace of the original __init__ exception; errors raised during session.expunge() will be reported as warnings --- diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 461b9da7ab..b037d0d184 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -670,13 +670,14 @@ class Mapper(object): if oldinit is not None: try: oldinit(self, *args, **kwargs) - except Exception, e: - try: + except: + def go(): if session is not None: session.expunge(self) - except: - pass # raise original exception instead - raise e + # convert expunge() exceptions to warnings + util.warn_exception(go) + raise + # 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/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index ea5a468d2a..0d66080ac2 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -11,6 +11,8 @@ except ImportError: import dummy_threading as threading import md5 +import sys +import warnings import __builtin__ @@ -127,6 +129,13 @@ def duck_type_collection(col, default=None): else: return default +def warn_exception(func): + """executes the given function, catches all exceptions and converts to a warning.""" + try: + return func() + except: + warnings.warn(RuntimeWarning("%s('%s') ignored" % sys.exc_info()[0:2])) + class SimpleProperty(object): """A *default* property accessor.""" diff --git a/test/orm/mapper.py b/test/orm/mapper.py index 399b87e330..94b66f93aa 100644 --- a/test/orm/mapper.py +++ b/test/orm/mapper.py @@ -178,19 +178,16 @@ class MapperTest(MapperSuperTest): except Exception, e: assert e is ex - class Bar(object): - def __init__(self): - object_session(self).expunge(self) - raise ex - - mapper(Bar, orders) - + def bad_expunge(foo): + raise Exception("this exception should be stated as a warning") + + sess.expunge = bad_expunge try: - Bar(_sa_session=sess) + Foo(_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""" s = create_session()