Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 1 | # Test the frozen module defined in frozen.c. |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test.support import captured_stdout, run_unittest |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 4 | import unittest |
Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 5 | import sys, os |
| 6 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 7 | class FrozenTests(unittest.TestCase): |
| 8 | def test_frozen(self): |
Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 9 | try: |
| 10 | import __hello__ |
| 11 | except ImportError as x: |
| 12 | self.fail("import __hello__ failed:" + str(x)) |
| 13 | self.assertEqual(__hello__.initialized, True) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 14 | self.assertEqual(len(dir(__hello__)), 6, dir(__hello__)) |
Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 15 | |
Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 16 | try: |
| 17 | import __phello__ |
| 18 | except ImportError as x: |
| 19 | self.fail("import __phello__ failed:" + str(x)) |
| 20 | self.assertEqual(__phello__.initialized, True) |
| 21 | if not "__phello__.spam" in sys.modules: |
Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 22 | self.assertEqual(len(dir(__phello__)), 7, dir(__phello__)) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 23 | else: |
| 24 | self.assertEqual(len(dir(__phello__)), 8, dir(__phello__)) |
Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 25 | self.assertEquals(__phello__.__path__, [__phello__.__name__]) |
Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 26 | |
Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 27 | try: |
| 28 | import __phello__.spam |
| 29 | except ImportError as x: |
| 30 | self.fail("import __phello__.spam failed:" + str(x)) |
| 31 | self.assertEqual(__phello__.spam.initialized, True) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 32 | self.assertEqual(len(dir(__phello__.spam)), 6) |
| 33 | self.assertEqual(len(dir(__phello__)), 8) |
Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 34 | |
Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 35 | try: |
| 36 | import __phello__.foo |
| 37 | except ImportError: |
| 38 | pass |
| 39 | else: |
| 40 | self.fail("import __phello__.foo should have failed") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 41 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 42 | if sys.platform != "mac": # On the Mac this import does succeed. |
| 43 | try: |
| 44 | import __phello__.foo |
| 45 | except ImportError: |
| 46 | pass |
| 47 | else: |
| 48 | self.fail("import __phello__.foo should have failed") |
| 49 | |
| 50 | del sys.modules['__hello__'] |
| 51 | del sys.modules['__phello__'] |
| 52 | del sys.modules['__phello__.spam'] |
| 53 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 54 | def test_main(): |
| 55 | run_unittest(FrozenTests) |
Guido van Rossum | 3b7210d | 2007-10-15 00:25:56 +0000 | [diff] [blame] | 56 | |
| 57 | if __name__ == "__main__": |
| 58 | test_main() |