]> granicus.if.org Git - python/commitdiff
Some tests did not pass on repeated calls (regrtest -R::)
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 18 Apr 2008 23:31:33 +0000 (23:31 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 18 Apr 2008 23:31:33 +0000 (23:31 +0000)
Perform additional cleanup, mostly deleting from sys.modules, or clearing the warnings registry.

Lib/test/regrtest.py
Lib/test/test_frozen.py
Lib/test/test_pkg.py
Lib/test/test_pkgutil.py
Lib/test/test_profile.py
Lib/test/test_structmembers.py
Lib/test/test_warnings.py

index 7ea8668d94c8bb9a79e8b88be6e7c287bed6f7df..b092bd93b744e67d5bbd2419d41977c317d57897 100755 (executable)
@@ -683,6 +683,11 @@ def dash_R_cleanup(fs, ps, pic, abcs):
     import struct, filecmp
     from distutils.dir_util import _path_created
 
+    # Clear the warnings registry, so they can be displayed again
+    for mod in sys.modules.values():
+        if hasattr(mod, '__warningregistry__'):
+            del mod.__warningregistry__
+
     # Restore some original values.
     warnings.filters[:] = fs
     copy_reg.dispatch_table.clear()
index e981fb3ddb772049b941398d5a78d56a267f1e9a..4835e475f859e86bcb71e57469c2ba23ac20525c 100644 (file)
@@ -35,6 +35,10 @@ class FrozenTests(unittest.TestCase):
         self.assertEquals(stdout.getvalue(),
                           'Hello world...\nHello world...\nHello world...\n')
 
+        del sys.modules['__hello__']
+        del sys.modules['__phello__']
+        del sys.modules['__phello__.spam']
+
 
 def test_main():
     run_unittest(FrozenTests)
index 4fa367f56001d222b1cd2affead7836b6d66ee11..3a954ca3663c01d9c4084608acf5253c74cf2f6d 100644 (file)
@@ -46,12 +46,20 @@ class Test(unittest.TestCase):
 
     def setUp(self):
         self.root = None
+        self.pkgname = None
         self.syspath = list(sys.path)
 
     def tearDown(self):
         sys.path[:] = self.syspath
         cleanout(self.root)
 
+        # delete all modules concerning the tested hiearchy
+        if self.pkgname:
+            modules = [name for name in sys.modules
+                       if self.pkgname in name.split('.')]
+            for name in modules:
+                del sys.modules[name]
+
     def run_code(self, code):
         exec(textwrap.dedent(code), globals(), {"self": self})
 
@@ -74,6 +82,8 @@ class Test(unittest.TestCase):
                     f.write('\n')
                 f.close()
         self.root = root
+        # package name is the name of the first item
+        self.pkgname = descr[0][0]
 
     def test_1(self):
         hier = [("t1", None), ("t1 __init__"+os.extsep+"py", "")]
@@ -223,8 +233,8 @@ class Test(unittest.TestCase):
 
     def test_7(self):
         hier = [
-                ("t7"+os.extsep+"py", ""),
                 ("t7", None),
+                ("t7"+os.extsep+"py", ""),
                 ("t7 __init__"+os.extsep+"py", ""),
                 ("t7 sub"+os.extsep+"py",
                  "raise RuntimeError('Shouldnt load sub.py')"),
index 4381f911bb482ecbcc5d7912ab6636888fd38868..a14f09187a5156b3718267977d2300e6353ce5ce 100644 (file)
@@ -48,6 +48,8 @@ class PkgutilTests(unittest.TestCase):
         res2 = pkgutil.get_data(pkg, 'sub/res.txt')
         self.assertEqual(res2, RESOURCE_DATA)
 
+        del sys.modules[pkg]
+
     def test_getdata_zipfile(self):
         zip = 'test_getdata_zipfile.zip'
         pkg = 'test_getdata_zipfile'
@@ -74,6 +76,8 @@ class PkgutilTests(unittest.TestCase):
         self.assertEqual(res2, RESOURCE_DATA)
         del sys.path[0]
 
+        del sys.modules[pkg]
+
 class PkgutilPEP302Tests(unittest.TestCase):
 
     class MyTestLoader(object):
index c8281c3436425ea38e7bc756e2b8ea98230011ee..0bd2530cf2abf5dd847716960e370ff3364f091f 100755 (executable)
@@ -21,8 +21,9 @@ class ProfileTest(unittest.TestCase):
     def do_profiling(cls):
         results = []
         prof = cls.profilerclass(timer, 0.001)
+        start_timer = timer()
         prof.runctx("testfunc()", globals(), locals())
-        results.append(timer())
+        results.append(timer() - start_timer)
         for methodname in cls.methodnames:
             s = StringIO()
             stats = pstats.Stats(prof, stream=s)
@@ -33,7 +34,7 @@ class ProfileTest(unittest.TestCase):
 
     def test_cprofile(self):
         results = self.do_profiling()
-        self.assertEqual(results[0], 43000)
+        self.assertEqual(results[0], 1000)
         for i, method in enumerate(self.methodnames):
             self.assertEqual(results[i+1], self.expected_output[method],
                              "Stats.%s output for %s doesn't fit expectation!" %
index a59cfa952f4e3daa541eb010a5aacd461a8dfe60..6c1a947fe3f1959ed4fd492fe2bc0b4b0d7a4eaf 100644 (file)
@@ -101,12 +101,6 @@ class TestWarnings(unittest.TestCase):
 
 
 def test_main(verbose=None):
-    # Obscure hack so that this test passes after reloads or repeated calls
-    # to test_main (regrtest -R).
-    if '__warningregistry__' in globals():
-        del globals()['__warningregistry__']
-    if hasattr(sys, '__warningregistry__'):
-        del sys.__warningregistry__
     test_support.run_unittest(__name__)
 
 if __name__ == "__main__":
index fb6aabbd35dbc7f225d7f85f991e678c053d9c2c..9fca0802473f94eb22ae02e2ec6a7d1edb7869e1 100644 (file)
@@ -391,6 +391,8 @@ class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests):
 
 
 def test_main():
+    py_warnings.onceregistry.clear()
+    c_warnings.onceregistry.clear()
     test_support.run_unittest(CFilterTests,
                                 PyFilterTests,
                                 CWarnTests,