blob: 3b0a13d9e551c2ec75b6ddb42c2eb090158130a8 [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Thomas Wouters89f507f2006-12-13 04:49:30 +00002import thread
3import unittest
4from test import test_support
Neal Norwitz2294c0d2003-02-12 23:02:21 +00005
Neal Norwitz2294c0d2003-02-12 23:02:21 +00006
Thomas Wouters89f507f2006-12-13 04:49:30 +00007class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +00008
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +000010
Thomas Wouters89f507f2006-12-13 04:49:30 +000011 def verify_lock_state(self, expected):
12 self.failUnlessEqual(imp.lock_held(), expected,
13 "expected imp.lock_held() to be %r" % expected)
14 def testLock(self):
15 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000016
Thomas Wouters89f507f2006-12-13 04:49:30 +000017 # The import lock may already be held, e.g. if the test suite is run
18 # via "import test.autotest".
19 lock_held_at_start = imp.lock_held()
20 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000021
Thomas Wouters89f507f2006-12-13 04:49:30 +000022 for i in range(LOOPS):
23 imp.acquire_lock()
24 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000025
Thomas Wouters89f507f2006-12-13 04:49:30 +000026 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000027 imp.release_lock()
Thomas Wouters89f507f2006-12-13 04:49:30 +000028
29 # The original state should be restored now.
30 self.verify_lock_state(lock_held_at_start)
31
32 if not lock_held_at_start:
33 try:
34 imp.release_lock()
35 except RuntimeError:
36 pass
37 else:
38 self.fail("release_lock() without lock should raise "
39 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000040
Guido van Rossumce3a72a2007-10-19 23:16:50 +000041class ImportTests(unittest.TestCase):
42
43 def test_find_module_encoding(self):
44 fd = imp.find_module("heapq")[0]
45 self.assertEqual(fd.encoding, "iso-8859-1")
46
Guido van Rossum40d20bc2007-10-22 00:09:51 +000047 def test_issue1267(self):
48 fp, filename, info = imp.find_module("pydoc")
49 self.assertNotEqual(fp, None)
50 self.assertEqual(fp.encoding, "iso-8859-1")
51 self.assertEqual(fp.tell(), 0)
52 self.assertEqual(fp.readline(), '#!/usr/bin/env python\n')
53 fp.close()
54
55 fp, filename, info = imp.find_module("tokenize")
56 self.assertNotEqual(fp, None)
57 self.assertEqual(fp.encoding, "utf-8")
58 self.assertEqual(fp.tell(), 0)
59 self.assertEqual(fp.readline(),
60 '"""Tokenization help for Python programs.\n')
61 fp.close()
62
Christian Heimes13a7a212008-01-07 17:13:09 +000063 def test_reload(self):
64 import marshal
65 imp.reload(marshal)
66 import string
67 imp.reload(string)
68 ## import sys
69 ## self.assertRaises(ImportError, reload, sys)
70
Guido van Rossum40d20bc2007-10-22 00:09:51 +000071
Neal Norwitz996acf12003-02-17 14:51:41 +000072def test_main():
Thomas Wouters89f507f2006-12-13 04:49:30 +000073 test_support.run_unittest(
74 LockTests,
Guido van Rossumce3a72a2007-10-19 23:16:50 +000075 ImportTests,
Thomas Wouters89f507f2006-12-13 04:49:30 +000076 )
Neal Norwitz996acf12003-02-17 14:51:41 +000077
Neal Norwitz2294c0d2003-02-12 23:02:21 +000078if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000079 test_main()