Kaydet (Commit) c9a1bfed authored tarafından Brett Cannon's avatar Brett Cannon

Move test___all__ over to unittest.main() and use ModuleNotFoundError

üst 603dcf27
...@@ -146,11 +146,11 @@ from inspect import isabstract ...@@ -146,11 +146,11 @@ from inspect import isabstract
try: try:
import threading import threading
except ImportError: except ModuleNotFoundError:
threading = None threading = None
try: try:
import multiprocessing.process import multiprocessing.process
except ImportError: except ModuleNotFoundError:
multiprocessing = None multiprocessing = None
...@@ -180,7 +180,7 @@ for module in sys.modules.values(): ...@@ -180,7 +180,7 @@ for module in sys.modules.values():
if sys.platform == 'darwin': if sys.platform == 'darwin':
try: try:
import resource import resource
except ImportError: except ModuleNotFoundError:
pass pass
else: else:
soft, hard = resource.getrlimit(resource.RLIMIT_STACK) soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
...@@ -571,7 +571,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, ...@@ -571,7 +571,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
if findleaks: if findleaks:
try: try:
import gc import gc
except ImportError: except ModuleNotFoundError:
print('No GC available, disabling findleaks.') print('No GC available, disabling findleaks.')
findleaks = False findleaks = False
else: else:
...@@ -692,7 +692,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, ...@@ -692,7 +692,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
if use_mp: if use_mp:
try: try:
from threading import Thread from threading import Thread
except ImportError: except ModuleNotFoundError:
print("Multiprocess option requires thread support") print("Multiprocess option requires thread support")
sys.exit(2) sys.exit(2)
from queue import Queue from queue import Queue
...@@ -1396,7 +1396,7 @@ def dash_R(the_module, test, indirect_test, huntrleaks): ...@@ -1396,7 +1396,7 @@ def dash_R(the_module, test, indirect_test, huntrleaks):
pic = sys.path_importer_cache.copy() pic = sys.path_importer_cache.copy()
try: try:
import zipimport import zipimport
except ImportError: except ModuleNotFoundError:
zdc = None # Run unmodified on platforms without zipimport support zdc = None # Run unmodified on platforms without zipimport support
else: else:
zdc = zipimport._zip_directory_cache.copy() zdc = zipimport._zip_directory_cache.copy()
...@@ -1473,7 +1473,7 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs): ...@@ -1473,7 +1473,7 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
sys.path_importer_cache.update(pic) sys.path_importer_cache.update(pic)
try: try:
import zipimport import zipimport
except ImportError: except ModuleNotFoundError:
pass # Run unmodified on platforms without zipimport support pass # Run unmodified on platforms without zipimport support
else: else:
zipimport._zip_directory_cache.clear() zipimport._zip_directory_cache.clear()
...@@ -1510,7 +1510,7 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs): ...@@ -1510,7 +1510,7 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
doctest.master = None doctest.master = None
try: try:
import ctypes import ctypes
except ImportError: except ModuleNotFoundError:
# Don't worry about resetting the cache if ctypes is not supported # Don't worry about resetting the cache if ctypes is not supported
pass pass
else: else:
......
...@@ -29,27 +29,27 @@ import _testcapi ...@@ -29,27 +29,27 @@ import _testcapi
try: try:
import _thread, threading import _thread, threading
except ImportError: except ModuleNotFoundError:
_thread = None _thread = None
threading = None threading = None
try: try:
import multiprocessing.process import multiprocessing.process
except ImportError: except ModuleNotFoundError:
multiprocessing = None multiprocessing = None
try: try:
import zlib import zlib
except ImportError: except ModuleNotFoundError:
zlib = None zlib = None
try: try:
import bz2 import bz2
except ImportError: except ModuleNotFoundError:
bz2 = None bz2 = None
try: try:
import lzma import lzma
except ImportError: except ModuleNotFoundError:
lzma = None lzma = None
__all__ = [ __all__ = [
...@@ -116,7 +116,7 @@ def import_module(name, deprecated=False, *, required_on=()): ...@@ -116,7 +116,7 @@ def import_module(name, deprecated=False, *, required_on=()):
with _ignore_deprecated_imports(deprecated): with _ignore_deprecated_imports(deprecated):
try: try:
return importlib.import_module(name) return importlib.import_module(name)
except ImportError as msg: except ModuleNotFoundError as msg:
if sys.platform.startswith(tuple(required_on)): if sys.platform.startswith(tuple(required_on)):
raise raise
raise unittest.SkipTest(str(msg)) raise unittest.SkipTest(str(msg))
...@@ -188,7 +188,7 @@ def import_fresh_module(name, fresh=(), blocked=(), deprecated=False): ...@@ -188,7 +188,7 @@ def import_fresh_module(name, fresh=(), blocked=(), deprecated=False):
if not _save_and_block_module(blocked_name, orig_modules): if not _save_and_block_module(blocked_name, orig_modules):
names_to_remove.append(blocked_name) names_to_remove.append(blocked_name)
fresh_module = importlib.import_module(name) fresh_module = importlib.import_module(name)
except ImportError: except ModuleNotFoundError:
fresh_module = None fresh_module = None
finally: finally:
for orig_name, module in orig_modules.items(): for orig_name, module in orig_modules.items():
......
...@@ -75,7 +75,7 @@ class AllTest(unittest.TestCase): ...@@ -75,7 +75,7 @@ class AllTest(unittest.TestCase):
try: try:
import rlcompleter import rlcompleter
import locale import locale
except ImportError: except ModuleNotFoundError:
pass pass
else: else:
locale.setlocale(locale.LC_CTYPE, 'C') locale.setlocale(locale.LC_CTYPE, 'C')
...@@ -113,8 +113,5 @@ class AllTest(unittest.TestCase): ...@@ -113,8 +113,5 @@ class AllTest(unittest.TestCase):
print('Following modules failed to be imported:', failed_imports) print('Following modules failed to be imported:', failed_imports)
def test_main():
support.run_unittest(AllTest)
if __name__ == "__main__": if __name__ == "__main__":
test_main() unittest.main()
...@@ -116,7 +116,7 @@ import inspect ...@@ -116,7 +116,7 @@ import inspect
import traceback import traceback
try: try:
import fcntl import fcntl
except ImportError: except ModuleNotFoundError:
fcntl = None fcntl = None
def resolve_dotted_attribute(obj, attr, allow_dotted_names=True): def resolve_dotted_attribute(obj, attr, allow_dotted_names=True):
......
...@@ -18,18 +18,18 @@ import binascii ...@@ -18,18 +18,18 @@ import binascii
try: try:
import zlib # We may need its compression method import zlib # We may need its compression method
crc32 = zlib.crc32 crc32 = zlib.crc32
except ImportError: except ModuleNotFoundError:
zlib = None zlib = None
crc32 = binascii.crc32 crc32 = binascii.crc32
try: try:
import bz2 # We may need its compression method import bz2 # We may need its compression method
except ImportError: except ModuleNotFoundError:
bz2 = None bz2 = None
try: try:
import lzma # We may need its compression method import lzma # We may need its compression method
except ImportError: except ModuleNotFoundError:
lzma = None lzma = None
__all__ = ["BadZipFile", "BadZipfile", "error", __all__ = ["BadZipFile", "BadZipfile", "error",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment