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 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 8 | class FullLoader: |
| 9 | @classmethod |
| 10 | def module_repr(cls, m): |
| 11 | return "<module '{}' (crafted)>".format(m.__name__) |
| 12 | |
| 13 | class BareLoader: |
| 14 | pass |
| 15 | |
| 16 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 17 | class ModuleTests(unittest.TestCase): |
| 18 | def test_uninitialized(self): |
| 19 | # An uninitialized module has no __dict__ or __name__, |
| 20 | # and __doc__ is None |
| 21 | foo = ModuleType.__new__(ModuleType) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 22 | self.assertTrue(foo.__dict__ is None) |
Benjamin Peterson | 1432771 | 2009-08-15 13:23:05 +0000 | [diff] [blame] | 23 | self.assertRaises(SystemError, dir, foo) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 24 | try: |
| 25 | s = foo.__name__ |
| 26 | self.fail("__name__ = %s" % repr(s)) |
| 27 | except AttributeError: |
| 28 | pass |
| 29 | self.assertEqual(foo.__doc__, ModuleType.__doc__) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 31 | def test_no_docstring(self): |
| 32 | # Regularly initialized module, no docstring |
| 33 | foo = ModuleType("foo") |
| 34 | self.assertEqual(foo.__name__, "foo") |
| 35 | self.assertEqual(foo.__doc__, None) |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 36 | self.assertIs(foo.__loader__, None) |
| 37 | self.assertIs(foo.__package__, None) |
| 38 | self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None, |
| 39 | "__loader__": None, "__package__": None}) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 41 | def test_ascii_docstring(self): |
| 42 | # ASCII docstring |
| 43 | foo = ModuleType("foo", "foodoc") |
| 44 | self.assertEqual(foo.__name__, "foo") |
| 45 | self.assertEqual(foo.__doc__, "foodoc") |
| 46 | self.assertEqual(foo.__dict__, |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 47 | {"__name__": "foo", "__doc__": "foodoc", |
| 48 | "__loader__": None, "__package__": None}) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 50 | def test_unicode_docstring(self): |
| 51 | # Unicode docstring |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 52 | foo = ModuleType("foo", "foodoc\u1234") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 53 | self.assertEqual(foo.__name__, "foo") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 54 | self.assertEqual(foo.__doc__, "foodoc\u1234") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 55 | self.assertEqual(foo.__dict__, |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 56 | {"__name__": "foo", "__doc__": "foodoc\u1234", |
| 57 | "__loader__": None, "__package__": None}) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 59 | def test_reinit(self): |
| 60 | # Reinitialization should not replace the __dict__ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 61 | foo = ModuleType("foo", "foodoc\u1234") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 62 | foo.bar = 42 |
| 63 | d = foo.__dict__ |
| 64 | foo.__init__("foo", "foodoc") |
| 65 | self.assertEqual(foo.__name__, "foo") |
| 66 | self.assertEqual(foo.__doc__, "foodoc") |
| 67 | self.assertEqual(foo.bar, 42) |
| 68 | self.assertEqual(foo.__dict__, |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 69 | {"__name__": "foo", "__doc__": "foodoc", "bar": 42, |
| 70 | "__loader__": None, "__package__": None}) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 71 | self.assertTrue(foo.__dict__ is d) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 72 | |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 73 | @unittest.expectedFailure |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 74 | def test_dont_clear_dict(self): |
| 75 | # See issue 7140. |
| 76 | def f(): |
| 77 | foo = ModuleType("foo") |
| 78 | foo.bar = 4 |
| 79 | return foo |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 80 | gc_collect() |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 81 | self.assertEqual(f().__dict__["bar"], 4) |
| 82 | |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 83 | def test_clear_dict_in_ref_cycle(self): |
| 84 | destroyed = [] |
| 85 | m = ModuleType("foo") |
| 86 | m.destroyed = destroyed |
| 87 | s = """class A: |
Benjamin Peterson | ca81bf7 | 2011-12-15 15:57:15 -0500 | [diff] [blame] | 88 | def __init__(self, l): |
| 89 | self.l = l |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 90 | def __del__(self): |
Benjamin Peterson | ca81bf7 | 2011-12-15 15:57:15 -0500 | [diff] [blame] | 91 | self.l.append(1) |
| 92 | a = A(destroyed)""" |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 93 | exec(s, m.__dict__) |
| 94 | del m |
| 95 | gc_collect() |
| 96 | self.assertEqual(destroyed, [1]) |
| 97 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 98 | def test_module_repr_minimal(self): |
| 99 | # reprs when modules have no __file__, __name__, or __loader__ |
| 100 | m = ModuleType('foo') |
| 101 | del m.__name__ |
| 102 | self.assertEqual(repr(m), "<module '?'>") |
| 103 | |
| 104 | def test_module_repr_with_name(self): |
| 105 | m = ModuleType('foo') |
| 106 | self.assertEqual(repr(m), "<module 'foo'>") |
| 107 | |
| 108 | def test_module_repr_with_name_and_filename(self): |
| 109 | m = ModuleType('foo') |
| 110 | m.__file__ = '/tmp/foo.py' |
| 111 | self.assertEqual(repr(m), "<module 'foo' from '/tmp/foo.py'>") |
| 112 | |
| 113 | def test_module_repr_with_filename_only(self): |
| 114 | m = ModuleType('foo') |
| 115 | del m.__name__ |
| 116 | m.__file__ = '/tmp/foo.py' |
| 117 | self.assertEqual(repr(m), "<module '?' from '/tmp/foo.py'>") |
| 118 | |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 119 | def test_module_repr_with_loader_as_None(self): |
| 120 | m = ModuleType('foo') |
| 121 | assert m.__loader__ is None |
| 122 | self.assertEqual(repr(m), "<module 'foo'>") |
| 123 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 124 | def test_module_repr_with_bare_loader_but_no_name(self): |
| 125 | m = ModuleType('foo') |
| 126 | del m.__name__ |
| 127 | # Yes, a class not an instance. |
| 128 | m.__loader__ = BareLoader |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 129 | loader_repr = repr(BareLoader) |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 130 | self.assertEqual( |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 131 | repr(m), "<module '?' ({})>".format(loader_repr)) |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 132 | |
| 133 | def test_module_repr_with_full_loader_but_no_name(self): |
| 134 | # m.__loader__.module_repr() will fail because the module has no |
| 135 | # m.__name__. This exception will get suppressed and instead the |
| 136 | # loader's repr will be used. |
| 137 | m = ModuleType('foo') |
| 138 | del m.__name__ |
| 139 | # Yes, a class not an instance. |
| 140 | m.__loader__ = FullLoader |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 141 | loader_repr = repr(FullLoader) |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 142 | self.assertEqual( |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 143 | repr(m), "<module '?' ({})>".format(loader_repr)) |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 144 | |
| 145 | def test_module_repr_with_bare_loader(self): |
| 146 | m = ModuleType('foo') |
| 147 | # Yes, a class not an instance. |
| 148 | m.__loader__ = BareLoader |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 149 | module_repr = repr(BareLoader) |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 150 | self.assertEqual( |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 151 | repr(m), "<module 'foo' ({})>".format(module_repr)) |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 152 | |
| 153 | def test_module_repr_with_full_loader(self): |
| 154 | m = ModuleType('foo') |
| 155 | # Yes, a class not an instance. |
| 156 | m.__loader__ = FullLoader |
| 157 | self.assertEqual( |
| 158 | repr(m), "<module 'foo' (crafted)>") |
| 159 | |
| 160 | def test_module_repr_with_bare_loader_and_filename(self): |
| 161 | # Because the loader has no module_repr(), use the file name. |
| 162 | m = ModuleType('foo') |
| 163 | # Yes, a class not an instance. |
| 164 | m.__loader__ = BareLoader |
| 165 | m.__file__ = '/tmp/foo.py' |
| 166 | self.assertEqual(repr(m), "<module 'foo' from '/tmp/foo.py'>") |
| 167 | |
| 168 | def test_module_repr_with_full_loader_and_filename(self): |
| 169 | # Even though the module has an __file__, use __loader__.module_repr() |
| 170 | m = ModuleType('foo') |
| 171 | # Yes, a class not an instance. |
| 172 | m.__loader__ = FullLoader |
| 173 | m.__file__ = '/tmp/foo.py' |
| 174 | self.assertEqual(repr(m), "<module 'foo' (crafted)>") |
| 175 | |
| 176 | def test_module_repr_builtin(self): |
| 177 | self.assertEqual(repr(sys), "<module 'sys' (built-in)>") |
| 178 | |
| 179 | def test_module_repr_source(self): |
| 180 | r = repr(unittest) |
| 181 | self.assertEqual(r[:25], "<module 'unittest' from '") |
| 182 | self.assertEqual(r[-13:], "__init__.py'>") |
| 183 | |
| 184 | # frozen and namespace module reprs are tested in importlib. |
| 185 | |
| 186 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 187 | def test_main(): |
| 188 | run_unittest(ModuleTests) |
| 189 | |
Eric V. Smith | 984b11f | 2012-05-24 20:21:04 -0400 | [diff] [blame] | 190 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 191 | if __name__ == '__main__': |
| 192 | test_main() |