]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
improved handling of exceptions upon __init__(): will preserve the stack
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 30 Jun 2007 16:57:20 +0000 (16:57 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 30 Jun 2007 16:57:20 +0000 (16:57 +0000)
trace of the original __init__ exception; errors raised during session.expunge() will be
reported as warnings

lib/sqlalchemy/orm/mapper.py
lib/sqlalchemy/util.py
test/orm/mapper.py

index 461b9da7ab8f280dbd2ea93f8a23ac15c2ce53f7..b037d0d1846f75b53d16fe24e54486434962d82b 100644 (file)
@@ -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
index ea5a468d2afdd3a0101a615622af5e48742cd707..0d66080ac2bd5bbdb48c74634e2da44f78e54724 100644 (file)
@@ -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."""
 
index 399b87e33095a518a551433ecb0ecf99408270a9..94b66f93aa9063411edfd351079056f725729181 100644 (file)
@@ -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()