move sections
[python/dscho.git] / Lib / test / threaded_import_hangers.py
blobd7508741d3fe11788b2b2dbfe3cbb5a0c92a5753
1 # 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.
8 TIMEOUT = 10
10 import threading
12 import tempfile
13 import os.path
15 errors = []
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.
21 class Worker(threading.Thread):
22 def __init__(self, function, args):
23 threading.Thread.__init__(self)
24 self.function = function
25 self.args = args
27 def run(self):
28 self.function(*self.args)
30 for name, func, args in [
31 # Bug 147376: TemporaryFile hung on Windows, starting in Python 2.4.
32 ("tempfile.TemporaryFile", tempfile.TemporaryFile, ()),
34 # The real cause for bug 147376: ntpath.abspath() caused the hang.
35 ("os.path.abspath", os.path.abspath, ('.',)),
38 t = Worker(func, args)
39 t.start()
40 t.join(TIMEOUT)
41 if t.is_alive():
42 errors.append("%s appeared to hang" % name)