Index: Lib/ctypes/test/test_errno.py =================================================================== --- Lib/ctypes/test/test_errno.py (revision 78619) +++ 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/sqlite3/test/dbapi.py =================================================================== --- Lib/sqlite3/test/dbapi.py (revision 78619) +++ Lib/sqlite3/test/dbapi.py (working copy) @@ -23,8 +23,11 @@ import unittest import sys -import threading import sqlite3 as sqlite +try: + import threading +except ImportError: + threading = None class ModuleTests(unittest.TestCase): def CheckAPILevel(self): @@ -465,6 +468,7 @@ except TypeError: pass +@unittest.skipUnless(threading, 'This test requires threading.') class ThreadTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") Index: Lib/test/test_urllib2_localnet.py =================================================================== --- Lib/test/test_urllib2_localnet.py (revision 78619) +++ Lib/test/test_urllib2_localnet.py (working copy) @@ -1,6 +1,5 @@ #!/usr/bin/env python -import threading import urlparse import urllib2 import BaseHTTPServer @@ -8,6 +7,7 @@ import hashlib from test import test_support mimetools = test_support.import_module('mimetools', deprecated=True) +threading = test_support.import_module('threading') # Loopback http server infrastructure Index: Lib/test/test_xmlrpc.py =================================================================== --- Lib/test/test_xmlrpc.py (revision 78619) +++ 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 @@ -414,6 +418,8 @@ requestHandler = None request_count = 1 threadFunc = staticmethod(http_server) + + @unittest.skipUnless(threading, 'Threading required for this test.') def setUp(self): # enable traceback reporting SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True @@ -692,6 +698,10 @@ connection.putheader("Content-Encoding", "gzip") return xmlrpclib.Transport.send_content(self, connection, body) + @unittest.skipUnless(threading, 'Threading required for this test.') + def setUp(self): + BaseServerTestCase.setUp(self) + def test_gzip_request(self): t = self.Transport() t.encode_threshold = None @@ -728,13 +738,20 @@ #Test special attributes of the ServerProxy object class ServerProxyTestCase(unittest.TestCase): + def setUp(self): + unittest.TestCase.setUp(self) + if threading: + self.url = URL + else: + self.url = 'http:' + def test_close(self): - p = xmlrpclib.ServerProxy(URL) + p = xmlrpclib.ServerProxy(self.url) self.assertEqual(p('close')(), None) def test_transport(self): t = xmlrpclib.Transport() - p = xmlrpclib.ServerProxy(URL, transport=t) + p = xmlrpclib.ServerProxy(self.url, transport=t) self.assertEqual(p('transport'), t) # This is a contrived way to make a failure occur on the server side @@ -748,6 +765,7 @@ class FailingServerTestCase(unittest.TestCase): + @unittest.skipUnless(threading, 'Threading required for this test.') 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 78619) +++ Lib/test/test_file2k.py (working copy) @@ -3,9 +3,12 @@ import unittest import itertools import time -import threading from array import array from weakref import proxy +try: + import threading +except ImportError: + threading = None from test import test_support from test.test_support import TESTFN, run_unittest @@ -409,6 +412,7 @@ # (including close()) concurrently without crashing the Python interpreter. # See #815646, #595601 + @unittest.skipUnless(threading, 'Threading required for this test.') def setUp(self): self._threads = test_support.threading_setup() self.f = None Index: Lib/test/test_cmd.py =================================================================== --- Lib/test/test_cmd.py (revision 78619) +++ Lib/test/test_cmd.py (working copy) @@ -7,6 +7,7 @@ import cmd import sys +from test import test_support class samplecmdclass(cmd.Cmd): """ @@ -168,11 +169,11 @@ return True def test_main(verbose=None): - from test import test_support, test_cmd + from test import test_cmd test_support.run_doctest(test_cmd, verbose) def test_coverage(coverdir): - import trace + 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 78619) +++ 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 @@ -39,6 +38,10 @@ import codecs import io # C implementation of io import _pyio as pyio # Python implementation of io +try: + import threading +except ImportError: + threading = None __metaclass__ = type bytes = support.py3k_bytes @@ -750,6 +753,7 @@ self.assertEquals(b"abcdefg", bufio.read()) + @unittest.skipUnless(threading, 'Threading required for this test.') def test_threads(self): try: # Write out many bytes with exactly the same number of 0's, @@ -997,6 +1001,7 @@ with self.open(support.TESTFN, "rb", buffering=0) as f: self.assertEqual(f.read(), b"abc") + @unittest.skipUnless(threading, 'Threading required for this test.') def test_threads(self): try: # Write out many bytes from many threads and test they were @@ -2101,7 +2106,7 @@ with self.open(support.TESTFN, "w", errors="replace") as f: self.assertEqual(f.errors, "replace") - + @unittest.skipUnless(threading, 'Threading required for this test.') def test_threads_write(self): # Issue6750: concurrent writes could duplicate data event = threading.Event() Index: Lib/test/test_smtplib.py =================================================================== --- Lib/test/test_smtplib.py (revision 78619) +++ 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_asyncore.py =================================================================== --- Lib/test/test_asyncore.py (revision 78619) +++ Lib/test/test_asyncore.py (working copy) @@ -3,7 +3,6 @@ import select import os import socket -import threading import sys import time @@ -11,6 +10,11 @@ from test.test_support import TESTFN, run_unittest, unlink from StringIO import StringIO +try: + import threading +except ImportError: + threading = None + HOST = test_support.HOST class dummysocket: @@ -319,6 +323,7 @@ def tearDown(self): asyncore.close_all() + @unittest.skipUnless(threading, 'Threading required for this test.') @test_support.reap_threads def test_send(self): evt = threading.Event() Index: Lib/test/test_threadedtempfile.py =================================================================== --- Lib/test/test_threadedtempfile.py (revision 78619) +++ 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 78619) +++ Lib/test/test_fork1.py (working copy) @@ -6,10 +6,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 78619) +++ 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 78619) +++ Lib/test/test_docxmlrpc.py (working copy) @@ -2,7 +2,7 @@ import httplib import sys from test import test_support -import threading +threading = test_support.import_module('threading') import time import socket import unittest Index: Lib/test/test_logging.py =================================================================== --- Lib/test/test_logging.py (revision 78619) +++ Lib/test/test_logging.py (working copy) @@ -41,12 +41,14 @@ from test.test_support import captured_stdout, run_with_locale, run_unittest,\ find_unused_port import textwrap -import threading import unittest import warnings import weakref +try: + import threading +except ImportError: + threading = None - class BaseTest(unittest.TestCase): """Base class for logging tests.""" @@ -763,6 +765,7 @@ """Test for SocketHandler objects.""" + @unittest.skipUnless(threading, 'Threading required for this test.') def setUp(self): """Set up a TCP server to receive log messages, and a SocketHandler pointing to that server's address and port.""" @@ -1569,6 +1572,7 @@ def test_config11_ok(self): self.test_config1_ok(self.config11) + @unittest.skipUnless(threading, 'Threading required for this test.') def setup_via_listener(self, text): port = find_unused_port() t = logging.config.listen(port) Index: Lib/test/test_threadsignals.py =================================================================== --- Lib/test/test_threadsignals.py (revision 78619) +++ 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 78619) +++ Lib/test/test_doctest.py (working copy) @@ -2466,8 +2466,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 78619) +++ 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 sys import weakref Index: Lib/test/test_hashlib.py =================================================================== --- Lib/test/test_hashlib.py (revision 78619) +++ Lib/test/test_hashlib.py (working copy) @@ -299,10 +299,9 @@ "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") + @unittest.skipUnless(threading, 'Threading required for this test.') + @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. # @@ -337,7 +336,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 78619) +++ Lib/test/test_capi.py (working copy) @@ -6,8 +6,11 @@ import time import random import unittest -import threading from test import test_support +try: + import threading +except ImportError: + threading = None import _testcapi class TestPendingCalls(unittest.TestCase): @@ -46,8 +49,8 @@ if False and test_support.verbose: print "(%i)"%(len(l),) + @unittest.skipUnless(threading, 'Threading required for this test.') def test_pendingcalls_threaded(self): - #do every callback on a separate thread n = 32 #total callbacks threads = [] @@ -82,6 +85,7 @@ if nFinished == context.nThreads: context.event.set() + @unittest.skipUnless(threading, 'Threading required for this test.') def test_pendingcalls_non_threaded(self): #again, just using the main thread, likely they will all be dispathced at #once. It is ok to ask for too many, because we loop until we find a slot. @@ -130,10 +134,10 @@ have_thread_state = False if have_thread_state: - import thread + thread = test_support.import_module('thread') import time TestThreadState() - import threading + threading = test_support.import_module('threading') t=threading.Thread(target=TestThreadState) t.start() t.join() Index: Lib/test/test_asynchat.py =================================================================== --- Lib/test/test_asynchat.py (revision 78619) +++ 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 78619) +++ Lib/test/test_socketserver.py (working copy) @@ -9,12 +9,15 @@ import signal import socket import tempfile -import threading import unittest import SocketServer import test.test_support from test.test_support import reap_children, reap_threads, verbose +try: + import threading +except ImportError: + threading = None test.test_support.requires("network") @@ -119,6 +122,7 @@ self.assertEquals(server.server_address, server.socket.getsockname()) return server + @unittest.skipUnless(threading, 'Threading required for this test.') @reap_threads def run_server(self, svrcls, hdlrbase, testfunc): server = self.make_server(self.pickaddr(svrcls.address_family), Index: Lib/test/test_queue.py =================================================================== --- Lib/test/test_queue.py (revision 78619) +++ Lib/test/test_queue.py (working copy) @@ -1,10 +1,10 @@ # Some simple queue module tests, plus some failure conditions # to ensure the Queue locks remain stable. import Queue -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 78619) +++ 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 78619) +++ Lib/test/test_bz2.py (working copy) @@ -7,8 +7,12 @@ import os import subprocess import sys -import threading +try: + import threading +except ImportError: + threading = None + bz2 = import_module('bz2') from bz2 import BZ2File, BZ2Compressor, BZ2Decompressor @@ -307,6 +311,7 @@ else: self.fail("1/0 didn't raise an exception") + @unittest.skipUnless(threading, 'Threading required for this test.') def testThreading(self): # Using a BZ2File from several threads doesn't deadlock (issue #7205). data = "1" * 2**20 Index: Lib/test/test_httpservers.py =================================================================== --- Lib/test/test_httpservers.py (revision 78619) +++ 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 78619) +++ Lib/test/test_contextlib.py (working copy) @@ -5,10 +5,13 @@ import sys 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): @@ -303,20 +306,24 @@ else: self.fail("Didn't raise ZeroDivisionError") + @unittest.skipUnless(threading, 'Threading required for this test.') def testWithLock(self): lock = threading.Lock() self.boilerPlate(lock, lock.locked) + @unittest.skipUnless(threading, 'Threading required for this test.') def testWithRLock(self): lock = threading.RLock() self.boilerPlate(lock, lock._is_owned) + @unittest.skipUnless(threading, 'Threading required for this test.') def testWithCondition(self): lock = threading.Condition() def locked(): return lock._is_owned() self.boilerPlate(lock, locked) + @unittest.skipUnless(threading, 'Threading required for this test.') def testWithSemaphore(self): lock = threading.Semaphore() def locked(): @@ -327,6 +334,7 @@ return True self.boilerPlate(lock, locked) + @unittest.skipUnless(threading, 'Threading required for this test.') def testWithBoundedSemaphore(self): lock = threading.BoundedSemaphore() def locked(): Index: Lib/test/test_threaded_import.py =================================================================== --- Lib/test/test_threaded_import.py (revision 78619) +++ Lib/test/test_threaded_import.py (working copy) @@ -5,9 +5,9 @@ # complains several times about module random having no attribute # randrange, and then Python hangs. -import thread import unittest -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 78619) +++ 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 78619) +++ 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() Index: Lib/test/fork_wait.py =================================================================== --- Lib/test/fork_wait.py (revision 78619) +++ 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,9 @@ the same application, the present example should work just fine. DC """ -import os, sys, time, thread, unittest +import os, sys, time, unittest +import test.test_support as test_support +thread = test_support.import_module('thread') LONGSLEEP = 2 SHORTSLEEP = 0.5 Index: Lib/test/test_threading_local.py =================================================================== --- Lib/test/test_threading_local.py (revision 78619) +++ 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 78619) +++ Lib/test/test_multiprocessing.py (working copy) @@ -5,7 +5,6 @@ # import unittest -import threading import Queue import time import sys @@ -19,8 +18,11 @@ import logging from test import test_support from StringIO import StringIO +try: + import threading +except ImportError: + threading = None - _multiprocessing = test_support.import_module('_multiprocessing') # Work around broken sem_open implementations Index: Lib/test/test_threading.py =================================================================== --- Lib/test/test_threading.py (revision 78619) +++ 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 78619) +++ Lib/test/test_support.py (working copy) @@ -16,6 +16,10 @@ import unittest import importlib import UserDict +try: + import thread +except ImportError: + thread = None __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module", "verbose", "use_resources", "max_memuse", "record_original_stdout", @@ -41,7 +45,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. """ @@ -967,29 +971,41 @@ # at the end of a test run. def threading_setup(): - import thread - return thread._count(), + if thread: + return thread._count(), def threading_cleanup(nb_threads): - import thread - import time + if thread: + import time - _MAX_COUNT = 10 - for count in range(_MAX_COUNT): - n = thread._count() - if n == nb_threads: - break - time.sleep(0.1) + _MAX_COUNT = 10 + for count in range(_MAX_COUNT): + n = thread._count() + if n == nb_threads: + break + time.sleep(0.1) # XXX print a warning in case of failure? def reap_threads(func): - @functools.wraps(func) - def decorator(*args): - key = threading_setup() - try: + """Use this function when threads are being used. This will + ensure that the threads are cleaned up even when the test fails. + If threading is unavailable this function does nothing. + """ + if thread: + @functools.wraps(func) + def thread_decorator(func): + key = threading_setup() + try: + return func(*args) + finally: + threading_cleanup(*key) + decorator = thread_decorator + else: + @functools.wraps(func) + def nothread_decorator(func): return func(*args) - finally: - threading_cleanup(*key) + decorator = nothread_decorator + return decorator def reap_children():