From: R. David Murray Date: Mon, 30 Mar 2009 19:04:00 +0000 (+0000) Subject: Add import_function method to test.test_support, and modify a number of X-Git-Tag: v2.7a1~1740 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=59beec326a4721154a952027cb34c3e2e4612e28;p=python 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. --- diff --git a/Lib/test/test_aepack.py b/Lib/test/test_aepack.py index 8a4b035f05..5d4ab3e20b 100755 --- a/Lib/test/test_aepack.py +++ b/Lib/test/test_aepack.py @@ -1,11 +1,12 @@ # Copyright (C) 2003 Python Software Foundation import unittest -import aepack -import aetypes import os from test import test_support +aetypes = test_support.import_module('aetypes') +aepack = test_support.import_module('aepack') + class TestAepack(unittest.TestCase): OBJECTS = [ aetypes.Enum('enum'), diff --git a/Lib/test/test_applesingle.py b/Lib/test/test_applesingle.py index e915028dca..5a8201c3d0 100644 --- a/Lib/test/test_applesingle.py +++ b/Lib/test/test_applesingle.py @@ -1,13 +1,15 @@ # Copyright (C) 2003 Python Software Foundation import unittest -import macostools -import Carbon.File -import MacOS import os from test import test_support import struct + +MacOS = test_support.import_module('MacOS') +# The following should exist if MacOS does. +import macostools import applesingle +import Carbon.File AS_MAGIC=0x00051600 AS_VERSION=0x00020000 diff --git a/Lib/test/test_asynchat.py b/Lib/test/test_asynchat.py index 5501c2defc..14528d8800 100644 --- a/Lib/test/test_asynchat.py +++ b/Lib/test/test_asynchat.py @@ -1,11 +1,13 @@ -# test asynchat -- requires threading +# test asynchat -import thread # If this fails, we can't test this module import asyncore, asynchat, socket, threading, time import unittest import sys from test import test_support +# Skip tests if thread module does not exist. +test_support.import_module('thread') + HOST = test_support.HOST SERVER_QUIT = 'QUIT\n' diff --git a/Lib/test/test_cd.py b/Lib/test/test_cd.py index daad223828..edaa82bf45 100755 --- a/Lib/test/test_cd.py +++ b/Lib/test/test_cd.py @@ -2,8 +2,9 @@ """Whimpy test script for the cd module Roger E. Masse """ -import cd -from test.test_support import verbose +from test.test_support import verbose, import_module + +cd = import_module('cd') cdattrs = ['BLOCKSIZE', 'CDROM', 'DATASIZE', 'ERROR', 'NODISC', 'PAUSED', 'PLAYING', 'READY', 'STILL', '__doc__', '__name__', 'atime', 'audio', 'catalog', 'control', 'createparser', 'error', diff --git a/Lib/test/test_cl.py b/Lib/test/test_cl.py index a2728b7df1..50102e9267 100755 --- a/Lib/test/test_cl.py +++ b/Lib/test/test_cl.py @@ -2,8 +2,9 @@ """Whimpy test script for the cl module Roger E. Masse """ -import cl -from test.test_support import verbose +from test.test_support import verbose, import_module + +cl = import_module('cl') clattrs = ['ADDED_ALGORITHM_ERROR', 'ALAW', 'ALGORITHM_ID', 'ALGORITHM_VERSION', 'AUDIO', 'AWARE_ERROR', 'AWARE_MPEG_AUDIO', diff --git a/Lib/test/test_crypt.py b/Lib/test/test_crypt.py index f6f3a8171e..4db200d403 100755 --- a/Lib/test/test_crypt.py +++ b/Lib/test/test_crypt.py @@ -1,6 +1,7 @@ from test import test_support import unittest -import crypt + +crypt = test_support.import_module('crypt') class CryptTestCase(unittest.TestCase): diff --git a/Lib/test/test_fork1.py b/Lib/test/test_fork1.py index dfd501665c..9306242120 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.test_support import run_unittest, reap_children +from test.test_support import run_unittest, reap_children, import_function + +#Skip test if fork does not exist. +import_function(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_gl.py b/Lib/test/test_gl.py index 1da55a071a..be76041863 100755 --- a/Lib/test/test_gl.py +++ b/Lib/test/test_gl.py @@ -3,8 +3,10 @@ taken mostly from the documentation. Roger E. Masse """ -from test.test_support import verbose -import gl, GL, time +from test.test_support import verbose, import_module +import time +gl = import_module('gl') +GL = import_module('GL') glattrs = ['RGBcolor', 'RGBcursor', 'RGBmode', 'RGBrange', 'RGBwritemask', '__doc__', '__name__', 'addtopup', 'altgetmatrix', 'arc', 'arcf', diff --git a/Lib/test/test_grp.py b/Lib/test/test_grp.py index 564c7f0067..b38b94c5a5 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 test_support +grp = test_support.import_module('grp') + class GroupDatabaseTestCase(unittest.TestCase): def check_value(self, value): diff --git a/Lib/test/test_macos.py b/Lib/test/test_macos.py index a9ff0b21d3..ce5f858dde 100644 --- a/Lib/test/test_macos.py +++ b/Lib/test/test_macos.py @@ -1,10 +1,12 @@ import unittest -import MacOS -import Carbon.File from test import test_support import os import subprocess +MacOS = test_support.import_module('MacOS') +#The following should exist if MacOS exists. +import Carbon.File + TESTFN2 = test_support.TESTFN + '2' class TestMacOS(unittest.TestCase): diff --git a/Lib/test/test_macostools.py b/Lib/test/test_macostools.py index 6110ce3dff..b84ad7202e 100644 --- a/Lib/test/test_macostools.py +++ b/Lib/test/test_macostools.py @@ -1,13 +1,15 @@ # Copyright (C) 2003 Python Software Foundation import unittest -import macostools -import Carbon.File -import MacOS import os import sys from test import test_support +MacOS = test_support.import_module('MacOS') +#The following modules should exist if MacOS exists. +import Carbon.File +import macostools + TESTFN2 = test_support.TESTFN + '2' class TestMacostools(unittest.TestCase): diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 31daa8df11..2dd76ad9ff 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -1,8 +1,9 @@ -from test.test_support import TESTFN, run_unittest -import mmap +from test.test_support import TESTFN, run_unittest, import_module import unittest import os, re +mmap = import_module('mmap') + PAGESIZE = mmap.PAGESIZE class MmapTests(unittest.TestCase): diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 864f96373a..a7255c41c5 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -2,10 +2,6 @@ from test import test_support -try: - import posix -except ImportError: - raise unittest.SkipTest, "posix is not available" import time import os @@ -13,6 +9,9 @@ import pwd import shutil import unittest import warnings + +posix = test_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 255a34c6d4..de4084e488 100644 --- a/Lib/test/test_pwd.py +++ b/Lib/test/test_pwd.py @@ -1,7 +1,7 @@ import unittest from test import test_support -import pwd +pwd = test_support.import_module('pwd') class PwdTest(unittest.TestCase): diff --git a/Lib/test/test_scriptpackages.py b/Lib/test/test_scriptpackages.py index ee14be2bb4..a3a1857d85 100644 --- a/Lib/test/test_scriptpackages.py +++ b/Lib/test/test_scriptpackages.py @@ -2,7 +2,8 @@ import unittest from test import test_support -import aetools + +aetools = test_support.import_module('aetools') class TestScriptpackages(unittest.TestCase): diff --git a/Lib/test/test_startfile.py b/Lib/test/test_startfile.py index c4d12d708e..23e2708cc1 100644 --- a/Lib/test/test_startfile.py +++ b/Lib/test/test_startfile.py @@ -9,9 +9,11 @@ import unittest from test import test_support +import os +from os import path + +startfile = test_support.import_function(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_support.py b/Lib/test/test_support.py index 4ad24015c8..90bd8e6a05 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -55,6 +55,20 @@ def import_module(name, deprecated=False): else: return module +def import_function(module, name, deprecated=False): + with warnings.catch_warnings(): + if deprecated: + warnings.filterwarnings("ignore", ".+ (module|package)", + DeprecationWarning) + try: + function = getattr(module, name) + except AttributeError: + raise unittest.SkipTest("No function named %s in module %s" % ( + name, module.__name__)) + else: + return function + + 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_ttk_guionly.py b/Lib/test/test_ttk_guionly.py index 831a22221a..aa17c4772e 100644 --- a/Lib/test/test_ttk_guionly.py +++ b/Lib/test/test_ttk_guionly.py @@ -1,10 +1,12 @@ import os import sys -import ttk import unittest -from _tkinter import TclError from test import test_support +ttk = test_support.import_module('ttk') +#If ttk exists _tkinter must exist. +from _tkinter import TclError + try: ttk.Button() except TclError, msg: diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py index f2ec2a8c0f..1dbccc0027 100644 --- a/Lib/test/test_winreg.py +++ b/Lib/test/test_winreg.py @@ -1,12 +1,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 test_support +#Do this first so test will be skipped if module doesn't exist +test_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 1ed43c162d..cba6ee6d36 100644 --- a/Lib/test/test_winsound.py +++ b/Lib/test/test_winsound.py @@ -2,10 +2,12 @@ import unittest from test import test_support -import winsound, time +import time import os import subprocess +winsound = test_support.import_module('winsound') + class BeepTest(unittest.TestCase): # As with PlaySoundTest, incorporate the _have_soundcard() check