1 | n/a | doctests = """ |
---|
2 | n/a | |
---|
3 | n/a | Basic class construction. |
---|
4 | n/a | |
---|
5 | n/a | >>> class C: |
---|
6 | n/a | ... def meth(self): print("Hello") |
---|
7 | n/a | ... |
---|
8 | n/a | >>> C.__class__ is type |
---|
9 | n/a | True |
---|
10 | n/a | >>> a = C() |
---|
11 | n/a | >>> a.__class__ is C |
---|
12 | n/a | True |
---|
13 | n/a | >>> a.meth() |
---|
14 | n/a | Hello |
---|
15 | n/a | >>> |
---|
16 | n/a | |
---|
17 | n/a | Use *args notation for the bases. |
---|
18 | n/a | |
---|
19 | n/a | >>> class A: pass |
---|
20 | n/a | >>> class B: pass |
---|
21 | n/a | >>> bases = (A, B) |
---|
22 | n/a | >>> class C(*bases): pass |
---|
23 | n/a | >>> C.__bases__ == bases |
---|
24 | n/a | True |
---|
25 | n/a | >>> |
---|
26 | n/a | |
---|
27 | n/a | Use a trivial metaclass. |
---|
28 | n/a | |
---|
29 | n/a | >>> class M(type): |
---|
30 | n/a | ... pass |
---|
31 | n/a | ... |
---|
32 | n/a | >>> class C(metaclass=M): |
---|
33 | n/a | ... def meth(self): print("Hello") |
---|
34 | n/a | ... |
---|
35 | n/a | >>> C.__class__ is M |
---|
36 | n/a | True |
---|
37 | n/a | >>> a = C() |
---|
38 | n/a | >>> a.__class__ is C |
---|
39 | n/a | True |
---|
40 | n/a | >>> a.meth() |
---|
41 | n/a | Hello |
---|
42 | n/a | >>> |
---|
43 | n/a | |
---|
44 | n/a | Use **kwds notation for the metaclass keyword. |
---|
45 | n/a | |
---|
46 | n/a | >>> kwds = {'metaclass': M} |
---|
47 | n/a | >>> class C(**kwds): pass |
---|
48 | n/a | ... |
---|
49 | n/a | >>> C.__class__ is M |
---|
50 | n/a | True |
---|
51 | n/a | >>> a = C() |
---|
52 | n/a | >>> a.__class__ is C |
---|
53 | n/a | True |
---|
54 | n/a | >>> |
---|
55 | n/a | |
---|
56 | n/a | Use a metaclass with a __prepare__ static method. |
---|
57 | n/a | |
---|
58 | n/a | >>> class M(type): |
---|
59 | n/a | ... @staticmethod |
---|
60 | n/a | ... def __prepare__(*args, **kwds): |
---|
61 | n/a | ... print("Prepare called:", args, kwds) |
---|
62 | n/a | ... return dict() |
---|
63 | n/a | ... def __new__(cls, name, bases, namespace, **kwds): |
---|
64 | n/a | ... print("New called:", kwds) |
---|
65 | n/a | ... return type.__new__(cls, name, bases, namespace) |
---|
66 | n/a | ... def __init__(cls, *args, **kwds): |
---|
67 | n/a | ... pass |
---|
68 | n/a | ... |
---|
69 | n/a | >>> class C(metaclass=M): |
---|
70 | n/a | ... def meth(self): print("Hello") |
---|
71 | n/a | ... |
---|
72 | n/a | Prepare called: ('C', ()) {} |
---|
73 | n/a | New called: {} |
---|
74 | n/a | >>> |
---|
75 | n/a | |
---|
76 | n/a | Also pass another keyword. |
---|
77 | n/a | |
---|
78 | n/a | >>> class C(object, metaclass=M, other="haha"): |
---|
79 | n/a | ... pass |
---|
80 | n/a | ... |
---|
81 | n/a | Prepare called: ('C', (<class 'object'>,)) {'other': 'haha'} |
---|
82 | n/a | New called: {'other': 'haha'} |
---|
83 | n/a | >>> C.__class__ is M |
---|
84 | n/a | True |
---|
85 | n/a | >>> C.__bases__ == (object,) |
---|
86 | n/a | True |
---|
87 | n/a | >>> a = C() |
---|
88 | n/a | >>> a.__class__ is C |
---|
89 | n/a | True |
---|
90 | n/a | >>> |
---|
91 | n/a | |
---|
92 | n/a | Check that build_class doesn't mutate the kwds dict. |
---|
93 | n/a | |
---|
94 | n/a | >>> kwds = {'metaclass': type} |
---|
95 | n/a | >>> class C(**kwds): pass |
---|
96 | n/a | ... |
---|
97 | n/a | >>> kwds == {'metaclass': type} |
---|
98 | n/a | True |
---|
99 | n/a | >>> |
---|
100 | n/a | |
---|
101 | n/a | Use various combinations of explicit keywords and **kwds. |
---|
102 | n/a | |
---|
103 | n/a | >>> bases = (object,) |
---|
104 | n/a | >>> kwds = {'metaclass': M, 'other': 'haha'} |
---|
105 | n/a | >>> class C(*bases, **kwds): pass |
---|
106 | n/a | ... |
---|
107 | n/a | Prepare called: ('C', (<class 'object'>,)) {'other': 'haha'} |
---|
108 | n/a | New called: {'other': 'haha'} |
---|
109 | n/a | >>> C.__class__ is M |
---|
110 | n/a | True |
---|
111 | n/a | >>> C.__bases__ == (object,) |
---|
112 | n/a | True |
---|
113 | n/a | >>> class B: pass |
---|
114 | n/a | >>> kwds = {'other': 'haha'} |
---|
115 | n/a | >>> class C(B, metaclass=M, *bases, **kwds): pass |
---|
116 | n/a | ... |
---|
117 | n/a | Prepare called: ('C', (<class 'test.test_metaclass.B'>, <class 'object'>)) {'other': 'haha'} |
---|
118 | n/a | New called: {'other': 'haha'} |
---|
119 | n/a | >>> C.__class__ is M |
---|
120 | n/a | True |
---|
121 | n/a | >>> C.__bases__ == (B, object) |
---|
122 | n/a | True |
---|
123 | n/a | >>> |
---|
124 | n/a | |
---|
125 | n/a | Check for duplicate keywords. |
---|
126 | n/a | |
---|
127 | n/a | >>> class C(metaclass=type, metaclass=type): pass |
---|
128 | n/a | ... |
---|
129 | n/a | Traceback (most recent call last): |
---|
130 | n/a | [...] |
---|
131 | n/a | SyntaxError: keyword argument repeated |
---|
132 | n/a | >>> |
---|
133 | n/a | |
---|
134 | n/a | Another way. |
---|
135 | n/a | |
---|
136 | n/a | >>> kwds = {'metaclass': type} |
---|
137 | n/a | >>> class C(metaclass=type, **kwds): pass |
---|
138 | n/a | ... |
---|
139 | n/a | Traceback (most recent call last): |
---|
140 | n/a | [...] |
---|
141 | n/a | TypeError: __build_class__() got multiple values for keyword argument 'metaclass' |
---|
142 | n/a | >>> |
---|
143 | n/a | |
---|
144 | n/a | Use a __prepare__ method that returns an instrumented dict. |
---|
145 | n/a | |
---|
146 | n/a | >>> class LoggingDict(dict): |
---|
147 | n/a | ... def __setitem__(self, key, value): |
---|
148 | n/a | ... print("d[%r] = %r" % (key, value)) |
---|
149 | n/a | ... dict.__setitem__(self, key, value) |
---|
150 | n/a | ... |
---|
151 | n/a | >>> class Meta(type): |
---|
152 | n/a | ... @staticmethod |
---|
153 | n/a | ... def __prepare__(name, bases): |
---|
154 | n/a | ... return LoggingDict() |
---|
155 | n/a | ... |
---|
156 | n/a | >>> class C(metaclass=Meta): |
---|
157 | n/a | ... foo = 2+2 |
---|
158 | n/a | ... foo = 42 |
---|
159 | n/a | ... bar = 123 |
---|
160 | n/a | ... |
---|
161 | n/a | d['__module__'] = 'test.test_metaclass' |
---|
162 | n/a | d['__qualname__'] = 'C' |
---|
163 | n/a | d['foo'] = 4 |
---|
164 | n/a | d['foo'] = 42 |
---|
165 | n/a | d['bar'] = 123 |
---|
166 | n/a | >>> |
---|
167 | n/a | |
---|
168 | n/a | Use a metaclass that doesn't derive from type. |
---|
169 | n/a | |
---|
170 | n/a | >>> def meta(name, bases, namespace, **kwds): |
---|
171 | n/a | ... print("meta:", name, bases) |
---|
172 | n/a | ... print("ns:", sorted(namespace.items())) |
---|
173 | n/a | ... print("kw:", sorted(kwds.items())) |
---|
174 | n/a | ... return namespace |
---|
175 | n/a | ... |
---|
176 | n/a | >>> class C(metaclass=meta): |
---|
177 | n/a | ... a = 42 |
---|
178 | n/a | ... b = 24 |
---|
179 | n/a | ... |
---|
180 | n/a | meta: C () |
---|
181 | n/a | ns: [('__module__', 'test.test_metaclass'), ('__qualname__', 'C'), ('a', 42), ('b', 24)] |
---|
182 | n/a | kw: [] |
---|
183 | n/a | >>> type(C) is dict |
---|
184 | n/a | True |
---|
185 | n/a | >>> print(sorted(C.items())) |
---|
186 | n/a | [('__module__', 'test.test_metaclass'), ('__qualname__', 'C'), ('a', 42), ('b', 24)] |
---|
187 | n/a | >>> |
---|
188 | n/a | |
---|
189 | n/a | And again, with a __prepare__ attribute. |
---|
190 | n/a | |
---|
191 | n/a | >>> def prepare(name, bases, **kwds): |
---|
192 | n/a | ... print("prepare:", name, bases, sorted(kwds.items())) |
---|
193 | n/a | ... return LoggingDict() |
---|
194 | n/a | ... |
---|
195 | n/a | >>> meta.__prepare__ = prepare |
---|
196 | n/a | >>> class C(metaclass=meta, other="booh"): |
---|
197 | n/a | ... a = 1 |
---|
198 | n/a | ... a = 2 |
---|
199 | n/a | ... b = 3 |
---|
200 | n/a | ... |
---|
201 | n/a | prepare: C () [('other', 'booh')] |
---|
202 | n/a | d['__module__'] = 'test.test_metaclass' |
---|
203 | n/a | d['__qualname__'] = 'C' |
---|
204 | n/a | d['a'] = 1 |
---|
205 | n/a | d['a'] = 2 |
---|
206 | n/a | d['b'] = 3 |
---|
207 | n/a | meta: C () |
---|
208 | n/a | ns: [('__module__', 'test.test_metaclass'), ('__qualname__', 'C'), ('a', 2), ('b', 3)] |
---|
209 | n/a | kw: [('other', 'booh')] |
---|
210 | n/a | >>> |
---|
211 | n/a | |
---|
212 | n/a | The default metaclass must define a __prepare__() method. |
---|
213 | n/a | |
---|
214 | n/a | >>> type.__prepare__() |
---|
215 | n/a | {} |
---|
216 | n/a | >>> |
---|
217 | n/a | |
---|
218 | n/a | Make sure it works with subclassing. |
---|
219 | n/a | |
---|
220 | n/a | >>> class M(type): |
---|
221 | n/a | ... @classmethod |
---|
222 | n/a | ... def __prepare__(cls, *args, **kwds): |
---|
223 | n/a | ... d = super().__prepare__(*args, **kwds) |
---|
224 | n/a | ... d["hello"] = 42 |
---|
225 | n/a | ... return d |
---|
226 | n/a | ... |
---|
227 | n/a | >>> class C(metaclass=M): |
---|
228 | n/a | ... print(hello) |
---|
229 | n/a | ... |
---|
230 | n/a | 42 |
---|
231 | n/a | >>> print(C.hello) |
---|
232 | n/a | 42 |
---|
233 | n/a | >>> |
---|
234 | n/a | |
---|
235 | n/a | Test failures in looking up the __prepare__ method work. |
---|
236 | n/a | >>> class ObscureException(Exception): |
---|
237 | n/a | ... pass |
---|
238 | n/a | >>> class FailDescr: |
---|
239 | n/a | ... def __get__(self, instance, owner): |
---|
240 | n/a | ... raise ObscureException |
---|
241 | n/a | >>> class Meta(type): |
---|
242 | n/a | ... __prepare__ = FailDescr() |
---|
243 | n/a | >>> class X(metaclass=Meta): |
---|
244 | n/a | ... pass |
---|
245 | n/a | Traceback (most recent call last): |
---|
246 | n/a | [...] |
---|
247 | n/a | test.test_metaclass.ObscureException |
---|
248 | n/a | |
---|
249 | n/a | """ |
---|
250 | n/a | |
---|
251 | n/a | import sys |
---|
252 | n/a | |
---|
253 | n/a | # Trace function introduces __locals__ which causes various tests to fail. |
---|
254 | n/a | if hasattr(sys, 'gettrace') and sys.gettrace(): |
---|
255 | n/a | __test__ = {} |
---|
256 | n/a | else: |
---|
257 | n/a | __test__ = {'doctests' : doctests} |
---|
258 | n/a | |
---|
259 | n/a | def test_main(verbose=False): |
---|
260 | n/a | from test import support |
---|
261 | n/a | from test import test_metaclass |
---|
262 | n/a | support.run_doctest(test_metaclass, verbose) |
---|
263 | n/a | |
---|
264 | n/a | if __name__ == "__main__": |
---|
265 | n/a | test_main(verbose=True) |
---|