]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] ensure pickleability of all ORM exceptions
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 12 Jan 2012 01:58:10 +0000 (20:58 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 12 Jan 2012 01:58:10 +0000 (20:58 -0500)
for multiprocessing compatibility. [ticket:2371]

CHANGES
lib/sqlalchemy/orm/exc.py
test/orm/test_pickled.py

diff --git a/CHANGES b/CHANGES
index fc72a7109c526f4b377e0e321429f0a49a796138..beac4243cd8772c0cf038afc8a1da776abf7fe09 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -29,10 +29,14 @@ CHANGES
     emitted on a pending object, it would fail 
     to load.
 
+  - [bug] ensure pickleability of all ORM exceptions
+    for multiprocessing compatibility. [ticket:2371]
+
 - engine
   - [bug] Added __reduce__ to StatementError, 
-    DBAPIError so that exceptions are pickleable,
-    as when using multiprocessing.  However, not 
+    DBAPIError, column errors so that exceptions 
+    are pickleable, as when using multiprocessing.  
+    However, not 
     all DBAPIs support this yet, such as 
     psycopg2. [ticket:2371]
 
index 1146284f95d563ec6f8f98db4563921a3bd95606..2aaf1490b16a0355857455aac67fc881bf865453 100644 (file)
@@ -72,6 +72,8 @@ class UnmappedInstanceError(UnmappedError):
                         'required?' % _safe_cls_name(obj))
         UnmappedError.__init__(self, msg)
 
+    def __reduce__(self):
+        return self.__class__, (None, self.args[0])
 
 class UnmappedClassError(UnmappedError):
     """An mapping operation was requested for an unknown class."""
@@ -81,6 +83,8 @@ class UnmappedClassError(UnmappedError):
             msg = _default_unmapped(cls)
         UnmappedError.__init__(self, msg)
 
+    def __reduce__(self):
+        return self.__class__, (None, self.args[0])
 
 class ObjectDeletedError(sa.exc.InvalidRequestError):
     """A refresh operation failed to retrieve the database
@@ -101,12 +105,15 @@ class ObjectDeletedError(sa.exc.InvalidRequestError):
     object.   
     
     """
-    def __init__(self, state):
-        sa.exc.InvalidRequestError.__init__(
-             self,
-             "Instance '%s' has been deleted, or its "
+    def __init__(self, state, msg=None):
+        if not msg:
+            msg = "Instance '%s' has been deleted, or its "\
              "row is otherwise not present." % orm_util.state_str(state)
-        )
+
+        sa.exc.InvalidRequestError.__init__(self, msg)
+
+    def __reduce__(self):
+        return self.__class__, (None, self.args[0])
 
 class UnmappedColumnError(sa.exc.InvalidRequestError):
     """Mapping operation was requested on an unknown column."""
index f63c4cfa2d657db8c5e6a58ab5c264ca3f86fb36..920159dc1820437ea5c0ff4a364cef0a844a9242 100644 (file)
@@ -319,6 +319,21 @@ class PickleTest(fixtures.MappedTest):
         screen2 = Screen(Child2(), screen1)
         pickle.loads(pickle.dumps(screen2))
 
+    def test_exceptions(self):
+        class Foo(object):
+            pass
+        users = self.tables.users
+        mapper(User, users)
+
+        for sa_exc in (
+            orm_exc.UnmappedInstanceError(Foo()),
+            orm_exc.UnmappedClassError(Foo),
+            orm_exc.ObjectDeletedError(attributes.instance_state(User())),
+        ):
+            for loads, dumps in picklers():
+                repickled = loads(dumps(sa_exc))
+                eq_(repickled.args[0], sa_exc.args[0])
+
 class PolymorphicDeferredTest(fixtures.MappedTest):
     @classmethod
     def define_tables(cls, metadata):