python - 使 python 3 异常向后兼容

标签 python python-3.x syntax compatibility python-2.x

此代码块(来自 PEP 3109)的向后(2.6,如果可能)兼容语法是什么:

try:
   self.spawn(pp_args)
except DistutilsExecError as msg:
   raise CompileError from msg

最佳答案

这与您在 python-2.x 中的表现一样接近:

try:
   self.spawn(pp_args)
except DistutilsExecError as msg:
    print "DistutilsExecError : " + str(DistutilsExecError(msg))
    print
    print "The above exception was the direct cause of the following exception:"
    raise CompileError

关于python - 使 python 3 异常向后兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125039/

相关文章:

mysql - 使用或不使用 CONSTRAINT

python - 猜词器无法打印足够的字符

python - 值错误: time data '67:01' does not match format '%M:%S'

python - 为什么 Virtualenv 继续使用全局 python 二进制文件?

Objective-C 变量声明困惑

javascript - 为什么要避免 JavaScript 中的递增 ("++") 和递减 ("--") 运算符?

python - 如何将 Python 源代码重新加载到 Eclipse/Pydev 的控制台窗口中?

python - 如何在 scikit learn 中使用 GP.fit 进行多维输入?

Django __init__() 需要 1 个位置参数,但给出了 2 个

python - 用 Python 制作彩色 Ascii 文本