Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 1 | # Test the module type |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2 | import unittest |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 3 | from test.support import run_unittest, gc_collect |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 4 | |
| 5 | import sys |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | ModuleType = type(sys) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 8 | class ModuleTests(unittest.TestCase): |
| 9 | def test_uninitialized(self): |
| 10 | # An uninitialized module has no __dict__ or __name__, |
| 11 | # and __doc__ is None |
| 12 | foo = ModuleType.__new__(ModuleType) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 13 | self.assertTrue(foo.__dict__ is None) |
Benjamin Peterson | 1432771 | 2009-08-15 13:23:05 +0000 | [diff] [blame] | 14 | self.assertRaises(SystemError, dir, foo) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 15 | try: |
| 16 | s = foo.__name__ |
| 17 | self.fail("__name__ = %s" % repr(s)) |
| 18 | except AttributeError: |
| 19 | pass |
| 20 | self.assertEqual(foo.__doc__, ModuleType.__doc__) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 22 | def test_no_docstring(self): |
| 23 | # Regularly initialized module, no docstring |
| 24 | foo = ModuleType("foo") |
| 25 | self.assertEqual(foo.__name__, "foo") |
| 26 | self.assertEqual(foo.__doc__, None) |
| 27 | self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None}) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 29 | def test_ascii_docstring(self): |
| 30 | # ASCII docstring |
| 31 | foo = ModuleType("foo", "foodoc") |
| 32 | self.assertEqual(foo.__name__, "foo") |
| 33 | self.assertEqual(foo.__doc__, "foodoc") |
| 34 | self.assertEqual(foo.__dict__, |
| 35 | {"__name__": "foo", "__doc__": "foodoc"}) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 37 | def test_unicode_docstring(self): |
| 38 | # Unicode docstring |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 39 | foo = ModuleType("foo", "foodoc\u1234") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 40 | self.assertEqual(foo.__name__, "foo") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 41 | self.assertEqual(foo.__doc__, "foodoc\u1234") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 42 | self.assertEqual(foo.__dict__, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 43 | {"__name__": "foo", "__doc__": "foodoc\u1234"}) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 45 | def test_reinit(self): |
| 46 | # Reinitialization should not replace the __dict__ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 47 | foo = ModuleType("foo", "foodoc\u1234") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 48 | foo.bar = 42 |
| 49 | d = foo.__dict__ |
| 50 | foo.__init__("foo", "foodoc") |
| 51 | self.assertEqual(foo.__name__, "foo") |
| 52 | self.assertEqual(foo.__doc__, "foodoc") |
| 53 | self.assertEqual(foo.bar, 42) |
| 54 | self.assertEqual(foo.__dict__, |
| 55 | {"__name__": "foo", "__doc__": "foodoc", "bar": 42}) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 56 | self.assertTrue(foo.__dict__ is d) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 57 | |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 58 | @unittest.expectedFailure |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 59 | def test_dont_clear_dict(self): |
| 60 | # See issue 7140. |
| 61 | def f(): |
| 62 | foo = ModuleType("foo") |
| 63 | foo.bar = 4 |
| 64 | return foo |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 65 | gc_collect() |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 66 | self.assertEqual(f().__dict__["bar"], 4) |
| 67 | |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 68 | def test_clear_dict_in_ref_cycle(self): |
| 69 | destroyed = [] |
| 70 | m = ModuleType("foo") |
| 71 | m.destroyed = destroyed |
| 72 | s = """class A: |
| 73 | def __del__(self): |
| 74 | destroyed.append(1) |
| 75 | a = A()""" |
| 76 | exec(s, m.__dict__) |
| 77 | del m |
| 78 | gc_collect() |
| 79 | self.assertEqual(destroyed, [1]) |
| 80 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 81 | def test_main(): |
| 82 | run_unittest(ModuleTests) |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | test_main() |