blob: e2b9e9ab245ddcba4a09c9d0cc9f4dafcb9fc84c [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Thomas Wouters89f507f2006-12-13 04:49:30 +00002import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Neal Norwitz2294c0d2003-02-12 23:02:21 +00004
Neal Norwitz2294c0d2003-02-12 23:02:21 +00005
Thomas Wouters89f507f2006-12-13 04:49:30 +00006class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +00007
Thomas Wouters89f507f2006-12-13 04:49:30 +00008 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +00009
Thomas Wouters89f507f2006-12-13 04:49:30 +000010 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
Tim Peters579bed72003-04-26 14:31:24 +000015
Thomas Wouters89f507f2006-12-13 04:49:30 +000016 # 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)
Tim Peters579bed72003-04-26 14:31:24 +000020
Thomas Wouters89f507f2006-12-13 04:49:30 +000021 for i in range(LOOPS):
22 imp.acquire_lock()
23 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000024
Thomas Wouters89f507f2006-12-13 04:49:30 +000025 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000026 imp.release_lock()
Thomas Wouters89f507f2006-12-13 04:49:30 +000027
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")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000039
Guido van Rossumce3a72a2007-10-19 23:16:50 +000040class ImportTests(unittest.TestCase):
41
42 def test_find_module_encoding(self):
43 fd = imp.find_module("heapq")[0]
44 self.assertEqual(fd.encoding, "iso-8859-1")
45
Guido van Rossum40d20bc2007-10-22 00:09:51 +000046 def test_issue1267(self):
47 fp, filename, info = imp.find_module("pydoc")
48 self.assertNotEqual(fp, None)
49 self.assertEqual(fp.encoding, "iso-8859-1")
50 self.assertEqual(fp.tell(), 0)
51 self.assertEqual(fp.readline(), '#!/usr/bin/env python\n')
52 fp.close()
53
54 fp, filename, info = imp.find_module("tokenize")
55 self.assertNotEqual(fp, None)
56 self.assertEqual(fp.encoding, "utf-8")
57 self.assertEqual(fp.tell(), 0)
58 self.assertEqual(fp.readline(),
59 '"""Tokenization help for Python programs.\n')
60 fp.close()
61
Christian Heimes13a7a212008-01-07 17:13:09 +000062 def test_reload(self):
63 import marshal
64 imp.reload(marshal)
65 import string
66 imp.reload(string)
67 ## import sys
68 ## self.assertRaises(ImportError, reload, sys)
69
Guido van Rossum40d20bc2007-10-22 00:09:51 +000070
Neal Norwitz996acf12003-02-17 14:51:41 +000071def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000072 support.run_unittest(
Thomas Wouters89f507f2006-12-13 04:49:30 +000073 LockTests,
Guido van Rossumce3a72a2007-10-19 23:16:50 +000074 ImportTests,
Thomas Wouters89f507f2006-12-13 04:49:30 +000075 )
Neal Norwitz996acf12003-02-17 14:51:41 +000076
Neal Norwitz2294c0d2003-02-12 23:02:21 +000077if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000078 test_main()