Change more tests to use import_module for the modules that
should cause tests to be skipped. Also rename import_function
to the more descriptive get_attribute and add a docstring.
diff --git a/Lib/test/test_bsddb3.py b/Lib/test/test_bsddb3.py
index 5925e15..b290a6e 100644
--- a/Lib/test/test_bsddb3.py
+++ b/Lib/test/test_bsddb3.py
@@ -7,7 +7,11 @@
import tempfile
import time
import unittest
-from test.test_support import requires, verbose, run_unittest, unlink, rmtree
+from test.test_support import (requires, verbose, run_unittest, unlink, rmtree,
+ import_module)
+
+#Skip test if bsddb cannot import _bsddb.
+import_module('bsddb')
# When running as a script instead of within the regrtest framework, skip the
# requires test, since it's obvious we want to run them.
diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
index 2f627c3..215a04e 100644
--- a/Lib/test/test_bz2.py
+++ b/Lib/test/test_bz2.py
@@ -1,6 +1,6 @@
#!/usr/bin/python
from test import test_support
-from test.test_support import TESTFN
+from test.test_support import TESTFN, import_module
import unittest
from cStringIO import StringIO
@@ -8,7 +8,7 @@
import subprocess
import sys
-import bz2
+bz2 = import_module('bz2')
from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
has_cmdline_bunzip2 = sys.platform not in ("win32", "os2emx", "riscos")
diff --git a/Lib/test/test_ctypes.py b/Lib/test/test_ctypes.py
index 7a81ab4..85f137e 100644
--- a/Lib/test/test_ctypes.py
+++ b/Lib/test/test_ctypes.py
@@ -1,6 +1,9 @@
import unittest
-from test.test_support import run_unittest
+from test.test_support import run_unittest, import_module
+#Skip tests if _ctypes module does not exist
+import_module('_ctypes')
+
import ctypes.test
def test_main():
diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py
index 26cfc81..e13c553 100644
--- a/Lib/test/test_curses.py
+++ b/Lib/test/test_curses.py
@@ -9,15 +9,16 @@
# 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.test_support import requires
+from test.test_support import requires, import_module
requires('curses')
+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')
diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py
index 0541f86..1627fff 100755
--- a/Lib/test/test_dbm.py
+++ b/Lib/test/test_dbm.py
@@ -1,6 +1,6 @@
from test import test_support
import unittest
-import dbm
+dbm = test_support.import_module('dbm')
class DbmTestCase(unittest.TestCase):
diff --git a/Lib/test/test_fork1.py b/Lib/test/test_fork1.py
index 9306242..b42304b 100644
--- a/Lib/test/test_fork1.py
+++ b/Lib/test/test_fork1.py
@@ -4,10 +4,10 @@
import os
import time
from test.fork_wait import ForkWait
-from test.test_support import run_unittest, reap_children, import_function
+from test.test_support import run_unittest, reap_children, get_attribute
#Skip test if fork does not exist.
-import_function(os, 'fork')
+get_attribute(os, 'fork')
class ForkTest(ForkWait):
diff --git a/Lib/test/test_gdbm.py b/Lib/test/test_gdbm.py
index 76689f0..131d22b 100755
--- a/Lib/test/test_gdbm.py
+++ b/Lib/test/test_gdbm.py
@@ -1,7 +1,8 @@
-import gdbm
import unittest
import os
-from test.test_support import verbose, TESTFN, run_unittest, unlink
+from test.test_support import (verbose, TESTFN, run_unittest, unlink,
+ import_module)
+gdbm = import_module('gdbm')
filename = TESTFN
diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py
index 4da209d..823d954 100644
--- a/Lib/test/test_ioctl.py
+++ b/Lib/test/test_ioctl.py
@@ -1,12 +1,9 @@
import unittest
-from test.test_support import run_unittest
+from test.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_multiprocessing.py b/Lib/test/test_multiprocessing.py
index 4388fe0..105a743 100644
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -17,20 +17,19 @@
import socket
import random
import logging
+import test_support
+_multiprocessing = test_support.import_module('_multiprocessing')
+
# Work around broken sem_open implementations
-try:
- import multiprocessing.synchronize
-except ImportError, e:
- raise unittest.SkipTest(e)
+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 0026953..8d49550 100644
--- a/Lib/test/test_nis.py
+++ b/Lib/test/test_nis.py
@@ -1,6 +1,7 @@
from test import test_support
import unittest
-import nis
+
+nis = test_support.import_module('nis')
class NisTests(unittest.TestCase):
def test_maps(self):
diff --git a/Lib/test/test_ossaudiodev.py b/Lib/test/test_ossaudiodev.py
index ddee6db..34893f1 100644
--- a/Lib/test/test_ossaudiodev.py
+++ b/Lib/test/test_ossaudiodev.py
@@ -3,8 +3,9 @@
from test.test_support import findfile
+ossaudiodev = test_support.import_module('ossaudiodev')
+
import errno
-import ossaudiodev
import sys
import sunau
import time
diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py
index f354a1b..52692a7 100644
--- a/Lib/test/test_resource.py
+++ b/Lib/test/test_resource.py
@@ -1,9 +1,9 @@
import unittest
from test import test_support
-
-import resource
import time
+resource = test_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 37481c0..f8b2f98 100644
--- a/Lib/test/test_sqlite.py
+++ b/Lib/test/test_sqlite.py
@@ -1,10 +1,9 @@
import unittest
-from test.test_support import run_unittest
+from test.test_support import run_unittest, import_module
-try:
- import _sqlite3
-except ImportError:
- raise unittest.SkipTest('no sqlite available')
+#Skip test of _sqlite3 module not installed
+import_module('_sqlite3')
+
from sqlite3.test import (dbapi, types, userfunctions, py25tests,
factory, transactions, hooks, regression,
dump)
diff --git a/Lib/test/test_startfile.py b/Lib/test/test_startfile.py
index 23e2708..8eeae72 100644
--- a/Lib/test/test_startfile.py
+++ b/Lib/test/test_startfile.py
@@ -12,7 +12,7 @@
import os
from os import path
-startfile = test_support.import_function(os, 'startfile')
+startfile = test_support.get_attribute(os, 'startfile')
class TestCase(unittest.TestCase):
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 90bd8e6..4353339 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -12,6 +12,7 @@
import shutil
import warnings
import unittest
+import importlib
__all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
"verbose", "use_resources", "max_memuse", "record_original_stdout",
@@ -25,7 +26,7 @@
"run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
"BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
"threading_cleanup", "reap_children", "cpython_only",
- "check_impl_detail"]
+ "check_impl_detail", "get_attribute"]
class Error(Exception):
"""Base class for regression test exceptions."""
@@ -49,24 +50,26 @@
warnings.filterwarnings("ignore", ".+ (module|package)",
DeprecationWarning)
try:
- module = __import__(name, level=0)
+ module = importlib.import_module(name)
except ImportError:
raise unittest.SkipTest("No module named " + name)
else:
return module
-def import_function(module, name, deprecated=False):
+def get_attribute(module, name, deprecated=False):
+ """Get an attribute from the module, raising SkipTest if it is
+ not available."""
with warnings.catch_warnings():
if deprecated:
warnings.filterwarnings("ignore", ".+ (module|package)",
DeprecationWarning)
try:
- function = getattr(module, name)
+ attribute = getattr(module, name)
except AttributeError:
- raise unittest.SkipTest("No function named %s in module %s" % (
- name, module.__name__))
+ raise unittest.SkipTest("module %s has no attribute %s" % (
+ module.__name__, name))
else:
- return function
+ return attribute
verbose = 1 # Flag set to 0 by regrtest.py
diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py
index 7ddd44b..3d5c14f 100644
--- a/Lib/test/test_xml_etree_c.py
+++ b/Lib/test/test_xml_etree_c.py
@@ -5,7 +5,7 @@
from test import test_support
-from xml.etree import cElementTree as ET
+ET = test_support.import_module('xml.etree.cElementTree')
SAMPLE_XML = """
<body>
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index adb87ff..cbf844f 100644
--- a/Lib/test/test_zlib.py
+++ b/Lib/test/test_zlib.py
@@ -1,9 +1,10 @@
import unittest
from test import test_support
-import zlib
import binascii
import random
+zlib = test_support.import_module('zlib')
+
class ChecksumTestCase(unittest.TestCase):
# checksum test cases