[Python-Dev] New Import Hooks PEP, a first draft (and req. for PEP #)

Thomas Heller theller@python.net
20 Dec 2002 20:46:05 +0100


Just van Rossum <just@letterror.com> writes:

> Thomas Heller wrote:
> 
> > But then, how would this (working) code have to be written with the
> > new imp.find_module() function:
> > 
> >     def get_exe_bytes (self):
> >         # wininst.exe is in the same directory as this file
> >         directory = os.path.dirname(__file__)
> >         filename = os.path.join(directory, "wininst.exe")
> >         return open(filename, "rb").read()
> 
> With the importer protocol (as I imagine it now, this doesn't match
> zipimporter's current behavior yet) it could be written like this:
> 
>     def get_exe_bytes (self):
>         if os.path.isfile(__file__):
>             # wininst.exe is in the same directory as this file
>             directory = os.path.dirname(__file__)
>             filename = os.path.join(directory, "wininst.exe")
>             return open(filename, "rb").read()
>         else:
>             path = __name__.replace(".", os.sep) + os.sep + \
>                    "wininst.exe"
>             return __importer__.get_data(path)
> 
Looks ok to me, although I would prefer to write

        path = os.sep.join(__name__.split(".") + ["wininst.exe"])
or
        path = os.path.join(*__name__.split(".") + ["wininst.exe"])

I just wanted to make sure that I don't have to construct
the arguments for imp.find_module() from __file__.
> 
> PS: please take care to not Cc to peps@python.org any more in this
> thread, I think I've done enough harm ;-)

Sorry for that, I noticed it as it was already too late.

Thomas