Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_module.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 2e8b28dbc395 |
| children |
| rev | line source |
|---|---|
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 # Test the module type |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
2 import unittest |
|
65232
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
3 from test.test_support import run_unittest, gc_collect |
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import sys |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
6 ModuleType = type(sys) |
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
8 class ModuleTests(unittest.TestCase): |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
9 def test_uninitialized(self): |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
10 # An uninitialized module has no __dict__ or __name__, |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
11 # and __doc__ is None |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
12 foo = ModuleType.__new__(ModuleType) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
13 self.assertTrue(foo.__dict__ is None) |
|
55645
bc1c403054e1
Merged revisions 74457 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
14 self.assertRaises(SystemError, dir, foo) |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
15 try: |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
16 s = foo.__name__ |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
17 self.fail("__name__ = %s" % repr(s)) |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
18 except AttributeError: |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
19 pass |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
20 self.assertEqual(foo.__doc__, ModuleType.__doc__) |
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
22 def test_no_docstring(self): |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
23 # Regularly initialized module, no docstring |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
24 foo = ModuleType("foo") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
25 self.assertEqual(foo.__name__, "foo") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
26 self.assertEqual(foo.__doc__, None) |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
27 self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None}) |
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
29 def test_ascii_docstring(self): |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
30 # ASCII docstring |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
31 foo = ModuleType("foo", "foodoc") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
32 self.assertEqual(foo.__name__, "foo") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
33 self.assertEqual(foo.__doc__, "foodoc") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
34 self.assertEqual(foo.__dict__, |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
35 {"__name__": "foo", "__doc__": "foodoc"}) |
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
37 def test_unicode_docstring(self): |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
38 # Unicode docstring |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
39 foo = ModuleType("foo", u"foodoc\u1234") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
40 self.assertEqual(foo.__name__, "foo") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
41 self.assertEqual(foo.__doc__, u"foodoc\u1234") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
42 self.assertEqual(foo.__dict__, |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
43 {"__name__": "foo", "__doc__": u"foodoc\u1234"}) |
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
45 def test_reinit(self): |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
46 # Reinitialization should not replace the __dict__ |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
47 foo = ModuleType("foo", u"foodoc\u1234") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
48 foo.bar = 42 |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
49 d = foo.__dict__ |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
50 foo.__init__("foo", "foodoc") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
51 self.assertEqual(foo.__name__, "foo") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
52 self.assertEqual(foo.__doc__, "foodoc") |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
53 self.assertEqual(foo.bar, 42) |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
54 self.assertEqual(foo.__dict__, |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
55 {"__name__": "foo", "__doc__": "foodoc", "bar": 42}) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
45468
diff
changeset
|
56 self.assertTrue(foo.__dict__ is d) |
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
65232
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
58 @unittest.expectedFailure |
|
56542
20d09549b82c
only clear a module's __dict__ if the module is the only one with a reference to it #7140
Benjamin Peterson <benjamin@python.org>
parents:
55644
diff
changeset
|
59 def test_dont_clear_dict(self): |
|
20d09549b82c
only clear a module's __dict__ if the module is the only one with a reference to it #7140
Benjamin Peterson <benjamin@python.org>
parents:
55644
diff
changeset
|
60 # See issue 7140. |
|
20d09549b82c
only clear a module's __dict__ if the module is the only one with a reference to it #7140
Benjamin Peterson <benjamin@python.org>
parents:
55644
diff
changeset
|
61 def f(): |
|
20d09549b82c
only clear a module's __dict__ if the module is the only one with a reference to it #7140
Benjamin Peterson <benjamin@python.org>
parents:
55644
diff
changeset
|
62 foo = ModuleType("foo") |
|
20d09549b82c
only clear a module's __dict__ if the module is the only one with a reference to it #7140
Benjamin Peterson <benjamin@python.org>
parents:
55644
diff
changeset
|
63 foo.bar = 4 |
|
20d09549b82c
only clear a module's __dict__ if the module is the only one with a reference to it #7140
Benjamin Peterson <benjamin@python.org>
parents:
55644
diff
changeset
|
64 return foo |
|
65232
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
65 gc_collect() |
|
56542
20d09549b82c
only clear a module's __dict__ if the module is the only one with a reference to it #7140
Benjamin Peterson <benjamin@python.org>
parents:
55644
diff
changeset
|
66 self.assertEqual(f().__dict__["bar"], 4) |
|
20d09549b82c
only clear a module's __dict__ if the module is the only one with a reference to it #7140
Benjamin Peterson <benjamin@python.org>
parents:
55644
diff
changeset
|
67 |
|
65232
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
68 def test_clear_dict_in_ref_cycle(self): |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
69 destroyed = [] |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
70 m = ModuleType("foo") |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
71 m.destroyed = destroyed |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
72 s = """class A: |
|
75072
2e8b28dbc395
don't rely on the order of module clearing
Benjamin Peterson <benjamin@python.org>
parents:
68256
diff
changeset
|
73 def __del__(self, destroyed=destroyed): |
|
65232
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
74 destroyed.append(1) |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
75 a = A()""" |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
76 exec(s, m.__dict__) |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
77 del m |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
78 gc_collect() |
|
7909436408e2
Merged revisions 85392 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56542
diff
changeset
|
79 self.assertEqual(destroyed, [1]) |
|
23652
b40165a4efe9
Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
81 def test_main(): |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
82 run_unittest(ModuleTests) |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41401
diff
changeset
|
83 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
84 if __name__ == '__main__': |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
85 test_main() |
