can't reload with PEP 302 import hooks

Discussion in 'Python' started by Mustafa Thamer, Aug 13, 2004.

  1. 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
    Mustafa Thamer, Aug 13, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gonçalo Rodrigues

    Help on import hooks

    Gonçalo Rodrigues, Nov 16, 2003, in forum: Python
    Replies:
    0
    Views:
    343
    Gonçalo Rodrigues
    Nov 16, 2003
  2. Simon Dahlbacka

    import hooks

    Simon Dahlbacka, Aug 12, 2004, in forum: Python
    Replies:
    0
    Views:
    357
    Simon Dahlbacka
    Aug 12, 2004
  3. Vinay Sajip

    Problems with import hooks and encoding

    Vinay Sajip, May 20, 2009, in forum: Python
    Replies:
    0
    Views:
    224
    Vinay Sajip
    May 20, 2009
  4. Sreejith K

    Creating Import Hooks

    Sreejith K, Feb 18, 2010, in forum: Python
    Replies:
    7
    Views:
    458
    Jonathan Gardner
    Feb 19, 2010
  5. Josh Haberman
    Replies:
    2
    Views:
    279
    Ryan Kelly
    Aug 7, 2011
Loading...

Share This Page