[Python-Dev] How to interpret get_code from PEP 302?

Paul Moore p.f.moore at gmail.com
Tue Aug 21 14:40:51 CEST 2007


On 21/08/07, Brett Cannon <brett at python.org> wrote:
> PEP 302 ("New Import Hooks") has an optional extensions section so
> that tools like py2exe and py2app have an easier time.  Part of the
> optional extensions is the method get_code that is to return the code
> object for the specified method (if the loader can handle it).
>
> But there is a lack in the definition of how get_code is supposed to
> be implemented.  The definition says that the "method should return
> the code object associated with the module", which is fine.  But then
> it goes on to state that "If the loader doesn't have the code object
> but it _does_ have the source code, it should return the compiled
> source code".  This throws me as this makes it sound like bytecode
> does not need to be used if the loader does not already have a code
> object and there is no source to be had; any bytecode can be ignored.
>
> Now I doubt this is how it is supposed to be read.  Does anyone
> disagree with that?  If not, I will change the wording to mention that
> bytecode must be used if no source is available (and that the magic
> number must be verified).

Hmm, yes, that's muddled. Maybe it made sense to me when I wrote it
:-) (I think it was my wording rather than Just's)

get_code must *always* return the same code object that
loader.load_module is using - whether that be bytecode or compiled
source (and it must respect things like file timestamps where
appropriate just like load_module does). What the sentence you quote
is trying to say is that if there's a need to compile source, the
get_code method must do this on behalf of the caller - it can't return
None and expect the caller to try get_source and compile it manually.
Someone who only wants a code object should never need to call
get_source.

I'm not sure that's any clearer! If you need further clarification,
let me know (either on or off list). I'd appreciate it if you can
clear the PEP's wording up.

Paul.


More information about the Python-Dev mailing list