summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/schema.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-03-02 13:59:06 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2014-03-02 13:59:06 -0500
commitbf89ca2e10ff1c38e76f78e2d11d7858a50df547 (patch)
tree2ba46827eb96416e0b8db822b6495d4bc1e6f673 /lib/sqlalchemy/sql/schema.py
parent6750c39a4fc6be01502f3c9b1c6ba2d3f67a030e (diff)
downloadsqlalchemy-bf89ca2e10ff1c38e76f78e2d11d7858a50df547.tar.gz
- get util.get_callable_argspec() to be completely bulletproof for 2.6-3.4,
methods, classes, builtins, functools.partial(), everything known so far - use get_callable_argspec() within ColumnDefault._maybe_wrap_callable, re: #2979
Diffstat (limited to 'lib/sqlalchemy/sql/schema.py')
-rw-r--r--lib/sqlalchemy/sql/schema.py30
1 files changed, 3 insertions, 27 deletions
diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py
index 2614c08c8..ce31310e7 100644
--- a/lib/sqlalchemy/sql/schema.py
+++ b/lib/sqlalchemy/sql/schema.py
@@ -1836,42 +1836,18 @@ class ColumnDefault(DefaultGenerator):
def _maybe_wrap_callable(self, fn):
"""Wrap callables that don't accept a context.
- The alternative here is to require that
- a simple callable passed to "default" would need
- to be of the form "default=lambda ctx: datetime.now".
- That is the more "correct" way to go, but the case
- of using a zero-arg callable for "default" is so
- much more prominent than the context-specific one
- I'm having trouble justifying putting that inconvenience
- on everyone.
+ This is to allow easy compatiblity with default callables
+ that aren't specific to accepting of a context.
"""
- # TODO: why aren't we using a util.langhelpers function
- # for this? e.g. get_callable_argspec
-
- if isinstance(fn, (types.BuiltinMethodType, types.BuiltinFunctionType)):
- return lambda ctx: fn()
- elif inspect.isfunction(fn) or inspect.ismethod(fn):
- inspectable = fn
- elif inspect.isclass(fn):
- inspectable = fn.__init__
- elif hasattr(fn, '__call__'):
- inspectable = fn.__call__
- else:
- # probably not inspectable, try anyways.
- inspectable = fn
try:
- argspec = inspect.getargspec(inspectable)
+ argspec = util.get_callable_argspec(fn, no_self=True)
except TypeError:
return lambda ctx: fn()
defaulted = argspec[3] is not None and len(argspec[3]) or 0
positionals = len(argspec[0]) - defaulted
- # Py3K compat - no unbound methods
- if inspect.ismethod(inspectable) or inspect.isclass(fn):
- positionals -= 1
-
if positionals == 0:
return lambda ctx: fn()
elif positionals == 1: