def fmod(name, path, parent): if path is None: if name in sys.builtin_module_names: return (None, None, ("", "", imp.C_BUILTIN)) path = mf.path fp, buf, stuff = find_module(name, path) if buf: buf = os.path.realpath(buf) return (fp, buf, stuff)
def test_find_module(self): for path in ('syspath', 'syspath.zip', 'syspath.egg'): path = os.path.join(os.path.dirname(TESTDATA), path) if os.path.exists(os.path.join(path, 'mymodule.pyc')): os.unlink(os.path.join(path, 'mymodule.pyc')) # Plain module info = modulegraph.find_module('mymodule', path=[path] + sys.path) fp = info[0] filename = info[1] description = info[2] self.assertTrue(hasattr(fp, 'read')) if path.endswith('.zip') or path.endswith('.egg'): # Zip importers may precompile if filename.endswith('.py'): self.assertEqual(filename, os.path.join(path, 'mymodule.py')) self.assertEqual(description, ('.py', 'rU', imp.PY_SOURCE)) else: self.assertEqual(filename, os.path.join(path, 'mymodule.pyc')) self.assertEqual(description, ('.pyc', 'rb', imp.PY_COMPILED)) else: self.assertEqual(filename, os.path.join(path, 'mymodule.py')) self.assertEqual(description, ('.py', 'rU', imp.PY_SOURCE)) # Compiled plain module, no source if path.endswith('.zip') or path.endswith('.egg'): self.assertRaises(ImportError, modulegraph.find_module, 'mymodule2', path=[path] + sys.path) else: info = modulegraph.find_module('mymodule2', path=[path] + sys.path) fp = info[0] filename = info[1] description = info[2] self.assertTrue(hasattr(fp, 'read')) 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) # # fp = info[0] # filename = info[1] # description = info[2] # # self.assertTrue(hasattr(fp, 'read')) # # if sys.version_info[:2] >= (3,2): # self.assertEqual(filename, os.path.join(path, '__pycache__', 'mymodule3.cpython-32.pyc')) # else: # self.assertEqual(filename, os.path.join(path, 'mymodule3.pyc')) # self.assertEqual(description, ('.pyc', 'rb', imp.PY_COMPILED)) # Package info = modulegraph.find_module('mypkg', path=[path] + sys.path) fp = info[0] filename = info[1] description = info[2] self.assertEqual(fp, None) self.assertEqual(filename, os.path.join(path, 'mypkg')) self.assertEqual(description, ('', '', imp.PKG_DIRECTORY)) # Extension if path.endswith('.zip'): self.assertRaises(ImportError, modulegraph.find_module, 'myext', path=[path] + sys.path) else: info = modulegraph.find_module('myext', path=[path] + sys.path) fp = info[0] filename = info[1] description = info[2] if sys.platform == 'win32': ext = '.pyd' else: # This is a ly, but is good enough for now ext = '.so' self.assertEqual(filename, os.path.join(path, 'myext' + ext)) self.assertEqual(description, (ext, 'rb', imp.C_EXTENSION)) self.assertEqual(fp, None)