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

Just van Rossum just@letterror.com
Fri, 20 Dec 2002 20:14:01 +0100


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)

Just

PS: please take care to not Cc to peps@python.org any more in this
thread, I think I've done enough harm ;-)