Beispiel #1
0
    def test_badimport(self):
        # This tests the fix for issue 5230, where if pydoc found the module
        # but the module had an internal import error pydoc would report no doc
        # found.
        modname = 'testmod_xyzzy'
        testpairs = (
            ('i_am_not_here', 'i_am_not_here'),
            ('test.i_am_not_here_either', 'i_am_not_here_either'),
            ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
            ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)),
            ('test.{}'.format(modname), modname),
            )

        @contextmanager
        def newdirinpath(dir):
            os.mkdir(dir)
            sys.path.insert(0, dir)
            yield
            sys.path.pop(0)
            rmtree(dir)

        with newdirinpath(TESTFN), EnvironmentVarGuard() as env:
            env['PYTHONPATH'] = TESTFN
            fullmodname = os.path.join(TESTFN, modname)
            sourcefn = fullmodname + os.extsep + "py"
            for importstring, expectedinmsg in testpairs:
                f = open(sourcefn, 'w')
                f.write("import {}\n".format(importstring))
                f.close()
                try:
                    result = run_pydoc(modname)
                finally:
                    forget(modname)
                expected = badimport_pattern % (modname, expectedinmsg)
                self.assertEqual(expected, result)
 def _check_module(self, depth):
     pkg_dir, mod_fname, mod_name = (self._make_pkg("x=1\n", depth))
     forget(mod_name)
     try:
         if verbose: print "Running from source:", mod_name
         d1 = run_module(mod_name)  # Read from source
         self.assertIn("x", d1)
         self.assertTrue(d1["x"] == 1)
         del d1  # Ensure __loader__ entry doesn't keep file open
         __import__(mod_name)
         os.remove(mod_fname)
         if verbose: print "Running from compiled:", mod_name
         d2 = run_module(mod_name)  # Read from bytecode
         self.assertIn("x", d2)
         self.assertTrue(d2["x"] == 1)
         del d2  # Ensure __loader__ entry doesn't keep file open
     finally:
         self._del_pkg(pkg_dir, depth, mod_name)
     if verbose: print "Module executed successfully"
Beispiel #3
0
 def _check_module(self, depth):
     pkg_dir, mod_fname, mod_name = (
            self._make_pkg("x=1\n", depth))
     forget(mod_name)
     try:
         if verbose: print "Running from source:", mod_name
         d1 = run_module(mod_name) # Read from source
         self.assertIn("x", d1)
         self.assertTrue(d1["x"] == 1)
         del d1 # Ensure __loader__ entry doesn't keep file open
         __import__(mod_name)
         os.remove(mod_fname)
         if verbose: print "Running from compiled:", mod_name
         d2 = run_module(mod_name) # Read from bytecode
         self.assertIn("x", d2)
         self.assertTrue(d2["x"] == 1)
         del d2 # Ensure __loader__ entry doesn't keep file open
     finally:
         self._del_pkg(pkg_dir, depth, mod_name)
     if verbose: print "Module executed successfully"
 def _check_package(self, depth):
     pkg_dir, mod_fname, mod_name = (self._make_pkg("x=1\n", depth,
                                                    "__main__"))
     pkg_name, _, _ = mod_name.rpartition(".")
     forget(mod_name)
     try:
         if verbose: print "Running from source:", pkg_name
         d1 = run_module(pkg_name)  # Read from source
         self.assertIn("x", d1)
         self.assertTrue(d1["x"] == 1)
         del d1  # Ensure __loader__ entry doesn't keep file open
         __import__(mod_name)
         os.remove(mod_fname)
         if not sys.dont_write_bytecode:
             if verbose: print "Running from compiled:", pkg_name
             d2 = run_module(pkg_name)  # Read from bytecode
             self.assertIn("x", d2)
             self.assertTrue(d2["x"] == 1)
             del d2  # Ensure __loader__ entry doesn't keep file open
     finally:
         self._del_pkg(pkg_dir, depth, pkg_name)
     if verbose: print "Package executed successfully"
Beispiel #5
0
 def _check_package(self, depth):
     pkg_dir, mod_fname, mod_name = (
            self._make_pkg("x=1\n", depth, "__main__"))
     pkg_name, _, _ = mod_name.rpartition(".")
     forget(mod_name)
     try:
         if verbose: print "Running from source:", pkg_name
         d1 = run_module(pkg_name) # Read from source
         self.assertIn("x", d1)
         self.assertTrue(d1["x"] == 1)
         del d1 # Ensure __loader__ entry doesn't keep file open
         __import__(mod_name)
         os.remove(mod_fname)
         if not sys.dont_write_bytecode:
             if verbose: print "Running from compiled:", pkg_name
             d2 = run_module(pkg_name) # Read from bytecode
             self.assertIn("x", d2)
             self.assertTrue(d2["x"] == 1)
             del d2 # Ensure __loader__ entry doesn't keep file open
     finally:
         self._del_pkg(pkg_dir, depth, pkg_name)
     if verbose: print "Package executed successfully"
Beispiel #6
0
    def test_badimport(self):
        # This tests the fix for issue 5230, where if pydoc found the module
        # but the module had an internal import error pydoc would report no doc
        # found.
        modname = 'testmod_xyzzy'
        testpairs = (
            ('i_am_not_here', 'i_am_not_here'),
            ('test.i_am_not_here_either', 'i_am_not_here_either'),
            ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'),
            ('i_am_not_here.{0}'.format(modname),
             'i_am_not_here.{0}'.format(modname)),
            ('test.{0}'.format(modname), modname),
        )

        @contextmanager
        def newdirinpath(dir):
            os.mkdir(dir)
            sys.path.insert(0, dir)
            yield
            sys.path.pop(0)
            rmtree(dir)

        with newdirinpath(TESTFN):
            with EnvironmentVarGuard() as env:
                env.set('PYTHONPATH', TESTFN)
                fullmodname = os.path.join(TESTFN, modname)
                sourcefn = fullmodname + os.extsep + "py"
                for importstring, expectedinmsg in testpairs:
                    f = open(sourcefn, 'w')
                    f.write("import {0}\n".format(importstring))
                    f.close()
                    try:
                        result = run_pydoc(modname)
                    finally:
                        forget(modname)
                    expected = badimport_pattern % (modname, expectedinmsg)
                    self.assertEqual(expected, result)