Exemple #1
0
    def test_alwaysPreferPy(self):
        """
        Verify that .py files will always be preferred to .pyc files, regardless of
        directory listing order.
        """
        mypath = FilePath(self.mktemp())
        mypath.createDirectory()
        pp = modules.PythonPath(sysPath=[mypath.path])
        originalSmartPath = pp._smartPath

        def _evilSmartPath(pathName):
            o = originalSmartPath(pathName)
            originalChildren = o.children

            def evilChildren():
                # normally this order is random; let's make sure it always
                # comes up .pyc-first.
                x = originalChildren()
                x.sort()
                x.reverse()
                return x

            o.children = evilChildren
            return o

        mypath.child("abcd.py").setContent('\n')
        compileall.compile_dir(mypath.path, quiet=True)
        # sanity check
        self.assertEquals(len(mypath.children()), 2)
        pp._smartPath = _evilSmartPath
        self.assertEquals(pp['abcd'].filePath, mypath.child('abcd.py'))
Exemple #2
0
 def test_unimportablePackageGetItem(self):
     """
     If a package has been explicitly forbidden from importing by setting a
     C{None} key in sys.modules under its name,
     L{modules.PythonPath.__getitem__} should still be able to retrieve an
     unloaded L{modules.PythonModule} for that package.
     """
     shouldNotLoad = []
     path = modules.PythonPath(
         sysPath=[self.pathEntryWithOnePackage().path],
         moduleLoader=shouldNotLoad.append,
         importerCache={},
         sysPathHooks={},
         moduleDict={'test_package': None})
     self.assertEquals(shouldNotLoad, [])
     self.assertEquals(path['test_package'].isLoaded(), False)
Exemple #3
0
 def test_inconsistentImporterCache(self):
     """
     If the path a module loaded with L{PythonPath.__getitem__} is not
     present in the path importer cache, a warning is emitted, but the
     L{PythonModule} is returned as usual.
     """
     space = modules.PythonPath([], sys.modules, [], {})
     thisModule = space[__name__]
     warnings = self.flushWarnings([self.test_inconsistentImporterCache])
     self.assertEquals(warnings[0]['category'], UserWarning)
     self.assertEquals(
         warnings[0]['message'],
         FilePath(modules.__file__).parent().dirname() + " (for module " +
         __name__ + ") not in path importer cache "
         "(PEP 302 violation - check your local configuration).")
     self.assertEquals(len(warnings), 1)
     self.assertEquals(thisModule.name, __name__)
Exemple #4
0
 def test_packageMissingPath(self):
     """
     A package can delete its __path__ for some reasons,
     C{modules.PythonPath} should be able to deal with it.
     """
     mypath = FilePath(self.mktemp())
     mypath.createDirectory()
     pp = modules.PythonPath(sysPath=[mypath.path])
     subpath = mypath.child("abcd")
     subpath.createDirectory()
     subpath.child("__init__.py").setContent('del __path__\n')
     sys.path.append(mypath.path)
     import abcd
     try:
         l = list(pp.walkModules())
         self.assertEquals(len(l), 1)
         self.assertEquals(l[0].name, 'abcd')
     finally:
         del abcd
         del sys.modules['abcd']
         sys.path.remove(mypath.path)
Exemple #5
0
    def test_unhandledImporter(self):
        """
        Make sure that the behavior when encountering an unknown importer
        type is not catastrophic failure.
        """
        class SecretImporter(object):
            pass

        def hook(name):
            return SecretImporter()

        syspath = ['example/path']
        sysmodules = {}
        syshooks = [hook]
        syscache = {}

        def sysloader(name):
            return None

        space = modules.PythonPath(syspath, sysmodules, syshooks, syscache,
                                   sysloader)
        entries = list(space.iterEntries())
        self.assertEquals(len(entries), 1)
        self.assertRaises(KeyError, lambda: entries[0]['module'])