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_Reloa dModule(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?
thanks
Here is my hook object code:
class myImportHandler :
def find_module(sel f, fullname, path=None):
return self # returns the loader object
def _get_code(self, fullname): # load code from game PAK
if (fullname in sys.builtin_mod ule_names):
return False,None
codeString = loadImportModul e(fullname) # CALLING C++ FUNCTION
bIsPackage = False # not using PACKAGES
if len(codeString) :
codeString2 = codeString.repl ace('\\r\','\')
code = compile(codeStr ing2, fullname, 'exec')
else:
code=None
return bIsPackage,code
def normal_import(s elf, fullname): # for built-ins
file,pathname,d esc=imp.find_mo dule(fullname)
return imp.load_module (fullname,file, pathname,desc)
def load_module(sel f, fullname):
try:
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
PAK
return self.normal_imp ort(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
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_Reloa dModule(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?
thanks
Here is my hook object code:
class myImportHandler :
def find_module(sel f, fullname, path=None):
return self # returns the loader object
def _get_code(self, fullname): # load code from game PAK
if (fullname in sys.builtin_mod ule_names):
return False,None
codeString = loadImportModul e(fullname) # CALLING C++ FUNCTION
bIsPackage = False # not using PACKAGES
if len(codeString) :
codeString2 = codeString.repl ace('\\r\','\')
code = compile(codeStr ing2, fullname, 'exec')
else:
code=None
return bIsPackage,code
def normal_import(s elf, fullname): # for built-ins
file,pathname,d esc=imp.find_mo dule(fullname)
return imp.load_module (fullname,file, pathname,desc)
def load_module(sel f, fullname):
try:
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
PAK
return self.normal_imp ort(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