Python 计算 gotos 和 USE_COMPUTED_GOTOS?

Python computed gotos and USE_COMPUTED_GOTOS?

自 Python 3.2 起,默认启用计算 goto:documentation

Computed gotos are now enabled by default on supported compilers (which are detected by the configure script). They can still be disabled selectively by specifying --without-computed-gotos.

但是在 Mac 上,安装的 Python 没有启用计算的 gotos。

# Tested on macOS 10.15.3, with pyenv installed Python 3.7, 3.8

import sysconfig

sysconfig.get_config_var('USE_COMPUTED_GOTOS') # outputs: 0   
sysconfig.get_config_var('HAVE_COMPUTED_GOTOS')  # outputs: 1

我猜是因为使用的编译器(默认为Clang)不支持这个功能。有没有办法绕过它?


更新:我为这个问题创建了 bpo-40790

首先,我在问题中做出了错误的假设。事实证明,Python 确实在 Mac 上启用了计算 goto。我放了一个 simple script 可以可靠地检测它。

现在我们有一个新问题:USE_COMPUTED_GOTOSHAVE_COMPUTED_GOTOS 有什么关系?

结论是:

sysconfig.get_config_var('USE_COMPUTED_GOTOS')sysconfig.get_config_var('HAVE_COMPUTED_GOTOS') 都不能指示计算的 goto 是否实际启用。

为了演示它,我手动编译了没有标志的 CPython 3.7 和 3.8,--without-computed-gotos--with-computed-gotos,然后使用上述脚本测试不同安装之间的行为。结果如下:

根据结果和我的猜测,似乎

  • USE_COMPUTED_GOTOS:用于检测编译时是否设置了--with-computed-gotos
  • HAVE_COMPUTED_GOTOS: 检测使用的编译器是否支持

在 Windows,因为 MSVC doesn't support label as value,行为是有道理的。

我也阅读了配置脚本和 ceval.c,但仍然不完全理解一切是如何工作的。