From e7fcb190cc0a6bf5631cee7dad78ff8c0490403d Mon Sep 17 00:00:00 2001 From: Michael Merickel <michael@merickel.org> Date: Mon, 10 Jul 2017 07:08:52 +0200 Subject: [PATCH] Merge pull request #3124 from mmerickel/circular-import --- pyramid/util.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 52 insertions(+), 0 deletions(-) diff --git a/pyramid/util.py b/pyramid/util.py index 2827884..97e761c 100644 --- a/pyramid/util.py +++ b/pyramid/util.py @@ -17,6 +17,8 @@ ) from pyramid.compat import ( + getargspec, + im_func, is_nonstr_iter, integer_types, string_types, @@ -645,3 +647,53 @@ return (pattern[0] == "." and (host.endswith(pattern) or host == pattern[1:]) or pattern == host) + + +def takes_one_arg(callee, attr=None, argname=None): + ismethod = False + if attr is None: + attr = '__call__' + if inspect.isroutine(callee): + fn = callee + elif inspect.isclass(callee): + try: + fn = callee.__init__ + except AttributeError: + return False + ismethod = hasattr(fn, '__call__') + else: + try: + fn = getattr(callee, attr) + except AttributeError: + return False + + try: + argspec = getargspec(fn) + except TypeError: + return False + + args = argspec[0] + + if hasattr(fn, im_func) or ismethod: + # it's an instance method (or unbound method on py2) + if not args: + return False + args = args[1:] + + if not args: + return False + + if len(args) == 1: + return True + + if argname: + + defaults = argspec[3] + if defaults is None: + defaults = () + + if args[0] == argname: + if len(args) - len(defaults) == 1: + return True + + return False -- Gitblit v1.9.3