blob: 7734fb04a260e694b7241704b36a3ae872296458 [file] [log] [blame]
Guido van Rossumc3a787e2002-06-04 05:52:47 +00001# Test the module type
Guido van Rossumd8faa362007-04-27 19:54:29 +00002import unittest
Benjamin Peterson5c4bfc42010-10-12 22:57:59 +00003from test.support import run_unittest, gc_collect
Guido van Rossumc3a787e2002-06-04 05:52:47 +00004
5import sys
Guido van Rossumd8faa362007-04-27 19:54:29 +00006ModuleType = type(sys)
Guido van Rossumc3a787e2002-06-04 05:52:47 +00007
Guido van Rossumd8faa362007-04-27 19:54:29 +00008class 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 Petersonc9c0f202009-06-30 23:06:06 +000013 self.assertTrue(foo.__dict__ is None)
Benjamin Peterson14327712009-08-15 13:23:05 +000014 self.assertRaises(SystemError, dir, foo)
Guido van Rossumd8faa362007-04-27 19:54:29 +000015 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 Rossumc3a787e2002-06-04 05:52:47 +000021
Guido van Rossumd8faa362007-04-27 19:54:29 +000022 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 Rossumc3a787e2002-06-04 05:52:47 +000028
Guido van Rossumd8faa362007-04-27 19:54:29 +000029 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 Rossumc3a787e2002-06-04 05:52:47 +000036
Guido van Rossumd8faa362007-04-27 19:54:29 +000037 def test_unicode_docstring(self):
38 # Unicode docstring
Guido van Rossumef87d6e2007-05-02 19:09:54 +000039 foo = ModuleType("foo", "foodoc\u1234")
Guido van Rossumd8faa362007-04-27 19:54:29 +000040 self.assertEqual(foo.__name__, "foo")
Guido van Rossumef87d6e2007-05-02 19:09:54 +000041 self.assertEqual(foo.__doc__, "foodoc\u1234")
Guido van Rossumd8faa362007-04-27 19:54:29 +000042 self.assertEqual(foo.__dict__,
Guido van Rossumef87d6e2007-05-02 19:09:54 +000043 {"__name__": "foo", "__doc__": "foodoc\u1234"})
Guido van Rossumc3a787e2002-06-04 05:52:47 +000044
Guido van Rossumd8faa362007-04-27 19:54:29 +000045 def test_reinit(self):
46 # Reinitialization should not replace the __dict__
Guido van Rossumef87d6e2007-05-02 19:09:54 +000047 foo = ModuleType("foo", "foodoc\u1234")
Guido van Rossumd8faa362007-04-27 19:54:29 +000048 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 Petersonc9c0f202009-06-30 23:06:06 +000056 self.assertTrue(foo.__dict__ is d)
Guido van Rossumc3a787e2002-06-04 05:52:47 +000057
Benjamin Peterson5c4bfc42010-10-12 22:57:59 +000058 @unittest.expectedFailure
Benjamin Petersona0dfa822009-11-13 02:25:08 +000059 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 Peterson5c4bfc42010-10-12 22:57:59 +000065 gc_collect()
Benjamin Petersona0dfa822009-11-13 02:25:08 +000066 self.assertEqual(f().__dict__["bar"], 4)
67
Benjamin Peterson5c4bfc42010-10-12 22:57:59 +000068 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)
75a = A()"""
76 exec(s, m.__dict__)
77 del m
78 gc_collect()
79 self.assertEqual(destroyed, [1])
80
Guido van Rossumd8faa362007-04-27 19:54:29 +000081def test_main():
82 run_unittest(ModuleTests)
83
84if __name__ == '__main__':
85 test_main()