Index: Lib/test/test_py3kwarn.py =================================================================== --- Lib/test/test_py3kwarn.py (revision 77727) +++ Lib/test/test_py3kwarn.py (working copy) @@ -8,6 +8,14 @@ if not sys.py3kwarning: raise unittest.SkipTest('%s must be run with the -3 flag' % __name__) +try: + from test.test_support import __warningregistry__ as registry +except ImportError: + past_warnings = [] +else: + past_warnings = registry.keys() + del registry + def reset_module_registry(module): try: registry = module.__warningregistry__ @@ -338,14 +346,20 @@ optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop', 'sv', 'bsddb', 'dbhash') + def check_registry(self, module_name): + """Lookup the past warnings for modules already loaded.""" + return any(module_name in msg and ' removed' in msg + and issubclass(cls, DeprecationWarning) + and (' module' in msg or ' package' in msg) + for (msg, cls, line) in past_warnings) + def check_removal(self, module_name, optional=False): """Make sure the specified module, when imported, raises a DeprecationWarning and specifies itself in the message.""" with nested(CleanImport(module_name), warnings.catch_warnings()): - # XXX: This is not quite enough for extension modules - those - # won't rerun their init code even with CleanImport. - # You can see this easily by running the whole test suite with -3 - warnings.filterwarnings("error", ".+ removed", + warnings.filterwarnings("error", ".+ (module|package) .+ removed", + DeprecationWarning, __name__) + warnings.filterwarnings("error", ".+ removed .+ (module|package)", DeprecationWarning, __name__) try: __import__(module_name, level=0) @@ -358,8 +372,11 @@ self.fail("Non-optional module {0} raised an " "ImportError.".format(module_name)) else: - self.fail("DeprecationWarning not raised for {0}" - .format(module_name)) + # For extension modules, check the __warningregistry__. + # They won't rerun their init code even with CleanImport. + if not self.check_registry(module_name): + self.fail("DeprecationWarning not raised for {0}" + .format(module_name)) def test_platform_independent_removals(self): # Make sure that the modules that are available on all platforms raise @@ -390,7 +407,7 @@ def test_reduce_move(self): from operator import add # reduce tests may have already triggered this warning - reset_module_registry(unittest) + reset_module_registry(unittest.case) with warnings.catch_warnings(): warnings.filterwarnings("error", "reduce") self.assertRaises(DeprecationWarning, reduce, add, range(10))