blob: b8862fd6fcacec559fa46fab5a9927d5fb188688 [file] [log] [blame]
Benjamin Petersonae937c02009-04-18 20:54:08 +00001import builtins
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002import copyreg
Benjamin Peterson52c42432012-03-07 18:41:11 -06003import gc
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004import itertools
5import math
6import pickle
Raymond Hettinger51f46882020-12-18 16:53:50 -08007import random
8import string
Benjamin Petersona5758c02009-05-09 18:15:04 +00009import sys
Guido van Rossum360e4b82007-05-14 22:51:27 +000010import types
Georg Brandl479a7e72008-02-05 18:13:15 +000011import unittest
Serhiy Storchaka5adfac22016-12-02 08:42:43 +020012import warnings
Benjamin Peterson52c42432012-03-07 18:41:11 -060013import weakref
Tim Peters4d9b4662002-04-16 01:59:17 +000014
Georg Brandl479a7e72008-02-05 18:13:15 +000015from copy import deepcopy
Benjamin Petersonee8712c2008-05-20 21:35:26 +000016from test import support
Guido van Rossum875eeaa2001-10-11 18:33:53 +000017
jdemeyer5a306202018-10-19 23:50:06 +020018try:
19 import _testcapi
20except ImportError:
21 _testcapi = None
22
Tim Peters6d6c1a32001-08-02 04:15:00 +000023
Georg Brandl479a7e72008-02-05 18:13:15 +000024class OperatorsTest(unittest.TestCase):
Tim Peters3caca232001-12-06 06:23:26 +000025
Georg Brandl479a7e72008-02-05 18:13:15 +000026 def __init__(self, *args, **kwargs):
27 unittest.TestCase.__init__(self, *args, **kwargs)
28 self.binops = {
29 'add': '+',
30 'sub': '-',
31 'mul': '*',
Serhiy Storchakac2ccce72015-03-12 22:01:30 +020032 'matmul': '@',
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +020033 'truediv': '/',
34 'floordiv': '//',
Georg Brandl479a7e72008-02-05 18:13:15 +000035 'divmod': 'divmod',
36 'pow': '**',
37 'lshift': '<<',
38 'rshift': '>>',
39 'and': '&',
40 'xor': '^',
41 'or': '|',
42 'cmp': 'cmp',
43 'lt': '<',
44 'le': '<=',
45 'eq': '==',
46 'ne': '!=',
47 'gt': '>',
48 'ge': '>=',
49 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000050
Georg Brandl479a7e72008-02-05 18:13:15 +000051 for name, expr in list(self.binops.items()):
52 if expr.islower():
53 expr = expr + "(a, b)"
54 else:
55 expr = 'a %s b' % expr
56 self.binops[name] = expr
Tim Peters6d6c1a32001-08-02 04:15:00 +000057
Georg Brandl479a7e72008-02-05 18:13:15 +000058 self.unops = {
59 'pos': '+',
60 'neg': '-',
61 'abs': 'abs',
62 'invert': '~',
63 'int': 'int',
64 'float': 'float',
Georg Brandl479a7e72008-02-05 18:13:15 +000065 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000066
Georg Brandl479a7e72008-02-05 18:13:15 +000067 for name, expr in list(self.unops.items()):
68 if expr.islower():
69 expr = expr + "(a)"
70 else:
71 expr = '%s a' % expr
72 self.unops[name] = expr
Tim Peters6d6c1a32001-08-02 04:15:00 +000073
Georg Brandl479a7e72008-02-05 18:13:15 +000074 def unop_test(self, a, res, expr="len(a)", meth="__len__"):
75 d = {'a': a}
76 self.assertEqual(eval(expr, d), res)
77 t = type(a)
78 m = getattr(t, meth)
Tim Peters6d6c1a32001-08-02 04:15:00 +000079
Georg Brandl479a7e72008-02-05 18:13:15 +000080 # Find method in parent class
81 while meth not in t.__dict__:
82 t = t.__bases__[0]
Benjamin Petersone549ead2009-03-28 21:42:05 +000083 # in some implementations (e.g. PyPy), 'm' can be a regular unbound
84 # method object; the getattr() below obtains its underlying function.
85 self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
Georg Brandl479a7e72008-02-05 18:13:15 +000086 self.assertEqual(m(a), res)
87 bm = getattr(a, meth)
88 self.assertEqual(bm(), res)
Tim Peters2f93e282001-10-04 05:27:00 +000089
Georg Brandl479a7e72008-02-05 18:13:15 +000090 def binop_test(self, a, b, res, expr="a+b", meth="__add__"):
91 d = {'a': a, 'b': b}
Tim Peters2f93e282001-10-04 05:27:00 +000092
Georg Brandl479a7e72008-02-05 18:13:15 +000093 self.assertEqual(eval(expr, d), res)
94 t = type(a)
95 m = getattr(t, meth)
96 while meth not in t.__dict__:
97 t = t.__bases__[0]
Benjamin Petersone549ead2009-03-28 21:42:05 +000098 # in some implementations (e.g. PyPy), 'm' can be a regular unbound
99 # method object; the getattr() below obtains its underlying function.
100 self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
Georg Brandl479a7e72008-02-05 18:13:15 +0000101 self.assertEqual(m(a, b), res)
102 bm = getattr(a, meth)
103 self.assertEqual(bm(b), res)
Tim Peters2f93e282001-10-04 05:27:00 +0000104
Georg Brandl479a7e72008-02-05 18:13:15 +0000105 def sliceop_test(self, a, b, c, res, expr="a[b:c]", meth="__getitem__"):
106 d = {'a': a, 'b': b, 'c': c}
107 self.assertEqual(eval(expr, d), res)
108 t = type(a)
109 m = getattr(t, meth)
110 while meth not in t.__dict__:
111 t = t.__bases__[0]
Benjamin Petersone549ead2009-03-28 21:42:05 +0000112 # in some implementations (e.g. PyPy), 'm' can be a regular unbound
113 # method object; the getattr() below obtains its underlying function.
114 self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
Georg Brandl479a7e72008-02-05 18:13:15 +0000115 self.assertEqual(m(a, slice(b, c)), res)
116 bm = getattr(a, meth)
117 self.assertEqual(bm(slice(b, c)), res)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000118
Georg Brandl479a7e72008-02-05 18:13:15 +0000119 def setop_test(self, a, b, res, stmt="a+=b", meth="__iadd__"):
120 d = {'a': deepcopy(a), 'b': b}
121 exec(stmt, d)
122 self.assertEqual(d['a'], res)
123 t = type(a)
124 m = getattr(t, meth)
125 while meth not in t.__dict__:
126 t = t.__bases__[0]
Benjamin Petersone549ead2009-03-28 21:42:05 +0000127 # in some implementations (e.g. PyPy), 'm' can be a regular unbound
128 # method object; the getattr() below obtains its underlying function.
129 self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
Georg Brandl479a7e72008-02-05 18:13:15 +0000130 d['a'] = deepcopy(a)
131 m(d['a'], b)
132 self.assertEqual(d['a'], res)
133 d['a'] = deepcopy(a)
134 bm = getattr(d['a'], meth)
135 bm(b)
136 self.assertEqual(d['a'], res)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000137
Georg Brandl479a7e72008-02-05 18:13:15 +0000138 def set2op_test(self, a, b, c, res, stmt="a[b]=c", meth="__setitem__"):
139 d = {'a': deepcopy(a), 'b': b, 'c': c}
140 exec(stmt, d)
141 self.assertEqual(d['a'], res)
142 t = type(a)
143 m = getattr(t, meth)
144 while meth not in t.__dict__:
145 t = t.__bases__[0]
Benjamin Petersone549ead2009-03-28 21:42:05 +0000146 # in some implementations (e.g. PyPy), 'm' can be a regular unbound
147 # method object; the getattr() below obtains its underlying function.
148 self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
Georg Brandl479a7e72008-02-05 18:13:15 +0000149 d['a'] = deepcopy(a)
150 m(d['a'], b, c)
151 self.assertEqual(d['a'], res)
152 d['a'] = deepcopy(a)
153 bm = getattr(d['a'], meth)
154 bm(b, c)
155 self.assertEqual(d['a'], res)
156
157 def setsliceop_test(self, a, b, c, d, res, stmt="a[b:c]=d", meth="__setitem__"):
158 dictionary = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d}
159 exec(stmt, dictionary)
160 self.assertEqual(dictionary['a'], res)
161 t = type(a)
162 while meth not in t.__dict__:
163 t = t.__bases__[0]
164 m = getattr(t, meth)
Benjamin Petersone549ead2009-03-28 21:42:05 +0000165 # in some implementations (e.g. PyPy), 'm' can be a regular unbound
166 # method object; the getattr() below obtains its underlying function.
167 self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
Georg Brandl479a7e72008-02-05 18:13:15 +0000168 dictionary['a'] = deepcopy(a)
169 m(dictionary['a'], slice(b, c), d)
170 self.assertEqual(dictionary['a'], res)
171 dictionary['a'] = deepcopy(a)
172 bm = getattr(dictionary['a'], meth)
173 bm(slice(b, c), d)
174 self.assertEqual(dictionary['a'], res)
175
176 def test_lists(self):
177 # Testing list operations...
178 # Asserts are within individual test methods
179 self.binop_test([1], [2], [1,2], "a+b", "__add__")
180 self.binop_test([1,2,3], 2, 1, "b in a", "__contains__")
181 self.binop_test([1,2,3], 4, 0, "b in a", "__contains__")
182 self.binop_test([1,2,3], 1, 2, "a[b]", "__getitem__")
183 self.sliceop_test([1,2,3], 0, 2, [1,2], "a[b:c]", "__getitem__")
184 self.setop_test([1], [2], [1,2], "a+=b", "__iadd__")
185 self.setop_test([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__")
186 self.unop_test([1,2,3], 3, "len(a)", "__len__")
187 self.binop_test([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__")
188 self.binop_test([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__")
189 self.set2op_test([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__")
190 self.setsliceop_test([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d",
191 "__setitem__")
192
193 def test_dicts(self):
194 # Testing dict operations...
Georg Brandl479a7e72008-02-05 18:13:15 +0000195 self.binop_test({1:2,3:4}, 1, 1, "b in a", "__contains__")
196 self.binop_test({1:2,3:4}, 2, 0, "b in a", "__contains__")
197 self.binop_test({1:2,3:4}, 1, 2, "a[b]", "__getitem__")
198
199 d = {1:2, 3:4}
200 l1 = []
201 for i in list(d.keys()):
202 l1.append(i)
203 l = []
204 for i in iter(d):
205 l.append(i)
206 self.assertEqual(l, l1)
207 l = []
208 for i in d.__iter__():
209 l.append(i)
210 self.assertEqual(l, l1)
211 l = []
212 for i in dict.__iter__(d):
213 l.append(i)
214 self.assertEqual(l, l1)
215 d = {1:2, 3:4}
216 self.unop_test(d, 2, "len(a)", "__len__")
217 self.assertEqual(eval(repr(d), {}), d)
218 self.assertEqual(eval(d.__repr__(), {}), d)
219 self.set2op_test({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c",
220 "__setitem__")
221
222 # Tests for unary and binary operators
223 def number_operators(self, a, b, skip=[]):
224 dict = {'a': a, 'b': b}
225
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200226 for name, expr in self.binops.items():
Georg Brandl479a7e72008-02-05 18:13:15 +0000227 if name not in skip:
228 name = "__%s__" % name
229 if hasattr(a, name):
230 res = eval(expr, dict)
231 self.binop_test(a, b, res, expr, name)
232
233 for name, expr in list(self.unops.items()):
234 if name not in skip:
235 name = "__%s__" % name
236 if hasattr(a, name):
237 res = eval(expr, dict)
238 self.unop_test(a, res, expr, name)
239
240 def test_ints(self):
241 # Testing int operations...
242 self.number_operators(100, 3)
243 # The following crashes in Python 2.2
244 self.assertEqual((1).__bool__(), 1)
245 self.assertEqual((0).__bool__(), 0)
246 # This returns 'NotImplemented' in Python 2.2
247 class C(int):
248 def __add__(self, other):
249 return NotImplemented
250 self.assertEqual(C(5), 5)
Tim Peters25786c02001-09-02 08:22:48 +0000251 try:
Georg Brandl479a7e72008-02-05 18:13:15 +0000252 C() + ""
Tim Peters25786c02001-09-02 08:22:48 +0000253 except TypeError:
254 pass
255 else:
Georg Brandl479a7e72008-02-05 18:13:15 +0000256 self.fail("NotImplemented should have caused TypeError")
Tim Peters25786c02001-09-02 08:22:48 +0000257
Georg Brandl479a7e72008-02-05 18:13:15 +0000258 def test_floats(self):
259 # Testing float operations...
260 self.number_operators(100.0, 3.0)
Tim Peters25786c02001-09-02 08:22:48 +0000261
Georg Brandl479a7e72008-02-05 18:13:15 +0000262 def test_complexes(self):
263 # Testing complex operations...
264 self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge',
Mark Dickinson5c2db372009-12-05 20:28:34 +0000265 'int', 'float',
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200266 'floordiv', 'divmod', 'mod'])
Tim Peters25786c02001-09-02 08:22:48 +0000267
Georg Brandl479a7e72008-02-05 18:13:15 +0000268 class Number(complex):
269 __slots__ = ['prec']
270 def __new__(cls, *args, **kwds):
271 result = complex.__new__(cls, *args)
272 result.prec = kwds.get('prec', 12)
273 return result
274 def __repr__(self):
275 prec = self.prec
276 if self.imag == 0.0:
277 return "%.*g" % (prec, self.real)
278 if self.real == 0.0:
279 return "%.*gj" % (prec, self.imag)
280 return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag)
281 __str__ = __repr__
Tim Peters25786c02001-09-02 08:22:48 +0000282
Georg Brandl479a7e72008-02-05 18:13:15 +0000283 a = Number(3.14, prec=6)
284 self.assertEqual(repr(a), "3.14")
285 self.assertEqual(a.prec, 6)
Tim Peters1fc240e2001-10-26 05:06:50 +0000286
Georg Brandl479a7e72008-02-05 18:13:15 +0000287 a = Number(a, prec=2)
288 self.assertEqual(repr(a), "3.1")
289 self.assertEqual(a.prec, 2)
Tim Peters1fc240e2001-10-26 05:06:50 +0000290
Georg Brandl479a7e72008-02-05 18:13:15 +0000291 a = Number(234.5)
292 self.assertEqual(repr(a), "234.5")
293 self.assertEqual(a.prec, 12)
Tim Peters1fc240e2001-10-26 05:06:50 +0000294
Mark Dickinsonb09a3d62010-09-23 20:11:19 +0000295 def test_explicit_reverse_methods(self):
296 # see issue 9930
297 self.assertEqual(complex.__radd__(3j, 4.0), complex(4.0, 3.0))
298 self.assertEqual(float.__rsub__(3.0, 1), -2.0)
299
Benjamin Petersone549ead2009-03-28 21:42:05 +0000300 @support.impl_detail("the module 'xxsubtype' is internal")
Georg Brandl479a7e72008-02-05 18:13:15 +0000301 def test_spam_lists(self):
302 # Testing spamlist operations...
303 import copy, xxsubtype as spam
304
305 def spamlist(l, memo=None):
306 import xxsubtype as spam
307 return spam.spamlist(l)
308
309 # This is an ugly hack:
310 copy._deepcopy_dispatch[spam.spamlist] = spamlist
311
312 self.binop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b",
313 "__add__")
314 self.binop_test(spamlist([1,2,3]), 2, 1, "b in a", "__contains__")
315 self.binop_test(spamlist([1,2,3]), 4, 0, "b in a", "__contains__")
316 self.binop_test(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__")
317 self.sliceop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]",
318 "__getitem__")
319 self.setop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b",
320 "__iadd__")
321 self.setop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b",
322 "__imul__")
323 self.unop_test(spamlist([1,2,3]), 3, "len(a)", "__len__")
324 self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b",
325 "__mul__")
326 self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a",
327 "__rmul__")
328 self.set2op_test(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c",
329 "__setitem__")
330 self.setsliceop_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]),
331 spamlist([1,5,6,4]), "a[b:c]=d", "__setitem__")
332 # Test subclassing
333 class C(spam.spamlist):
334 def foo(self): return 1
335 a = C()
336 self.assertEqual(a, [])
337 self.assertEqual(a.foo(), 1)
338 a.append(100)
339 self.assertEqual(a, [100])
340 self.assertEqual(a.getstate(), 0)
341 a.setstate(42)
342 self.assertEqual(a.getstate(), 42)
343
Benjamin Petersone549ead2009-03-28 21:42:05 +0000344 @support.impl_detail("the module 'xxsubtype' is internal")
Georg Brandl479a7e72008-02-05 18:13:15 +0000345 def test_spam_dicts(self):
346 # Testing spamdict operations...
347 import copy, xxsubtype as spam
348 def spamdict(d, memo=None):
349 import xxsubtype as spam
350 sd = spam.spamdict()
351 for k, v in list(d.items()):
352 sd[k] = v
353 return sd
354 # This is an ugly hack:
355 copy._deepcopy_dispatch[spam.spamdict] = spamdict
356
Georg Brandl479a7e72008-02-05 18:13:15 +0000357 self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
358 self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
359 self.binop_test(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__")
360 d = spamdict({1:2,3:4})
361 l1 = []
362 for i in list(d.keys()):
363 l1.append(i)
364 l = []
365 for i in iter(d):
366 l.append(i)
367 self.assertEqual(l, l1)
368 l = []
369 for i in d.__iter__():
370 l.append(i)
371 self.assertEqual(l, l1)
372 l = []
373 for i in type(spamdict({})).__iter__(d):
374 l.append(i)
375 self.assertEqual(l, l1)
376 straightd = {1:2, 3:4}
377 spamd = spamdict(straightd)
378 self.unop_test(spamd, 2, "len(a)", "__len__")
379 self.unop_test(spamd, repr(straightd), "repr(a)", "__repr__")
380 self.set2op_test(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}),
381 "a[b]=c", "__setitem__")
382 # Test subclassing
383 class C(spam.spamdict):
384 def foo(self): return 1
385 a = C()
386 self.assertEqual(list(a.items()), [])
387 self.assertEqual(a.foo(), 1)
388 a['foo'] = 'bar'
389 self.assertEqual(list(a.items()), [('foo', 'bar')])
390 self.assertEqual(a.getstate(), 0)
391 a.setstate(100)
392 self.assertEqual(a.getstate(), 100)
393
Zackery Spytz05f16412019-05-28 06:55:29 -0600394 def test_wrap_lenfunc_bad_cast(self):
395 self.assertEqual(range(sys.maxsize).__len__(), sys.maxsize)
396
397
Georg Brandl479a7e72008-02-05 18:13:15 +0000398class ClassPropertiesAndMethods(unittest.TestCase):
399
Serhiy Storchaka76edd212013-11-17 23:38:50 +0200400 def assertHasAttr(self, obj, name):
401 self.assertTrue(hasattr(obj, name),
402 '%r has no attribute %r' % (obj, name))
403
404 def assertNotHasAttr(self, obj, name):
405 self.assertFalse(hasattr(obj, name),
406 '%r has unexpected attribute %r' % (obj, name))
407
Georg Brandl479a7e72008-02-05 18:13:15 +0000408 def test_python_dicts(self):
409 # Testing Python subclass of dict...
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000410 self.assertTrue(issubclass(dict, dict))
Ezio Melottie9615932010-01-24 19:26:24 +0000411 self.assertIsInstance({}, dict)
Georg Brandl479a7e72008-02-05 18:13:15 +0000412 d = dict()
413 self.assertEqual(d, {})
Serhiy Storchaka76edd212013-11-17 23:38:50 +0200414 self.assertIs(d.__class__, dict)
Ezio Melottie9615932010-01-24 19:26:24 +0000415 self.assertIsInstance(d, dict)
Georg Brandl479a7e72008-02-05 18:13:15 +0000416 class C(dict):
417 state = -1
418 def __init__(self_local, *a, **kw):
419 if a:
420 self.assertEqual(len(a), 1)
421 self_local.state = a[0]
422 if kw:
423 for k, v in list(kw.items()):
424 self_local[v] = k
425 def __getitem__(self, key):
426 return self.get(key, 0)
427 def __setitem__(self_local, key, value):
Ezio Melottie9615932010-01-24 19:26:24 +0000428 self.assertIsInstance(key, type(0))
Georg Brandl479a7e72008-02-05 18:13:15 +0000429 dict.__setitem__(self_local, key, value)
430 def setstate(self, state):
431 self.state = state
432 def getstate(self):
433 return self.state
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000434 self.assertTrue(issubclass(C, dict))
Georg Brandl479a7e72008-02-05 18:13:15 +0000435 a1 = C(12)
436 self.assertEqual(a1.state, 12)
437 a2 = C(foo=1, bar=2)
438 self.assertEqual(a2[1] == 'foo' and a2[2], 'bar')
439 a = C()
440 self.assertEqual(a.state, -1)
441 self.assertEqual(a.getstate(), -1)
442 a.setstate(0)
443 self.assertEqual(a.state, 0)
444 self.assertEqual(a.getstate(), 0)
445 a.setstate(10)
446 self.assertEqual(a.state, 10)
447 self.assertEqual(a.getstate(), 10)
448 self.assertEqual(a[42], 0)
449 a[42] = 24
450 self.assertEqual(a[42], 24)
451 N = 50
452 for i in range(N):
453 a[i] = C()
454 for j in range(N):
455 a[i][j] = i*j
456 for i in range(N):
457 for j in range(N):
458 self.assertEqual(a[i][j], i*j)
459
460 def test_python_lists(self):
461 # Testing Python subclass of list...
462 class C(list):
463 def __getitem__(self, i):
464 if isinstance(i, slice):
465 return i.start, i.stop
466 return list.__getitem__(self, i) + 100
467 a = C()
468 a.extend([0,1,2])
469 self.assertEqual(a[0], 100)
470 self.assertEqual(a[1], 101)
471 self.assertEqual(a[2], 102)
472 self.assertEqual(a[100:200], (100,200))
473
474 def test_metaclass(self):
Georg Brandle81f5ef2008-05-27 20:34:09 +0000475 # Testing metaclasses...
Georg Brandl479a7e72008-02-05 18:13:15 +0000476 class C(metaclass=type):
477 def __init__(self):
478 self.__state = 0
479 def getstate(self):
480 return self.__state
481 def setstate(self, state):
482 self.__state = state
483 a = C()
484 self.assertEqual(a.getstate(), 0)
485 a.setstate(10)
486 self.assertEqual(a.getstate(), 10)
487 class _metaclass(type):
488 def myself(cls): return cls
489 class D(metaclass=_metaclass):
490 pass
491 self.assertEqual(D.myself(), D)
492 d = D()
493 self.assertEqual(d.__class__, D)
494 class M1(type):
495 def __new__(cls, name, bases, dict):
496 dict['__spam__'] = 1
497 return type.__new__(cls, name, bases, dict)
498 class C(metaclass=M1):
499 pass
500 self.assertEqual(C.__spam__, 1)
501 c = C()
502 self.assertEqual(c.__spam__, 1)
503
504 class _instance(object):
505 pass
506 class M2(object):
507 @staticmethod
508 def __new__(cls, name, bases, dict):
509 self = object.__new__(cls)
510 self.name = name
511 self.bases = bases
512 self.dict = dict
513 return self
514 def __call__(self):
515 it = _instance()
516 # Early binding of methods
517 for key in self.dict:
518 if key.startswith("__"):
519 continue
520 setattr(it, key, self.dict[key].__get__(it, self))
521 return it
522 class C(metaclass=M2):
523 def spam(self):
524 return 42
525 self.assertEqual(C.name, 'C')
526 self.assertEqual(C.bases, ())
Benjamin Peterson577473f2010-01-19 00:09:57 +0000527 self.assertIn('spam', C.dict)
Georg Brandl479a7e72008-02-05 18:13:15 +0000528 c = C()
529 self.assertEqual(c.spam(), 42)
530
531 # More metaclass examples
532
533 class autosuper(type):
534 # Automatically add __super to the class
535 # This trick only works for dynamic classes
536 def __new__(metaclass, name, bases, dict):
537 cls = super(autosuper, metaclass).__new__(metaclass,
538 name, bases, dict)
539 # Name mangling for __super removes leading underscores
540 while name[:1] == "_":
541 name = name[1:]
542 if name:
543 name = "_%s__super" % name
544 else:
545 name = "__super"
546 setattr(cls, name, super(cls))
547 return cls
548 class A(metaclass=autosuper):
549 def meth(self):
550 return "A"
551 class B(A):
552 def meth(self):
553 return "B" + self.__super.meth()
554 class C(A):
555 def meth(self):
556 return "C" + self.__super.meth()
557 class D(C, B):
558 def meth(self):
559 return "D" + self.__super.meth()
560 self.assertEqual(D().meth(), "DCBA")
561 class E(B, C):
562 def meth(self):
563 return "E" + self.__super.meth()
564 self.assertEqual(E().meth(), "EBCA")
565
566 class autoproperty(type):
567 # Automatically create property attributes when methods
568 # named _get_x and/or _set_x are found
569 def __new__(metaclass, name, bases, dict):
570 hits = {}
571 for key, val in dict.items():
572 if key.startswith("_get_"):
573 key = key[5:]
574 get, set = hits.get(key, (None, None))
575 get = val
576 hits[key] = get, set
577 elif key.startswith("_set_"):
578 key = key[5:]
579 get, set = hits.get(key, (None, None))
580 set = val
581 hits[key] = get, set
582 for key, (get, set) in hits.items():
583 dict[key] = property(get, set)
584 return super(autoproperty, metaclass).__new__(metaclass,
585 name, bases, dict)
586 class A(metaclass=autoproperty):
587 def _get_x(self):
588 return -self.__x
589 def _set_x(self, x):
590 self.__x = -x
591 a = A()
Serhiy Storchaka76edd212013-11-17 23:38:50 +0200592 self.assertNotHasAttr(a, "x")
Georg Brandl479a7e72008-02-05 18:13:15 +0000593 a.x = 12
594 self.assertEqual(a.x, 12)
595 self.assertEqual(a._A__x, -12)
596
597 class multimetaclass(autoproperty, autosuper):
598 # Merge of multiple cooperating metaclasses
599 pass
600 class A(metaclass=multimetaclass):
601 def _get_x(self):
602 return "A"
603 class B(A):
604 def _get_x(self):
605 return "B" + self.__super._get_x()
606 class C(A):
607 def _get_x(self):
608 return "C" + self.__super._get_x()
609 class D(C, B):
610 def _get_x(self):
611 return "D" + self.__super._get_x()
612 self.assertEqual(D().x, "DCBA")
613
614 # Make sure type(x) doesn't call x.__class__.__init__
615 class T(type):
616 counter = 0
617 def __init__(self, *args):
618 T.counter += 1
619 class C(metaclass=T):
620 pass
621 self.assertEqual(T.counter, 1)
622 a = C()
623 self.assertEqual(type(a), C)
624 self.assertEqual(T.counter, 1)
625
626 class C(object): pass
627 c = C()
628 try: c()
629 except TypeError: pass
630 else: self.fail("calling object w/o call method should raise "
631 "TypeError")
632
633 # Testing code to find most derived baseclass
634 class A(type):
635 def __new__(*args, **kwargs):
636 return type.__new__(*args, **kwargs)
637
638 class B(object):
639 pass
640
641 class C(object, metaclass=A):
642 pass
643
644 # The most derived metaclass of D is A rather than type.
645 class D(B, C):
646 pass
Nick Coghlande31b192011-10-23 22:04:16 +1000647 self.assertIs(A, type(D))
648
649 # issue1294232: correct metaclass calculation
650 new_calls = [] # to check the order of __new__ calls
651 class AMeta(type):
652 @staticmethod
653 def __new__(mcls, name, bases, ns):
654 new_calls.append('AMeta')
655 return super().__new__(mcls, name, bases, ns)
656 @classmethod
657 def __prepare__(mcls, name, bases):
658 return {}
659
660 class BMeta(AMeta):
661 @staticmethod
662 def __new__(mcls, name, bases, ns):
663 new_calls.append('BMeta')
664 return super().__new__(mcls, name, bases, ns)
665 @classmethod
666 def __prepare__(mcls, name, bases):
667 ns = super().__prepare__(name, bases)
668 ns['BMeta_was_here'] = True
669 return ns
670
671 class A(metaclass=AMeta):
672 pass
673 self.assertEqual(['AMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000674 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000675
676 class B(metaclass=BMeta):
677 pass
678 # BMeta.__new__ calls AMeta.__new__ with super:
679 self.assertEqual(['BMeta', 'AMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000680 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000681
682 class C(A, B):
683 pass
684 # The most derived metaclass is BMeta:
685 self.assertEqual(['BMeta', 'AMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000686 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000687 # BMeta.__prepare__ should've been called:
688 self.assertIn('BMeta_was_here', C.__dict__)
689
690 # The order of the bases shouldn't matter:
691 class C2(B, A):
692 pass
693 self.assertEqual(['BMeta', 'AMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000694 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000695 self.assertIn('BMeta_was_here', C2.__dict__)
696
697 # Check correct metaclass calculation when a metaclass is declared:
698 class D(C, metaclass=type):
699 pass
700 self.assertEqual(['BMeta', 'AMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000701 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000702 self.assertIn('BMeta_was_here', D.__dict__)
703
704 class E(C, metaclass=AMeta):
705 pass
706 self.assertEqual(['BMeta', 'AMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000707 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000708 self.assertIn('BMeta_was_here', E.__dict__)
709
710 # Special case: the given metaclass isn't a class,
711 # so there is no metaclass calculation.
712 marker = object()
713 def func(*args, **kwargs):
714 return marker
715 class X(metaclass=func):
716 pass
717 class Y(object, metaclass=func):
718 pass
719 class Z(D, metaclass=func):
720 pass
721 self.assertIs(marker, X)
722 self.assertIs(marker, Y)
723 self.assertIs(marker, Z)
724
725 # The given metaclass is a class,
726 # but not a descendant of type.
727 prepare_calls = [] # to track __prepare__ calls
728 class ANotMeta:
729 def __new__(mcls, *args, **kwargs):
730 new_calls.append('ANotMeta')
731 return super().__new__(mcls)
732 @classmethod
733 def __prepare__(mcls, name, bases):
734 prepare_calls.append('ANotMeta')
735 return {}
736 class BNotMeta(ANotMeta):
737 def __new__(mcls, *args, **kwargs):
738 new_calls.append('BNotMeta')
739 return super().__new__(mcls)
740 @classmethod
741 def __prepare__(mcls, name, bases):
742 prepare_calls.append('BNotMeta')
743 return super().__prepare__(name, bases)
744
745 class A(metaclass=ANotMeta):
746 pass
747 self.assertIs(ANotMeta, type(A))
748 self.assertEqual(['ANotMeta'], prepare_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000749 prepare_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000750 self.assertEqual(['ANotMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000751 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000752
753 class B(metaclass=BNotMeta):
754 pass
755 self.assertIs(BNotMeta, type(B))
756 self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000757 prepare_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000758 self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000759 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000760
761 class C(A, B):
762 pass
763 self.assertIs(BNotMeta, type(C))
764 self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000765 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000766 self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000767 prepare_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000768
769 class C2(B, A):
770 pass
771 self.assertIs(BNotMeta, type(C2))
772 self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000773 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000774 self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000775 prepare_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000776
777 # This is a TypeError, because of a metaclass conflict:
778 # BNotMeta is neither a subclass, nor a superclass of type
779 with self.assertRaises(TypeError):
780 class D(C, metaclass=type):
781 pass
782
783 class E(C, metaclass=ANotMeta):
784 pass
785 self.assertIs(BNotMeta, type(E))
786 self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000787 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000788 self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000789 prepare_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000790
791 class F(object(), C):
792 pass
793 self.assertIs(BNotMeta, type(F))
794 self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000795 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000796 self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000797 prepare_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000798
799 class F2(C, object()):
800 pass
801 self.assertIs(BNotMeta, type(F2))
802 self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000803 new_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000804 self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
Nick Coghlan9715d262011-10-23 22:36:42 +1000805 prepare_calls.clear()
Nick Coghlande31b192011-10-23 22:04:16 +1000806
807 # TypeError: BNotMeta is neither a
808 # subclass, nor a superclass of int
809 with self.assertRaises(TypeError):
810 class X(C, int()):
811 pass
812 with self.assertRaises(TypeError):
813 class X(int(), C):
814 pass
Georg Brandl479a7e72008-02-05 18:13:15 +0000815
816 def test_module_subclasses(self):
817 # Testing Python subclass of module...
818 log = []
Georg Brandl479a7e72008-02-05 18:13:15 +0000819 MT = type(sys)
820 class MM(MT):
821 def __init__(self, name):
822 MT.__init__(self, name)
823 def __getattribute__(self, name):
824 log.append(("getattr", name))
825 return MT.__getattribute__(self, name)
826 def __setattr__(self, name, value):
827 log.append(("setattr", name, value))
828 MT.__setattr__(self, name, value)
829 def __delattr__(self, name):
830 log.append(("delattr", name))
831 MT.__delattr__(self, name)
832 a = MM("a")
833 a.foo = 12
834 x = a.foo
835 del a.foo
836 self.assertEqual(log, [("setattr", "foo", 12),
837 ("getattr", "foo"),
838 ("delattr", "foo")])
839
840 # http://python.org/sf/1174712
Tim Peters1fc240e2001-10-26 05:06:50 +0000841 try:
Georg Brandl479a7e72008-02-05 18:13:15 +0000842 class Module(types.ModuleType, str):
843 pass
844 except TypeError:
Tim Peters1fc240e2001-10-26 05:06:50 +0000845 pass
846 else:
Georg Brandl479a7e72008-02-05 18:13:15 +0000847 self.fail("inheriting from ModuleType and str at the same time "
848 "should fail")
Tim Peters1fc240e2001-10-26 05:06:50 +0000849
Raymond Hettinger51f46882020-12-18 16:53:50 -0800850 # Issue 34805: Verify that definition order is retained
851 def random_name():
852 return ''.join(random.choices(string.ascii_letters, k=10))
853 class A:
854 pass
855 subclasses = [type(random_name(), (A,), {}) for i in range(100)]
856 self.assertEqual(A.__subclasses__(), subclasses)
857
Georg Brandl479a7e72008-02-05 18:13:15 +0000858 def test_multiple_inheritance(self):
859 # Testing multiple inheritance...
860 class C(object):
861 def __init__(self):
862 self.__state = 0
863 def getstate(self):
864 return self.__state
865 def setstate(self, state):
866 self.__state = state
867 a = C()
868 self.assertEqual(a.getstate(), 0)
869 a.setstate(10)
870 self.assertEqual(a.getstate(), 10)
871 class D(dict, C):
872 def __init__(self):
873 type({}).__init__(self)
874 C.__init__(self)
875 d = D()
876 self.assertEqual(list(d.keys()), [])
877 d["hello"] = "world"
878 self.assertEqual(list(d.items()), [("hello", "world")])
879 self.assertEqual(d["hello"], "world")
880 self.assertEqual(d.getstate(), 0)
881 d.setstate(10)
882 self.assertEqual(d.getstate(), 10)
883 self.assertEqual(D.__mro__, (D, dict, C, object))
Tim Peters5d2b77c2001-09-03 05:47:38 +0000884
Georg Brandl479a7e72008-02-05 18:13:15 +0000885 # SF bug #442833
886 class Node(object):
887 def __int__(self):
888 return int(self.foo())
889 def foo(self):
890 return "23"
891 class Frag(Node, list):
892 def foo(self):
893 return "42"
894 self.assertEqual(Node().__int__(), 23)
895 self.assertEqual(int(Node()), 23)
896 self.assertEqual(Frag().__int__(), 42)
897 self.assertEqual(int(Frag()), 42)
Tim Peters5d2b77c2001-09-03 05:47:38 +0000898
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700899 def test_diamond_inheritance(self):
Georg Brandl479a7e72008-02-05 18:13:15 +0000900 # Testing multiple inheritance special cases...
901 class A(object):
902 def spam(self): return "A"
903 self.assertEqual(A().spam(), "A")
904 class B(A):
905 def boo(self): return "B"
906 def spam(self): return "B"
907 self.assertEqual(B().spam(), "B")
908 self.assertEqual(B().boo(), "B")
909 class C(A):
910 def boo(self): return "C"
911 self.assertEqual(C().spam(), "A")
912 self.assertEqual(C().boo(), "C")
913 class D(B, C): pass
914 self.assertEqual(D().spam(), "B")
915 self.assertEqual(D().boo(), "B")
916 self.assertEqual(D.__mro__, (D, B, C, A, object))
917 class E(C, B): pass
918 self.assertEqual(E().spam(), "B")
919 self.assertEqual(E().boo(), "C")
920 self.assertEqual(E.__mro__, (E, C, B, A, object))
921 # MRO order disagreement
922 try:
923 class F(D, E): pass
924 except TypeError:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000925 pass
Georg Brandl479a7e72008-02-05 18:13:15 +0000926 else:
927 self.fail("expected MRO order disagreement (F)")
928 try:
929 class G(E, D): pass
930 except TypeError:
931 pass
932 else:
933 self.fail("expected MRO order disagreement (G)")
Guido van Rossum360e4b82007-05-14 22:51:27 +0000934
Georg Brandl479a7e72008-02-05 18:13:15 +0000935 # see thread python-dev/2002-October/029035.html
936 def test_ex5_from_c3_switch(self):
937 # Testing ex5 from C3 switch discussion...
938 class A(object): pass
939 class B(object): pass
940 class C(object): pass
941 class X(A): pass
942 class Y(A): pass
943 class Z(X,B,Y,C): pass
944 self.assertEqual(Z.__mro__, (Z, X, B, Y, A, C, object))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945
Georg Brandl479a7e72008-02-05 18:13:15 +0000946 # see "A Monotonic Superclass Linearization for Dylan",
947 # by Kim Barrett et al. (OOPSLA 1996)
948 def test_monotonicity(self):
949 # Testing MRO monotonicity...
950 class Boat(object): pass
951 class DayBoat(Boat): pass
952 class WheelBoat(Boat): pass
953 class EngineLess(DayBoat): pass
954 class SmallMultihull(DayBoat): pass
955 class PedalWheelBoat(EngineLess,WheelBoat): pass
956 class SmallCatamaran(SmallMultihull): pass
957 class Pedalo(PedalWheelBoat,SmallCatamaran): pass
Guido van Rossume45763a2001-08-10 21:28:46 +0000958
Georg Brandl479a7e72008-02-05 18:13:15 +0000959 self.assertEqual(PedalWheelBoat.__mro__,
960 (PedalWheelBoat, EngineLess, DayBoat, WheelBoat, Boat, object))
961 self.assertEqual(SmallCatamaran.__mro__,
962 (SmallCatamaran, SmallMultihull, DayBoat, Boat, object))
963 self.assertEqual(Pedalo.__mro__,
964 (Pedalo, PedalWheelBoat, EngineLess, SmallCatamaran,
965 SmallMultihull, DayBoat, WheelBoat, Boat, object))
Guido van Rossum9a818922002-11-14 19:50:14 +0000966
Georg Brandl479a7e72008-02-05 18:13:15 +0000967 # see "A Monotonic Superclass Linearization for Dylan",
968 # by Kim Barrett et al. (OOPSLA 1996)
969 def test_consistency_with_epg(self):
Ezio Melotti42da6632011-03-15 05:18:48 +0200970 # Testing consistency with EPG...
Georg Brandl479a7e72008-02-05 18:13:15 +0000971 class Pane(object): pass
972 class ScrollingMixin(object): pass
973 class EditingMixin(object): pass
974 class ScrollablePane(Pane,ScrollingMixin): pass
975 class EditablePane(Pane,EditingMixin): pass
976 class EditableScrollablePane(ScrollablePane,EditablePane): pass
Guido van Rossum9a818922002-11-14 19:50:14 +0000977
Georg Brandl479a7e72008-02-05 18:13:15 +0000978 self.assertEqual(EditableScrollablePane.__mro__,
979 (EditableScrollablePane, ScrollablePane, EditablePane, Pane,
980 ScrollingMixin, EditingMixin, object))
Guido van Rossum9a818922002-11-14 19:50:14 +0000981
Georg Brandl479a7e72008-02-05 18:13:15 +0000982 def test_mro_disagreement(self):
983 # Testing error messages for MRO disagreement...
984 mro_err_msg = """Cannot create a consistent method resolution
Raymond Hettingerf394df42003-04-06 19:13:41 +0000985order (MRO) for bases """
Raymond Hettinger83245b52003-03-12 04:25:42 +0000986
Georg Brandl479a7e72008-02-05 18:13:15 +0000987 def raises(exc, expected, callable, *args):
Guido van Rossum58da9312007-11-10 23:39:45 +0000988 try:
Georg Brandl479a7e72008-02-05 18:13:15 +0000989 callable(*args)
990 except exc as msg:
Benjamin Petersone549ead2009-03-28 21:42:05 +0000991 # the exact msg is generally considered an impl detail
992 if support.check_impl_detail():
993 if not str(msg).startswith(expected):
994 self.fail("Message %r, expected %r" %
995 (str(msg), expected))
Georg Brandl479a7e72008-02-05 18:13:15 +0000996 else:
997 self.fail("Expected %s" % exc)
Guido van Rossum58da9312007-11-10 23:39:45 +0000998
Georg Brandl479a7e72008-02-05 18:13:15 +0000999 class A(object): pass
1000 class B(A): pass
1001 class C(object): pass
Christian Heimes9a371592007-12-28 14:08:13 +00001002
Georg Brandl479a7e72008-02-05 18:13:15 +00001003 # Test some very simple errors
1004 raises(TypeError, "duplicate base class A",
1005 type, "X", (A, A), {})
1006 raises(TypeError, mro_err_msg,
1007 type, "X", (A, B), {})
1008 raises(TypeError, mro_err_msg,
1009 type, "X", (A, C, B), {})
1010 # Test a slightly more complex error
1011 class GridLayout(object): pass
1012 class HorizontalGrid(GridLayout): pass
1013 class VerticalGrid(GridLayout): pass
1014 class HVGrid(HorizontalGrid, VerticalGrid): pass
1015 class VHGrid(VerticalGrid, HorizontalGrid): pass
1016 raises(TypeError, mro_err_msg,
1017 type, "ConfusedGrid", (HVGrid, VHGrid), {})
Guido van Rossum58da9312007-11-10 23:39:45 +00001018
Georg Brandl479a7e72008-02-05 18:13:15 +00001019 def test_object_class(self):
1020 # Testing object class...
1021 a = object()
1022 self.assertEqual(a.__class__, object)
1023 self.assertEqual(type(a), object)
1024 b = object()
1025 self.assertNotEqual(a, b)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001026 self.assertNotHasAttr(a, "foo")
Tim Peters808b94e2001-09-13 19:33:07 +00001027 try:
Georg Brandl479a7e72008-02-05 18:13:15 +00001028 a.foo = 12
1029 except (AttributeError, TypeError):
Tim Peters808b94e2001-09-13 19:33:07 +00001030 pass
1031 else:
Georg Brandl479a7e72008-02-05 18:13:15 +00001032 self.fail("object() should not allow setting a foo attribute")
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001033 self.assertNotHasAttr(object(), "__dict__")
Tim Peters561f8992001-09-13 19:36:36 +00001034
Georg Brandl479a7e72008-02-05 18:13:15 +00001035 class Cdict(object):
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001036 pass
Georg Brandl479a7e72008-02-05 18:13:15 +00001037 x = Cdict()
1038 self.assertEqual(x.__dict__, {})
1039 x.foo = 1
1040 self.assertEqual(x.foo, 1)
1041 self.assertEqual(x.__dict__, {'foo': 1})
Guido van Rossumd8faa362007-04-27 19:54:29 +00001042
Benjamin Peterson9d4cbcc2015-01-30 13:33:42 -05001043 def test_object_class_assignment_between_heaptypes_and_nonheaptypes(self):
1044 class SubType(types.ModuleType):
1045 a = 1
1046
1047 m = types.ModuleType("m")
1048 self.assertTrue(m.__class__ is types.ModuleType)
1049 self.assertFalse(hasattr(m, "a"))
1050
1051 m.__class__ = SubType
1052 self.assertTrue(m.__class__ is SubType)
1053 self.assertTrue(hasattr(m, "a"))
1054
1055 m.__class__ = types.ModuleType
1056 self.assertTrue(m.__class__ is types.ModuleType)
1057 self.assertFalse(hasattr(m, "a"))
1058
Guido van Rossum7d293ee2015-09-04 20:54:07 -07001059 # Make sure that builtin immutable objects don't support __class__
1060 # assignment, because the object instances may be interned.
1061 # We set __slots__ = () to ensure that the subclasses are
1062 # memory-layout compatible, and thus otherwise reasonable candidates
1063 # for __class__ assignment.
1064
1065 # The following types have immutable instances, but are not
1066 # subclassable and thus don't need to be checked:
1067 # NoneType, bool
1068
1069 class MyInt(int):
1070 __slots__ = ()
1071 with self.assertRaises(TypeError):
1072 (1).__class__ = MyInt
1073
1074 class MyFloat(float):
1075 __slots__ = ()
1076 with self.assertRaises(TypeError):
1077 (1.0).__class__ = MyFloat
1078
1079 class MyComplex(complex):
1080 __slots__ = ()
1081 with self.assertRaises(TypeError):
1082 (1 + 2j).__class__ = MyComplex
1083
1084 class MyStr(str):
1085 __slots__ = ()
1086 with self.assertRaises(TypeError):
1087 "a".__class__ = MyStr
1088
1089 class MyBytes(bytes):
1090 __slots__ = ()
1091 with self.assertRaises(TypeError):
1092 b"a".__class__ = MyBytes
1093
1094 class MyTuple(tuple):
1095 __slots__ = ()
1096 with self.assertRaises(TypeError):
1097 ().__class__ = MyTuple
1098
1099 class MyFrozenSet(frozenset):
1100 __slots__ = ()
1101 with self.assertRaises(TypeError):
1102 frozenset().__class__ = MyFrozenSet
1103
Georg Brandl479a7e72008-02-05 18:13:15 +00001104 def test_slots(self):
1105 # Testing __slots__...
1106 class C0(object):
1107 __slots__ = []
1108 x = C0()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001109 self.assertNotHasAttr(x, "__dict__")
1110 self.assertNotHasAttr(x, "foo")
Georg Brandl479a7e72008-02-05 18:13:15 +00001111
1112 class C1(object):
1113 __slots__ = ['a']
1114 x = C1()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001115 self.assertNotHasAttr(x, "__dict__")
1116 self.assertNotHasAttr(x, "a")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001117 x.a = 1
Georg Brandl479a7e72008-02-05 18:13:15 +00001118 self.assertEqual(x.a, 1)
1119 x.a = None
1120 self.assertEqual(x.a, None)
1121 del x.a
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001122 self.assertNotHasAttr(x, "a")
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001123
Georg Brandl479a7e72008-02-05 18:13:15 +00001124 class C3(object):
1125 __slots__ = ['a', 'b', 'c']
1126 x = C3()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001127 self.assertNotHasAttr(x, "__dict__")
1128 self.assertNotHasAttr(x, 'a')
1129 self.assertNotHasAttr(x, 'b')
1130 self.assertNotHasAttr(x, 'c')
Georg Brandl479a7e72008-02-05 18:13:15 +00001131 x.a = 1
1132 x.b = 2
1133 x.c = 3
1134 self.assertEqual(x.a, 1)
1135 self.assertEqual(x.b, 2)
1136 self.assertEqual(x.c, 3)
1137
1138 class C4(object):
1139 """Validate name mangling"""
1140 __slots__ = ['__a']
1141 def __init__(self, value):
1142 self.__a = value
1143 def get(self):
1144 return self.__a
1145 x = C4(5)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001146 self.assertNotHasAttr(x, '__dict__')
1147 self.assertNotHasAttr(x, '__a')
Georg Brandl479a7e72008-02-05 18:13:15 +00001148 self.assertEqual(x.get(), 5)
Guido van Rossum6661be32001-10-26 04:26:12 +00001149 try:
Georg Brandl479a7e72008-02-05 18:13:15 +00001150 x.__a = 6
1151 except AttributeError:
Guido van Rossum6661be32001-10-26 04:26:12 +00001152 pass
1153 else:
Georg Brandl479a7e72008-02-05 18:13:15 +00001154 self.fail("Double underscored names not mangled")
Guido van Rossum360e4b82007-05-14 22:51:27 +00001155
Georg Brandl479a7e72008-02-05 18:13:15 +00001156 # Make sure slot names are proper identifiers
Guido van Rossum360e4b82007-05-14 22:51:27 +00001157 try:
Georg Brandl479a7e72008-02-05 18:13:15 +00001158 class C(object):
1159 __slots__ = [None]
Guido van Rossum360e4b82007-05-14 22:51:27 +00001160 except TypeError:
1161 pass
1162 else:
Georg Brandl479a7e72008-02-05 18:13:15 +00001163 self.fail("[None] slots not caught")
Guido van Rossum360e4b82007-05-14 22:51:27 +00001164 try:
Georg Brandl479a7e72008-02-05 18:13:15 +00001165 class C(object):
1166 __slots__ = ["foo bar"]
1167 except TypeError:
Guido van Rossum360e4b82007-05-14 22:51:27 +00001168 pass
1169 else:
Georg Brandl479a7e72008-02-05 18:13:15 +00001170 self.fail("['foo bar'] slots not caught")
1171 try:
1172 class C(object):
1173 __slots__ = ["foo\0bar"]
1174 except TypeError:
1175 pass
1176 else:
1177 self.fail("['foo\\0bar'] slots not caught")
1178 try:
1179 class C(object):
1180 __slots__ = ["1"]
1181 except TypeError:
1182 pass
1183 else:
1184 self.fail("['1'] slots not caught")
1185 try:
1186 class C(object):
1187 __slots__ = [""]
1188 except TypeError:
1189 pass
1190 else:
1191 self.fail("[''] slots not caught")
1192 class C(object):
1193 __slots__ = ["a", "a_b", "_a", "A0123456789Z"]
1194 # XXX(nnorwitz): was there supposed to be something tested
1195 # from the class above?
Guido van Rossum360e4b82007-05-14 22:51:27 +00001196
Georg Brandl479a7e72008-02-05 18:13:15 +00001197 # Test a single string is not expanded as a sequence.
1198 class C(object):
1199 __slots__ = "abc"
1200 c = C()
1201 c.abc = 5
1202 self.assertEqual(c.abc, 5)
Guido van Rossum6661be32001-10-26 04:26:12 +00001203
Georg Brandl479a7e72008-02-05 18:13:15 +00001204 # Test unicode slot names
1205 # Test a single unicode string is not expanded as a sequence.
1206 class C(object):
1207 __slots__ = "abc"
1208 c = C()
1209 c.abc = 5
1210 self.assertEqual(c.abc, 5)
Guido van Rossum3926a632001-09-25 16:25:58 +00001211
Georg Brandl479a7e72008-02-05 18:13:15 +00001212 # _unicode_to_string used to modify slots in certain circumstances
1213 slots = ("foo", "bar")
1214 class C(object):
1215 __slots__ = slots
1216 x = C()
1217 x.foo = 5
1218 self.assertEqual(x.foo, 5)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001219 self.assertIs(type(slots[0]), str)
Georg Brandl479a7e72008-02-05 18:13:15 +00001220 # this used to leak references
1221 try:
1222 class C(object):
1223 __slots__ = [chr(128)]
1224 except (TypeError, UnicodeEncodeError):
1225 pass
1226 else:
Terry Jan Reedyaf9eb962014-06-20 15:16:35 -04001227 self.fail("[chr(128)] slots not caught")
Guido van Rossum3926a632001-09-25 16:25:58 +00001228
Georg Brandl479a7e72008-02-05 18:13:15 +00001229 # Test leaks
1230 class Counted(object):
1231 counter = 0 # counts the number of instances alive
1232 def __init__(self):
1233 Counted.counter += 1
1234 def __del__(self):
1235 Counted.counter -= 1
1236 class C(object):
1237 __slots__ = ['a', 'b', 'c']
1238 x = C()
1239 x.a = Counted()
1240 x.b = Counted()
1241 x.c = Counted()
1242 self.assertEqual(Counted.counter, 3)
1243 del x
Benjamin Petersone549ead2009-03-28 21:42:05 +00001244 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00001245 self.assertEqual(Counted.counter, 0)
1246 class D(C):
1247 pass
1248 x = D()
1249 x.a = Counted()
1250 x.z = Counted()
1251 self.assertEqual(Counted.counter, 2)
1252 del x
Benjamin Petersone549ead2009-03-28 21:42:05 +00001253 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00001254 self.assertEqual(Counted.counter, 0)
1255 class E(D):
1256 __slots__ = ['e']
1257 x = E()
1258 x.a = Counted()
1259 x.z = Counted()
1260 x.e = Counted()
1261 self.assertEqual(Counted.counter, 3)
1262 del x
Benjamin Petersone549ead2009-03-28 21:42:05 +00001263 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00001264 self.assertEqual(Counted.counter, 0)
Guido van Rossum3926a632001-09-25 16:25:58 +00001265
Georg Brandl479a7e72008-02-05 18:13:15 +00001266 # Test cyclical leaks [SF bug 519621]
1267 class F(object):
1268 __slots__ = ['a', 'b']
Georg Brandl479a7e72008-02-05 18:13:15 +00001269 s = F()
1270 s.a = [Counted(), s]
1271 self.assertEqual(Counted.counter, 1)
1272 s = None
Benjamin Petersone549ead2009-03-28 21:42:05 +00001273 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00001274 self.assertEqual(Counted.counter, 0)
Guido van Rossum3926a632001-09-25 16:25:58 +00001275
Georg Brandl479a7e72008-02-05 18:13:15 +00001276 # Test lookup leaks [SF bug 572567]
Benjamin Petersone549ead2009-03-28 21:42:05 +00001277 if hasattr(gc, 'get_objects'):
1278 class G(object):
Benjamin Petersona8b976b2009-10-11 18:28:48 +00001279 def __eq__(self, other):
1280 return False
Benjamin Petersone549ead2009-03-28 21:42:05 +00001281 g = G()
1282 orig_objects = len(gc.get_objects())
1283 for i in range(10):
1284 g==g
1285 new_objects = len(gc.get_objects())
1286 self.assertEqual(orig_objects, new_objects)
1287
Georg Brandl479a7e72008-02-05 18:13:15 +00001288 class H(object):
1289 __slots__ = ['a', 'b']
1290 def __init__(self):
1291 self.a = 1
1292 self.b = 2
1293 def __del__(self_):
1294 self.assertEqual(self_.a, 1)
1295 self.assertEqual(self_.b, 2)
Benjamin Petersonc1de4cc2008-11-03 21:29:09 +00001296 with support.captured_output('stderr') as s:
Benjamin Petersonc0747cf2008-11-03 20:31:38 +00001297 h = H()
Georg Brandl479a7e72008-02-05 18:13:15 +00001298 del h
Benjamin Petersonc0747cf2008-11-03 20:31:38 +00001299 self.assertEqual(s.getvalue(), '')
Guido van Rossum90c45142001-11-24 21:07:01 +00001300
Benjamin Petersond12362a2009-12-30 19:44:54 +00001301 class X(object):
1302 __slots__ = "a"
1303 with self.assertRaises(AttributeError):
1304 del X().a
1305
Miss Islington (bot)efda9052021-07-16 17:01:53 -07001306 # Inherit from object on purpose to check some backwards compatibility paths
1307 class X(object):
1308 __slots__ = "a"
1309 with self.assertRaisesRegex(AttributeError, "'X' object has no attribute 'a'"):
1310 X().a
1311
Georg Brandl479a7e72008-02-05 18:13:15 +00001312 def test_slots_special(self):
1313 # Testing __dict__ and __weakref__ in __slots__...
1314 class D(object):
1315 __slots__ = ["__dict__"]
1316 a = D()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001317 self.assertHasAttr(a, "__dict__")
1318 self.assertNotHasAttr(a, "__weakref__")
Georg Brandl479a7e72008-02-05 18:13:15 +00001319 a.foo = 42
1320 self.assertEqual(a.__dict__, {"foo": 42})
Guido van Rossum90c45142001-11-24 21:07:01 +00001321
Georg Brandl479a7e72008-02-05 18:13:15 +00001322 class W(object):
1323 __slots__ = ["__weakref__"]
1324 a = W()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001325 self.assertHasAttr(a, "__weakref__")
1326 self.assertNotHasAttr(a, "__dict__")
Georg Brandl479a7e72008-02-05 18:13:15 +00001327 try:
1328 a.foo = 42
1329 except AttributeError:
1330 pass
1331 else:
1332 self.fail("shouldn't be allowed to set a.foo")
1333
1334 class C1(W, D):
1335 __slots__ = []
1336 a = C1()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001337 self.assertHasAttr(a, "__dict__")
1338 self.assertHasAttr(a, "__weakref__")
Georg Brandl479a7e72008-02-05 18:13:15 +00001339 a.foo = 42
1340 self.assertEqual(a.__dict__, {"foo": 42})
1341
1342 class C2(D, W):
1343 __slots__ = []
1344 a = C2()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001345 self.assertHasAttr(a, "__dict__")
1346 self.assertHasAttr(a, "__weakref__")
Georg Brandl479a7e72008-02-05 18:13:15 +00001347 a.foo = 42
1348 self.assertEqual(a.__dict__, {"foo": 42})
1349
Xiang Zhangc393ee82017-03-08 11:18:49 +08001350 def test_slots_special2(self):
1351 # Testing __qualname__ and __classcell__ in __slots__
1352 class Meta(type):
1353 def __new__(cls, name, bases, namespace, attr):
1354 self.assertIn(attr, namespace)
1355 return super().__new__(cls, name, bases, namespace)
1356
1357 class C1:
1358 def __init__(self):
1359 self.b = 42
1360 class C2(C1, metaclass=Meta, attr="__classcell__"):
1361 __slots__ = ["__classcell__"]
1362 def __init__(self):
1363 super().__init__()
1364 self.assertIsInstance(C2.__dict__["__classcell__"],
1365 types.MemberDescriptorType)
1366 c = C2()
1367 self.assertEqual(c.b, 42)
1368 self.assertNotHasAttr(c, "__classcell__")
1369 c.__classcell__ = 42
1370 self.assertEqual(c.__classcell__, 42)
1371 with self.assertRaises(TypeError):
1372 class C3:
1373 __classcell__ = 42
1374 __slots__ = ["__classcell__"]
1375
1376 class Q1(metaclass=Meta, attr="__qualname__"):
1377 __slots__ = ["__qualname__"]
1378 self.assertEqual(Q1.__qualname__, C1.__qualname__[:-2] + "Q1")
1379 self.assertIsInstance(Q1.__dict__["__qualname__"],
1380 types.MemberDescriptorType)
1381 q = Q1()
1382 self.assertNotHasAttr(q, "__qualname__")
1383 q.__qualname__ = "q"
1384 self.assertEqual(q.__qualname__, "q")
1385 with self.assertRaises(TypeError):
1386 class Q2:
1387 __qualname__ = object()
1388 __slots__ = ["__qualname__"]
1389
Christian Heimesa156e092008-02-16 07:38:31 +00001390 def test_slots_descriptor(self):
1391 # Issue2115: slot descriptors did not correctly check
1392 # the type of the given object
1393 import abc
1394 class MyABC(metaclass=abc.ABCMeta):
1395 __slots__ = "a"
1396
1397 class Unrelated(object):
1398 pass
1399 MyABC.register(Unrelated)
1400
1401 u = Unrelated()
Ezio Melottie9615932010-01-24 19:26:24 +00001402 self.assertIsInstance(u, MyABC)
Christian Heimesa156e092008-02-16 07:38:31 +00001403
1404 # This used to crash
1405 self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
1406
Georg Brandl479a7e72008-02-05 18:13:15 +00001407 def test_dynamics(self):
1408 # Testing class attribute propagation...
1409 class D(object):
1410 pass
1411 class E(D):
1412 pass
1413 class F(D):
1414 pass
1415 D.foo = 1
1416 self.assertEqual(D.foo, 1)
1417 # Test that dynamic attributes are inherited
1418 self.assertEqual(E.foo, 1)
1419 self.assertEqual(F.foo, 1)
1420 # Test dynamic instances
1421 class C(object):
1422 pass
1423 a = C()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001424 self.assertNotHasAttr(a, "foobar")
Georg Brandl479a7e72008-02-05 18:13:15 +00001425 C.foobar = 2
1426 self.assertEqual(a.foobar, 2)
1427 C.method = lambda self: 42
1428 self.assertEqual(a.method(), 42)
1429 C.__repr__ = lambda self: "C()"
1430 self.assertEqual(repr(a), "C()")
1431 C.__int__ = lambda self: 100
1432 self.assertEqual(int(a), 100)
1433 self.assertEqual(a.foobar, 2)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001434 self.assertNotHasAttr(a, "spam")
Georg Brandl479a7e72008-02-05 18:13:15 +00001435 def mygetattr(self, name):
1436 if name == "spam":
1437 return "spam"
1438 raise AttributeError
1439 C.__getattr__ = mygetattr
1440 self.assertEqual(a.spam, "spam")
1441 a.new = 12
1442 self.assertEqual(a.new, 12)
1443 def mysetattr(self, name, value):
1444 if name == "spam":
1445 raise AttributeError
1446 return object.__setattr__(self, name, value)
1447 C.__setattr__ = mysetattr
1448 try:
1449 a.spam = "not spam"
1450 except AttributeError:
1451 pass
1452 else:
1453 self.fail("expected AttributeError")
1454 self.assertEqual(a.spam, "spam")
1455 class D(C):
1456 pass
1457 d = D()
1458 d.foo = 1
1459 self.assertEqual(d.foo, 1)
1460
1461 # Test handling of int*seq and seq*int
1462 class I(int):
1463 pass
1464 self.assertEqual("a"*I(2), "aa")
1465 self.assertEqual(I(2)*"a", "aa")
1466 self.assertEqual(2*I(3), 6)
1467 self.assertEqual(I(3)*2, 6)
1468 self.assertEqual(I(3)*I(2), 6)
1469
Georg Brandl479a7e72008-02-05 18:13:15 +00001470 # Test comparison of classes with dynamic metaclasses
1471 class dynamicmetaclass(type):
1472 pass
1473 class someclass(metaclass=dynamicmetaclass):
1474 pass
1475 self.assertNotEqual(someclass, object)
1476
1477 def test_errors(self):
1478 # Testing errors...
1479 try:
1480 class C(list, dict):
1481 pass
1482 except TypeError:
1483 pass
1484 else:
1485 self.fail("inheritance from both list and dict should be illegal")
1486
1487 try:
1488 class C(object, None):
1489 pass
1490 except TypeError:
1491 pass
1492 else:
1493 self.fail("inheritance from non-type should be illegal")
1494 class Classic:
1495 pass
1496
1497 try:
1498 class C(type(len)):
1499 pass
1500 except TypeError:
1501 pass
1502 else:
1503 self.fail("inheritance from CFunction should be illegal")
1504
1505 try:
1506 class C(object):
1507 __slots__ = 1
1508 except TypeError:
1509 pass
1510 else:
1511 self.fail("__slots__ = 1 should be illegal")
1512
1513 try:
1514 class C(object):
1515 __slots__ = [1]
1516 except TypeError:
1517 pass
1518 else:
1519 self.fail("__slots__ = [1] should be illegal")
1520
1521 class M1(type):
1522 pass
1523 class M2(type):
1524 pass
1525 class A1(object, metaclass=M1):
1526 pass
1527 class A2(object, metaclass=M2):
1528 pass
1529 try:
1530 class B(A1, A2):
1531 pass
1532 except TypeError:
1533 pass
1534 else:
1535 self.fail("finding the most derived metaclass should have failed")
1536
1537 def test_classmethods(self):
1538 # Testing class methods...
1539 class C(object):
1540 def foo(*a): return a
1541 goo = classmethod(foo)
1542 c = C()
1543 self.assertEqual(C.goo(1), (C, 1))
1544 self.assertEqual(c.goo(1), (C, 1))
1545 self.assertEqual(c.foo(1), (c, 1))
1546 class D(C):
1547 pass
1548 d = D()
1549 self.assertEqual(D.goo(1), (D, 1))
1550 self.assertEqual(d.goo(1), (D, 1))
1551 self.assertEqual(d.foo(1), (d, 1))
1552 self.assertEqual(D.foo(d, 1), (d, 1))
1553 # Test for a specific crash (SF bug 528132)
Victor Stinner507a5742021-04-09 17:51:22 +02001554 def f(cls, arg):
1555 "f docstring"
1556 return (cls, arg)
Georg Brandl479a7e72008-02-05 18:13:15 +00001557 ff = classmethod(f)
1558 self.assertEqual(ff.__get__(0, int)(42), (int, 42))
1559 self.assertEqual(ff.__get__(0)(42), (int, 42))
1560
1561 # Test super() with classmethods (SF bug 535444)
1562 self.assertEqual(C.goo.__self__, C)
1563 self.assertEqual(D.goo.__self__, D)
1564 self.assertEqual(super(D,D).goo.__self__, D)
1565 self.assertEqual(super(D,d).goo.__self__, D)
1566 self.assertEqual(super(D,D).goo(), (D,))
1567 self.assertEqual(super(D,d).goo(), (D,))
1568
Benjamin Peterson8719ad52009-09-11 22:24:02 +00001569 # Verify that a non-callable will raise
1570 meth = classmethod(1).__get__(1)
1571 self.assertRaises(TypeError, meth)
Georg Brandl479a7e72008-02-05 18:13:15 +00001572
1573 # Verify that classmethod() doesn't allow keyword args
1574 try:
1575 classmethod(f, kw=1)
1576 except TypeError:
1577 pass
1578 else:
1579 self.fail("classmethod shouldn't accept keyword args")
1580
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001581 cm = classmethod(f)
Victor Stinner507a5742021-04-09 17:51:22 +02001582 cm_dict = {'__annotations__': {},
1583 '__doc__': "f docstring",
1584 '__module__': __name__,
1585 '__name__': 'f',
1586 '__qualname__': f.__qualname__}
1587 self.assertEqual(cm.__dict__, cm_dict)
1588
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001589 cm.x = 42
1590 self.assertEqual(cm.x, 42)
Victor Stinner507a5742021-04-09 17:51:22 +02001591 self.assertEqual(cm.__dict__, {"x" : 42, **cm_dict})
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001592 del cm.x
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001593 self.assertNotHasAttr(cm, "x")
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001594
Oren Milmand019bc82018-02-13 12:28:33 +02001595 @support.refcount_test
1596 def test_refleaks_in_classmethod___init__(self):
1597 gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
1598 cm = classmethod(None)
1599 refs_before = gettotalrefcount()
1600 for i in range(100):
1601 cm.__init__(None)
1602 self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
1603
Benjamin Petersone549ead2009-03-28 21:42:05 +00001604 @support.impl_detail("the module 'xxsubtype' is internal")
Georg Brandl479a7e72008-02-05 18:13:15 +00001605 def test_classmethods_in_c(self):
1606 # Testing C-based class methods...
1607 import xxsubtype as spam
1608 a = (1, 2, 3)
1609 d = {'abc': 123}
1610 x, a1, d1 = spam.spamlist.classmeth(*a, **d)
1611 self.assertEqual(x, spam.spamlist)
1612 self.assertEqual(a, a1)
1613 self.assertEqual(d, d1)
1614 x, a1, d1 = spam.spamlist().classmeth(*a, **d)
1615 self.assertEqual(x, spam.spamlist)
1616 self.assertEqual(a, a1)
1617 self.assertEqual(d, d1)
Benjamin Peterson7295c6a2012-05-01 09:51:09 -04001618 spam_cm = spam.spamlist.__dict__['classmeth']
1619 x2, a2, d2 = spam_cm(spam.spamlist, *a, **d)
1620 self.assertEqual(x2, spam.spamlist)
1621 self.assertEqual(a2, a1)
1622 self.assertEqual(d2, d1)
1623 class SubSpam(spam.spamlist): pass
1624 x2, a2, d2 = spam_cm(SubSpam, *a, **d)
1625 self.assertEqual(x2, SubSpam)
1626 self.assertEqual(a2, a1)
1627 self.assertEqual(d2, d1)
Inada Naoki871309c2019-03-26 18:26:33 +09001628
1629 with self.assertRaises(TypeError) as cm:
Benjamin Peterson7295c6a2012-05-01 09:51:09 -04001630 spam_cm()
Inada Naoki871309c2019-03-26 18:26:33 +09001631 self.assertEqual(
1632 str(cm.exception),
1633 "descriptor 'classmeth' of 'xxsubtype.spamlist' "
1634 "object needs an argument")
1635
1636 with self.assertRaises(TypeError) as cm:
Benjamin Peterson7295c6a2012-05-01 09:51:09 -04001637 spam_cm(spam.spamlist())
Inada Naoki871309c2019-03-26 18:26:33 +09001638 self.assertEqual(
1639 str(cm.exception),
Jeroen Demeyer3f345c32019-06-07 12:20:24 +02001640 "descriptor 'classmeth' for type 'xxsubtype.spamlist' "
1641 "needs a type, not a 'xxsubtype.spamlist' as arg 2")
Inada Naoki871309c2019-03-26 18:26:33 +09001642
1643 with self.assertRaises(TypeError) as cm:
Benjamin Peterson7295c6a2012-05-01 09:51:09 -04001644 spam_cm(list)
Inada Naoki62f95882019-04-01 17:56:11 +09001645 expected_errmsg = (
Inada Naoki871309c2019-03-26 18:26:33 +09001646 "descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' "
1647 "but received 'list'")
Inada Naoki62f95882019-04-01 17:56:11 +09001648 self.assertEqual(str(cm.exception), expected_errmsg)
1649
1650 with self.assertRaises(TypeError) as cm:
1651 spam_cm.__get__(None, list)
1652 self.assertEqual(str(cm.exception), expected_errmsg)
Georg Brandl479a7e72008-02-05 18:13:15 +00001653
1654 def test_staticmethods(self):
1655 # Testing static methods...
1656 class C(object):
1657 def foo(*a): return a
1658 goo = staticmethod(foo)
1659 c = C()
1660 self.assertEqual(C.goo(1), (1,))
1661 self.assertEqual(c.goo(1), (1,))
1662 self.assertEqual(c.foo(1), (c, 1,))
1663 class D(C):
1664 pass
1665 d = D()
1666 self.assertEqual(D.goo(1), (1,))
1667 self.assertEqual(d.goo(1), (1,))
1668 self.assertEqual(d.foo(1), (d, 1))
1669 self.assertEqual(D.foo(d, 1), (d, 1))
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001670 sm = staticmethod(None)
Victor Stinner507a5742021-04-09 17:51:22 +02001671 self.assertEqual(sm.__dict__, {'__doc__': None})
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001672 sm.x = 42
1673 self.assertEqual(sm.x, 42)
Victor Stinner507a5742021-04-09 17:51:22 +02001674 self.assertEqual(sm.__dict__, {"x" : 42, '__doc__': None})
Benjamin Peterson01d7eba2012-02-19 01:10:25 -05001675 del sm.x
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001676 self.assertNotHasAttr(sm, "x")
Georg Brandl479a7e72008-02-05 18:13:15 +00001677
Oren Milmand019bc82018-02-13 12:28:33 +02001678 @support.refcount_test
1679 def test_refleaks_in_staticmethod___init__(self):
1680 gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
1681 sm = staticmethod(None)
1682 refs_before = gettotalrefcount()
1683 for i in range(100):
1684 sm.__init__(None)
1685 self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
1686
Benjamin Petersone549ead2009-03-28 21:42:05 +00001687 @support.impl_detail("the module 'xxsubtype' is internal")
Georg Brandl479a7e72008-02-05 18:13:15 +00001688 def test_staticmethods_in_c(self):
1689 # Testing C-based static methods...
1690 import xxsubtype as spam
1691 a = (1, 2, 3)
1692 d = {"abc": 123}
1693 x, a1, d1 = spam.spamlist.staticmeth(*a, **d)
1694 self.assertEqual(x, None)
1695 self.assertEqual(a, a1)
1696 self.assertEqual(d, d1)
1697 x, a1, d2 = spam.spamlist().staticmeth(*a, **d)
1698 self.assertEqual(x, None)
1699 self.assertEqual(a, a1)
1700 self.assertEqual(d, d1)
1701
1702 def test_classic(self):
1703 # Testing classic classes...
1704 class C:
1705 def foo(*a): return a
1706 goo = classmethod(foo)
1707 c = C()
1708 self.assertEqual(C.goo(1), (C, 1))
1709 self.assertEqual(c.goo(1), (C, 1))
1710 self.assertEqual(c.foo(1), (c, 1))
1711 class D(C):
1712 pass
1713 d = D()
1714 self.assertEqual(D.goo(1), (D, 1))
1715 self.assertEqual(d.goo(1), (D, 1))
1716 self.assertEqual(d.foo(1), (d, 1))
1717 self.assertEqual(D.foo(d, 1), (d, 1))
1718 class E: # *not* subclassing from C
1719 foo = C.foo
1720 self.assertEqual(E().foo.__func__, C.foo) # i.e., unbound
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001721 self.assertTrue(repr(C.foo.__get__(C())).startswith("<bound method "))
Georg Brandl479a7e72008-02-05 18:13:15 +00001722
1723 def test_compattr(self):
1724 # Testing computed attributes...
1725 class C(object):
1726 class computed_attribute(object):
1727 def __init__(self, get, set=None, delete=None):
1728 self.__get = get
1729 self.__set = set
1730 self.__delete = delete
1731 def __get__(self, obj, type=None):
1732 return self.__get(obj)
1733 def __set__(self, obj, value):
1734 return self.__set(obj, value)
1735 def __delete__(self, obj):
1736 return self.__delete(obj)
1737 def __init__(self):
1738 self.__x = 0
1739 def __get_x(self):
1740 x = self.__x
1741 self.__x = x+1
1742 return x
1743 def __set_x(self, x):
1744 self.__x = x
1745 def __delete_x(self):
1746 del self.__x
1747 x = computed_attribute(__get_x, __set_x, __delete_x)
1748 a = C()
1749 self.assertEqual(a.x, 0)
1750 self.assertEqual(a.x, 1)
1751 a.x = 10
1752 self.assertEqual(a.x, 10)
1753 self.assertEqual(a.x, 11)
1754 del a.x
Serhiy Storchaka76edd212013-11-17 23:38:50 +02001755 self.assertNotHasAttr(a, 'x')
Georg Brandl479a7e72008-02-05 18:13:15 +00001756
1757 def test_newslots(self):
1758 # Testing __new__ slot override...
1759 class C(list):
1760 def __new__(cls):
1761 self = list.__new__(cls)
1762 self.foo = 1
1763 return self
1764 def __init__(self):
1765 self.foo = self.foo + 2
1766 a = C()
1767 self.assertEqual(a.foo, 3)
1768 self.assertEqual(a.__class__, C)
1769 class D(C):
1770 pass
1771 b = D()
1772 self.assertEqual(b.foo, 3)
1773 self.assertEqual(b.__class__, D)
1774
Serhiy Storchaka49010ee2016-12-14 19:52:17 +02001775 @unittest.expectedFailure
Serhiy Storchaka5adfac22016-12-02 08:42:43 +02001776 def test_bad_new(self):
1777 self.assertRaises(TypeError, object.__new__)
1778 self.assertRaises(TypeError, object.__new__, '')
1779 self.assertRaises(TypeError, list.__new__, object)
1780 self.assertRaises(TypeError, object.__new__, list)
1781 class C(object):
1782 __new__ = list.__new__
1783 self.assertRaises(TypeError, C)
1784 class C(list):
1785 __new__ = object.__new__
1786 self.assertRaises(TypeError, C)
1787
1788 def test_object_new(self):
1789 class A(object):
1790 pass
1791 object.__new__(A)
1792 self.assertRaises(TypeError, object.__new__, A, 5)
1793 object.__init__(A())
1794 self.assertRaises(TypeError, object.__init__, A(), 5)
1795
1796 class A(object):
1797 def __init__(self, foo):
1798 self.foo = foo
1799 object.__new__(A)
1800 object.__new__(A, 5)
1801 object.__init__(A(3))
1802 self.assertRaises(TypeError, object.__init__, A(3), 5)
1803
1804 class A(object):
1805 def __new__(cls, foo):
1806 return object.__new__(cls)
1807 object.__new__(A)
1808 self.assertRaises(TypeError, object.__new__, A, 5)
1809 object.__init__(A(3))
1810 object.__init__(A(3), 5)
1811
1812 class A(object):
1813 def __new__(cls, foo):
1814 return object.__new__(cls)
1815 def __init__(self, foo):
1816 self.foo = foo
1817 object.__new__(A)
1818 self.assertRaises(TypeError, object.__new__, A, 5)
1819 object.__init__(A(3))
1820 self.assertRaises(TypeError, object.__init__, A(3), 5)
1821
Serhiy Storchaka49010ee2016-12-14 19:52:17 +02001822 @unittest.expectedFailure
Serhiy Storchaka5adfac22016-12-02 08:42:43 +02001823 def test_restored_object_new(self):
1824 class A(object):
1825 def __new__(cls, *args, **kwargs):
1826 raise AssertionError
1827 self.assertRaises(AssertionError, A)
1828 class B(A):
1829 __new__ = object.__new__
1830 def __init__(self, foo):
1831 self.foo = foo
1832 with warnings.catch_warnings():
1833 warnings.simplefilter('error', DeprecationWarning)
1834 b = B(3)
1835 self.assertEqual(b.foo, 3)
1836 self.assertEqual(b.__class__, B)
1837 del B.__new__
1838 self.assertRaises(AssertionError, B)
1839 del A.__new__
1840 with warnings.catch_warnings():
1841 warnings.simplefilter('error', DeprecationWarning)
1842 b = B(3)
1843 self.assertEqual(b.foo, 3)
1844 self.assertEqual(b.__class__, B)
1845
Georg Brandl479a7e72008-02-05 18:13:15 +00001846 def test_altmro(self):
1847 # Testing mro() and overriding it...
1848 class A(object):
1849 def f(self): return "A"
1850 class B(A):
1851 pass
1852 class C(A):
1853 def f(self): return "C"
1854 class D(B, C):
1855 pass
Antoine Pitrou1f1a34c2017-12-20 15:58:21 +01001856 self.assertEqual(A.mro(), [A, object])
1857 self.assertEqual(A.__mro__, (A, object))
1858 self.assertEqual(B.mro(), [B, A, object])
1859 self.assertEqual(B.__mro__, (B, A, object))
1860 self.assertEqual(C.mro(), [C, A, object])
1861 self.assertEqual(C.__mro__, (C, A, object))
Georg Brandl479a7e72008-02-05 18:13:15 +00001862 self.assertEqual(D.mro(), [D, B, C, A, object])
1863 self.assertEqual(D.__mro__, (D, B, C, A, object))
1864 self.assertEqual(D().f(), "C")
1865
1866 class PerverseMetaType(type):
1867 def mro(cls):
1868 L = type.mro(cls)
1869 L.reverse()
1870 return L
1871 class X(D,B,C,A, metaclass=PerverseMetaType):
1872 pass
1873 self.assertEqual(X.__mro__, (object, A, C, B, D, X))
1874 self.assertEqual(X().f(), "A")
1875
1876 try:
1877 class _metaclass(type):
1878 def mro(self):
1879 return [self, dict, object]
1880 class X(object, metaclass=_metaclass):
1881 pass
Benjamin Petersone549ead2009-03-28 21:42:05 +00001882 # In CPython, the class creation above already raises
1883 # TypeError, as a protection against the fact that
1884 # instances of X would segfault it. In other Python
1885 # implementations it would be ok to let the class X
1886 # be created, but instead get a clean TypeError on the
1887 # __setitem__ below.
1888 x = object.__new__(X)
1889 x[5] = 6
Georg Brandl479a7e72008-02-05 18:13:15 +00001890 except TypeError:
1891 pass
1892 else:
1893 self.fail("devious mro() return not caught")
1894
1895 try:
1896 class _metaclass(type):
1897 def mro(self):
1898 return [1]
1899 class X(object, metaclass=_metaclass):
1900 pass
1901 except TypeError:
1902 pass
1903 else:
1904 self.fail("non-class mro() return not caught")
1905
1906 try:
1907 class _metaclass(type):
1908 def mro(self):
1909 return 1
1910 class X(object, metaclass=_metaclass):
1911 pass
1912 except TypeError:
1913 pass
1914 else:
1915 self.fail("non-sequence mro() return not caught")
1916
1917 def test_overloading(self):
1918 # Testing operator overloading...
1919
1920 class B(object):
1921 "Intermediate class because object doesn't have a __setattr__"
1922
1923 class C(B):
1924 def __getattr__(self, name):
1925 if name == "foo":
1926 return ("getattr", name)
1927 else:
1928 raise AttributeError
1929 def __setattr__(self, name, value):
1930 if name == "foo":
1931 self.setattr = (name, value)
1932 else:
1933 return B.__setattr__(self, name, value)
1934 def __delattr__(self, name):
1935 if name == "foo":
1936 self.delattr = name
1937 else:
1938 return B.__delattr__(self, name)
1939
1940 def __getitem__(self, key):
1941 return ("getitem", key)
1942 def __setitem__(self, key, value):
1943 self.setitem = (key, value)
1944 def __delitem__(self, key):
1945 self.delitem = key
1946
1947 a = C()
1948 self.assertEqual(a.foo, ("getattr", "foo"))
1949 a.foo = 12
1950 self.assertEqual(a.setattr, ("foo", 12))
1951 del a.foo
1952 self.assertEqual(a.delattr, "foo")
1953
1954 self.assertEqual(a[12], ("getitem", 12))
1955 a[12] = 21
1956 self.assertEqual(a.setitem, (12, 21))
1957 del a[12]
1958 self.assertEqual(a.delitem, 12)
1959
1960 self.assertEqual(a[0:10], ("getitem", slice(0, 10)))
1961 a[0:10] = "foo"
1962 self.assertEqual(a.setitem, (slice(0, 10), "foo"))
1963 del a[0:10]
1964 self.assertEqual(a.delitem, (slice(0, 10)))
1965
1966 def test_methods(self):
1967 # Testing methods...
1968 class C(object):
1969 def __init__(self, x):
1970 self.x = x
1971 def foo(self):
1972 return self.x
1973 c1 = C(1)
1974 self.assertEqual(c1.foo(), 1)
1975 class D(C):
1976 boo = C.foo
1977 goo = c1.foo
1978 d2 = D(2)
1979 self.assertEqual(d2.foo(), 2)
1980 self.assertEqual(d2.boo(), 2)
1981 self.assertEqual(d2.goo(), 1)
1982 class E(object):
1983 foo = C.foo
1984 self.assertEqual(E().foo.__func__, C.foo) # i.e., unbound
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001985 self.assertTrue(repr(C.foo.__get__(C(1))).startswith("<bound method "))
Georg Brandl479a7e72008-02-05 18:13:15 +00001986
Inada Naoki62f95882019-04-01 17:56:11 +09001987 @support.impl_detail("testing error message from implementation")
1988 def test_methods_in_c(self):
1989 # This test checks error messages in builtin method descriptor.
1990 # It is allowed that other Python implementations use
1991 # different error messages.
1992 set_add = set.add
1993
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01001994 expected_errmsg = "unbound method set.add() needs an argument"
Inada Naoki62f95882019-04-01 17:56:11 +09001995
1996 with self.assertRaises(TypeError) as cm:
1997 set_add()
1998 self.assertEqual(cm.exception.args[0], expected_errmsg)
1999
2000 expected_errmsg = "descriptor 'add' for 'set' objects doesn't apply to a 'int' object"
2001
2002 with self.assertRaises(TypeError) as cm:
2003 set_add(0)
2004 self.assertEqual(cm.exception.args[0], expected_errmsg)
2005
2006 with self.assertRaises(TypeError) as cm:
2007 set_add.__get__(0)
2008 self.assertEqual(cm.exception.args[0], expected_errmsg)
2009
Benjamin Peterson224205f2009-05-08 03:25:19 +00002010 def test_special_method_lookup(self):
2011 # The lookup of special methods bypasses __getattr__ and
2012 # __getattribute__, but they still can be descriptors.
2013
2014 def run_context(manager):
2015 with manager:
2016 pass
2017 def iden(self):
2018 return self
2019 def hello(self):
2020 return b"hello"
Benjamin Peterson053c61f2009-05-09 17:21:13 +00002021 def empty_seq(self):
2022 return []
Benjamin Peterson71557592013-04-13 17:20:36 -04002023 def zero(self):
Benjamin Petersona5758c02009-05-09 18:15:04 +00002024 return 0
Benjamin Petersonaea44282010-01-04 01:10:28 +00002025 def complex_num(self):
2026 return 1j
Benjamin Petersona5758c02009-05-09 18:15:04 +00002027 def stop(self):
2028 raise StopIteration
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002029 def return_true(self, thing=None):
2030 return True
2031 def do_isinstance(obj):
2032 return isinstance(int, obj)
2033 def do_issubclass(obj):
2034 return issubclass(int, obj)
Benjamin Petersona7205592009-05-27 03:08:59 +00002035 def do_dict_missing(checker):
2036 class DictSub(checker.__class__, dict):
2037 pass
2038 self.assertEqual(DictSub()["hi"], 4)
2039 def some_number(self_, key):
2040 self.assertEqual(key, "hi")
2041 return 4
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002042 def swallow(*args): pass
Benjamin Petersonda2cf042010-06-05 00:45:37 +00002043 def format_impl(self, spec):
2044 return "hello"
Benjamin Peterson224205f2009-05-08 03:25:19 +00002045
2046 # It would be nice to have every special method tested here, but I'm
2047 # only listing the ones I can remember outside of typeobject.c, since it
2048 # does it right.
2049 specials = [
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002050 ("__bytes__", bytes, hello, set(), {}),
2051 ("__reversed__", reversed, empty_seq, set(), {}),
2052 ("__length_hint__", list, zero, set(),
Benjamin Petersona5758c02009-05-09 18:15:04 +00002053 {"__iter__" : iden, "__next__" : stop}),
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002054 ("__sizeof__", sys.getsizeof, zero, set(), {}),
2055 ("__instancecheck__", do_isinstance, return_true, set(), {}),
Benjamin Petersona7205592009-05-27 03:08:59 +00002056 ("__missing__", do_dict_missing, some_number,
2057 set(("__class__",)), {}),
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002058 ("__subclasscheck__", do_issubclass, return_true,
2059 set(("__bases__",)), {}),
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002060 ("__enter__", run_context, iden, set(), {"__exit__" : swallow}),
2061 ("__exit__", run_context, swallow, set(), {"__enter__" : iden}),
Benjamin Petersonaea44282010-01-04 01:10:28 +00002062 ("__complex__", complex, complex_num, set(), {}),
Benjamin Petersonda2cf042010-06-05 00:45:37 +00002063 ("__format__", format, format_impl, set(), {}),
Benjamin Peterson8bb9cde2010-07-01 15:16:55 +00002064 ("__floor__", math.floor, zero, set(), {}),
2065 ("__trunc__", math.trunc, zero, set(), {}),
Benjamin Peterson1b1a8e72012-03-20 23:48:11 -04002066 ("__trunc__", int, zero, set(), {}),
Benjamin Petersonf751bc92010-07-02 13:46:42 +00002067 ("__ceil__", math.ceil, zero, set(), {}),
Benjamin Peterson7963a352011-05-23 16:11:05 -05002068 ("__dir__", dir, empty_seq, set(), {}),
Benjamin Peterson214a7d22013-04-13 17:19:01 -04002069 ("__round__", round, zero, set(), {}),
Benjamin Peterson224205f2009-05-08 03:25:19 +00002070 ]
2071
2072 class Checker(object):
2073 def __getattr__(self, attr, test=self):
2074 test.fail("__getattr__ called with {0}".format(attr))
2075 def __getattribute__(self, attr, test=self):
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002076 if attr not in ok:
2077 test.fail("__getattribute__ called with {0}".format(attr))
Benjamin Petersona7205592009-05-27 03:08:59 +00002078 return object.__getattribute__(self, attr)
Benjamin Peterson224205f2009-05-08 03:25:19 +00002079 class SpecialDescr(object):
2080 def __init__(self, impl):
2081 self.impl = impl
2082 def __get__(self, obj, owner):
2083 record.append(1)
Benjamin Peterson8a282d12009-05-08 18:18:45 +00002084 return self.impl.__get__(obj, owner)
Benjamin Peterson94c65d92009-05-25 03:10:48 +00002085 class MyException(Exception):
2086 pass
2087 class ErrDescr(object):
2088 def __get__(self, obj, owner):
2089 raise MyException
Benjamin Peterson224205f2009-05-08 03:25:19 +00002090
Benjamin Peterson88fe5f92009-05-16 21:55:24 +00002091 for name, runner, meth_impl, ok, env in specials:
Benjamin Peterson224205f2009-05-08 03:25:19 +00002092 class X(Checker):
2093 pass
Benjamin Petersona5758c02009-05-09 18:15:04 +00002094 for attr, obj in env.items():
2095 setattr(X, attr, obj)
Benjamin Peterson8a282d12009-05-08 18:18:45 +00002096 setattr(X, name, meth_impl)
Benjamin Peterson224205f2009-05-08 03:25:19 +00002097 runner(X())
2098
2099 record = []
2100 class X(Checker):
2101 pass
Benjamin Petersona5758c02009-05-09 18:15:04 +00002102 for attr, obj in env.items():
2103 setattr(X, attr, obj)
Benjamin Peterson224205f2009-05-08 03:25:19 +00002104 setattr(X, name, SpecialDescr(meth_impl))
2105 runner(X())
2106 self.assertEqual(record, [1], name)
2107
Benjamin Peterson94c65d92009-05-25 03:10:48 +00002108 class X(Checker):
2109 pass
2110 for attr, obj in env.items():
2111 setattr(X, attr, obj)
2112 setattr(X, name, ErrDescr())
Benjamin Petersonb45c7082011-05-24 19:31:01 -05002113 self.assertRaises(MyException, runner, X())
Benjamin Peterson94c65d92009-05-25 03:10:48 +00002114
Georg Brandl479a7e72008-02-05 18:13:15 +00002115 def test_specials(self):
2116 # Testing special operators...
2117 # Test operators like __hash__ for which a built-in default exists
2118
2119 # Test the default behavior for static classes
2120 class C(object):
2121 def __getitem__(self, i):
2122 if 0 <= i < 10: return i
2123 raise IndexError
2124 c1 = C()
2125 c2 = C()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002126 self.assertFalse(not c1)
Georg Brandl479a7e72008-02-05 18:13:15 +00002127 self.assertNotEqual(id(c1), id(c2))
2128 hash(c1)
2129 hash(c2)
Georg Brandl479a7e72008-02-05 18:13:15 +00002130 self.assertEqual(c1, c1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002131 self.assertTrue(c1 != c2)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002132 self.assertFalse(c1 != c1)
2133 self.assertFalse(c1 == c2)
Georg Brandl479a7e72008-02-05 18:13:15 +00002134 # Note that the module name appears in str/repr, and that varies
2135 # depending on whether this test is run standalone or from a framework.
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002136 self.assertGreaterEqual(str(c1).find('C object at '), 0)
Georg Brandl479a7e72008-02-05 18:13:15 +00002137 self.assertEqual(str(c1), repr(c1))
Benjamin Peterson577473f2010-01-19 00:09:57 +00002138 self.assertNotIn(-1, c1)
Georg Brandl479a7e72008-02-05 18:13:15 +00002139 for i in range(10):
Benjamin Peterson577473f2010-01-19 00:09:57 +00002140 self.assertIn(i, c1)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00002141 self.assertNotIn(10, c1)
Georg Brandl479a7e72008-02-05 18:13:15 +00002142 # Test the default behavior for dynamic classes
2143 class D(object):
2144 def __getitem__(self, i):
2145 if 0 <= i < 10: return i
2146 raise IndexError
2147 d1 = D()
2148 d2 = D()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002149 self.assertFalse(not d1)
Georg Brandl479a7e72008-02-05 18:13:15 +00002150 self.assertNotEqual(id(d1), id(d2))
2151 hash(d1)
2152 hash(d2)
Georg Brandl479a7e72008-02-05 18:13:15 +00002153 self.assertEqual(d1, d1)
2154 self.assertNotEqual(d1, d2)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002155 self.assertFalse(d1 != d1)
2156 self.assertFalse(d1 == d2)
Georg Brandl479a7e72008-02-05 18:13:15 +00002157 # Note that the module name appears in str/repr, and that varies
2158 # depending on whether this test is run standalone or from a framework.
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002159 self.assertGreaterEqual(str(d1).find('D object at '), 0)
Georg Brandl479a7e72008-02-05 18:13:15 +00002160 self.assertEqual(str(d1), repr(d1))
Benjamin Peterson577473f2010-01-19 00:09:57 +00002161 self.assertNotIn(-1, d1)
Georg Brandl479a7e72008-02-05 18:13:15 +00002162 for i in range(10):
Benjamin Peterson577473f2010-01-19 00:09:57 +00002163 self.assertIn(i, d1)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00002164 self.assertNotIn(10, d1)
Benjamin Peterson60192082008-10-16 19:34:46 +00002165 # Test overridden behavior
Georg Brandl479a7e72008-02-05 18:13:15 +00002166 class Proxy(object):
2167 def __init__(self, x):
2168 self.x = x
2169 def __bool__(self):
2170 return not not self.x
2171 def __hash__(self):
2172 return hash(self.x)
2173 def __eq__(self, other):
2174 return self.x == other
2175 def __ne__(self, other):
2176 return self.x != other
Benjamin Peterson60192082008-10-16 19:34:46 +00002177 def __ge__(self, other):
2178 return self.x >= other
2179 def __gt__(self, other):
2180 return self.x > other
2181 def __le__(self, other):
2182 return self.x <= other
2183 def __lt__(self, other):
2184 return self.x < other
Georg Brandl479a7e72008-02-05 18:13:15 +00002185 def __str__(self):
2186 return "Proxy:%s" % self.x
2187 def __repr__(self):
2188 return "Proxy(%r)" % self.x
2189 def __contains__(self, value):
2190 return value in self.x
2191 p0 = Proxy(0)
2192 p1 = Proxy(1)
2193 p_1 = Proxy(-1)
2194 self.assertFalse(p0)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002195 self.assertFalse(not p1)
Georg Brandl479a7e72008-02-05 18:13:15 +00002196 self.assertEqual(hash(p0), hash(0))
2197 self.assertEqual(p0, p0)
2198 self.assertNotEqual(p0, p1)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002199 self.assertFalse(p0 != p0)
Georg Brandl479a7e72008-02-05 18:13:15 +00002200 self.assertEqual(not p0, p1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002201 self.assertTrue(p0 < p1)
2202 self.assertTrue(p0 <= p1)
2203 self.assertTrue(p1 > p0)
2204 self.assertTrue(p1 >= p0)
Georg Brandl479a7e72008-02-05 18:13:15 +00002205 self.assertEqual(str(p0), "Proxy:0")
2206 self.assertEqual(repr(p0), "Proxy(0)")
2207 p10 = Proxy(range(10))
Ezio Melottib58e0bd2010-01-23 15:40:09 +00002208 self.assertNotIn(-1, p10)
Georg Brandl479a7e72008-02-05 18:13:15 +00002209 for i in range(10):
Benjamin Peterson577473f2010-01-19 00:09:57 +00002210 self.assertIn(i, p10)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00002211 self.assertNotIn(10, p10)
Georg Brandl479a7e72008-02-05 18:13:15 +00002212
Georg Brandl479a7e72008-02-05 18:13:15 +00002213 def test_weakrefs(self):
2214 # Testing weak references...
2215 import weakref
2216 class C(object):
2217 pass
2218 c = C()
2219 r = weakref.ref(c)
2220 self.assertEqual(r(), c)
2221 del c
Benjamin Petersone549ead2009-03-28 21:42:05 +00002222 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00002223 self.assertEqual(r(), None)
2224 del r
2225 class NoWeak(object):
2226 __slots__ = ['foo']
2227 no = NoWeak()
2228 try:
2229 weakref.ref(no)
2230 except TypeError as msg:
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002231 self.assertIn("weak reference", str(msg))
Georg Brandl479a7e72008-02-05 18:13:15 +00002232 else:
2233 self.fail("weakref.ref(no) should be illegal")
2234 class Weak(object):
2235 __slots__ = ['foo', '__weakref__']
2236 yes = Weak()
2237 r = weakref.ref(yes)
2238 self.assertEqual(r(), yes)
2239 del yes
Benjamin Petersone549ead2009-03-28 21:42:05 +00002240 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00002241 self.assertEqual(r(), None)
2242 del r
2243
2244 def test_properties(self):
2245 # Testing property...
2246 class C(object):
2247 def getx(self):
2248 return self.__x
2249 def setx(self, value):
2250 self.__x = value
2251 def delx(self):
2252 del self.__x
2253 x = property(getx, setx, delx, doc="I'm the x property.")
2254 a = C()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002255 self.assertNotHasAttr(a, "x")
Georg Brandl479a7e72008-02-05 18:13:15 +00002256 a.x = 42
2257 self.assertEqual(a._C__x, 42)
2258 self.assertEqual(a.x, 42)
2259 del a.x
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002260 self.assertNotHasAttr(a, "x")
2261 self.assertNotHasAttr(a, "_C__x")
Georg Brandl479a7e72008-02-05 18:13:15 +00002262 C.x.__set__(a, 100)
2263 self.assertEqual(C.x.__get__(a), 100)
2264 C.x.__delete__(a)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002265 self.assertNotHasAttr(a, "x")
Georg Brandl479a7e72008-02-05 18:13:15 +00002266
2267 raw = C.__dict__['x']
Ezio Melottie9615932010-01-24 19:26:24 +00002268 self.assertIsInstance(raw, property)
Georg Brandl479a7e72008-02-05 18:13:15 +00002269
2270 attrs = dir(raw)
Benjamin Peterson577473f2010-01-19 00:09:57 +00002271 self.assertIn("__doc__", attrs)
2272 self.assertIn("fget", attrs)
2273 self.assertIn("fset", attrs)
2274 self.assertIn("fdel", attrs)
Georg Brandl479a7e72008-02-05 18:13:15 +00002275
2276 self.assertEqual(raw.__doc__, "I'm the x property.")
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002277 self.assertIs(raw.fget, C.__dict__['getx'])
2278 self.assertIs(raw.fset, C.__dict__['setx'])
2279 self.assertIs(raw.fdel, C.__dict__['delx'])
Georg Brandl479a7e72008-02-05 18:13:15 +00002280
Raymond Hettingereac503a2015-05-13 01:09:59 -07002281 for attr in "fget", "fset", "fdel":
Georg Brandl479a7e72008-02-05 18:13:15 +00002282 try:
2283 setattr(raw, attr, 42)
2284 except AttributeError as msg:
2285 if str(msg).find('readonly') < 0:
2286 self.fail("when setting readonly attr %r on a property, "
2287 "got unexpected AttributeError msg %r" % (attr, str(msg)))
2288 else:
2289 self.fail("expected AttributeError from trying to set readonly %r "
2290 "attr on a property" % attr)
2291
Raymond Hettingereac503a2015-05-13 01:09:59 -07002292 raw.__doc__ = 42
2293 self.assertEqual(raw.__doc__, 42)
2294
Georg Brandl479a7e72008-02-05 18:13:15 +00002295 class D(object):
2296 __getitem__ = property(lambda s: 1/0)
2297
2298 d = D()
2299 try:
2300 for i in d:
2301 str(i)
2302 except ZeroDivisionError:
2303 pass
2304 else:
2305 self.fail("expected ZeroDivisionError from bad property")
2306
R. David Murray378c0cf2010-02-24 01:46:21 +00002307 @unittest.skipIf(sys.flags.optimize >= 2,
2308 "Docstrings are omitted with -O2 and above")
2309 def test_properties_doc_attrib(self):
Georg Brandl479a7e72008-02-05 18:13:15 +00002310 class E(object):
2311 def getter(self):
2312 "getter method"
2313 return 0
2314 def setter(self_, value):
2315 "setter method"
2316 pass
2317 prop = property(getter)
2318 self.assertEqual(prop.__doc__, "getter method")
2319 prop2 = property(fset=setter)
2320 self.assertEqual(prop2.__doc__, None)
2321
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +02002322 @support.cpython_only
R. David Murray378c0cf2010-02-24 01:46:21 +00002323 def test_testcapi_no_segfault(self):
Georg Brandl479a7e72008-02-05 18:13:15 +00002324 # this segfaulted in 2.5b2
2325 try:
2326 import _testcapi
2327 except ImportError:
2328 pass
2329 else:
2330 class X(object):
2331 p = property(_testcapi.test_with_docstring)
2332
2333 def test_properties_plus(self):
2334 class C(object):
2335 foo = property(doc="hello")
2336 @foo.getter
2337 def foo(self):
2338 return self._foo
2339 @foo.setter
2340 def foo(self, value):
2341 self._foo = abs(value)
2342 @foo.deleter
2343 def foo(self):
2344 del self._foo
2345 c = C()
2346 self.assertEqual(C.foo.__doc__, "hello")
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002347 self.assertNotHasAttr(c, "foo")
Georg Brandl479a7e72008-02-05 18:13:15 +00002348 c.foo = -42
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002349 self.assertHasAttr(c, '_foo')
Georg Brandl479a7e72008-02-05 18:13:15 +00002350 self.assertEqual(c._foo, 42)
2351 self.assertEqual(c.foo, 42)
2352 del c.foo
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002353 self.assertNotHasAttr(c, '_foo')
2354 self.assertNotHasAttr(c, "foo")
Georg Brandl479a7e72008-02-05 18:13:15 +00002355
2356 class D(C):
2357 @C.foo.deleter
2358 def foo(self):
2359 try:
2360 del self._foo
2361 except AttributeError:
2362 pass
2363 d = D()
2364 d.foo = 24
2365 self.assertEqual(d.foo, 24)
2366 del d.foo
2367 del d.foo
2368
2369 class E(object):
2370 @property
2371 def foo(self):
2372 return self._foo
2373 @foo.setter
2374 def foo(self, value):
2375 raise RuntimeError
2376 @foo.setter
2377 def foo(self, value):
2378 self._foo = abs(value)
2379 @foo.deleter
2380 def foo(self, value=None):
2381 del self._foo
2382
2383 e = E()
2384 e.foo = -42
2385 self.assertEqual(e.foo, 42)
2386 del e.foo
2387
2388 class F(E):
2389 @E.foo.deleter
2390 def foo(self):
2391 del self._foo
2392 @foo.setter
2393 def foo(self, value):
2394 self._foo = max(0, value)
2395 f = F()
2396 f.foo = -10
2397 self.assertEqual(f.foo, 0)
2398 del f.foo
2399
2400 def test_dict_constructors(self):
2401 # Testing dict constructor ...
2402 d = dict()
2403 self.assertEqual(d, {})
2404 d = dict({})
2405 self.assertEqual(d, {})
2406 d = dict({1: 2, 'a': 'b'})
2407 self.assertEqual(d, {1: 2, 'a': 'b'})
2408 self.assertEqual(d, dict(list(d.items())))
2409 self.assertEqual(d, dict(iter(d.items())))
2410 d = dict({'one':1, 'two':2})
2411 self.assertEqual(d, dict(one=1, two=2))
2412 self.assertEqual(d, dict(**d))
2413 self.assertEqual(d, dict({"one": 1}, two=2))
2414 self.assertEqual(d, dict([("two", 2)], one=1))
2415 self.assertEqual(d, dict([("one", 100), ("two", 200)], **d))
2416 self.assertEqual(d, dict(**d))
2417
2418 for badarg in 0, 0, 0j, "0", [0], (0,):
2419 try:
2420 dict(badarg)
2421 except TypeError:
2422 pass
2423 except ValueError:
2424 if badarg == "0":
2425 # It's a sequence, and its elements are also sequences (gotta
2426 # love strings <wink>), but they aren't of length 2, so this
2427 # one seemed better as a ValueError than a TypeError.
2428 pass
2429 else:
2430 self.fail("no TypeError from dict(%r)" % badarg)
2431 else:
2432 self.fail("no TypeError from dict(%r)" % badarg)
2433
2434 try:
2435 dict({}, {})
2436 except TypeError:
2437 pass
2438 else:
2439 self.fail("no TypeError from dict({}, {})")
2440
2441 class Mapping:
2442 # Lacks a .keys() method; will be added later.
2443 dict = {1:2, 3:4, 'a':1j}
2444
2445 try:
2446 dict(Mapping())
2447 except TypeError:
2448 pass
2449 else:
2450 self.fail("no TypeError from dict(incomplete mapping)")
2451
2452 Mapping.keys = lambda self: list(self.dict.keys())
2453 Mapping.__getitem__ = lambda self, i: self.dict[i]
2454 d = dict(Mapping())
2455 self.assertEqual(d, Mapping.dict)
2456
2457 # Init from sequence of iterable objects, each producing a 2-sequence.
2458 class AddressBookEntry:
2459 def __init__(self, first, last):
2460 self.first = first
2461 self.last = last
2462 def __iter__(self):
2463 return iter([self.first, self.last])
2464
2465 d = dict([AddressBookEntry('Tim', 'Warsaw'),
2466 AddressBookEntry('Barry', 'Peters'),
2467 AddressBookEntry('Tim', 'Peters'),
2468 AddressBookEntry('Barry', 'Warsaw')])
2469 self.assertEqual(d, {'Barry': 'Warsaw', 'Tim': 'Peters'})
2470
2471 d = dict(zip(range(4), range(1, 5)))
2472 self.assertEqual(d, dict([(i, i+1) for i in range(4)]))
2473
2474 # Bad sequence lengths.
2475 for bad in [('tooshort',)], [('too', 'long', 'by 1')]:
2476 try:
2477 dict(bad)
2478 except ValueError:
2479 pass
2480 else:
2481 self.fail("no ValueError from dict(%r)" % bad)
2482
2483 def test_dir(self):
2484 # Testing dir() ...
2485 junk = 12
2486 self.assertEqual(dir(), ['junk', 'self'])
2487 del junk
2488
2489 # Just make sure these don't blow up!
2490 for arg in 2, 2, 2j, 2e0, [2], "2", b"2", (2,), {2:2}, type, self.test_dir:
2491 dir(arg)
2492
2493 # Test dir on new-style classes. Since these have object as a
2494 # base class, a lot more gets sucked in.
2495 def interesting(strings):
2496 return [s for s in strings if not s.startswith('_')]
2497
2498 class C(object):
2499 Cdata = 1
2500 def Cmethod(self): pass
2501
2502 cstuff = ['Cdata', 'Cmethod']
2503 self.assertEqual(interesting(dir(C)), cstuff)
2504
2505 c = C()
2506 self.assertEqual(interesting(dir(c)), cstuff)
Benjamin Peterson577473f2010-01-19 00:09:57 +00002507 ## self.assertIn('__self__', dir(C.Cmethod))
Georg Brandl479a7e72008-02-05 18:13:15 +00002508
2509 c.cdata = 2
2510 c.cmethod = lambda self: 0
2511 self.assertEqual(interesting(dir(c)), cstuff + ['cdata', 'cmethod'])
Benjamin Peterson577473f2010-01-19 00:09:57 +00002512 ## self.assertIn('__self__', dir(c.Cmethod))
Georg Brandl479a7e72008-02-05 18:13:15 +00002513
2514 class A(C):
2515 Adata = 1
2516 def Amethod(self): pass
2517
2518 astuff = ['Adata', 'Amethod'] + cstuff
2519 self.assertEqual(interesting(dir(A)), astuff)
Benjamin Peterson577473f2010-01-19 00:09:57 +00002520 ## self.assertIn('__self__', dir(A.Amethod))
Georg Brandl479a7e72008-02-05 18:13:15 +00002521 a = A()
2522 self.assertEqual(interesting(dir(a)), astuff)
2523 a.adata = 42
2524 a.amethod = lambda self: 3
2525 self.assertEqual(interesting(dir(a)), astuff + ['adata', 'amethod'])
Benjamin Peterson577473f2010-01-19 00:09:57 +00002526 ## self.assertIn('__self__', dir(a.Amethod))
Georg Brandl479a7e72008-02-05 18:13:15 +00002527
2528 # Try a module subclass.
Georg Brandl479a7e72008-02-05 18:13:15 +00002529 class M(type(sys)):
2530 pass
2531 minstance = M("m")
2532 minstance.b = 2
2533 minstance.a = 1
Brett Cannon4c14b5d2013-05-04 13:56:58 -04002534 default_attributes = ['__name__', '__doc__', '__package__',
Eric Snowb523f842013-11-22 09:05:39 -07002535 '__loader__', '__spec__']
Brett Cannon4c14b5d2013-05-04 13:56:58 -04002536 names = [x for x in dir(minstance) if x not in default_attributes]
Georg Brandl479a7e72008-02-05 18:13:15 +00002537 self.assertEqual(names, ['a', 'b'])
2538
2539 class M2(M):
2540 def getdict(self):
2541 return "Not a dict!"
2542 __dict__ = property(getdict)
2543
2544 m2instance = M2("m2")
2545 m2instance.b = 2
2546 m2instance.a = 1
2547 self.assertEqual(m2instance.__dict__, "Not a dict!")
2548 try:
2549 dir(m2instance)
2550 except TypeError:
2551 pass
2552
MojoVampire469325c2020-03-03 18:50:17 +00002553 # Two essentially featureless objects, (Ellipsis just inherits stuff
2554 # from object.
2555 self.assertEqual(dir(object()), dir(Ellipsis))
Georg Brandl479a7e72008-02-05 18:13:15 +00002556
2557 # Nasty test case for proxied objects
2558 class Wrapper(object):
2559 def __init__(self, obj):
2560 self.__obj = obj
2561 def __repr__(self):
2562 return "Wrapper(%s)" % repr(self.__obj)
2563 def __getitem__(self, key):
2564 return Wrapper(self.__obj[key])
2565 def __len__(self):
2566 return len(self.__obj)
2567 def __getattr__(self, name):
2568 return Wrapper(getattr(self.__obj, name))
2569
2570 class C(object):
2571 def __getclass(self):
2572 return Wrapper(type(self))
2573 __class__ = property(__getclass)
2574
2575 dir(C()) # This used to segfault
2576
2577 def test_supers(self):
2578 # Testing super...
2579
2580 class A(object):
2581 def meth(self, a):
2582 return "A(%r)" % a
2583
2584 self.assertEqual(A().meth(1), "A(1)")
2585
2586 class B(A):
2587 def __init__(self):
2588 self.__super = super(B, self)
2589 def meth(self, a):
2590 return "B(%r)" % a + self.__super.meth(a)
2591
2592 self.assertEqual(B().meth(2), "B(2)A(2)")
2593
2594 class C(A):
2595 def meth(self, a):
2596 return "C(%r)" % a + self.__super.meth(a)
2597 C._C__super = super(C)
2598
2599 self.assertEqual(C().meth(3), "C(3)A(3)")
2600
2601 class D(C, B):
2602 def meth(self, a):
2603 return "D(%r)" % a + super(D, self).meth(a)
2604
2605 self.assertEqual(D().meth(4), "D(4)C(4)B(4)A(4)")
2606
2607 # Test for subclassing super
2608
2609 class mysuper(super):
2610 def __init__(self, *args):
2611 return super(mysuper, self).__init__(*args)
2612
2613 class E(D):
2614 def meth(self, a):
2615 return "E(%r)" % a + mysuper(E, self).meth(a)
2616
2617 self.assertEqual(E().meth(5), "E(5)D(5)C(5)B(5)A(5)")
2618
2619 class F(E):
2620 def meth(self, a):
2621 s = self.__super # == mysuper(F, self)
2622 return "F(%r)[%s]" % (a, s.__class__.__name__) + s.meth(a)
2623 F._F__super = mysuper(F)
2624
2625 self.assertEqual(F().meth(6), "F(6)[mysuper]E(6)D(6)C(6)B(6)A(6)")
2626
2627 # Make sure certain errors are raised
2628
2629 try:
2630 super(D, 42)
2631 except TypeError:
2632 pass
2633 else:
2634 self.fail("shouldn't allow super(D, 42)")
2635
2636 try:
2637 super(D, C())
2638 except TypeError:
2639 pass
2640 else:
2641 self.fail("shouldn't allow super(D, C())")
2642
2643 try:
2644 super(D).__get__(12)
2645 except TypeError:
2646 pass
2647 else:
2648 self.fail("shouldn't allow super(D).__get__(12)")
2649
2650 try:
2651 super(D).__get__(C())
2652 except TypeError:
2653 pass
2654 else:
2655 self.fail("shouldn't allow super(D).__get__(C())")
2656
2657 # Make sure data descriptors can be overridden and accessed via super
2658 # (new feature in Python 2.3)
2659
2660 class DDbase(object):
2661 def getx(self): return 42
2662 x = property(getx)
2663
2664 class DDsub(DDbase):
2665 def getx(self): return "hello"
2666 x = property(getx)
2667
2668 dd = DDsub()
2669 self.assertEqual(dd.x, "hello")
2670 self.assertEqual(super(DDsub, dd).x, 42)
2671
2672 # Ensure that super() lookup of descriptor from classmethod
2673 # works (SF ID# 743627)
2674
2675 class Base(object):
2676 aProp = property(lambda self: "foo")
2677
2678 class Sub(Base):
2679 @classmethod
2680 def test(klass):
2681 return super(Sub,klass).aProp
2682
2683 self.assertEqual(Sub.test(), Base.aProp)
2684
2685 # Verify that super() doesn't allow keyword args
Zackery Spytz6b2e3252019-08-26 16:41:11 -06002686 with self.assertRaises(TypeError):
Georg Brandl479a7e72008-02-05 18:13:15 +00002687 super(Base, kw=1)
Georg Brandl479a7e72008-02-05 18:13:15 +00002688
2689 def test_basic_inheritance(self):
2690 # Testing inheritance from basic types...
2691
2692 class hexint(int):
2693 def __repr__(self):
2694 return hex(self)
2695 def __add__(self, other):
2696 return hexint(int.__add__(self, other))
2697 # (Note that overriding __radd__ doesn't work,
2698 # because the int type gets first dibs.)
2699 self.assertEqual(repr(hexint(7) + 9), "0x10")
2700 self.assertEqual(repr(hexint(1000) + 7), "0x3ef")
2701 a = hexint(12345)
2702 self.assertEqual(a, 12345)
2703 self.assertEqual(int(a), 12345)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002704 self.assertIs(int(a).__class__, int)
Georg Brandl479a7e72008-02-05 18:13:15 +00002705 self.assertEqual(hash(a), hash(12345))
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002706 self.assertIs((+a).__class__, int)
2707 self.assertIs((a >> 0).__class__, int)
2708 self.assertIs((a << 0).__class__, int)
2709 self.assertIs((hexint(0) << 12).__class__, int)
2710 self.assertIs((hexint(0) >> 12).__class__, int)
Georg Brandl479a7e72008-02-05 18:13:15 +00002711
2712 class octlong(int):
2713 __slots__ = []
2714 def __str__(self):
Mark Dickinson5c2db372009-12-05 20:28:34 +00002715 return oct(self)
Georg Brandl479a7e72008-02-05 18:13:15 +00002716 def __add__(self, other):
2717 return self.__class__(super(octlong, self).__add__(other))
2718 __radd__ = __add__
2719 self.assertEqual(str(octlong(3) + 5), "0o10")
2720 # (Note that overriding __radd__ here only seems to work
2721 # because the example uses a short int left argument.)
2722 self.assertEqual(str(5 + octlong(3000)), "0o5675")
2723 a = octlong(12345)
2724 self.assertEqual(a, 12345)
2725 self.assertEqual(int(a), 12345)
2726 self.assertEqual(hash(a), hash(12345))
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002727 self.assertIs(int(a).__class__, int)
2728 self.assertIs((+a).__class__, int)
2729 self.assertIs((-a).__class__, int)
2730 self.assertIs((-octlong(0)).__class__, int)
2731 self.assertIs((a >> 0).__class__, int)
2732 self.assertIs((a << 0).__class__, int)
2733 self.assertIs((a - 0).__class__, int)
2734 self.assertIs((a * 1).__class__, int)
2735 self.assertIs((a ** 1).__class__, int)
2736 self.assertIs((a // 1).__class__, int)
2737 self.assertIs((1 * a).__class__, int)
2738 self.assertIs((a | 0).__class__, int)
2739 self.assertIs((a ^ 0).__class__, int)
2740 self.assertIs((a & -1).__class__, int)
2741 self.assertIs((octlong(0) << 12).__class__, int)
2742 self.assertIs((octlong(0) >> 12).__class__, int)
2743 self.assertIs(abs(octlong(0)).__class__, int)
Georg Brandl479a7e72008-02-05 18:13:15 +00002744
2745 # Because octlong overrides __add__, we can't check the absence of +0
2746 # optimizations using octlong.
2747 class longclone(int):
2748 pass
2749 a = longclone(1)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002750 self.assertIs((a + 0).__class__, int)
2751 self.assertIs((0 + a).__class__, int)
Georg Brandl479a7e72008-02-05 18:13:15 +00002752
2753 # Check that negative clones don't segfault
2754 a = longclone(-1)
2755 self.assertEqual(a.__dict__, {})
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002756 self.assertEqual(int(a), -1) # self.assertTrue PyNumber_Long() copies the sign bit
Georg Brandl479a7e72008-02-05 18:13:15 +00002757
2758 class precfloat(float):
2759 __slots__ = ['prec']
2760 def __init__(self, value=0.0, prec=12):
2761 self.prec = int(prec)
2762 def __repr__(self):
2763 return "%.*g" % (self.prec, self)
2764 self.assertEqual(repr(precfloat(1.1)), "1.1")
2765 a = precfloat(12345)
2766 self.assertEqual(a, 12345.0)
2767 self.assertEqual(float(a), 12345.0)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002768 self.assertIs(float(a).__class__, float)
Georg Brandl479a7e72008-02-05 18:13:15 +00002769 self.assertEqual(hash(a), hash(12345.0))
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002770 self.assertIs((+a).__class__, float)
Georg Brandl479a7e72008-02-05 18:13:15 +00002771
2772 class madcomplex(complex):
2773 def __repr__(self):
2774 return "%.17gj%+.17g" % (self.imag, self.real)
2775 a = madcomplex(-3, 4)
2776 self.assertEqual(repr(a), "4j-3")
2777 base = complex(-3, 4)
2778 self.assertEqual(base.__class__, complex)
2779 self.assertEqual(a, base)
2780 self.assertEqual(complex(a), base)
2781 self.assertEqual(complex(a).__class__, complex)
2782 a = madcomplex(a) # just trying another form of the constructor
2783 self.assertEqual(repr(a), "4j-3")
2784 self.assertEqual(a, base)
2785 self.assertEqual(complex(a), base)
2786 self.assertEqual(complex(a).__class__, complex)
2787 self.assertEqual(hash(a), hash(base))
2788 self.assertEqual((+a).__class__, complex)
2789 self.assertEqual((a + 0).__class__, complex)
2790 self.assertEqual(a + 0, base)
2791 self.assertEqual((a - 0).__class__, complex)
2792 self.assertEqual(a - 0, base)
2793 self.assertEqual((a * 1).__class__, complex)
2794 self.assertEqual(a * 1, base)
2795 self.assertEqual((a / 1).__class__, complex)
2796 self.assertEqual(a / 1, base)
2797
2798 class madtuple(tuple):
2799 _rev = None
2800 def rev(self):
2801 if self._rev is not None:
2802 return self._rev
2803 L = list(self)
2804 L.reverse()
2805 self._rev = self.__class__(L)
2806 return self._rev
2807 a = madtuple((1,2,3,4,5,6,7,8,9,0))
2808 self.assertEqual(a, (1,2,3,4,5,6,7,8,9,0))
2809 self.assertEqual(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1)))
2810 self.assertEqual(a.rev().rev(), madtuple((1,2,3,4,5,6,7,8,9,0)))
2811 for i in range(512):
2812 t = madtuple(range(i))
2813 u = t.rev()
2814 v = u.rev()
2815 self.assertEqual(v, t)
2816 a = madtuple((1,2,3,4,5))
2817 self.assertEqual(tuple(a), (1,2,3,4,5))
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002818 self.assertIs(tuple(a).__class__, tuple)
Georg Brandl479a7e72008-02-05 18:13:15 +00002819 self.assertEqual(hash(a), hash((1,2,3,4,5)))
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002820 self.assertIs(a[:].__class__, tuple)
2821 self.assertIs((a * 1).__class__, tuple)
2822 self.assertIs((a * 0).__class__, tuple)
2823 self.assertIs((a + ()).__class__, tuple)
Georg Brandl479a7e72008-02-05 18:13:15 +00002824 a = madtuple(())
2825 self.assertEqual(tuple(a), ())
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002826 self.assertIs(tuple(a).__class__, tuple)
2827 self.assertIs((a + a).__class__, tuple)
2828 self.assertIs((a * 0).__class__, tuple)
2829 self.assertIs((a * 1).__class__, tuple)
2830 self.assertIs((a * 2).__class__, tuple)
2831 self.assertIs(a[:].__class__, tuple)
Georg Brandl479a7e72008-02-05 18:13:15 +00002832
2833 class madstring(str):
2834 _rev = None
2835 def rev(self):
2836 if self._rev is not None:
2837 return self._rev
2838 L = list(self)
2839 L.reverse()
2840 self._rev = self.__class__("".join(L))
2841 return self._rev
2842 s = madstring("abcdefghijklmnopqrstuvwxyz")
2843 self.assertEqual(s, "abcdefghijklmnopqrstuvwxyz")
2844 self.assertEqual(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba"))
2845 self.assertEqual(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz"))
2846 for i in range(256):
2847 s = madstring("".join(map(chr, range(i))))
2848 t = s.rev()
2849 u = t.rev()
2850 self.assertEqual(u, s)
2851 s = madstring("12345")
2852 self.assertEqual(str(s), "12345")
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002853 self.assertIs(str(s).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002854
2855 base = "\x00" * 5
2856 s = madstring(base)
2857 self.assertEqual(s, base)
2858 self.assertEqual(str(s), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002859 self.assertIs(str(s).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002860 self.assertEqual(hash(s), hash(base))
2861 self.assertEqual({s: 1}[base], 1)
2862 self.assertEqual({base: 1}[s], 1)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002863 self.assertIs((s + "").__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002864 self.assertEqual(s + "", base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002865 self.assertIs(("" + s).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002866 self.assertEqual("" + s, base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002867 self.assertIs((s * 0).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002868 self.assertEqual(s * 0, "")
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002869 self.assertIs((s * 1).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002870 self.assertEqual(s * 1, base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002871 self.assertIs((s * 2).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002872 self.assertEqual(s * 2, base + base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002873 self.assertIs(s[:].__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002874 self.assertEqual(s[:], base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002875 self.assertIs(s[0:0].__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002876 self.assertEqual(s[0:0], "")
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002877 self.assertIs(s.strip().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002878 self.assertEqual(s.strip(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002879 self.assertIs(s.lstrip().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002880 self.assertEqual(s.lstrip(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002881 self.assertIs(s.rstrip().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002882 self.assertEqual(s.rstrip(), base)
2883 identitytab = {}
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002884 self.assertIs(s.translate(identitytab).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002885 self.assertEqual(s.translate(identitytab), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002886 self.assertIs(s.replace("x", "x").__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002887 self.assertEqual(s.replace("x", "x"), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002888 self.assertIs(s.ljust(len(s)).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002889 self.assertEqual(s.ljust(len(s)), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002890 self.assertIs(s.rjust(len(s)).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002891 self.assertEqual(s.rjust(len(s)), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002892 self.assertIs(s.center(len(s)).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002893 self.assertEqual(s.center(len(s)), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002894 self.assertIs(s.lower().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002895 self.assertEqual(s.lower(), base)
2896
2897 class madunicode(str):
2898 _rev = None
2899 def rev(self):
2900 if self._rev is not None:
2901 return self._rev
2902 L = list(self)
2903 L.reverse()
2904 self._rev = self.__class__("".join(L))
2905 return self._rev
2906 u = madunicode("ABCDEF")
2907 self.assertEqual(u, "ABCDEF")
2908 self.assertEqual(u.rev(), madunicode("FEDCBA"))
2909 self.assertEqual(u.rev().rev(), madunicode("ABCDEF"))
2910 base = "12345"
2911 u = madunicode(base)
2912 self.assertEqual(str(u), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002913 self.assertIs(str(u).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002914 self.assertEqual(hash(u), hash(base))
2915 self.assertEqual({u: 1}[base], 1)
2916 self.assertEqual({base: 1}[u], 1)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002917 self.assertIs(u.strip().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002918 self.assertEqual(u.strip(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002919 self.assertIs(u.lstrip().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002920 self.assertEqual(u.lstrip(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002921 self.assertIs(u.rstrip().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002922 self.assertEqual(u.rstrip(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002923 self.assertIs(u.replace("x", "x").__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002924 self.assertEqual(u.replace("x", "x"), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002925 self.assertIs(u.replace("xy", "xy").__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002926 self.assertEqual(u.replace("xy", "xy"), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002927 self.assertIs(u.center(len(u)).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002928 self.assertEqual(u.center(len(u)), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002929 self.assertIs(u.ljust(len(u)).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002930 self.assertEqual(u.ljust(len(u)), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002931 self.assertIs(u.rjust(len(u)).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002932 self.assertEqual(u.rjust(len(u)), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002933 self.assertIs(u.lower().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002934 self.assertEqual(u.lower(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002935 self.assertIs(u.upper().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002936 self.assertEqual(u.upper(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002937 self.assertIs(u.capitalize().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002938 self.assertEqual(u.capitalize(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002939 self.assertIs(u.title().__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002940 self.assertEqual(u.title(), base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002941 self.assertIs((u + "").__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002942 self.assertEqual(u + "", base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002943 self.assertIs(("" + u).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002944 self.assertEqual("" + u, base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002945 self.assertIs((u * 0).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002946 self.assertEqual(u * 0, "")
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002947 self.assertIs((u * 1).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002948 self.assertEqual(u * 1, base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002949 self.assertIs((u * 2).__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002950 self.assertEqual(u * 2, base + base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002951 self.assertIs(u[:].__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002952 self.assertEqual(u[:], base)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02002953 self.assertIs(u[0:0].__class__, str)
Georg Brandl479a7e72008-02-05 18:13:15 +00002954 self.assertEqual(u[0:0], "")
2955
2956 class sublist(list):
2957 pass
2958 a = sublist(range(5))
2959 self.assertEqual(a, list(range(5)))
2960 a.append("hello")
2961 self.assertEqual(a, list(range(5)) + ["hello"])
2962 a[5] = 5
2963 self.assertEqual(a, list(range(6)))
2964 a.extend(range(6, 20))
2965 self.assertEqual(a, list(range(20)))
2966 a[-5:] = []
2967 self.assertEqual(a, list(range(15)))
2968 del a[10:15]
2969 self.assertEqual(len(a), 10)
2970 self.assertEqual(a, list(range(10)))
2971 self.assertEqual(list(a), list(range(10)))
2972 self.assertEqual(a[0], 0)
2973 self.assertEqual(a[9], 9)
2974 self.assertEqual(a[-10], 0)
2975 self.assertEqual(a[-1], 9)
2976 self.assertEqual(a[:5], list(range(5)))
2977
2978 ## class CountedInput(file):
2979 ## """Counts lines read by self.readline().
2980 ##
2981 ## self.lineno is the 0-based ordinal of the last line read, up to
2982 ## a maximum of one greater than the number of lines in the file.
2983 ##
2984 ## self.ateof is true if and only if the final "" line has been read,
2985 ## at which point self.lineno stops incrementing, and further calls
2986 ## to readline() continue to return "".
2987 ## """
2988 ##
2989 ## lineno = 0
2990 ## ateof = 0
2991 ## def readline(self):
2992 ## if self.ateof:
2993 ## return ""
2994 ## s = file.readline(self)
2995 ## # Next line works too.
2996 ## # s = super(CountedInput, self).readline()
2997 ## self.lineno += 1
2998 ## if s == "":
2999 ## self.ateof = 1
3000 ## return s
3001 ##
Hai Shifcce8c62020-08-08 05:55:35 +08003002 ## f = file(name=os_helper.TESTFN, mode='w')
Georg Brandl479a7e72008-02-05 18:13:15 +00003003 ## lines = ['a\n', 'b\n', 'c\n']
3004 ## try:
3005 ## f.writelines(lines)
3006 ## f.close()
Hai Shifcce8c62020-08-08 05:55:35 +08003007 ## f = CountedInput(os_helper.TESTFN)
Georg Brandl479a7e72008-02-05 18:13:15 +00003008 ## for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]):
3009 ## got = f.readline()
3010 ## self.assertEqual(expected, got)
3011 ## self.assertEqual(f.lineno, i)
3012 ## self.assertEqual(f.ateof, (i > len(lines)))
3013 ## f.close()
3014 ## finally:
3015 ## try:
3016 ## f.close()
3017 ## except:
3018 ## pass
Hai Shifcce8c62020-08-08 05:55:35 +08003019 ## os_helper.unlink(os_helper.TESTFN)
Georg Brandl479a7e72008-02-05 18:13:15 +00003020
3021 def test_keywords(self):
3022 # Testing keyword args to basic type constructors ...
Serhiy Storchakad908fd92017-03-06 21:08:59 +02003023 with self.assertRaisesRegex(TypeError, 'keyword argument'):
3024 int(x=1)
3025 with self.assertRaisesRegex(TypeError, 'keyword argument'):
3026 float(x=2)
3027 with self.assertRaisesRegex(TypeError, 'keyword argument'):
3028 bool(x=2)
Georg Brandl479a7e72008-02-05 18:13:15 +00003029 self.assertEqual(complex(imag=42, real=666), complex(666, 42))
3030 self.assertEqual(str(object=500), '500')
3031 self.assertEqual(str(object=b'abc', errors='strict'), 'abc')
Serhiy Storchakad908fd92017-03-06 21:08:59 +02003032 with self.assertRaisesRegex(TypeError, 'keyword argument'):
3033 tuple(sequence=range(3))
3034 with self.assertRaisesRegex(TypeError, 'keyword argument'):
3035 list(sequence=(0, 1, 2))
Georg Brandl479a7e72008-02-05 18:13:15 +00003036 # note: as of Python 2.3, dict() no longer has an "items" keyword arg
3037
3038 for constructor in (int, float, int, complex, str, str,
3039 tuple, list):
3040 try:
3041 constructor(bogus_keyword_arg=1)
3042 except TypeError:
3043 pass
3044 else:
3045 self.fail("expected TypeError from bogus keyword argument to %r"
3046 % constructor)
3047
3048 def test_str_subclass_as_dict_key(self):
3049 # Testing a str subclass used as dict key ..
3050
3051 class cistr(str):
Min ho Kim39d87b52019-08-31 06:21:19 +10003052 """Subclass of str that computes __eq__ case-insensitively.
Georg Brandl479a7e72008-02-05 18:13:15 +00003053
3054 Also computes a hash code of the string in canonical form.
3055 """
3056
3057 def __init__(self, value):
3058 self.canonical = value.lower()
3059 self.hashcode = hash(self.canonical)
3060
3061 def __eq__(self, other):
3062 if not isinstance(other, cistr):
3063 other = cistr(other)
3064 return self.canonical == other.canonical
3065
3066 def __hash__(self):
3067 return self.hashcode
3068
3069 self.assertEqual(cistr('ABC'), 'abc')
3070 self.assertEqual('aBc', cistr('ABC'))
3071 self.assertEqual(str(cistr('ABC')), 'ABC')
3072
3073 d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3}
3074 self.assertEqual(d[cistr('one')], 1)
3075 self.assertEqual(d[cistr('tWo')], 2)
3076 self.assertEqual(d[cistr('THrEE')], 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +00003077 self.assertIn(cistr('ONe'), d)
Georg Brandl479a7e72008-02-05 18:13:15 +00003078 self.assertEqual(d.get(cistr('thrEE')), 3)
3079
3080 def test_classic_comparisons(self):
3081 # Testing classic comparisons...
3082 class classic:
3083 pass
3084
3085 for base in (classic, int, object):
3086 class C(base):
3087 def __init__(self, value):
3088 self.value = int(value)
3089 def __eq__(self, other):
3090 if isinstance(other, C):
3091 return self.value == other.value
3092 if isinstance(other, int) or isinstance(other, int):
3093 return self.value == other
3094 return NotImplemented
3095 def __ne__(self, other):
3096 if isinstance(other, C):
3097 return self.value != other.value
3098 if isinstance(other, int) or isinstance(other, int):
3099 return self.value != other
3100 return NotImplemented
3101 def __lt__(self, other):
3102 if isinstance(other, C):
3103 return self.value < other.value
3104 if isinstance(other, int) or isinstance(other, int):
3105 return self.value < other
3106 return NotImplemented
3107 def __le__(self, other):
3108 if isinstance(other, C):
3109 return self.value <= other.value
3110 if isinstance(other, int) or isinstance(other, int):
3111 return self.value <= other
3112 return NotImplemented
3113 def __gt__(self, other):
3114 if isinstance(other, C):
3115 return self.value > other.value
3116 if isinstance(other, int) or isinstance(other, int):
3117 return self.value > other
3118 return NotImplemented
3119 def __ge__(self, other):
3120 if isinstance(other, C):
3121 return self.value >= other.value
3122 if isinstance(other, int) or isinstance(other, int):
3123 return self.value >= other
3124 return NotImplemented
3125
3126 c1 = C(1)
3127 c2 = C(2)
3128 c3 = C(3)
3129 self.assertEqual(c1, 1)
3130 c = {1: c1, 2: c2, 3: c3}
3131 for x in 1, 2, 3:
3132 for y in 1, 2, 3:
Georg Brandl479a7e72008-02-05 18:13:15 +00003133 for op in "<", "<=", "==", "!=", ">", ">=":
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003134 self.assertEqual(eval("c[x] %s c[y]" % op),
Mark Dickinsona56c4672009-01-27 18:17:45 +00003135 eval("x %s y" % op),
3136 "x=%d, y=%d" % (x, y))
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003137 self.assertEqual(eval("c[x] %s y" % op),
Mark Dickinsona56c4672009-01-27 18:17:45 +00003138 eval("x %s y" % op),
3139 "x=%d, y=%d" % (x, y))
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003140 self.assertEqual(eval("x %s c[y]" % op),
Mark Dickinsona56c4672009-01-27 18:17:45 +00003141 eval("x %s y" % op),
3142 "x=%d, y=%d" % (x, y))
Georg Brandl479a7e72008-02-05 18:13:15 +00003143
3144 def test_rich_comparisons(self):
3145 # Testing rich comparisons...
3146 class Z(complex):
3147 pass
3148 z = Z(1)
3149 self.assertEqual(z, 1+0j)
3150 self.assertEqual(1+0j, z)
3151 class ZZ(complex):
3152 def __eq__(self, other):
3153 try:
3154 return abs(self - other) <= 1e-6
3155 except:
3156 return NotImplemented
3157 zz = ZZ(1.0000003)
3158 self.assertEqual(zz, 1+0j)
3159 self.assertEqual(1+0j, zz)
3160
3161 class classic:
3162 pass
3163 for base in (classic, int, object, list):
3164 class C(base):
3165 def __init__(self, value):
3166 self.value = int(value)
3167 def __cmp__(self_, other):
3168 self.fail("shouldn't call __cmp__")
3169 def __eq__(self, other):
3170 if isinstance(other, C):
3171 return self.value == other.value
3172 if isinstance(other, int) or isinstance(other, int):
3173 return self.value == other
3174 return NotImplemented
3175 def __ne__(self, other):
3176 if isinstance(other, C):
3177 return self.value != other.value
3178 if isinstance(other, int) or isinstance(other, int):
3179 return self.value != other
3180 return NotImplemented
3181 def __lt__(self, other):
3182 if isinstance(other, C):
3183 return self.value < other.value
3184 if isinstance(other, int) or isinstance(other, int):
3185 return self.value < other
3186 return NotImplemented
3187 def __le__(self, other):
3188 if isinstance(other, C):
3189 return self.value <= other.value
3190 if isinstance(other, int) or isinstance(other, int):
3191 return self.value <= other
3192 return NotImplemented
3193 def __gt__(self, other):
3194 if isinstance(other, C):
3195 return self.value > other.value
3196 if isinstance(other, int) or isinstance(other, int):
3197 return self.value > other
3198 return NotImplemented
3199 def __ge__(self, other):
3200 if isinstance(other, C):
3201 return self.value >= other.value
3202 if isinstance(other, int) or isinstance(other, int):
3203 return self.value >= other
3204 return NotImplemented
3205 c1 = C(1)
3206 c2 = C(2)
3207 c3 = C(3)
3208 self.assertEqual(c1, 1)
3209 c = {1: c1, 2: c2, 3: c3}
3210 for x in 1, 2, 3:
3211 for y in 1, 2, 3:
3212 for op in "<", "<=", "==", "!=", ">", ">=":
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003213 self.assertEqual(eval("c[x] %s c[y]" % op),
3214 eval("x %s y" % op),
3215 "x=%d, y=%d" % (x, y))
3216 self.assertEqual(eval("c[x] %s y" % op),
3217 eval("x %s y" % op),
3218 "x=%d, y=%d" % (x, y))
3219 self.assertEqual(eval("x %s c[y]" % op),
3220 eval("x %s y" % op),
3221 "x=%d, y=%d" % (x, y))
Georg Brandl479a7e72008-02-05 18:13:15 +00003222
3223 def test_descrdoc(self):
3224 # Testing descriptor doc strings...
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003225 from _io import FileIO
Georg Brandl479a7e72008-02-05 18:13:15 +00003226 def check(descr, what):
3227 self.assertEqual(descr.__doc__, what)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003228 check(FileIO.closed, "True if the file is closed") # getset descriptor
Georg Brandl479a7e72008-02-05 18:13:15 +00003229 check(complex.real, "the real part of a complex number") # member descriptor
3230
3231 def test_doc_descriptor(self):
3232 # Testing __doc__ descriptor...
3233 # SF bug 542984
3234 class DocDescr(object):
3235 def __get__(self, object, otype):
3236 if object:
3237 object = object.__class__.__name__ + ' instance'
3238 if otype:
3239 otype = otype.__name__
3240 return 'object=%s; type=%s' % (object, otype)
3241 class OldClass:
3242 __doc__ = DocDescr()
3243 class NewClass(object):
3244 __doc__ = DocDescr()
3245 self.assertEqual(OldClass.__doc__, 'object=None; type=OldClass')
3246 self.assertEqual(OldClass().__doc__, 'object=OldClass instance; type=OldClass')
3247 self.assertEqual(NewClass.__doc__, 'object=None; type=NewClass')
3248 self.assertEqual(NewClass().__doc__, 'object=NewClass instance; type=NewClass')
3249
3250 def test_set_class(self):
3251 # Testing __class__ assignment...
3252 class C(object): pass
3253 class D(object): pass
3254 class E(object): pass
3255 class F(D, E): pass
3256 for cls in C, D, E, F:
3257 for cls2 in C, D, E, F:
3258 x = cls()
3259 x.__class__ = cls2
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003260 self.assertIs(x.__class__, cls2)
Georg Brandl479a7e72008-02-05 18:13:15 +00003261 x.__class__ = cls
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003262 self.assertIs(x.__class__, cls)
Georg Brandl479a7e72008-02-05 18:13:15 +00003263 def cant(x, C):
3264 try:
3265 x.__class__ = C
3266 except TypeError:
3267 pass
3268 else:
3269 self.fail("shouldn't allow %r.__class__ = %r" % (x, C))
3270 try:
3271 delattr(x, "__class__")
Benjamin Petersone549ead2009-03-28 21:42:05 +00003272 except (TypeError, AttributeError):
Georg Brandl479a7e72008-02-05 18:13:15 +00003273 pass
3274 else:
3275 self.fail("shouldn't allow del %r.__class__" % x)
3276 cant(C(), list)
3277 cant(list(), C)
3278 cant(C(), 1)
3279 cant(C(), object)
3280 cant(object(), list)
3281 cant(list(), object)
3282 class Int(int): __slots__ = []
Georg Brandl479a7e72008-02-05 18:13:15 +00003283 cant(True, int)
3284 cant(2, bool)
3285 o = object()
3286 cant(o, type(1))
3287 cant(o, type(None))
3288 del o
3289 class G(object):
3290 __slots__ = ["a", "b"]
3291 class H(object):
3292 __slots__ = ["b", "a"]
3293 class I(object):
3294 __slots__ = ["a", "b"]
3295 class J(object):
3296 __slots__ = ["c", "b"]
3297 class K(object):
3298 __slots__ = ["a", "b", "d"]
3299 class L(H):
3300 __slots__ = ["e"]
3301 class M(I):
3302 __slots__ = ["e"]
3303 class N(J):
3304 __slots__ = ["__weakref__"]
3305 class P(J):
3306 __slots__ = ["__dict__"]
3307 class Q(J):
3308 pass
3309 class R(J):
3310 __slots__ = ["__dict__", "__weakref__"]
3311
3312 for cls, cls2 in ((G, H), (G, I), (I, H), (Q, R), (R, Q)):
3313 x = cls()
3314 x.a = 1
3315 x.__class__ = cls2
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003316 self.assertIs(x.__class__, cls2,
Georg Brandl479a7e72008-02-05 18:13:15 +00003317 "assigning %r as __class__ for %r silently failed" % (cls2, x))
3318 self.assertEqual(x.a, 1)
3319 x.__class__ = cls
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003320 self.assertIs(x.__class__, cls,
Georg Brandl479a7e72008-02-05 18:13:15 +00003321 "assigning %r as __class__ for %r silently failed" % (cls, x))
3322 self.assertEqual(x.a, 1)
3323 for cls in G, J, K, L, M, N, P, R, list, Int:
3324 for cls2 in G, J, K, L, M, N, P, R, list, Int:
3325 if cls is cls2:
3326 continue
3327 cant(cls(), cls2)
3328
Benjamin Peterson193152c2009-04-25 01:08:45 +00003329 # Issue5283: when __class__ changes in __del__, the wrong
3330 # type gets DECREF'd.
3331 class O(object):
3332 pass
3333 class A(object):
3334 def __del__(self):
3335 self.__class__ = O
3336 l = [A() for x in range(100)]
3337 del l
3338
Georg Brandl479a7e72008-02-05 18:13:15 +00003339 def test_set_dict(self):
3340 # Testing __dict__ assignment...
3341 class C(object): pass
3342 a = C()
3343 a.__dict__ = {'b': 1}
3344 self.assertEqual(a.b, 1)
3345 def cant(x, dict):
3346 try:
3347 x.__dict__ = dict
3348 except (AttributeError, TypeError):
3349 pass
3350 else:
3351 self.fail("shouldn't allow %r.__dict__ = %r" % (x, dict))
3352 cant(a, None)
3353 cant(a, [])
3354 cant(a, 1)
3355 del a.__dict__ # Deleting __dict__ is allowed
3356
3357 class Base(object):
3358 pass
3359 def verify_dict_readonly(x):
3360 """
3361 x has to be an instance of a class inheriting from Base.
3362 """
3363 cant(x, {})
3364 try:
3365 del x.__dict__
3366 except (AttributeError, TypeError):
3367 pass
3368 else:
3369 self.fail("shouldn't allow del %r.__dict__" % x)
3370 dict_descr = Base.__dict__["__dict__"]
3371 try:
3372 dict_descr.__set__(x, {})
3373 except (AttributeError, TypeError):
3374 pass
3375 else:
3376 self.fail("dict_descr allowed access to %r's dict" % x)
3377
3378 # Classes don't allow __dict__ assignment and have readonly dicts
3379 class Meta1(type, Base):
3380 pass
3381 class Meta2(Base, type):
3382 pass
3383 class D(object, metaclass=Meta1):
3384 pass
3385 class E(object, metaclass=Meta2):
3386 pass
3387 for cls in C, D, E:
3388 verify_dict_readonly(cls)
3389 class_dict = cls.__dict__
3390 try:
3391 class_dict["spam"] = "eggs"
3392 except TypeError:
3393 pass
3394 else:
3395 self.fail("%r's __dict__ can be modified" % cls)
3396
3397 # Modules also disallow __dict__ assignment
3398 class Module1(types.ModuleType, Base):
3399 pass
3400 class Module2(Base, types.ModuleType):
3401 pass
3402 for ModuleType in Module1, Module2:
3403 mod = ModuleType("spam")
3404 verify_dict_readonly(mod)
3405 mod.__dict__["spam"] = "eggs"
3406
3407 # Exception's __dict__ can be replaced, but not deleted
Benjamin Petersone549ead2009-03-28 21:42:05 +00003408 # (at least not any more than regular exception's __dict__ can
3409 # be deleted; on CPython it is not the case, whereas on PyPy they
3410 # can, just like any other new-style instance's __dict__.)
3411 def can_delete_dict(e):
3412 try:
3413 del e.__dict__
3414 except (TypeError, AttributeError):
3415 return False
3416 else:
3417 return True
Georg Brandl479a7e72008-02-05 18:13:15 +00003418 class Exception1(Exception, Base):
3419 pass
3420 class Exception2(Base, Exception):
3421 pass
3422 for ExceptionType in Exception, Exception1, Exception2:
3423 e = ExceptionType()
3424 e.__dict__ = {"a": 1}
3425 self.assertEqual(e.a, 1)
Benjamin Petersone549ead2009-03-28 21:42:05 +00003426 self.assertEqual(can_delete_dict(e), can_delete_dict(ValueError()))
Georg Brandl479a7e72008-02-05 18:13:15 +00003427
Georg Brandl479a7e72008-02-05 18:13:15 +00003428 def test_binary_operator_override(self):
3429 # Testing overrides of binary operations...
3430 class I(int):
3431 def __repr__(self):
3432 return "I(%r)" % int(self)
3433 def __add__(self, other):
3434 return I(int(self) + int(other))
3435 __radd__ = __add__
3436 def __pow__(self, other, mod=None):
3437 if mod is None:
3438 return I(pow(int(self), int(other)))
3439 else:
3440 return I(pow(int(self), int(other), int(mod)))
3441 def __rpow__(self, other, mod=None):
3442 if mod is None:
3443 return I(pow(int(other), int(self), mod))
3444 else:
3445 return I(pow(int(other), int(self), int(mod)))
3446
3447 self.assertEqual(repr(I(1) + I(2)), "I(3)")
3448 self.assertEqual(repr(I(1) + 2), "I(3)")
3449 self.assertEqual(repr(1 + I(2)), "I(3)")
3450 self.assertEqual(repr(I(2) ** I(3)), "I(8)")
3451 self.assertEqual(repr(2 ** I(3)), "I(8)")
3452 self.assertEqual(repr(I(2) ** 3), "I(8)")
3453 self.assertEqual(repr(pow(I(2), I(3), I(5))), "I(3)")
3454 class S(str):
3455 def __eq__(self, other):
3456 return self.lower() == other.lower()
3457
3458 def test_subclass_propagation(self):
3459 # Testing propagation of slot functions to subclasses...
3460 class A(object):
3461 pass
3462 class B(A):
3463 pass
3464 class C(A):
3465 pass
3466 class D(B, C):
3467 pass
3468 d = D()
3469 orig_hash = hash(d) # related to id(d) in platform-dependent ways
3470 A.__hash__ = lambda self: 42
3471 self.assertEqual(hash(d), 42)
3472 C.__hash__ = lambda self: 314
3473 self.assertEqual(hash(d), 314)
3474 B.__hash__ = lambda self: 144
3475 self.assertEqual(hash(d), 144)
3476 D.__hash__ = lambda self: 100
3477 self.assertEqual(hash(d), 100)
Nick Coghland1abd252008-07-15 15:46:38 +00003478 D.__hash__ = None
3479 self.assertRaises(TypeError, hash, d)
Georg Brandl479a7e72008-02-05 18:13:15 +00003480 del D.__hash__
3481 self.assertEqual(hash(d), 144)
Nick Coghland1abd252008-07-15 15:46:38 +00003482 B.__hash__ = None
3483 self.assertRaises(TypeError, hash, d)
Georg Brandl479a7e72008-02-05 18:13:15 +00003484 del B.__hash__
3485 self.assertEqual(hash(d), 314)
Nick Coghland1abd252008-07-15 15:46:38 +00003486 C.__hash__ = None
3487 self.assertRaises(TypeError, hash, d)
Georg Brandl479a7e72008-02-05 18:13:15 +00003488 del C.__hash__
3489 self.assertEqual(hash(d), 42)
Nick Coghland1abd252008-07-15 15:46:38 +00003490 A.__hash__ = None
3491 self.assertRaises(TypeError, hash, d)
Georg Brandl479a7e72008-02-05 18:13:15 +00003492 del A.__hash__
3493 self.assertEqual(hash(d), orig_hash)
3494 d.foo = 42
3495 d.bar = 42
3496 self.assertEqual(d.foo, 42)
3497 self.assertEqual(d.bar, 42)
3498 def __getattribute__(self, name):
3499 if name == "foo":
3500 return 24
3501 return object.__getattribute__(self, name)
3502 A.__getattribute__ = __getattribute__
3503 self.assertEqual(d.foo, 24)
3504 self.assertEqual(d.bar, 42)
3505 def __getattr__(self, name):
3506 if name in ("spam", "foo", "bar"):
3507 return "hello"
3508 raise AttributeError(name)
3509 B.__getattr__ = __getattr__
3510 self.assertEqual(d.spam, "hello")
3511 self.assertEqual(d.foo, 24)
3512 self.assertEqual(d.bar, 42)
3513 del A.__getattribute__
3514 self.assertEqual(d.foo, 42)
3515 del d.foo
3516 self.assertEqual(d.foo, "hello")
3517 self.assertEqual(d.bar, 42)
3518 del B.__getattr__
Guido van Rossum8c842552002-03-14 23:05:54 +00003519 try:
Georg Brandl479a7e72008-02-05 18:13:15 +00003520 d.foo
3521 except AttributeError:
3522 pass
3523 else:
3524 self.fail("d.foo should be undefined now")
3525
3526 # Test a nasty bug in recurse_down_subclasses()
Georg Brandl479a7e72008-02-05 18:13:15 +00003527 class A(object):
3528 pass
3529 class B(A):
3530 pass
3531 del B
Benjamin Petersone549ead2009-03-28 21:42:05 +00003532 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00003533 A.__setitem__ = lambda *a: None # crash
3534
3535 def test_buffer_inheritance(self):
3536 # Testing that buffer interface is inherited ...
3537
3538 import binascii
3539 # SF bug [#470040] ParseTuple t# vs subclasses.
3540
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003541 class MyBytes(bytes):
Georg Brandl479a7e72008-02-05 18:13:15 +00003542 pass
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003543 base = b'abc'
3544 m = MyBytes(base)
Georg Brandl479a7e72008-02-05 18:13:15 +00003545 # b2a_hex uses the buffer interface to get its argument's value, via
3546 # PyArg_ParseTuple 't#' code.
3547 self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base))
3548
Georg Brandl479a7e72008-02-05 18:13:15 +00003549 class MyInt(int):
3550 pass
3551 m = MyInt(42)
3552 try:
3553 binascii.b2a_hex(m)
3554 self.fail('subclass of int should not have a buffer interface')
3555 except TypeError:
3556 pass
3557
3558 def test_str_of_str_subclass(self):
3559 # Testing __str__ defined in subclass of str ...
3560 import binascii
3561 import io
3562
3563 class octetstring(str):
3564 def __str__(self):
Martin v. Löwis15b16a32008-12-02 06:00:15 +00003565 return binascii.b2a_hex(self.encode('ascii')).decode("ascii")
Georg Brandl479a7e72008-02-05 18:13:15 +00003566 def __repr__(self):
3567 return self + " repr"
3568
3569 o = octetstring('A')
3570 self.assertEqual(type(o), octetstring)
3571 self.assertEqual(type(str(o)), str)
3572 self.assertEqual(type(repr(o)), str)
3573 self.assertEqual(ord(o), 0x41)
3574 self.assertEqual(str(o), '41')
3575 self.assertEqual(repr(o), 'A repr')
3576 self.assertEqual(o.__str__(), '41')
3577 self.assertEqual(o.__repr__(), 'A repr')
3578
Georg Brandl479a7e72008-02-05 18:13:15 +00003579 def test_keyword_arguments(self):
3580 # Testing keyword arguments to __init__, __call__...
3581 def f(a): return a
3582 self.assertEqual(f.__call__(a=42), 42)
Serhiy Storchakad908fd92017-03-06 21:08:59 +02003583 ba = bytearray()
3584 bytearray.__init__(ba, 'abc\xbd\u20ac',
3585 encoding='latin1', errors='replace')
3586 self.assertEqual(ba, b'abc\xbd?')
Georg Brandl479a7e72008-02-05 18:13:15 +00003587
3588 def test_recursive_call(self):
3589 # Testing recursive __call__() by setting to instance of class...
3590 class A(object):
3591 pass
3592
3593 A.__call__ = A()
3594 try:
3595 A()()
Yury Selivanovf488fb42015-07-03 01:04:23 -04003596 except RecursionError:
Georg Brandl479a7e72008-02-05 18:13:15 +00003597 pass
3598 else:
3599 self.fail("Recursion limit should have been reached for __call__()")
3600
3601 def test_delete_hook(self):
3602 # Testing __del__ hook...
3603 log = []
3604 class C(object):
3605 def __del__(self):
3606 log.append(1)
3607 c = C()
3608 self.assertEqual(log, [])
3609 del c
Benjamin Petersone549ead2009-03-28 21:42:05 +00003610 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00003611 self.assertEqual(log, [1])
3612
3613 class D(object): pass
3614 d = D()
3615 try: del d[0]
3616 except TypeError: pass
3617 else: self.fail("invalid del() didn't raise TypeError")
3618
3619 def test_hash_inheritance(self):
3620 # Testing hash of mutable subclasses...
3621
3622 class mydict(dict):
3623 pass
3624 d = mydict()
3625 try:
3626 hash(d)
Guido van Rossum8c842552002-03-14 23:05:54 +00003627 except TypeError:
3628 pass
3629 else:
Georg Brandl479a7e72008-02-05 18:13:15 +00003630 self.fail("hash() of dict subclass should fail")
3631
3632 class mylist(list):
3633 pass
3634 d = mylist()
Guido van Rossum8c842552002-03-14 23:05:54 +00003635 try:
Georg Brandl479a7e72008-02-05 18:13:15 +00003636 hash(d)
Guido van Rossum8c842552002-03-14 23:05:54 +00003637 except TypeError:
3638 pass
3639 else:
Georg Brandl479a7e72008-02-05 18:13:15 +00003640 self.fail("hash() of list subclass should fail")
3641
3642 def test_str_operations(self):
3643 try: 'a' + 5
3644 except TypeError: pass
3645 else: self.fail("'' + 5 doesn't raise TypeError")
3646
3647 try: ''.split('')
3648 except ValueError: pass
3649 else: self.fail("''.split('') doesn't raise ValueError")
3650
3651 try: ''.join([0])
3652 except TypeError: pass
3653 else: self.fail("''.join([0]) doesn't raise TypeError")
3654
3655 try: ''.rindex('5')
3656 except ValueError: pass
3657 else: self.fail("''.rindex('5') doesn't raise ValueError")
3658
3659 try: '%(n)s' % None
3660 except TypeError: pass
3661 else: self.fail("'%(n)s' % None doesn't raise TypeError")
3662
3663 try: '%(n' % {}
3664 except ValueError: pass
3665 else: self.fail("'%(n' % {} '' doesn't raise ValueError")
3666
3667 try: '%*s' % ('abc')
3668 except TypeError: pass
3669 else: self.fail("'%*s' % ('abc') doesn't raise TypeError")
3670
3671 try: '%*.*s' % ('abc', 5)
3672 except TypeError: pass
3673 else: self.fail("'%*.*s' % ('abc', 5) doesn't raise TypeError")
3674
3675 try: '%s' % (1, 2)
3676 except TypeError: pass
3677 else: self.fail("'%s' % (1, 2) doesn't raise TypeError")
3678
3679 try: '%' % None
3680 except ValueError: pass
3681 else: self.fail("'%' % None doesn't raise ValueError")
3682
3683 self.assertEqual('534253'.isdigit(), 1)
3684 self.assertEqual('534253x'.isdigit(), 0)
3685 self.assertEqual('%c' % 5, '\x05')
3686 self.assertEqual('%c' % '5', '5')
3687
3688 def test_deepcopy_recursive(self):
3689 # Testing deepcopy of recursive objects...
3690 class Node:
Guido van Rossum8c842552002-03-14 23:05:54 +00003691 pass
Georg Brandl479a7e72008-02-05 18:13:15 +00003692 a = Node()
3693 b = Node()
3694 a.b = b
3695 b.a = a
3696 z = deepcopy(a) # This blew up before
3697
Martin Panterf05641642016-05-08 13:48:10 +00003698 def test_uninitialized_modules(self):
Georg Brandl479a7e72008-02-05 18:13:15 +00003699 # Testing uninitialized module objects...
3700 from types import ModuleType as M
3701 m = M.__new__(M)
3702 str(m)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003703 self.assertNotHasAttr(m, "__name__")
3704 self.assertNotHasAttr(m, "__file__")
3705 self.assertNotHasAttr(m, "foo")
Benjamin Petersone549ead2009-03-28 21:42:05 +00003706 self.assertFalse(m.__dict__) # None or {} are both reasonable answers
Georg Brandl479a7e72008-02-05 18:13:15 +00003707 m.foo = 1
3708 self.assertEqual(m.__dict__, {"foo": 1})
3709
3710 def test_funny_new(self):
3711 # Testing __new__ returning something unexpected...
3712 class C(object):
3713 def __new__(cls, arg):
3714 if isinstance(arg, str): return [1, 2, 3]
3715 elif isinstance(arg, int): return object.__new__(D)
3716 else: return object.__new__(cls)
3717 class D(C):
3718 def __init__(self, arg):
3719 self.foo = arg
3720 self.assertEqual(C("1"), [1, 2, 3])
3721 self.assertEqual(D("1"), [1, 2, 3])
3722 d = D(None)
3723 self.assertEqual(d.foo, None)
3724 d = C(1)
Ezio Melottie9615932010-01-24 19:26:24 +00003725 self.assertIsInstance(d, D)
Georg Brandl479a7e72008-02-05 18:13:15 +00003726 self.assertEqual(d.foo, 1)
3727 d = D(1)
Ezio Melottie9615932010-01-24 19:26:24 +00003728 self.assertIsInstance(d, D)
Georg Brandl479a7e72008-02-05 18:13:15 +00003729 self.assertEqual(d.foo, 1)
3730
Serhiy Storchaka5adfac22016-12-02 08:42:43 +02003731 class C(object):
3732 @staticmethod
3733 def __new__(*args):
3734 return args
3735 self.assertEqual(C(1, 2), (C, 1, 2))
3736 class D(C):
3737 pass
3738 self.assertEqual(D(1, 2), (D, 1, 2))
3739
3740 class C(object):
3741 @classmethod
3742 def __new__(*args):
3743 return args
3744 self.assertEqual(C(1, 2), (C, C, 1, 2))
3745 class D(C):
3746 pass
3747 self.assertEqual(D(1, 2), (D, D, 1, 2))
3748
Georg Brandl479a7e72008-02-05 18:13:15 +00003749 def test_imul_bug(self):
3750 # Testing for __imul__ problems...
3751 # SF bug 544647
3752 class C(object):
3753 def __imul__(self, other):
3754 return (self, other)
Guido van Rossum8c842552002-03-14 23:05:54 +00003755 x = C()
Georg Brandl479a7e72008-02-05 18:13:15 +00003756 y = x
3757 y *= 1.0
3758 self.assertEqual(y, (x, 1.0))
3759 y = x
3760 y *= 2
3761 self.assertEqual(y, (x, 2))
3762 y = x
3763 y *= 3
3764 self.assertEqual(y, (x, 3))
3765 y = x
3766 y *= 1<<100
3767 self.assertEqual(y, (x, 1<<100))
3768 y = x
3769 y *= None
3770 self.assertEqual(y, (x, None))
3771 y = x
3772 y *= "foo"
3773 self.assertEqual(y, (x, "foo"))
Guido van Rossum8c842552002-03-14 23:05:54 +00003774
Georg Brandl479a7e72008-02-05 18:13:15 +00003775 def test_copy_setstate(self):
3776 # Testing that copy.*copy() correctly uses __setstate__...
3777 import copy
3778 class C(object):
3779 def __init__(self, foo=None):
3780 self.foo = foo
3781 self.__foo = foo
3782 def setfoo(self, foo=None):
3783 self.foo = foo
3784 def getfoo(self):
3785 return self.__foo
3786 def __getstate__(self):
3787 return [self.foo]
3788 def __setstate__(self_, lst):
3789 self.assertEqual(len(lst), 1)
3790 self_.__foo = self_.foo = lst[0]
3791 a = C(42)
3792 a.setfoo(24)
3793 self.assertEqual(a.foo, 24)
3794 self.assertEqual(a.getfoo(), 42)
3795 b = copy.copy(a)
3796 self.assertEqual(b.foo, 24)
3797 self.assertEqual(b.getfoo(), 24)
3798 b = copy.deepcopy(a)
3799 self.assertEqual(b.foo, 24)
3800 self.assertEqual(b.getfoo(), 24)
Guido van Rossum6cef6d52001-09-28 18:13:29 +00003801
Georg Brandl479a7e72008-02-05 18:13:15 +00003802 def test_slices(self):
3803 # Testing cases with slices and overridden __getitem__ ...
Guido van Rossum6cef6d52001-09-28 18:13:29 +00003804
Georg Brandl479a7e72008-02-05 18:13:15 +00003805 # Strings
3806 self.assertEqual("hello"[:4], "hell")
3807 self.assertEqual("hello"[slice(4)], "hell")
3808 self.assertEqual(str.__getitem__("hello", slice(4)), "hell")
3809 class S(str):
3810 def __getitem__(self, x):
3811 return str.__getitem__(self, x)
3812 self.assertEqual(S("hello")[:4], "hell")
3813 self.assertEqual(S("hello")[slice(4)], "hell")
3814 self.assertEqual(S("hello").__getitem__(slice(4)), "hell")
3815 # Tuples
3816 self.assertEqual((1,2,3)[:2], (1,2))
3817 self.assertEqual((1,2,3)[slice(2)], (1,2))
3818 self.assertEqual(tuple.__getitem__((1,2,3), slice(2)), (1,2))
3819 class T(tuple):
3820 def __getitem__(self, x):
3821 return tuple.__getitem__(self, x)
3822 self.assertEqual(T((1,2,3))[:2], (1,2))
3823 self.assertEqual(T((1,2,3))[slice(2)], (1,2))
3824 self.assertEqual(T((1,2,3)).__getitem__(slice(2)), (1,2))
3825 # Lists
3826 self.assertEqual([1,2,3][:2], [1,2])
3827 self.assertEqual([1,2,3][slice(2)], [1,2])
3828 self.assertEqual(list.__getitem__([1,2,3], slice(2)), [1,2])
3829 class L(list):
3830 def __getitem__(self, x):
3831 return list.__getitem__(self, x)
3832 self.assertEqual(L([1,2,3])[:2], [1,2])
3833 self.assertEqual(L([1,2,3])[slice(2)], [1,2])
3834 self.assertEqual(L([1,2,3]).__getitem__(slice(2)), [1,2])
3835 # Now do lists and __setitem__
3836 a = L([1,2,3])
3837 a[slice(1, 3)] = [3,2]
3838 self.assertEqual(a, [1,3,2])
3839 a[slice(0, 2, 1)] = [3,1]
3840 self.assertEqual(a, [3,1,2])
3841 a.__setitem__(slice(1, 3), [2,1])
3842 self.assertEqual(a, [3,2,1])
3843 a.__setitem__(slice(0, 2, 1), [2,3])
3844 self.assertEqual(a, [2,3,1])
Guido van Rossum6cef6d52001-09-28 18:13:29 +00003845
Georg Brandl479a7e72008-02-05 18:13:15 +00003846 def test_subtype_resurrection(self):
3847 # Testing resurrection of new-style instance...
Guido van Rossum6cef6d52001-09-28 18:13:29 +00003848
Georg Brandl479a7e72008-02-05 18:13:15 +00003849 class C(object):
3850 container = []
Tim Peters2f93e282001-10-04 05:27:00 +00003851
Georg Brandl479a7e72008-02-05 18:13:15 +00003852 def __del__(self):
3853 # resurrect the instance
3854 C.container.append(self)
Guido van Rossum4bb1e362001-09-28 23:49:48 +00003855
Georg Brandl479a7e72008-02-05 18:13:15 +00003856 c = C()
3857 c.attr = 42
Tim Petersfc57ccb2001-10-12 02:38:24 +00003858
Benjamin Petersone549ead2009-03-28 21:42:05 +00003859 # The most interesting thing here is whether this blows up, due to
3860 # flawed GC tracking logic in typeobject.c's call_finalizer() (a 2.2.1
3861 # bug).
Georg Brandl479a7e72008-02-05 18:13:15 +00003862 del c
Guido van Rossume7f3e242002-06-14 02:35:45 +00003863
Benjamin Petersone549ead2009-03-28 21:42:05 +00003864 support.gc_collect()
Georg Brandl479a7e72008-02-05 18:13:15 +00003865 self.assertEqual(len(C.container), 1)
Tim Petersfc57ccb2001-10-12 02:38:24 +00003866
Georg Brandl479a7e72008-02-05 18:13:15 +00003867 # Make c mortal again, so that the test framework with -l doesn't report
3868 # it as a leak.
3869 del C.__del__
Tim Petersfc57ccb2001-10-12 02:38:24 +00003870
Georg Brandl479a7e72008-02-05 18:13:15 +00003871 def test_slots_trash(self):
3872 # Testing slot trash...
3873 # Deallocating deeply nested slotted trash caused stack overflows
3874 class trash(object):
3875 __slots__ = ['x']
3876 def __init__(self, x):
3877 self.x = x
3878 o = None
3879 for i in range(50000):
3880 o = trash(o)
3881 del o
Tim Petersfc57ccb2001-10-12 02:38:24 +00003882
Georg Brandl479a7e72008-02-05 18:13:15 +00003883 def test_slots_multiple_inheritance(self):
3884 # SF bug 575229, multiple inheritance w/ slots dumps core
3885 class A(object):
3886 __slots__=()
3887 class B(object):
Walter Dörwalddbd2d252002-03-25 18:36:32 +00003888 pass
Georg Brandl479a7e72008-02-05 18:13:15 +00003889 class C(A,B) :
3890 __slots__=()
Benjamin Petersone549ead2009-03-28 21:42:05 +00003891 if support.check_impl_detail():
3892 self.assertEqual(C.__basicsize__, B.__basicsize__)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02003893 self.assertHasAttr(C, '__dict__')
3894 self.assertHasAttr(C, '__weakref__')
Georg Brandl479a7e72008-02-05 18:13:15 +00003895 C().x = 2
Walter Dörwalddbd2d252002-03-25 18:36:32 +00003896
Georg Brandl479a7e72008-02-05 18:13:15 +00003897 def test_rmul(self):
3898 # Testing correct invocation of __rmul__...
3899 # SF patch 592646
3900 class C(object):
3901 def __mul__(self, other):
3902 return "mul"
3903 def __rmul__(self, other):
3904 return "rmul"
3905 a = C()
3906 self.assertEqual(a*2, "mul")
3907 self.assertEqual(a*2.2, "mul")
3908 self.assertEqual(2*a, "rmul")
3909 self.assertEqual(2.2*a, "rmul")
3910
3911 def test_ipow(self):
3912 # Testing correct invocation of __ipow__...
3913 # [SF bug 620179]
3914 class C(object):
3915 def __ipow__(self, other):
3916 pass
3917 a = C()
3918 a **= 2
3919
Alexcc02b4f2021-02-26 21:58:39 +02003920 def test_ipow_returns_not_implemented(self):
3921 class A:
3922 def __ipow__(self, other):
3923 return NotImplemented
3924
3925 class B(A):
3926 def __rpow__(self, other):
3927 return 1
3928
3929 class C(A):
3930 def __pow__(self, other):
3931 return 2
3932 a = A()
3933 b = B()
3934 c = C()
3935
3936 a **= b
3937 self.assertEqual(a, 1)
3938
3939 c **= b
3940 self.assertEqual(c, 2)
3941
3942 def test_no_ipow(self):
3943 class B:
3944 def __rpow__(self, other):
3945 return 1
3946
3947 a = object()
3948 b = B()
3949 a **= b
3950 self.assertEqual(a, 1)
3951
3952 def test_ipow_exception_text(self):
3953 x = None
3954 with self.assertRaises(TypeError) as cm:
3955 x **= 2
3956 self.assertIn('unsupported operand type(s) for **=', str(cm.exception))
3957
3958 with self.assertRaises(TypeError) as cm:
3959 y = x ** 2
3960 self.assertIn('unsupported operand type(s) for **', str(cm.exception))
3961
Georg Brandl479a7e72008-02-05 18:13:15 +00003962 def test_mutable_bases(self):
3963 # Testing mutable bases...
3964
3965 # stuff that should work:
3966 class C(object):
Walter Dörwalddbd2d252002-03-25 18:36:32 +00003967 pass
Georg Brandl479a7e72008-02-05 18:13:15 +00003968 class C2(object):
3969 def __getattribute__(self, attr):
3970 if attr == 'a':
3971 return 2
3972 else:
3973 return super(C2, self).__getattribute__(attr)
3974 def meth(self):
3975 return 1
3976 class D(C):
Walter Dörwalddbd2d252002-03-25 18:36:32 +00003977 pass
Georg Brandl479a7e72008-02-05 18:13:15 +00003978 class E(D):
Guido van Rossum6e5680f2002-10-15 01:01:53 +00003979 pass
Georg Brandl479a7e72008-02-05 18:13:15 +00003980 d = D()
3981 e = E()
3982 D.__bases__ = (C,)
3983 D.__bases__ = (C2,)
3984 self.assertEqual(d.meth(), 1)
3985 self.assertEqual(e.meth(), 1)
3986 self.assertEqual(d.a, 2)
3987 self.assertEqual(e.a, 2)
3988 self.assertEqual(C2.__subclasses__(), [D])
Guido van Rossum6e5680f2002-10-15 01:01:53 +00003989
Georg Brandl479a7e72008-02-05 18:13:15 +00003990 try:
3991 del D.__bases__
Benjamin Petersone549ead2009-03-28 21:42:05 +00003992 except (TypeError, AttributeError):
Georg Brandl479a7e72008-02-05 18:13:15 +00003993 pass
3994 else:
3995 self.fail("shouldn't be able to delete .__bases__")
Michael W. Hudson98bbc492002-11-26 14:47:27 +00003996
Georg Brandl479a7e72008-02-05 18:13:15 +00003997 try:
3998 D.__bases__ = ()
3999 except TypeError as msg:
4000 if str(msg) == "a new-style class can't have only classic bases":
4001 self.fail("wrong error message for .__bases__ = ()")
4002 else:
4003 self.fail("shouldn't be able to set .__bases__ to ()")
Michael W. Hudson98bbc492002-11-26 14:47:27 +00004004
Georg Brandl479a7e72008-02-05 18:13:15 +00004005 try:
4006 D.__bases__ = (D,)
4007 except TypeError:
4008 pass
4009 else:
4010 # actually, we'll have crashed by here...
4011 self.fail("shouldn't be able to create inheritance cycles")
Thomas Wouters89f507f2006-12-13 04:49:30 +00004012
Georg Brandl479a7e72008-02-05 18:13:15 +00004013 try:
4014 D.__bases__ = (C, C)
4015 except TypeError:
4016 pass
4017 else:
4018 self.fail("didn't detect repeated base classes")
Michael W. Hudson98bbc492002-11-26 14:47:27 +00004019
Georg Brandl479a7e72008-02-05 18:13:15 +00004020 try:
4021 D.__bases__ = (E,)
4022 except TypeError:
4023 pass
4024 else:
4025 self.fail("shouldn't be able to create inheritance cycles")
Guido van Rossum3bbc0ee2002-12-13 17:49:38 +00004026
Benjamin Petersonae937c02009-04-18 20:54:08 +00004027 def test_builtin_bases(self):
4028 # Make sure all the builtin types can have their base queried without
4029 # segfaulting. See issue #5787.
4030 builtin_types = [tp for tp in builtins.__dict__.values()
4031 if isinstance(tp, type)]
4032 for tp in builtin_types:
4033 object.__getattribute__(tp, "__bases__")
4034 if tp is not object:
4035 self.assertEqual(len(tp.__bases__), 1, tp)
4036
Benjamin Peterson25c95f12009-05-08 20:42:26 +00004037 class L(list):
4038 pass
4039
4040 class C(object):
4041 pass
4042
4043 class D(C):
4044 pass
4045
4046 try:
4047 L.__bases__ = (dict,)
4048 except TypeError:
4049 pass
4050 else:
4051 self.fail("shouldn't turn list subclass into dict subclass")
4052
4053 try:
4054 list.__bases__ = (dict,)
4055 except TypeError:
4056 pass
4057 else:
4058 self.fail("shouldn't be able to assign to list.__bases__")
4059
4060 try:
4061 D.__bases__ = (C, list)
4062 except TypeError:
4063 pass
4064 else:
4065 assert 0, "best_base calculation found wanting"
4066
Benjamin Petersonbd6c41a2015-10-06 19:36:54 -07004067 def test_unsubclassable_types(self):
4068 with self.assertRaises(TypeError):
4069 class X(type(None)):
4070 pass
4071 with self.assertRaises(TypeError):
4072 class X(object, type(None)):
4073 pass
4074 with self.assertRaises(TypeError):
4075 class X(type(None), object):
4076 pass
4077 class O(object):
4078 pass
4079 with self.assertRaises(TypeError):
4080 class X(O, type(None)):
4081 pass
4082 with self.assertRaises(TypeError):
4083 class X(type(None), O):
4084 pass
4085
4086 class X(object):
4087 pass
4088 with self.assertRaises(TypeError):
4089 X.__bases__ = type(None),
4090 with self.assertRaises(TypeError):
4091 X.__bases__ = object, type(None)
4092 with self.assertRaises(TypeError):
4093 X.__bases__ = type(None), object
4094 with self.assertRaises(TypeError):
4095 X.__bases__ = O, type(None)
4096 with self.assertRaises(TypeError):
4097 X.__bases__ = type(None), O
Benjamin Petersonae937c02009-04-18 20:54:08 +00004098
Georg Brandl479a7e72008-02-05 18:13:15 +00004099 def test_mutable_bases_with_failing_mro(self):
4100 # Testing mutable bases with failing mro...
4101 class WorkOnce(type):
4102 def __new__(self, name, bases, ns):
4103 self.flag = 0
4104 return super(WorkOnce, self).__new__(WorkOnce, name, bases, ns)
4105 def mro(self):
4106 if self.flag > 0:
4107 raise RuntimeError("bozo")
4108 else:
4109 self.flag += 1
4110 return type.mro(self)
Michael W. Hudson98bbc492002-11-26 14:47:27 +00004111
Georg Brandl479a7e72008-02-05 18:13:15 +00004112 class WorkAlways(type):
4113 def mro(self):
4114 # this is here to make sure that .mro()s aren't called
4115 # with an exception set (which was possible at one point).
4116 # An error message will be printed in a debug build.
4117 # What's a good way to test for this?
Michael W. Hudson586da8f2002-11-27 15:20:19 +00004118 return type.mro(self)
4119
Georg Brandl479a7e72008-02-05 18:13:15 +00004120 class C(object):
4121 pass
Michael W. Hudson586da8f2002-11-27 15:20:19 +00004122
Georg Brandl479a7e72008-02-05 18:13:15 +00004123 class C2(object):
4124 pass
Michael W. Hudson586da8f2002-11-27 15:20:19 +00004125
Georg Brandl479a7e72008-02-05 18:13:15 +00004126 class D(C):
4127 pass
Michael W. Hudson586da8f2002-11-27 15:20:19 +00004128
Georg Brandl479a7e72008-02-05 18:13:15 +00004129 class E(D):
4130 pass
Michael W. Hudson586da8f2002-11-27 15:20:19 +00004131
Georg Brandl479a7e72008-02-05 18:13:15 +00004132 class F(D, metaclass=WorkOnce):
4133 pass
Michael W. Hudson586da8f2002-11-27 15:20:19 +00004134
Georg Brandl479a7e72008-02-05 18:13:15 +00004135 class G(D, metaclass=WorkAlways):
4136 pass
Michael W. Hudson586da8f2002-11-27 15:20:19 +00004137
Georg Brandl479a7e72008-02-05 18:13:15 +00004138 # Immediate subclasses have their mro's adjusted in alphabetical
4139 # order, so E's will get adjusted before adjusting F's fails. We
4140 # check here that E's gets restored.
Michael W. Hudson586da8f2002-11-27 15:20:19 +00004141
Georg Brandl479a7e72008-02-05 18:13:15 +00004142 E_mro_before = E.__mro__
4143 D_mro_before = D.__mro__
Armin Rigofd163f92005-12-29 15:59:19 +00004144
Armin Rigofd163f92005-12-29 15:59:19 +00004145 try:
Georg Brandl479a7e72008-02-05 18:13:15 +00004146 D.__bases__ = (C2,)
4147 except RuntimeError:
4148 self.assertEqual(E.__mro__, E_mro_before)
4149 self.assertEqual(D.__mro__, D_mro_before)
4150 else:
4151 self.fail("exception not propagated")
4152
4153 def test_mutable_bases_catch_mro_conflict(self):
4154 # Testing mutable bases catch mro conflict...
4155 class A(object):
4156 pass
4157
4158 class B(object):
4159 pass
4160
4161 class C(A, B):
4162 pass
4163
4164 class D(A, B):
4165 pass
4166
4167 class E(C, D):
4168 pass
4169
4170 try:
4171 C.__bases__ = (B, A)
Armin Rigofd163f92005-12-29 15:59:19 +00004172 except TypeError:
4173 pass
4174 else:
Georg Brandl479a7e72008-02-05 18:13:15 +00004175 self.fail("didn't catch MRO conflict")
Armin Rigofd163f92005-12-29 15:59:19 +00004176
Georg Brandl479a7e72008-02-05 18:13:15 +00004177 def test_mutable_names(self):
4178 # Testing mutable names...
4179 class C(object):
4180 pass
4181
4182 # C.__module__ could be 'test_descr' or '__main__'
4183 mod = C.__module__
4184
4185 C.__name__ = 'D'
4186 self.assertEqual((C.__module__, C.__name__), (mod, 'D'))
4187
4188 C.__name__ = 'D.E'
4189 self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))
4190
Mark Dickinson64aafeb2013-04-13 15:26:58 +01004191 def test_evil_type_name(self):
4192 # A badly placed Py_DECREF in type_set_name led to arbitrary code
4193 # execution while the type structure was not in a sane state, and a
4194 # possible segmentation fault as a result. See bug #16447.
4195 class Nasty(str):
4196 def __del__(self):
4197 C.__name__ = "other"
4198
4199 class C:
4200 pass
4201
4202 C.__name__ = Nasty("abc")
4203 C.__name__ = "normal"
4204
Georg Brandl479a7e72008-02-05 18:13:15 +00004205 def test_subclass_right_op(self):
4206 # Testing correct dispatch of subclass overloading __r<op>__...
4207
4208 # This code tests various cases where right-dispatch of a subclass
4209 # should be preferred over left-dispatch of a base class.
4210
4211 # Case 1: subclass of int; this tests code in abstract.c::binary_op1()
4212
4213 class B(int):
4214 def __floordiv__(self, other):
4215 return "B.__floordiv__"
4216 def __rfloordiv__(self, other):
4217 return "B.__rfloordiv__"
4218
4219 self.assertEqual(B(1) // 1, "B.__floordiv__")
4220 self.assertEqual(1 // B(1), "B.__rfloordiv__")
4221
4222 # Case 2: subclass of object; this is just the baseline for case 3
4223
4224 class C(object):
4225 def __floordiv__(self, other):
4226 return "C.__floordiv__"
4227 def __rfloordiv__(self, other):
4228 return "C.__rfloordiv__"
4229
4230 self.assertEqual(C() // 1, "C.__floordiv__")
4231 self.assertEqual(1 // C(), "C.__rfloordiv__")
4232
4233 # Case 3: subclass of new-style class; here it gets interesting
4234
4235 class D(C):
4236 def __floordiv__(self, other):
4237 return "D.__floordiv__"
4238 def __rfloordiv__(self, other):
4239 return "D.__rfloordiv__"
4240
4241 self.assertEqual(D() // C(), "D.__floordiv__")
4242 self.assertEqual(C() // D(), "D.__rfloordiv__")
4243
4244 # Case 4: this didn't work right in 2.2.2 and 2.3a1
4245
4246 class E(C):
4247 pass
4248
4249 self.assertEqual(E.__rfloordiv__, C.__rfloordiv__)
4250
4251 self.assertEqual(E() // 1, "C.__floordiv__")
4252 self.assertEqual(1 // E(), "C.__rfloordiv__")
4253 self.assertEqual(E() // C(), "C.__floordiv__")
4254 self.assertEqual(C() // E(), "C.__floordiv__") # This one would fail
4255
Benjamin Petersone549ead2009-03-28 21:42:05 +00004256 @support.impl_detail("testing an internal kind of method object")
Georg Brandl479a7e72008-02-05 18:13:15 +00004257 def test_meth_class_get(self):
4258 # Testing __get__ method of METH_CLASS C methods...
4259 # Full coverage of descrobject.c::classmethod_get()
4260
4261 # Baseline
4262 arg = [1, 2, 3]
4263 res = {1: None, 2: None, 3: None}
4264 self.assertEqual(dict.fromkeys(arg), res)
4265 self.assertEqual({}.fromkeys(arg), res)
4266
4267 # Now get the descriptor
4268 descr = dict.__dict__["fromkeys"]
4269
4270 # More baseline using the descriptor directly
4271 self.assertEqual(descr.__get__(None, dict)(arg), res)
4272 self.assertEqual(descr.__get__({})(arg), res)
4273
4274 # Now check various error cases
4275 try:
4276 descr.__get__(None, None)
4277 except TypeError:
4278 pass
4279 else:
4280 self.fail("shouldn't have allowed descr.__get__(None, None)")
4281 try:
4282 descr.__get__(42)
4283 except TypeError:
4284 pass
4285 else:
4286 self.fail("shouldn't have allowed descr.__get__(42)")
4287 try:
4288 descr.__get__(None, 42)
4289 except TypeError:
4290 pass
4291 else:
4292 self.fail("shouldn't have allowed descr.__get__(None, 42)")
4293 try:
4294 descr.__get__(None, int)
4295 except TypeError:
4296 pass
4297 else:
4298 self.fail("shouldn't have allowed descr.__get__(None, int)")
4299
4300 def test_isinst_isclass(self):
4301 # Testing proxy isinstance() and isclass()...
4302 class Proxy(object):
4303 def __init__(self, obj):
4304 self.__obj = obj
4305 def __getattribute__(self, name):
4306 if name.startswith("_Proxy__"):
4307 return object.__getattribute__(self, name)
4308 else:
4309 return getattr(self.__obj, name)
4310 # Test with a classic class
4311 class C:
4312 pass
4313 a = C()
4314 pa = Proxy(a)
Ezio Melottie9615932010-01-24 19:26:24 +00004315 self.assertIsInstance(a, C) # Baseline
4316 self.assertIsInstance(pa, C) # Test
Georg Brandl479a7e72008-02-05 18:13:15 +00004317 # Test with a classic subclass
4318 class D(C):
4319 pass
4320 a = D()
4321 pa = Proxy(a)
Ezio Melottie9615932010-01-24 19:26:24 +00004322 self.assertIsInstance(a, C) # Baseline
4323 self.assertIsInstance(pa, C) # Test
Georg Brandl479a7e72008-02-05 18:13:15 +00004324 # Test with a new-style class
4325 class C(object):
4326 pass
4327 a = C()
4328 pa = Proxy(a)
Ezio Melottie9615932010-01-24 19:26:24 +00004329 self.assertIsInstance(a, C) # Baseline
4330 self.assertIsInstance(pa, C) # Test
Georg Brandl479a7e72008-02-05 18:13:15 +00004331 # Test with a new-style subclass
4332 class D(C):
4333 pass
4334 a = D()
4335 pa = Proxy(a)
Ezio Melottie9615932010-01-24 19:26:24 +00004336 self.assertIsInstance(a, C) # Baseline
4337 self.assertIsInstance(pa, C) # Test
Georg Brandl479a7e72008-02-05 18:13:15 +00004338
4339 def test_proxy_super(self):
4340 # Testing super() for a proxy object...
4341 class Proxy(object):
4342 def __init__(self, obj):
4343 self.__obj = obj
4344 def __getattribute__(self, name):
4345 if name.startswith("_Proxy__"):
4346 return object.__getattribute__(self, name)
4347 else:
4348 return getattr(self.__obj, name)
4349
4350 class B(object):
4351 def f(self):
4352 return "B.f"
4353
4354 class C(B):
4355 def f(self):
4356 return super(C, self).f() + "->C.f"
4357
4358 obj = C()
4359 p = Proxy(obj)
4360 self.assertEqual(C.__dict__["f"](p), "B.f->C.f")
4361
4362 def test_carloverre(self):
4363 # Testing prohibition of Carlo Verre's hack...
4364 try:
4365 object.__setattr__(str, "foo", 42)
4366 except TypeError:
4367 pass
4368 else:
Ezio Melotti13925002011-03-16 11:05:33 +02004369 self.fail("Carlo Verre __setattr__ succeeded!")
Georg Brandl479a7e72008-02-05 18:13:15 +00004370 try:
4371 object.__delattr__(str, "lower")
4372 except TypeError:
4373 pass
4374 else:
4375 self.fail("Carlo Verre __delattr__ succeeded!")
4376
scoderc53b3102020-07-18 23:19:50 +02004377 def test_carloverre_multi_inherit_valid(self):
4378 class A(type):
4379 def __setattr__(cls, key, value):
4380 type.__setattr__(cls, key, value)
4381
4382 class B:
4383 pass
4384
4385 class C(B, A):
4386 pass
4387
4388 obj = C('D', (object,), {})
4389 try:
4390 obj.test = True
4391 except TypeError:
4392 self.fail("setattr through direct base types should be legal")
4393
4394 def test_carloverre_multi_inherit_invalid(self):
4395 class A(type):
4396 def __setattr__(cls, key, value):
4397 object.__setattr__(cls, key, value) # this should fail!
4398
4399 class B:
4400 pass
4401
4402 class C(B, A):
4403 pass
4404
4405 obj = C('D', (object,), {})
4406 try:
4407 obj.test = True
4408 except TypeError:
4409 pass
4410 else:
4411 self.fail("setattr through indirect base types should be rejected")
4412
Georg Brandl479a7e72008-02-05 18:13:15 +00004413 def test_weakref_segfault(self):
4414 # Testing weakref segfault...
4415 # SF 742911
4416 import weakref
4417
4418 class Provoker:
4419 def __init__(self, referrent):
4420 self.ref = weakref.ref(referrent)
4421
4422 def __del__(self):
4423 x = self.ref()
4424
4425 class Oops(object):
4426 pass
4427
4428 o = Oops()
4429 o.whatever = Provoker(o)
4430 del o
4431
4432 def test_wrapper_segfault(self):
4433 # SF 927248: deeply nested wrappers could cause stack overflow
4434 f = lambda:None
4435 for i in range(1000000):
4436 f = f.__call__
4437 f = None
4438
4439 def test_file_fault(self):
4440 # Testing sys.stdout is changed in getattr...
Nick Coghlan6ead5522009-10-18 13:19:33 +00004441 test_stdout = sys.stdout
Georg Brandl479a7e72008-02-05 18:13:15 +00004442 class StdoutGuard:
4443 def __getattr__(self, attr):
4444 sys.stdout = sys.__stdout__
4445 raise RuntimeError("Premature access to sys.stdout.%s" % attr)
4446 sys.stdout = StdoutGuard()
4447 try:
4448 print("Oops!")
4449 except RuntimeError:
4450 pass
Nick Coghlan6ead5522009-10-18 13:19:33 +00004451 finally:
4452 sys.stdout = test_stdout
Georg Brandl479a7e72008-02-05 18:13:15 +00004453
4454 def test_vicious_descriptor_nonsense(self):
4455 # Testing vicious_descriptor_nonsense...
4456
4457 # A potential segfault spotted by Thomas Wouters in mail to
4458 # python-dev 2003-04-17, turned into an example & fixed by Michael
4459 # Hudson just less than four months later...
4460
4461 class Evil(object):
4462 def __hash__(self):
4463 return hash('attr')
4464 def __eq__(self, other):
Serhiy Storchakaff3d39f2019-02-26 08:03:21 +02004465 try:
4466 del C.attr
4467 except AttributeError:
4468 # possible race condition
4469 pass
Georg Brandl479a7e72008-02-05 18:13:15 +00004470 return 0
4471
4472 class Descr(object):
4473 def __get__(self, ob, type=None):
4474 return 1
4475
4476 class C(object):
4477 attr = Descr()
4478
4479 c = C()
4480 c.__dict__[Evil()] = 0
4481
4482 self.assertEqual(c.attr, 1)
4483 # this makes a crash more likely:
Benjamin Petersone549ead2009-03-28 21:42:05 +00004484 support.gc_collect()
Serhiy Storchaka76edd212013-11-17 23:38:50 +02004485 self.assertNotHasAttr(c, 'attr')
Georg Brandl479a7e72008-02-05 18:13:15 +00004486
4487 def test_init(self):
4488 # SF 1155938
4489 class Foo(object):
4490 def __init__(self):
4491 return 10
4492 try:
4493 Foo()
4494 except TypeError:
4495 pass
4496 else:
4497 self.fail("did not test __init__() for None return")
4498
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03004499 def assertNotOrderable(self, a, b):
4500 with self.assertRaises(TypeError):
4501 a < b
4502 with self.assertRaises(TypeError):
4503 a > b
4504 with self.assertRaises(TypeError):
4505 a <= b
4506 with self.assertRaises(TypeError):
4507 a >= b
4508
Georg Brandl479a7e72008-02-05 18:13:15 +00004509 def test_method_wrapper(self):
4510 # Testing method-wrapper objects...
4511 # <type 'method-wrapper'> did not support any reflection before 2.5
Georg Brandl479a7e72008-02-05 18:13:15 +00004512 l = []
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03004513 self.assertTrue(l.__add__ == l.__add__)
4514 self.assertFalse(l.__add__ != l.__add__)
4515 self.assertFalse(l.__add__ == [].__add__)
4516 self.assertTrue(l.__add__ != [].__add__)
4517 self.assertFalse(l.__add__ == l.__mul__)
4518 self.assertTrue(l.__add__ != l.__mul__)
4519 self.assertNotOrderable(l.__add__, l.__add__)
Serhiy Storchaka76edd212013-11-17 23:38:50 +02004520 self.assertEqual(l.__add__.__name__, '__add__')
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03004521 self.assertIs(l.__add__.__self__, l)
4522 self.assertIs(l.__add__.__objclass__, list)
Georg Brandl479a7e72008-02-05 18:13:15 +00004523 self.assertEqual(l.__add__.__doc__, list.__add__.__doc__)
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03004524 # hash([].__add__) should not be based on hash([])
4525 hash(l.__add__)
Georg Brandl479a7e72008-02-05 18:13:15 +00004526
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03004527 def test_builtin_function_or_method(self):
4528 # Not really belonging to test_descr, but introspection and
4529 # comparison on <type 'builtin_function_or_method'> seems not
4530 # to be tested elsewhere
4531 l = []
4532 self.assertTrue(l.append == l.append)
4533 self.assertFalse(l.append != l.append)
4534 self.assertFalse(l.append == [].append)
4535 self.assertTrue(l.append != [].append)
4536 self.assertFalse(l.append == l.pop)
4537 self.assertTrue(l.append != l.pop)
4538 self.assertNotOrderable(l.append, l.append)
4539 self.assertEqual(l.append.__name__, 'append')
4540 self.assertIs(l.append.__self__, l)
4541 # self.assertIs(l.append.__objclass__, list) --- could be added?
4542 self.assertEqual(l.append.__doc__, list.append.__doc__)
4543 # hash([].append) should not be based on hash([])
4544 hash(l.append)
4545
4546 def test_special_unbound_method_types(self):
4547 # Testing objects of <type 'wrapper_descriptor'>...
4548 self.assertTrue(list.__add__ == list.__add__)
4549 self.assertFalse(list.__add__ != list.__add__)
4550 self.assertFalse(list.__add__ == list.__mul__)
4551 self.assertTrue(list.__add__ != list.__mul__)
4552 self.assertNotOrderable(list.__add__, list.__add__)
4553 self.assertEqual(list.__add__.__name__, '__add__')
4554 self.assertIs(list.__add__.__objclass__, list)
4555
4556 # Testing objects of <type 'method_descriptor'>...
4557 self.assertTrue(list.append == list.append)
4558 self.assertFalse(list.append != list.append)
4559 self.assertFalse(list.append == list.pop)
4560 self.assertTrue(list.append != list.pop)
4561 self.assertNotOrderable(list.append, list.append)
4562 self.assertEqual(list.append.__name__, 'append')
4563 self.assertIs(list.append.__objclass__, list)
Georg Brandl479a7e72008-02-05 18:13:15 +00004564
4565 def test_not_implemented(self):
4566 # Testing NotImplemented...
4567 # all binary methods should be able to return a NotImplemented
Georg Brandl479a7e72008-02-05 18:13:15 +00004568 import operator
4569
4570 def specialmethod(self, other):
4571 return NotImplemented
4572
4573 def check(expr, x, y):
4574 try:
4575 exec(expr, {'x': x, 'y': y, 'operator': operator})
4576 except TypeError:
4577 pass
4578 else:
4579 self.fail("no TypeError from %r" % (expr,))
4580
4581 N1 = sys.maxsize + 1 # might trigger OverflowErrors instead of
4582 # TypeErrors
4583 N2 = sys.maxsize # if sizeof(int) < sizeof(long), might trigger
4584 # ValueErrors instead of TypeErrors
Armin Rigofd163f92005-12-29 15:59:19 +00004585 for name, expr, iexpr in [
4586 ('__add__', 'x + y', 'x += y'),
4587 ('__sub__', 'x - y', 'x -= y'),
4588 ('__mul__', 'x * y', 'x *= y'),
Benjamin Petersond51374e2014-04-09 23:55:56 -04004589 ('__matmul__', 'x @ y', 'x @= y'),
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +02004590 ('__truediv__', 'x / y', 'x /= y'),
4591 ('__floordiv__', 'x // y', 'x //= y'),
Armin Rigofd163f92005-12-29 15:59:19 +00004592 ('__mod__', 'x % y', 'x %= y'),
4593 ('__divmod__', 'divmod(x, y)', None),
4594 ('__pow__', 'x ** y', 'x **= y'),
4595 ('__lshift__', 'x << y', 'x <<= y'),
4596 ('__rshift__', 'x >> y', 'x >>= y'),
4597 ('__and__', 'x & y', 'x &= y'),
4598 ('__or__', 'x | y', 'x |= y'),
Georg Brandl479a7e72008-02-05 18:13:15 +00004599 ('__xor__', 'x ^ y', 'x ^= y')]:
Neal Norwitz4886cc32006-08-21 17:06:07 +00004600 rname = '__r' + name[2:]
Georg Brandl479a7e72008-02-05 18:13:15 +00004601 A = type('A', (), {name: specialmethod})
Armin Rigofd163f92005-12-29 15:59:19 +00004602 a = A()
Armin Rigofd163f92005-12-29 15:59:19 +00004603 check(expr, a, a)
Armin Rigofd163f92005-12-29 15:59:19 +00004604 check(expr, a, N1)
4605 check(expr, a, N2)
Armin Rigofd163f92005-12-29 15:59:19 +00004606 if iexpr:
4607 check(iexpr, a, a)
Armin Rigofd163f92005-12-29 15:59:19 +00004608 check(iexpr, a, N1)
4609 check(iexpr, a, N2)
4610 iname = '__i' + name[2:]
Georg Brandl479a7e72008-02-05 18:13:15 +00004611 C = type('C', (), {iname: specialmethod})
Armin Rigofd163f92005-12-29 15:59:19 +00004612 c = C()
4613 check(iexpr, c, a)
Armin Rigofd163f92005-12-29 15:59:19 +00004614 check(iexpr, c, N1)
4615 check(iexpr, c, N2)
4616
Georg Brandl479a7e72008-02-05 18:13:15 +00004617 def test_assign_slice(self):
4618 # ceval.c's assign_slice used to check for
4619 # tp->tp_as_sequence->sq_slice instead of
4620 # tp->tp_as_sequence->sq_ass_slice
Guido van Rossumd8faa362007-04-27 19:54:29 +00004621
Georg Brandl479a7e72008-02-05 18:13:15 +00004622 class C(object):
4623 def __setitem__(self, idx, value):
4624 self.value = value
Guido van Rossumd8faa362007-04-27 19:54:29 +00004625
Georg Brandl479a7e72008-02-05 18:13:15 +00004626 c = C()
4627 c[1:2] = 3
4628 self.assertEqual(c.value, 3)
Guido van Rossumd8faa362007-04-27 19:54:29 +00004629
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +00004630 def test_set_and_no_get(self):
4631 # See
4632 # http://mail.python.org/pipermail/python-dev/2010-January/095637.html
4633 class Descr(object):
4634
4635 def __init__(self, name):
4636 self.name = name
4637
4638 def __set__(self, obj, value):
4639 obj.__dict__[self.name] = value
4640 descr = Descr("a")
4641
4642 class X(object):
4643 a = descr
4644
4645 x = X()
4646 self.assertIs(x.a, descr)
4647 x.a = 42
4648 self.assertEqual(x.a, 42)
4649
Benjamin Peterson21896a32010-03-21 22:03:03 +00004650 # Also check type_getattro for correctness.
4651 class Meta(type):
4652 pass
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +02004653 class X(metaclass=Meta):
4654 pass
Benjamin Peterson21896a32010-03-21 22:03:03 +00004655 X.a = 42
4656 Meta.a = Descr("a")
4657 self.assertEqual(X.a, 42)
4658
Benjamin Peterson9262b842008-11-17 22:45:50 +00004659 def test_getattr_hooks(self):
4660 # issue 4230
4661
4662 class Descriptor(object):
4663 counter = 0
4664 def __get__(self, obj, objtype=None):
4665 def getter(name):
4666 self.counter += 1
4667 raise AttributeError(name)
4668 return getter
4669
4670 descr = Descriptor()
4671 class A(object):
4672 __getattribute__ = descr
4673 class B(object):
4674 __getattr__ = descr
4675 class C(object):
4676 __getattribute__ = descr
4677 __getattr__ = descr
4678
4679 self.assertRaises(AttributeError, getattr, A(), "attr")
Ezio Melottib3aedd42010-11-20 19:04:17 +00004680 self.assertEqual(descr.counter, 1)
Benjamin Peterson9262b842008-11-17 22:45:50 +00004681 self.assertRaises(AttributeError, getattr, B(), "attr")
Ezio Melottib3aedd42010-11-20 19:04:17 +00004682 self.assertEqual(descr.counter, 2)
Benjamin Peterson9262b842008-11-17 22:45:50 +00004683 self.assertRaises(AttributeError, getattr, C(), "attr")
Ezio Melottib3aedd42010-11-20 19:04:17 +00004684 self.assertEqual(descr.counter, 4)
Benjamin Peterson9262b842008-11-17 22:45:50 +00004685
Benjamin Peterson9262b842008-11-17 22:45:50 +00004686 class EvilGetattribute(object):
4687 # This used to segfault
4688 def __getattr__(self, name):
4689 raise AttributeError(name)
4690 def __getattribute__(self, name):
4691 del EvilGetattribute.__getattr__
4692 for i in range(5):
4693 gc.collect()
4694 raise AttributeError(name)
4695
4696 self.assertRaises(AttributeError, getattr, EvilGetattribute(), "attr")
4697
Benjamin Peterson16d84ac2012-03-16 09:32:59 -05004698 def test_type___getattribute__(self):
4699 self.assertRaises(TypeError, type.__getattribute__, list, type)
4700
Benjamin Peterson477ba912011-01-12 15:34:01 +00004701 def test_abstractmethods(self):
Benjamin Peterson5e8dada2011-01-12 15:25:02 +00004702 # type pretends not to have __abstractmethods__.
4703 self.assertRaises(AttributeError, getattr, type, "__abstractmethods__")
4704 class meta(type):
4705 pass
4706 self.assertRaises(AttributeError, getattr, meta, "__abstractmethods__")
Benjamin Peterson477ba912011-01-12 15:34:01 +00004707 class X(object):
4708 pass
4709 with self.assertRaises(AttributeError):
4710 del X.__abstractmethods__
Benjamin Peterson5e8dada2011-01-12 15:25:02 +00004711
Victor Stinner3249dec2011-05-01 23:19:15 +02004712 def test_proxy_call(self):
4713 class FakeStr:
4714 __class__ = str
4715
4716 fake_str = FakeStr()
4717 # isinstance() reads __class__
Serhiy Storchaka76edd212013-11-17 23:38:50 +02004718 self.assertIsInstance(fake_str, str)
Victor Stinner3249dec2011-05-01 23:19:15 +02004719
4720 # call a method descriptor
4721 with self.assertRaises(TypeError):
4722 str.split(fake_str)
4723
4724 # call a slot wrapper descriptor
4725 with self.assertRaises(TypeError):
4726 str.__add__(fake_str, "abc")
4727
Antoine Pitrou8cdc40e2011-07-15 21:15:07 +02004728 def test_repr_as_str(self):
4729 # Issue #11603: crash or infinite loop when rebinding __str__ as
4730 # __repr__.
4731 class Foo:
4732 pass
4733 Foo.__repr__ = Foo.__str__
4734 foo = Foo()
Yury Selivanovf488fb42015-07-03 01:04:23 -04004735 self.assertRaises(RecursionError, str, foo)
4736 self.assertRaises(RecursionError, repr, foo)
Benjamin Peterson7b166872012-04-24 11:06:25 -04004737
4738 def test_mixing_slot_wrappers(self):
4739 class X(dict):
4740 __setattr__ = dict.__setitem__
Jeroen Demeyer2e9954d2019-06-17 13:53:21 +02004741 __neg__ = dict.copy
Benjamin Peterson7b166872012-04-24 11:06:25 -04004742 x = X()
4743 x.y = 42
4744 self.assertEqual(x["y"], 42)
Jeroen Demeyer2e9954d2019-06-17 13:53:21 +02004745 self.assertEqual(x, -x)
Christian Heimesbbffeb62008-01-24 09:42:52 +00004746
Jeroen Demeyer57ea3352019-09-10 13:21:57 +02004747 def test_wrong_class_slot_wrapper(self):
4748 # Check bpo-37619: a wrapper descriptor taken from the wrong class
4749 # should raise an exception instead of silently being ignored
4750 class A(int):
4751 __eq__ = str.__eq__
4752 __add__ = str.__add__
4753 a = A()
4754 with self.assertRaises(TypeError):
4755 a == a
4756 with self.assertRaises(TypeError):
4757 a + a
4758
Benjamin Petersonaf3dcd22011-08-17 11:48:23 -05004759 def test_slot_shadows_class_variable(self):
Benjamin Petersonc4085c82011-08-16 18:53:26 -05004760 with self.assertRaises(ValueError) as cm:
4761 class X:
4762 __slots__ = ["foo"]
4763 foo = None
4764 m = str(cm.exception)
4765 self.assertEqual("'foo' in __slots__ conflicts with class variable", m)
4766
Benjamin Peterson01fc6cd2011-08-17 12:03:47 -05004767 def test_set_doc(self):
4768 class X:
4769 "elephant"
4770 X.__doc__ = "banana"
4771 self.assertEqual(X.__doc__, "banana")
Erlend Egeberg Aasland64141382021-04-30 15:25:43 +02004772
Benjamin Peterson01fc6cd2011-08-17 12:03:47 -05004773 with self.assertRaises(TypeError) as cm:
4774 type(list).__dict__["__doc__"].__set__(list, "blah")
Erlend Egeberg Aasland64141382021-04-30 15:25:43 +02004775 self.assertIn("cannot set '__doc__' attribute of immutable type 'list'", str(cm.exception))
4776
Benjamin Peterson01fc6cd2011-08-17 12:03:47 -05004777 with self.assertRaises(TypeError) as cm:
4778 type(X).__dict__["__doc__"].__delete__(X)
Erlend Egeberg Aasland64141382021-04-30 15:25:43 +02004779 self.assertIn("cannot delete '__doc__' attribute of immutable type 'X'", str(cm.exception))
Benjamin Peterson01fc6cd2011-08-17 12:03:47 -05004780 self.assertEqual(X.__doc__, "banana")
4781
Antoine Pitrou9d574812011-12-12 13:47:25 +01004782 def test_qualname(self):
4783 descriptors = [str.lower, complex.real, float.real, int.__add__]
4784 types = ['method', 'member', 'getset', 'wrapper']
4785
4786 # make sure we have an example of each type of descriptor
4787 for d, n in zip(descriptors, types):
4788 self.assertEqual(type(d).__name__, n + '_descriptor')
4789
4790 for d in descriptors:
4791 qualname = d.__objclass__.__qualname__ + '.' + d.__name__
4792 self.assertEqual(d.__qualname__, qualname)
4793
4794 self.assertEqual(str.lower.__qualname__, 'str.lower')
4795 self.assertEqual(complex.real.__qualname__, 'complex.real')
4796 self.assertEqual(float.real.__qualname__, 'float.real')
4797 self.assertEqual(int.__add__.__qualname__, 'int.__add__')
4798
Benjamin Peterson2c05a2e2012-10-31 00:01:15 -04004799 class X:
4800 pass
4801 with self.assertRaises(TypeError):
4802 del X.__qualname__
4803
4804 self.assertRaises(TypeError, type.__dict__['__qualname__'].__set__,
4805 str, 'Oink')
4806
Benjamin Peterson3d9e4812013-10-19 16:01:13 -04004807 global Y
4808 class Y:
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004809 class Inside:
4810 pass
Benjamin Peterson3d9e4812013-10-19 16:01:13 -04004811 self.assertEqual(Y.__qualname__, 'Y')
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004812 self.assertEqual(Y.Inside.__qualname__, 'Y.Inside')
Benjamin Peterson3d9e4812013-10-19 16:01:13 -04004813
Victor Stinner6f738742012-02-25 01:22:36 +01004814 def test_qualname_dict(self):
4815 ns = {'__qualname__': 'some.name'}
4816 tp = type('Foo', (), ns)
4817 self.assertEqual(tp.__qualname__, 'some.name')
Benjamin Peterson8afa7fa2012-10-30 23:51:03 -04004818 self.assertNotIn('__qualname__', tp.__dict__)
Victor Stinner6f738742012-02-25 01:22:36 +01004819 self.assertEqual(ns, {'__qualname__': 'some.name'})
4820
4821 ns = {'__qualname__': 1}
4822 self.assertRaises(TypeError, type, 'Foo', (), ns)
4823
Benjamin Peterson52c42432012-03-07 18:41:11 -06004824 def test_cycle_through_dict(self):
4825 # See bug #1469629
4826 class X(dict):
4827 def __init__(self):
4828 dict.__init__(self)
4829 self.__dict__ = self
4830 x = X()
4831 x.attr = 42
4832 wr = weakref.ref(x)
4833 del x
4834 support.gc_collect()
4835 self.assertIsNone(wr())
4836 for o in gc.get_objects():
4837 self.assertIsNot(type(o), X)
4838
Benjamin Peterson96384b92012-03-17 00:05:44 -05004839 def test_object_new_and_init_with_parameters(self):
4840 # See issue #1683368
4841 class OverrideNeither:
4842 pass
4843 self.assertRaises(TypeError, OverrideNeither, 1)
4844 self.assertRaises(TypeError, OverrideNeither, kw=1)
4845 class OverrideNew:
4846 def __new__(cls, foo, kw=0, *args, **kwds):
4847 return object.__new__(cls, *args, **kwds)
4848 class OverrideInit:
4849 def __init__(self, foo, kw=0, *args, **kwargs):
4850 return object.__init__(self, *args, **kwargs)
4851 class OverrideBoth(OverrideNew, OverrideInit):
4852 pass
4853 for case in OverrideNew, OverrideInit, OverrideBoth:
4854 case(1)
4855 case(1, kw=2)
4856 self.assertRaises(TypeError, case, 1, 2, 3)
4857 self.assertRaises(TypeError, case, 1, 2, foo=3)
4858
Benjamin Petersondf813792014-03-17 15:57:17 -05004859 def test_subclassing_does_not_duplicate_dict_descriptors(self):
4860 class Base:
4861 pass
4862 class Sub(Base):
4863 pass
4864 self.assertIn("__dict__", Base.__dict__)
4865 self.assertNotIn("__dict__", Sub.__dict__)
4866
Benjamin Peterson48ad7c02014-08-20 18:41:57 -05004867 def test_bound_method_repr(self):
4868 class Foo:
4869 def method(self):
4870 pass
4871 self.assertRegex(repr(Foo().method),
4872 r"<bound method .*Foo\.method of <.*Foo object at .*>>")
4873
4874
4875 class Base:
4876 def method(self):
4877 pass
4878 class Derived1(Base):
4879 pass
4880 class Derived2(Base):
4881 def method(self):
4882 pass
4883 base = Base()
4884 derived1 = Derived1()
4885 derived2 = Derived2()
4886 super_d2 = super(Derived2, derived2)
4887 self.assertRegex(repr(base.method),
4888 r"<bound method .*Base\.method of <.*Base object at .*>>")
4889 self.assertRegex(repr(derived1.method),
4890 r"<bound method .*Base\.method of <.*Derived1 object at .*>>")
4891 self.assertRegex(repr(derived2.method),
4892 r"<bound method .*Derived2\.method of <.*Derived2 object at .*>>")
4893 self.assertRegex(repr(super_d2.method),
4894 r"<bound method .*Base\.method of <.*Derived2 object at .*>>")
4895
4896 class Foo:
4897 @classmethod
4898 def method(cls):
4899 pass
4900 foo = Foo()
4901 self.assertRegex(repr(foo.method), # access via instance
Benjamin Petersonab078e92016-07-13 21:13:29 -07004902 r"<bound method .*Foo\.method of <class '.*Foo'>>")
Benjamin Peterson48ad7c02014-08-20 18:41:57 -05004903 self.assertRegex(repr(Foo.method), # access via the class
Benjamin Petersonab078e92016-07-13 21:13:29 -07004904 r"<bound method .*Foo\.method of <class '.*Foo'>>")
Benjamin Peterson48ad7c02014-08-20 18:41:57 -05004905
4906
4907 class MyCallable:
4908 def __call__(self, arg):
4909 pass
4910 func = MyCallable() # func has no __name__ or __qualname__ attributes
4911 instance = object()
4912 method = types.MethodType(func, instance)
4913 self.assertRegex(repr(method),
4914 r"<bound method \? of <object object at .*>>")
4915 func.__name__ = "name"
4916 self.assertRegex(repr(method),
4917 r"<bound method name of <object object at .*>>")
4918 func.__qualname__ = "qualname"
4919 self.assertRegex(repr(method),
4920 r"<bound method qualname of <object object at .*>>")
4921
jdemeyer5a306202018-10-19 23:50:06 +02004922 @unittest.skipIf(_testcapi is None, 'need the _testcapi module')
4923 def test_bpo25750(self):
4924 # bpo-25750: calling a descriptor (implemented as built-in
4925 # function with METH_FASTCALL) should not crash CPython if the
4926 # descriptor deletes itself from the class.
4927 class Descr:
4928 __get__ = _testcapi.bad_get
4929
4930 class X:
4931 descr = Descr()
4932 def __new__(cls):
4933 cls.descr = None
4934 # Create this large list to corrupt some unused memory
4935 cls.lst = [2**i for i in range(10000)]
4936 X.descr
4937
Antoine Pitrou9d574812011-12-12 13:47:25 +01004938
Georg Brandl479a7e72008-02-05 18:13:15 +00004939class DictProxyTests(unittest.TestCase):
4940 def setUp(self):
4941 class C(object):
4942 def meth(self):
4943 pass
4944 self.C = C
Christian Heimesbbffeb62008-01-24 09:42:52 +00004945
Brett Cannon7a540732011-02-22 03:04:06 +00004946 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
4947 'trace function introduces __local__')
Georg Brandl479a7e72008-02-05 18:13:15 +00004948 def test_iter_keys(self):
Benjamin Peterson0eb7f862010-12-07 03:46:27 +00004949 # Testing dict-proxy keys...
4950 it = self.C.__dict__.keys()
4951 self.assertNotIsInstance(it, list)
4952 keys = list(it)
Georg Brandl479a7e72008-02-05 18:13:15 +00004953 keys.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +00004954 self.assertEqual(keys, ['__dict__', '__doc__', '__module__',
Benjamin Peterson8afa7fa2012-10-30 23:51:03 -04004955 '__weakref__', 'meth'])
Christian Heimesbbffeb62008-01-24 09:42:52 +00004956
Brett Cannon7a540732011-02-22 03:04:06 +00004957 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
4958 'trace function introduces __local__')
Georg Brandl479a7e72008-02-05 18:13:15 +00004959 def test_iter_values(self):
Benjamin Peterson0eb7f862010-12-07 03:46:27 +00004960 # Testing dict-proxy values...
4961 it = self.C.__dict__.values()
4962 self.assertNotIsInstance(it, list)
4963 values = list(it)
Benjamin Peterson8afa7fa2012-10-30 23:51:03 -04004964 self.assertEqual(len(values), 5)
Christian Heimesbbffeb62008-01-24 09:42:52 +00004965
Brett Cannon7a540732011-02-22 03:04:06 +00004966 @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
4967 'trace function introduces __local__')
Georg Brandl479a7e72008-02-05 18:13:15 +00004968 def test_iter_items(self):
4969 # Testing dict-proxy iteritems...
Benjamin Peterson0eb7f862010-12-07 03:46:27 +00004970 it = self.C.__dict__.items()
4971 self.assertNotIsInstance(it, list)
4972 keys = [item[0] for item in it]
Georg Brandl479a7e72008-02-05 18:13:15 +00004973 keys.sort()
4974 self.assertEqual(keys, ['__dict__', '__doc__', '__module__',
Benjamin Peterson8afa7fa2012-10-30 23:51:03 -04004975 '__weakref__', 'meth'])
Christian Heimesbbffeb62008-01-24 09:42:52 +00004976
Georg Brandl479a7e72008-02-05 18:13:15 +00004977 def test_dict_type_with_metaclass(self):
4978 # Testing type of __dict__ when metaclass set...
4979 class B(object):
4980 pass
4981 class M(type):
4982 pass
4983 class C(metaclass=M):
4984 # In 2.3a1, C.__dict__ was a real dict rather than a dict proxy
4985 pass
4986 self.assertEqual(type(C.__dict__), type(B.__dict__))
Christian Heimesbbffeb62008-01-24 09:42:52 +00004987
Ezio Melottiac53ab62010-12-18 14:59:43 +00004988 def test_repr(self):
Victor Stinner0db176f2012-04-16 00:16:30 +02004989 # Testing mappingproxy.__repr__.
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004990 # We can't blindly compare with the repr of another dict as ordering
4991 # of keys and values is arbitrary and may differ.
4992 r = repr(self.C.__dict__)
Victor Stinner0db176f2012-04-16 00:16:30 +02004993 self.assertTrue(r.startswith('mappingproxy('), r)
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004994 self.assertTrue(r.endswith(')'), r)
4995 for k, v in self.C.__dict__.items():
4996 self.assertIn('{!r}: {!r}'.format(k, v), r)
Ezio Melottiac53ab62010-12-18 14:59:43 +00004997
Christian Heimesbbffeb62008-01-24 09:42:52 +00004998
Serhiy Storchakabedce352021-09-19 22:36:03 +03004999class AAAPTypesLongInitTest(unittest.TestCase):
Georg Brandl479a7e72008-02-05 18:13:15 +00005000 # This is in its own TestCase so that it can be run before any other tests.
Serhiy Storchakabedce352021-09-19 22:36:03 +03005001 # (Hence the 'AAA' in the test class name: to make it the first
5002 # item in a list sorted by name, like
5003 # unittest.TestLoader.getTestCaseNames() does.)
Georg Brandl479a7e72008-02-05 18:13:15 +00005004 def test_pytype_long_ready(self):
5005 # Testing SF bug 551412 ...
Christian Heimesbbffeb62008-01-24 09:42:52 +00005006
Georg Brandl479a7e72008-02-05 18:13:15 +00005007 # This dumps core when SF bug 551412 isn't fixed --
5008 # but only when test_descr.py is run separately.
5009 # (That can't be helped -- as soon as PyType_Ready()
5010 # is called for PyLong_Type, the bug is gone.)
5011 class UserLong(object):
5012 def __pow__(self, *args):
5013 pass
5014 try:
5015 pow(0, UserLong(), 0)
5016 except:
5017 pass
Christian Heimesbbffeb62008-01-24 09:42:52 +00005018
Georg Brandl479a7e72008-02-05 18:13:15 +00005019 # Another segfault only when run early
5020 # (before PyType_Ready(tuple) is called)
5021 type.mro(tuple)
Christian Heimes969fe572008-01-25 11:23:10 +00005022
5023
Victor Stinnerd74782b2012-03-09 00:39:08 +01005024class MiscTests(unittest.TestCase):
5025 def test_type_lookup_mro_reference(self):
5026 # Issue #14199: _PyType_Lookup() has to keep a strong reference to
5027 # the type MRO because it may be modified during the lookup, if
5028 # __bases__ is set during the lookup for example.
5029 class MyKey(object):
5030 def __hash__(self):
5031 return hash('mykey')
5032
5033 def __eq__(self, other):
5034 X.__bases__ = (Base2,)
5035
5036 class Base(object):
5037 mykey = 'from Base'
5038 mykey2 = 'from Base'
5039
5040 class Base2(object):
5041 mykey = 'from Base2'
5042 mykey2 = 'from Base2'
5043
5044 X = type('X', (Base,), {MyKey(): 5})
5045 # mykey is read from Base
5046 self.assertEqual(X.mykey, 'from Base')
5047 # mykey2 is read from Base2 because MyKey.__eq__ has set __bases__
5048 self.assertEqual(X.mykey2, 'from Base2')
5049
5050
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005051class PicklingTests(unittest.TestCase):
5052
Antoine Pitrou7cd9fbe2013-11-23 19:01:36 +01005053 def _check_reduce(self, proto, obj, args=(), kwargs={}, state=None,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005054 listitems=None, dictitems=None):
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02005055 if proto >= 2:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005056 reduce_value = obj.__reduce_ex__(proto)
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02005057 if kwargs:
5058 self.assertEqual(reduce_value[0], copyreg.__newobj_ex__)
5059 self.assertEqual(reduce_value[1], (type(obj), args, kwargs))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005060 else:
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02005061 self.assertEqual(reduce_value[0], copyreg.__newobj__)
5062 self.assertEqual(reduce_value[1], (type(obj),) + args)
5063 self.assertEqual(reduce_value[2], state)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005064 if listitems is not None:
5065 self.assertListEqual(list(reduce_value[3]), listitems)
5066 else:
5067 self.assertIsNone(reduce_value[3])
5068 if dictitems is not None:
5069 self.assertDictEqual(dict(reduce_value[4]), dictitems)
5070 else:
5071 self.assertIsNone(reduce_value[4])
5072 else:
5073 base_type = type(obj).__base__
5074 reduce_value = (copyreg._reconstructor,
5075 (type(obj),
Antoine Pitrou7cd9fbe2013-11-23 19:01:36 +01005076 base_type,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005077 None if base_type is object else base_type(obj)))
5078 if state is not None:
5079 reduce_value += (state,)
5080 self.assertEqual(obj.__reduce_ex__(proto), reduce_value)
5081 self.assertEqual(obj.__reduce__(), reduce_value)
5082
5083 def test_reduce(self):
5084 protocols = range(pickle.HIGHEST_PROTOCOL + 1)
5085 args = (-101, "spam")
5086 kwargs = {'bacon': -201, 'fish': -301}
5087 state = {'cheese': -401}
5088
5089 class C1:
5090 def __getnewargs__(self):
5091 return args
5092 obj = C1()
5093 for proto in protocols:
5094 self._check_reduce(proto, obj, args)
5095
5096 for name, value in state.items():
5097 setattr(obj, name, value)
5098 for proto in protocols:
5099 self._check_reduce(proto, obj, args, state=state)
5100
5101 class C2:
5102 def __getnewargs__(self):
5103 return "bad args"
5104 obj = C2()
5105 for proto in protocols:
5106 if proto >= 2:
5107 with self.assertRaises(TypeError):
5108 obj.__reduce_ex__(proto)
5109
5110 class C3:
5111 def __getnewargs_ex__(self):
5112 return (args, kwargs)
5113 obj = C3()
5114 for proto in protocols:
Serhiy Storchaka20d15b52015-10-11 17:52:09 +03005115 if proto >= 2:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005116 self._check_reduce(proto, obj, args, kwargs)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005117
5118 class C4:
5119 def __getnewargs_ex__(self):
5120 return (args, "bad dict")
5121 class C5:
5122 def __getnewargs_ex__(self):
5123 return ("bad tuple", kwargs)
5124 class C6:
5125 def __getnewargs_ex__(self):
5126 return ()
5127 class C7:
5128 def __getnewargs_ex__(self):
5129 return "bad args"
5130 for proto in protocols:
5131 for cls in C4, C5, C6, C7:
5132 obj = cls()
5133 if proto >= 2:
5134 with self.assertRaises((TypeError, ValueError)):
5135 obj.__reduce_ex__(proto)
5136
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005137 class C9:
5138 def __getnewargs_ex__(self):
5139 return (args, {})
5140 obj = C9()
5141 for proto in protocols:
5142 self._check_reduce(proto, obj, args)
5143
5144 class C10:
5145 def __getnewargs_ex__(self):
5146 raise IndexError
5147 obj = C10()
5148 for proto in protocols:
5149 if proto >= 2:
5150 with self.assertRaises(IndexError):
5151 obj.__reduce_ex__(proto)
5152
5153 class C11:
5154 def __getstate__(self):
5155 return state
5156 obj = C11()
5157 for proto in protocols:
5158 self._check_reduce(proto, obj, state=state)
5159
5160 class C12:
5161 def __getstate__(self):
5162 return "not dict"
5163 obj = C12()
5164 for proto in protocols:
5165 self._check_reduce(proto, obj, state="not dict")
5166
5167 class C13:
5168 def __getstate__(self):
5169 raise IndexError
5170 obj = C13()
5171 for proto in protocols:
5172 with self.assertRaises(IndexError):
5173 obj.__reduce_ex__(proto)
5174 if proto < 2:
5175 with self.assertRaises(IndexError):
5176 obj.__reduce__()
5177
5178 class C14:
5179 __slots__ = tuple(state)
5180 def __init__(self):
5181 for name, value in state.items():
5182 setattr(self, name, value)
5183
5184 obj = C14()
5185 for proto in protocols:
5186 if proto >= 2:
5187 self._check_reduce(proto, obj, state=(None, state))
5188 else:
5189 with self.assertRaises(TypeError):
5190 obj.__reduce_ex__(proto)
5191 with self.assertRaises(TypeError):
5192 obj.__reduce__()
5193
5194 class C15(dict):
5195 pass
5196 obj = C15({"quebec": -601})
5197 for proto in protocols:
5198 self._check_reduce(proto, obj, dictitems=dict(obj))
5199
5200 class C16(list):
5201 pass
5202 obj = C16(["yukon"])
5203 for proto in protocols:
5204 self._check_reduce(proto, obj, listitems=list(obj))
5205
Benjamin Peterson2626fab2014-02-16 13:49:16 -05005206 def test_special_method_lookup(self):
5207 protocols = range(pickle.HIGHEST_PROTOCOL + 1)
5208 class Picky:
5209 def __getstate__(self):
5210 return {}
5211
5212 def __getattr__(self, attr):
5213 if attr in ("__getnewargs__", "__getnewargs_ex__"):
5214 raise AssertionError(attr)
5215 return None
5216 for protocol in protocols:
5217 state = {} if protocol >= 2 else None
5218 self._check_reduce(protocol, Picky(), state=state)
5219
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005220 def _assert_is_copy(self, obj, objcopy, msg=None):
5221 """Utility method to verify if two objects are copies of each others.
5222 """
5223 if msg is None:
5224 msg = "{!r} is not a copy of {!r}".format(obj, objcopy)
5225 if type(obj).__repr__ is object.__repr__:
5226 # We have this limitation for now because we use the object's repr
5227 # to help us verify that the two objects are copies. This allows
5228 # us to delegate the non-generic verification logic to the objects
5229 # themselves.
5230 raise ValueError("object passed to _assert_is_copy must " +
5231 "override the __repr__ method.")
5232 self.assertIsNot(obj, objcopy, msg=msg)
5233 self.assertIs(type(obj), type(objcopy), msg=msg)
5234 if hasattr(obj, '__dict__'):
5235 self.assertDictEqual(obj.__dict__, objcopy.__dict__, msg=msg)
5236 self.assertIsNot(obj.__dict__, objcopy.__dict__, msg=msg)
5237 if hasattr(obj, '__slots__'):
5238 self.assertListEqual(obj.__slots__, objcopy.__slots__, msg=msg)
5239 for slot in obj.__slots__:
5240 self.assertEqual(
5241 hasattr(obj, slot), hasattr(objcopy, slot), msg=msg)
5242 self.assertEqual(getattr(obj, slot, None),
5243 getattr(objcopy, slot, None), msg=msg)
5244 self.assertEqual(repr(obj), repr(objcopy), msg=msg)
5245
5246 @staticmethod
5247 def _generate_pickle_copiers():
5248 """Utility method to generate the many possible pickle configurations.
5249 """
5250 class PickleCopier:
5251 "This class copies object using pickle."
5252 def __init__(self, proto, dumps, loads):
5253 self.proto = proto
5254 self.dumps = dumps
5255 self.loads = loads
5256 def copy(self, obj):
5257 return self.loads(self.dumps(obj, self.proto))
5258 def __repr__(self):
5259 # We try to be as descriptive as possible here since this is
5260 # the string which we will allow us to tell the pickle
5261 # configuration we are using during debugging.
5262 return ("PickleCopier(proto={}, dumps={}.{}, loads={}.{})"
5263 .format(self.proto,
5264 self.dumps.__module__, self.dumps.__qualname__,
5265 self.loads.__module__, self.loads.__qualname__))
5266 return (PickleCopier(*args) for args in
5267 itertools.product(range(pickle.HIGHEST_PROTOCOL + 1),
5268 {pickle.dumps, pickle._dumps},
5269 {pickle.loads, pickle._loads}))
5270
5271 def test_pickle_slots(self):
5272 # Tests pickling of classes with __slots__.
5273
5274 # Pickling of classes with __slots__ but without __getstate__ should
5275 # fail (if using protocol 0 or 1)
5276 global C
5277 class C:
5278 __slots__ = ['a']
5279 with self.assertRaises(TypeError):
5280 pickle.dumps(C(), 0)
5281
5282 global D
5283 class D(C):
5284 pass
5285 with self.assertRaises(TypeError):
5286 pickle.dumps(D(), 0)
5287
5288 class C:
5289 "A class with __getstate__ and __setstate__ implemented."
5290 __slots__ = ['a']
5291 def __getstate__(self):
5292 state = getattr(self, '__dict__', {}).copy()
5293 for cls in type(self).__mro__:
Antoine Pitrou7cd9fbe2013-11-23 19:01:36 +01005294 for slot in cls.__dict__.get('__slots__', ()):
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005295 try:
5296 state[slot] = getattr(self, slot)
5297 except AttributeError:
5298 pass
5299 return state
5300 def __setstate__(self, state):
5301 for k, v in state.items():
5302 setattr(self, k, v)
5303 def __repr__(self):
5304 return "%s()<%r>" % (type(self).__name__, self.__getstate__())
5305
5306 class D(C):
5307 "A subclass of a class with slots."
5308 pass
5309
5310 global E
5311 class E(C):
5312 "A subclass with an extra slot."
5313 __slots__ = ['b']
5314
5315 # Now it should work
5316 for pickle_copier in self._generate_pickle_copiers():
5317 with self.subTest(pickle_copier=pickle_copier):
5318 x = C()
5319 y = pickle_copier.copy(x)
5320 self._assert_is_copy(x, y)
5321
5322 x.a = 42
5323 y = pickle_copier.copy(x)
5324 self._assert_is_copy(x, y)
5325
5326 x = D()
5327 x.a = 42
5328 x.b = 100
5329 y = pickle_copier.copy(x)
5330 self._assert_is_copy(x, y)
5331
5332 x = E()
5333 x.a = 42
5334 x.b = "foo"
5335 y = pickle_copier.copy(x)
5336 self._assert_is_copy(x, y)
5337
5338 def test_reduce_copying(self):
5339 # Tests pickling and copying new-style classes and objects.
5340 global C1
5341 class C1:
5342 "The state of this class is copyable via its instance dict."
5343 ARGS = (1, 2)
5344 NEED_DICT_COPYING = True
5345 def __init__(self, a, b):
5346 super().__init__()
5347 self.a = a
5348 self.b = b
5349 def __repr__(self):
5350 return "C1(%r, %r)" % (self.a, self.b)
5351
5352 global C2
5353 class C2(list):
5354 "A list subclass copyable via __getnewargs__."
5355 ARGS = (1, 2)
5356 NEED_DICT_COPYING = False
5357 def __new__(cls, a, b):
5358 self = super().__new__(cls)
5359 self.a = a
5360 self.b = b
5361 return self
5362 def __init__(self, *args):
5363 super().__init__()
5364 # This helps testing that __init__ is not called during the
5365 # unpickling process, which would cause extra appends.
5366 self.append("cheese")
5367 @classmethod
5368 def __getnewargs__(cls):
5369 return cls.ARGS
5370 def __repr__(self):
5371 return "C2(%r, %r)<%r>" % (self.a, self.b, list(self))
5372
5373 global C3
5374 class C3(list):
5375 "A list subclass copyable via __getstate__."
5376 ARGS = (1, 2)
5377 NEED_DICT_COPYING = False
5378 def __init__(self, a, b):
5379 self.a = a
5380 self.b = b
5381 # This helps testing that __init__ is not called during the
5382 # unpickling process, which would cause extra appends.
5383 self.append("cheese")
5384 @classmethod
5385 def __getstate__(cls):
5386 return cls.ARGS
5387 def __setstate__(self, state):
5388 a, b = state
5389 self.a = a
5390 self.b = b
5391 def __repr__(self):
5392 return "C3(%r, %r)<%r>" % (self.a, self.b, list(self))
5393
5394 global C4
5395 class C4(int):
5396 "An int subclass copyable via __getnewargs__."
5397 ARGS = ("hello", "world", 1)
5398 NEED_DICT_COPYING = False
5399 def __new__(cls, a, b, value):
5400 self = super().__new__(cls, value)
5401 self.a = a
5402 self.b = b
5403 return self
5404 @classmethod
5405 def __getnewargs__(cls):
5406 return cls.ARGS
5407 def __repr__(self):
5408 return "C4(%r, %r)<%r>" % (self.a, self.b, int(self))
5409
5410 global C5
5411 class C5(int):
5412 "An int subclass copyable via __getnewargs_ex__."
5413 ARGS = (1, 2)
5414 KWARGS = {'value': 3}
5415 NEED_DICT_COPYING = False
5416 def __new__(cls, a, b, *, value=0):
5417 self = super().__new__(cls, value)
5418 self.a = a
5419 self.b = b
5420 return self
5421 @classmethod
5422 def __getnewargs_ex__(cls):
5423 return (cls.ARGS, cls.KWARGS)
5424 def __repr__(self):
5425 return "C5(%r, %r)<%r>" % (self.a, self.b, int(self))
5426
5427 test_classes = (C1, C2, C3, C4, C5)
5428 # Testing copying through pickle
5429 pickle_copiers = self._generate_pickle_copiers()
5430 for cls, pickle_copier in itertools.product(test_classes, pickle_copiers):
5431 with self.subTest(cls=cls, pickle_copier=pickle_copier):
5432 kwargs = getattr(cls, 'KWARGS', {})
5433 obj = cls(*cls.ARGS, **kwargs)
5434 proto = pickle_copier.proto
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005435 objcopy = pickle_copier.copy(obj)
5436 self._assert_is_copy(obj, objcopy)
5437 # For test classes that supports this, make sure we didn't go
5438 # around the reduce protocol by simply copying the attribute
5439 # dictionary. We clear attributes using the previous copy to
5440 # not mutate the original argument.
5441 if proto >= 2 and not cls.NEED_DICT_COPYING:
5442 objcopy.__dict__.clear()
5443 objcopy2 = pickle_copier.copy(objcopy)
5444 self._assert_is_copy(obj, objcopy2)
5445
5446 # Testing copying through copy.deepcopy()
5447 for cls in test_classes:
5448 with self.subTest(cls=cls):
5449 kwargs = getattr(cls, 'KWARGS', {})
5450 obj = cls(*cls.ARGS, **kwargs)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005451 objcopy = deepcopy(obj)
5452 self._assert_is_copy(obj, objcopy)
5453 # For test classes that supports this, make sure we didn't go
5454 # around the reduce protocol by simply copying the attribute
5455 # dictionary. We clear attributes using the previous copy to
5456 # not mutate the original argument.
5457 if not cls.NEED_DICT_COPYING:
5458 objcopy.__dict__.clear()
5459 objcopy2 = deepcopy(objcopy)
5460 self._assert_is_copy(obj, objcopy2)
5461
Serhiy Storchakad28bb622015-11-25 18:33:29 +02005462 def test_issue24097(self):
5463 # Slot name is freed inside __getattr__ and is later used.
5464 class S(str): # Not interned
5465 pass
5466 class A:
5467 __slotnames__ = [S('spam')]
5468 def __getattr__(self, attr):
5469 if attr == 'spam':
5470 A.__slotnames__[:] = [S('spam')]
5471 return 42
5472 else:
5473 raise AttributeError
5474
5475 import copyreg
5476 expected = (copyreg.__newobj__, (A,), (None, {'spam': 42}), None, None)
Serhiy Storchaka205e00c2017-04-08 09:52:59 +03005477 self.assertEqual(A().__reduce_ex__(2), expected) # Shouldn't crash
5478
5479 def test_object_reduce(self):
5480 # Issue #29914
5481 # __reduce__() takes no arguments
5482 object().__reduce__()
5483 with self.assertRaises(TypeError):
5484 object().__reduce__(0)
5485 # __reduce_ex__() takes one integer argument
5486 object().__reduce_ex__(0)
5487 with self.assertRaises(TypeError):
5488 object().__reduce_ex__()
5489 with self.assertRaises(TypeError):
5490 object().__reduce_ex__(None)
Serhiy Storchakad28bb622015-11-25 18:33:29 +02005491
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005492
Benjamin Peterson2a605342014-03-17 16:20:12 -05005493class SharedKeyTests(unittest.TestCase):
5494
5495 @support.cpython_only
5496 def test_subclasses(self):
5497 # Verify that subclasses can share keys (per PEP 412)
5498 class A:
5499 pass
5500 class B(A):
5501 pass
5502
5503 a, b = A(), B()
5504 self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
Inada Naokif2a18672019-03-12 17:25:44 +09005505 self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({"a":1}))
Victor Stinner742da042016-09-07 17:40:12 -07005506 # Initial hash table can contain at most 5 elements.
5507 # Set 6 attributes to cause internal resizing.
5508 a.x, a.y, a.z, a.w, a.v, a.u = range(6)
Benjamin Peterson2a605342014-03-17 16:20:12 -05005509 self.assertNotEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
5510 a2 = A()
5511 self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(a2)))
Inada Naokif2a18672019-03-12 17:25:44 +09005512 self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({"a":1}))
Victor Stinner742da042016-09-07 17:40:12 -07005513 b.u, b.v, b.w, b.t, b.s, b.r = range(6)
Inada Naokif2a18672019-03-12 17:25:44 +09005514 self.assertLess(sys.getsizeof(vars(b)), sys.getsizeof({"a":1}))
Benjamin Peterson2a605342014-03-17 16:20:12 -05005515
5516
Benjamin Peterson104b9e02015-02-05 22:29:14 -05005517class DebugHelperMeta(type):
5518 """
5519 Sets default __doc__ and simplifies repr() output.
5520 """
5521 def __new__(mcls, name, bases, attrs):
5522 if attrs.get('__doc__') is None:
5523 attrs['__doc__'] = name # helps when debugging with gdb
5524 return type.__new__(mcls, name, bases, attrs)
5525 def __repr__(cls):
5526 return repr(cls.__name__)
5527
5528
5529class MroTest(unittest.TestCase):
5530 """
5531 Regressions for some bugs revealed through
5532 mcsl.mro() customization (typeobject.c: mro_internal()) and
5533 cls.__bases__ assignment (typeobject.c: type_set_bases()).
5534 """
5535
5536 def setUp(self):
5537 self.step = 0
5538 self.ready = False
5539
5540 def step_until(self, limit):
5541 ret = (self.step < limit)
5542 if ret:
5543 self.step += 1
5544 return ret
5545
5546 def test_incomplete_set_bases_on_self(self):
5547 """
5548 type_set_bases must be aware that type->tp_mro can be NULL.
5549 """
5550 class M(DebugHelperMeta):
5551 def mro(cls):
5552 if self.step_until(1):
5553 assert cls.__mro__ is None
5554 cls.__bases__ += ()
5555
5556 return type.mro(cls)
5557
5558 class A(metaclass=M):
5559 pass
5560
5561 def test_reent_set_bases_on_base(self):
5562 """
5563 Deep reentrancy must not over-decref old_mro.
5564 """
5565 class M(DebugHelperMeta):
5566 def mro(cls):
5567 if cls.__mro__ is not None and cls.__name__ == 'B':
5568 # 4-5 steps are usually enough to make it crash somewhere
5569 if self.step_until(10):
5570 A.__bases__ += ()
5571
5572 return type.mro(cls)
5573
5574 class A(metaclass=M):
5575 pass
5576 class B(A):
5577 pass
5578 B.__bases__ += ()
5579
5580 def test_reent_set_bases_on_direct_base(self):
5581 """
5582 Similar to test_reent_set_bases_on_base, but may crash differently.
5583 """
5584 class M(DebugHelperMeta):
5585 def mro(cls):
5586 base = cls.__bases__[0]
5587 if base is not object:
5588 if self.step_until(5):
5589 base.__bases__ += ()
5590
5591 return type.mro(cls)
5592
5593 class A(metaclass=M):
5594 pass
5595 class B(A):
5596 pass
5597 class C(B):
5598 pass
5599
5600 def test_reent_set_bases_tp_base_cycle(self):
5601 """
5602 type_set_bases must check for an inheritance cycle not only through
5603 MRO of the type, which may be not yet updated in case of reentrance,
5604 but also through tp_base chain, which is assigned before diving into
5605 inner calls to mro().
5606
5607 Otherwise, the following snippet can loop forever:
5608 do {
5609 // ...
5610 type = type->tp_base;
5611 } while (type != NULL);
5612
5613 Functions that rely on tp_base (like solid_base and PyType_IsSubtype)
5614 would not be happy in that case, causing a stack overflow.
5615 """
5616 class M(DebugHelperMeta):
5617 def mro(cls):
5618 if self.ready:
5619 if cls.__name__ == 'B1':
5620 B2.__bases__ = (B1,)
5621 if cls.__name__ == 'B2':
5622 B1.__bases__ = (B2,)
5623 return type.mro(cls)
5624
5625 class A(metaclass=M):
5626 pass
5627 class B1(A):
5628 pass
5629 class B2(A):
5630 pass
5631
5632 self.ready = True
5633 with self.assertRaises(TypeError):
5634 B1.__bases__ += ()
5635
5636 def test_tp_subclasses_cycle_in_update_slots(self):
5637 """
5638 type_set_bases must check for reentrancy upon finishing its job
5639 by updating tp_subclasses of old/new bases of the type.
5640 Otherwise, an implicit inheritance cycle through tp_subclasses
5641 can break functions that recurse on elements of that field
5642 (like recurse_down_subclasses and mro_hierarchy) eventually
5643 leading to a stack overflow.
5644 """
5645 class M(DebugHelperMeta):
5646 def mro(cls):
5647 if self.ready and cls.__name__ == 'C':
5648 self.ready = False
5649 C.__bases__ = (B2,)
5650 return type.mro(cls)
5651
5652 class A(metaclass=M):
5653 pass
5654 class B1(A):
5655 pass
5656 class B2(A):
5657 pass
5658 class C(A):
5659 pass
5660
5661 self.ready = True
5662 C.__bases__ = (B1,)
5663 B1.__bases__ = (C,)
5664
5665 self.assertEqual(C.__bases__, (B2,))
5666 self.assertEqual(B2.__subclasses__(), [C])
5667 self.assertEqual(B1.__subclasses__(), [])
5668
5669 self.assertEqual(B1.__bases__, (C,))
5670 self.assertEqual(C.__subclasses__(), [B1])
5671
5672 def test_tp_subclasses_cycle_error_return_path(self):
5673 """
5674 The same as test_tp_subclasses_cycle_in_update_slots, but tests
5675 a code path executed on error (goto bail).
5676 """
5677 class E(Exception):
5678 pass
5679 class M(DebugHelperMeta):
5680 def mro(cls):
5681 if self.ready and cls.__name__ == 'C':
5682 if C.__bases__ == (B2,):
5683 self.ready = False
5684 else:
5685 C.__bases__ = (B2,)
5686 raise E
5687 return type.mro(cls)
5688
5689 class A(metaclass=M):
5690 pass
5691 class B1(A):
5692 pass
5693 class B2(A):
5694 pass
5695 class C(A):
5696 pass
5697
5698 self.ready = True
5699 with self.assertRaises(E):
5700 C.__bases__ = (B1,)
5701 B1.__bases__ = (C,)
5702
5703 self.assertEqual(C.__bases__, (B2,))
5704 self.assertEqual(C.__mro__, tuple(type.mro(C)))
5705
5706 def test_incomplete_extend(self):
5707 """
5708 Extending an unitialized type with type->tp_mro == NULL must
5709 throw a reasonable TypeError exception, instead of failing
5710 with PyErr_BadInternalCall.
5711 """
5712 class M(DebugHelperMeta):
5713 def mro(cls):
5714 if cls.__mro__ is None and cls.__name__ != 'X':
5715 with self.assertRaises(TypeError):
5716 class X(cls):
5717 pass
5718
5719 return type.mro(cls)
5720
5721 class A(metaclass=M):
5722 pass
5723
5724 def test_incomplete_super(self):
5725 """
5726 Attrubute lookup on a super object must be aware that
5727 its target type can be uninitialized (type->tp_mro == NULL).
5728 """
5729 class M(DebugHelperMeta):
5730 def mro(cls):
5731 if cls.__mro__ is None:
5732 with self.assertRaises(AttributeError):
5733 super(cls, cls).xxx
5734
5735 return type.mro(cls)
5736
5737 class A(metaclass=M):
5738 pass
5739
5740
Guido van Rossuma56b42b2001-09-20 21:39:07 +00005741if __name__ == "__main__":
Serhiy Storchakabedce352021-09-19 22:36:03 +03005742 unittest.main()