diff options
Diffstat (limited to 'Lib/test')
29 files changed, 120 insertions, 67 deletions
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py index dae7442..edfaa9a 100755 --- a/Lib/test/regrtest.py +++ b/Lib/test/regrtest.py @@ -628,7 +628,7 @@ def runtest_inner(test, generate, verbose, quiet, test_times, print(test, "skipped --", msg) sys.stdout.flush() return -2 - except (ImportError, unittest.SkipTest) as msg: + except unittest.SkipTest as msg: if not quiet: print(test, "skipped --", msg) sys.stdout.flush() diff --git a/Lib/test/support.py b/Lib/test/support.py index e209170..9cd50a0 100644 --- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -12,6 +12,7 @@ import platform import shutil import warnings import unittest +import importlib __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module", "verbose", "use_resources", "max_memuse", "record_original_stdout", @@ -24,7 +25,7 @@ __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module", "TransientResource", "transient_internet", "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup", "threading_cleanup", - "reap_children", "cpython_only", "check_impl_detail"] + "reap_children", "cpython_only", "check_impl_detail", "get_attribute"] class Error(Exception): """Base class for regression test exceptions.""" @@ -41,19 +42,32 @@ class ResourceDenied(unittest.SkipTest): """ def import_module(name, deprecated=False): - """Import the module to be tested, raising SkipTest if it is not - available.""" + """Import and return the module to be tested, raising SkipTest if + it is not available. + + If deprecated is True, any module or package deprecation messages + will be suppressed.""" with warnings.catch_warnings(): if deprecated: warnings.filterwarnings("ignore", ".+ (module|package)", DeprecationWarning) try: - module = __import__(name, level=0) - except ImportError: - raise unittest.SkipTest("No module named " + name) + module = importlib.import_module(name) + except ImportError as msg: + raise unittest.SkipTest(str(msg)) else: return module +def get_attribute(obj, name): + """Get an attribute, raising SkipTest if AttributeError is raised.""" + try: + attribute = getattr(obj, name) + except AttributeError: + raise unittest.SkipTest("module %s has no attribute %s" % ( + obj.__name__, name)) + else: + return attribute + verbose = 1 # Flag set to 0 by regrtest.py use_resources = None # Flag set to [] by regrtest.py max_memuse = 0 # Disable bigmem tests (they will still be run with diff --git a/Lib/test/test_asynchat.py b/Lib/test/test_asynchat.py index db0b194..3da5878 100644 --- a/Lib/test/test_asynchat.py +++ b/Lib/test/test_asynchat.py @@ -1,10 +1,13 @@ -# test asynchat -- requires threading +# test asynchat + +from test import support + +# If this fails, the test will be skipped. +thread = support.import_module('_thread') -import _thread as thread # If this fails, we can't test this module import asyncore, asynchat, socket, threading, time import unittest import sys -from test import support HOST = support.HOST SERVER_QUIT = b'QUIT\n' diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index 18d84bb..6715a02 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -8,7 +8,9 @@ import os import subprocess import sys -import bz2 +# Skip tests if the bz2 module doesn't exist. +bz2 = support.import_module('bz2') + from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor has_cmdline_bunzip2 = sys.platform not in ("win32", "os2emx") diff --git a/Lib/test/test_crypt.py b/Lib/test/test_crypt.py index b4d0ac9..2adb28d 100755 --- a/Lib/test/test_crypt.py +++ b/Lib/test/test_crypt.py @@ -1,6 +1,7 @@ from test import support import unittest -import crypt + +crypt = support.import_module('crypt') class CryptTestCase(unittest.TestCase): diff --git a/Lib/test/test_ctypes.py b/Lib/test/test_ctypes.py index 92c98ad..7d9abdc 100644 --- a/Lib/test/test_ctypes.py +++ b/Lib/test/test_ctypes.py @@ -1,6 +1,10 @@ import unittest -from test.support import run_unittest +from test.support import run_unittest, import_module + +# Skip tests if _ctypes module was not built. +import_module('_ctypes') + import ctypes.test def test_main(): diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index ed93946..3f43c11 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -9,16 +9,19 @@ # Only called, not tested: getmouse(), ungetmouse() # -import curses, sys, tempfile, os -import curses.panel +import sys, tempfile, os # Optionally test curses module. This currently requires that the # 'curses' resource be given on the regrtest command line using the -u # option. If not available, nothing after this line will be executed. -from test.support import requires +from test.support import requires, import_module requires('curses') +# If either of these don't exist, skip the tests. +curses = import_module('curses') +curses.panel = import_module('curses.panel') + # XXX: if newterm was supported we could use it instead of initscr and not exit term = os.environ.get('TERM') if not term or term == 'unknown': diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py index cd2ff27..35d12e4 100644 --- a/Lib/test/test_dbm.py +++ b/Lib/test/test_dbm.py @@ -3,10 +3,12 @@ import os import unittest -import dbm import glob import test.support +# Skip tests if dbm module doesn't exist. +dbm = test.support.import_module('dbm') + _fname = test.support.TESTFN # diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py index afb511c..72748f9 100755 --- a/Lib/test/test_fcntl.py +++ b/Lib/test/test_fcntl.py @@ -3,12 +3,15 @@ OS/2+EMX doesn't support the file locking operations. """ -import fcntl import os import struct import sys import unittest -from test.support import verbose, TESTFN, unlink, run_unittest +from test.support import verbose, TESTFN, unlink, run_unittest, import_module + +# Skip test if no fnctl module. +fcntl = import_module('fcntl') + # TODO - Write tests for flock() and lockf(). diff --git a/Lib/test/test_fork1.py b/Lib/test/test_fork1.py index c9f18d9..e036661 100644 --- a/Lib/test/test_fork1.py +++ b/Lib/test/test_fork1.py @@ -3,14 +3,12 @@ import os import time -import unittest from test.fork_wait import ForkWait -from test.support import run_unittest, reap_children +from test.support import run_unittest, reap_children, get_attribute + +# Skip test if fork does not exist. +get_attribute(os, 'fork') -try: - os.fork -except AttributeError: - raise unittest.SkipTest("os.fork not defined -- skipping test_fork1") class ForkTest(ForkWait): def wait_impl(self, cpid): diff --git a/Lib/test/test_grp.py b/Lib/test/test_grp.py index 5acda52..1c1f75f 100755 --- a/Lib/test/test_grp.py +++ b/Lib/test/test_grp.py @@ -1,9 +1,10 @@ """Test script for the grp module.""" -import grp import unittest from test import support +grp = support.import_module('grp') + class GroupDatabaseTestCase(unittest.TestCase): def check_value(self, value): diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py index f6508c2..dcb6695 100644 --- a/Lib/test/test_ioctl.py +++ b/Lib/test/test_ioctl.py @@ -1,12 +1,9 @@ import unittest -from test.support import run_unittest +from test.support import run_unittest, import_module, get_attribute import os, struct -try: - import fcntl, termios -except ImportError: - raise unittest.SkipTest("No fcntl or termios module") -if not hasattr(termios,'TIOCGPGRP'): - raise unittest.SkipTest("termios module doesn't have TIOCGPGRP") +fcntl = import_module('fcntl') +termios = import_module('termios') +get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature try: tty = open("/dev/tty", "r") diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index a2e9f72..99cc63f 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1,8 +1,10 @@ -from test.support import TESTFN, run_unittest -import mmap +from test.support import TESTFN, run_unittest, import_module import unittest import os, re, itertools +# Skip test if we can't import mmap. +mmap = import_module('mmap') + PAGESIZE = mmap.PAGESIZE class MmapTests(unittest.TestCase): diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py index 29f823d..be108b1 100644 --- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -17,20 +17,19 @@ import copy import socket import random import logging +import test.support -# Work around broken sem_open implementations -try: - import multiprocessing.synchronize -except ImportError as e: - raise unittest.SkipTest(e) +# Skip tests if _multiprocessing wasn't built. +_multiprocessing = test.support.import_module('_multiprocessing') +# Skip tests if sem_open implementation is broken. +test.support.import_module('multiprocessing.synchronize') import multiprocessing.dummy import multiprocessing.connection import multiprocessing.managers import multiprocessing.heap import multiprocessing.pool -import _multiprocessing from multiprocessing import util diff --git a/Lib/test/test_nis.py b/Lib/test/test_nis.py index b4af95e..7aad33d 100644 --- a/Lib/test/test_nis.py +++ b/Lib/test/test_nis.py @@ -1,6 +1,8 @@ from test import support import unittest -import nis + +# Skip test if nis module does not exist. +nis = support.import_module('nis') raise unittest.SkipTest("test_nis hangs on Solaris") diff --git a/Lib/test/test_ossaudiodev.py b/Lib/test/test_ossaudiodev.py index e49cf2a..603aea3 100644 --- a/Lib/test/test_ossaudiodev.py +++ b/Lib/test/test_ossaudiodev.py @@ -3,8 +3,9 @@ support.requires('audio') from test.support import findfile +ossaudiodev = support.import_module('ossaudiodev') + import errno -import ossaudiodev import sys import sunau import time diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 2a0a876..b1a9caf 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -2,17 +2,15 @@ from test import support -try: - import posix -except ImportError: - raise unittest.SkipTest("posix is not available") - import time import os import pwd import shutil import unittest import warnings + +posix = support.import_module('posix') + warnings.filterwarnings('ignore', '.* potential security risk .*', RuntimeWarning) diff --git a/Lib/test/test_pwd.py b/Lib/test/test_pwd.py index b5a5de7..450306c 100644 --- a/Lib/test/test_pwd.py +++ b/Lib/test/test_pwd.py @@ -1,7 +1,7 @@ import unittest from test import support -import pwd +pwd = support.import_module('pwd') class PwdTest(unittest.TestCase): diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py index 7063ff9..4546349 100644 --- a/Lib/test/test_resource.py +++ b/Lib/test/test_resource.py @@ -1,9 +1,9 @@ import unittest from test import support - -import resource import time +resource = support.import_module('resource') + # This test is checking a few specific problem spots with the resource module. class ResourceTest(unittest.TestCase): diff --git a/Lib/test/test_sqlite.py b/Lib/test/test_sqlite.py index 611b30a..ec6f634 100644 --- a/Lib/test/test_sqlite.py +++ b/Lib/test/test_sqlite.py @@ -1,10 +1,9 @@ import unittest -from test.support import run_unittest +from test.support import run_unittest, import_module + +# Skip test if _sqlite3 module not installed +import_module('_sqlite3') -try: - import _sqlite3 -except ImportError: - raise unittest.SkipTest('no sqlite available') from sqlite3.test import (dbapi, types, userfunctions, factory, transactions, hooks, regression, dump) diff --git a/Lib/test/test_startfile.py b/Lib/test/test_startfile.py index 8d23dbb..7a003eb 100644 --- a/Lib/test/test_startfile.py +++ b/Lib/test/test_startfile.py @@ -9,9 +9,11 @@ import unittest from test import support +import os +from os import path + +startfile = support.get_attribute(os, 'startfile') -# use this form so that the test is skipped when startfile is not available: -from os import startfile, path class TestCase(unittest.TestCase): def test_nonexisting(self): diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py index fa1727c..f0eb863 100644 --- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tcl.py @@ -2,8 +2,11 @@ import unittest import os -import _tkinter from test import support + +# Skip this test if the _tkinter module wasn't built. +_tkinter = support.import_module('_tkinter') + from tkinter import Tcl from _tkinter import TclError diff --git a/Lib/test/test_tk.py b/Lib/test/test_tk.py index 20b3fb5..2eca27b 100644 --- a/Lib/test/test_tk.py +++ b/Lib/test/test_tk.py @@ -3,6 +3,11 @@ from tkinter.test import runtktests from test import support import unittest +# Skip test if _tkinter wasn't built. +support.import_module('_tkinter') + +import tkinter + try: tkinter.Button() except tkinter.TclError as msg: diff --git a/Lib/test/test_ttk_guionly.py b/Lib/test/test_ttk_guionly.py index 4a9a30c..4e43a18 100644 --- a/Lib/test/test_ttk_guionly.py +++ b/Lib/test/test_ttk_guionly.py @@ -1,11 +1,15 @@ import os import sys -from tkinter import ttk -from tkinter.test import runtktests import unittest -from _tkinter import TclError from test import support +# Skip this test if _tkinter wasn't built. +support.import_module('_tkinter') + +from _tkinter import TclError +from tkinter import ttk +from tkinter.test import runtktests + try: ttk.Button() except TclError as msg: diff --git a/Lib/test/test_ttk_textonly.py b/Lib/test/test_ttk_textonly.py index 131e420..b99625a 100644 --- a/Lib/test/test_ttk_textonly.py +++ b/Lib/test/test_ttk_textonly.py @@ -1,6 +1,10 @@ import os import sys from test import support + +# Skip this test if _tkinter does not exist. +support.import_module('_tkinter') + from tkinter.test import runtktests def test_main(): diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py index b4ca582..0b778b2 100644 --- a/Lib/test/test_winreg.py +++ b/Lib/test/test_winreg.py @@ -2,12 +2,15 @@ # Test the windows specific win32reg module. # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey -from winreg import * import os, sys import unittest - from test import support +# Do this first so test will be skipped if module doesn't exist +support.import_module('winreg') +# Now import everything +from winreg import * + test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me" test_data = [ diff --git a/Lib/test/test_winsound.py b/Lib/test/test_winsound.py index c39a233..2950bb7 100644 --- a/Lib/test/test_winsound.py +++ b/Lib/test/test_winsound.py @@ -3,10 +3,12 @@ import unittest from test import support support.requires('audio') -import winsound, time +import time import os import subprocess +winsound = support.import_module('winsound') + class BeepTest(unittest.TestCase): # As with PlaySoundTest, incorporate the _have_soundcard() check diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index 4d28d01..71973ed 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -5,7 +5,7 @@ import sys from test import support -from xml.etree import cElementTree as ET +ET = support.import_module('xml.etree.cElementTree') SAMPLE_XML = """ <body> diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index 99c8dda..71f5ecf 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -1,9 +1,10 @@ import unittest from test import support -import zlib import binascii import random +zlib = support.import_module('zlib') + class ChecksumTestCase(unittest.TestCase): # checksum test cases |