Attachment 'knee-import.diff'

Download

   1 --- orig/MoinMoin/util/pysupport.py
   2 +++ mod/MoinMoin/util/pysupport.py
   3 @@ -41,35 +41,51 @@
   4          Return None on failure.
   5      """
   6      if path:
   7 -        import imp
   8 -        complete_modulename = modulename
   9 +        #
  10 +        # see Python-src/Demo/imputil/knee.py how import should be done
  11 +        #
  12 +        import imp, sys
  13 +
  14          items = modulename.split('.')
  15 -        modulename = items[-1]
  16 -        del items[-1]
  17 -        
  18 -        try:
  19 -            # get the real path of the package now:
  20 -            real_path = path        
  21 -            for package_name in items:
  22 -                file = None
  23 -                try:
  24 -                    file, real_path, descriptor = imp.find_module(package_name, [real_path])
  25 -                finally:
  26 -                    if file: file.close()
  27 -        
  28 -            md_file = None
  29 +        parent = None
  30 +        real_path = [path]
  31 +        fqname = None
  32 +        for part_name in items:
  33 +            # keep full qualified module name up to date
  34 +            if fqname is None:
  35 +                fqname = part_name
  36 +            else:
  37 +                fqname = fqname + '.' + part_name
  38 +            ## this is the place to check sys.modules if the module
  39 +            ## is already available
  40 +            ## WARNING: this does not work with farm wikis, cause all
  41 +            ## farm plugin paths would map to the same 'plugin' top
  42 +            ## module!
  43 +            ## We need a dummy module ('wiki_'+sha(path)) to keep them
  44 +            ## apart (create with imp.new_module()?)
  45 +            # find & import the module
  46 +            try:
  47 +                fp, pathname, stuff = imp.find_module(part_name, real_path or parent.__path__)
  48 +            except ImportError:
  49 +                # no need to close fp here, cause its only open if found
  50 +                return None
  51              try:
  52 -                md_file, md_fname, md_fd = imp.find_module(modulename, [real_path])
  53 -                mod = imp.load_module(complete_modulename, md_file, md_fname, md_fd)
  54 -                return mod.__dict__[name]
  55 +                mod = imp.load_module(fqname, fp, pathname, stuff)
  56              finally:
  57 -                if md_file:
  58 -                    md_file.close()
  59 -        except KeyError:
  60 -            raise Exception('Something with your module directories is wrong (wrong __init__.py files?)')
  61 -        except ImportError:
  62 -            return None
  63 +                if fp: fp.close()
  64 +            # update parent module up to date
  65 +            if parent is None:
  66 +                # we only need real_path for the first import, after
  67 +                # this parent.__path__ is enough
  68 +                real_path = None
  69 +            else:
  70 +                setattr(parent, part_name, mod)
  71 +            parent = mod
  72 +        return getattr(mod, name, None)
  73      else:
  74 +        # this part is for MoinMoin imports, we use __import__
  75 +        # which will also use sys.modules as cache, but thats
  76 +        # no harm cause MoinMoin is global anyway.
  77          try:
  78              module = __import__(modulename, globals(), {}, [name]) # {} was: locals()
  79          except ImportError:

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-10-18 13:16:08, 0.1 KB) [[attachment:TestTest.txt]]
  • [get | view] (2004-10-19 11:19:14, 3.0 KB) [[attachment:knee-import.diff]]
  • [get | view] (2004-10-18 13:04:42, 0.5 KB) [[attachment:test.py]]
  • [get | view] (2004-10-19 11:19:43, 2.8 KB) [[attachment:test_pysupport.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.