<py3fix>是什么意思?

2024-09-30 14:36:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读瓶子源代码,看到:

eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<py3fix>', 'exec'))

我阅读了关于compile的文档,它只告诉我<string>是常用的。我也搜索了stackoverflow,但找不到相关信息。你知道吗

有人能告诉我<py3fix>是如何影响编译的吗?还有其他文件名吗?我在哪里可以找到相关的文档?你知道吗

提前谢谢。你知道吗


Tags: 文档信息瓶子string源代码文件名defeval
1条回答
网友
1楼 · 发布于 2024-09-30 14:36:07

一点也不影响。它只是一个名称,用于标识编译代码的来源,因此可以使用所需的字符串。你知道吗

就像医生说的:

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>作为一种方法来确定断言是从用户运行2.x时他编译的def _raise中产生的:

>>> 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

相关问题 更多 >