1

Since Python 3.2, Computed gotos are enabled by default: 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.

However on Mac, installed Python does not have computed gotos enabled.

# 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

I'm guessing it's because the compiler used (Clang by default) doesn't support this feature. Is there anyway to get around it?


Update: I created bpo-40790 for this issue.

laike9m
  • 18,344
  • 20
  • 107
  • 140
  • I'm under the impression clang optimizes the switch statement into something equivalent to computed gotos so, for clang at least, this might be 0 unless explicitly configured with it. – Dimitris Fasarakis Hilliard May 18 '20 at 17:07

1 Answers1

1

First and foremost, I made the wrong assumption in the question. It turns out that, Python does have computed gotos enabled on Mac. I put up a simple script that can detect it in a reliable way.

Now we have a new question: what's all about USE_COMPUTED_GOTOS and HAVE_COMPUTED_GOTOS?

The conclusion is:

Neither sysconfig.get_config_var('USE_COMPUTED_GOTOS') or sysconfig.get_config_var('HAVE_COMPUTED_GOTOS') can indicate whether computed gotos is actually enabled.

To demonstrate it, I manaully compiled CPython 3.7 and 3.8 with no flag, --without-computed-gotos and --with-computed-gotos, then test the bebavior between different installations using the aforementioned script. Here's the result:

Based on the result and my guesses, it seems

  • USE_COMPUTED_GOTOS: is for detecting whether --with-computed-gotos is set when compiling.
  • HAVE_COMPUTED_GOTOS: is for detecting whether the compiler used supports it.

On Windows, since MSVC doesn't support label as value, the behavior makes sense.

I also read the configure script and ceval.c, but still don't fully understand how everything works.

laike9m
  • 18,344
  • 20
  • 107
  • 140