在retry
您使用的装饰是建立在顶部decorator.decorator
实用的装饰,如果未安装包一个简单的回退。
结果具有一个__wrapped__
属性,可让您访问原始功能:
orig = _sftp_command_with_retries.__wrapped__
如果decorator
未安装, 并且 您使用的是3.2之前的Python版本,则该属性将不存在;您必须手动进入装饰器闭合:
orig = _sftp_command_with_retries.__closure__[1].cell_contents
(索引0处的闭包是retry_decorator
在调用retry()
自身时产生的)。
需要注意的是decorator
被列为依赖retry
包的元数据,如果你安装它pip
的decorator
包会被自动安装。
您可以使用try...except
:
try:
orig = _sftp_command_with_retries.__wrapped__
except AttributeError:
# decorator.decorator not available and not Python 3.2 or newer.
orig = _sftp_command_with_retries.__closure__[1].cell_contents
请注意,您 始终 可以time.sleep()
使用模拟补丁。装饰器代码将使用该模拟程序,因为它引用了模块源代码中的“全局”time
模块。
或者,您可以修补retry.api.__retry_internal
:
import retry.api
def dontretry(f, *args, **kw):
return f()
with mock.patch.object(retry.api, '__retry_internal', dontretry):
# use your decorated method