Exemple #1
0
 def check_removal(self, module_name, optional=False):
     """Make sure the specified module, when imported, raises a
     DeprecationWarning and specifies itself in the message."""
     if module_name in sys.modules:
         mod = sys.modules[module_name]
         filename = getattr(mod, '__file__', '')
         mod = None
         # the module is not implemented in C?
         if not filename.endswith(('.py', '.pyc', '.pyo')):
             # Issue #23375: If the module was already loaded, reimporting
             # the module will not emit again the warning. The warning is
             # emited when the module is loaded, but C modules cannot
             # unloaded.
             if test_support.verbose:
                 print(
                     "Cannot test the Python 3 DeprecationWarning of the "
                     "%s module, the C module is already loaded" %
                     module_name)
             return
     with CleanImport(module_name), warnings.catch_warnings():
         warnings.filterwarnings("error", ".+ (module|package) .+ removed",
                                 DeprecationWarning, __name__)
         warnings.filterwarnings("error", ".+ removed .+ (module|package)",
                                 DeprecationWarning, __name__)
         try:
             __import__(module_name, level=0)
         except DeprecationWarning as exc:
             self.assertIn(
                 module_name, exc.args[0],
                 "%s warning didn't contain module name" % module_name)
         except ImportError:
             if not optional:
                 self.fail("Non-optional module {0} raised an "
                           "ImportError.".format(module_name))
         else:
             # For extension modules, check the __warningregistry__.
             # They won't rerun their init code even with CleanImport.
             if not check_deprecated_module(module_name):
                 self.fail("DeprecationWarning not raised for {0}".format(
                     module_name))
Exemple #2
0
 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", DeprecationWarning,
                                 __name__)
         try:
             __import__(module_name, level=0)
         except DeprecationWarning as exc:
             self.assert_(
                 module_name in exc.args[0],
                 "%s warning didn't contain module name" % module_name)
         except ImportError:
             if not optional:
                 self.fail("Non-optional module {0} raised an "
                           "ImportError.".format(module_name))
         else:
             self.fail("DeprecationWarning not raised for {0}".format(
                 module_name))
Exemple #3
0
 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 CleanImport(module_name), warnings.catch_warnings():
         warnings.filterwarnings("error", ".+ (module|package) .+ removed",
                                 DeprecationWarning, __name__)
         warnings.filterwarnings("error", ".+ removed .+ (module|package)",
                                 DeprecationWarning, __name__)
         try:
             __import__(module_name, level=0)
         except DeprecationWarning as exc:
             self.assertIn(
                 module_name, exc.args[0],
                 "%s warning didn't contain module name" % module_name)
         except ImportError:
             if not optional:
                 self.fail("Non-optional module {0} raised an "
                           "ImportError.".format(module_name))
         else:
             # For extension modules, check the __warningregistry__.
             # They won't rerun their init code even with CleanImport.
             if not check_deprecated_module(module_name):
                 self.fail("DeprecationWarning not raised for {0}".format(
                     module_name))