Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 1 | # Test the module type |
| 2 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 3 | from test.test_support import verify, vereq, verbose, TestFailed |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 4 | |
| 5 | import sys |
| 6 | module = type(sys) |
| 7 | |
| 8 | # An uninitialized module has no __dict__ or __name__, and __doc__ is None |
| 9 | foo = module.__new__(module) |
| 10 | verify(foo.__dict__ is None) |
| 11 | try: |
| 12 | s = foo.__name__ |
| 13 | except AttributeError: |
| 14 | pass |
| 15 | else: |
| 16 | raise TestFailed, "__name__ = %s" % repr(s) |
Guido van Rossum | bdabecc | 2002-06-04 06:06:54 +0000 | [diff] [blame] | 17 | vereq(foo.__doc__, module.__doc__) |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 18 | |
| 19 | # Regularly initialized module, no docstring |
| 20 | foo = module("foo") |
| 21 | vereq(foo.__name__, "foo") |
| 22 | vereq(foo.__doc__, None) |
| 23 | vereq(foo.__dict__, {"__name__": "foo", "__doc__": None}) |
| 24 | |
| 25 | # ASCII docstring |
| 26 | foo = module("foo", "foodoc") |
| 27 | vereq(foo.__name__, "foo") |
| 28 | vereq(foo.__doc__, "foodoc") |
| 29 | vereq(foo.__dict__, {"__name__": "foo", "__doc__": "foodoc"}) |
| 30 | |
| 31 | # Unicode docstring |
| 32 | foo = module("foo", u"foodoc\u1234") |
| 33 | vereq(foo.__name__, "foo") |
| 34 | vereq(foo.__doc__, u"foodoc\u1234") |
| 35 | vereq(foo.__dict__, {"__name__": "foo", "__doc__": u"foodoc\u1234"}) |
| 36 | |
| 37 | # Reinitialization should not replace the __dict__ |
| 38 | foo.bar = 42 |
| 39 | d = foo.__dict__ |
| 40 | foo.__init__("foo", "foodoc") |
| 41 | vereq(foo.__name__, "foo") |
| 42 | vereq(foo.__doc__, "foodoc") |
| 43 | vereq(foo.bar, 42) |
| 44 | vereq(foo.__dict__, {"__name__": "foo", "__doc__": "foodoc", "bar": 42}) |
| 45 | verify(foo.__dict__ is d) |
| 46 | |
| 47 | if verbose: |
| 48 | print "All OK" |