imputils problem

Rainer Deyke root at rainerdeyke.com
Sun Dec 10 13:30:47 EST 2000


"Magnus Heino" <magnus.heino at rivermen.se> wrote in message
news:90viai$slg$1 at merlin.rembrandtstr.bocholt.de...
>
> > I've been having a problem with imputils: a module imported through a
> > custom
> > importer seems unable in import .pyd modules.  In particular, if I
import
> > the zipfile module though a custom importer, it is unable to find the
zlib
> > module.  Does anybody here know how to fix this?
>
> I have it working, but I had to tweek imputils.py a bit.

Strangely enough, the problem went away when I moved zlib.pyd from the DLLs
subdirectory to the main directory of my program (which is a C++ program
with embedded Python 2.0).  The question: why did this change anything?
Does the default __import__ search directories that are not in sys.path?

> How are you doing things?

I took inspiration from the "small" Python distribution and provided a
custom site.py:

--- start.py ---
import marshal
import imputil
import cStringIO
import sys
import struct


class ArchiveImporter(imputil.Importer):

  def __init__(self, name):
    self.file = open(name, 'rb')
    (pos,) = struct.unpack('<l', self.file.read(4))
    self.file.seek(pos)
    self.toc = marshal.load(self.file)

  def get_code(self, parent, modname, fqname):
    name = fqname.replace('.', '/')
    if self.toc.has_key(name):
      return (0, self.__load(name), {})
    elif self.toc.has_key(name + '/__init__'):
      return (1, self.__load(name + '/__init__'), {})
    else:
      return None

  def __load(self, name):
    self.file.seek(self.toc[name])
    return marshal.load(self.file)


if __name__ != '__main__':
  imputil.ImportManager().install()
  sys.path.append(imputil.BuiltinImporter())
  sys.path.append(ArchiveImporter('code'))

--- end site.py ---



--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list