[Python-Dev] New Import Hooks PEP, a first draft (and req. for
PEP #)
James C. Ahlstrom
jim@interet.com
Fri, 20 Dec 2002 15:22:22 -0500
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()
It would look like this:
suffix = [(".exe", "rb", 0)]
file, path, descr = imp.find_module("wininst", __path__, suffix)
data = file.read()
file.close()
return data
Note that for a zip file, __file__ is something like
C:/Python/lib/zarchive.zip/subdir/mymod.pyc
and nothing is going to make code using __file__ Just Work.
Well, maybe you could strip the last component "mymod.pyc" and
use it as __path__ in a call to imp.find_module(). Not too
hard, and it works with files and zip archives. But maybe not
with future ftp:// imported modules.
Your code knows the directory. My version would search in your
package __path__ for it. But you only have one wininst.exe per
package, right? And maybe putting it on package path is a feature.
JimA