blob: 09ebe3fec0bdbe32c6d113871fc752601fc694dc [file] [log] [blame]
Miss Islington (bot)31f1b522018-04-07 09:27:05 -07001"""Test module for the custom examples
Georg Brandl116aa622007-08-15 14:28:22 +00002
Miss Islington (bot)31f1b522018-04-07 09:27:05 -07003Custom 1:
Georg Brandl116aa622007-08-15 14:28:22 +00004
Miss Islington (bot)31f1b522018-04-07 09:27:05 -07005>>> 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
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070012Custom 2
Georg Brandl116aa622007-08-15 14:28:22 +000013
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070014>>> import custom2
15>>> c1 = custom2.Custom('jim', 'fulton', 42)
16>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +000017'jim'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070018>>> c1.last
Georg Brandl116aa622007-08-15 14:28:22 +000019'fulton'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070020>>> c1.number
Georg Brandl116aa622007-08-15 14:28:22 +00002142
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070022>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000023'jim fulton'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070024>>> c1.first = 'will'
25>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000026'will fulton'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070027>>> c1.last = 'tell'
28>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000029'will tell'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070030>>> del c1.first
31>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000032Traceback (most recent call last):
33...
34AttributeError: first
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070035>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +000036Traceback (most recent call last):
37...
38AttributeError: first
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070039>>> c1.first = 'drew'
40>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +000041'drew'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070042>>> del c1.number
Georg Brandl116aa622007-08-15 14:28:22 +000043Traceback (most recent call last):
44...
45TypeError: can't delete numeric/char attribute
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070046>>> c1.number=2
47>>> c1.number
Georg Brandl116aa622007-08-15 14:28:22 +0000482
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070049>>> c1.first = 42
50>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000051'42 tell'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070052>>> c2 = custom2.Custom()
53>>> c2.name()
Georg Brandl116aa622007-08-15 14:28:22 +000054' '
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070055>>> c2.first
Georg Brandl116aa622007-08-15 14:28:22 +000056''
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070057>>> c2.last
Georg Brandl116aa622007-08-15 14:28:22 +000058''
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070059>>> del c2.first
60>>> c2.first
Georg Brandl116aa622007-08-15 14:28:22 +000061Traceback (most recent call last):
62...
63AttributeError: first
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070064>>> c2.first
Georg Brandl116aa622007-08-15 14:28:22 +000065Traceback (most recent call last):
66...
67AttributeError: first
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070068>>> c2.name()
Georg Brandl116aa622007-08-15 14:28:22 +000069Traceback (most recent call last):
70 File "<stdin>", line 1, in ?
71AttributeError: first
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070072>>> c2.number
Georg Brandl116aa622007-08-15 14:28:22 +0000730
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070074>>> n3 = custom2.Custom('jim', 'fulton', 'waaa')
Georg Brandl116aa622007-08-15 14:28:22 +000075Traceback (most recent call last):
76 File "<stdin>", line 1, in ?
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070077TypeError: an integer is required (got type str)
78>>> del c1
79>>> del c2
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070082Custom 3
Georg Brandl116aa622007-08-15 14:28:22 +000083
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070084>>> 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'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070089>>> 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
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070093>>> 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
Miss Islington (bot)31f1b522018-04-07 09:27:05 -070097>>> c1.first = 'will'
98>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +000099'will fulton'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700100>>> 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 ?
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700106TypeError: an integer is required (got type str)
107>>> del c1
108>>> del c2
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700110Custom 4
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700112>>> import custom4
113>>> c1 = custom4.Custom('jim', 'fulton', 42)
114>>> c1.first
Georg Brandl116aa622007-08-15 14:28:22 +0000115'jim'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700116>>> c1.last
Georg Brandl116aa622007-08-15 14:28:22 +0000117'fulton'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700118>>> c1.number
Georg Brandl116aa622007-08-15 14:28:22 +000011942
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700120>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +0000121'jim fulton'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700122>>> c1.first = 'will'
123>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +0000124'will fulton'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700125>>> c1.last = 'tell'
126>>> c1.name()
Georg Brandl116aa622007-08-15 14:28:22 +0000127'will tell'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700128>>> del c1.first
Georg Brandl116aa622007-08-15 14:28:22 +0000129Traceback (most recent call last):
130...
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700131TypeError: 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'
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700137>>> del c1.number
Georg Brandl116aa622007-08-15 14:28:22 +0000138Traceback (most recent call last):
139...
140TypeError: can't delete numeric/char attribute
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700141>>> c1.number=2
142>>> c1.number
Georg Brandl116aa622007-08-15 14:28:22 +00001432
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700144>>> 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' '
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700156>>> c2.first
Georg Brandl116aa622007-08-15 14:28:22 +0000157''
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700158>>> c2.last
Georg Brandl116aa622007-08-15 14:28:22 +0000159''
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700160>>> c2.number
Georg Brandl116aa622007-08-15 14:28:22 +00001610
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700162>>> n3 = custom4.Custom('jim', 'fulton', 'waaa')
Georg Brandl116aa622007-08-15 14:28:22 +0000163Traceback (most recent call last):
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700164...
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
Miss Islington (bot)31f1b522018-04-07 09:27:05 -0700173>>> 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__)