From c7d04beeac6ad54d638afb01783dee2d769aef9d Mon Sep 17 00:00:00 2001 From: "Martin J. Hsu" Date: Fri, 25 Sep 2015 16:15:28 +0800 Subject: - wrap ColumnDefault empty arg callables like functools.wraps, setting __name__, __doc__, and __module__ --- lib/sqlalchemy/sql/schema.py | 5 +++-- lib/sqlalchemy/util/__init__.py | 2 +- lib/sqlalchemy/util/langhelpers.py | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 137208584..0c433d16e 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -1981,13 +1981,14 @@ class ColumnDefault(DefaultGenerator): try: argspec = util.get_callable_argspec(fn, no_self=True) except TypeError: - return lambda ctx: fn() + return util.wrap_callable(fn) defaulted = argspec[3] is not None and len(argspec[3]) or 0 positionals = len(argspec[0]) - defaulted if positionals == 0: - return lambda ctx: fn() + return util.wrap_callable(fn) + elif positionals == 1: return fn else: diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py index ed968f168..36a81dbce 100644 --- a/lib/sqlalchemy/util/__init__.py +++ b/lib/sqlalchemy/util/__init__.py @@ -36,7 +36,7 @@ from .langhelpers import iterate_attributes, class_hierarchy, \ generic_repr, counter, PluginLoader, hybridproperty, hybridmethod, \ safe_reraise,\ get_callable_argspec, only_once, attrsetter, ellipses_string, \ - warn_limited, map_bits, MemoizedSlots, EnsureKWArgType + warn_limited, map_bits, MemoizedSlots, EnsureKWArgType, wrap_callable from .deprecations import warn_deprecated, warn_pending_deprecation, \ deprecated, pending_deprecation, inject_docstring_text diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 743afccfd..9f259aea3 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -1377,3 +1377,29 @@ class EnsureKWArgType(type): return fn(*arg) return update_wrapper(wrap, fn) + +def wrap_callable(fn): + """Wrap callable and set __name__, __doc__, and __module__. + + :param fn: + object with __call__ method + """ + if hasattr(fn, '__name__'): + _f = update_wrapper(lambda ctx: fn(), fn) + _f.__doc__ = _f.__doc__ or fn.__name__ + return _f + else: + _f = lambda ctx: fn() + _f.__name__ = fn.__class__.__name__ + _f.__module__ = fn.__module__ + + if hasattr(fn.__call__, '__doc__') and fn.__call__.__doc__: + _f.__doc__ = fn.__call__.__doc__ + elif hasattr(fn.__class__, '__doc__') and fn.__class__.__doc__: + _f.__doc__ = fn.__class__.__doc__ + elif fn.__doc__: + _f.__doc__ = fn.__doc__ + else: + _f.__doc__ = fn.__class__.__name__ + + return _f -- cgit v1.2.1