Attachment 'zipextimporter-debug-0.6.9.py'

Download

   1 r"""zipextimporter - an importer which can import extension modules from zipfiles
   2 
   3 This file and also _memimporter.pyd is part of the py2exe package.
   4 
   5 Overview
   6 ========
   7 
   8 zipextimporter.py contains the ZipExtImporter class which allows to
   9 load Python binary extension modules contained in a zip.archive,
  10 without unpacking them to the file system.
  11 
  12 Call the zipextimporter.install() function to install the import hook,
  13 add a zip-file containing .pyd or .dll extension modules to sys.path,
  14 and import them.
  15 
  16 It uses the _memimporter extension which uses code from Joachim
  17 Bauch's MemoryModule library.  This library emulates the win32 api
  18 function LoadLibrary.
  19 
  20 Sample usage
  21 ============
  22 
  23 You have to prepare a zip-archive 'lib.zip' containing
  24 your Python's _socket.pyd for this example to work.
  25 
  26 >>> import zipextimporter
  27 >>> zipextimporter.install()
  28 >>> import sys
  29 >>> sys.path.insert(0, "lib.zip")
  30 >>> import _socket
  31 >>> print _socket
  32 <module '_socket' from 'lib.zip\_socket.pyd'>
  33 >>> _socket.__file__
  34 'lib.zip\\_socket.pyd'
  35 >>> _socket.__loader__
  36 <ZipExtensionImporter object 'lib.zip'>
  37 >>> # Reloading also works correctly:
  38 >>> _socket is reload(_socket)
  39 True
  40 >>>
  41 
  42 """
  43 import imp, sys
  44 import zipimport
  45 import _memimporter
  46 
  47 class ZipExtensionImporter(zipimport.zipimporter):
  48     _suffixes = [s[0] for s in imp.get_suffixes() if s[2] == imp.C_EXTENSION]
  49 
  50     def find_module(self, fullname, path=None):
  51         result = zipimport.zipimporter.find_module(self, fullname, path)
  52         if result:
  53             if _memimporter.get_verbose_flag() > 2:
  54                 sys.stderr.write("find_module: found %s by standard zipimport\n" % (fullname))
  55             return result
  56         if fullname in ("pywintypes", "pythoncom"):
  57             fullname = fullname + "%d%d" % sys.version_info[:2]
  58             fullname = fullname.replace(".", "\\") + ".dll"
  59             if fullname in self._files:
  60                 if _memimporter.get_verbose_flag() > 2:
  61                     sys.stderr.write("find_module: found %s by py2exe hack\n" % (fullname))
  62                 return self
  63         else:
  64             fullname = fullname.replace(".", "\\")
  65             for s in self._suffixes:
  66                 if (fullname + s) in self._files:
  67                     if _memimporter.get_verbose_flag() > 2:
  68                         sys.stderr.write("find_module: found %s by py2exe with %s extension\n" % (fullname, s))
  69                     return self
  70         if _memimporter.get_verbose_flag() > 2:
  71             sys.stderr.write("find_module: not found %s by py2exe\n" % (fullname))
  72         return None
  73 
  74     def locate_dll_image(self, name):
  75         # A callback function for_memimporter.import_module.  Tries to
  76         # locate additional dlls.  Returns the image as Python string,
  77         # or None if not found.
  78         if name in self._files:
  79             if _memimporter.get_verbose_flag() > 2:
  80                 sys.stderr.write("locate_dll_image: %s - yes\n" % (name))
  81             return self.get_data(name)
  82         if _memimporter.get_verbose_flag() > 2:
  83             sys.stderr.write("locate_dll_image: %s - no\n" % (name))
  84         return None
  85 
  86     def load_module(self, fullname):
  87         if sys.modules.has_key(fullname):
  88             mod = sys.modules[fullname]
  89             if _memimporter.get_verbose_flag():
  90                 sys.stderr.write("load_module: import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
  91             return mod
  92         _memimporter.set_find_proc(self.locate_dll_image)
  93         try:
  94             return zipimport.zipimporter.load_module(self, fullname)
  95         except zipimport.ZipImportError:
  96             if _memimporter.get_verbose_flag() > 2:
  97                 sys.stderr.write("load_module: standard zipimport failed\n")
  98             pass
  99         if _memimporter.get_verbose_flag() > 2:
 100             sys.stderr.write("load_module: %s is not loaded by standard zipimport\n" % (fullname))
 101         initname = "init" + fullname.split(".")[-1] # name of initfunction
 102         filename = fullname.replace(".", "\\")
 103         if filename in ("pywintypes", "pythoncom"):
 104             filename = filename + "%d%d" % sys.version_info[:2]
 105             suffixes = ('.dll',)
 106         else:
 107             suffixes = self._suffixes
 108         for s in suffixes:
 109             path = filename + s
 110             if path in self._files:
 111                 if _memimporter.get_verbose_flag():
 112                     sys.stderr.write("load_module: found %s in zipfile %s\n" % (path, self.archive))
 113                 code = self.get_data(path)
 114                 mod = _memimporter.import_module(code, initname, fullname, path)
 115                 mod.__file__ = "%s\\%s" % (self.archive, path)
 116                 mod.__loader__ = self
 117                 if _memimporter.get_verbose_flag():
 118                     sys.stderr.write("load_module: %s is loaded from zipfile %s\n" % (fullname, mod.__file__))
 119                 return mod
 120         raise zipimport.ZipImportError, "can't find module %s" % fullname
 121 
 122     def __repr__(self):
 123         return "<%s object %r>" % (self.__class__.__name__, self.archive)
 124 
 125 def install():
 126     "Install the zipextimporter"
 127     sys.path_hooks.insert(0, ZipExtensionImporter)
 128     sys.path_importer_cache.clear()
 129 
 130 ##if __name__ == "__main__":
 131 ##    import doctest
 132 ##    doctest.testmod()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-02-02 19:30:31, 5.3 KB) [[attachment:zipextimporter-debug-0.6.9.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.