我正在阅读bottle源代码并看到:
eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<py3fix>', 'exec'))
我阅读了有关 compile
的文档它只告诉我 <string>
是常用的。我也在 stackoverflow 上进行了谷歌搜索,但找不到相关信息。
谁能告诉我怎么做<py3fix>
影响编译吗?还有其他文件名吗?在哪里可以找到相关文档?
提前致谢。
最佳答案
完全不影响。它只是一个名称,用于标识编译后的代码来自何处,因此您可以使用您想要的字符串。
就像文档所说:
compile(source, filename, mode[, flags[, dont_inherit]])
The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('< string>' is commonly used).
在 source
的情况下没有从文件中读取(如这里),他们建议您使用 <string>
这样您就知道该代码是从书面字符串编译而来的。
提交代码的人,在修复一些Bottle Python 2/3 bugs时这样做的。所以我猜测他使用了 <py3fix>
作为识别断言是从 def _raise
提出的一种方法当用户运行 2.x 时他进行编译:
>>> eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<py3fix>', 'exec'))
>>> _raise(Exception, "error message", None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<py3fix>", line 1, in _raise
Exception: error message
>>> eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<my_source_file>', 'exec'))
>>> _raise(Exception, "error message", None)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<my_source_file>", line 1, in _raise
Exception: error message
关于python - <py3fix> 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247277/