super(ObjectNotExecutableError, self).__init__(
"Not an executable object: %r" % target
)
+ self.target = target
+
+ def __reduce__(self):
+ return self.__class__, (self.target,)
class NoSuchModuleError(ArgumentError):
self.edges = edges
def __reduce__(self):
- return self.__class__, (None, self.cycles, self.edges, self.args[0])
+ return (
+ self.__class__,
+ (None, self.cycles, self.edges, self.args[0]),
+ {"code": self.code} if self.code is not None else {},
+ )
class CompileError(SQLAlchemyError):
"Compiler %r can't render element of type %s%s"
% (compiler, element_type, ": %s" % message if message else "")
)
+ self.compiler = compiler
+ self.element_type = element_type
+ self.message = message
+
+ def __reduce__(self):
+ return self.__class__, (self.compiler, self.element_type, self.message)
class IdentifierError(SQLAlchemyError):
object that's in a closed state."""
-class NoSuchColumnError(KeyError, InvalidRequestError):
+class NoSuchColumnError(InvalidRequestError, KeyError):
"""A nonexistent column is requested from a ``Row``."""
self.params,
self.orig,
self.hide_parameters,
+ self.__dict__.get("code"),
self.ismulti,
),
+ {"detail": self.detail},
)
@_preloaded.preload_module("sqlalchemy.sql.util")
self.orig,
self.hide_parameters,
self.connection_invalidated,
+ self.__dict__.get("code"),
self.ismulti,
),
+ {"detail": self.detail},
)
def __init__(
"""Tests exceptions and DB-API exception wrapping."""
+from itertools import product
+import pickle
from sqlalchemy import exc as sa_exceptions
from sqlalchemy.engine import default
+from sqlalchemy.testing import combinations_list
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.util import compat
self.assert_(False)
except SystemExit:
self.assert_(True)
+
+
+def details(cls):
+ inst = cls("msg", "stmt", (), "orig")
+ inst.add_detail("d1")
+ inst.add_detail("d2")
+ return inst
+
+
+ALL_EXC = [
+ (
+ [sa_exceptions.SQLAlchemyError],
+ [lambda cls: cls(1, 2, code="42")],
+ ),
+ ([sa_exceptions.ObjectNotExecutableError], [lambda cls: cls("xx")]),
+ (
+ [
+ sa_exceptions.ArgumentError,
+ sa_exceptions.NoSuchModuleError,
+ sa_exceptions.NoForeignKeysError,
+ sa_exceptions.AmbiguousForeignKeysError,
+ sa_exceptions.CompileError,
+ sa_exceptions.IdentifierError,
+ sa_exceptions.DisconnectionError,
+ sa_exceptions.InvalidatePoolError,
+ sa_exceptions.TimeoutError,
+ sa_exceptions.InvalidRequestError,
+ sa_exceptions.NoInspectionAvailable,
+ sa_exceptions.PendingRollbackError,
+ sa_exceptions.ResourceClosedError,
+ sa_exceptions.NoSuchColumnError,
+ sa_exceptions.NoResultFound,
+ sa_exceptions.MultipleResultsFound,
+ sa_exceptions.NoReferenceError,
+ sa_exceptions.AwaitRequired,
+ sa_exceptions.MissingGreenlet,
+ sa_exceptions.NoSuchTableError,
+ sa_exceptions.UnreflectableTableError,
+ sa_exceptions.UnboundExecutionError,
+ ],
+ [lambda cls: cls("foo", code="42")],
+ ),
+ (
+ [sa_exceptions.CircularDependencyError],
+ [
+ lambda cls: cls("msg", ["cycles"], "edges"),
+ lambda cls: cls("msg", ["cycles"], "edges", "xx", "zz"),
+ ],
+ ),
+ (
+ [sa_exceptions.UnsupportedCompilationError],
+ [lambda cls: cls("cmp", "el"), lambda cls: cls("cmp", "el", "msg")],
+ ),
+ (
+ [sa_exceptions.NoReferencedTableError],
+ [lambda cls: cls("msg", "tbl")],
+ ),
+ (
+ [sa_exceptions.NoReferencedColumnError],
+ [lambda cls: cls("msg", "tbl", "col")],
+ ),
+ (
+ [sa_exceptions.StatementError],
+ [
+ lambda cls: cls("msg", "stmt", (), "orig"),
+ lambda cls: cls("msg", "stmt", (), "orig", True, "99", True),
+ details,
+ ],
+ ),
+ (
+ [
+ sa_exceptions.DBAPIError,
+ sa_exceptions.InterfaceError,
+ sa_exceptions.DatabaseError,
+ sa_exceptions.DataError,
+ sa_exceptions.OperationalError,
+ sa_exceptions.IntegrityError,
+ sa_exceptions.InternalError,
+ sa_exceptions.ProgrammingError,
+ sa_exceptions.NotSupportedError,
+ ],
+ [
+ lambda cls: cls("stmt", (), "orig"),
+ lambda cls: cls("stmt", (), "orig", True, True, "99", True),
+ details,
+ ],
+ ),
+ (
+ [
+ sa_exceptions.SADeprecationWarning,
+ sa_exceptions.RemovedIn20Warning,
+ sa_exceptions.MovedIn20Warning,
+ sa_exceptions.SAWarning,
+ ],
+ [lambda cls: cls("foo", code="42")],
+ ),
+ ([sa_exceptions.SAPendingDeprecationWarning], [lambda cls: cls(1, 2, 3)]),
+]
+
+
+class PickleException(fixtures.TestBase):
+ def test_all_exc(self):
+ found = {
+ e
+ for e in vars(sa_exceptions).values()
+ if isinstance(e, type) and issubclass(e, Exception)
+ }
+
+ listed = set()
+ for cls_list, _ in ALL_EXC:
+ listed.update(cls_list)
+
+ eq_(found, listed)
+
+ def make_combinations():
+ unroll = []
+ for cls_list, callable_list in ALL_EXC:
+ unroll.extend(product(cls_list, callable_list))
+
+ print(unroll)
+ return combinations_list(unroll)
+
+ @make_combinations()
+ def test_exc(self, cls, ctor):
+ inst = ctor(cls)
+ re_created = pickle.loads(pickle.dumps(inst))
+
+ eq_(re_created.__class__, cls)
+ eq_(re_created.args, inst.args)
+ eq_(re_created.__dict__, inst.__dict__)