473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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?

thanks

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')
else:
code=None
return bIsPackage,code
def normal_import(self, fullname): # for built-ins
file,pathname,desc=imp.find_module(fullname)
return imp.load_module(fullname,file,pathname,desc)
def load_module(self, 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_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 1290

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Ellinghaus, Lance | last post by:
> > Other surprises: Deprecating reload() >Reload doesn't work the way most people think >it does: if you've got any references to the old module, >they stay around. They aren't replaced. ...
0
by: Carlos Ribeiro | last post by:
I am looking for information on packages & import hooks, including simple examples on how to implement a simple import hook. Quick googling turns out some documents, such as: -- PEP 302:...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
4
by: Lonnie Princehouse | last post by:
So, it turns out that reload() fails if the module being reloaded isn't in sys.path. Maybe it could fall back to module.__file__ if the module isn't found in sys.path?? .... or reload could...
23
by: Shane Hathaway | last post by:
Here's a heretical idea. I'd like a way to import modules at the point where I need the functionality, rather than remember to import ahead of time. This might eliminate a step in my coding...
3
by: John Salerno | last post by:
I understand that after you import something once, you can reload it to pick up new changes. But does reload work with from statements? I tried this: from X import * and then did my testing....
3
by: alf | last post by:
Hi, wonder if in the python I could treat modules imorts like classes instances. It means I could import it twice or more times under different names. -- alfz1
1
by: bernhard.voigt | last post by:
Hey! I'm using ipython as my python shell and often run scripts with the magic command %run: In : %run script.py If modules are loaded within the script these are not reloaded when I rerun...
4
by: Patrick C | last post by:
Someone suggested I use reload when trying to reopen a script i've written (the suggestion was made in response to the problem ive reposted below) however when i do reload(modolename) i only get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.