[Python-ideas] Changing optimisation level from a script

Guido van Rossum guido at python.org
Fri Sep 9 13:22:02 EDT 2016


What is to stop you from adding this to micropython as a library
extension? I've never felt the urge to do this and I don't think I've
ever heard it requested before. If you think it should be in the
stdlib, given the timing of the 3.6 feature freeze the earliest time
it could land would be Python 3.7. Finally, let's not define APIs with
British spelling. :-)

On Thu, Sep 8, 2016 at 9:35 PM, Damien George <damien.p.george at gmail.com> wrote:
> 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.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list