Home | History | Annotate | Download | only in test
      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.
      7 
      8 TIMEOUT = 10
      9 
     10 import threading
     11 
     12 import tempfile
     13 import os.path
     14 
     15 errors = []
     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.
     21 class 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 
     30 for name, func, args in [
     31         # Bug 147376:  TemporaryFile hung on Windows, starting in Python 2.4.
     32         ("tempfile.TemporaryFile", lambda: tempfile.TemporaryFile().close(), ()),
     33 
     34         # The real cause for bug 147376:  ntpath.abspath() caused the hang.
     35         ("os.path.abspath", os.path.abspath, ('.',)),
     36         ]:
     37 
     38     try:
     39         t = Worker(func, args)
     40         t.start()
     41         t.join(TIMEOUT)
     42         if t.is_alive():
     43             errors.append("%s appeared to hang" % name)
     44     finally:
     45         del t
     46