]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
merged, with some modifications, mapped constructor exceptions fix from trunk 2825
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 30 Jun 2007 17:05:27 +0000 (17:05 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 30 Jun 2007 17:05:27 +0000 (17:05 +0000)
lib/sqlalchemy/orm/mapper.py
lib/sqlalchemy/util.py
test/orm/mapper.py

index 8b0878688d4d47efe7cec4d286412fa5664fd78e..8c4a9c8a31aba7e17d2368fa56b4791e902fecf1 100644 (file)
@@ -760,12 +760,11 @@ class Mapper(object):
             if oldinit is not None:
                 try:
                     oldinit(instance, *args, **kwargs)
-                except Exception, e:
-                    try:
-                        self.extension.init_failed(self, self.class_, instance, args, kwargs)
-                    except:
-                        pass # raise original exception instead
-                    raise e
+                except:
+                    # call init_failed but suppress exceptions into warnings so that original __init__ 
+                    # exception is raised
+                    util.warn_exception(self.extension.init_failed, self, self.class_, instance, args, kwargs)
+                    raise
 
         # override oldinit, ensuring that its not already a Mapper-decorated init method
         if oldinit is None or not hasattr(oldinit, '_oldinit'):
index 9047d5c4149e9b0c4769ff2db751b7ecbfd20b55..2e1c09c0e5661fbe03ce7754403f0fd730f0d387 100644 (file)
@@ -12,6 +12,7 @@ except ImportError:
 
 import md5
 import sys
+import warnings
 import __builtin__
 
 try:
@@ -157,6 +158,13 @@ def duck_type_collection(specimen, default=None):
         return dict
     else:
         return default
+
+def warn_exception(func, *args, **kwargs):
+    """executes the given function, catches all exceptions and converts to a warning."""
+    try:
+        return func(*args, **kwargs)
+    except:
+        warnings.warn(RuntimeWarning("%s('%s') ignored" % sys.exc_info()[0:2]))
     
 class SimpleProperty(object):
     """A *default* property accessor."""
index 19a2e0a3c0059ee010c5d9b9de67cfce5dd4f485..f5aa1f7e7a0a956ab9e753da38941199a74d23e0 100644 (file)
@@ -145,15 +145,14 @@ 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, extension=SessionContextExt(SessionContext()))
+        clear_mappers()
+        mapper(Foo, users, extension=SessionContextExt(SessionContext()))
+        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