Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_module.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 20d09549b82c |
| children | 7909436408e2 |
| 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 |
|
45468
1d84bf5f9478
Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents:
41532
diff
changeset
|
3 from test.test_support import run_unittest |
|
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) |
|
55644
113c0b86fd74
#6707 fix a crash with dir() on an uninitialized module
Benjamin Peterson <benjamin@python.org>
parents:
55002
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 |
|
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
|
58 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
|
59 # 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
|
60 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
|
61 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
|
62 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
|
63 return 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
|
64 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
|
65 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
66 def test_main(): |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
67 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
|
68 |
|
41401
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
69 if __name__ == '__main__': |
|
c7b830c86f42
Convert test_module to use unittest.
Collin Winter <collinw@gmail.com>
parents:
24364
diff
changeset
|
70 test_main() |
