From: Mike Bayer Date: Sun, 19 Dec 2010 23:49:26 +0000 (-0500) Subject: - use a subset of inspect.getargspec() so that get_func_kwargs()/constructor_copy... X-Git-Tag: rel_0_7b1~152 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99b91647e996630d778df89c08ca909a6194182b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - use a subset of inspect.getargspec() so that get_func_kwargs()/constructor_copy() don't take up 20 function calls --- diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 945e2a6bd4..2b9e890fc6 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -54,6 +54,9 @@ def get_cls_kwargs(cls): pass along unrecognized keywords to it's base classes, and the collection process is repeated recursively on each of the bases. + Uses a subset of inspect.getargspec() to cut down on method overhead. + No anonymous tuple arguments please ! + """ for c in cls.__mro__: @@ -70,15 +73,39 @@ def get_cls_kwargs(cls): if not ctr or not isinstance(ctr, types.FunctionType): stack.update(class_.__bases__) continue - names, _, has_kw, _ = inspect.getargspec(ctr) + + # this is shorthand for + # names, _, has_kw, _ = inspect.getargspec(ctr) + + names, has_kw = inspect_func_args(ctr) args.update(names) if has_kw: stack.update(class_.__bases__) args.discard('self') return args +try: + from inspect import CO_VARKEYWORDS + def inspect_func_args(fn): + co = fn.func_code + nargs = co.co_argcount + names = co.co_varnames + args = list(names[:nargs]) + has_kw = bool(co.co_flags & CO_VARKEYWORDS) + return args, has_kw +except ImportError: + def inspect_func_args(fn): + names, _, has_kw, _ = inspect.getargspec(fn) + return names, bool(has_kw) + def get_func_kwargs(func): - """Return the full set of legal kwargs for the given `func`.""" + """Return the set of legal kwargs for the given `func`. + + Uses getargspec so is safe to call for methods, functions, + etc. + + """ + return inspect.getargspec(func)[0] def format_argspec_plus(fn, grouped=True):