[Python-Dev] PEP 451 update

Eric Snow ericsnowcurrently at gmail.com
Wed Oct 30 23:54:13 CET 2013


On Wed, Oct 30, 2013 at 4:09 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 31 Oct 2013 03:41, "Eric Snow" <ericsnowcurrently at gmail.com> wrote:
>> Our recent discovery about reloading should probably be reflected in
>> the signature of finder.find_spec():
>>
>>   MetaPathFinder.find_spec(name, path=None, existing=None)
>>   PathEntryFinder.find_spec(name, existing=None)
>>
>> This way the finder has an opportunity to incorporate information from
>> an existing spec into the spec it returns.  reload() would make use of
>> this by passing module.__spec__ (or None if the module has no
>> __spec__) to _bootstrap._find_spec().
>>
>> This approach should also address what you are looking for.  I'd
>> prefer it over passing the existing spec to exec_module().  The module
>> (and its __spec__) should have everything exec_module() needs to do
>> its job.
>
> Yes, that should work.

Cool.  I'll update the PEP.

>
>> We would still need to use loader.supports_reload() in reload().
>
> Why? If the reload isn't supported, exec_module can just throw an exception
> based on the loader state in the spec.

At the point that exec_module() gets called, the loader can't check
sys.modules to see if it's a reload or not.  As a workaround, the
finder could set up some loader state to indicate to the loader that
it's a reload and then the loader, during exec_module(), would check
that and act accordingly.  However, that's the sort of boilerplate
that PEP 451 is trying to offload onto the import machinery.  With
Loader.supports_reload() it's a lot cleaner.

-eric

>
> From the import system's point of view "reload not permitted" is no
> different from any other exec time failure.
>
> Cheers,
> Nick.


More information about the Python-Dev mailing list