[Python-Dev] PEP 451 update (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Wed Oct 30 23:09:44 CET 2013


On 31 Oct 2013 03:41, "Eric Snow" <ericsnowcurrently at gmail.com> wrote:

On Tue, Oct 29, 2013 at 7:29 PM, Nick Coghlan <ncoghlan at gmail.com> wrote: > OK, time for me to stop trying to remember the details of the problem > I'm trying to solve and go look them up in the source code :) > > One of my goals here is to be able to migrate extension loading from > the old API to the new plugin API. That means being able to break up > the existing loadmodule implementation: > > http://hg.python.org/cpython/file/1787277915e9/Python/importdl.c#l23 > > For loading, that's a fairly straightforward createmodule/execmodule > split, but reloading gets a bit more interesting. > > Specifically, I'd like to be able to implement the relevant parts of > PyImportFindExtensionObject as a precheck for reloading: > > http://hg.python.org/cpython/file/1787277915e9/Python/import.c#l533 > > That means just having access to the module name isn't enough: the > extensions dictionary is keyed by a (name, filename) 2-tuple rather > than just by the module name. Using the findspec API, that filename > would be stored in the loader state on the spec object rather than > being looked up anew during the load operation. > > However, rereading this method also indicates that I really want to > know in execmodule whether this is a reload or not, since extension > loading needs to handle reloads differently from initial execution. > > So I'm back to my original preference: I'd like the previous spec to > be passed to execmodule in the reloading case. If reloading is not > supported at all, it's up to the loader to throw an appropriate > exception when the the previous spec is not None. If loading and > reloading doesn't make any difference, then they can just ignore it. > But when both are supported, but handled differently (as for extension > modules), then that can be detected, and the information from the old > spec (including the original loader and loader state) is available if > needed. Our recent discovery about reloading should probably be reflected in the signature of finder.findspec(): MetaPathFinder.findspec(name, path=None, existing=None) PathEntryFinder.findspec(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.findspec(). This approach should also address what you are looking for. I'd prefer it over passing the existing spec to execmodule(). The module (and its spec) should have everything execmodule() needs to do its job.

Yes, that should work.

We would still need to use loader.supportsreload() in reload().

Why? If the reload isn't supported, exec_module can just throw an exception based on the loader state in the spec.

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

Cheers, Nick. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20131031/c9f2411f/attachment.html>



More information about the Python-Dev mailing list