diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-12-19 18:49:26 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-12-19 18:49:26 -0500 |
| commit | 99b91647e996630d778df89c08ca909a6194182b (patch) | |
| tree | eaedcb701ff76bcc3561ee2c020f64fe316e3bc6 /lib/sqlalchemy/util | |
| parent | 8e6d27c8a4bef3ec4ca404cba88a968f6b1f1832 (diff) | |
| download | sqlalchemy-99b91647e996630d778df89c08ca909a6194182b.tar.gz | |
- use a subset of inspect.getargspec() so that get_func_kwargs()/constructor_copy() don't take up 20 function calls
Diffstat (limited to 'lib/sqlalchemy/util')
| -rw-r--r-- | lib/sqlalchemy/util/langhelpers.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 945e2a6bd..2b9e890fc 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): |
