ยปCore Development>Code coverage>Lib/test/threaded_import_hangers.py

Python code coverage for Lib/test/threaded_import_hangers.py

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