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