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__)) |
Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 25 | |
Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 26 | try: |
| 27 | import __phello__.spam |
| 28 | except ImportError as x: |
| 29 | self.fail("import __phello__.spam failed:" + str(x)) |
| 30 | self.assertEqual(__phello__.spam.initialized, True) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 31 | self.assertEqual(len(dir(__phello__.spam)), 6) |
| 32 | self.assertEqual(len(dir(__phello__)), 8) |
Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 33 | |
Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 34 | try: |
| 35 | import __phello__.foo |
| 36 | except ImportError: |
| 37 | pass |
| 38 | else: |
| 39 | self.fail("import __phello__.foo should have failed") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 40 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 41 | if sys.platform != "mac": # On the Mac this import does succeed. |
| 42 | try: |
| 43 | import __phello__.foo |
| 44 | except ImportError: |
| 45 | pass |
| 46 | else: |
| 47 | self.fail("import __phello__.foo should have failed") |
| 48 | |
| 49 | del sys.modules['__hello__'] |
| 50 | del sys.modules['__phello__'] |
| 51 | del sys.modules['__phello__.spam'] |
| 52 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 53 | def test_main(): |
| 54 | run_unittest(FrozenTests) |
Guido van Rossum | 3b7210d | 2007-10-15 00:25:56 +0000 | [diff] [blame] | 55 | |
| 56 | if __name__ == "__main__": |
| 57 | test_main() |