本文整理匯總了Python中types.UnboundMethodType方法的典型用法代碼示例。如果您正苦於以下問題:Python types.UnboundMethodType方法的具體用法?Python types.UnboundMethodType怎麽用?Python types.UnboundMethodType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類types
的用法示例。
在下文中一共展示了types.UnboundMethodType方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_signature
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def get_signature(obj, method_name):
method = getattr(obj, method_name)
# Eat self for unbound methods bc signature doesn't do it
if PY3:
if (
inspect.isclass(obj)
and not inspect.ismethod(method)
and not isinstance(obj.__dict__.get(method_name), staticmethod)
):
method = functools.partial(method, None)
else:
if (
isinstance(method, types.UnboundMethodType)
and method.__self__ is None
):
method = functools.partial(method, None)
try:
return signature(method)
except Exception:
return None
示例2: _curry_callable
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def _curry_callable(obj, *args, **kwargs):
"""Takes a callable and arguments and returns a task queue tuple.
The returned tuple consists of (callable, args, kwargs), and can be pickled
and unpickled safely.
Args:
obj: The callable to curry. See the module docstring for restrictions.
args: Positional arguments to call the callable with.
kwargs: Keyword arguments to call the callable with.
Returns:
A tuple consisting of (callable, args, kwargs) that can be evaluated by
run() with equivalent effect of executing the function directly.
Raises:
ValueError: If the passed in object is not of a valid callable type.
"""
if isinstance(obj, types.MethodType):
return (invoke_member, (obj.im_self, obj.im_func.__name__) + args, kwargs)
elif isinstance(obj, types.BuiltinMethodType):
if not obj.__self__:
return (obj, args, kwargs)
else:
return (invoke_member, (obj.__self__, obj.__name__) + args, kwargs)
elif isinstance(obj, types.ObjectType) and hasattr(obj, "__call__"):
return (obj, args, kwargs)
elif isinstance(obj, (types.FunctionType, types.BuiltinFunctionType,
types.ClassType, types.UnboundMethodType)):
return (obj, args, kwargs)
else:
raise ValueError("obj must be callable")
示例3: loadTestsFromName
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def loadTestsFromName(self, name, module=None):
"""Return a suite of all tests cases given a string specifier.
The name may resolve either to a module, a test case class, a
test method within a test case class, or a callable object which
returns a TestCase or TestSuite instance.
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.')
if module is None:
parts_copy = parts[:]
while parts_copy:
try:
module = __import__('.'.join(parts_copy))
break
except ImportError:
del parts_copy[-1]
if not parts_copy: raise
parts = parts[1:]
obj = module
for part in parts:
parent, obj = obj, getattr(obj, part)
if type(obj) == types.ModuleType:
return self.loadTestsFromModule(obj)
elif (isinstance(obj, (type, types.ClassType)) and
issubclass(obj, TestCase)):
return self.loadTestsFromTestCase(obj)
elif type(obj) == types.UnboundMethodType:
return parent(obj.__name__)
elif isinstance(obj, TestSuite):
return obj
elif callable(obj):
test = obj()
if not isinstance(test, (TestCase, TestSuite)):
raise ValueError, \
"calling %s returned %s, not a test" % (obj,test)
return test
else:
raise ValueError, "don't know how to make test from: %s" % obj
示例4: _is_unsupported_type
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def _is_unsupported_type(obj):
return isinstance(obj, (types.ComplexType, types.TupleType, types.FunctionType, types.LambdaType,
types.GeneratorType, types.MethodType, types.UnboundMethodType, types.BuiltinFunctionType, types.BuiltinMethodType, types.FileType,
types.XRangeType, types.TracebackType, types.FrameType, types.DictProxyType, types.NotImplementedType, types.GetSetDescriptorType,
types.MemberDescriptorType))
示例5: loadTestsFromName
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def loadTestsFromName(self, name, module=None):
"""Return a suite of all tests cases given a string specifier.
The name may resolve either to a module, a test case class, a
test method within a test case class, or a callable object which
returns a TestCase or TestSuite instance.
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.')
if module is None:
parts_copy = parts[:]
while parts_copy:
try:
module = __import__('.'.join(parts_copy))
break
except ImportError:
del parts_copy[-1]
if not parts_copy:
raise
parts = parts[1:]
obj = module
for part in parts:
parent, obj = obj, getattr(obj, part)
if isinstance(obj, types.ModuleType):
return self.loadTestsFromModule(obj)
elif isinstance(obj, type) and issubclass(obj, case.TestCase):
return self.loadTestsFromTestCase(obj)
elif (isinstance(obj, types.UnboundMethodType) and
isinstance(parent, type) and
issubclass(parent, case.TestCase)):
return self.suiteClass([parent(obj.__name__)])
elif isinstance(obj, suite.TestSuite):
return obj
elif hasattr(obj, '__call__'):
test = obj()
if isinstance(test, suite.TestSuite):
return test
elif isinstance(test, case.TestCase):
return self.suiteClass([test])
else:
raise TypeError("calling %s returned %s, not a test" %
(obj, test))
else:
raise TypeError("don't know how to make test from: %s" % obj)
示例6: loadTestsFromName
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def loadTestsFromName(self, name, module=None):
"""Return a suite of all test cases given a string specifier.
The name may resolve either to a module, a test case class, a
test method within a test case class, or a callable object which
returns a TestCase or TestSuite instance.
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.')
if module is None:
parts_copy = parts[:]
while parts_copy:
try:
module = __import__('.'.join(parts_copy))
break
except ImportError:
del parts_copy[-1]
if not parts_copy:
raise
parts = parts[1:]
obj = module
for part in parts:
parent, obj = obj, getattr(obj, part)
if isinstance(obj, types.ModuleType):
return self.loadTestsFromModule(obj)
elif isinstance(obj, type) and issubclass(obj, case.TestCase):
return self.loadTestsFromTestCase(obj)
elif (isinstance(obj, types.UnboundMethodType) and
isinstance(parent, type) and
issubclass(parent, case.TestCase)):
name = parts[-1]
inst = parent(name)
return self.suiteClass([inst])
elif isinstance(obj, suite.TestSuite):
return obj
elif hasattr(obj, '__call__'):
test = obj()
if isinstance(test, suite.TestSuite):
return test
elif isinstance(test, case.TestCase):
return self.suiteClass([test])
else:
raise TypeError("calling %s returned %s, not a test" %
(obj, test))
else:
raise TypeError("don't know how to make test from: %s" % obj)
示例7: loadTestsFromName
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def loadTestsFromName(self, name, module=None):
"""Return a suite of all tests cases given a string specifier.
The name may resolve either to a module, a test case class, a
test method within a test case class, or a callable object which
returns a TestCase or TestSuite instance.
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.')
if module is None:
parts_copy = parts[:]
while parts_copy:
try:
module = __import__('.'.join(parts_copy))
break
except ImportError:
del parts_copy[-1]
if not parts_copy:
raise
parts = parts[1:]
obj = module
for part in parts:
parent, obj = obj, getattr(obj, part)
if isinstance(obj, types.ModuleType):
return self.loadTestsFromModule(obj)
elif isinstance(obj, type) and issubclass(obj, case.TestCase):
return self.loadTestsFromTestCase(obj)
elif (isinstance(obj, types.UnboundMethodType) and
isinstance(parent, type) and
issubclass(parent, case.TestCase)):
name = parts[-1]
inst = parent(name)
return self.suiteClass([inst])
elif isinstance(obj, suite.TestSuite):
return obj
elif hasattr(obj, '__call__'):
test = obj()
if isinstance(test, suite.TestSuite):
return test
elif isinstance(test, case.TestCase):
return self.suiteClass([test])
else:
raise TypeError("calling %s returned %s, not a test" %
(obj, test))
else:
raise TypeError("don't know how to make test from: %s" % obj)
示例8: loadTestsFromName
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def loadTestsFromName(self, name, module=None):
"""Return a suite of all tests cases given a string specifier.
The name may resolve either to a module, a test case class, a
test method within a test case class, or a callable object which
returns a TestCase or TestSuite instance.
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.')
if module is None:
parts_copy = parts[:]
while parts_copy:
try:
module = __import__('.'.join(parts_copy))
break
except ImportError:
del parts_copy[-1]
if not parts_copy:
raise
parts = parts[1:]
obj = module
for part in parts:
parent, obj = obj, getattr(obj, part)
if isinstance(obj, types.ModuleType):
return self.loadTestsFromModule(obj)
elif isinstance(obj, type) and issubclass(obj, unittest.TestCase):
return self.loadTestsFromTestCase(obj)
elif (isinstance(obj, types.UnboundMethodType) and
isinstance(parent, type) and
issubclass(parent, case.TestCase)):
return self.suiteClass([parent(obj.__name__)])
elif isinstance(obj, unittest.TestSuite):
return obj
elif hasattr(obj, '__call__'):
test = obj()
if isinstance(test, unittest.TestSuite):
return test
elif isinstance(test, unittest.TestCase):
return self.suiteClass([test])
else:
raise TypeError("calling %s returned %s, not a test" %
(obj, test))
else:
raise TypeError("don't know how to make test from: %s" % obj)
示例9: _copyReusedRecipes
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def _copyReusedRecipes(self):
# XXX HACK - get rid of this when we move the
# recipe classes to the repository.
# makes copies of some of the superclass recipes that are
# created in this module. (specifically, the ones with buildreqs)
recipeClassDict = {}
for recipeClass in self.module.__dict__.values():
if (type(recipeClass) != type or
not issubclass(recipeClass, recipe.Recipe)):
continue
numParents = len(inspect.getmro(recipeClass))
recipeClassDict[recipeClass.__name__] = (numParents, recipeClass)
# create copies of recipes by the number of parents they have
# a class always has more parents than its parent does,
# if you copy the superClasses first, the copies will.
recipeClasses = [ x[1] for x in sorted(recipeClassDict.values(),
key=lambda x: x[0]) ]
for recipeClass in recipeClasses:
className = recipeClass.__name__
# when we create a new class object, it needs its superclasses.
# get the original superclass list and substitute in any
# copies
mro = list(inspect.getmro(recipeClass)[1:])
newMro = []
for superClass in mro:
superName = superClass.__name__
newMro.append(self.module.__dict__.get(superName, superClass))
newDict = {}
for name, attr in recipeClass.__dict__.iteritems():
if type(attr) in [ types.ModuleType, types.MethodType,
types.UnboundMethodType,
types.FunctionType,
staticmethod,
# don't copy in flags, as they
# need to have their data copied out
use.LocalFlagCollection]:
newDict[name] = attr
else:
newDict[name] = copy.deepcopy(attr)
self.module.__dict__[className] = \
new.classobj(className, tuple(newMro), newDict)
示例10: loadTestsFromName
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def loadTestsFromName(self, name, module=None):
"""Return a suite of all tests cases given a string specifier.
The name may resolve either to a module, a test case class, a
test method within a test case class, or a callable object which
returns a TestCase or TestSuite instance.
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.')
unused_parts = []
if module is None:
if not parts:
raise ValueError('incomplete test name: %s' % name)
else:
parts_copy = parts[:]
while parts_copy:
target = '.'.join(parts_copy)
if target in sys.modules:
module = reload(sys.modules[target])
parts = unused_parts
break
else:
try:
module = __import__(target)
parts = unused_parts
break
except ImportError:
unused_parts.insert(0, parts_copy[-1])
del parts_copy[-1]
if not parts_copy:
raise
parts = parts[1:]
obj = module
for part in parts:
obj = getattr(obj, part)
if isinstance(obj, types.ModuleType):
return self.loadTestsFromModule(obj)
elif (((six.PY3 and isinstance(obj, type))
or isinstance(obj, (type, types.ClassType)))
and issubclass(obj, unittest.TestCase)):
return self.loadTestsFromTestCase(obj)
elif isinstance(obj, types.UnboundMethodType):
if six.PY3:
return obj.__self__.__class__(obj.__name__)
else:
return obj.im_class(obj.__name__)
elif hasattr(obj, '__call__'):
test = obj()
if not isinstance(test, unittest.TestCase) and \
not isinstance(test, unittest.TestSuite):
raise ValueError('calling %s returned %s, '
'not a test' % (obj, test))
return test
else:
raise ValueError('do not know how to make test from: %s' % obj)
示例11: loadTestsFromName
# 需要導入模塊: import types [as 別名]
# 或者: from types import UnboundMethodType [as 別名]
def loadTestsFromName(self, name, module=None):
"""Return a suite of all tests cases given a string specifier.
The name may resolve either to a module, a test case class, a
test method within a test case class, or a callable object which
returns a TestCase or TestSuite instance.
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.')
unused_parts = []
if module is None:
if not parts:
raise ValueError("incomplete test name: %s" % name)
else:
parts_copy = parts[:]
while parts_copy:
target = ".".join(parts_copy)
if target in sys.modules:
module = reload(sys.modules[target])
parts = unused_parts
break
else:
try:
module = __import__(target)
parts = unused_parts
break
except ImportError:
unused_parts.insert(0,parts_copy[-1])
del parts_copy[-1]
if not parts_copy:
raise
parts = parts[1:]
obj = module
for part in parts:
obj = getattr(obj, part)
if type(obj) == types.ModuleType:
return self.loadTestsFromModule(obj)
elif (((py3k and isinstance(obj, type))
or isinstance(obj, (type, types.ClassType)))
and issubclass(obj, TestCase)):
return self.loadTestsFromTestCase(obj)
elif type(obj) == types.UnboundMethodType:
if py3k:
return obj.__self__.__class__(obj.__name__)
else:
return obj.im_class(obj.__name__)
elif hasattr(obj, '__call__'):
test = obj()
if not isinstance(test, TestCase) and \
not isinstance(test, TestSuite):
raise ValueError("calling %s returned %s, "
"not a test" % (obj,test))
return test
else:
raise ValueError("do not know how to make test from: %s" % obj)