Commits

Ronald Oussoren  committed 458305b

fix py 3.3 test failure for modulegraph.util.imp_walk

Also deprecates this function because it is no longer used.

  • Participants
  • Parent commits a9a13c3

Comments (0)

Files changed (3)

File doc/changelog.rst

 * Ensure modulegraph works with changes introduced after
   Python 3.3b1.
 
+* ``modulegraph.util.imp_walk`` is deprecated and will be 
+  removed in the next release of this package.
+
 0.9.2
 -----
 

File modulegraph/find_modules.py

 
 import modulegraph.modulegraph as modulegraph
 from modulegraph.modulegraph import Alias
-from modulegraph.util import imp_find_module, imp_walk
+from modulegraph.util import imp_find_module
 
 __all__ = [
     'find_modules', 'parse_mf_results'

File modulegraph/util.py

 import imp
 import sys
 import re
+import marshal
+import warnings
 
 try:
     unicode
 except NameError:
     unicode = str
 
+
+if sys.version_info[0] == 2:
+    from StringIO import StringIO as BytesIO
+    from StringIO import StringIO
+
+else:
+    from io import BytesIO, StringIO
+
+
+
 def imp_find_module(name, path=None):
     """
     same as imp.find_module, but handles dotted names
 
     raise ImportError if a name can not be found.
     """
+    warnings.warn("imp_walk will be removed in a future version", DeprecationWarning)
+
     if name in sys.builtin_module_names:
         yield name, (None, None, ("", "", imp.C_BUILTIN))
         return
     for namepart in name.split('.'):
         for path_item in paths:
             res = _check_importer_for_path(namepart, path_item)
-            if hasattr(res, 'find_module'):
+            if hasattr(res, 'load_module'):
+                if res.path.endswith('.py') or res.path.endswith('.pyw'):
+                    fp = StringIO(res.get_source(namepart))
+                    res = (fp, res.path, ('.py', 'rU', imp.PY_SOURCE))
+                elif res.path.endswith('.pyc') or res.path.endswith('.pyo'):
+                    co  = res.get_code(namepart)
+                    fp = BytesIO(imp.get_magic() + b'\0\0\0\0' + marshal.dumps(co))
+                    res = (fp, res.path, ('.pyc', 'rb', imp.PY_COMPILED))
+
+                else:
+                    res = (None, loader.path, (os.path.splitext(loader.path)[-1], 'rb', imp.C_EXTENSION))
+
                 break
             elif isinstance(res, tuple):
                 break