Commits

Ronald Oussoren committed a9a13c3

Fix a number of test failures. All tests now pass for py 2.7 and 3.2, still have some 3.3 failures

  • Participants
  • Parent commits 9243710

Comments (0)

Files changed (2)

File modulegraph/modulegraph.py

 
 if sys.version_info[0] == 2:
     from StringIO import StringIO as BytesIO
+    from StringIO import StringIO 
     def _Bchr(value):
         return chr(value)
 
 else:
-    from io import BytesIO
+    from io import BytesIO, StringIO
 
     def _Bchr(value):
         return value
     # - In setuptools 0.7 and later there's _pkgutil.ImpImporter
     # - In earlier setuptools versions you pkg_resources.ImpWrapper
     #
-    # This is a bit of a hack, should check if we can just rely on
+    # XXX: This is a bit of a hack, should check if we can just rely on
     # PEP302's get_code() method with all recent versions of pkgutil and/or
     # setuptools (setuptools 0.6.latest, setuptools trunk and python2.[45])
+    #
+    # For python 3.3 this code should be replaced by code using importlib,
+    # for python 3.2 and 2.7 this should be cleaned up a lot.
     try:
         from pkgutil import ImpImporter
     except ImportError:
 
                 return (None, filename, description)
 
-        elif hasattr(loader, 'get_code'):
-            co = loader.get_code(name)
-            fp = _code_to_file(co)
+        if hasattr(loader, 'path'):
+            if loader.path.endswith('.pyc') or loader.path.endswith('.pyo'):
+                fp = open(loader.path, 'rb')
+                description = ('.pyc', 'rb', imp.PY_COMPILED)
+                return (fp, loader.path, description)
+
+
+        if hasattr(loader, 'path') and hasattr(loader, 'get_source'):
+            source = loader.get_source(name)
+            fp = StringIO(source)
+            co = None
 
         else:
-            fp = None
-            co = None
+            source = None
+
+        if source is None:
+            if hasattr(loader, 'get_code'):
+                co = loader.get_code(name)
+                fp = _code_to_file(co)
+
+            else:
+                fp = None
+                co = None
 
         pathname = os.path.join(entry, *name.split('.'))
 
 
         if co is None:
             if loader.path.endswith('.py') or loader.path.endswith('.pyw'):
-                pathname = pathname + '.py'
-                return (fp, pathname, ('.py', 'rU', imp.PY_SOURCE))
+                return (fp, loader.path, ('.py', 'rU', imp.PY_SOURCE))
             else:
                 return (None, loader.path, (os.path.splitext(loader.path)[-1], 'rb', imp.C_EXTENSION))
 
         else:
-            return (fp, loader.path, ('.pyc', 'rb', imp.PY_COMPILED))
+            if hasattr(loader, 'path'):
+                return (fp, loader.path, ('.pyc', 'rb', imp.PY_COMPILED))
+            else:
+                return (fp, pathname + '.pyc', ('.pyc', 'rb', imp.PY_COMPILED))
 
     raise ImportError(name)
 
                 contents += '\n'
 
 
-            print(pathname)
-            print(repr(contents))
-
             co = compile(contents, pathname, 'exec', 0, True)
             cls = SourceModule
 

File modulegraph_tests/test_modulegraph.py

                 self.assertEqual(filename, os.path.join(path, 'mymodule2.pyc'))
                 self.assertEqual(description, ('.pyc', 'rb', imp.PY_COMPILED))
 
+                fp.close()
+
             # Compiled plain module, with source
 #            info = modulegraph.find_module('mymodule3', path=[path] + sys.path)
 #
         d = dict(klass.__dict__)
         del d['__doc__']
         del d['__module__']
+        if '__qualname__' in d:
+            # New in Python 3.3
+            del d['__qualname__']
         self.assertEqual(d, {})
 
     def assertHasExactMethods(self, klass, *methods):
         d = dict(klass.__dict__)
         del d['__doc__']
         del d['__module__']
+        if '__qualname__' in d:
+            # New in Python 3.3
+            del d['__qualname__']
 
         for nm in methods:
             self.assertTrue(nm in d, "%s doesn't have attribute %r"%(klass, nm))