]> granicus.if.org Git - python/commitdiff
Issue #7449, last part (11): fix many tests if thread support is disabled
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 27 Apr 2010 23:55:59 +0000 (23:55 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 27 Apr 2010 23:55:59 +0000 (23:55 +0000)
 * Use try/except ImportError or test_support.import_module() to import thread
   and threading modules
 * Add @unittest.skipUnless(threading, ...) to testcases using threads

26 files changed:
Lib/sqlite3/test/dbapi.py
Lib/test/fork_wait.py
Lib/test/test_asyncore.py
Lib/test/test_bz2.py
Lib/test/test_contextlib.py
Lib/test/test_docxmlrpc.py
Lib/test/test_file2k.py
Lib/test/test_fork1.py
Lib/test/test_ftplib.py
Lib/test/test_httpservers.py
Lib/test/test_io.py
Lib/test/test_logging.py
Lib/test/test_poplib.py
Lib/test/test_queue.py
Lib/test/test_smtplib.py
Lib/test/test_socket.py
Lib/test/test_socketserver.py
Lib/test/test_telnetlib.py
Lib/test/test_thread.py
Lib/test/test_threaded_import.py
Lib/test/test_threadedtempfile.py
Lib/test/test_threading.py
Lib/test/test_threading_local.py
Lib/test/test_threadsignals.py
Lib/test/test_urllib2_localnet.py
Misc/NEWS

index 9d8721229305c12797860741ffd90de37160a56e..401ba79df82fb033bb6ac5e5e5ac3c7c57786e52 100644 (file)
 
 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 @@ class CursorTests(unittest.TestCase):
         except TypeError:
             pass
 
+@unittest.skipUnless(threading, 'This test requires threading.')
 class ThreadTests(unittest.TestCase):
     def setUp(self):
         self.con = sqlite.connect(":memory:")
index 3c75de39d66ddc45b739f4be88ebd6fdb537eb51..cc51f899584bf13197ff2a23cbc5e2dbbd0e628a 100644 (file)
@@ -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 @@ While BeOS doesn't officially support fork and native threading in
 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 1a54ce5da77a0e16eab797690acfdbf61710b859..0cf14bf285405b9fa288f65014996b6c523148b4 100644 (file)
@@ -3,7 +3,6 @@ import unittest
 import select
 import os
 import socket
-import threading
 import sys
 import time
 
@@ -11,6 +10,11 @@ from test import test_support
 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 @@ class DispatcherWithSendTests(unittest.TestCase):
     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 0541535a3c722e14406fbb4770e69f807b34b99d..555cdeba23e1621f5e838b85ceb073064d432b3f 100644 (file)
@@ -7,7 +7,11 @@ from cStringIO import StringIO
 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 @@ class BZ2FileTest(BaseTest):
         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 42b90b612de0e07aaa14a27cf1782b87ff91ee1e..f28c95eadbc1968b50c2fa1ca79d8547b583b2c5 100644 (file)
@@ -3,9 +3,12 @@
 import sys
 import tempfile
 import unittest
-import threading
 from contextlib import *  # Tests __all__
 from test import test_support
+try:
+    import threading
+except ImportError:
+    threading = None
 
 
 class ContextManagerTestCase(unittest.TestCase):
@@ -264,6 +267,7 @@ class FileContextTestCase(unittest.TestCase):
         finally:
             test_support.unlink(tfn)
 
+@unittest.skipUnless(threading, 'Threading required for this test.')
 class LockContextTestCase(unittest.TestCase):
 
     def boilerPlate(self, lock, locked):
index 1af3433d02c16251264ea152438143a66296e204..716333e67f672f61397cc7d9253a533f32404262 100644 (file)
@@ -2,7 +2,7 @@ from DocXMLRPCServer import DocXMLRPCServer
 import httplib
 import sys
 from test import test_support
-import threading
+threading = test_support.import_module('threading')
 import time
 import socket
 import unittest
index a7681a9a848bfa3e28a20194014b58285ebe7f01..accdc9570313d231b3b6ca49838e7b106f02e0f2 100644 (file)
@@ -3,9 +3,12 @@ import os
 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
@@ -411,6 +414,7 @@ class FileSubclassTests(unittest.TestCase):
         self.assertTrue(f.subclass_closed)
 
 
+@unittest.skipUnless(threading, 'Threading required for this test.')
 class FileThreadingTests(unittest.TestCase):
     # These tests check the ability to call various methods of file objects
     # (including close()) concurrently without crashing the Python interpreter.
index 0640071370060411348584f03dcba16552d0dc50..3930667d5c140b2756151e467ea43777e531168e 100644 (file)
@@ -6,10 +6,10 @@ import os
 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 4f6d1c12b2ed1069faa9190905f715f17866d0b5..3a2ec5e84b103ce34ee1bb6a231c07780b390f97 100644 (file)
@@ -4,7 +4,6 @@
 # environment
 
 import ftplib
-import threading
 import asyncore
 import asynchat
 import socket
@@ -19,6 +18,7 @@ except ImportError:
 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
index e392aeedfec02b57700f58bd5df3daba9f27afe3..0dc0a32b0093202f27001a3b8e2c3a65c3dac870 100644 (file)
@@ -16,10 +16,10 @@ import shutil
 import urllib
 import httplib
 import tempfile
-import threading
 
 import unittest
 from test import test_support
+threading = test_support.import_module('threading')
 
 
 class NoLogRequestHandler:
index 9ffe646ea9fd2b67e8765efd9f1dad69862f0bdc..b3feb1b065c90843c7ebb9ab8ce9188625844ad3 100644 (file)
@@ -26,7 +26,6 @@ import os
 import sys
 import time
 import array
-import threading
 import random
 import unittest
 import weakref
@@ -38,6 +37,10 @@ from test import test_support as support
 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
@@ -749,6 +752,7 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
 
         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,
@@ -996,6 +1000,7 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
         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
@@ -2090,7 +2095,7 @@ class TextIOWrapperTest(unittest.TestCase):
         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 432493b8e5b9742901514405de54cb8793e8cb54..0211f560747192a67593eb22a5e81723f4173ba1 100644 (file)
@@ -41,11 +41,13 @@ import tempfile
 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):
 
@@ -765,6 +767,7 @@ class LogRecordSocketReceiver(ThreadingTCPServer):
         self.server_close()
 
 
+@unittest.skipUnless(threading, 'Threading required for this test.')
 class SocketHandlerTest(BaseTest):
 
     """Test for SocketHandler objects."""
@@ -1659,6 +1662,7 @@ class ConfigDictTest(BaseTest):
     def test_config13_failure(self):
         self.assertRaises(StandardError, self.apply_config, self.config13)
 
+    @unittest.skipUnless(threading, 'listen() needs threading to work')
     def setup_via_listener(self, text):
         port = find_unused_port()
         t = logging.config.listen(port)
index f75f0b1a519475c8ab4bec1faea2fecaa707bc36..4375f71761a94d354f6f4c9c1325649b8952b92b 100644 (file)
@@ -4,7 +4,6 @@
 # a real test suite
 
 import poplib
-import threading
 import asyncore
 import asynchat
 import socket
@@ -15,6 +14,7 @@ import errno
 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
index 163628078f26ecceade29f37d6ede978138db6fc..5f5a6acd7147bdf0ca8daf8aa99261c0444dfcbc 100644 (file)
@@ -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 9a492f627fe4ef38050b3dfd279eaaf007dc98f9..977a1527b36f218b11e2b57444d6a8b598d2e1b8 100644 (file)
@@ -1,7 +1,6 @@
 import asyncore
 import email.utils
 import socket
-import threading
 import smtpd
 import smtplib
 import StringIO
@@ -9,9 +8,14 @@ import sys
 import time
 import select
 
-from unittest import TestCase
+import unittest
 from test import test_support
 
+try:
+    import threading
+except ImportError:
+    threading = None
+
 HOST = test_support.HOST
 
 def server(evt, buf, serv):
@@ -36,7 +40,8 @@ def server(evt, buf, serv):
         serv.close()
         evt.set()
 
-class GeneralTests(TestCase):
+@unittest.skipUnless(threading, 'Threading required for this test.')
+class GeneralTests(unittest.TestCase):
 
     def setUp(self):
         self._threads = test_support.threading_setup()
@@ -138,7 +143,8 @@ MSG_END = '------------ END MESSAGE ------------\n'
 # test server times out, causing the test to fail.
 
 # Test behavior of smtpd.DebuggingServer
-class DebuggingServerTests(TestCase):
+@unittest.skipUnless(threading, 'Threading required for this test.')
+class DebuggingServerTests(unittest.TestCase):
 
     def setUp(self):
         # temporarily replace sys.stdout to capture DebuggingServer output
@@ -233,7 +239,7 @@ class DebuggingServerTests(TestCase):
         self.assertEqual(self.output.getvalue(), mexpect)
 
 
-class NonConnectingTests(TestCase):
+class NonConnectingTests(unittest.TestCase):
 
     def testNotConnected(self):
         # Test various operations on an unconnected SMTP object that
@@ -254,7 +260,8 @@ class NonConnectingTests(TestCase):
 
 
 # test response of client to a non-successful HELO message
-class BadHELOServerTests(TestCase):
+@unittest.skipUnless(threading, 'Threading required for this test.')
+class BadHELOServerTests(unittest.TestCase):
 
     def setUp(self):
         self.old_stdout = sys.stdout
@@ -378,7 +385,8 @@ class SimSMTPServer(smtpd.SMTPServer):
 
 # Test various SMTP & ESMTP commands/behaviors that require a simulated server
 # (i.e., something with more features than DebuggingServer)
-class SMTPSimTests(TestCase):
+@unittest.skipUnless(threading, 'Threading required for this test.')
+class SMTPSimTests(unittest.TestCase):
 
     def setUp(self):
         self._threads = test_support.threading_setup()
index f699e3c083c9d6822c73c1add7465da332806603..0ce329f12c55ba70d3e03eb5f77ebbdcf5bef6d6 100644 (file)
@@ -6,7 +6,6 @@ from test import test_support
 import errno
 import socket
 import select
-import thread, threading
 import time
 import traceback
 import Queue
@@ -16,6 +15,13 @@ import array
 from weakref import proxy
 import signal
 
+try:
+    import thread
+    import threading
+except ImportError:
+    thread = None
+    threading = None
+
 HOST = test_support.HOST
 MSG = 'Michael Gilfix was here\n'
 
@@ -550,6 +556,7 @@ class GeneralModuleTests(unittest.TestCase):
         s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100))
 
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class BasicTCPTest(SocketConnectedTest):
 
     def __init__(self, methodName='runTest'):
@@ -630,6 +637,7 @@ class BasicTCPTest(SocketConnectedTest):
         self.serv_conn.send(MSG)
         self.serv_conn.shutdown(2)
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class BasicUDPTest(ThreadedUDPSocketTest):
 
     def __init__(self, methodName='runTest'):
@@ -658,6 +666,7 @@ class BasicUDPTest(ThreadedUDPSocketTest):
     def _testRecvFromNegative(self):
         self.cli.sendto(MSG, 0, (HOST, self.port))
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class TCPCloserTest(ThreadedTCPSocketTest):
 
     def testClose(self):
@@ -673,6 +682,7 @@ class TCPCloserTest(ThreadedTCPSocketTest):
         self.cli.connect((HOST, self.port))
         time.sleep(1.0)
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class BasicSocketPairTest(SocketPairTest):
 
     def __init__(self, methodName='runTest'):
@@ -692,6 +702,7 @@ class BasicSocketPairTest(SocketPairTest):
         msg = self.cli.recv(1024)
         self.assertEqual(msg, MSG)
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class NonBlockingTCPTests(ThreadedTCPSocketTest):
 
     def __init__(self, methodName='runTest'):
@@ -760,6 +771,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
         time.sleep(0.1)
         self.cli.send(MSG)
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class FileObjectClassTestCase(SocketConnectedTest):
 
     bufsize = -1 # Use default buffer size
@@ -989,6 +1001,7 @@ class NetworkConnectionNoServer(unittest.TestCase):
             lambda: socket.create_connection((HOST, port))
         )
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
 
     def __init__(self, methodName='runTest'):
@@ -1051,6 +1064,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
         self.cli = socket.create_connection((HOST, self.port), 30)
         self.assertEqual(self.cli.gettimeout(), 30)
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
 
     def __init__(self, methodName='runTest'):
@@ -1220,6 +1234,7 @@ class TestLinuxAbstractNamespace(unittest.TestCase):
         self.assertRaises(socket.error, s.bind, address)
 
 
+@unittest.skipUnless(thread, 'Threading required for this test.')
 class BufferIOTest(SocketConnectedTest):
     """
     Test the buffer versions of socket.recv() and socket.send().
index 4f0dd3ee5698cb5f991d9ac3f00c65ee89c0257c..a478bd45d7ab25ff625359cf17763aa779eda0ba 100644 (file)
@@ -9,12 +9,15 @@ import select
 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 @@ class SocketServerTest(unittest.TestCase):
         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 a04bc3fe678ee817980732683e7c8c1ebb2f855b..fd5c6f7c3c5372896c2956a7445d7e3516db46e9 100644 (file)
@@ -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 33109ec0123d55b7d2579933bb178f15c8fb1694..544e70d2fdf7ab3a599def978979628c01c8530f 100644 (file)
@@ -2,7 +2,7 @@ import os
 import unittest
 import random
 from test import test_support
-import thread
+thread = test_support.import_module('thread')
 import time
 import sys
 import weakref
index 4b925c42b5989fbff73f03f7940938a0aa8f8107..b31fdf1363a43de3be3367585b9cb458fef33a4a 100644 (file)
@@ -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 753f38847e95f923d25e8dd2a2ae21359fb4f961..4aeb711269b77613d341a234e80833f4912ce800 100644 (file)
@@ -16,11 +16,10 @@ provoking a 2.0 failure under Linux.
 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 70dd026a90c97df15f36cbb666faf598a016aed6..2e2558a6562d1c91c66b9a266076c97e07628d53 100644 (file)
@@ -5,8 +5,8 @@ from test.test_support import verbose
 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 ab3b358245b54ac7b1ba5b48991be119b50208a1..ad0f43cbf02b8336d397ecd07ff786e6467c1489 100644 (file)
@@ -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 b28705f3a385ec466b21c7b1bd6f9f838321ac0a..127ddd262f19c19c8bf1d0f68dd95d4610bda32e 100644 (file)
@@ -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 e3860f26802c334c53b7d913a3b00d629a68d203..ef2614bc4139c9536835288614c33f7ae6abab68 100644 (file)
@@ -1,6 +1,5 @@
 #!/usr/bin/env python
 
-import threading
 import urlparse
 import urllib2
 import BaseHTTPServer
@@ -8,6 +7,7 @@ import unittest
 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 929a246eaeb16261131a11355b5f87e46f80950f..d026e36ba7e5b693eee15473f71a226e75645ed2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -112,7 +112,8 @@ Extension Modules
 Tests
 -----
 
-- Issue #7449: Fix many tests to support Python compiled without thread support
+- Issue #7449: Fix many tests to support Python compiled without thread
+  support. Patches written by Jerry Seutter.
 
 - Issue #8108: test_ftplib's non-blocking SSL server now has proper handling
   of SSL shutdowns.