开发者

python import hooks: accessing the full dotted module name before processing

开发者 https://www.devze.com 2023-02-20 19:23 出处:网络
I understand the fact that the importer protocol operates at the level of individual imports. However, in my case, it would be really helpful if I could know the full \"dotted module name\" BEFORE ful

I understand the fact that the importer protocol operates at the level of individual imports. However, in my case, it would be really helpful if I could know the full "dotted module name" BEFORE fully experiencing it one path element at time (that is, "a priori" if I'm allowed to say that) from my import hook.

class myimporter(object):
    def find_module(self,fullname,path=None):
        if fullname == 'rootnamespace':
            #at this moment I need to know the full dotted module name
            #that I am going to process (e.g. rootnamespace.foo.bar.baz)

    def load_module(self,fullname):
         pass

Is there a way (hopefully safe and sound) to know beforehand the full "dotted module name" from within an import hook?


--- more infos, probably not really needed (so you can skip straight to answering me ;) ), however:

If the reader wonders why would I need that or just maybe a little bit more info is to be helpful: I want to import perl modules in python. How strange does this sound? :) In reality, the import process will just make available a python stub for a perl module. The stub is generated by introspecting the perl package and see what it has to offer; I wont enter in the details of implementation here:

import perlmod.www.mechanize as wwwmech
mymech = wwwmech() #return a python proxy object to be used with later calls
mymech.get(uri) #returns another proxy object to the newly generated HTTP::Response perl object
#etc etc

my problem lies at

import perlmod.www.mechanize

because I need to know beforehand if the requested module does really exist in the system's perl installation, without being forced to wait the full module name so I can take the decision if I should fail or not. Moreover, if the user does:

import perlmod.www

I have no chance to get back and say "wait wait wait, this is wrong" after loading a www stub package. My hook won't be called after that.

Basically, when my import hook is called for "www" I must already know if the import will fail or succ开发者_开发问答eed for the full dotted module name. I will stop here with the details, please don't forget the real question :)

Thank you very much, even only for reading this.

0

精彩评论

暂无评论...
验证码 换一张
取 消