也许最简单的解决方案之一是编写自己的函数并捆绑调用序列,@dsc(@min(...))
同时防止异常。
假设您有一个提升脚本,请将以下 here-doc 作为参数exec
嵌入其中:
exec <<EOF
if self:
def brpdsc_factory(recov):
Recoverable = recov
def brpdsc_handler(self, subarg):
if not subarg:
raise Recoverable("function @brpdsc() called with selection that resolves to empty set")
minset = set([])
descendants = set([])
try:
minset = self.min_handler(subarg)
except ValueError as e:
raise Recoverable("invalid value passed to function @min()")
try:
descendants = self.dsc_handler(minset)
except:
raise Recoverable("the implicit call to @dsc() failed")
return descendants
return brpdsc_handler
brpdsc_handler = brpdsc_factory(Recoverable)
setattr(self, 'brpdsc_handler', brpdsc_handler.__get__(self, self.__class__))
EOF
这会将本地函数植入brpdsc_handler
为同名的类成员,class RepoSurgeon
并使函数@brpdsc
立即可用于脚本或命令提示符。
工厂函数的存在是为了继承脚本Recoverable
中定义的异常类的名称,reposurgeon
但在完成后不再可用于我们的代码exec
。其他符号可以以相同的方式进行。
现在所有可能发生的是我们收到:
reposurgeon: function @brpdsc() called with selection that resolves to empty set
从 RepoSurgeon 尝试运行后@brpdsc/NO_SUCH_BRANCH/) list
。