我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用six.get_unbound_function()。
def settimeout(): """Function to return wrapper over ``socket.socket.set_timeout``.""" def wrapper(self, timeout): """Wrapper to prevent ability change default socket timeout to None. Note: This is workaround for https://github.com/kennethreitz/requests/blob/5524472cc76ea00d64181505f1fbb7f93f11cc2b/requests/packages/urllib3/connectionpool.py#L381 # noqa Args: timeout (int): Seconds of socket timeout. """ if self.gettimeout() and timeout is None: return settimeout_func(self, timeout) settimeout_func = six.get_unbound_function(socket.socket.settimeout) wrapper.__doc__ = settimeout_func.__doc__ wrapper.__name__ = settimeout_func.__name__ return wrapper
def pytest_configure(config): """Hook to check steps consistency.""" if config.option.disable_steps_checker: config.warn('P1', 'Step consistency checker is disabled!') return errors = [] for step_cls in _get_step_classes(): for attr_name in dir(step_cls): if attr_name not in STEPS: continue step_func = six.get_unbound_function(getattr(step_cls, attr_name)) step_func = utils.get_unwrapped_func(step_func) validator = StepValidator(step_func) errors.extend(validator.validate()) if errors: pytest.exit('Some steps are not consistent!\n' + '\n'.join(errors))
def autodoc(base_doc="", classess=DEFAULT_CLASSESS, add_classess=None, skip_classess=None): def copy_method(cls, method_name, method): """ create facade for a method with preservation of original docstring """ def shadow_method(self, *args, **kwargs): return method(self, *args, **kwargs) shadow_method.__doc__ = method.__doc__ setattr(cls, method_name, shadow_method) def wrapped(cls): applies_to = set([]) classess_to_apply = [c for c in classess if not skip_classess or c not in skip_classess] if add_classess: classess_to_apply += [c for c in add_classess if not skip_classess or c not in skip_classess] for autodoc_class in classess_to_apply: applies_to |= set(autodoc_class.applies_to) # Create facades for original methods - docstring of methods are immutable, so we need to change docstring of # functions. But without shadowing the method.__func__ will point to the same function for classes that # inherits them from the same parents. for method_name in applies_to: method = getattr(cls, method_name, None) if method: copy_method(cls, method_name, method) # update docstrings for autodoc_class in classess_to_apply: for method_name in autodoc_class.applies_to: method = getattr(cls, method_name, None) if method: six.get_unbound_function(method).__doc__ = \ autodoc_class.update_docstring(cls, base_doc, six.get_unbound_function(method).__doc__, method_name) return cls return wrapped
def __init__(self, likelihood, Y, tolerance): self.likelihood, self.Y, self.tolerance = likelihood, Y, tolerance self.is_analytic = six.get_unbound_function(likelihood.predict_density) is not\ six.get_unbound_function(gpflow.likelihoods.Likelihood.predict_density)
def test_get_unbound_function(): class X(object): def m(self): pass assert six.get_unbound_function(X.m) is X.__dict__["m"]
def get_unbound_function(unbound): # Op.make_thunk isn't bound, so don't have a __func__ attr. # But bound method, have a __func__ method that point to the # not bound method. That is what we want. if hasattr(unbound, '__func__'): return unbound.__func__ return unbound
def test_replace(): """Replaced methods replace the original one, but are restored after the with.""" class SomethingElse(object): def foo(self, n, y=None): assert None, 'This should never be reached in this test' # Case: bound method s = SomethingElse() def replacement(n, y=None): return y original_method = six.get_method_function(s.foo) with replaced(s.foo, replacement): assert s.foo(1, y='a') == 'a' assert s.foo(2) == None assert six.get_method_function(s.foo) is original_method # Case: unbound method """Python 3 does not support the concept of unbound methods, they are just plain functions without an im_class pointing back to their class. See https://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods, and https://mail.python.org/pipermail/python-dev/2005-January/050625.html for the rationale. To be able to support them under Python3, on= is mandatory. """ s = SomethingElse() def replacement(self, n, y=None): return y original_method = six.get_unbound_function(SomethingElse.foo) with replaced(SomethingElse.foo, replacement, on=SomethingElse): assert s.foo(1, y='a') == 'a' assert s.foo(2) == None restored_method = six.get_unbound_function(SomethingElse.foo) assert restored_method is original_method # Case: unbound method (no on= given) s = SomethingElse() def replacement(self, n, y=None): return y with pytest.raises(ValueError, message='You have to supply a on= when stubbing an unbound method'): with replaced(SomethingElse.foo, replacement): pass