Example #1
0
 def test_lookForSource(self):
     """
     If a .py file exists for a loaded module, use it as the module path. If not, use __file__.
     """
     import pickle, datetime
     self.assertTrue(modules.getModule('pickle').filePath.path.endswith('.py'))
     self.assertFalse(modules.getModule('datetime').filePath.path.endswith('.py'))
Example #2
0
 def test_lookForSource(self):
     """
     If a .py file exists for a loaded module, use it as the module path. If not, use __file__.
     """
     import pickle, datetime
     self.assertTrue(
         modules.getModule('pickle').filePath.path.endswith('.py'))
     self.assertFalse(
         modules.getModule('datetime').filePath.path.endswith('.py'))
Example #3
0
    def test_dottedNames(self):
        """
        Verify that the walkModules APIs will give us back subpackages, not just
        subpackages.

        XXX Docstring makes no sense.
        """
        self.assertEquals(
            modules.getModule('exocet.test'),
            self.findByIteration("exocet.test",
                                 where=modules.getModule('exocet')))
Example #4
0
    def test_dottedNames(self):
        """
        Verify that the walkModules APIs will give us back subpackages, not just
        subpackages.

        XXX Docstring makes no sense.
        """
        self.assertEquals(
            modules.getModule('exocet.test'),
            self.findByIteration("exocet.test",
                                 where=modules.getModule('exocet')))
Example #5
0
 def test_moduleExportProblems(self):
     """
     C{SyntaxError} is raised when doing inspection of module
     exports if __all__ is not a single list of string literals.
     """
     self.packagePath.child("e.py").setContent("__all__ = ['a' + 'b']")
     self.packagePath.child("f.py").setContent("__all__ = ['a']\n__all__ = ['a', 'b']")
     self._setupSysPath()
     modinfo1 = modules.getModule(self.packageName + ".e")
     modinfo2 = modules.getModule(self.packageName + ".f")
     self.assertRaises(SyntaxError, lambda: list(modinfo1.iterExportNames()))
     self.assertRaises(SyntaxError, lambda: list(modinfo2.iterExportNames()))
Example #6
0
 def test_moduleExportProblems(self):
     """
     C{SyntaxError} is raised when doing inspection of module
     exports if __all__ is not a single list of string literals.
     """
     self.packagePath.child("e.py").setContent("__all__ = ['a' + 'b']")
     self.packagePath.child("f.py").setContent(
         "__all__ = ['a']\n__all__ = ['a', 'b']")
     self._setupSysPath()
     modinfo1 = modules.getModule(self.packageName + ".e")
     modinfo2 = modules.getModule(self.packageName + ".f")
     self.assertRaises(SyntaxError,
                       lambda: list(modinfo1.iterExportNames()))
     self.assertRaises(SyntaxError,
                       lambda: list(modinfo2.iterExportNames()))
Example #7
0
 def __init__(self, packageMaker, mapper, getModule):
     """
     @param packageMaker: A L{modules._modules.PythonModule} object
     representing a package.
     @param mapper: A L{Mapper}.
     @param getModule: A function to fetch module maker objects, given an
     FQN. (For example, {twisted.python.}modules.getModule.)
     """
     self.packageMaker = packageMaker
     self.mapper = _PackageMapper(packageMaker.name, mapper)
     initModule = _buildAndStoreEmptyModule(packageMaker, self.mapper)
     if '.' in packageMaker.name:
         modulePath = packageMaker.name.split(".")
         if not self.mapper.contains(modulePath[0]):
             parentMaker = getModule(modulePath[0])
             parent = _buildAndStoreEmptyModule(parentMaker, self.mapper)
             self.mapper.add(modulePath[0], parent)
             for seg in modulePath[1:-1]:
                 new = _buildAndStoreEmptyModule(parentMaker[seg],
                                                 self.mapper)
                 setattr(parent, seg, new)
                 parent = new
             setattr(parent, modulePath[-1], initModule)
     self.allModules  = analyzePackage(self.packageMaker)
     sortedModules = robust_topological_sort(self.allModules)
     self.loadOrder = list(itertools.chain(*sortedModules))
     self.loadOrder.reverse()
     trace("load __init__", vars(initModule), id(initModule))
     self._reallyLoadModule(packageMaker.name, initModule)
Example #8
0
 def test_pathEntriesOnPath(self):
     """
     Verify that path entries discovered via module loading are, in fact, on
     sys.path somewhere.
     """
     for n in ["os", "exocet", "exocet.test", "exocet.test.test_modules"]:
         self.failUnlessIn(modules.getModule(n).pathEntry.filePath.path, sys.path)
Example #9
0
 def __init__(self, packageMaker, mapper, getModule):
     """
     @param packageMaker: A L{modules._modules.PythonModule} object
     representing a package.
     @param mapper: A L{Mapper}.
     @param getModule: A function to fetch module maker objects, given an
     FQN. (For example, {twisted.python.}modules.getModule.)
     """
     self.packageMaker = packageMaker
     self.mapper = _PackageMapper(packageMaker.name, mapper)
     initModule = _buildAndStoreEmptyModule(packageMaker, self.mapper)
     if '.' in packageMaker.name:
         modulePath = packageMaker.name.split(".")
         if not self.mapper.contains(modulePath[0]):
             parentMaker = getModule(modulePath[0])
             parent = _buildAndStoreEmptyModule(parentMaker, self.mapper)
             self.mapper.add(modulePath[0], parent)
             for seg in modulePath[1:-1]:
                 new = _buildAndStoreEmptyModule(parentMaker[seg],
                                                 self.mapper)
                 setattr(parent, seg, new)
                 parent = new
             setattr(parent, modulePath[-1], initModule)
     self.allModules = analyzePackage(self.packageMaker)
     sortedModules = robust_topological_sort(self.allModules)
     self.loadOrder = list(itertools.chain(*sortedModules))
     self.loadOrder.reverse()
     trace("load __init__", vars(initModule), id(initModule))
     self._reallyLoadModule(packageMaker.name, initModule)
Example #10
0
 def test_modulesShowsUp(self):
     """
     Scrounge around in the top-level module namespace and make sure that
     L{modules} shows up, and that the module thusly obtained is the same as
     the module that we find when we look for it explicitly by name.
     """
     self.assertEquals(modules.getModule("exocet._modules"), self.findByIteration("exocet._modules"))
Example #11
0
 def _listModules(self):
     pkginfo = modules.getModule(self.packageName)
     nfni = [
         modinfo.name.split(".")[-1] for modinfo in pkginfo.iterModules()
     ]
     nfni.sort()
     self.failUnlessEqual(nfni, ['a', 'b', 'c__init__', 'd'])
Example #12
0
 def test_modulesShowsUp(self):
     """
     Scrounge around in the top-level module namespace and make sure that
     L{modules} shows up, and that the module thusly obtained is the same as
     the module that we find when we look for it explicitly by name.
     """
     self.assertEquals(modules.getModule('exocet._modules'),
                       self.findByIteration("exocet._modules"))
Example #13
0
 def test_moduleExportAll(self):
     """
     If __all__ is defined as a list of string literals, the names
     in it are used as the list of the module's exports.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".b")
     self.assertEqual(sorted(modinfo.iterExportNames()), sorted(["foo"]))
Example #14
0
 def test_moduleExportDefinedNames(self):
     """
     The exports of a module with no __all__ are all its defined
     names.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".a")
     self.assertEqual(sorted(modinfo.iterExportNames()), sorted(["foo", "doFoo", "Foo"]))
Example #15
0
 def test_moduleExportAll(self):
     """
     If __all__ is defined as a list of string literals, the names
     in it are used as the list of the module's exports.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".b")
     self.assertEqual(sorted(modinfo.iterExportNames()), sorted(["foo"]))
Example #16
0
 def test_pathEntriesOnPath(self):
     """
     Verify that path entries discovered via module loading are, in fact, on
     sys.path somewhere.
     """
     for n in ['os', 'exocet', 'exocet.test', 'exocet.test.test_modules']:
         self.failUnlessIn(
             modules.getModule(n).pathEntry.filePath.path, sys.path)
Example #17
0
 def test_moduleExportDefinedNames(self):
     """
     The exports of a module with no __all__ are all its defined
     names.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".a")
     self.assertEqual(sorted(modinfo.iterExportNames()),
                      sorted(["foo", "doFoo", "Foo"]))
Example #18
0
 def test_loadPackagesAndModules(self):
     """
     Verify that we can locate and load packages, modules, submodules, and
     subpackages.
     """
     for n in ["os", "exocet", "exocet.test", "exocet.test.test_modules"]:
         m = namedAny(n)
         self.failUnlessIdentical(modules.getModule(n).load(), m)
         self.failUnlessIdentical(self.findByIteration(n).load(), m)
Example #19
0
 def test_loadPackagesAndModules(self):
     """
     Verify that we can locate and load packages, modules, submodules, and
     subpackages.
     """
     for n in ['os', 'exocet', 'exocet.test', 'exocet.test.test_modules']:
         m = namedAny(n)
         self.failUnlessIdentical(modules.getModule(n).load(), m)
         self.failUnlessIdentical(self.findByIteration(n).load(), m)
Example #20
0
 def test_exportedImports(self):
     """
     If __all__ mentions imports, they're included in the collection of defined names.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".d")
     self.assertEqual(sorted([x.name.rsplit('.', 1)[1] for x in
                              modinfo.iterAttributes()]),
                      sorted(["foo", "registerAdapter", "reflect"]))
Example #21
0
 def test_exportedImports(self):
     """
     If __all__ mentions imports, they're included in the collection of defined names.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".d")
     self.assertEqual(
         sorted(
             [x.name.rsplit('.', 1)[1] for x in modinfo.iterAttributes()]),
         sorted(["foo", "registerAdapter", "reflect"]))
Example #22
0
 def test_moduleImportNames(self):
     """
     The fully qualified names imported by a module can be inspected.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".a")
     self.assertEquals(sorted(modinfo.iterImportNames()),
                      sorted(["sys", "os", "datetime",
                              "twisted.python.reflect",
                              "twisted.python.filepath",
                              "twisted.python.components.registerAdapter"]))
Example #23
0
 def test_moduleAttributes(self):
     """
     Module attributes can be iterated over without executing the code.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".a")
     attrs = sorted(modinfo.iterAttributes(), key=lambda a: a.name)
     names = sorted(["foo", "doFoo", "Foo"])
     for attr, name in zip(attrs, names):
         self.assertEquals(attr.onObject, modinfo)
         self.assertFalse(attr.isLoaded())
         self.assertEquals(attr.name, modinfo.name + "." + name)
         self.assertRaises(NotImplementedError, lambda: list(attr.iterAttributes()))
Example #24
0
 def test_moduleImportNames(self):
     """
     The fully qualified names imported by a module can be inspected.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".a")
     self.assertEquals(
         sorted(modinfo.iterImportNames()),
         sorted([
             "sys", "os", "datetime", "twisted.python.reflect",
             "twisted.python.filepath",
             "twisted.python.components.registerAdapter"
         ]))
Example #25
0
 def _underUnderPathTest(self, doImport=True):
     moddir2 = self.mktemp()
     fpmd = FilePath(moddir2)
     fpmd.createDirectory()
     fpmd.child("foozle.py").setContent("x = 123\n")
     self.packagePath.child("__init__.py").setContent("__path__.append(%r)\n" % (moddir2,))
     # Cut here
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName)
     self.assertEquals(
         self.findByIteration(self.packageName + ".foozle", modinfo, importPackages=doImport), modinfo["foozle"]
     )
     self.assertEquals(modinfo["foozle"].load().x, 123)
Example #26
0
 def test_moduleAttributes(self):
     """
     Module attributes can be iterated over without executing the code.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".a")
     attrs = sorted(modinfo.iterAttributes(), key=lambda a: a.name)
     names = sorted(["foo", "doFoo", "Foo"])
     for attr, name in zip(attrs, names):
         self.assertEquals(attr.onObject, modinfo)
         self.assertFalse(attr.isLoaded())
         self.assertEquals(attr.name, modinfo.name + '.' + name)
         self.assertRaises(NotImplementedError,
                           lambda: list(attr.iterAttributes()))
Example #27
0
def loadPackageNamed(fqn, mapper):
    """
    Analyze a Python package to determine its internal and external
    dependencies and produce an object capable of creating modules
    that refer to each other.

    @param fqn: The fully qualified name of a Python module, e.g
    C{twisted.python.filepath}.

    @param mapper: A L{Mapper}.

    @returns: A package loader object.
    """
    pkg = getModule(fqn)
    return loadPackage(pkg, mapper)
Example #28
0
def loadPackageNamed(fqn, mapper):
    """
    Analyze a Python package to determine its internal and external
    dependencies and produce an object capable of creating modules
    that refer to each other.

    @param fqn: The fully qualified name of a Python module, e.g
    C{twisted.python.filepath}.

    @param mapper: A L{Mapper}.

    @returns: A package loader object.
    """
    pkg = getModule(fqn)
    return loadPackage(pkg, mapper)
Example #29
0
 def _underUnderPathTest(self, doImport=True):
     moddir2 = self.mktemp()
     fpmd = FilePath(moddir2)
     fpmd.createDirectory()
     fpmd.child("foozle.py").setContent("x = 123\n")
     self.packagePath.child("__init__.py").setContent(
         "__path__.append(%r)\n" % (moddir2, ))
     # Cut here
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName)
     self.assertEquals(
         self.findByIteration(self.packageName + ".foozle",
                              modinfo,
                              importPackages=doImport), modinfo['foozle'])
     self.assertEquals(modinfo['foozle'].load().x, 123)
Example #30
0
 def test_loadedModuleAttributes(self):
     """
     Module attributes can be iterated over after the module has been loaded.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".a")
     modinfo.load()
     attrs = sorted([a for a in modinfo.iterAttributes() if "_" not in a.name], key=lambda a: a.name)
     names = sorted(["foo", "doFoo", "Foo"])
     for attr, name in zip(attrs, names):
         self.assertEquals(attr.onObject, modinfo)
         self.assertTrue(attr.isLoaded())
         self.assertEquals(attr.name, modinfo.name + "." + name)
         if name == "Foo":
             classattrs = [a.name for a in attr.iterAttributes()]
             self.assertIn(modinfo.name + ".Foo.x", classattrs)
Example #31
0
def loadNamed(fqn, mapper, m=None):
    """
    Load a Python module, eliminating as much of its access to global state as
    possible. If a package name is given, its __init__.py is loaded.

    @param fqn: The fully qualified name of a Python module, e.g
    C{twisted.python.filepath}.

    @param mapper: A L{Mapper}.

    @param m: An optional empty module object to load code into. (For
    resolving circular module imports.)

    @returns: An instance of the module name requested.
    """
    maker = getModule(fqn)
    return load(maker, mapper, m=m)
Example #32
0
    def test_attributeLoading(self):
        """
        Calling .load() on a L{PythonAttribute} loads the module it's a part of and returns the attribute value.
        """
        self._setupSysPath()
        modinfo = modules.getModule(self.packageName + ".a")
        attr = [x for x in modinfo.iterAttributes() if x.name.endswith("foo")][0]

        mod = attr.onObject
        self.assertFalse(attr.isLoaded())
        self.assertFalse(mod.isLoaded())
        val = attr.load()
        self.assertTrue(attr.isLoaded())
        self.assertTrue(mod.isLoaded())
        self.assertEquals(val, 123)
        self.assertEquals(attr.pythonValue, 123)
        self.assertEquals(attr.load(), 123)
Example #33
0
def loadNamed(fqn, mapper, m=None):
    """
    Load a Python module, eliminating as much of its access to global state as
    possible. If a package name is given, its __init__.py is loaded.

    @param fqn: The fully qualified name of a Python module, e.g
    C{twisted.python.filepath}.

    @param mapper: A L{Mapper}.

    @param m: An optional empty module object to load code into. (For
    resolving circular module imports.)

    @returns: An instance of the module name requested.
    """

    maker = getModule(fqn)
    return load(maker, mapper, m=m)
Example #34
0
 def test_loadedModuleAttributes(self):
     """
     Module attributes can be iterated over after the module has been loaded.
     """
     self._setupSysPath()
     modinfo = modules.getModule(self.packageName + ".a")
     modinfo.load()
     attrs = sorted(
         [a for a in modinfo.iterAttributes() if '_' not in a.name],
         key=lambda a: a.name)
     names = sorted(["foo", "doFoo", "Foo"])
     for attr, name in zip(attrs, names):
         self.assertEquals(attr.onObject, modinfo)
         self.assertTrue(attr.isLoaded())
         self.assertEquals(attr.name, modinfo.name + '.' + name)
         if name == "Foo":
             classattrs = [a.name for a in attr.iterAttributes()]
             self.assertIn(modinfo.name + '.Foo.x', classattrs)
Example #35
0
    def test_attributeLoading(self):
        """
        Calling .load() on a L{PythonAttribute} loads the module it's a part of and returns the attribute value.
        """
        self._setupSysPath()
        modinfo = modules.getModule(self.packageName + ".a")
        attr = [x for x in modinfo.iterAttributes()
                if x.name.endswith('foo')][0]

        mod = attr.onObject
        self.assertFalse(attr.isLoaded())
        self.assertFalse(mod.isLoaded())
        val = attr.load()
        self.assertTrue(attr.isLoaded())
        self.assertTrue(mod.isLoaded())
        self.assertEquals(val, 123)
        self.assertEquals(attr.pythonValue, 123)
        self.assertEquals(attr.load(), 123)
Example #36
0
    def test_nonexistentPaths(self):
        """
        Verify that L{modules.walkModules} ignores entries in sys.path which
        do not exist in the filesystem.
        """
        existentPath = self.pathEntryWithOnePackage()

        nonexistentPath = FilePath(self.mktemp())
        self.failIf(nonexistentPath.exists())

        self.replaceSysPath([existentPath.path])

        expected = [modules.getModule("test_package")]

        beforeModules = list(modules.walkModules())
        sys.path.append(nonexistentPath.path)
        afterModules = list(modules.walkModules())

        self.assertEquals(beforeModules, expected)
        self.assertEquals(afterModules, expected)
Example #37
0
    def test_nonexistentPaths(self):
        """
        Verify that L{modules.walkModules} ignores entries in sys.path which
        do not exist in the filesystem.
        """
        existentPath = self.pathEntryWithOnePackage()

        nonexistentPath = FilePath(self.mktemp())
        self.failIf(nonexistentPath.exists())

        self.replaceSysPath([existentPath.path])

        expected = [modules.getModule("test_package")]

        beforeModules = list(modules.walkModules())
        sys.path.append(nonexistentPath.path)
        afterModules = list(modules.walkModules())

        self.assertEquals(beforeModules, expected)
        self.assertEquals(afterModules, expected)
Example #38
0
 def _listModules(self):
     pkginfo = modules.getModule(self.packageName)
     nfni = [modinfo.name.split(".")[-1] for modinfo in pkginfo.iterModules()]
     nfni.sort()
     self.failUnlessEqual(nfni, ["a", "b", "c__init__", "d"])