_version_token = None
-class SQLAlchemyError(Exception):
- """Generic error class."""
+class _CodeStrMixin(object):
+ """helper which adds 'code' as an attribute and '_code_str' as a method"""
code = None
code = kw.pop("code", None)
if code is not None:
self.code = code
- super(SQLAlchemyError, self).__init__(*arg, **kw)
+ super(_CodeStrMixin, self).__init__(*arg, **kw)
def _code_str(self):
if not self.code:
)
)
+
+class SQLAlchemyError(_CodeStrMixin, Exception):
+ """Generic error class."""
+
def _message(self, as_unicode=compat.py3k):
# rules:
#
# Warnings
-class SADeprecationWarning(DeprecationWarning):
+class SADeprecationWarning(_CodeStrMixin, DeprecationWarning):
"""Issued for usage of deprecated APIs."""
deprecated_since = None
"Indicates the version that started raising this deprecation warning"
+ def __str__(self):
+ message = super(SADeprecationWarning, self).__str__()
+ if self.code:
+ message = "%s %s" % (message, self._code_str())
+ return message
+
class RemovedIn20Warning(SADeprecationWarning):
"""Issued for usage of APIs specifically deprecated in SQLAlchemy 2.0.
SQLALCHEMY_WARN_20 = True
-def _warn_with_version(msg, version, type_, stacklevel):
+def _warn_with_version(msg, version, type_, stacklevel, code=None):
is_20 = issubclass(type_, exc.RemovedIn20Warning)
if is_20 and not SQLALCHEMY_WARN_20:
if is_20:
msg += " (Background on SQLAlchemy 2.0 at: http://sqlalche.me/e/b8d9)"
- warn = type_(msg)
+ warn = type_(msg, code=code)
warn.deprecated_since = version
warnings.warn(warn, stacklevel=stacklevel + 1)
-def warn_deprecated(msg, version, stacklevel=3):
- _warn_with_version(msg, version, exc.SADeprecationWarning, stacklevel)
+def warn_deprecated(msg, version, stacklevel=3, code=None):
+ _warn_with_version(
+ msg, version, exc.SADeprecationWarning, stacklevel, code=code
+ )
-def warn_deprecated_limited(msg, args, version, stacklevel=3):
+def warn_deprecated_limited(msg, args, version, stacklevel=3, code=None):
"""Issue a deprecation warning with a parameterized string,
limiting the number of registrations.
"""
if args:
msg = _hash_limit_string(msg, 10, args)
- _warn_with_version(msg, version, exc.SADeprecationWarning, stacklevel)
+ _warn_with_version(
+ msg, version, exc.SADeprecationWarning, stacklevel, code=code
+ )
-def warn_deprecated_20(msg, stacklevel=3):
+def warn_deprecated_20(msg, stacklevel=3, code=None):
_warn_with_version(
msg,
exc.RemovedIn20Warning.deprecated_since,
exc.RemovedIn20Warning,
stacklevel,
+ code=code,
)