]> granicus.if.org Git - python/commitdiff
Merged revisions 70734,70775,70856,70874,70876-70877 via svnmerge
authorR. David Murray <rdmurray@bitdance.com>
Tue, 31 Mar 2009 23:16:50 +0000 (23:16 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Tue, 31 Mar 2009 23:16:50 +0000 (23:16 +0000)
........
  r70734 | r.david.murray | 2009-03-30 15:04:00 -0400 (Mon, 30 Mar 2009) | 7 lines

  Add import_function method to test.test_support, and modify a number of
  tests that expect to be skipped if imports fail or functions don't
  exist to use import_function and import_module.  The ultimate goal is
  to change regrtest to not skip automatically on ImportError.  Checking
  in now to make sure the buldbots don't show any errors on platforms
  I can't direct test on.
........
  r70775 | r.david.murray | 2009-03-30 19:05:48 -0400 (Mon, 30 Mar 2009) | 4 lines

  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.
........
  r70856 | r.david.murray | 2009-03-31 14:32:17 -0400 (Tue, 31 Mar 2009) | 7 lines

  A few more test skips via import_module, and change import_module to
  return the error message produced by importlib, so that if an import
  in the package whose import is being wrapped is what failed the skip
  message will contain the name of that module instead of the name of the
  wrapped module.  Also fixed formatting of some previous comments.
........
  r70874 | r.david.murray | 2009-03-31 15:33:15 -0400 (Tue, 31 Mar 2009) | 5 lines

  Improve test_support.import_module docstring, remove
  deprecated flag from get_attribute since it isn't likely
  to do anything useful.
........
  r70876 | r.david.murray | 2009-03-31 15:49:15 -0400 (Tue, 31 Mar 2009) | 4 lines

  Remove the regrtest check that turns any ImportError into a skipped test.
  Hopefully all modules whose imports legitimately result in a skipped
  test have been properly wrapped by the previous commits.
........
  r70877 | r.david.murray | 2009-03-31 15:57:24 -0400 (Tue, 31 Mar 2009) | 2 lines

  Add NEWS entry for regrtest change.
........

30 files changed:
Lib/test/regrtest.py
Lib/test/support.py
Lib/test/test_asynchat.py
Lib/test/test_bz2.py
Lib/test/test_crypt.py
Lib/test/test_ctypes.py
Lib/test/test_curses.py
Lib/test/test_dbm.py
Lib/test/test_fcntl.py
Lib/test/test_fork1.py
Lib/test/test_grp.py
Lib/test/test_ioctl.py
Lib/test/test_mmap.py
Lib/test/test_multiprocessing.py
Lib/test/test_nis.py
Lib/test/test_ossaudiodev.py
Lib/test/test_posix.py
Lib/test/test_pwd.py
Lib/test/test_resource.py
Lib/test/test_sqlite.py
Lib/test/test_startfile.py
Lib/test/test_tcl.py
Lib/test/test_tk.py
Lib/test/test_ttk_guionly.py
Lib/test/test_ttk_textonly.py
Lib/test/test_winreg.py
Lib/test/test_winsound.py
Lib/test/test_xml_etree_c.py
Lib/test/test_zlib.py
Misc/NEWS

index dae7442037ec73ee67f979b53f7dd9bd499c9658..edfaa9a01cfe23c3e2a44e3f9e94387a4fb778d4 100755 (executable)
@@ -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()
index e20917007d6022b25ce87e2483d89896bf109580..9cd50a02f2f6f30ccd2d23eaf196eb8224d6ce6b 100644 (file)
@@ -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
index db0b194a9aecbd7abb81d5de86bff2a339dfce45..3da5878f47746c7e4b071bc8a6a52371704788f1 100644 (file)
@@ -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'
index 18d84bb60e24a42372705f1a32a70e160d1b4757..6715a023a839ae554df6088b64e298c8446785d8 100644 (file)
@@ -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")
index b4d0ac97c6e542d31d86fa548ac903d4febf46c7..2adb28d7cea6052e478c49f6aa4d49f1fda84661 100755 (executable)
@@ -1,6 +1,7 @@
 from test import support
 import unittest
-import crypt
+
+crypt = support.import_module('crypt')
 
 class CryptTestCase(unittest.TestCase):
 
index 92c98ada28493476c6915a8ed13bfacb0c5e1916..7d9abdcba7220a54ef0fcd0e815d8c786734e6de 100644 (file)
@@ -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():
index ed93946229c1466ec59971f541f07e7994913537..3f43c11edd6ab62f7132d256113cbbe9d2fa2adf 100644 (file)
@@ -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':
index cd2ff2705d2632ce1b6239d4ba7e39c641f48266..35d12e43ba8b64a82998ef4b87586020a378a9aa 100644 (file)
@@ -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
 
 #
index afb511cd063d7eddc4c40347b2babea472fcec88..72748f95167e33bba2b85bb6ebe9dca550b70520 100755 (executable)
@@ -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().
 
index c9f18d9c08721f27a161ddfa9cd540aaa08ed22f..e0366614e47ebe0ba3bd7b60d1faf9a9c8b7e1c6 100644 (file)
@@ -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):
index 5acda52e9a6349f9478599cd1e93bdce258ef443..1c1f75f776c296c90a5dd823f22bd16880fa1b5c 100755 (executable)
@@ -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):
index f6508c25d23d310223d4e9fa73e928e806a65251..dcb6695458a47315aa8424965378f5479ed0e0a8 100644 (file)
@@ -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")
index a2e9f7242a1a20dfede1306e4953e339d7f8795c..99cc63f077d4ddfaf8a072a40d913da7bbaeaecb 100644 (file)
@@ -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):
index 29f823de08037fb69f11568edc852134a0fbdb37..be108b1d30fe906e9a92cd418c99a93178b8b005 100644 (file)
@@ -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
 
index b4af95e3d888fd5bbed0cc11724be1ef8bb1ecb4..7aad33d55d99187f1b60ad29429eeb43205afc1a 100644 (file)
@@ -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")
 
index e49cf2acb91b47af8541bf006fbf7abfdb59d854..603aea3f254e4809d9d02f24515f823468cea3f7 100644 (file)
@@ -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
index 2a0a876846f74bf6ec6eaaf0cf1bca258b7be8e4..b1a9caf9acf23104c7c836dc65e3d16f39503a08 100644 (file)
@@ -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)
 
index b5a5de79d777f6ea2c30bba4281034dd0b113dd1..450306c45aef35ccf00144f760122d82a9606b99 100644 (file)
@@ -1,7 +1,7 @@
 import unittest
 from test import support
 
-import pwd
+pwd = support.import_module('pwd')
 
 class PwdTest(unittest.TestCase):
 
index 7063ff9c2a6a178f5e87a9a696cb12375a973185..454634922b5207cd0f64cc616a20ac3fa82f1995 100644 (file)
@@ -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):
index 611b30a99a527cea0c2a7ab5255941a0dfb22376..ec6f6345924f13438b3e25c8656c0a083004ea8c 100644 (file)
@@ -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)
index 8d23dbb71e5ffd94fa3f891d4bdd7a9894202486..7a003eb54b692569c9c4f0636e5538e569614989 100644 (file)
@@ -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):
index fa1727c81f2582d91fefd13da0bf89ff9339022f..f0eb863594be7b484009df82bac2f15e2f1d723e 100644 (file)
@@ -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
 
index 20b3fb59e322d56c4f49da8f0a53a4378bf5a2c6..2eca27b2b06818df75c6151ca361e7a5a7593a11 100644 (file)
@@ -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:
index 4a9a30cecab9f74aaaca864e247fde730ae32748..4e43a1873f881abec4fee7c1f7c73563ad8632cf 100644 (file)
@@ -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:
index 131e420013457f14cb44b0630f04cb3983af916e..b99625a18900dc44a067abf3eb821c40ded9701f 100644 (file)
@@ -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():
index b4ca5827c22dbdedde29d23517452ef92c7cb934..0b778b2056e5bf6db1acbbf19980254e46869cfd 100644 (file)
@@ -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 = [
index c39a233ca8fd52fc4f4afd9c7eef54547f1d6052..2950bb7d88cba1fd171f0a88f31e38c089e4a936 100644 (file)
@@ -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
index 4d28d01222263195992ec012381a67998651d829..71973ed234793f6a20a1b7ac862ee9742bd06414 100644 (file)
@@ -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>
index 99c8ddac7151c250d4c9364781e2a869f10eae0b..71f5ecfbb0a3b96527c082c26dbe0d17a75cfa68 100644 (file)
@@ -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
index 914e257a885038c00ef31f2105b06e1c738e4dec..ee96d22d83798c92e22d098e60a2a8916e465da0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -725,6 +725,10 @@ Extension Modules
 Tests
 -----
 
+- regrtest no longer treats ImportError as equivalent to SkipTest.  Imports
+  that should cause a test to be skipped are now done using import_module 
+  from test support, which does the conversion.
+
 - Issue #5083: New 'gui' resource for regrtest.