Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 1 | import imp |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2 | import unittest |
| 3 | from test import test_support |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 4 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6 | class LockTests(unittest.TestCase): |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8 | """Very basic test of import lock functions.""" |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 9 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 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 |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 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) |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 21 | for i in range(LOOPS): |
| 22 | imp.acquire_lock() |
| 23 | self.verify_lock_state(True) |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | for i in range(LOOPS): |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 26 | imp.release_lock() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 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") |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 40 | class 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 Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 46 | 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 Heimes | 13a7a21 | 2008-01-07 17:13:09 +0000 | [diff] [blame] | 62 | 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 Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 70 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 71 | def test_main(): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 72 | test_support.run_unittest( |
| 73 | LockTests, |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 74 | ImportTests, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 75 | ) |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 76 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 77 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 78 | test_main() |