Index: Lib/ctypes/test/test_errno.py =================================================================== --- Lib/ctypes/test/test_errno.py (revision 76735) +++ Lib/ctypes/test/test_errno.py (working copy) @@ -1,7 +1,8 @@ import unittest, os, errno from ctypes import * from ctypes.util import find_library -import threading +from test import test_support +threading = test_support.import_module('threading') class Test(unittest.TestCase): def test_open(self): Index: Lib/test/test_urllib2_localnet.py =================================================================== --- Lib/test/test_urllib2_localnet.py (revision 76735) +++ Lib/test/test_urllib2_localnet.py (working copy) @@ -1,13 +1,13 @@ #!/usr/bin/env python import mimetools -import threading import urlparse import urllib2 import BaseHTTPServer import unittest import hashlib from test import test_support +threading = test_support.import_module('threading') # Loopback http server infrastructure Index: Lib/test/test_xmlrpc.py =================================================================== --- Lib/test/test_xmlrpc.py (revision 76735) +++ Lib/test/test_xmlrpc.py (working copy) @@ -5,7 +5,6 @@ import unittest import xmlrpclib import SimpleXMLRPCServer -import threading import mimetools import httplib import socket @@ -15,6 +14,11 @@ from test import test_support try: + import threading +except ImportError: + threading = None + +try: unicode except NameError: have_unicode = False @@ -38,7 +42,7 @@ }] class XMLRPCTestCase(unittest.TestCase): - + def test_dump_load(self): self.assertEquals(alist, xmlrpclib.loads(xmlrpclib.dumps((alist,)))[0][0]) @@ -414,6 +418,8 @@ requestHandler = None request_count = 1 threadFunc = staticmethod(http_server) + + @test_support.skip_if_no('threading') def setUp(self): # enable traceback reporting SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True @@ -690,6 +696,10 @@ connection.putheader("Content-Encoding", "gzip") return xmlrpclib.Transport.send_content(self, connection, body) + @test_support.skip_if_no('threading') + def setUp(self): + BaseServerTestCase.setUp(self) + def test_gzip_request(self): t = self.Transport() t.encode_threshold = None @@ -727,12 +737,12 @@ #Test special attributes of the ServerProxy object class ServerProxyTestCase(unittest.TestCase): def test_close(self): - p = xmlrpclib.ServerProxy(URL) + p = xmlrpclib.ServerProxy('http:') self.assertEqual(p('close')(), None) def test_transport(self): t = xmlrpclib.Transport() - p = xmlrpclib.ServerProxy(URL, transport=t) + p = xmlrpclib.ServerProxy('http:', transport=t) self.assertEqual(p('transport'), t) # This is a contrived way to make a failure occur on the server side @@ -746,6 +756,7 @@ class FailingServerTestCase(unittest.TestCase): + @test_support.skip_if_no('threading') def setUp(self): self.evt = threading.Event() # start server thread to handle requests Index: Lib/test/test_file2k.py =================================================================== --- Lib/test/test_file2k.py (revision 76735) +++ Lib/test/test_file2k.py (working copy) @@ -3,14 +3,20 @@ import unittest import itertools import time -import threading from array import array from weakref import proxy +# threading not available is handled by a guard around the specific +# tests that use it. +try: + import threading +except ImportError: + threading = None from test import test_support from test.test_support import TESTFN, findfile, run_unittest from UserList import UserList + class AutoFileTests(unittest.TestCase): # file tests for which a test file is automatically set up @@ -373,6 +379,7 @@ # (including close()) concurrently without crashing the Python interpreter. # See #815646, #595601 + @test_support.skip_if_no('threading') def setUp(self): self._threads = test_support.threading_setup() self.f = None Index: Lib/test/test_cmd.py =================================================================== --- Lib/test/test_cmd.py (revision 76735) +++ Lib/test/test_cmd.py (working copy) @@ -169,8 +169,9 @@ from test import test_support, test_cmd test_support.run_doctest(test_cmd, verbose) -import trace, sys +import sys def test_coverage(coverdir): + trace = test_support.import_module('trace') tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, count=1) tracer.run('reload(cmd);test_main()') Index: Lib/test/test_io.py =================================================================== --- Lib/test/test_io.py (revision 76735) +++ Lib/test/test_io.py (working copy) @@ -26,7 +26,6 @@ import sys import time import array -import threading import random import unittest import warnings @@ -735,7 +734,9 @@ self.assertEquals(b"abcdefg", bufio.read()) + @support.skip_if_no('threading') def test_threads(self): + import threading try: # Write out many bytes with exactly the same number of 0's, # 1's... 255's. This will help us check that concurrent reading @@ -982,7 +983,9 @@ with self.open(support.TESTFN, "rb", buffering=0) as f: self.assertEqual(f.read(), b"abc") + @support.skip_if_no('threading') def test_threads(self): + import threading try: # Write out many bytes from many threads and test they were # all flushed. @@ -2060,8 +2063,9 @@ with self.open(support.TESTFN, "w", errors="replace") as f: self.assertEqual(f.errors, "replace") - + @support.skip_if_no('threading') def test_threads_write(self): + import threading # Issue6750: concurrent writes could duplicate data event = threading.Event() with self.open(support.TESTFN, "w", buffering=1) as f: Index: Lib/test/test_smtplib.py =================================================================== --- Lib/test/test_smtplib.py (revision 76735) +++ Lib/test/test_smtplib.py (working copy) @@ -1,7 +1,6 @@ import asyncore import email.utils import socket -import threading import smtpd import smtplib import StringIO @@ -12,6 +11,8 @@ from unittest import TestCase from test import test_support +threading = test_support.import_module('threading') + HOST = test_support.HOST def server(evt, buf, serv): Index: Lib/test/test_macostools.py =================================================================== --- Lib/test/test_macostools.py (revision 76735) +++ Lib/test/test_macostools.py (working copy) @@ -77,16 +77,16 @@ self.assertEqual(fss.as_pathname(), os.path.realpath(test_support.TESTFN)) def test_mkalias_relative(self): - try: - os.unlink(TESTFN2) - except: - pass # If the directory doesn't exist, then chances are this is a new # install of Python so don't create it since the user might end up # running ``sudo make install`` and creating the directory here won't # leave it with the proper permissions. - if not os.path.exists(sys.prefix): + if not os.path.isdir(sys.prefix): return + try: + os.unlink(os.path.join(sys.prefix, TESTFN2)) + except: + pass macostools.mkalias(test_support.TESTFN, TESTFN2, sys.prefix) fss, _, _ = Carbon.File.ResolveAliasFile(TESTFN2, 0) self.assertEqual(fss.as_pathname(), os.path.realpath(test_support.TESTFN)) Index: Lib/test/test_asyncore.py =================================================================== --- Lib/test/test_asyncore.py (revision 76735) +++ Lib/test/test_asyncore.py (working copy) @@ -3,7 +3,6 @@ import select import os import socket -import threading import sys import time @@ -319,8 +318,10 @@ def tearDown(self): asyncore.close_all() + @test_support.skip_if_no('threading') @test_support.reap_threads def test_send(self): + import threading evt = threading.Event() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(3) Index: Lib/test/test_threadedtempfile.py =================================================================== --- Lib/test/test_threadedtempfile.py (revision 76735) +++ Lib/test/test_threadedtempfile.py (working copy) @@ -16,11 +16,10 @@ NUM_THREADS = 20 FILES_PER_THREAD = 50 -import thread # If this fails, we can't test this module -import threading import tempfile -from test.test_support import threading_setup, threading_cleanup, run_unittest +from test.test_support import threading_setup, threading_cleanup, run_unittest, import_module +threading = import_module('threading') import unittest import StringIO from traceback import print_exc Index: Lib/test/test_fork1.py =================================================================== --- Lib/test/test_fork1.py (revision 76735) +++ Lib/test/test_fork1.py (working copy) @@ -7,10 +7,10 @@ import signal import sys import time -import threading from test.fork_wait import ForkWait -from test.test_support import run_unittest, reap_children, get_attribute +from test.test_support import run_unittest, reap_children, get_attribute, import_module +threading = import_module('threading') #Skip test if fork does not exist. get_attribute(os, 'fork') Index: Lib/test/test_socket.py =================================================================== --- Lib/test/test_socket.py (revision 76735) +++ Lib/test/test_socket.py (working copy) @@ -6,7 +6,8 @@ import errno import socket import select -import thread, threading +thread = test_support.import_module('thread') +threading = test_support.import_module('threading') import time import traceback import Queue Index: Lib/test/test_docxmlrpc.py =================================================================== --- Lib/test/test_docxmlrpc.py (revision 76735) +++ Lib/test/test_docxmlrpc.py (working copy) @@ -1,7 +1,7 @@ from DocXMLRPCServer import DocXMLRPCServer import httplib from test import test_support -import threading +threading = test_support.import_module('threading') import time import unittest import xmlrpclib Index: Lib/test/test_logging.py =================================================================== --- Lib/test/test_logging.py (revision 76735) +++ Lib/test/test_logging.py (working copy) @@ -40,8 +40,8 @@ import sys import tempfile from test.test_support import captured_stdout, run_with_locale, run_unittest +import test.test_support as test_support import textwrap -import threading import time import types import unittest @@ -747,6 +747,7 @@ port=logging.handlers.DEFAULT_TCP_LOGGING_PORT, handler=LogRecordStreamHandler): ThreadingTCPServer.__init__(self, (host, port), handler) + import threading self.abort = False self.timeout = 0.1 self.finished = threading.Event() @@ -767,9 +768,11 @@ """Test for SocketHandler objects.""" + @test_support.skip_if_no('threading') def setUp(self): """Set up a TCP server to receive log messages, and a SocketHandler pointing to that server's address and port.""" + import threading BaseTest.setUp(self) self.tcpserver = LogRecordSocketReceiver(port=0) self.port = self.tcpserver.socket.getsockname()[1] Index: Lib/test/test_threadsignals.py =================================================================== --- Lib/test/test_threadsignals.py (revision 76735) +++ Lib/test/test_threadsignals.py (working copy) @@ -1,11 +1,11 @@ """PyUnit testing that threads honor our signal semantics""" import unittest -import thread import signal import os import sys -from test.test_support import run_unittest +from test.test_support import run_unittest, import_module +thread = import_module('thread') if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos': raise unittest.SkipTest, "Can't test signal on %s" % sys.platform Index: Lib/test/test_doctest.py =================================================================== --- Lib/test/test_doctest.py (revision 76735) +++ Lib/test/test_doctest.py (working copy) @@ -2423,8 +2423,9 @@ from test import test_doctest test_support.run_doctest(test_doctest, verbosity=True) -import trace, sys +import sys def test_coverage(coverdir): + trace = test_support.import_module('trace') tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, count=1) tracer.run('reload(doctest); test_main()') Index: Lib/test/test_thread.py =================================================================== --- Lib/test/test_thread.py (revision 76735) +++ Lib/test/test_thread.py (working copy) @@ -2,7 +2,7 @@ import unittest import random from test import test_support -import thread +thread = test_support.import_module('thread') import time import weakref Index: Lib/test/test_hashlib.py =================================================================== --- Lib/test/test_hashlib.py (revision 76735) +++ Lib/test/test_hashlib.py (working copy) @@ -216,10 +216,9 @@ "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") + @test_support.skip_if_no('thread') + @test_support.reap_threads def test_threaded_hashing(self): - if not threading: - raise unittest.SkipTest('No threading module.') - # Updating the same hash object from several threads at once # using data chunk sizes containing the same byte sequences. # @@ -254,7 +253,6 @@ self.assertEqual(expected_hash, hasher.hexdigest()) -@test_support.reap_threads def test_main(): test_support.run_unittest(HashLibTestCase) Index: Lib/test/test_capi.py =================================================================== --- Lib/test/test_capi.py (revision 76735) +++ Lib/test/test_capi.py (working copy) @@ -6,8 +6,8 @@ import time import random import unittest -import threading from test import test_support +threading = test_support.import_module('threading') import _testcapi class TestPendingCalls(unittest.TestCase): @@ -47,7 +47,8 @@ print "(%i)"%(len(l),) def test_pendingcalls_threaded(self): - + import threading + #do every callback on a separate thread n = 32 #total callbacks threads = [] Index: Lib/test/test_asynchat.py =================================================================== --- Lib/test/test_asynchat.py (revision 76735) +++ Lib/test/test_asynchat.py (working copy) @@ -1,13 +1,11 @@ # test asynchat -import asyncore, asynchat, socket, threading, time +import asyncore, asynchat, socket, time import unittest import sys from test import test_support +threading = test_support.import_module('threading') -# Skip tests if thread module does not exist. -test_support.import_module('thread') - HOST = test_support.HOST SERVER_QUIT = 'QUIT\n' Index: Lib/test/test_socketserver.py =================================================================== --- Lib/test/test_socketserver.py (revision 76735) +++ Lib/test/test_socketserver.py (working copy) @@ -10,13 +10,12 @@ import signal import socket import tempfile -import threading import time import unittest import SocketServer import test.test_support -from test.test_support import reap_children, reap_threads, verbose +from test.test_support import reap_children, reap_threads, verbose, skip_if_no from test.test_support import TESTFN as TEST_FILE test.test_support.requires("network") @@ -122,8 +121,10 @@ self.assertEquals(server.server_address, server.socket.getsockname()) return server + @skip_if_no('threading') @reap_threads def run_server(self, svrcls, hdlrbase, testfunc): + import threading server = self.make_server(self.pickaddr(svrcls.address_family), svrcls, hdlrbase) # We had the OS pick a port, so pull the real address out of Index: Lib/test/test_queue.py =================================================================== --- Lib/test/test_queue.py (revision 76735) +++ Lib/test/test_queue.py (working copy) @@ -2,10 +2,10 @@ # to ensure the Queue locks remain stable. import Queue import sys -import threading import time import unittest from test import test_support +threading = test_support.import_module('threading') QUEUE_SIZE = 5 Index: Lib/test/test_poplib.py =================================================================== --- Lib/test/test_poplib.py (revision 76735) +++ Lib/test/test_poplib.py (working copy) @@ -4,7 +4,6 @@ # a real test suite import poplib -import threading import asyncore import asynchat import socket @@ -14,8 +13,8 @@ from unittest import TestCase from test import test_support from test.test_support import HOST +threading = test_support.import_module('threading') - # the dummy data returned by server when LIST and RETR commands are issued LIST_RESP = '1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n.\r\n' RETR_RESP = """From: postmaster@python.org\ Index: Lib/test/test_bz2.py =================================================================== --- Lib/test/test_bz2.py (revision 76735) +++ Lib/test/test_bz2.py (working copy) @@ -7,7 +7,6 @@ import os import subprocess import sys -import threading bz2 = import_module('bz2') from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor @@ -307,7 +306,9 @@ else: self.fail("1/0 didn't raise an exception") + @test_support.skip_if_no('threading') def testThreading(self): + import threading # Using a BZ2File from several threads doesn't deadlock (issue #7205). data = "1" * 2**20 nthreads = 10 Index: Lib/test/test_httpservers.py =================================================================== --- Lib/test/test_httpservers.py (revision 76735) +++ Lib/test/test_httpservers.py (working copy) @@ -16,10 +16,10 @@ import urllib import httplib import tempfile -import threading import unittest from test import test_support +threading = test_support.import_module('threading') class NoLogRequestHandler: Index: Lib/test/test_contextlib.py =================================================================== --- Lib/test/test_contextlib.py (revision 76735) +++ Lib/test/test_contextlib.py (working copy) @@ -6,11 +6,15 @@ import decimal import tempfile import unittest -import threading from contextlib import * # Tests __all__ from test import test_support import warnings +try: + import threading +except ImportError: + threading = None + class ContextManagerTestCase(unittest.TestCase): def test_contextmanager_plain(self): @@ -296,20 +300,24 @@ else: self.fail("Didn't raise ZeroDivisionError") + @test_support.skip_if_no('threading') def testWithLock(self): lock = threading.Lock() self.boilerPlate(lock, lock.locked) + @test_support.skip_if_no('threading') def testWithRLock(self): lock = threading.RLock() self.boilerPlate(lock, lock._is_owned) + @test_support.skip_if_no('threading') def testWithCondition(self): lock = threading.Condition() def locked(): return lock._is_owned() self.boilerPlate(lock, locked) + @test_support.skip_if_no('threading') def testWithSemaphore(self): lock = threading.Semaphore() def locked(): @@ -320,6 +328,7 @@ return True self.boilerPlate(lock, locked) + @test_support.skip_if_no('threading') def testWithBoundedSemaphore(self): lock = threading.BoundedSemaphore() def locked(): Index: Lib/test/test_threaded_import.py =================================================================== --- Lib/test/test_threaded_import.py (revision 76735) +++ Lib/test/test_threaded_import.py (working copy) @@ -5,8 +5,8 @@ # complains several times about module random having no attribute # randrange, and then Python hangs. -import thread -from test.test_support import verbose, TestFailed +from test.test_support import verbose, TestFailed, import_module +thread = import_module('thread') critical_section = thread.allocate_lock() done = thread.allocate_lock() Index: Lib/test/test_ftplib.py =================================================================== --- Lib/test/test_ftplib.py (revision 76735) +++ Lib/test/test_ftplib.py (working copy) @@ -4,7 +4,6 @@ # environment import ftplib -import threading import asyncore import asynchat import socket @@ -19,8 +18,8 @@ from unittest import TestCase from test import test_support from test.test_support import HOST +threading = test_support.import_module('threading') - # the dummy data returned by server over the data channel when # RETR, LIST and NLST commands are issued RETR_DATA = 'abcde12345\r\n' * 1000 Index: Lib/test/test_telnetlib.py =================================================================== --- Lib/test/test_telnetlib.py (revision 76735) +++ Lib/test/test_telnetlib.py (working copy) @@ -1,11 +1,11 @@ import socket -import threading import telnetlib import time import Queue from unittest import TestCase from test import test_support +threading = test_support.import_module('threading') HOST = test_support.HOST EOF_sigil = object() @@ -43,6 +43,7 @@ class GeneralTests(TestCase): def setUp(self): + import threading self.evt = threading.Event() self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(3) Index: Lib/test/fork_wait.py =================================================================== --- Lib/test/fork_wait.py (revision 76735) +++ Lib/test/fork_wait.py (working copy) @@ -1,6 +1,6 @@ """This test case provides support for checking forking and wait behavior. -To test different wait behavior, overrise the wait_impl method. +To test different wait behavior, override the wait_impl method. We want fork1() semantics -- only the forking thread survives in the child after a fork(). @@ -12,7 +12,8 @@ the same application, the present example should work just fine. DC """ -import os, sys, time, thread, unittest +import os, sys, time, unittest +import test_support LONGSLEEP = 2 SHORTSLEEP = 0.5 @@ -44,7 +45,9 @@ self.assertEquals(spid, cpid) self.assertEquals(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) + @test_support.skip_if_no('thread') def test_wait(self): + import thread for i in range(NUM_THREADS): thread.start_new(self.f, (i,)) Index: Lib/test/test_sqlite.py =================================================================== --- Lib/test/test_sqlite.py (revision 76735) +++ Lib/test/test_sqlite.py (working copy) @@ -3,6 +3,8 @@ # Skip test if _sqlite3 module was not built. import_module('_sqlite3') +# Skip test if threading is not enabled. +import_module('threading') from sqlite3.test import (dbapi, types, userfunctions, py25tests, factory, transactions, hooks, regression, Index: Lib/test/test_threading_local.py =================================================================== --- Lib/test/test_threading_local.py (revision 76735) +++ Lib/test/test_threading_local.py (working copy) @@ -1,7 +1,7 @@ import unittest from doctest import DocTestSuite from test import test_support -import threading +threading = test_support.import_module('threading') import weakref import gc Index: Lib/test/test_multiprocessing.py =================================================================== --- Lib/test/test_multiprocessing.py (revision 76735) +++ Lib/test/test_multiprocessing.py (working copy) @@ -5,7 +5,6 @@ # import unittest -import threading import Queue import time import sys @@ -21,6 +20,7 @@ from StringIO import StringIO +threading = test_support.import_module('threading') _multiprocessing = test_support.import_module('_multiprocessing') # Work around broken sem_open implementations Index: Lib/test/test_threading.py =================================================================== --- Lib/test/test_threading.py (revision 76735) +++ Lib/test/test_threading.py (working copy) @@ -5,8 +5,8 @@ import random import re import sys -import threading -import thread +thread = test.test_support.import_module('thread') +threading = test.test_support.import_module('threading') import time import unittest import weakref Index: Lib/test/test_support.py =================================================================== --- Lib/test/test_support.py (revision 76735) +++ Lib/test/test_support.py (working copy) @@ -41,7 +41,7 @@ """Test skipped because it requested a disallowed resource. This is raised when a test calls requires() for a resource that - has not be enabled. It is used to distinguish between expected + has not been enabled. It is used to distinguish between expected and unexpected skips. """ @@ -987,11 +987,20 @@ def reap_threads(func): @functools.wraps(func) def decorator(*args): - key = threading_setup() + have_threads = True try: + import thread + except ImportError: + have_threads = False + + if have_threads: + key = threading_setup() + try: + return func(*args) + finally: + threading_cleanup(*key) + else: return func(*args) - finally: - threading_cleanup(*key) return decorator def reap_children(): @@ -1014,6 +1023,15 @@ except: break +def skip_if_no(module_name): + '''Raise a SkipTest exception if module_name cannot be imported.''' + def decorator(func): + def wrapper(*args): + import_module(module_name) + return func(*args) + return wrapper + return decorator + def py3k_bytes(b): """Emulate the py3k bytes() constructor.