From bf89ca2e10ff1c38e76f78e2d11d7858a50df547 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 2 Mar 2014 13:59:06 -0500 Subject: - 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 --- lib/sqlalchemy/sql/schema.py | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) (limited to 'lib/sqlalchemy/sql/schema.py') 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: -- cgit v1.2.1