[Python-ideas] Changing optimisation level from a script

Damien George damien.p.george at gmail.com
Fri Sep 9 00:35:23 EDT 2016


Hi all,

When starting CPython from the command line you can pass the -O option
to enable optimisations (eg `assert 0` won't raise an exception when
-O is passed).  But, AFAIK, there is no way to change the optimisation
level after the interpreter has started up, ie there is no Python
function call or variable that can change the optimisation.

In MicroPython we want to be able to change the optimisation level
from within a script because (on bare metal at least) there is no
analog of passing options like -O.

My idea would be to have a function like `sys.optimise(value)` that
sets the optimisation level for all subsequent runs of the
parser/compiler.  For example:

import sys
import mymodule # no optimisations
exec('assert 0') # will raise an exception
sys.optimise(1) # enable optimisations
import myothermodule # optimisations are enabled for this (unless it's
already imported by mymodule)
exec('assert 0') # will not raise an exception

What do you think?  Sorry if this has been discussed before!

Cheers,
Damien.


More information about the Python-ideas mailing list