469,599 Members | 2,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,599 developers. It's quick & easy.

can't reload with PEP 302 import hooks

Hi, I'm using import hooks according to PEP 302, in order to load
python files from a game PAK file. The game is C++ using embedded and
extended Python (v2.33) and Boost. The importing works fine, but
after modules are imported I can't reload them. I've tried
'reload(foo)' and 'PyImport_ReloadModule(pModPtr)', but both return
'ImportError: No module named foo'.
Is it safe to assume that reload doesn't respect the import hook?
That seems like a problem. Is there any work-around?


Here is my hook object code:

class myImportHandler:
def find_module(self, fullname, path=None):
return self # returns the loader object
def _get_code(self, fullname): # load code from game PAK
if (fullname in sys.builtin_module_names):
return False,None
codeString = loadImportModule(fullname) # CALLING C++ FUNCTION
bIsPackage = False # not using PACKAGES
if len(codeString):
codeString2 = codeString.replace('\\r\','\')
code = compile(codeString2, fullname, 'exec')
return bIsPackage,code
def normal_import(self, fullname): # for built-ins
return imp.load_module(fullname,file,pathname,desc)
def load_module(self, fullname):
mod = sys.modules[fullname] # check if module is
already loaded in sys.module?
except KeyError:
ispkg, code = self._get_code(fullname)
if code==None: # failed importing from game
return self.normal_import(fullname)
mod = imp.new_module(fullname) # create new module
sys.modules[fullname] = mod
mod.__file__ = '<%s>' % self.__class__.__name__
mod.__loader__ = self
if ispkg:
mod.__path__ = []
exec code in mod.__dict__
return mod
Jul 18 '05 #1
0 1105

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

66 posts views Thread by Ellinghaus, Lance | last post: by
reply views Thread by Carlos Ribeiro | last post: by
5 posts views Thread by Steve Holden | last post: by
4 posts views Thread by Lonnie Princehouse | last post: by
23 posts views Thread by Shane Hathaway | last post: by
3 posts views Thread by John Salerno | last post: by
1 post views Thread by bernhard.voigt | last post: by
4 posts views Thread by Patrick C | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.