symbian-qemu-0.9.1-12/python-2.6.1/Lib/test/test_imp.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 import imp
       
     2 import unittest
       
     3 from test import test_support
       
     4 
       
     5 
       
     6 class LockTests(unittest.TestCase):
       
     7 
       
     8     """Very basic test of import lock functions."""
       
     9 
       
    10     def verify_lock_state(self, expected):
       
    11         self.failUnlessEqual(imp.lock_held(), expected,
       
    12                              "expected imp.lock_held() to be %r" % expected)
       
    13     def testLock(self):
       
    14         LOOPS = 50
       
    15 
       
    16         # The import lock may already be held, e.g. if the test suite is run
       
    17         # via "import test.autotest".
       
    18         lock_held_at_start = imp.lock_held()
       
    19         self.verify_lock_state(lock_held_at_start)
       
    20 
       
    21         for i in range(LOOPS):
       
    22             imp.acquire_lock()
       
    23             self.verify_lock_state(True)
       
    24 
       
    25         for i in range(LOOPS):
       
    26             imp.release_lock()
       
    27 
       
    28         # The original state should be restored now.
       
    29         self.verify_lock_state(lock_held_at_start)
       
    30 
       
    31         if not lock_held_at_start:
       
    32             try:
       
    33                 imp.release_lock()
       
    34             except RuntimeError:
       
    35                 pass
       
    36             else:
       
    37                 self.fail("release_lock() without lock should raise "
       
    38                             "RuntimeError")
       
    39 
       
    40 class ReloadTests(unittest.TestCase):
       
    41 
       
    42     """Very basic tests to make sure that imp.reload() operates just like
       
    43     reload()."""
       
    44 
       
    45     def test_source(self):
       
    46         import os
       
    47         imp.reload(os)
       
    48 
       
    49     def test_extension(self):
       
    50         import time
       
    51         imp.reload(time)
       
    52 
       
    53     def test_builtin(self):
       
    54         import marshal
       
    55         imp.reload(marshal)
       
    56 
       
    57 
       
    58 def test_main():
       
    59     tests = [
       
    60         ReloadTests,
       
    61     ]
       
    62     try:
       
    63         import thread
       
    64     except ImportError:
       
    65         pass
       
    66     else:
       
    67         tests.append(LockTests)
       
    68     test_support.run_unittest(*tests)
       
    69 
       
    70 if __name__ == "__main__":
       
    71     test_main()