Fix a SyntaxError on Python 2.6 and early versions of 2.7.

Review Request #10113 — Created Aug. 3, 2018 and submitted — Latest diff uploaded

Information

kgb
master
a6ecfaf...

Reviewers

kgb

exec() in Python has had two forms over the years:

  • exec 'code' in globals, locals
  • exec('code', globals, locals)

The first form was the original, and the second was added early in
Python's life (and is now the only allowed form in 3.x). Unfortunately,
when executing inside a function that contains a subfunction (or when
defining a subfunction in the executed code), some versions of Python
prior to 2.7.9 had an issue factoring in the local variables and would
throw a SyntaxError stating:

SyntaxError: unqualified exec is not allowed in function '__init__' it
contains a nested function with free variables

We can avoid this by using eval(compile(..., 'exec')), which will
provide the results we want in a way that's compatible with all versions
of Python.

Tested on Python 2.6 through 3.7.

    Loading...