blob: 09ebe3fec0bdbe32c6d113871fc752601fc694dc [file] [log] [blame]
Antoine Pitrou1d80a562018-04-07 18:14:03 +02001"""Test module for the custom examples
Georg Brandl116aa622007-08-15 14:28:22 +00002
Antoine Pitrou1d80a562018-04-07 18:14:03 +02003Custom 1:
Georg Brandl116aa622007-08-15 14:28:22 +00004
Antoine Pitrou1d80a562018-04-07 18:14:03 +02005>>> import custom
6>>> c1 = custom.Custom()
7>>> c2 = custom.Custom()
8>>> del c1
9>>> del c2
Georg Brandl116aa622007-08-15 14:28:22 +000010
11
Antoine Pitrou1d80a562018-04-07 18:14:03 +020012Custom 2
Georg Brandl116aa622007-08-15 14:28:22 +000013
Antoine Pitrou1d80a562018-04-07 18:14:03 +020014>>> import custom2
15>>> c1 = custom2.Custom('jim', 'fulton', 42)
16>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +000017'jim'
Antoine Pitrou1d80a562018-04-07 18:14:03 +020018>>> c1.last
Georg Brandl116aa622007-08-15 14:28:22 +000019'fulton'
Antoine Pitrou1d80a562018-04-07 18:14:03 +020020>>> c1.number
Georg Brandl116aa622007-08-15 14:28:22 +00002142
Antoine Pitrou1d80a562018-04-07 18:14:03 +020022>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000023'jim fulton'
Antoine Pitrou1d80a562018-04-07 18:14:03 +020024>>> c1.first = 'will'
25>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000026'will fulton'
Antoine Pitrou1d80a562018-04-07 18:14:03 +020027>>> c1.last = 'tell'
28>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000029'will tell'
Antoine Pitrou1d80a562018-04-07 18:14:03 +020030>>> del c1.first
31>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000032Traceback (most recent call last):
33...
34AttributeError: first
Antoine Pitrou1d80a562018-04-07 18:14:03 +020035>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +000036Traceback (most recent call last):
37...
38AttributeError: first
Antoine Pitrou1d80a562018-04-07 18:14:03 +020039>>> c1.first = 'drew'
40>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +000041'drew'
Antoine Pitrou1d80a562018-04-07 18:14:03 +020042>>> del c1.number
Georg Brandl116aa622007-08-15 14:28:22 +000043Traceback (most recent call last):
44...
45TypeError: can't delete numeric/char attribute
Antoine Pitrou1d80a562018-04-07 18:14:03 +020046>>> c1.number=2
47>>> c1.number
Georg Brandl116aa622007-08-15 14:28:22 +0000482
Antoine Pitrou1d80a562018-04-07 18:14:03 +020049>>> c1.first = 42
50>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000051'42 tell'
Antoine Pitrou1d80a562018-04-07 18:14:03 +020052>>> c2 = custom2.Custom()
53>>> c2.name()
Georg Brandl116aa622007-08-15 14:28:22 +000054' '
Antoine Pitrou1d80a562018-04-07 18:14:03 +020055>>> c2.first
Georg Brandl116aa622007-08-15 14:28:22 +000056''
Antoine Pitrou1d80a562018-04-07 18:14:03 +020057>>> c2.last
Georg Brandl116aa622007-08-15 14:28:22 +000058''
Antoine Pitrou1d80a562018-04-07 18:14:03 +020059>>> del c2.first
60>>> c2.first
Georg Brandl116aa622007-08-15 14:28:22 +000061Traceback (most recent call last):
62...
63AttributeError: first
Antoine Pitrou1d80a562018-04-07 18:14:03 +020064>>> c2.first
Georg Brandl116aa622007-08-15 14:28:22 +000065Traceback (most recent call last):
66...
67AttributeError: first
Antoine Pitrou1d80a562018-04-07 18:14:03 +020068>>> c2.name()
Georg Brandl116aa622007-08-15 14:28:22 +000069Traceback (most recent call last):
70 File "<stdin>", line 1, in ?
71AttributeError: first
Antoine Pitrou1d80a562018-04-07 18:14:03 +020072>>> c2.number
Georg Brandl116aa622007-08-15 14:28:22 +0000730
Antoine Pitrou1d80a562018-04-07 18:14:03 +020074>>> n3 = custom2.Custom('jim', 'fulton', 'waaa')
Georg Brandl116aa622007-08-15 14:28:22 +000075Traceback (most recent call last):
76 File "<stdin>", line 1, in ?
Antoine Pitrou1d80a562018-04-07 18:14:03 +020077TypeError: an integer is required (got type str)
78>>> del c1
79>>> del c2
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Antoine Pitrou1d80a562018-04-07 18:14:03 +020082Custom 3
Georg Brandl116aa622007-08-15 14:28:22 +000083
Antoine Pitrou1d80a562018-04-07 18:14:03 +020084>>> import custom3
85>>> c1 = custom3.Custom('jim', 'fulton', 42)
86>>> c1 = custom3.Custom('jim', 'fulton', 42)
87>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000088'jim fulton'
Antoine Pitrou1d80a562018-04-07 18:14:03 +020089>>> del c1.first
Georg Brandl116aa622007-08-15 14:28:22 +000090Traceback (most recent call last):
91 File "<stdin>", line 1, in ?
92TypeError: Cannot delete the first attribute
Antoine Pitrou1d80a562018-04-07 18:14:03 +020093>>> c1.first = 42
Georg Brandl116aa622007-08-15 14:28:22 +000094Traceback (most recent call last):
95 File "<stdin>", line 1, in ?
96TypeError: The first attribute value must be a string
Antoine Pitrou1d80a562018-04-07 18:14:03 +020097>>> c1.first = 'will'
98>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000099'will fulton'
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200100>>> c2 = custom3.Custom()
101>>> c2 = custom3.Custom()
102>>> c2 = custom3.Custom()
103>>> n3 = custom3.Custom('jim', 'fulton', 'waaa')
Georg Brandl116aa622007-08-15 14:28:22 +0000104Traceback (most recent call last):
105 File "<stdin>", line 1, in ?
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200106TypeError: an integer is required (got type str)
107>>> del c1
108>>> del c2
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200110Custom 4
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200112>>> import custom4
113>>> c1 = custom4.Custom('jim', 'fulton', 42)
114>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +0000115'jim'
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200116>>> c1.last
Georg Brandl116aa622007-08-15 14:28:22 +0000117'fulton'
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200118>>> c1.number
Georg Brandl116aa622007-08-15 14:28:22 +000011942
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200120>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +0000121'jim fulton'
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200122>>> c1.first = 'will'
123>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +0000124'will fulton'
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200125>>> c1.last = 'tell'
126>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +0000127'will tell'
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200128>>> del c1.first
Georg Brandl116aa622007-08-15 14:28:22 +0000129Traceback (most recent call last):
130...
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200131TypeError: Cannot delete the first attribute
132>>> c1.name()
133'will tell'
134>>> c1.first = 'drew'
135>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +0000136'drew'
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200137>>> del c1.number
Georg Brandl116aa622007-08-15 14:28:22 +0000138Traceback (most recent call last):
139...
140TypeError: can't delete numeric/char attribute
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200141>>> c1.number=2
142>>> c1.number
Georg Brandl116aa622007-08-15 14:28:22 +00001432
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200144>>> c1.first = 42
145Traceback (most recent call last):
146...
147TypeError: The first attribute value must be a string
148>>> c1.name()
149'drew tell'
150>>> c2 = custom4.Custom()
151>>> c2 = custom4.Custom()
152>>> c2 = custom4.Custom()
153>>> c2 = custom4.Custom()
154>>> c2.name()
Georg Brandl116aa622007-08-15 14:28:22 +0000155' '
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200156>>> c2.first
Georg Brandl116aa622007-08-15 14:28:22 +0000157''
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200158>>> c2.last
Georg Brandl116aa622007-08-15 14:28:22 +0000159''
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200160>>> c2.number
Georg Brandl116aa622007-08-15 14:28:22 +00001610
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200162>>> n3 = custom4.Custom('jim', 'fulton', 'waaa')
Georg Brandl116aa622007-08-15 14:28:22 +0000163Traceback (most recent call last):
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200164...
165TypeError: an integer is required (got type str)
Georg Brandl116aa622007-08-15 14:28:22 +0000166
167
168Test cyclic gc(?)
169
170>>> import gc
171>>> gc.disable()
172
Antoine Pitrou1d80a562018-04-07 18:14:03 +0200173>>> class Subclass(custom4.Custom): pass
174...
175>>> s = Subclass()
176>>> s.cycle = [s]
177>>> s.cycle.append(s.cycle)
178>>> x = object()
179>>> s.x = x
180>>> del s
Georg Brandl116aa622007-08-15 14:28:22 +0000181>>> sys.getrefcount(x)
1823
183>>> ignore = gc.collect()
184>>> sys.getrefcount(x)
1852
186
187>>> gc.enable()
188"""
189
190import os
191import sys
192from distutils.util import get_platform
Serhiy Storchaka885bdc42016-02-11 13:10:36 +0200193PLAT_SPEC = "%s-%d.%d" % (get_platform(), *sys.version_info[:2])
Georg Brandl116aa622007-08-15 14:28:22 +0000194src = os.path.join("build", "lib.%s" % PLAT_SPEC)
195sys.path.append(src)
196
197if __name__ == "__main__":
198 import doctest, __main__
199 doctest.testmod(__main__)