[Python-Dev] #12982: Should -O be required to *read* .pyo files?

Terry Reedy tjreedy at udel.edu
Thu Jun 14 03:54:30 CEST 2012


On 6/13/2012 8:55 PM, Steven D'Aprano wrote:
> On Wed, Jun 13, 2012 at 01:58:10PM -0400, R. David Murray wrote:
>
>> So, is there any reason to not use the .pyo file (if that's all that is
>> around) when -O is not specified?
>
> .pyo and .pyc files have potentially different semantics. Right now,
> .pyo files don't include asserts, so that's one difference right there.
> In the future there may be more aggressive optimizations.
>
> Good practice is to never write an assert that actually changes the
> semantics of your program, but in practice people don't write asserts
> correctly, e.g. they use them for checking user-input or function
> parameters.
>
> So, no, we

You mean the interpreter?

> should never use

Do you mean import or execute?
Current, the interpreter executes any bytecode that gets imported.

> .pyo files unless explicitly told to do so,

What constitutes 'explicitly told to do so'? Currently, an 'optimized' 
file written as .pyo gets imported (and hence executed) if
1) the interpreter is started with -O
2) a custom importer ignores the absence of -O
3) someone renames x.pyo to x.pyc.

> since doing so risks breaking poorly-written but otherwise working code.

Agreed, though a slightly different issue. Would you somehow disable 2) 
or 3) if not considered 'explicit' enough?

-- 
Terry Jan Reedy



More information about the Python-Dev mailing list