This stupid code works for modules, but not for packages. It probably has bugs.
import marshal, types
class StringImporter:
def __init__(self, old_import, modules):
self._import = old_import
self._modules = modules
def __call__(self, name, *args):
module = self._modules.get(name, None)
if module is None:
return self._import(name, *args)
code = marshal.loads(module)
mod = types.ModuleType(name)
exec code in mod.__dict__
return mod
def test():
import __builtin__
__builtin__.__import__ = StringImporter(__builtin__.__import__,
{ 'test_importer': open("/usr/lib/python2.3/os.pyc").read()[8:] })
import test_importer
print test_importer.path.join("a", "b")
print test_importer.__doc__
if __name__ == '__main__': test()
pgpkB79URBafp.pgp
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list
