Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Doc/includes/test.py
12 views
1
"""Test module for the custom examples
2
3
Custom 1:
4
5
>>> import custom
6
>>> c1 = custom.Custom()
7
>>> c2 = custom.Custom()
8
>>> del c1
9
>>> del c2
10
11
12
Custom 2
13
14
>>> import custom2
15
>>> c1 = custom2.Custom('jim', 'fulton', 42)
16
>>> c1.first
17
'jim'
18
>>> c1.last
19
'fulton'
20
>>> c1.number
21
42
22
>>> c1.name()
23
'jim fulton'
24
>>> c1.first = 'will'
25
>>> c1.name()
26
'will fulton'
27
>>> c1.last = 'tell'
28
>>> c1.name()
29
'will tell'
30
>>> del c1.first
31
>>> c1.name()
32
Traceback (most recent call last):
33
...
34
AttributeError: first
35
>>> c1.first
36
Traceback (most recent call last):
37
...
38
AttributeError: first
39
>>> c1.first = 'drew'
40
>>> c1.first
41
'drew'
42
>>> del c1.number
43
Traceback (most recent call last):
44
...
45
TypeError: can't delete numeric/char attribute
46
>>> c1.number=2
47
>>> c1.number
48
2
49
>>> c1.first = 42
50
>>> c1.name()
51
'42 tell'
52
>>> c2 = custom2.Custom()
53
>>> c2.name()
54
' '
55
>>> c2.first
56
''
57
>>> c2.last
58
''
59
>>> del c2.first
60
>>> c2.first
61
Traceback (most recent call last):
62
...
63
AttributeError: first
64
>>> c2.first
65
Traceback (most recent call last):
66
...
67
AttributeError: first
68
>>> c2.name()
69
Traceback (most recent call last):
70
File "<stdin>", line 1, in ?
71
AttributeError: first
72
>>> c2.number
73
0
74
>>> n3 = custom2.Custom('jim', 'fulton', 'waaa')
75
Traceback (most recent call last):
76
File "<stdin>", line 1, in ?
77
TypeError: an integer is required (got type str)
78
>>> del c1
79
>>> del c2
80
81
82
Custom 3
83
84
>>> import custom3
85
>>> c1 = custom3.Custom('jim', 'fulton', 42)
86
>>> c1 = custom3.Custom('jim', 'fulton', 42)
87
>>> c1.name()
88
'jim fulton'
89
>>> del c1.first
90
Traceback (most recent call last):
91
File "<stdin>", line 1, in ?
92
TypeError: Cannot delete the first attribute
93
>>> c1.first = 42
94
Traceback (most recent call last):
95
File "<stdin>", line 1, in ?
96
TypeError: The first attribute value must be a string
97
>>> c1.first = 'will'
98
>>> c1.name()
99
'will fulton'
100
>>> c2 = custom3.Custom()
101
>>> c2 = custom3.Custom()
102
>>> c2 = custom3.Custom()
103
>>> n3 = custom3.Custom('jim', 'fulton', 'waaa')
104
Traceback (most recent call last):
105
File "<stdin>", line 1, in ?
106
TypeError: an integer is required (got type str)
107
>>> del c1
108
>>> del c2
109
110
Custom 4
111
112
>>> import custom4
113
>>> c1 = custom4.Custom('jim', 'fulton', 42)
114
>>> c1.first
115
'jim'
116
>>> c1.last
117
'fulton'
118
>>> c1.number
119
42
120
>>> c1.name()
121
'jim fulton'
122
>>> c1.first = 'will'
123
>>> c1.name()
124
'will fulton'
125
>>> c1.last = 'tell'
126
>>> c1.name()
127
'will tell'
128
>>> del c1.first
129
Traceback (most recent call last):
130
...
131
TypeError: Cannot delete the first attribute
132
>>> c1.name()
133
'will tell'
134
>>> c1.first = 'drew'
135
>>> c1.first
136
'drew'
137
>>> del c1.number
138
Traceback (most recent call last):
139
...
140
TypeError: can't delete numeric/char attribute
141
>>> c1.number=2
142
>>> c1.number
143
2
144
>>> c1.first = 42
145
Traceback (most recent call last):
146
...
147
TypeError: 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()
155
' '
156
>>> c2.first
157
''
158
>>> c2.last
159
''
160
>>> c2.number
161
0
162
>>> n3 = custom4.Custom('jim', 'fulton', 'waaa')
163
Traceback (most recent call last):
164
...
165
TypeError: an integer is required (got type str)
166
167
168
Test cyclic gc(?)
169
170
>>> import gc
171
>>> gc.disable()
172
173
>>> 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
181
>>> sys.getrefcount(x)
182
3
183
>>> ignore = gc.collect()
184
>>> sys.getrefcount(x)
185
2
186
187
>>> gc.enable()
188
"""
189
190
import os
191
import sys
192
from distutils.util import get_platform
193
PLAT_SPEC = "%s-%d.%d" % (get_platform(), *sys.version_info[:2])
194
src = os.path.join("build", "lib.%s" % PLAT_SPEC)
195
sys.path.append(src)
196
197
if __name__ == "__main__":
198
import doctest, __main__
199
doctest.testmod(__main__)
200
201