如何编写与 Python 2 和 Python 3 兼容的异常重新引发代码? [英] How to write exception reraising code that's compatible with both Python 2 and Python 3?
问题描述
我正在尝试使我的 WSGI 服务器实现与 Python 2 和 Python 3 兼容.我有以下代码:
I'm trying to make my WSGI server implementation compatible with both Python 2 and Python 3. I had this code:
def start_response(status, response_headers, exc_info = None):
if exc_info:
try:
if headers_sent:
# Re-raise original exception if headers sent.
raise exc_info[0], exc_info[1], exc_info[2]
finally:
# Avoid dangling circular ref.
exc_info = None
elif headers_set:
raise AssertionError("Headers already set!")
headers_set[:] = [status, response_headers]
return write
...相关部分是:
# Re-raise original exception if headers sent.
raise exc_info[0], exc_info[1], exc_info[2]
Python 3 不再支持该语法,因此必须将其转换为:
Python 3 doesn't support that syntax anymore so it must be translated to:
raise exc_info[0].with_traceback(exc_info[1], exc_info[2])
问题:Python 2 语法在 Python 3 中生成解析错误.如何编写可以被 Python 2 和 Python 3 解析的代码?我尝试了以下方法,但不起作用:
Problem: the Python 2 syntax generates a parse error in Python 3. How do I write code that can be parsed by both Python 2 and Python 3? I've tried the following, but that doesn't work:
if sys.version_info[0] >= 3:
raise exc_info[0].with_traceback(exc_info[1], exc_info[2])
else:
eval("raise exc_info[0], exc_info[1], exc_info[2]; 1", None, { 'exc_info': exc_info })
推荐答案
你可以做一些有创意的事情.
You could do something creative.
在代码的开头进行检查 - 您的构造函数或其他任何东西,检查您使用的 Python 版本,因为您的普通版本检查器不起作用,请尝试以下操作:
Have a check at the start of your code - your constructor or whatever, check what version of python you are using, since your normal version checker is not working, try this instead:
try:
eval('a python 3 expression') # something that only works in python3+
python_version = 3
except:
python_version = 2
然后您的其余代码可以轻松地引用它以了解要使用的内容.
Then the rest of your code can easily just reference this to know what to use.
至于解析错误,你可以在函数中使用 exec,如下所示:
As for the parse errors, you can use exec in a function, like so:
def what_to_run():
if python_version = 3:
return 'raise exc_info[0].with_traceback(exc_info[1], exc_info[2])'
else:
return 'raise exc_info[0], exc_info[1], exc_info[2]'
在你的函数中你会这样写:
In your function you would write this:
def start_response(status, response_headers, exc_info = None):
if exc_info:
try:
if headers_sent:
# Re-raise original exception if headers sent.
exec(what_to_run())
finally:
# Avoid dangling circular ref.
exc_info = None
elif headers_set:
raise AssertionError("Headers already set!")
headers_set[:] = [status, response_headers]
return write
有点乱,未经测试,但它应该工作,至少你理解这个想法.
A bit messy, untested, but it should work, at least you understand the idea.
这篇关于如何编写与 Python 2 和 Python 3 兼容的异常重新引发代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!