Index: Lib/importlib/test/source/test_abc_loader.py =================================================================== --- Lib/importlib/test/source/test_abc_loader.py (revision 72524) +++ Lib/importlib/test/source/test_abc_loader.py (working copy) @@ -157,10 +157,10 @@ mock = self.mocker({name: path}) with util.uncache(name): module = mock.load_module(name) - self.assert_(name in sys.modules) + self.assertIn(name, sys.modules) self.eq_attrs(module, __name__=name, __file__=path, __package__='pkg', __loader__=mock) - self.assert_(not hasattr(module, '__path__')) + self.assertFalse(hasattr(module, '__path__')) return mock, name def test_module_reuse(self): @@ -247,17 +247,17 @@ mocker = PyPycLoaderMock - @source_util.writes_bytecode + @source_util.writes_bytecode_files def verify_bytecode(self, mock, name): assert name in mock.module_paths - self.assert_(name in mock.module_bytecode) + self.assertIn(name, mock.module_bytecode) magic = mock.module_bytecode[name][:4] self.assertEqual(magic, imp.get_magic()) mtime = importlib._r_long(mock.module_bytecode[name][4:8]) self.assertEqual(mtime, 1) bc = mock.module_bytecode[name][8:] + self.assertEqual(bc, mock.compile_bc(name)) - def test_module(self): mock, name = super().test_module() self.verify_bytecode(mock, name) @@ -286,7 +286,7 @@ """Test that bytecode is properly handled based on sys.dont_write_bytecode.""" - @source_util.writes_bytecode + @source_util.writes_bytecode_files def run_test(self, dont_write_bytecode): name = 'mod' mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')}) @@ -307,7 +307,7 @@ """Test that bytecode is regenerated as expected.""" - @source_util.writes_bytecode + @source_util.writes_bytecode_files def test_different_magic(self): # A different magic number should lead to new bytecode. name = 'mod' @@ -323,7 +323,7 @@ magic = mock.module_bytecode[name][:4] self.assertEqual(magic, imp.get_magic()) - @source_util.writes_bytecode + @source_util.writes_bytecode_files def test_old_mtime(self): # Bytecode with an older mtime should be regenerated. name = 'mod' Index: Lib/importlib/test/source/util.py =================================================================== --- Lib/importlib/test/source/util.py (revision 72524) +++ Lib/importlib/test/source/util.py (working copy) @@ -9,32 +9,23 @@ from test import support -def writes_bytecode(fxn): - """Decorator to protect sys.dont_write_bytecode from mutation.""" +def writes_bytecode_files(fxn): + """Decorator to protect sys.dont_write_bytecode from mutation and to skip + tests that require it to be set to False.""" + if sys.dont_write_bytecode: + return lambda *args, **kwargs: None @functools.wraps(fxn) def wrapper(*args, **kwargs): original = sys.dont_write_bytecode sys.dont_write_bytecode = False - to_return = fxn(*args, **kwargs) - sys.dont_write_bytecode = original + try: + to_return = fxn(*args, **kwargs) + finally: + sys.dont_write_bytecode = original return to_return return wrapper -def writes_bytecode_files(fxn): - """Decorator that returns the function if writing bytecode is enabled, else - a stub function that accepts anything and simply returns None.""" - if sys.dont_write_bytecode: - return lambda *args, **kwargs: None - else: - @functools.wraps(fxn) - def wrapper(*args, **kwargs): - to_return = fxn(*args, **kwargs) - sys.dont_write_bytecode = False - return to_return - return wrapper - - def bytecode_path(source_path): for suffix, _, type_ in imp.get_suffixes(): if type_ == imp.PY_COMPILED: