python - <py3fix> 是什么意思?

标签 python compilation bottle

我正在阅读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/

相关文章:

c++ - 尝试编译 C++ 程序后出错

javascript - Bottle 不识别 Axios.js json post

c - 可以强制可执行文件使用用户定义的 'malloc' 吗?

java - IntelliJ 2018.1 首选项中的 “Use ' --release' 选项是什么?

python - Jython & Bottle : SSL-enabled web server

python - 哪个网络服务器与 Bottle 一起使用?

python - pandas2ri.ri2py_dataframe(r_dataframe) 返回 float 而不是 ISO-8601 (YYYY-MM-DD) 格式的日期

OS X 与 Ubuntu 上的 Python asyncio 性能

python - 使用嵌套 for 循环附加到多个列表

python - 如何将图片从 url 上传到 Blobstore?