blob: adf03e31ffd663ac2df95a2b4fabc053b04b9bbe [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001# This is a helper module for test_threaded_import. The test imports this
2# module, and this module tries to run various Python library functions in
3# their own thread, as a side effect of being imported. If the spawned
4# thread doesn't complete in TIMEOUT seconds, an "appeared to hang" message
5# is appended to the module-global `errors` list. That list remains empty
6# if (and only if) all functions tested complete.
7
8TIMEOUT = 10
9
10import threading
11
12import tempfile
13import os.path
14
15errors = []
16
17# This class merely runs a function in its own thread T. The thread importing
18# this module holds the import lock, so if the function called by T tries
19# to do its own imports it will block waiting for this module's import
20# to complete.
21class Worker(threading.Thread):
22 def __init__(self, function, args):
23 threading.Thread.__init__(self)
24 self.function = function
25 self.args = args
26
27 def run(self):
28 self.function(*self.args)
29
30for name, func, args in [
31 # Bug 147376: TemporaryFile hung on Windows, starting in Python 2.4.
Antoine Pitrou5efe9d02010-10-29 11:08:32 +000032 ("tempfile.TemporaryFile", lambda: tempfile.TemporaryFile().close(), ()),
Thomas Wouters477c8d52006-05-27 19:21:47 +000033
34 # The real cause for bug 147376: ntpath.abspath() caused the hang.
35 ("os.path.abspath", os.path.abspath, ('.',)),
36 ]:
37
38 t = Worker(func, args)
39 t.start()
40 t.join(TIMEOUT)
Benjamin Peterson672b8032008-06-11 19:14:14 +000041 if t.is_alive():
Thomas Wouters477c8d52006-05-27 19:21:47 +000042 errors.append("%s appeared to hang" % name)