Commit a21e4ca3 authored by R. David Murray's avatar R. David Murray

Merged revisions 70734,70775,70856,70874,70876-70877 via svnmerge

........
  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.
........
parent a1b49013
...@@ -628,7 +628,7 @@ def runtest_inner(test, generate, verbose, quiet, test_times, ...@@ -628,7 +628,7 @@ def runtest_inner(test, generate, verbose, quiet, test_times,
print(test, "skipped --", msg) print(test, "skipped --", msg)
sys.stdout.flush() sys.stdout.flush()
return -2 return -2
except (ImportError, unittest.SkipTest) as msg: except unittest.SkipTest as msg:
if not quiet: if not quiet:
print(test, "skipped --", msg) print(test, "skipped --", msg)
sys.stdout.flush() sys.stdout.flush()
......
...@@ -12,6 +12,7 @@ import platform ...@@ -12,6 +12,7 @@ import platform
import shutil import shutil
import warnings import warnings
import unittest import unittest
import importlib
__all__ = ["Error", "TestFailed", "ResourceDenied", "import_module", __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
"verbose", "use_resources", "max_memuse", "record_original_stdout", "verbose", "use_resources", "max_memuse", "record_original_stdout",
...@@ -24,7 +25,7 @@ __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module", ...@@ -24,7 +25,7 @@ __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
"TransientResource", "transient_internet", "run_with_locale", "TransientResource", "transient_internet", "run_with_locale",
"set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner", "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner",
"run_unittest", "run_doctest", "threading_setup", "threading_cleanup", "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): class Error(Exception):
"""Base class for regression test exceptions.""" """Base class for regression test exceptions."""
...@@ -41,19 +42,32 @@ class ResourceDenied(unittest.SkipTest): ...@@ -41,19 +42,32 @@ class ResourceDenied(unittest.SkipTest):
""" """
def import_module(name, deprecated=False): def import_module(name, deprecated=False):
"""Import the module to be tested, raising SkipTest if it is not """Import and return the module to be tested, raising SkipTest if
available.""" it is not available.
If deprecated is True, any module or package deprecation messages
will be suppressed."""
with warnings.catch_warnings(): with warnings.catch_warnings():
if deprecated: if deprecated:
warnings.filterwarnings("ignore", ".+ (module|package)", warnings.filterwarnings("ignore", ".+ (module|package)",
DeprecationWarning) DeprecationWarning)
try: try:
module = __import__(name, level=0) module = importlib.import_module(name)
except ImportError: except ImportError as msg:
raise unittest.SkipTest("No module named " + name) raise unittest.SkipTest(str(msg))
else: else:
return module 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 verbose = 1 # Flag set to 0 by regrtest.py
use_resources = None # Flag set to [] by regrtest.py use_resources = None # Flag set to [] by regrtest.py
max_memuse = 0 # Disable bigmem tests (they will still be run with max_memuse = 0 # Disable bigmem tests (they will still be run with
......
# 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 asyncore, asynchat, socket, threading, time
import unittest import unittest
import sys import sys
from test import support
HOST = support.HOST HOST = support.HOST
SERVER_QUIT = b'QUIT\n' SERVER_QUIT = b'QUIT\n'
......
...@@ -8,7 +8,9 @@ import os ...@@ -8,7 +8,9 @@ import os
import subprocess import subprocess
import sys import sys
import bz2 # Skip tests if the bz2 module doesn't exist.
bz2 = support.import_module('bz2')
from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor
has_cmdline_bunzip2 = sys.platform not in ("win32", "os2emx") has_cmdline_bunzip2 = sys.platform not in ("win32", "os2emx")
......
from test import support from test import support
import unittest import unittest
import crypt
crypt = support.import_module('crypt')
class CryptTestCase(unittest.TestCase): class CryptTestCase(unittest.TestCase):
......
import unittest 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 import ctypes.test
def test_main(): def test_main():
......
...@@ -9,16 +9,19 @@ ...@@ -9,16 +9,19 @@
# Only called, not tested: getmouse(), ungetmouse() # Only called, not tested: getmouse(), ungetmouse()
# #
import curses, sys, tempfile, os import sys, tempfile, os
import curses.panel
# Optionally test curses module. This currently requires that the # Optionally test curses module. This currently requires that the
# 'curses' resource be given on the regrtest command line using the -u # 'curses' resource be given on the regrtest command line using the -u
# option. If not available, nothing after this line will be executed. # 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') 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 # XXX: if newterm was supported we could use it instead of initscr and not exit
term = os.environ.get('TERM') term = os.environ.get('TERM')
if not term or term == 'unknown': if not term or term == 'unknown':
......
...@@ -3,10 +3,12 @@ ...@@ -3,10 +3,12 @@
import os import os
import unittest import unittest
import dbm
import glob import glob
import test.support import test.support
# Skip tests if dbm module doesn't exist.
dbm = test.support.import_module('dbm')
_fname = test.support.TESTFN _fname = test.support.TESTFN
# #
......
...@@ -3,12 +3,15 @@ ...@@ -3,12 +3,15 @@
OS/2+EMX doesn't support the file locking operations. OS/2+EMX doesn't support the file locking operations.
""" """
import fcntl
import os import os
import struct import struct
import sys import sys
import unittest 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(). # TODO - Write tests for flock() and lockf().
......
...@@ -3,14 +3,12 @@ ...@@ -3,14 +3,12 @@
import os import os
import time import time
import unittest
from test.fork_wait import ForkWait 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): class ForkTest(ForkWait):
def wait_impl(self, cpid): def wait_impl(self, cpid):
......
"""Test script for the grp module.""" """Test script for the grp module."""
import grp
import unittest import unittest
from test import support from test import support
grp = support.import_module('grp')
class GroupDatabaseTestCase(unittest.TestCase): class GroupDatabaseTestCase(unittest.TestCase):
def check_value(self, value): def check_value(self, value):
......
import unittest import unittest
from test.support import run_unittest from test.support import run_unittest, import_module, get_attribute
import os, struct import os, struct
try: fcntl = import_module('fcntl')
import fcntl, termios termios = import_module('termios')
except ImportError: get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature
raise unittest.SkipTest("No fcntl or termios module")
if not hasattr(termios,'TIOCGPGRP'):
raise unittest.SkipTest("termios module doesn't have TIOCGPGRP")
try: try:
tty = open("/dev/tty", "r") tty = open("/dev/tty", "r")
......
from test.support import TESTFN, run_unittest from test.support import TESTFN, run_unittest, import_module
import mmap
import unittest import unittest
import os, re, itertools import os, re, itertools
# Skip test if we can't import mmap.
mmap = import_module('mmap')
PAGESIZE = mmap.PAGESIZE PAGESIZE = mmap.PAGESIZE
class MmapTests(unittest.TestCase): class MmapTests(unittest.TestCase):
......
...@@ -17,20 +17,19 @@ import copy ...@@ -17,20 +17,19 @@ import copy
import socket import socket
import random import random
import logging import logging
import test.support
# Work around broken sem_open implementations # Skip tests if _multiprocessing wasn't built.
try: _multiprocessing = test.support.import_module('_multiprocessing')
import multiprocessing.synchronize # Skip tests if sem_open implementation is broken.
except ImportError as e: test.support.import_module('multiprocessing.synchronize')
raise unittest.SkipTest(e)
import multiprocessing.dummy import multiprocessing.dummy
import multiprocessing.connection import multiprocessing.connection
import multiprocessing.managers import multiprocessing.managers
import multiprocessing.heap import multiprocessing.heap
import multiprocessing.pool import multiprocessing.pool
import _multiprocessing
from multiprocessing import util from multiprocessing import util
......
from test import support from test import support
import unittest 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") raise unittest.SkipTest("test_nis hangs on Solaris")
......
...@@ -3,8 +3,9 @@ support.requires('audio') ...@@ -3,8 +3,9 @@ support.requires('audio')
from test.support import findfile from test.support import findfile
ossaudiodev = support.import_module('ossaudiodev')
import errno import errno
import ossaudiodev
import sys import sys
import sunau import sunau
import time import time
......
...@@ -2,17 +2,15 @@ ...@@ -2,17 +2,15 @@
from test import support from test import support
try:
import posix
except ImportError:
raise unittest.SkipTest("posix is not available")
import time import time
import os import os
import pwd import pwd
import shutil import shutil
import unittest import unittest
import warnings import warnings
posix = support.import_module('posix')
warnings.filterwarnings('ignore', '.* potential security risk .*', warnings.filterwarnings('ignore', '.* potential security risk .*',
RuntimeWarning) RuntimeWarning)
......
import unittest import unittest
from test import support from test import support
import pwd pwd = support.import_module('pwd')
class PwdTest(unittest.TestCase): class PwdTest(unittest.TestCase):
......
import unittest import unittest
from test import support from test import support
import resource
import time import time
resource = support.import_module('resource')
# This test is checking a few specific problem spots with the resource module. # This test is checking a few specific problem spots with the resource module.
class ResourceTest(unittest.TestCase): class ResourceTest(unittest.TestCase):
......
import unittest 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, from sqlite3.test import (dbapi, types, userfunctions,
factory, transactions, hooks, regression, factory, transactions, hooks, regression,
dump) dump)
......
...@@ -9,9 +9,11 @@ ...@@ -9,9 +9,11 @@
import unittest import unittest
from test import support 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): class TestCase(unittest.TestCase):
def test_nonexisting(self): def test_nonexisting(self):
......
...@@ -2,8 +2,11 @@ ...@@ -2,8 +2,11 @@
import unittest import unittest
import os import os
import _tkinter
from test import support 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 Tcl
from _tkinter import TclError from _tkinter import TclError
......
...@@ -3,6 +3,11 @@ from tkinter.test import runtktests ...@@ -3,6 +3,11 @@ from tkinter.test import runtktests
from test import support from test import support
import unittest import unittest
# Skip test if _tkinter wasn't built.
support.import_module('_tkinter')
import tkinter
try: try:
tkinter.Button() tkinter.Button()
except tkinter.TclError as msg: except tkinter.TclError as msg:
......
import os import os
import sys import sys
from tkinter import ttk
from tkinter.test import runtktests
import unittest import unittest
from _tkinter import TclError
from test import support 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: try:
ttk.Button() ttk.Button()
except TclError as msg: except TclError as msg:
......
import os import os
import sys import sys
from test import support from test import support
# Skip this test if _tkinter does not exist.
support.import_module('_tkinter')
from tkinter.test import runtktests from tkinter.test import runtktests
def test_main(): def test_main():
......
...@@ -2,12 +2,15 @@ ...@@ -2,12 +2,15 @@
# Test the windows specific win32reg module. # Test the windows specific win32reg module.
# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
from winreg import *
import os, sys import os, sys
import unittest import unittest
from test import support 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_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
test_data = [ test_data = [
......
...@@ -3,10 +3,12 @@ ...@@ -3,10 +3,12 @@
import unittest import unittest
from test import support from test import support
support.requires('audio') support.requires('audio')
import winsound, time import time
import os import os
import subprocess import subprocess
winsound = support.import_module('winsound')
class BeepTest(unittest.TestCase): class BeepTest(unittest.TestCase):
# As with PlaySoundTest, incorporate the _have_soundcard() check # As with PlaySoundTest, incorporate the _have_soundcard() check
......
...@@ -5,7 +5,7 @@ import sys ...@@ -5,7 +5,7 @@ import sys
from test import support from test import support
from xml.etree import cElementTree as ET ET = support.import_module('xml.etree.cElementTree')
SAMPLE_XML = """ SAMPLE_XML = """
<body> <body>
......
import unittest import unittest
from test import support from test import support
import zlib
import binascii import binascii
import random import random
zlib = support.import_module('zlib')
class ChecksumTestCase(unittest.TestCase): class ChecksumTestCase(unittest.TestCase):
# checksum test cases # checksum test cases
......
...@@ -725,6 +725,10 @@ Extension Modules ...@@ -725,6 +725,10 @@ Extension Modules
Tests 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. - Issue #5083: New 'gui' resource for regrtest.
......
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