Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45df820

Browse files
author
Victor Stinner
committed
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r80552 | victor.stinner | 2010-04-27 23:46:03 +0200 (mar., 27 avril 2010) | 3 lines Issue #7449, part 1: fix test_support.py for Python compiled without thread ........ r80553 | victor.stinner | 2010-04-27 23:47:01 +0200 (mar., 27 avril 2010) | 1 line Issue #7449, part 2: regrtest.py -j option requires thread support ........ r80554 | victor.stinner | 2010-04-27 23:51:26 +0200 (mar., 27 avril 2010) | 9 lines Issue #7449 part 3, test_doctest: import trace module in test_coverage() Import trace module fail if the threading module is missing. test_coverage() is only used if test_doctest.py is used with the -c option. This commit allows to execute the test suite without thread support. Move "import trace" in test_coverage() and use test_support.import_module('trace'). ........ r80555 | victor.stinner | 2010-04-27 23:56:26 +0200 (mar., 27 avril 2010) | 6 lines Issue #7449, part 4: skip test_multiprocessing if thread support is disabled import threading after _multiprocessing to raise a more revelant error message: "No module named _multiprocessing". _multiprocessing is not compiled without thread support. ........ r80556 | victor.stinner | 2010-04-28 00:01:24 +0200 (mer., 28 avril 2010) | 8 lines Issue #7449, part 5: split Test.test_open() of ctypes/test/test_errno.py * Split Test.test_open() in 2 functions: test_open() and test_thread_open() * Skip test_open() and test_thread_open() if we are unable to find the C library * Skip test_thread_open() if thread support is disabled * Use unittest.skipUnless(os.name == "nt", ...) on test_GetLastError() ........ r80564 | victor.stinner | 2010-04-28 00:59:35 +0200 (mer., 28 avril 2010) | 4 lines Issue #7449, part 6: fix test_hashlib for missing threading module Move @test_support.reap_thread decorator from test_main() to test_threaded_hashing(). ........ r80565 | victor.stinner | 2010-04-28 01:01:29 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, part 7: simplify threading detection in test_capi * Skip TestPendingCalls if threading module is missing * Test if threading module is present or not, instead of test the presence of _testcapi._test_thread_state ........ r80566 | victor.stinner | 2010-04-28 01:03:16 +0200 (mer., 28 avril 2010) | 4 lines Issue #7449, part 8: don't skip the whole test_asynchat if threading is missing TestFifo can be executed without the threading module ........ r80568 | victor.stinner | 2010-04-28 01:14:58 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, part 9: fix test_xmlrpclib for missing threading module * Skip testcases using threads if threading module is missing * Use "http://" instead of URL in ServerProxyTestCase if threading is missing because URL is not set in this case ........ r80569 | victor.stinner | 2010-04-28 01:33:58 +0200 (mer., 28 avril 2010) | 6 lines Partial revert of r80556 (Issue #7449, part 5, fix ctypes test) Rewrite r80556: the thread test have to be executed just after the test on libc_open() and so the test cannot be splitted in two functions (without duplicating code, and I don't want to duplicate code). ........ r80570 | victor.stinner | 2010-04-28 01:51:16 +0200 (mer., 28 avril 2010) | 8 lines Issue #7449, part 10: test_cmd imports trace module using test_support.import_module() Use test_support.import_module() instead of import to raise a SkipTest exception if the import fail. Import trace fails if the threading module is missing. See also part 3: test_doctest: import trace module in test_coverage(). ........ r80571 | victor.stinner | 2010-04-28 01:55:59 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, last part (11): fix many tests if thread support is disabled * Use try/except ImportError or test_support.import_module() to import thread and threading modules * Add @unittest.skipUnless(threading, ...) to testcases using threads ........
1 parent 480a124 commit 45df820

36 files changed

Lines changed: 290 additions & 183 deletions

Lib/ctypes/test/test_errno.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
import unittest, os, errno
22
from ctypes import *
33
from ctypes.util import find_library
4-
import threading
4+
try:
5+
import threading
6+
except ImportError:
7+
threading = None
58

69
class Test(unittest.TestCase):
710
def test_open(self):
811
libc_name = find_library("c")
9-
if libc_name is not None:
10-
libc = CDLL(libc_name, use_errno=True)
11-
if os.name == "nt":
12-
libc_open = libc._open
13-
else:
14-
libc_open = libc.open
12+
if libc_name is None:
13+
raise unittest.SkipTest("Unable to find C library")
14+
libc = CDLL(libc_name, use_errno=True)
15+
if os.name == "nt":
16+
libc_open = libc._open
17+
else:
18+
libc_open = libc.open
1519

16-
libc_open.argtypes = c_char_p, c_int
20+
libc_open.argtypes = c_char_p, c_int
1721

18-
self.assertEqual(libc_open("", 0), -1)
19-
self.assertEqual(get_errno(), errno.ENOENT)
20-
21-
self.assertEqual(set_errno(32), errno.ENOENT)
22-
self.assertEqual(get_errno(), 32)
22+
self.assertEqual(libc_open("", 0), -1)
23+
self.assertEqual(get_errno(), errno.ENOENT)
2324

25+
self.assertEqual(set_errno(32), errno.ENOENT)
26+
self.assertEqual(get_errno(), 32)
2427

28+
if threading:
2529
def _worker():
2630
set_errno(0)
2731

@@ -41,36 +45,35 @@ def _worker():
4145
self.assertEqual(get_errno(), 32)
4246
set_errno(0)
4347

44-
if os.name == "nt":
45-
46-
def test_GetLastError(self):
47-
dll = WinDLL("kernel32", use_last_error=True)
48-
GetModuleHandle = dll.GetModuleHandleA
49-
GetModuleHandle.argtypes = [c_wchar_p]
48+
@unittest.skipUnless(os.name == "nt", 'Test specific to Windows')
49+
def test_GetLastError(self):
50+
dll = WinDLL("kernel32", use_last_error=True)
51+
GetModuleHandle = dll.GetModuleHandleA
52+
GetModuleHandle.argtypes = [c_wchar_p]
5053

51-
self.assertEqual(0, GetModuleHandle("foo"))
52-
self.assertEqual(get_last_error(), 126)
54+
self.assertEqual(0, GetModuleHandle("foo"))
55+
self.assertEqual(get_last_error(), 126)
5356

54-
self.assertEqual(set_last_error(32), 126)
55-
self.assertEqual(get_last_error(), 32)
57+
self.assertEqual(set_last_error(32), 126)
58+
self.assertEqual(get_last_error(), 32)
5659

57-
def _worker():
58-
set_last_error(0)
60+
def _worker():
61+
set_last_error(0)
5962

60-
dll = WinDLL("kernel32", use_last_error=False)
61-
GetModuleHandle = dll.GetModuleHandleW
62-
GetModuleHandle.argtypes = [c_wchar_p]
63-
GetModuleHandle("bar")
63+
dll = WinDLL("kernel32", use_last_error=False)
64+
GetModuleHandle = dll.GetModuleHandleW
65+
GetModuleHandle.argtypes = [c_wchar_p]
66+
GetModuleHandle("bar")
6467

65-
self.assertEqual(get_last_error(), 0)
68+
self.assertEqual(get_last_error(), 0)
6669

67-
t = threading.Thread(target=_worker)
68-
t.start()
69-
t.join()
70+
t = threading.Thread(target=_worker)
71+
t.start()
72+
t.join()
7073

71-
self.assertEqual(get_last_error(), 32)
74+
self.assertEqual(get_last_error(), 32)
7275

73-
set_last_error(0)
76+
set_last_error(0)
7477

7578
if __name__ == "__main__":
7679
unittest.main()

Lib/sqlite3/test/dbapi.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
# 3. This notice may not be removed or altered from any source distribution.
2323

2424
import unittest
25-
import threading
2625
import sqlite3 as sqlite
26+
try:
27+
import threading
28+
except ImportError:
29+
threading = None
2730

2831
class ModuleTests(unittest.TestCase):
2932
def CheckAPILevel(self):
@@ -460,6 +463,7 @@ class Foo: pass
460463
except TypeError:
461464
pass
462465

466+
@unittest.skipUnless(threading, 'This test requires threading.')
463467
class ThreadTests(unittest.TestCase):
464468
def setUp(self):
465469
self.con = sqlite.connect(":memory:")

Lib/test/fork_wait.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""This test case provides support for checking forking and wait behavior.
22
3-
To test different wait behavior, overrise the wait_impl method.
3+
To test different wait behavior, override the wait_impl method.
44
55
We want fork1() semantics -- only the forking thread survives in the
66
child after a fork().
@@ -9,7 +9,9 @@
99
active threads survive in the child after a fork(); this is an error.
1010
"""
1111

12-
import os, sys, time, _thread, unittest
12+
import os, sys, time, unittest
13+
import test.support as support
14+
_thread = support.import_module('_thread')
1315

1416
LONGSLEEP = 2
1517
SHORTSLEEP = 0.5

Lib/test/regrtest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,11 @@ def test_forever(tests=list(selected)):
515515
tests = iter(selected)
516516

517517
if use_mp:
518-
from threading import Thread
518+
try:
519+
from threading import Thread
520+
except ImportError:
521+
print("Multiprocess option requires thread support")
522+
sys.exit(2)
519523
from queue import Queue
520524
from subprocess import Popen, PIPE
521525
debug_output_pat = re.compile(r"\[\d+ refs\]$")

Lib/test/support.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import re
2020
import imp
2121
import time
22+
try:
23+
import _thread
24+
except ImportError:
25+
_thread = None
2226

2327
__all__ = [
2428
"Error", "TestFailed", "ResourceDenied", "import_module",
@@ -1111,12 +1115,14 @@ def modules_cleanup(oldmodules):
11111115
# at the end of a test run.
11121116

11131117
def threading_setup():
1114-
import _thread
1115-
return _thread._count(),
1118+
if _thread:
1119+
return _thread._count(),
1120+
else:
1121+
return 1,
11161122

11171123
def threading_cleanup(nb_threads):
1118-
import _thread
1119-
1124+
if not _thread:
1125+
return
11201126
_MAX_COUNT = 10
11211127
for count in range(_MAX_COUNT):
11221128
n = _thread._count()
@@ -1126,6 +1132,13 @@ def threading_cleanup(nb_threads):
11261132
# XXX print a warning in case of failure?
11271133

11281134
def reap_threads(func):
1135+
"""Use this function when threads are being used. This will
1136+
ensure that the threads are cleaned up even when the test fails.
1137+
If threading is unavailable this function does nothing.
1138+
"""
1139+
if not _thread:
1140+
return func
1141+
11291142
@functools.wraps(func)
11301143
def decorator(*args):
11311144
key = threading_setup()

Lib/test/test_asynchat.py

Lines changed: 87 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -5,96 +5,101 @@
55
# If this fails, the test will be skipped.
66
thread = support.import_module('_thread')
77

8-
import asyncore, asynchat, socket, threading, time
8+
import asyncore, asynchat, socket, time
99
import unittest
1010
import sys
11+
try:
12+
import threading
13+
except ImportError:
14+
threading = None
1115

1216
HOST = support.HOST
1317
SERVER_QUIT = b'QUIT\n'
1418

15-
class echo_server(threading.Thread):
16-
# parameter to determine the number of bytes passed back to the
17-
# client each send
18-
chunk_size = 1
19-
20-
def __init__(self, event):
21-
threading.Thread.__init__(self)
22-
self.event = event
23-
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
24-
self.port = support.bind_port(self.sock)
25-
# This will be set if the client wants us to wait before echoing data
26-
# back.
27-
self.start_resend_event = None
28-
29-
def run(self):
30-
self.sock.listen(1)
31-
self.event.set()
32-
conn, client = self.sock.accept()
33-
self.buffer = b""
34-
# collect data until quit message is seen
35-
while SERVER_QUIT not in self.buffer:
36-
data = conn.recv(1)
37-
if not data:
38-
break
39-
self.buffer = self.buffer + data
40-
41-
# remove the SERVER_QUIT message
42-
self.buffer = self.buffer.replace(SERVER_QUIT, b'')
43-
44-
if self.start_resend_event:
45-
self.start_resend_event.wait()
46-
47-
# re-send entire set of collected data
48-
try:
49-
# this may fail on some tests, such as test_close_when_done, since
50-
# the client closes the channel when it's done sending
51-
while self.buffer:
52-
n = conn.send(self.buffer[:self.chunk_size])
53-
time.sleep(0.001)
54-
self.buffer = self.buffer[n:]
55-
except:
56-
pass
57-
58-
conn.close()
59-
self.sock.close()
60-
61-
class echo_client(asynchat.async_chat):
62-
63-
def __init__(self, terminator, server_port):
64-
asynchat.async_chat.__init__(self)
65-
self.contents = []
66-
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
67-
self.connect((HOST, server_port))
68-
self.set_terminator(terminator)
69-
self.buffer = b""
70-
71-
def handle_connect(self):
72-
pass
73-
74-
if sys.platform == 'darwin':
75-
# select.poll returns a select.POLLHUP at the end of the tests
76-
# on darwin, so just ignore it
77-
def handle_expt(self):
78-
pass
79-
80-
def collect_incoming_data(self, data):
81-
self.buffer += data
82-
83-
def found_terminator(self):
84-
self.contents.append(self.buffer)
85-
self.buffer = b""
86-
87-
88-
def start_echo_server():
89-
event = threading.Event()
90-
s = echo_server(event)
91-
s.start()
92-
event.wait()
93-
event.clear()
94-
time.sleep(0.01) # Give server time to start accepting.
95-
return s, event
19+
if threading:
20+
class echo_server(threading.Thread):
21+
# parameter to determine the number of bytes passed back to the
22+
# client each send
23+
chunk_size = 1
24+
25+
def __init__(self, event):
26+
threading.Thread.__init__(self)
27+
self.event = event
28+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29+
self.port = support.bind_port(self.sock)
30+
# This will be set if the client wants us to wait before echoing data
31+
# back.
32+
self.start_resend_event = None
33+
34+
def run(self):
35+
self.sock.listen(1)
36+
self.event.set()
37+
conn, client = self.sock.accept()
38+
self.buffer = b""
39+
# collect data until quit message is seen
40+
while SERVER_QUIT not in self.buffer:
41+
data = conn.recv(1)
42+
if not data:
43+
break
44+
self.buffer = self.buffer + data
45+
46+
# remove the SERVER_QUIT message
47+
self.buffer = self.buffer.replace(SERVER_QUIT, b'')
48+
49+
if self.start_resend_event:
50+
self.start_resend_event.wait()
51+
52+
# re-send entire set of collected data
53+
try:
54+
# this may fail on some tests, such as test_close_when_done, since
55+
# the client closes the channel when it's done sending
56+
while self.buffer:
57+
n = conn.send(self.buffer[:self.chunk_size])
58+
time.sleep(0.001)
59+
self.buffer = self.buffer[n:]
60+
except:
61+
pass
62+
63+
conn.close()
64+
self.sock.close()
65+
66+
class echo_client(asynchat.async_chat):
67+
68+
def __init__(self, terminator, server_port):
69+
asynchat.async_chat.__init__(self)
70+
self.contents = []
71+
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
72+
self.connect((HOST, server_port))
73+
self.set_terminator(terminator)
74+
self.buffer = b""
75+
76+
def handle_connect(self):
77+
pass
78+
79+
if sys.platform == 'darwin':
80+
# select.poll returns a select.POLLHUP at the end of the tests
81+
# on darwin, so just ignore it
82+
def handle_expt(self):
83+
pass
84+
85+
def collect_incoming_data(self, data):
86+
self.buffer += data
87+
88+
def found_terminator(self):
89+
self.contents.append(self.buffer)
90+
self.buffer = b""
91+
92+
def start_echo_server():
93+
event = threading.Event()
94+
s = echo_server(event)
95+
s.start()
96+
event.wait()
97+
event.clear()
98+
time.sleep(0.01) # Give server time to start accepting.
99+
return s, event
96100

97101

102+
@unittest.skipUnless(threading, 'Threading required for this test.')
98103
class TestAsynchat(unittest.TestCase):
99104
usepoll = False
100105

0 commit comments

Comments
 (0)