• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import builtins
2import copyreg
3import gc
4import itertools
5import math
6import pickle
7import random
8import string
9import sys
10import types
11import unittest
12import warnings
13import weakref
14
15from copy import deepcopy
16from contextlib import redirect_stdout
17from test import support
18
19try:
20    import _testcapi
21except ImportError:
22    _testcapi = None
23
24try:
25    import xxsubtype
26except ImportError:
27    xxsubtype = None
28
29
30class OperatorsTest(unittest.TestCase):
31
32    def __init__(self, *args, **kwargs):
33        unittest.TestCase.__init__(self, *args, **kwargs)
34        self.binops = {
35            'add': '+',
36            'sub': '-',
37            'mul': '*',
38            'matmul': '@',
39            'truediv': '/',
40            'floordiv': '//',
41            'divmod': 'divmod',
42            'pow': '**',
43            'lshift': '<<',
44            'rshift': '>>',
45            'and': '&',
46            'xor': '^',
47            'or': '|',
48            'cmp': 'cmp',
49            'lt': '<',
50            'le': '<=',
51            'eq': '==',
52            'ne': '!=',
53            'gt': '>',
54            'ge': '>=',
55        }
56
57        for name, expr in list(self.binops.items()):
58            if expr.islower():
59                expr = expr + "(a, b)"
60            else:
61                expr = 'a %s b' % expr
62            self.binops[name] = expr
63
64        self.unops = {
65            'pos': '+',
66            'neg': '-',
67            'abs': 'abs',
68            'invert': '~',
69            'int': 'int',
70            'float': 'float',
71        }
72
73        for name, expr in list(self.unops.items()):
74            if expr.islower():
75                expr = expr + "(a)"
76            else:
77                expr = '%s a' % expr
78            self.unops[name] = expr
79
80    def unop_test(self, a, res, expr="len(a)", meth="__len__"):
81        d = {'a': a}
82        self.assertEqual(eval(expr, d), res)
83        t = type(a)
84        m = getattr(t, meth)
85
86        # Find method in parent class
87        while meth not in t.__dict__:
88            t = t.__bases__[0]
89        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
90        # method object; the getattr() below obtains its underlying function.
91        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
92        self.assertEqual(m(a), res)
93        bm = getattr(a, meth)
94        self.assertEqual(bm(), res)
95
96    def binop_test(self, a, b, res, expr="a+b", meth="__add__"):
97        d = {'a': a, 'b': b}
98
99        self.assertEqual(eval(expr, d), res)
100        t = type(a)
101        m = getattr(t, meth)
102        while meth not in t.__dict__:
103            t = t.__bases__[0]
104        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
105        # method object; the getattr() below obtains its underlying function.
106        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
107        self.assertEqual(m(a, b), res)
108        bm = getattr(a, meth)
109        self.assertEqual(bm(b), res)
110
111    def sliceop_test(self, a, b, c, res, expr="a[b:c]", meth="__getitem__"):
112        d = {'a': a, 'b': b, 'c': c}
113        self.assertEqual(eval(expr, d), res)
114        t = type(a)
115        m = getattr(t, meth)
116        while meth not in t.__dict__:
117            t = t.__bases__[0]
118        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
119        # method object; the getattr() below obtains its underlying function.
120        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
121        self.assertEqual(m(a, slice(b, c)), res)
122        bm = getattr(a, meth)
123        self.assertEqual(bm(slice(b, c)), res)
124
125    def setop_test(self, a, b, res, stmt="a+=b", meth="__iadd__"):
126        d = {'a': deepcopy(a), 'b': b}
127        exec(stmt, d)
128        self.assertEqual(d['a'], res)
129        t = type(a)
130        m = getattr(t, meth)
131        while meth not in t.__dict__:
132            t = t.__bases__[0]
133        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
134        # method object; the getattr() below obtains its underlying function.
135        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
136        d['a'] = deepcopy(a)
137        m(d['a'], b)
138        self.assertEqual(d['a'], res)
139        d['a'] = deepcopy(a)
140        bm = getattr(d['a'], meth)
141        bm(b)
142        self.assertEqual(d['a'], res)
143
144    def set2op_test(self, a, b, c, res, stmt="a[b]=c", meth="__setitem__"):
145        d = {'a': deepcopy(a), 'b': b, 'c': c}
146        exec(stmt, d)
147        self.assertEqual(d['a'], res)
148        t = type(a)
149        m = getattr(t, meth)
150        while meth not in t.__dict__:
151            t = t.__bases__[0]
152        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
153        # method object; the getattr() below obtains its underlying function.
154        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
155        d['a'] = deepcopy(a)
156        m(d['a'], b, c)
157        self.assertEqual(d['a'], res)
158        d['a'] = deepcopy(a)
159        bm = getattr(d['a'], meth)
160        bm(b, c)
161        self.assertEqual(d['a'], res)
162
163    def setsliceop_test(self, a, b, c, d, res, stmt="a[b:c]=d", meth="__setitem__"):
164        dictionary = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d}
165        exec(stmt, dictionary)
166        self.assertEqual(dictionary['a'], res)
167        t = type(a)
168        while meth not in t.__dict__:
169            t = t.__bases__[0]
170        m = getattr(t, meth)
171        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
172        # method object; the getattr() below obtains its underlying function.
173        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
174        dictionary['a'] = deepcopy(a)
175        m(dictionary['a'], slice(b, c), d)
176        self.assertEqual(dictionary['a'], res)
177        dictionary['a'] = deepcopy(a)
178        bm = getattr(dictionary['a'], meth)
179        bm(slice(b, c), d)
180        self.assertEqual(dictionary['a'], res)
181
182    def test_lists(self):
183        # Testing list operations...
184        # Asserts are within individual test methods
185        self.binop_test([1], [2], [1,2], "a+b", "__add__")
186        self.binop_test([1,2,3], 2, 1, "b in a", "__contains__")
187        self.binop_test([1,2,3], 4, 0, "b in a", "__contains__")
188        self.binop_test([1,2,3], 1, 2, "a[b]", "__getitem__")
189        self.sliceop_test([1,2,3], 0, 2, [1,2], "a[b:c]", "__getitem__")
190        self.setop_test([1], [2], [1,2], "a+=b", "__iadd__")
191        self.setop_test([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__")
192        self.unop_test([1,2,3], 3, "len(a)", "__len__")
193        self.binop_test([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__")
194        self.binop_test([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__")
195        self.set2op_test([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__")
196        self.setsliceop_test([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d",
197                        "__setitem__")
198
199    def test_dicts(self):
200        # Testing dict operations...
201        self.binop_test({1:2,3:4}, 1, 1, "b in a", "__contains__")
202        self.binop_test({1:2,3:4}, 2, 0, "b in a", "__contains__")
203        self.binop_test({1:2,3:4}, 1, 2, "a[b]", "__getitem__")
204
205        d = {1:2, 3:4}
206        l1 = []
207        for i in list(d.keys()):
208            l1.append(i)
209        l = []
210        for i in iter(d):
211            l.append(i)
212        self.assertEqual(l, l1)
213        l = []
214        for i in d.__iter__():
215            l.append(i)
216        self.assertEqual(l, l1)
217        l = []
218        for i in dict.__iter__(d):
219            l.append(i)
220        self.assertEqual(l, l1)
221        d = {1:2, 3:4}
222        self.unop_test(d, 2, "len(a)", "__len__")
223        self.assertEqual(eval(repr(d), {}), d)
224        self.assertEqual(eval(d.__repr__(), {}), d)
225        self.set2op_test({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c",
226                        "__setitem__")
227
228    # Tests for unary and binary operators
229    def number_operators(self, a, b, skip=[]):
230        dict = {'a': a, 'b': b}
231
232        for name, expr in self.binops.items():
233            if name not in skip:
234                name = "__%s__" % name
235                if hasattr(a, name):
236                    res = eval(expr, dict)
237                    self.binop_test(a, b, res, expr, name)
238
239        for name, expr in list(self.unops.items()):
240            if name not in skip:
241                name = "__%s__" % name
242                if hasattr(a, name):
243                    res = eval(expr, dict)
244                    self.unop_test(a, res, expr, name)
245
246    def test_ints(self):
247        # Testing int operations...
248        self.number_operators(100, 3)
249        # The following crashes in Python 2.2
250        self.assertEqual((1).__bool__(), 1)
251        self.assertEqual((0).__bool__(), 0)
252        # This returns 'NotImplemented' in Python 2.2
253        class C(int):
254            def __add__(self, other):
255                return NotImplemented
256        self.assertEqual(C(5), 5)
257        try:
258            C() + ""
259        except TypeError:
260            pass
261        else:
262            self.fail("NotImplemented should have caused TypeError")
263
264    def test_floats(self):
265        # Testing float operations...
266        self.number_operators(100.0, 3.0)
267
268    def test_complexes(self):
269        # Testing complex operations...
270        self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge',
271                                                  'int', 'float',
272                                                  'floordiv', 'divmod', 'mod'])
273
274        class Number(complex):
275            __slots__ = ['prec']
276            def __new__(cls, *args, **kwds):
277                result = complex.__new__(cls, *args)
278                result.prec = kwds.get('prec', 12)
279                return result
280            def __repr__(self):
281                prec = self.prec
282                if self.imag == 0.0:
283                    return "%.*g" % (prec, self.real)
284                if self.real == 0.0:
285                    return "%.*gj" % (prec, self.imag)
286                return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag)
287            __str__ = __repr__
288
289        a = Number(3.14, prec=6)
290        self.assertEqual(repr(a), "3.14")
291        self.assertEqual(a.prec, 6)
292
293        a = Number(a, prec=2)
294        self.assertEqual(repr(a), "3.1")
295        self.assertEqual(a.prec, 2)
296
297        a = Number(234.5)
298        self.assertEqual(repr(a), "234.5")
299        self.assertEqual(a.prec, 12)
300
301    def test_explicit_reverse_methods(self):
302        # see issue 9930
303        self.assertEqual(complex.__radd__(3j, 4.0), complex(4.0, 3.0))
304        self.assertEqual(float.__rsub__(3.0, 1), -2.0)
305
306    @support.impl_detail("the module 'xxsubtype' is internal")
307    @unittest.skipIf(xxsubtype is None, "requires xxsubtype module")
308    def test_spam_lists(self):
309        # Testing spamlist operations...
310        import copy, xxsubtype as spam
311
312        def spamlist(l, memo=None):
313            import xxsubtype as spam
314            return spam.spamlist(l)
315
316        # This is an ugly hack:
317        copy._deepcopy_dispatch[spam.spamlist] = spamlist
318
319        self.binop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b",
320                       "__add__")
321        self.binop_test(spamlist([1,2,3]), 2, 1, "b in a", "__contains__")
322        self.binop_test(spamlist([1,2,3]), 4, 0, "b in a", "__contains__")
323        self.binop_test(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__")
324        self.sliceop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]",
325                          "__getitem__")
326        self.setop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b",
327                        "__iadd__")
328        self.setop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b",
329                        "__imul__")
330        self.unop_test(spamlist([1,2,3]), 3, "len(a)", "__len__")
331        self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b",
332                        "__mul__")
333        self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a",
334                        "__rmul__")
335        self.set2op_test(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c",
336                         "__setitem__")
337        self.setsliceop_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]),
338                             spamlist([1,5,6,4]), "a[b:c]=d", "__setitem__")
339        # Test subclassing
340        class C(spam.spamlist):
341            def foo(self): return 1
342        a = C()
343        self.assertEqual(a, [])
344        self.assertEqual(a.foo(), 1)
345        a.append(100)
346        self.assertEqual(a, [100])
347        self.assertEqual(a.getstate(), 0)
348        a.setstate(42)
349        self.assertEqual(a.getstate(), 42)
350
351    @support.impl_detail("the module 'xxsubtype' is internal")
352    @unittest.skipIf(xxsubtype is None, "requires xxsubtype module")
353    def test_spam_dicts(self):
354        # Testing spamdict operations...
355        import copy, xxsubtype as spam
356        def spamdict(d, memo=None):
357            import xxsubtype as spam
358            sd = spam.spamdict()
359            for k, v in list(d.items()):
360                sd[k] = v
361            return sd
362        # This is an ugly hack:
363        copy._deepcopy_dispatch[spam.spamdict] = spamdict
364
365        self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
366        self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
367        self.binop_test(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__")
368        d = spamdict({1:2,3:4})
369        l1 = []
370        for i in list(d.keys()):
371            l1.append(i)
372        l = []
373        for i in iter(d):
374            l.append(i)
375        self.assertEqual(l, l1)
376        l = []
377        for i in d.__iter__():
378            l.append(i)
379        self.assertEqual(l, l1)
380        l = []
381        for i in type(spamdict({})).__iter__(d):
382            l.append(i)
383        self.assertEqual(l, l1)
384        straightd = {1:2, 3:4}
385        spamd = spamdict(straightd)
386        self.unop_test(spamd, 2, "len(a)", "__len__")
387        self.unop_test(spamd, repr(straightd), "repr(a)", "__repr__")
388        self.set2op_test(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}),
389                   "a[b]=c", "__setitem__")
390        # Test subclassing
391        class C(spam.spamdict):
392            def foo(self): return 1
393        a = C()
394        self.assertEqual(list(a.items()), [])
395        self.assertEqual(a.foo(), 1)
396        a['foo'] = 'bar'
397        self.assertEqual(list(a.items()), [('foo', 'bar')])
398        self.assertEqual(a.getstate(), 0)
399        a.setstate(100)
400        self.assertEqual(a.getstate(), 100)
401
402    def test_wrap_lenfunc_bad_cast(self):
403        self.assertEqual(range(sys.maxsize).__len__(), sys.maxsize)
404
405
406class ClassPropertiesAndMethods(unittest.TestCase):
407
408    def assertHasAttr(self, obj, name):
409        self.assertTrue(hasattr(obj, name),
410                        '%r has no attribute %r' % (obj, name))
411
412    def assertNotHasAttr(self, obj, name):
413        self.assertFalse(hasattr(obj, name),
414                         '%r has unexpected attribute %r' % (obj, name))
415
416    def test_python_dicts(self):
417        # Testing Python subclass of dict...
418        self.assertTrue(issubclass(dict, dict))
419        self.assertIsInstance({}, dict)
420        d = dict()
421        self.assertEqual(d, {})
422        self.assertIs(d.__class__, dict)
423        self.assertIsInstance(d, dict)
424        class C(dict):
425            state = -1
426            def __init__(self_local, *a, **kw):
427                if a:
428                    self.assertEqual(len(a), 1)
429                    self_local.state = a[0]
430                if kw:
431                    for k, v in list(kw.items()):
432                        self_local[v] = k
433            def __getitem__(self, key):
434                return self.get(key, 0)
435            def __setitem__(self_local, key, value):
436                self.assertIsInstance(key, int)
437                dict.__setitem__(self_local, key, value)
438            def setstate(self, state):
439                self.state = state
440            def getstate(self):
441                return self.state
442        self.assertTrue(issubclass(C, dict))
443        a1 = C(12)
444        self.assertEqual(a1.state, 12)
445        a2 = C(foo=1, bar=2)
446        self.assertEqual(a2[1] == 'foo' and a2[2], 'bar')
447        a = C()
448        self.assertEqual(a.state, -1)
449        self.assertEqual(a.getstate(), -1)
450        a.setstate(0)
451        self.assertEqual(a.state, 0)
452        self.assertEqual(a.getstate(), 0)
453        a.setstate(10)
454        self.assertEqual(a.state, 10)
455        self.assertEqual(a.getstate(), 10)
456        self.assertEqual(a[42], 0)
457        a[42] = 24
458        self.assertEqual(a[42], 24)
459        N = 50
460        for i in range(N):
461            a[i] = C()
462            for j in range(N):
463                a[i][j] = i*j
464        for i in range(N):
465            for j in range(N):
466                self.assertEqual(a[i][j], i*j)
467
468    def test_python_lists(self):
469        # Testing Python subclass of list...
470        class C(list):
471            def __getitem__(self, i):
472                if isinstance(i, slice):
473                    return i.start, i.stop
474                return list.__getitem__(self, i) + 100
475        a = C()
476        a.extend([0,1,2])
477        self.assertEqual(a[0], 100)
478        self.assertEqual(a[1], 101)
479        self.assertEqual(a[2], 102)
480        self.assertEqual(a[100:200], (100,200))
481
482    def test_metaclass(self):
483        # Testing metaclasses...
484        class C(metaclass=type):
485            def __init__(self):
486                self.__state = 0
487            def getstate(self):
488                return self.__state
489            def setstate(self, state):
490                self.__state = state
491        a = C()
492        self.assertEqual(a.getstate(), 0)
493        a.setstate(10)
494        self.assertEqual(a.getstate(), 10)
495        class _metaclass(type):
496            def myself(cls): return cls
497        class D(metaclass=_metaclass):
498            pass
499        self.assertEqual(D.myself(), D)
500        d = D()
501        self.assertEqual(d.__class__, D)
502        class M1(type):
503            def __new__(cls, name, bases, dict):
504                dict['__spam__'] = 1
505                return type.__new__(cls, name, bases, dict)
506        class C(metaclass=M1):
507            pass
508        self.assertEqual(C.__spam__, 1)
509        c = C()
510        self.assertEqual(c.__spam__, 1)
511
512        class _instance(object):
513            pass
514        class M2(object):
515            @staticmethod
516            def __new__(cls, name, bases, dict):
517                self = object.__new__(cls)
518                self.name = name
519                self.bases = bases
520                self.dict = dict
521                return self
522            def __call__(self):
523                it = _instance()
524                # Early binding of methods
525                for key in self.dict:
526                    if key.startswith("__"):
527                        continue
528                    setattr(it, key, self.dict[key].__get__(it, self))
529                return it
530        class C(metaclass=M2):
531            def spam(self):
532                return 42
533        self.assertEqual(C.name, 'C')
534        self.assertEqual(C.bases, ())
535        self.assertIn('spam', C.dict)
536        c = C()
537        self.assertEqual(c.spam(), 42)
538
539        # More metaclass examples
540
541        class autosuper(type):
542            # Automatically add __super to the class
543            # This trick only works for dynamic classes
544            def __new__(metaclass, name, bases, dict):
545                cls = super(autosuper, metaclass).__new__(metaclass,
546                                                          name, bases, dict)
547                # Name mangling for __super removes leading underscores
548                while name[:1] == "_":
549                    name = name[1:]
550                if name:
551                    name = "_%s__super" % name
552                else:
553                    name = "__super"
554                setattr(cls, name, super(cls))
555                return cls
556        class A(metaclass=autosuper):
557            def meth(self):
558                return "A"
559        class B(A):
560            def meth(self):
561                return "B" + self.__super.meth()
562        class C(A):
563            def meth(self):
564                return "C" + self.__super.meth()
565        class D(C, B):
566            def meth(self):
567                return "D" + self.__super.meth()
568        self.assertEqual(D().meth(), "DCBA")
569        class E(B, C):
570            def meth(self):
571                return "E" + self.__super.meth()
572        self.assertEqual(E().meth(), "EBCA")
573
574        class autoproperty(type):
575            # Automatically create property attributes when methods
576            # named _get_x and/or _set_x are found
577            def __new__(metaclass, name, bases, dict):
578                hits = {}
579                for key, val in dict.items():
580                    if key.startswith("_get_"):
581                        key = key[5:]
582                        get, set = hits.get(key, (None, None))
583                        get = val
584                        hits[key] = get, set
585                    elif key.startswith("_set_"):
586                        key = key[5:]
587                        get, set = hits.get(key, (None, None))
588                        set = val
589                        hits[key] = get, set
590                for key, (get, set) in hits.items():
591                    dict[key] = property(get, set)
592                return super(autoproperty, metaclass).__new__(metaclass,
593                                                            name, bases, dict)
594        class A(metaclass=autoproperty):
595            def _get_x(self):
596                return -self.__x
597            def _set_x(self, x):
598                self.__x = -x
599        a = A()
600        self.assertNotHasAttr(a, "x")
601        a.x = 12
602        self.assertEqual(a.x, 12)
603        self.assertEqual(a._A__x, -12)
604
605        class multimetaclass(autoproperty, autosuper):
606            # Merge of multiple cooperating metaclasses
607            pass
608        class A(metaclass=multimetaclass):
609            def _get_x(self):
610                return "A"
611        class B(A):
612            def _get_x(self):
613                return "B" + self.__super._get_x()
614        class C(A):
615            def _get_x(self):
616                return "C" + self.__super._get_x()
617        class D(C, B):
618            def _get_x(self):
619                return "D" + self.__super._get_x()
620        self.assertEqual(D().x, "DCBA")
621
622        # Make sure type(x) doesn't call x.__class__.__init__
623        class T(type):
624            counter = 0
625            def __init__(self, *args):
626                T.counter += 1
627        class C(metaclass=T):
628            pass
629        self.assertEqual(T.counter, 1)
630        a = C()
631        self.assertEqual(type(a), C)
632        self.assertEqual(T.counter, 1)
633
634        class C(object): pass
635        c = C()
636        try: c()
637        except TypeError: pass
638        else: self.fail("calling object w/o call method should raise "
639                        "TypeError")
640
641        # Testing code to find most derived baseclass
642        class A(type):
643            def __new__(*args, **kwargs):
644                return type.__new__(*args, **kwargs)
645
646        class B(object):
647            pass
648
649        class C(object, metaclass=A):
650            pass
651
652        # The most derived metaclass of D is A rather than type.
653        class D(B, C):
654            pass
655        self.assertIs(A, type(D))
656
657        # issue1294232: correct metaclass calculation
658        new_calls = []  # to check the order of __new__ calls
659        class AMeta(type):
660            @staticmethod
661            def __new__(mcls, name, bases, ns):
662                new_calls.append('AMeta')
663                return super().__new__(mcls, name, bases, ns)
664            @classmethod
665            def __prepare__(mcls, name, bases):
666                return {}
667
668        class BMeta(AMeta):
669            @staticmethod
670            def __new__(mcls, name, bases, ns):
671                new_calls.append('BMeta')
672                return super().__new__(mcls, name, bases, ns)
673            @classmethod
674            def __prepare__(mcls, name, bases):
675                ns = super().__prepare__(name, bases)
676                ns['BMeta_was_here'] = True
677                return ns
678
679        class A(metaclass=AMeta):
680            pass
681        self.assertEqual(['AMeta'], new_calls)
682        new_calls.clear()
683
684        class B(metaclass=BMeta):
685            pass
686        # BMeta.__new__ calls AMeta.__new__ with super:
687        self.assertEqual(['BMeta', 'AMeta'], new_calls)
688        new_calls.clear()
689
690        class C(A, B):
691            pass
692        # The most derived metaclass is BMeta:
693        self.assertEqual(['BMeta', 'AMeta'], new_calls)
694        new_calls.clear()
695        # BMeta.__prepare__ should've been called:
696        self.assertIn('BMeta_was_here', C.__dict__)
697
698        # The order of the bases shouldn't matter:
699        class C2(B, A):
700            pass
701        self.assertEqual(['BMeta', 'AMeta'], new_calls)
702        new_calls.clear()
703        self.assertIn('BMeta_was_here', C2.__dict__)
704
705        # Check correct metaclass calculation when a metaclass is declared:
706        class D(C, metaclass=type):
707            pass
708        self.assertEqual(['BMeta', 'AMeta'], new_calls)
709        new_calls.clear()
710        self.assertIn('BMeta_was_here', D.__dict__)
711
712        class E(C, metaclass=AMeta):
713            pass
714        self.assertEqual(['BMeta', 'AMeta'], new_calls)
715        new_calls.clear()
716        self.assertIn('BMeta_was_here', E.__dict__)
717
718        # Special case: the given metaclass isn't a class,
719        # so there is no metaclass calculation.
720        marker = object()
721        def func(*args, **kwargs):
722            return marker
723        class X(metaclass=func):
724            pass
725        class Y(object, metaclass=func):
726            pass
727        class Z(D, metaclass=func):
728            pass
729        self.assertIs(marker, X)
730        self.assertIs(marker, Y)
731        self.assertIs(marker, Z)
732
733        # The given metaclass is a class,
734        # but not a descendant of type.
735        prepare_calls = []  # to track __prepare__ calls
736        class ANotMeta:
737            def __new__(mcls, *args, **kwargs):
738                new_calls.append('ANotMeta')
739                return super().__new__(mcls)
740            @classmethod
741            def __prepare__(mcls, name, bases):
742                prepare_calls.append('ANotMeta')
743                return {}
744        class BNotMeta(ANotMeta):
745            def __new__(mcls, *args, **kwargs):
746                new_calls.append('BNotMeta')
747                return super().__new__(mcls)
748            @classmethod
749            def __prepare__(mcls, name, bases):
750                prepare_calls.append('BNotMeta')
751                return super().__prepare__(name, bases)
752
753        class A(metaclass=ANotMeta):
754            pass
755        self.assertIs(ANotMeta, type(A))
756        self.assertEqual(['ANotMeta'], prepare_calls)
757        prepare_calls.clear()
758        self.assertEqual(['ANotMeta'], new_calls)
759        new_calls.clear()
760
761        class B(metaclass=BNotMeta):
762            pass
763        self.assertIs(BNotMeta, type(B))
764        self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
765        prepare_calls.clear()
766        self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
767        new_calls.clear()
768
769        class C(A, B):
770            pass
771        self.assertIs(BNotMeta, type(C))
772        self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
773        new_calls.clear()
774        self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
775        prepare_calls.clear()
776
777        class C2(B, A):
778            pass
779        self.assertIs(BNotMeta, type(C2))
780        self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
781        new_calls.clear()
782        self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
783        prepare_calls.clear()
784
785        # This is a TypeError, because of a metaclass conflict:
786        # BNotMeta is neither a subclass, nor a superclass of type
787        with self.assertRaises(TypeError):
788            class D(C, metaclass=type):
789                pass
790
791        class E(C, metaclass=ANotMeta):
792            pass
793        self.assertIs(BNotMeta, type(E))
794        self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
795        new_calls.clear()
796        self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
797        prepare_calls.clear()
798
799        class F(object(), C):
800            pass
801        self.assertIs(BNotMeta, type(F))
802        self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
803        new_calls.clear()
804        self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
805        prepare_calls.clear()
806
807        class F2(C, object()):
808            pass
809        self.assertIs(BNotMeta, type(F2))
810        self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls)
811        new_calls.clear()
812        self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls)
813        prepare_calls.clear()
814
815        # TypeError: BNotMeta is neither a
816        # subclass, nor a superclass of int
817        with self.assertRaises(TypeError):
818            class X(C, int()):
819                pass
820        with self.assertRaises(TypeError):
821            class X(int(), C):
822                pass
823
824    def test_module_subclasses(self):
825        # Testing Python subclass of module...
826        log = []
827        MT = type(sys)
828        class MM(MT):
829            def __init__(self, name):
830                MT.__init__(self, name)
831            def __getattribute__(self, name):
832                log.append(("getattr", name))
833                return MT.__getattribute__(self, name)
834            def __setattr__(self, name, value):
835                log.append(("setattr", name, value))
836                MT.__setattr__(self, name, value)
837            def __delattr__(self, name):
838                log.append(("delattr", name))
839                MT.__delattr__(self, name)
840        a = MM("a")
841        a.foo = 12
842        x = a.foo
843        del a.foo
844        self.assertEqual(log, [("setattr", "foo", 12),
845                               ("getattr", "foo"),
846                               ("delattr", "foo")])
847
848        # https://bugs.python.org/issue1174712
849        try:
850            class Module(types.ModuleType, str):
851                pass
852        except TypeError:
853            pass
854        else:
855            self.fail("inheriting from ModuleType and str at the same time "
856                      "should fail")
857
858        # Issue 34805: Verify that definition order is retained
859        def random_name():
860            return ''.join(random.choices(string.ascii_letters, k=10))
861        class A:
862            pass
863        subclasses = [type(random_name(), (A,), {}) for i in range(100)]
864        self.assertEqual(A.__subclasses__(), subclasses)
865
866    def test_multiple_inheritance(self):
867        # Testing multiple inheritance...
868        class C(object):
869            def __init__(self):
870                self.__state = 0
871            def getstate(self):
872                return self.__state
873            def setstate(self, state):
874                self.__state = state
875        a = C()
876        self.assertEqual(a.getstate(), 0)
877        a.setstate(10)
878        self.assertEqual(a.getstate(), 10)
879        class D(dict, C):
880            def __init__(self):
881                dict.__init__(self)
882                C.__init__(self)
883        d = D()
884        self.assertEqual(list(d.keys()), [])
885        d["hello"] = "world"
886        self.assertEqual(list(d.items()), [("hello", "world")])
887        self.assertEqual(d["hello"], "world")
888        self.assertEqual(d.getstate(), 0)
889        d.setstate(10)
890        self.assertEqual(d.getstate(), 10)
891        self.assertEqual(D.__mro__, (D, dict, C, object))
892
893        # SF bug #442833
894        class Node(object):
895            def __int__(self):
896                return int(self.foo())
897            def foo(self):
898                return "23"
899        class Frag(Node, list):
900            def foo(self):
901                return "42"
902        self.assertEqual(Node().__int__(), 23)
903        self.assertEqual(int(Node()), 23)
904        self.assertEqual(Frag().__int__(), 42)
905        self.assertEqual(int(Frag()), 42)
906
907    def test_diamond_inheritance(self):
908        # Testing multiple inheritance special cases...
909        class A(object):
910            def spam(self): return "A"
911        self.assertEqual(A().spam(), "A")
912        class B(A):
913            def boo(self): return "B"
914            def spam(self): return "B"
915        self.assertEqual(B().spam(), "B")
916        self.assertEqual(B().boo(), "B")
917        class C(A):
918            def boo(self): return "C"
919        self.assertEqual(C().spam(), "A")
920        self.assertEqual(C().boo(), "C")
921        class D(B, C): pass
922        self.assertEqual(D().spam(), "B")
923        self.assertEqual(D().boo(), "B")
924        self.assertEqual(D.__mro__, (D, B, C, A, object))
925        class E(C, B): pass
926        self.assertEqual(E().spam(), "B")
927        self.assertEqual(E().boo(), "C")
928        self.assertEqual(E.__mro__, (E, C, B, A, object))
929        # MRO order disagreement
930        try:
931            class F(D, E): pass
932        except TypeError:
933            pass
934        else:
935            self.fail("expected MRO order disagreement (F)")
936        try:
937            class G(E, D): pass
938        except TypeError:
939            pass
940        else:
941            self.fail("expected MRO order disagreement (G)")
942
943    # see thread python-dev/2002-October/029035.html
944    def test_ex5_from_c3_switch(self):
945        # Testing ex5 from C3 switch discussion...
946        class A(object): pass
947        class B(object): pass
948        class C(object): pass
949        class X(A): pass
950        class Y(A): pass
951        class Z(X,B,Y,C): pass
952        self.assertEqual(Z.__mro__, (Z, X, B, Y, A, C, object))
953
954    # see "A Monotonic Superclass Linearization for Dylan",
955    # by Kim Barrett et al. (OOPSLA 1996)
956    def test_monotonicity(self):
957        # Testing MRO monotonicity...
958        class Boat(object): pass
959        class DayBoat(Boat): pass
960        class WheelBoat(Boat): pass
961        class EngineLess(DayBoat): pass
962        class SmallMultihull(DayBoat): pass
963        class PedalWheelBoat(EngineLess,WheelBoat): pass
964        class SmallCatamaran(SmallMultihull): pass
965        class Pedalo(PedalWheelBoat,SmallCatamaran): pass
966
967        self.assertEqual(PedalWheelBoat.__mro__,
968              (PedalWheelBoat, EngineLess, DayBoat, WheelBoat, Boat, object))
969        self.assertEqual(SmallCatamaran.__mro__,
970              (SmallCatamaran, SmallMultihull, DayBoat, Boat, object))
971        self.assertEqual(Pedalo.__mro__,
972              (Pedalo, PedalWheelBoat, EngineLess, SmallCatamaran,
973               SmallMultihull, DayBoat, WheelBoat, Boat, object))
974
975    # see "A Monotonic Superclass Linearization for Dylan",
976    # by Kim Barrett et al. (OOPSLA 1996)
977    def test_consistency_with_epg(self):
978        # Testing consistency with EPG...
979        class Pane(object): pass
980        class ScrollingMixin(object): pass
981        class EditingMixin(object): pass
982        class ScrollablePane(Pane,ScrollingMixin): pass
983        class EditablePane(Pane,EditingMixin): pass
984        class EditableScrollablePane(ScrollablePane,EditablePane): pass
985
986        self.assertEqual(EditableScrollablePane.__mro__,
987              (EditableScrollablePane, ScrollablePane, EditablePane, Pane,
988                ScrollingMixin, EditingMixin, object))
989
990    def test_mro_disagreement(self):
991        # Testing error messages for MRO disagreement...
992        mro_err_msg = ("Cannot create a consistent method resolution "
993                       "order (MRO) for bases ")
994
995        def raises(exc, expected, callable, *args):
996            try:
997                callable(*args)
998            except exc as msg:
999                # the exact msg is generally considered an impl detail
1000                if support.check_impl_detail():
1001                    if not str(msg).startswith(expected):
1002                        self.fail("Message %r, expected %r" %
1003                                  (str(msg), expected))
1004            else:
1005                self.fail("Expected %s" % exc)
1006
1007        class A(object): pass
1008        class B(A): pass
1009        class C(object): pass
1010
1011        # Test some very simple errors
1012        raises(TypeError, "duplicate base class A",
1013               type, "X", (A, A), {})
1014        raises(TypeError, mro_err_msg,
1015               type, "X", (A, B), {})
1016        raises(TypeError, mro_err_msg,
1017               type, "X", (A, C, B), {})
1018        # Test a slightly more complex error
1019        class GridLayout(object): pass
1020        class HorizontalGrid(GridLayout): pass
1021        class VerticalGrid(GridLayout): pass
1022        class HVGrid(HorizontalGrid, VerticalGrid): pass
1023        class VHGrid(VerticalGrid, HorizontalGrid): pass
1024        raises(TypeError, mro_err_msg,
1025               type, "ConfusedGrid", (HVGrid, VHGrid), {})
1026
1027    def test_object_class(self):
1028        # Testing object class...
1029        a = object()
1030        self.assertEqual(a.__class__, object)
1031        self.assertEqual(type(a), object)
1032        b = object()
1033        self.assertNotEqual(a, b)
1034        self.assertNotHasAttr(a, "foo")
1035        try:
1036            a.foo = 12
1037        except (AttributeError, TypeError):
1038            pass
1039        else:
1040            self.fail("object() should not allow setting a foo attribute")
1041        self.assertNotHasAttr(object(), "__dict__")
1042
1043        class Cdict(object):
1044            pass
1045        x = Cdict()
1046        self.assertEqual(x.__dict__, {})
1047        x.foo = 1
1048        self.assertEqual(x.foo, 1)
1049        self.assertEqual(x.__dict__, {'foo': 1})
1050
1051    def test_object_class_assignment_between_heaptypes_and_nonheaptypes(self):
1052        class SubType(types.ModuleType):
1053            a = 1
1054
1055        m = types.ModuleType("m")
1056        self.assertTrue(m.__class__ is types.ModuleType)
1057        self.assertFalse(hasattr(m, "a"))
1058
1059        m.__class__ = SubType
1060        self.assertTrue(m.__class__ is SubType)
1061        self.assertTrue(hasattr(m, "a"))
1062
1063        m.__class__ = types.ModuleType
1064        self.assertTrue(m.__class__ is types.ModuleType)
1065        self.assertFalse(hasattr(m, "a"))
1066
1067        # Make sure that builtin immutable objects don't support __class__
1068        # assignment, because the object instances may be interned.
1069        # We set __slots__ = () to ensure that the subclasses are
1070        # memory-layout compatible, and thus otherwise reasonable candidates
1071        # for __class__ assignment.
1072
1073        # The following types have immutable instances, but are not
1074        # subclassable and thus don't need to be checked:
1075        #   NoneType, bool
1076
1077        class MyInt(int):
1078            __slots__ = ()
1079        with self.assertRaises(TypeError):
1080            (1).__class__ = MyInt
1081
1082        class MyFloat(float):
1083            __slots__ = ()
1084        with self.assertRaises(TypeError):
1085            (1.0).__class__ = MyFloat
1086
1087        class MyComplex(complex):
1088            __slots__ = ()
1089        with self.assertRaises(TypeError):
1090            (1 + 2j).__class__ = MyComplex
1091
1092        class MyStr(str):
1093            __slots__ = ()
1094        with self.assertRaises(TypeError):
1095            "a".__class__ = MyStr
1096
1097        class MyBytes(bytes):
1098            __slots__ = ()
1099        with self.assertRaises(TypeError):
1100            b"a".__class__ = MyBytes
1101
1102        class MyTuple(tuple):
1103            __slots__ = ()
1104        with self.assertRaises(TypeError):
1105            ().__class__ = MyTuple
1106
1107        class MyFrozenSet(frozenset):
1108            __slots__ = ()
1109        with self.assertRaises(TypeError):
1110            frozenset().__class__ = MyFrozenSet
1111
1112    def test_slots(self):
1113        # Testing __slots__...
1114        class C0(object):
1115            __slots__ = []
1116        x = C0()
1117        self.assertNotHasAttr(x, "__dict__")
1118        self.assertNotHasAttr(x, "foo")
1119
1120        class C1(object):
1121            __slots__ = ['a']
1122        x = C1()
1123        self.assertNotHasAttr(x, "__dict__")
1124        self.assertNotHasAttr(x, "a")
1125        x.a = 1
1126        self.assertEqual(x.a, 1)
1127        x.a = None
1128        self.assertEqual(x.a, None)
1129        del x.a
1130        self.assertNotHasAttr(x, "a")
1131
1132        class C3(object):
1133            __slots__ = ['a', 'b', 'c']
1134        x = C3()
1135        self.assertNotHasAttr(x, "__dict__")
1136        self.assertNotHasAttr(x, 'a')
1137        self.assertNotHasAttr(x, 'b')
1138        self.assertNotHasAttr(x, 'c')
1139        x.a = 1
1140        x.b = 2
1141        x.c = 3
1142        self.assertEqual(x.a, 1)
1143        self.assertEqual(x.b, 2)
1144        self.assertEqual(x.c, 3)
1145
1146        class C4(object):
1147            """Validate name mangling"""
1148            __slots__ = ['__a']
1149            def __init__(self, value):
1150                self.__a = value
1151            def get(self):
1152                return self.__a
1153        x = C4(5)
1154        self.assertNotHasAttr(x, '__dict__')
1155        self.assertNotHasAttr(x, '__a')
1156        self.assertEqual(x.get(), 5)
1157        try:
1158            x.__a = 6
1159        except AttributeError:
1160            pass
1161        else:
1162            self.fail("Double underscored names not mangled")
1163
1164        # Make sure slot names are proper identifiers
1165        try:
1166            class C(object):
1167                __slots__ = [None]
1168        except TypeError:
1169            pass
1170        else:
1171            self.fail("[None] slots not caught")
1172        try:
1173            class C(object):
1174                __slots__ = ["foo bar"]
1175        except TypeError:
1176            pass
1177        else:
1178            self.fail("['foo bar'] slots not caught")
1179        try:
1180            class C(object):
1181                __slots__ = ["foo\0bar"]
1182        except TypeError:
1183            pass
1184        else:
1185            self.fail("['foo\\0bar'] slots not caught")
1186        try:
1187            class C(object):
1188                __slots__ = ["1"]
1189        except TypeError:
1190            pass
1191        else:
1192            self.fail("['1'] slots not caught")
1193        try:
1194            class C(object):
1195                __slots__ = [""]
1196        except TypeError:
1197            pass
1198        else:
1199            self.fail("[''] slots not caught")
1200        class C(object):
1201            __slots__ = ["a", "a_b", "_a", "A0123456789Z"]
1202        # XXX(nnorwitz): was there supposed to be something tested
1203        # from the class above?
1204
1205        # Test a single 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)
1211
1212        # Test unicode slot names
1213        # Test a single unicode string is not expanded as a sequence.
1214        class C(object):
1215            __slots__ = "abc"
1216        c = C()
1217        c.abc = 5
1218        self.assertEqual(c.abc, 5)
1219
1220        # _unicode_to_string used to modify slots in certain circumstances
1221        slots = ("foo", "bar")
1222        class C(object):
1223            __slots__ = slots
1224        x = C()
1225        x.foo = 5
1226        self.assertEqual(x.foo, 5)
1227        self.assertIs(type(slots[0]), str)
1228        # this used to leak references
1229        try:
1230            class C(object):
1231                __slots__ = [chr(128)]
1232        except (TypeError, UnicodeEncodeError):
1233            pass
1234        else:
1235            self.fail("[chr(128)] slots not caught")
1236
1237        # Test leaks
1238        class Counted(object):
1239            counter = 0    # counts the number of instances alive
1240            def __init__(self):
1241                Counted.counter += 1
1242            def __del__(self):
1243                Counted.counter -= 1
1244        class C(object):
1245            __slots__ = ['a', 'b', 'c']
1246        x = C()
1247        x.a = Counted()
1248        x.b = Counted()
1249        x.c = Counted()
1250        self.assertEqual(Counted.counter, 3)
1251        del x
1252        support.gc_collect()
1253        self.assertEqual(Counted.counter, 0)
1254        class D(C):
1255            pass
1256        x = D()
1257        x.a = Counted()
1258        x.z = Counted()
1259        self.assertEqual(Counted.counter, 2)
1260        del x
1261        support.gc_collect()
1262        self.assertEqual(Counted.counter, 0)
1263        class E(D):
1264            __slots__ = ['e']
1265        x = E()
1266        x.a = Counted()
1267        x.z = Counted()
1268        x.e = Counted()
1269        self.assertEqual(Counted.counter, 3)
1270        del x
1271        support.gc_collect()
1272        self.assertEqual(Counted.counter, 0)
1273
1274        # Test cyclical leaks [SF bug 519621]
1275        class F(object):
1276            __slots__ = ['a', 'b']
1277        s = F()
1278        s.a = [Counted(), s]
1279        self.assertEqual(Counted.counter, 1)
1280        s = None
1281        support.gc_collect()
1282        self.assertEqual(Counted.counter, 0)
1283
1284        # Test lookup leaks [SF bug 572567]
1285        if hasattr(gc, 'get_objects'):
1286            class G(object):
1287                def __eq__(self, other):
1288                    return False
1289            g = G()
1290            orig_objects = len(gc.get_objects())
1291            for i in range(10):
1292                g==g
1293            new_objects = len(gc.get_objects())
1294            self.assertEqual(orig_objects, new_objects)
1295
1296        class H(object):
1297            __slots__ = ['a', 'b']
1298            def __init__(self):
1299                self.a = 1
1300                self.b = 2
1301            def __del__(self_):
1302                self.assertEqual(self_.a, 1)
1303                self.assertEqual(self_.b, 2)
1304        with support.captured_output('stderr') as s:
1305            h = H()
1306            del h
1307        self.assertEqual(s.getvalue(), '')
1308
1309        class X(object):
1310            __slots__ = "a"
1311        with self.assertRaises(AttributeError):
1312            del X().a
1313
1314        # Inherit from object on purpose to check some backwards compatibility paths
1315        class X(object):
1316            __slots__ = "a"
1317        with self.assertRaisesRegex(AttributeError, "'test.test_descr.ClassPropertiesAndMethods.test_slots.<locals>.X' object has no attribute 'a'"):
1318            X().a
1319
1320        # Test string subclass in `__slots__`, see gh-98783
1321        class SubStr(str):
1322            pass
1323        class X(object):
1324            __slots__ = (SubStr('x'),)
1325        X().x = 1
1326        with self.assertRaisesRegex(AttributeError, "'X' object has no attribute 'a'"):
1327            X().a
1328
1329    def test_slots_special(self):
1330        # Testing __dict__ and __weakref__ in __slots__...
1331        class D(object):
1332            __slots__ = ["__dict__"]
1333        a = D()
1334        self.assertHasAttr(a, "__dict__")
1335        self.assertNotHasAttr(a, "__weakref__")
1336        a.foo = 42
1337        self.assertEqual(a.__dict__, {"foo": 42})
1338
1339        class W(object):
1340            __slots__ = ["__weakref__"]
1341        a = W()
1342        self.assertHasAttr(a, "__weakref__")
1343        self.assertNotHasAttr(a, "__dict__")
1344        try:
1345            a.foo = 42
1346        except AttributeError:
1347            pass
1348        else:
1349            self.fail("shouldn't be allowed to set a.foo")
1350
1351        class C1(W, D):
1352            __slots__ = []
1353        a = C1()
1354        self.assertHasAttr(a, "__dict__")
1355        self.assertHasAttr(a, "__weakref__")
1356        a.foo = 42
1357        self.assertEqual(a.__dict__, {"foo": 42})
1358
1359        class C2(D, W):
1360            __slots__ = []
1361        a = C2()
1362        self.assertHasAttr(a, "__dict__")
1363        self.assertHasAttr(a, "__weakref__")
1364        a.foo = 42
1365        self.assertEqual(a.__dict__, {"foo": 42})
1366
1367    def test_slots_special2(self):
1368        # Testing __qualname__ and __classcell__ in __slots__
1369        class Meta(type):
1370            def __new__(cls, name, bases, namespace, attr):
1371                self.assertIn(attr, namespace)
1372                return super().__new__(cls, name, bases, namespace)
1373
1374        class C1:
1375            def __init__(self):
1376                self.b = 42
1377        class C2(C1, metaclass=Meta, attr="__classcell__"):
1378            __slots__ = ["__classcell__"]
1379            def __init__(self):
1380                super().__init__()
1381        self.assertIsInstance(C2.__dict__["__classcell__"],
1382                              types.MemberDescriptorType)
1383        c = C2()
1384        self.assertEqual(c.b, 42)
1385        self.assertNotHasAttr(c, "__classcell__")
1386        c.__classcell__ = 42
1387        self.assertEqual(c.__classcell__, 42)
1388        with self.assertRaises(TypeError):
1389            class C3:
1390                __classcell__ = 42
1391                __slots__ = ["__classcell__"]
1392
1393        class Q1(metaclass=Meta, attr="__qualname__"):
1394            __slots__ = ["__qualname__"]
1395        self.assertEqual(Q1.__qualname__, C1.__qualname__[:-2] + "Q1")
1396        self.assertIsInstance(Q1.__dict__["__qualname__"],
1397                              types.MemberDescriptorType)
1398        q = Q1()
1399        self.assertNotHasAttr(q, "__qualname__")
1400        q.__qualname__ = "q"
1401        self.assertEqual(q.__qualname__, "q")
1402        with self.assertRaises(TypeError):
1403            class Q2:
1404                __qualname__ = object()
1405                __slots__ = ["__qualname__"]
1406
1407    def test_slots_descriptor(self):
1408        # Issue2115: slot descriptors did not correctly check
1409        # the type of the given object
1410        import abc
1411        class MyABC(metaclass=abc.ABCMeta):
1412            __slots__ = "a"
1413
1414        class Unrelated(object):
1415            pass
1416        MyABC.register(Unrelated)
1417
1418        u = Unrelated()
1419        self.assertIsInstance(u, MyABC)
1420
1421        # This used to crash
1422        self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
1423
1424    def test_dynamics(self):
1425        # Testing class attribute propagation...
1426        class D(object):
1427            pass
1428        class E(D):
1429            pass
1430        class F(D):
1431            pass
1432        D.foo = 1
1433        self.assertEqual(D.foo, 1)
1434        # Test that dynamic attributes are inherited
1435        self.assertEqual(E.foo, 1)
1436        self.assertEqual(F.foo, 1)
1437        # Test dynamic instances
1438        class C(object):
1439            pass
1440        a = C()
1441        self.assertNotHasAttr(a, "foobar")
1442        C.foobar = 2
1443        self.assertEqual(a.foobar, 2)
1444        C.method = lambda self: 42
1445        self.assertEqual(a.method(), 42)
1446        C.__repr__ = lambda self: "C()"
1447        self.assertEqual(repr(a), "C()")
1448        C.__int__ = lambda self: 100
1449        self.assertEqual(int(a), 100)
1450        self.assertEqual(a.foobar, 2)
1451        self.assertNotHasAttr(a, "spam")
1452        def mygetattr(self, name):
1453            if name == "spam":
1454                return "spam"
1455            raise AttributeError
1456        C.__getattr__ = mygetattr
1457        self.assertEqual(a.spam, "spam")
1458        a.new = 12
1459        self.assertEqual(a.new, 12)
1460        def mysetattr(self, name, value):
1461            if name == "spam":
1462                raise AttributeError
1463            return object.__setattr__(self, name, value)
1464        C.__setattr__ = mysetattr
1465        with self.assertRaises(AttributeError):
1466            a.spam = "not spam"
1467
1468        self.assertEqual(a.spam, "spam")
1469        class D(C):
1470            pass
1471        d = D()
1472        d.foo = 1
1473        self.assertEqual(d.foo, 1)
1474
1475        # Test handling of int*seq and seq*int
1476        class I(int):
1477            pass
1478        self.assertEqual("a"*I(2), "aa")
1479        self.assertEqual(I(2)*"a", "aa")
1480        self.assertEqual(2*I(3), 6)
1481        self.assertEqual(I(3)*2, 6)
1482        self.assertEqual(I(3)*I(2), 6)
1483
1484        # Test comparison of classes with dynamic metaclasses
1485        class dynamicmetaclass(type):
1486            pass
1487        class someclass(metaclass=dynamicmetaclass):
1488            pass
1489        self.assertNotEqual(someclass, object)
1490
1491    def test_errors(self):
1492        # Testing errors...
1493        try:
1494            class C(list, dict):
1495                pass
1496        except TypeError:
1497            pass
1498        else:
1499            self.fail("inheritance from both list and dict should be illegal")
1500
1501        try:
1502            class C(object, None):
1503                pass
1504        except TypeError:
1505            pass
1506        else:
1507            self.fail("inheritance from non-type should be illegal")
1508        class Classic:
1509            pass
1510
1511        try:
1512            class C(type(len)):
1513                pass
1514        except TypeError:
1515            pass
1516        else:
1517            self.fail("inheritance from CFunction should be illegal")
1518
1519        try:
1520            class C(object):
1521                __slots__ = 1
1522        except TypeError:
1523            pass
1524        else:
1525            self.fail("__slots__ = 1 should be illegal")
1526
1527        try:
1528            class C(object):
1529                __slots__ = [1]
1530        except TypeError:
1531            pass
1532        else:
1533            self.fail("__slots__ = [1] should be illegal")
1534
1535        class M1(type):
1536            pass
1537        class M2(type):
1538            pass
1539        class A1(object, metaclass=M1):
1540            pass
1541        class A2(object, metaclass=M2):
1542            pass
1543        try:
1544            class B(A1, A2):
1545                pass
1546        except TypeError:
1547            pass
1548        else:
1549            self.fail("finding the most derived metaclass should have failed")
1550
1551    def test_classmethods(self):
1552        # Testing class methods...
1553        class C(object):
1554            def foo(*a): return a
1555            goo = classmethod(foo)
1556        c = C()
1557        self.assertEqual(C.goo(1), (C, 1))
1558        self.assertEqual(c.goo(1), (C, 1))
1559        self.assertEqual(c.foo(1), (c, 1))
1560        class D(C):
1561            pass
1562        d = D()
1563        self.assertEqual(D.goo(1), (D, 1))
1564        self.assertEqual(d.goo(1), (D, 1))
1565        self.assertEqual(d.foo(1), (d, 1))
1566        self.assertEqual(D.foo(d, 1), (d, 1))
1567        # Test for a specific crash (SF bug 528132)
1568        def f(cls, arg):
1569            "f docstring"
1570            return (cls, arg)
1571        ff = classmethod(f)
1572        self.assertEqual(ff.__get__(0, int)(42), (int, 42))
1573        self.assertEqual(ff.__get__(0)(42), (int, 42))
1574
1575        # Test super() with classmethods (SF bug 535444)
1576        self.assertEqual(C.goo.__self__, C)
1577        self.assertEqual(D.goo.__self__, D)
1578        self.assertEqual(super(D,D).goo.__self__, D)
1579        self.assertEqual(super(D,d).goo.__self__, D)
1580        self.assertEqual(super(D,D).goo(), (D,))
1581        self.assertEqual(super(D,d).goo(), (D,))
1582
1583        # Verify that a non-callable will raise
1584        meth = classmethod(1).__get__(1)
1585        self.assertRaises(TypeError, meth)
1586
1587        # Verify that classmethod() doesn't allow keyword args
1588        try:
1589            classmethod(f, kw=1)
1590        except TypeError:
1591            pass
1592        else:
1593            self.fail("classmethod shouldn't accept keyword args")
1594
1595        cm = classmethod(f)
1596        cm_dict = {'__annotations__': {},
1597                   '__doc__': (
1598                       "f docstring"
1599                       if support.HAVE_DOCSTRINGS
1600                       else None
1601                    ),
1602                   '__module__': __name__,
1603                   '__name__': 'f',
1604                   '__qualname__': f.__qualname__}
1605        self.assertEqual(cm.__dict__, cm_dict)
1606
1607        cm.x = 42
1608        self.assertEqual(cm.x, 42)
1609        self.assertEqual(cm.__dict__, {"x" : 42, **cm_dict})
1610        del cm.x
1611        self.assertNotHasAttr(cm, "x")
1612
1613    @support.refcount_test
1614    def test_refleaks_in_classmethod___init__(self):
1615        gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
1616        cm = classmethod(None)
1617        refs_before = gettotalrefcount()
1618        for i in range(100):
1619            cm.__init__(None)
1620        self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
1621
1622    @support.impl_detail("the module 'xxsubtype' is internal")
1623    @unittest.skipIf(xxsubtype is None, "requires xxsubtype module")
1624    def test_classmethods_in_c(self):
1625        # Testing C-based class methods...
1626        import xxsubtype as spam
1627        a = (1, 2, 3)
1628        d = {'abc': 123}
1629        x, a1, d1 = spam.spamlist.classmeth(*a, **d)
1630        self.assertEqual(x, spam.spamlist)
1631        self.assertEqual(a, a1)
1632        self.assertEqual(d, d1)
1633        x, a1, d1 = spam.spamlist().classmeth(*a, **d)
1634        self.assertEqual(x, spam.spamlist)
1635        self.assertEqual(a, a1)
1636        self.assertEqual(d, d1)
1637        spam_cm = spam.spamlist.__dict__['classmeth']
1638        x2, a2, d2 = spam_cm(spam.spamlist, *a, **d)
1639        self.assertEqual(x2, spam.spamlist)
1640        self.assertEqual(a2, a1)
1641        self.assertEqual(d2, d1)
1642        class SubSpam(spam.spamlist): pass
1643        x2, a2, d2 = spam_cm(SubSpam, *a, **d)
1644        self.assertEqual(x2, SubSpam)
1645        self.assertEqual(a2, a1)
1646        self.assertEqual(d2, d1)
1647
1648        with self.assertRaises(TypeError) as cm:
1649            spam_cm()
1650        self.assertEqual(
1651            str(cm.exception),
1652            "descriptor 'classmeth' of 'xxsubtype.spamlist' "
1653            "object needs an argument")
1654
1655        with self.assertRaises(TypeError) as cm:
1656            spam_cm(spam.spamlist())
1657        self.assertEqual(
1658            str(cm.exception),
1659            "descriptor 'classmeth' for type 'xxsubtype.spamlist' "
1660            "needs a type, not a 'xxsubtype.spamlist' as arg 2")
1661
1662        with self.assertRaises(TypeError) as cm:
1663            spam_cm(list)
1664        expected_errmsg = (
1665            "descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' "
1666            "but received 'list'")
1667        self.assertEqual(str(cm.exception), expected_errmsg)
1668
1669        with self.assertRaises(TypeError) as cm:
1670            spam_cm.__get__(None, list)
1671        self.assertEqual(str(cm.exception), expected_errmsg)
1672
1673    def test_staticmethods(self):
1674        # Testing static methods...
1675        class C(object):
1676            def foo(*a): return a
1677            goo = staticmethod(foo)
1678        c = C()
1679        self.assertEqual(C.goo(1), (1,))
1680        self.assertEqual(c.goo(1), (1,))
1681        self.assertEqual(c.foo(1), (c, 1,))
1682        class D(C):
1683            pass
1684        d = D()
1685        self.assertEqual(D.goo(1), (1,))
1686        self.assertEqual(d.goo(1), (1,))
1687        self.assertEqual(d.foo(1), (d, 1))
1688        self.assertEqual(D.foo(d, 1), (d, 1))
1689        sm = staticmethod(None)
1690        self.assertEqual(sm.__dict__, {'__doc__': None.__doc__})
1691        sm.x = 42
1692        self.assertEqual(sm.x, 42)
1693        self.assertEqual(sm.__dict__, {"x" : 42, '__doc__': None.__doc__})
1694        del sm.x
1695        self.assertNotHasAttr(sm, "x")
1696
1697    @support.refcount_test
1698    def test_refleaks_in_staticmethod___init__(self):
1699        gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
1700        sm = staticmethod(None)
1701        refs_before = gettotalrefcount()
1702        for i in range(100):
1703            sm.__init__(None)
1704        self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
1705
1706    @support.impl_detail("the module 'xxsubtype' is internal")
1707    @unittest.skipIf(xxsubtype is None, "requires xxsubtype module")
1708    def test_staticmethods_in_c(self):
1709        # Testing C-based static methods...
1710        import xxsubtype as spam
1711        a = (1, 2, 3)
1712        d = {"abc": 123}
1713        x, a1, d1 = spam.spamlist.staticmeth(*a, **d)
1714        self.assertEqual(x, None)
1715        self.assertEqual(a, a1)
1716        self.assertEqual(d, d1)
1717        x, a1, d2 = spam.spamlist().staticmeth(*a, **d)
1718        self.assertEqual(x, None)
1719        self.assertEqual(a, a1)
1720        self.assertEqual(d, d1)
1721
1722    def test_classic(self):
1723        # Testing classic classes...
1724        class C:
1725            def foo(*a): return a
1726            goo = classmethod(foo)
1727        c = C()
1728        self.assertEqual(C.goo(1), (C, 1))
1729        self.assertEqual(c.goo(1), (C, 1))
1730        self.assertEqual(c.foo(1), (c, 1))
1731        class D(C):
1732            pass
1733        d = D()
1734        self.assertEqual(D.goo(1), (D, 1))
1735        self.assertEqual(d.goo(1), (D, 1))
1736        self.assertEqual(d.foo(1), (d, 1))
1737        self.assertEqual(D.foo(d, 1), (d, 1))
1738        class E: # *not* subclassing from C
1739            foo = C.foo
1740        self.assertEqual(E().foo.__func__, C.foo) # i.e., unbound
1741        self.assertTrue(repr(C.foo.__get__(C())).startswith("<bound method "))
1742
1743    def test_compattr(self):
1744        # Testing computed attributes...
1745        class C(object):
1746            class computed_attribute(object):
1747                def __init__(self, get, set=None, delete=None):
1748                    self.__get = get
1749                    self.__set = set
1750                    self.__delete = delete
1751                def __get__(self, obj, type=None):
1752                    return self.__get(obj)
1753                def __set__(self, obj, value):
1754                    return self.__set(obj, value)
1755                def __delete__(self, obj):
1756                    return self.__delete(obj)
1757            def __init__(self):
1758                self.__x = 0
1759            def __get_x(self):
1760                x = self.__x
1761                self.__x = x+1
1762                return x
1763            def __set_x(self, x):
1764                self.__x = x
1765            def __delete_x(self):
1766                del self.__x
1767            x = computed_attribute(__get_x, __set_x, __delete_x)
1768        a = C()
1769        self.assertEqual(a.x, 0)
1770        self.assertEqual(a.x, 1)
1771        a.x = 10
1772        self.assertEqual(a.x, 10)
1773        self.assertEqual(a.x, 11)
1774        del a.x
1775        self.assertNotHasAttr(a, 'x')
1776
1777    def test_newslots(self):
1778        # Testing __new__ slot override...
1779        class C(list):
1780            def __new__(cls):
1781                self = list.__new__(cls)
1782                self.foo = 1
1783                return self
1784            def __init__(self):
1785                self.foo = self.foo + 2
1786        a = C()
1787        self.assertEqual(a.foo, 3)
1788        self.assertEqual(a.__class__, C)
1789        class D(C):
1790            pass
1791        b = D()
1792        self.assertEqual(b.foo, 3)
1793        self.assertEqual(b.__class__, D)
1794
1795    @unittest.expectedFailure
1796    def test_bad_new(self):
1797        self.assertRaises(TypeError, object.__new__)
1798        self.assertRaises(TypeError, object.__new__, '')
1799        self.assertRaises(TypeError, list.__new__, object)
1800        self.assertRaises(TypeError, object.__new__, list)
1801        class C(object):
1802            __new__ = list.__new__
1803        self.assertRaises(TypeError, C)
1804        class C(list):
1805            __new__ = object.__new__
1806        self.assertRaises(TypeError, C)
1807
1808    def test_object_new(self):
1809        class A(object):
1810            pass
1811        object.__new__(A)
1812        self.assertRaises(TypeError, object.__new__, A, 5)
1813        object.__init__(A())
1814        self.assertRaises(TypeError, object.__init__, A(), 5)
1815
1816        class A(object):
1817            def __init__(self, foo):
1818                self.foo = foo
1819        object.__new__(A)
1820        object.__new__(A, 5)
1821        object.__init__(A(3))
1822        self.assertRaises(TypeError, object.__init__, A(3), 5)
1823
1824        class A(object):
1825            def __new__(cls, foo):
1826                return object.__new__(cls)
1827        object.__new__(A)
1828        self.assertRaises(TypeError, object.__new__, A, 5)
1829        object.__init__(A(3))
1830        object.__init__(A(3), 5)
1831
1832        class A(object):
1833            def __new__(cls, foo):
1834                return object.__new__(cls)
1835            def __init__(self, foo):
1836                self.foo = foo
1837        object.__new__(A)
1838        self.assertRaises(TypeError, object.__new__, A, 5)
1839        object.__init__(A(3))
1840        self.assertRaises(TypeError, object.__init__, A(3), 5)
1841
1842    @unittest.expectedFailure
1843    def test_restored_object_new(self):
1844        class A(object):
1845            def __new__(cls, *args, **kwargs):
1846                raise AssertionError
1847        self.assertRaises(AssertionError, A)
1848        class B(A):
1849            __new__ = object.__new__
1850            def __init__(self, foo):
1851                self.foo = foo
1852        with warnings.catch_warnings():
1853            warnings.simplefilter('error', DeprecationWarning)
1854            b = B(3)
1855        self.assertEqual(b.foo, 3)
1856        self.assertEqual(b.__class__, B)
1857        del B.__new__
1858        self.assertRaises(AssertionError, B)
1859        del A.__new__
1860        with warnings.catch_warnings():
1861            warnings.simplefilter('error', DeprecationWarning)
1862            b = B(3)
1863        self.assertEqual(b.foo, 3)
1864        self.assertEqual(b.__class__, B)
1865
1866    def test_altmro(self):
1867        # Testing mro() and overriding it...
1868        class A(object):
1869            def f(self): return "A"
1870        class B(A):
1871            pass
1872        class C(A):
1873            def f(self): return "C"
1874        class D(B, C):
1875            pass
1876        self.assertEqual(A.mro(), [A, object])
1877        self.assertEqual(A.__mro__, (A, object))
1878        self.assertEqual(B.mro(), [B, A, object])
1879        self.assertEqual(B.__mro__, (B, A, object))
1880        self.assertEqual(C.mro(), [C, A, object])
1881        self.assertEqual(C.__mro__, (C, A, object))
1882        self.assertEqual(D.mro(), [D, B, C, A, object])
1883        self.assertEqual(D.__mro__, (D, B, C, A, object))
1884        self.assertEqual(D().f(), "C")
1885
1886        class PerverseMetaType(type):
1887            def mro(cls):
1888                L = type.mro(cls)
1889                L.reverse()
1890                return L
1891        class X(D,B,C,A, metaclass=PerverseMetaType):
1892            pass
1893        self.assertEqual(X.__mro__, (object, A, C, B, D, X))
1894        self.assertEqual(X().f(), "A")
1895
1896        try:
1897            class _metaclass(type):
1898                def mro(self):
1899                    return [self, dict, object]
1900            class X(object, metaclass=_metaclass):
1901                pass
1902            # In CPython, the class creation above already raises
1903            # TypeError, as a protection against the fact that
1904            # instances of X would segfault it.  In other Python
1905            # implementations it would be ok to let the class X
1906            # be created, but instead get a clean TypeError on the
1907            # __setitem__ below.
1908            x = object.__new__(X)
1909            x[5] = 6
1910        except TypeError:
1911            pass
1912        else:
1913            self.fail("devious mro() return not caught")
1914
1915        try:
1916            class _metaclass(type):
1917                def mro(self):
1918                    return [1]
1919            class X(object, metaclass=_metaclass):
1920                pass
1921        except TypeError:
1922            pass
1923        else:
1924            self.fail("non-class mro() return not caught")
1925
1926        try:
1927            class _metaclass(type):
1928                def mro(self):
1929                    return 1
1930            class X(object, metaclass=_metaclass):
1931                pass
1932        except TypeError:
1933            pass
1934        else:
1935            self.fail("non-sequence mro() return not caught")
1936
1937    def test_overloading(self):
1938        # Testing operator overloading...
1939
1940        class B(object):
1941            "Intermediate class because object doesn't have a __setattr__"
1942
1943        class C(B):
1944            def __getattr__(self, name):
1945                if name == "foo":
1946                    return ("getattr", name)
1947                else:
1948                    raise AttributeError
1949            def __setattr__(self, name, value):
1950                if name == "foo":
1951                    self.setattr = (name, value)
1952                else:
1953                    return B.__setattr__(self, name, value)
1954            def __delattr__(self, name):
1955                if name == "foo":
1956                    self.delattr = name
1957                else:
1958                    return B.__delattr__(self, name)
1959
1960            def __getitem__(self, key):
1961                return ("getitem", key)
1962            def __setitem__(self, key, value):
1963                self.setitem = (key, value)
1964            def __delitem__(self, key):
1965                self.delitem = key
1966
1967        a = C()
1968        self.assertEqual(a.foo, ("getattr", "foo"))
1969        a.foo = 12
1970        self.assertEqual(a.setattr, ("foo", 12))
1971        del a.foo
1972        self.assertEqual(a.delattr, "foo")
1973
1974        self.assertEqual(a[12], ("getitem", 12))
1975        a[12] = 21
1976        self.assertEqual(a.setitem, (12, 21))
1977        del a[12]
1978        self.assertEqual(a.delitem, 12)
1979
1980        self.assertEqual(a[0:10], ("getitem", slice(0, 10)))
1981        a[0:10] = "foo"
1982        self.assertEqual(a.setitem, (slice(0, 10), "foo"))
1983        del a[0:10]
1984        self.assertEqual(a.delitem, (slice(0, 10)))
1985
1986    def test_load_attr_extended_arg(self):
1987        # https://github.com/python/cpython/issues/91625
1988        class Numbers:
1989            def __getattr__(self, attr):
1990                return int(attr.lstrip("_"))
1991        attrs = ", ".join(f"Z._{n:03d}" for n in range(280))
1992        code = f"def number_attrs(Z):\n    return [ {attrs} ]"
1993        ns = {}
1994        exec(code, ns)
1995        number_attrs = ns["number_attrs"]
1996        # Warm up the function for quickening (PEP 659)
1997        for _ in range(30):
1998            self.assertEqual(number_attrs(Numbers()), list(range(280)))
1999
2000    def test_methods(self):
2001        # Testing methods...
2002        class C(object):
2003            def __init__(self, x):
2004                self.x = x
2005            def foo(self):
2006                return self.x
2007        c1 = C(1)
2008        self.assertEqual(c1.foo(), 1)
2009        class D(C):
2010            boo = C.foo
2011            goo = c1.foo
2012        d2 = D(2)
2013        self.assertEqual(d2.foo(), 2)
2014        self.assertEqual(d2.boo(), 2)
2015        self.assertEqual(d2.goo(), 1)
2016        class E(object):
2017            foo = C.foo
2018        self.assertEqual(E().foo.__func__, C.foo) # i.e., unbound
2019        self.assertTrue(repr(C.foo.__get__(C(1))).startswith("<bound method "))
2020
2021    @support.impl_detail("testing error message from implementation")
2022    def test_methods_in_c(self):
2023        # This test checks error messages in builtin method descriptor.
2024        # It is allowed that other Python implementations use
2025        # different error messages.
2026        set_add = set.add
2027
2028        expected_errmsg = "unbound method set.add() needs an argument"
2029
2030        with self.assertRaises(TypeError) as cm:
2031            set_add()
2032        self.assertEqual(cm.exception.args[0], expected_errmsg)
2033
2034        expected_errmsg = "descriptor 'add' for 'set' objects doesn't apply to a 'int' object"
2035
2036        with self.assertRaises(TypeError) as cm:
2037            set_add(0)
2038        self.assertEqual(cm.exception.args[0], expected_errmsg)
2039
2040        with self.assertRaises(TypeError) as cm:
2041            set_add.__get__(0)
2042        self.assertEqual(cm.exception.args[0], expected_errmsg)
2043
2044    def test_special_method_lookup(self):
2045        # The lookup of special methods bypasses __getattr__ and
2046        # __getattribute__, but they still can be descriptors.
2047
2048        def run_context(manager):
2049            with manager:
2050                pass
2051        def iden(self):
2052            return self
2053        def hello(self):
2054            return b"hello"
2055        def empty_seq(self):
2056            return []
2057        def zero(self):
2058            return 0
2059        def complex_num(self):
2060            return 1j
2061        def stop(self):
2062            raise StopIteration
2063        def return_true(self, thing=None):
2064            return True
2065        def do_isinstance(obj):
2066            return isinstance(int, obj)
2067        def do_issubclass(obj):
2068            return issubclass(int, obj)
2069        def do_dict_missing(checker):
2070            class DictSub(checker.__class__, dict):
2071                pass
2072            self.assertEqual(DictSub()["hi"], 4)
2073        def some_number(self_, key):
2074            self.assertEqual(key, "hi")
2075            return 4
2076        def swallow(*args): pass
2077        def format_impl(self, spec):
2078            return "hello"
2079
2080        # It would be nice to have every special method tested here, but I'm
2081        # only listing the ones I can remember outside of typeobject.c, since it
2082        # does it right.
2083        specials = [
2084            ("__bytes__", bytes, hello, set(), {}),
2085            ("__reversed__", reversed, empty_seq, set(), {}),
2086            ("__length_hint__", list, zero, set(),
2087             {"__iter__" : iden, "__next__" : stop}),
2088            ("__sizeof__", sys.getsizeof, zero, set(), {}),
2089            ("__instancecheck__", do_isinstance, return_true, set(), {}),
2090            ("__missing__", do_dict_missing, some_number,
2091             set(("__class__",)), {}),
2092            ("__subclasscheck__", do_issubclass, return_true,
2093             set(("__bases__",)), {}),
2094            ("__enter__", run_context, iden, set(), {"__exit__" : swallow}),
2095            ("__exit__", run_context, swallow, set(), {"__enter__" : iden}),
2096            ("__complex__", complex, complex_num, set(), {}),
2097            ("__format__", format, format_impl, set(), {}),
2098            ("__floor__", math.floor, zero, set(), {}),
2099            ("__trunc__", math.trunc, zero, set(), {}),
2100            ("__ceil__", math.ceil, zero, set(), {}),
2101            ("__dir__", dir, empty_seq, set(), {}),
2102            ("__round__", round, zero, set(), {}),
2103            ]
2104
2105        class Checker(object):
2106            def __getattr__(self, attr, test=self):
2107                test.fail("__getattr__ called with {0}".format(attr))
2108            def __getattribute__(self, attr, test=self):
2109                if attr not in ok:
2110                    test.fail("__getattribute__ called with {0}".format(attr))
2111                return object.__getattribute__(self, attr)
2112        class SpecialDescr(object):
2113            def __init__(self, impl):
2114                self.impl = impl
2115            def __get__(self, obj, owner):
2116                record.append(1)
2117                return self.impl.__get__(obj, owner)
2118        class MyException(Exception):
2119            pass
2120        class ErrDescr(object):
2121            def __get__(self, obj, owner):
2122                raise MyException
2123
2124        for name, runner, meth_impl, ok, env in specials:
2125            class X(Checker):
2126                pass
2127            for attr, obj in env.items():
2128                setattr(X, attr, obj)
2129            setattr(X, name, meth_impl)
2130            runner(X())
2131
2132            record = []
2133            class X(Checker):
2134                pass
2135            for attr, obj in env.items():
2136                setattr(X, attr, obj)
2137            setattr(X, name, SpecialDescr(meth_impl))
2138            runner(X())
2139            self.assertEqual(record, [1], name)
2140
2141            class X(Checker):
2142                pass
2143            for attr, obj in env.items():
2144                setattr(X, attr, obj)
2145            setattr(X, name, ErrDescr())
2146            self.assertRaises(MyException, runner, X())
2147
2148    def test_specials(self):
2149        # Testing special operators...
2150        # Test operators like __hash__ for which a built-in default exists
2151
2152        # Test the default behavior for static classes
2153        class C(object):
2154            def __getitem__(self, i):
2155                if 0 <= i < 10: return i
2156                raise IndexError
2157        c1 = C()
2158        c2 = C()
2159        self.assertFalse(not c1)
2160        self.assertNotEqual(id(c1), id(c2))
2161        hash(c1)
2162        hash(c2)
2163        self.assertEqual(c1, c1)
2164        self.assertTrue(c1 != c2)
2165        self.assertFalse(c1 != c1)
2166        self.assertFalse(c1 == c2)
2167        # Note that the module name appears in str/repr, and that varies
2168        # depending on whether this test is run standalone or from a framework.
2169        self.assertGreaterEqual(str(c1).find('C object at '), 0)
2170        self.assertEqual(str(c1), repr(c1))
2171        self.assertNotIn(-1, c1)
2172        for i in range(10):
2173            self.assertIn(i, c1)
2174        self.assertNotIn(10, c1)
2175        # Test the default behavior for dynamic classes
2176        class D(object):
2177            def __getitem__(self, i):
2178                if 0 <= i < 10: return i
2179                raise IndexError
2180        d1 = D()
2181        d2 = D()
2182        self.assertFalse(not d1)
2183        self.assertNotEqual(id(d1), id(d2))
2184        hash(d1)
2185        hash(d2)
2186        self.assertEqual(d1, d1)
2187        self.assertNotEqual(d1, d2)
2188        self.assertFalse(d1 != d1)
2189        self.assertFalse(d1 == d2)
2190        # Note that the module name appears in str/repr, and that varies
2191        # depending on whether this test is run standalone or from a framework.
2192        self.assertGreaterEqual(str(d1).find('D object at '), 0)
2193        self.assertEqual(str(d1), repr(d1))
2194        self.assertNotIn(-1, d1)
2195        for i in range(10):
2196            self.assertIn(i, d1)
2197        self.assertNotIn(10, d1)
2198        # Test overridden behavior
2199        class Proxy(object):
2200            def __init__(self, x):
2201                self.x = x
2202            def __bool__(self):
2203                return not not self.x
2204            def __hash__(self):
2205                return hash(self.x)
2206            def __eq__(self, other):
2207                return self.x == other
2208            def __ne__(self, other):
2209                return self.x != other
2210            def __ge__(self, other):
2211                return self.x >= other
2212            def __gt__(self, other):
2213                return self.x > other
2214            def __le__(self, other):
2215                return self.x <= other
2216            def __lt__(self, other):
2217                return self.x < other
2218            def __str__(self):
2219                return "Proxy:%s" % self.x
2220            def __repr__(self):
2221                return "Proxy(%r)" % self.x
2222            def __contains__(self, value):
2223                return value in self.x
2224        p0 = Proxy(0)
2225        p1 = Proxy(1)
2226        p_1 = Proxy(-1)
2227        self.assertFalse(p0)
2228        self.assertFalse(not p1)
2229        self.assertEqual(hash(p0), hash(0))
2230        self.assertEqual(p0, p0)
2231        self.assertNotEqual(p0, p1)
2232        self.assertFalse(p0 != p0)
2233        self.assertEqual(not p0, p1)
2234        self.assertTrue(p0 < p1)
2235        self.assertTrue(p0 <= p1)
2236        self.assertTrue(p1 > p0)
2237        self.assertTrue(p1 >= p0)
2238        self.assertEqual(str(p0), "Proxy:0")
2239        self.assertEqual(repr(p0), "Proxy(0)")
2240        p10 = Proxy(range(10))
2241        self.assertNotIn(-1, p10)
2242        for i in range(10):
2243            self.assertIn(i, p10)
2244        self.assertNotIn(10, p10)
2245
2246    def test_weakrefs(self):
2247        # Testing weak references...
2248        import weakref
2249        class C(object):
2250            pass
2251        c = C()
2252        r = weakref.ref(c)
2253        self.assertEqual(r(), c)
2254        del c
2255        support.gc_collect()
2256        self.assertEqual(r(), None)
2257        del r
2258        class NoWeak(object):
2259            __slots__ = ['foo']
2260        no = NoWeak()
2261        try:
2262            weakref.ref(no)
2263        except TypeError as msg:
2264            self.assertIn("weak reference", str(msg))
2265        else:
2266            self.fail("weakref.ref(no) should be illegal")
2267        class Weak(object):
2268            __slots__ = ['foo', '__weakref__']
2269        yes = Weak()
2270        r = weakref.ref(yes)
2271        self.assertEqual(r(), yes)
2272        del yes
2273        support.gc_collect()
2274        self.assertEqual(r(), None)
2275        del r
2276
2277    def test_properties(self):
2278        # Testing property...
2279        class C(object):
2280            def getx(self):
2281                return self.__x
2282            def setx(self, value):
2283                self.__x = value
2284            def delx(self):
2285                del self.__x
2286            x = property(getx, setx, delx, doc="I'm the x property.")
2287        a = C()
2288        self.assertNotHasAttr(a, "x")
2289        a.x = 42
2290        self.assertEqual(a._C__x, 42)
2291        self.assertEqual(a.x, 42)
2292        del a.x
2293        self.assertNotHasAttr(a, "x")
2294        self.assertNotHasAttr(a, "_C__x")
2295        C.x.__set__(a, 100)
2296        self.assertEqual(C.x.__get__(a), 100)
2297        C.x.__delete__(a)
2298        self.assertNotHasAttr(a, "x")
2299
2300        raw = C.__dict__['x']
2301        self.assertIsInstance(raw, property)
2302
2303        attrs = dir(raw)
2304        self.assertIn("__doc__", attrs)
2305        self.assertIn("fget", attrs)
2306        self.assertIn("fset", attrs)
2307        self.assertIn("fdel", attrs)
2308
2309        self.assertEqual(raw.__doc__, "I'm the x property.")
2310        self.assertIs(raw.fget, C.__dict__['getx'])
2311        self.assertIs(raw.fset, C.__dict__['setx'])
2312        self.assertIs(raw.fdel, C.__dict__['delx'])
2313
2314        for attr in "fget", "fset", "fdel":
2315            try:
2316                setattr(raw, attr, 42)
2317            except AttributeError as msg:
2318                if str(msg).find('readonly') < 0:
2319                    self.fail("when setting readonly attr %r on a property, "
2320                              "got unexpected AttributeError msg %r" % (attr, str(msg)))
2321            else:
2322                self.fail("expected AttributeError from trying to set readonly %r "
2323                          "attr on a property" % attr)
2324
2325        raw.__doc__ = 42
2326        self.assertEqual(raw.__doc__, 42)
2327
2328        class D(object):
2329            __getitem__ = property(lambda s: 1/0)
2330
2331        d = D()
2332        try:
2333            for i in d:
2334                str(i)
2335        except ZeroDivisionError:
2336            pass
2337        else:
2338            self.fail("expected ZeroDivisionError from bad property")
2339
2340    @unittest.skipIf(sys.flags.optimize >= 2,
2341                     "Docstrings are omitted with -O2 and above")
2342    def test_properties_doc_attrib(self):
2343        class E(object):
2344            def getter(self):
2345                "getter method"
2346                return 0
2347            def setter(self_, value):
2348                "setter method"
2349                pass
2350            prop = property(getter)
2351            self.assertEqual(prop.__doc__, "getter method")
2352            prop2 = property(fset=setter)
2353            self.assertEqual(prop2.__doc__, None)
2354
2355    @support.cpython_only
2356    def test_testcapi_no_segfault(self):
2357        # this segfaulted in 2.5b2
2358        try:
2359            import _testcapi
2360        except ImportError:
2361            pass
2362        else:
2363            class X(object):
2364                p = property(_testcapi.test_with_docstring)
2365
2366    def test_properties_plus(self):
2367        class C(object):
2368            foo = property(doc="hello")
2369            @foo.getter
2370            def foo(self):
2371                return self._foo
2372            @foo.setter
2373            def foo(self, value):
2374                self._foo = abs(value)
2375            @foo.deleter
2376            def foo(self):
2377                del self._foo
2378        c = C()
2379        self.assertEqual(C.foo.__doc__, "hello")
2380        self.assertNotHasAttr(c, "foo")
2381        c.foo = -42
2382        self.assertHasAttr(c, '_foo')
2383        self.assertEqual(c._foo, 42)
2384        self.assertEqual(c.foo, 42)
2385        del c.foo
2386        self.assertNotHasAttr(c, '_foo')
2387        self.assertNotHasAttr(c, "foo")
2388
2389        class D(C):
2390            @C.foo.deleter
2391            def foo(self):
2392                try:
2393                    del self._foo
2394                except AttributeError:
2395                    pass
2396        d = D()
2397        d.foo = 24
2398        self.assertEqual(d.foo, 24)
2399        del d.foo
2400        del d.foo
2401
2402        class E(object):
2403            @property
2404            def foo(self):
2405                return self._foo
2406            @foo.setter
2407            def foo(self, value):
2408                raise RuntimeError
2409            @foo.setter
2410            def foo(self, value):
2411                self._foo = abs(value)
2412            @foo.deleter
2413            def foo(self, value=None):
2414                del self._foo
2415
2416        e = E()
2417        e.foo = -42
2418        self.assertEqual(e.foo, 42)
2419        del e.foo
2420
2421        class F(E):
2422            @E.foo.deleter
2423            def foo(self):
2424                del self._foo
2425            @foo.setter
2426            def foo(self, value):
2427                self._foo = max(0, value)
2428        f = F()
2429        f.foo = -10
2430        self.assertEqual(f.foo, 0)
2431        del f.foo
2432
2433    def test_dict_constructors(self):
2434        # Testing dict constructor ...
2435        d = dict()
2436        self.assertEqual(d, {})
2437        d = dict({})
2438        self.assertEqual(d, {})
2439        d = dict({1: 2, 'a': 'b'})
2440        self.assertEqual(d, {1: 2, 'a': 'b'})
2441        self.assertEqual(d, dict(list(d.items())))
2442        self.assertEqual(d, dict(iter(d.items())))
2443        d = dict({'one':1, 'two':2})
2444        self.assertEqual(d, dict(one=1, two=2))
2445        self.assertEqual(d, dict(**d))
2446        self.assertEqual(d, dict({"one": 1}, two=2))
2447        self.assertEqual(d, dict([("two", 2)], one=1))
2448        self.assertEqual(d, dict([("one", 100), ("two", 200)], **d))
2449        self.assertEqual(d, dict(**d))
2450
2451        for badarg in 0, 0, 0j, "0", [0], (0,):
2452            try:
2453                dict(badarg)
2454            except TypeError:
2455                pass
2456            except ValueError:
2457                if badarg == "0":
2458                    # It's a sequence, and its elements are also sequences (gotta
2459                    # love strings <wink>), but they aren't of length 2, so this
2460                    # one seemed better as a ValueError than a TypeError.
2461                    pass
2462                else:
2463                    self.fail("no TypeError from dict(%r)" % badarg)
2464            else:
2465                self.fail("no TypeError from dict(%r)" % badarg)
2466
2467        with self.assertRaises(TypeError):
2468            dict({}, {})
2469
2470        class Mapping:
2471            # Lacks a .keys() method; will be added later.
2472            dict = {1:2, 3:4, 'a':1j}
2473
2474        try:
2475            dict(Mapping())
2476        except TypeError:
2477            pass
2478        else:
2479            self.fail("no TypeError from dict(incomplete mapping)")
2480
2481        Mapping.keys = lambda self: list(self.dict.keys())
2482        Mapping.__getitem__ = lambda self, i: self.dict[i]
2483        d = dict(Mapping())
2484        self.assertEqual(d, Mapping.dict)
2485
2486        # Init from sequence of iterable objects, each producing a 2-sequence.
2487        class AddressBookEntry:
2488            def __init__(self, first, last):
2489                self.first = first
2490                self.last = last
2491            def __iter__(self):
2492                return iter([self.first, self.last])
2493
2494        d = dict([AddressBookEntry('Tim', 'Warsaw'),
2495                  AddressBookEntry('Barry', 'Peters'),
2496                  AddressBookEntry('Tim', 'Peters'),
2497                  AddressBookEntry('Barry', 'Warsaw')])
2498        self.assertEqual(d, {'Barry': 'Warsaw', 'Tim': 'Peters'})
2499
2500        d = dict(zip(range(4), range(1, 5)))
2501        self.assertEqual(d, dict([(i, i+1) for i in range(4)]))
2502
2503        # Bad sequence lengths.
2504        for bad in [('tooshort',)], [('too', 'long', 'by 1')]:
2505            try:
2506                dict(bad)
2507            except ValueError:
2508                pass
2509            else:
2510                self.fail("no ValueError from dict(%r)" % bad)
2511
2512    def test_dir(self):
2513        # Testing dir() ...
2514        junk = 12
2515        self.assertEqual(dir(), ['junk', 'self'])
2516        del junk
2517
2518        # Just make sure these don't blow up!
2519        for arg in 2, 2, 2j, 2e0, [2], "2", b"2", (2,), {2:2}, type, self.test_dir:
2520            dir(arg)
2521
2522        # Test dir on new-style classes.  Since these have object as a
2523        # base class, a lot more gets sucked in.
2524        def interesting(strings):
2525            return [s for s in strings if not s.startswith('_')]
2526
2527        class C(object):
2528            Cdata = 1
2529            def Cmethod(self): pass
2530
2531        cstuff = ['Cdata', 'Cmethod']
2532        self.assertEqual(interesting(dir(C)), cstuff)
2533
2534        c = C()
2535        self.assertEqual(interesting(dir(c)), cstuff)
2536        ## self.assertIn('__self__', dir(C.Cmethod))
2537
2538        c.cdata = 2
2539        c.cmethod = lambda self: 0
2540        self.assertEqual(interesting(dir(c)), cstuff + ['cdata', 'cmethod'])
2541        ## self.assertIn('__self__', dir(c.Cmethod))
2542
2543        class A(C):
2544            Adata = 1
2545            def Amethod(self): pass
2546
2547        astuff = ['Adata', 'Amethod'] + cstuff
2548        self.assertEqual(interesting(dir(A)), astuff)
2549        ## self.assertIn('__self__', dir(A.Amethod))
2550        a = A()
2551        self.assertEqual(interesting(dir(a)), astuff)
2552        a.adata = 42
2553        a.amethod = lambda self: 3
2554        self.assertEqual(interesting(dir(a)), astuff + ['adata', 'amethod'])
2555        ## self.assertIn('__self__', dir(a.Amethod))
2556
2557        # Try a module subclass.
2558        class M(type(sys)):
2559            pass
2560        minstance = M("m")
2561        minstance.b = 2
2562        minstance.a = 1
2563        default_attributes = ['__name__', '__doc__', '__package__',
2564                              '__loader__', '__spec__']
2565        names = [x for x in dir(minstance) if x not in default_attributes]
2566        self.assertEqual(names, ['a', 'b'])
2567
2568        class M2(M):
2569            def getdict(self):
2570                return "Not a dict!"
2571            __dict__ = property(getdict)
2572
2573        m2instance = M2("m2")
2574        m2instance.b = 2
2575        m2instance.a = 1
2576        self.assertEqual(m2instance.__dict__, "Not a dict!")
2577        with self.assertRaises(TypeError):
2578            dir(m2instance)
2579
2580        # Two essentially featureless objects, (Ellipsis just inherits stuff
2581        # from object.
2582        self.assertEqual(dir(object()), dir(Ellipsis))
2583
2584        # Nasty test case for proxied objects
2585        class Wrapper(object):
2586            def __init__(self, obj):
2587                self.__obj = obj
2588            def __repr__(self):
2589                return "Wrapper(%s)" % repr(self.__obj)
2590            def __getitem__(self, key):
2591                return Wrapper(self.__obj[key])
2592            def __len__(self):
2593                return len(self.__obj)
2594            def __getattr__(self, name):
2595                return Wrapper(getattr(self.__obj, name))
2596
2597        class C(object):
2598            def __getclass(self):
2599                return Wrapper(type(self))
2600            __class__ = property(__getclass)
2601
2602        dir(C()) # This used to segfault
2603
2604    def test_supers(self):
2605        # Testing super...
2606
2607        class A(object):
2608            def meth(self, a):
2609                return "A(%r)" % a
2610
2611        self.assertEqual(A().meth(1), "A(1)")
2612
2613        class B(A):
2614            def __init__(self):
2615                self.__super = super(B, self)
2616            def meth(self, a):
2617                return "B(%r)" % a + self.__super.meth(a)
2618
2619        self.assertEqual(B().meth(2), "B(2)A(2)")
2620
2621        class C(A):
2622            def meth(self, a):
2623                return "C(%r)" % a + self.__super.meth(a)
2624        C._C__super = super(C)
2625
2626        self.assertEqual(C().meth(3), "C(3)A(3)")
2627
2628        class D(C, B):
2629            def meth(self, a):
2630                return "D(%r)" % a + super(D, self).meth(a)
2631
2632        self.assertEqual(D().meth(4), "D(4)C(4)B(4)A(4)")
2633
2634        # Test for subclassing super
2635
2636        class mysuper(super):
2637            def __init__(self, *args):
2638                return super(mysuper, self).__init__(*args)
2639
2640        class E(D):
2641            def meth(self, a):
2642                return "E(%r)" % a + mysuper(E, self).meth(a)
2643
2644        self.assertEqual(E().meth(5), "E(5)D(5)C(5)B(5)A(5)")
2645
2646        class F(E):
2647            def meth(self, a):
2648                s = self.__super # == mysuper(F, self)
2649                return "F(%r)[%s]" % (a, s.__class__.__name__) + s.meth(a)
2650        F._F__super = mysuper(F)
2651
2652        self.assertEqual(F().meth(6), "F(6)[mysuper]E(6)D(6)C(6)B(6)A(6)")
2653
2654        # Make sure certain errors are raised
2655
2656        try:
2657            super(D, 42)
2658        except TypeError:
2659            pass
2660        else:
2661            self.fail("shouldn't allow super(D, 42)")
2662
2663        try:
2664            super(D, C())
2665        except TypeError:
2666            pass
2667        else:
2668            self.fail("shouldn't allow super(D, C())")
2669
2670        try:
2671            super(D).__get__(12)
2672        except TypeError:
2673            pass
2674        else:
2675            self.fail("shouldn't allow super(D).__get__(12)")
2676
2677        try:
2678            super(D).__get__(C())
2679        except TypeError:
2680            pass
2681        else:
2682            self.fail("shouldn't allow super(D).__get__(C())")
2683
2684        # Make sure data descriptors can be overridden and accessed via super
2685        # (new feature in Python 2.3)
2686
2687        class DDbase(object):
2688            def getx(self): return 42
2689            x = property(getx)
2690
2691        class DDsub(DDbase):
2692            def getx(self): return "hello"
2693            x = property(getx)
2694
2695        dd = DDsub()
2696        self.assertEqual(dd.x, "hello")
2697        self.assertEqual(super(DDsub, dd).x, 42)
2698
2699        # Ensure that super() lookup of descriptor from classmethod
2700        # works (SF ID# 743627)
2701
2702        class Base(object):
2703            aProp = property(lambda self: "foo")
2704
2705        class Sub(Base):
2706            @classmethod
2707            def test(klass):
2708                return super(Sub,klass).aProp
2709
2710        self.assertEqual(Sub.test(), Base.aProp)
2711
2712        # Verify that super() doesn't allow keyword args
2713        with self.assertRaises(TypeError):
2714            super(Base, kw=1)
2715
2716    def test_basic_inheritance(self):
2717        # Testing inheritance from basic types...
2718
2719        class hexint(int):
2720            def __repr__(self):
2721                return hex(self)
2722            def __add__(self, other):
2723                return hexint(int.__add__(self, other))
2724            # (Note that overriding __radd__ doesn't work,
2725            # because the int type gets first dibs.)
2726        self.assertEqual(repr(hexint(7) + 9), "0x10")
2727        self.assertEqual(repr(hexint(1000) + 7), "0x3ef")
2728        a = hexint(12345)
2729        self.assertEqual(a, 12345)
2730        self.assertEqual(int(a), 12345)
2731        self.assertIs(int(a).__class__, int)
2732        self.assertEqual(hash(a), hash(12345))
2733        self.assertIs((+a).__class__, int)
2734        self.assertIs((a >> 0).__class__, int)
2735        self.assertIs((a << 0).__class__, int)
2736        self.assertIs((hexint(0) << 12).__class__, int)
2737        self.assertIs((hexint(0) >> 12).__class__, int)
2738
2739        class octlong(int):
2740            __slots__ = []
2741            def __str__(self):
2742                return oct(self)
2743            def __add__(self, other):
2744                return self.__class__(super(octlong, self).__add__(other))
2745            __radd__ = __add__
2746        self.assertEqual(str(octlong(3) + 5), "0o10")
2747        # (Note that overriding __radd__ here only seems to work
2748        # because the example uses a short int left argument.)
2749        self.assertEqual(str(5 + octlong(3000)), "0o5675")
2750        a = octlong(12345)
2751        self.assertEqual(a, 12345)
2752        self.assertEqual(int(a), 12345)
2753        self.assertEqual(hash(a), hash(12345))
2754        self.assertIs(int(a).__class__, int)
2755        self.assertIs((+a).__class__, int)
2756        self.assertIs((-a).__class__, int)
2757        self.assertIs((-octlong(0)).__class__, int)
2758        self.assertIs((a >> 0).__class__, int)
2759        self.assertIs((a << 0).__class__, int)
2760        self.assertIs((a - 0).__class__, int)
2761        self.assertIs((a * 1).__class__, int)
2762        self.assertIs((a ** 1).__class__, int)
2763        self.assertIs((a // 1).__class__, int)
2764        self.assertIs((1 * a).__class__, int)
2765        self.assertIs((a | 0).__class__, int)
2766        self.assertIs((a ^ 0).__class__, int)
2767        self.assertIs((a & -1).__class__, int)
2768        self.assertIs((octlong(0) << 12).__class__, int)
2769        self.assertIs((octlong(0) >> 12).__class__, int)
2770        self.assertIs(abs(octlong(0)).__class__, int)
2771
2772        # Because octlong overrides __add__, we can't check the absence of +0
2773        # optimizations using octlong.
2774        class longclone(int):
2775            pass
2776        a = longclone(1)
2777        self.assertIs((a + 0).__class__, int)
2778        self.assertIs((0 + a).__class__, int)
2779
2780        # Check that negative clones don't segfault
2781        a = longclone(-1)
2782        self.assertEqual(a.__dict__, {})
2783        self.assertEqual(int(a), -1)  # self.assertTrue PyNumber_Long() copies the sign bit
2784
2785        class precfloat(float):
2786            __slots__ = ['prec']
2787            def __init__(self, value=0.0, prec=12):
2788                self.prec = int(prec)
2789            def __repr__(self):
2790                return "%.*g" % (self.prec, self)
2791        self.assertEqual(repr(precfloat(1.1)), "1.1")
2792        a = precfloat(12345)
2793        self.assertEqual(a, 12345.0)
2794        self.assertEqual(float(a), 12345.0)
2795        self.assertIs(float(a).__class__, float)
2796        self.assertEqual(hash(a), hash(12345.0))
2797        self.assertIs((+a).__class__, float)
2798
2799        class madcomplex(complex):
2800            def __repr__(self):
2801                return "%.17gj%+.17g" % (self.imag, self.real)
2802        a = madcomplex(-3, 4)
2803        self.assertEqual(repr(a), "4j-3")
2804        base = complex(-3, 4)
2805        self.assertEqual(base.__class__, complex)
2806        self.assertEqual(a, base)
2807        self.assertEqual(complex(a), base)
2808        self.assertEqual(complex(a).__class__, complex)
2809        a = madcomplex(a)  # just trying another form of the constructor
2810        self.assertEqual(repr(a), "4j-3")
2811        self.assertEqual(a, base)
2812        self.assertEqual(complex(a), base)
2813        self.assertEqual(complex(a).__class__, complex)
2814        self.assertEqual(hash(a), hash(base))
2815        self.assertEqual((+a).__class__, complex)
2816        self.assertEqual((a + 0).__class__, complex)
2817        self.assertEqual(a + 0, base)
2818        self.assertEqual((a - 0).__class__, complex)
2819        self.assertEqual(a - 0, base)
2820        self.assertEqual((a * 1).__class__, complex)
2821        self.assertEqual(a * 1, base)
2822        self.assertEqual((a / 1).__class__, complex)
2823        self.assertEqual(a / 1, base)
2824
2825        class madtuple(tuple):
2826            _rev = None
2827            def rev(self):
2828                if self._rev is not None:
2829                    return self._rev
2830                L = list(self)
2831                L.reverse()
2832                self._rev = self.__class__(L)
2833                return self._rev
2834        a = madtuple((1,2,3,4,5,6,7,8,9,0))
2835        self.assertEqual(a, (1,2,3,4,5,6,7,8,9,0))
2836        self.assertEqual(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1)))
2837        self.assertEqual(a.rev().rev(), madtuple((1,2,3,4,5,6,7,8,9,0)))
2838        for i in range(512):
2839            t = madtuple(range(i))
2840            u = t.rev()
2841            v = u.rev()
2842            self.assertEqual(v, t)
2843        a = madtuple((1,2,3,4,5))
2844        self.assertEqual(tuple(a), (1,2,3,4,5))
2845        self.assertIs(tuple(a).__class__, tuple)
2846        self.assertEqual(hash(a), hash((1,2,3,4,5)))
2847        self.assertIs(a[:].__class__, tuple)
2848        self.assertIs((a * 1).__class__, tuple)
2849        self.assertIs((a * 0).__class__, tuple)
2850        self.assertIs((a + ()).__class__, tuple)
2851        a = madtuple(())
2852        self.assertEqual(tuple(a), ())
2853        self.assertIs(tuple(a).__class__, tuple)
2854        self.assertIs((a + a).__class__, tuple)
2855        self.assertIs((a * 0).__class__, tuple)
2856        self.assertIs((a * 1).__class__, tuple)
2857        self.assertIs((a * 2).__class__, tuple)
2858        self.assertIs(a[:].__class__, tuple)
2859
2860        class madstring(str):
2861            _rev = None
2862            def rev(self):
2863                if self._rev is not None:
2864                    return self._rev
2865                L = list(self)
2866                L.reverse()
2867                self._rev = self.__class__("".join(L))
2868                return self._rev
2869        s = madstring("abcdefghijklmnopqrstuvwxyz")
2870        self.assertEqual(s, "abcdefghijklmnopqrstuvwxyz")
2871        self.assertEqual(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba"))
2872        self.assertEqual(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz"))
2873        for i in range(256):
2874            s = madstring("".join(map(chr, range(i))))
2875            t = s.rev()
2876            u = t.rev()
2877            self.assertEqual(u, s)
2878        s = madstring("12345")
2879        self.assertEqual(str(s), "12345")
2880        self.assertIs(str(s).__class__, str)
2881
2882        base = "\x00" * 5
2883        s = madstring(base)
2884        self.assertEqual(s, base)
2885        self.assertEqual(str(s), base)
2886        self.assertIs(str(s).__class__, str)
2887        self.assertEqual(hash(s), hash(base))
2888        self.assertEqual({s: 1}[base], 1)
2889        self.assertEqual({base: 1}[s], 1)
2890        self.assertIs((s + "").__class__, str)
2891        self.assertEqual(s + "", base)
2892        self.assertIs(("" + s).__class__, str)
2893        self.assertEqual("" + s, base)
2894        self.assertIs((s * 0).__class__, str)
2895        self.assertEqual(s * 0, "")
2896        self.assertIs((s * 1).__class__, str)
2897        self.assertEqual(s * 1, base)
2898        self.assertIs((s * 2).__class__, str)
2899        self.assertEqual(s * 2, base + base)
2900        self.assertIs(s[:].__class__, str)
2901        self.assertEqual(s[:], base)
2902        self.assertIs(s[0:0].__class__, str)
2903        self.assertEqual(s[0:0], "")
2904        self.assertIs(s.strip().__class__, str)
2905        self.assertEqual(s.strip(), base)
2906        self.assertIs(s.lstrip().__class__, str)
2907        self.assertEqual(s.lstrip(), base)
2908        self.assertIs(s.rstrip().__class__, str)
2909        self.assertEqual(s.rstrip(), base)
2910        identitytab = {}
2911        self.assertIs(s.translate(identitytab).__class__, str)
2912        self.assertEqual(s.translate(identitytab), base)
2913        self.assertIs(s.replace("x", "x").__class__, str)
2914        self.assertEqual(s.replace("x", "x"), base)
2915        self.assertIs(s.ljust(len(s)).__class__, str)
2916        self.assertEqual(s.ljust(len(s)), base)
2917        self.assertIs(s.rjust(len(s)).__class__, str)
2918        self.assertEqual(s.rjust(len(s)), base)
2919        self.assertIs(s.center(len(s)).__class__, str)
2920        self.assertEqual(s.center(len(s)), base)
2921        self.assertIs(s.lower().__class__, str)
2922        self.assertEqual(s.lower(), base)
2923
2924        class madunicode(str):
2925            _rev = None
2926            def rev(self):
2927                if self._rev is not None:
2928                    return self._rev
2929                L = list(self)
2930                L.reverse()
2931                self._rev = self.__class__("".join(L))
2932                return self._rev
2933        u = madunicode("ABCDEF")
2934        self.assertEqual(u, "ABCDEF")
2935        self.assertEqual(u.rev(), madunicode("FEDCBA"))
2936        self.assertEqual(u.rev().rev(), madunicode("ABCDEF"))
2937        base = "12345"
2938        u = madunicode(base)
2939        self.assertEqual(str(u), base)
2940        self.assertIs(str(u).__class__, str)
2941        self.assertEqual(hash(u), hash(base))
2942        self.assertEqual({u: 1}[base], 1)
2943        self.assertEqual({base: 1}[u], 1)
2944        self.assertIs(u.strip().__class__, str)
2945        self.assertEqual(u.strip(), base)
2946        self.assertIs(u.lstrip().__class__, str)
2947        self.assertEqual(u.lstrip(), base)
2948        self.assertIs(u.rstrip().__class__, str)
2949        self.assertEqual(u.rstrip(), base)
2950        self.assertIs(u.replace("x", "x").__class__, str)
2951        self.assertEqual(u.replace("x", "x"), base)
2952        self.assertIs(u.replace("xy", "xy").__class__, str)
2953        self.assertEqual(u.replace("xy", "xy"), base)
2954        self.assertIs(u.center(len(u)).__class__, str)
2955        self.assertEqual(u.center(len(u)), base)
2956        self.assertIs(u.ljust(len(u)).__class__, str)
2957        self.assertEqual(u.ljust(len(u)), base)
2958        self.assertIs(u.rjust(len(u)).__class__, str)
2959        self.assertEqual(u.rjust(len(u)), base)
2960        self.assertIs(u.lower().__class__, str)
2961        self.assertEqual(u.lower(), base)
2962        self.assertIs(u.upper().__class__, str)
2963        self.assertEqual(u.upper(), base)
2964        self.assertIs(u.capitalize().__class__, str)
2965        self.assertEqual(u.capitalize(), base)
2966        self.assertIs(u.title().__class__, str)
2967        self.assertEqual(u.title(), base)
2968        self.assertIs((u + "").__class__, str)
2969        self.assertEqual(u + "", base)
2970        self.assertIs(("" + u).__class__, str)
2971        self.assertEqual("" + u, base)
2972        self.assertIs((u * 0).__class__, str)
2973        self.assertEqual(u * 0, "")
2974        self.assertIs((u * 1).__class__, str)
2975        self.assertEqual(u * 1, base)
2976        self.assertIs((u * 2).__class__, str)
2977        self.assertEqual(u * 2, base + base)
2978        self.assertIs(u[:].__class__, str)
2979        self.assertEqual(u[:], base)
2980        self.assertIs(u[0:0].__class__, str)
2981        self.assertEqual(u[0:0], "")
2982
2983        class sublist(list):
2984            pass
2985        a = sublist(range(5))
2986        self.assertEqual(a, list(range(5)))
2987        a.append("hello")
2988        self.assertEqual(a, list(range(5)) + ["hello"])
2989        a[5] = 5
2990        self.assertEqual(a, list(range(6)))
2991        a.extend(range(6, 20))
2992        self.assertEqual(a, list(range(20)))
2993        a[-5:] = []
2994        self.assertEqual(a, list(range(15)))
2995        del a[10:15]
2996        self.assertEqual(len(a), 10)
2997        self.assertEqual(a, list(range(10)))
2998        self.assertEqual(list(a), list(range(10)))
2999        self.assertEqual(a[0], 0)
3000        self.assertEqual(a[9], 9)
3001        self.assertEqual(a[-10], 0)
3002        self.assertEqual(a[-1], 9)
3003        self.assertEqual(a[:5], list(range(5)))
3004
3005        ## class CountedInput(file):
3006        ##    """Counts lines read by self.readline().
3007        ##
3008        ##     self.lineno is the 0-based ordinal of the last line read, up to
3009        ##     a maximum of one greater than the number of lines in the file.
3010        ##
3011        ##     self.ateof is true if and only if the final "" line has been read,
3012        ##     at which point self.lineno stops incrementing, and further calls
3013        ##     to readline() continue to return "".
3014        ##     """
3015        ##
3016        ##     lineno = 0
3017        ##     ateof = 0
3018        ##     def readline(self):
3019        ##         if self.ateof:
3020        ##             return ""
3021        ##         s = file.readline(self)
3022        ##         # Next line works too.
3023        ##         # s = super(CountedInput, self).readline()
3024        ##         self.lineno += 1
3025        ##         if s == "":
3026        ##             self.ateof = 1
3027        ##        return s
3028        ##
3029        ## f = file(name=os_helper.TESTFN, mode='w')
3030        ## lines = ['a\n', 'b\n', 'c\n']
3031        ## try:
3032        ##     f.writelines(lines)
3033        ##     f.close()
3034        ##     f = CountedInput(os_helper.TESTFN)
3035        ##     for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]):
3036        ##         got = f.readline()
3037        ##         self.assertEqual(expected, got)
3038        ##         self.assertEqual(f.lineno, i)
3039        ##         self.assertEqual(f.ateof, (i > len(lines)))
3040        ##     f.close()
3041        ## finally:
3042        ##     try:
3043        ##         f.close()
3044        ##     except:
3045        ##         pass
3046        ##     os_helper.unlink(os_helper.TESTFN)
3047
3048    def test_keywords(self):
3049        # Testing keyword args to basic type constructors ...
3050        with self.assertRaisesRegex(TypeError, 'keyword argument'):
3051            int(x=1)
3052        with self.assertRaisesRegex(TypeError, 'keyword argument'):
3053            float(x=2)
3054        with self.assertRaisesRegex(TypeError, 'keyword argument'):
3055            bool(x=2)
3056        self.assertEqual(complex(imag=42, real=666), complex(666, 42))
3057        self.assertEqual(str(object=500), '500')
3058        self.assertEqual(str(object=b'abc', errors='strict'), 'abc')
3059        with self.assertRaisesRegex(TypeError, 'keyword argument'):
3060            tuple(sequence=range(3))
3061        with self.assertRaisesRegex(TypeError, 'keyword argument'):
3062            list(sequence=(0, 1, 2))
3063        # note: as of Python 2.3, dict() no longer has an "items" keyword arg
3064
3065        for constructor in (int, float, int, complex, str, str,
3066                            tuple, list):
3067            try:
3068                constructor(bogus_keyword_arg=1)
3069            except TypeError:
3070                pass
3071            else:
3072                self.fail("expected TypeError from bogus keyword argument to %r"
3073                            % constructor)
3074
3075    def test_str_subclass_as_dict_key(self):
3076        # Testing a str subclass used as dict key ..
3077
3078        class cistr(str):
3079            """Subclass of str that computes __eq__ case-insensitively.
3080
3081            Also computes a hash code of the string in canonical form.
3082            """
3083
3084            def __init__(self, value):
3085                self.canonical = value.lower()
3086                self.hashcode = hash(self.canonical)
3087
3088            def __eq__(self, other):
3089                if not isinstance(other, cistr):
3090                    other = cistr(other)
3091                return self.canonical == other.canonical
3092
3093            def __hash__(self):
3094                return self.hashcode
3095
3096        self.assertEqual(cistr('ABC'), 'abc')
3097        self.assertEqual('aBc', cistr('ABC'))
3098        self.assertEqual(str(cistr('ABC')), 'ABC')
3099
3100        d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3}
3101        self.assertEqual(d[cistr('one')], 1)
3102        self.assertEqual(d[cistr('tWo')], 2)
3103        self.assertEqual(d[cistr('THrEE')], 3)
3104        self.assertIn(cistr('ONe'), d)
3105        self.assertEqual(d.get(cistr('thrEE')), 3)
3106
3107    def test_classic_comparisons(self):
3108        # Testing classic comparisons...
3109        class classic:
3110            pass
3111
3112        for base in (classic, int, object):
3113            class C(base):
3114                def __init__(self, value):
3115                    self.value = int(value)
3116                def __eq__(self, other):
3117                    if isinstance(other, C):
3118                        return self.value == other.value
3119                    if isinstance(other, int) or isinstance(other, int):
3120                        return self.value == other
3121                    return NotImplemented
3122                def __ne__(self, other):
3123                    if isinstance(other, C):
3124                        return self.value != other.value
3125                    if isinstance(other, int) or isinstance(other, int):
3126                        return self.value != other
3127                    return NotImplemented
3128                def __lt__(self, other):
3129                    if isinstance(other, C):
3130                        return self.value < other.value
3131                    if isinstance(other, int) or isinstance(other, int):
3132                        return self.value < other
3133                    return NotImplemented
3134                def __le__(self, other):
3135                    if isinstance(other, C):
3136                        return self.value <= other.value
3137                    if isinstance(other, int) or isinstance(other, int):
3138                        return self.value <= other
3139                    return NotImplemented
3140                def __gt__(self, other):
3141                    if isinstance(other, C):
3142                        return self.value > other.value
3143                    if isinstance(other, int) or isinstance(other, int):
3144                        return self.value > other
3145                    return NotImplemented
3146                def __ge__(self, other):
3147                    if isinstance(other, C):
3148                        return self.value >= other.value
3149                    if isinstance(other, int) or isinstance(other, int):
3150                        return self.value >= other
3151                    return NotImplemented
3152
3153            c1 = C(1)
3154            c2 = C(2)
3155            c3 = C(3)
3156            self.assertEqual(c1, 1)
3157            c = {1: c1, 2: c2, 3: c3}
3158            for x in 1, 2, 3:
3159                for y in 1, 2, 3:
3160                    for op in "<", "<=", "==", "!=", ">", ">=":
3161                        self.assertEqual(eval("c[x] %s c[y]" % op),
3162                                     eval("x %s y" % op),
3163                                     "x=%d, y=%d" % (x, y))
3164                        self.assertEqual(eval("c[x] %s y" % op),
3165                                     eval("x %s y" % op),
3166                                     "x=%d, y=%d" % (x, y))
3167                        self.assertEqual(eval("x %s c[y]" % op),
3168                                     eval("x %s y" % op),
3169                                     "x=%d, y=%d" % (x, y))
3170
3171    def test_rich_comparisons(self):
3172        # Testing rich comparisons...
3173        class Z(complex):
3174            pass
3175        z = Z(1)
3176        self.assertEqual(z, 1+0j)
3177        self.assertEqual(1+0j, z)
3178        class ZZ(complex):
3179            def __eq__(self, other):
3180                try:
3181                    return abs(self - other) <= 1e-6
3182                except:
3183                    return NotImplemented
3184        zz = ZZ(1.0000003)
3185        self.assertEqual(zz, 1+0j)
3186        self.assertEqual(1+0j, zz)
3187
3188        class classic:
3189            pass
3190        for base in (classic, int, object, list):
3191            class C(base):
3192                def __init__(self, value):
3193                    self.value = int(value)
3194                def __cmp__(self_, other):
3195                    self.fail("shouldn't call __cmp__")
3196                def __eq__(self, other):
3197                    if isinstance(other, C):
3198                        return self.value == other.value
3199                    if isinstance(other, int) or isinstance(other, int):
3200                        return self.value == other
3201                    return NotImplemented
3202                def __ne__(self, other):
3203                    if isinstance(other, C):
3204                        return self.value != other.value
3205                    if isinstance(other, int) or isinstance(other, int):
3206                        return self.value != other
3207                    return NotImplemented
3208                def __lt__(self, other):
3209                    if isinstance(other, C):
3210                        return self.value < other.value
3211                    if isinstance(other, int) or isinstance(other, int):
3212                        return self.value < other
3213                    return NotImplemented
3214                def __le__(self, other):
3215                    if isinstance(other, C):
3216                        return self.value <= other.value
3217                    if isinstance(other, int) or isinstance(other, int):
3218                        return self.value <= other
3219                    return NotImplemented
3220                def __gt__(self, other):
3221                    if isinstance(other, C):
3222                        return self.value > other.value
3223                    if isinstance(other, int) or isinstance(other, int):
3224                        return self.value > other
3225                    return NotImplemented
3226                def __ge__(self, other):
3227                    if isinstance(other, C):
3228                        return self.value >= other.value
3229                    if isinstance(other, int) or isinstance(other, int):
3230                        return self.value >= other
3231                    return NotImplemented
3232            c1 = C(1)
3233            c2 = C(2)
3234            c3 = C(3)
3235            self.assertEqual(c1, 1)
3236            c = {1: c1, 2: c2, 3: c3}
3237            for x in 1, 2, 3:
3238                for y in 1, 2, 3:
3239                    for op in "<", "<=", "==", "!=", ">", ">=":
3240                        self.assertEqual(eval("c[x] %s c[y]" % op),
3241                                         eval("x %s y" % op),
3242                                         "x=%d, y=%d" % (x, y))
3243                        self.assertEqual(eval("c[x] %s y" % op),
3244                                         eval("x %s y" % op),
3245                                         "x=%d, y=%d" % (x, y))
3246                        self.assertEqual(eval("x %s c[y]" % op),
3247                                         eval("x %s y" % op),
3248                                         "x=%d, y=%d" % (x, y))
3249
3250    def test_descrdoc(self):
3251        # Testing descriptor doc strings...
3252        from _io import FileIO
3253        def check(descr, what):
3254            self.assertEqual(descr.__doc__, what)
3255        check(FileIO.closed, "True if the file is closed") # getset descriptor
3256        check(complex.real, "the real part of a complex number") # member descriptor
3257
3258    def test_doc_descriptor(self):
3259        # Testing __doc__ descriptor...
3260        # SF bug 542984
3261        class DocDescr(object):
3262            def __get__(self, object, otype):
3263                if object:
3264                    object = object.__class__.__name__ + ' instance'
3265                if otype:
3266                    otype = otype.__name__
3267                return 'object=%s; type=%s' % (object, otype)
3268        class NewClass:
3269            __doc__ = DocDescr()
3270        self.assertEqual(NewClass.__doc__, 'object=None; type=NewClass')
3271        self.assertEqual(NewClass().__doc__, 'object=NewClass instance; type=NewClass')
3272
3273    def test_set_class(self):
3274        # Testing __class__ assignment...
3275        class C(object): pass
3276        class D(object): pass
3277        class E(object): pass
3278        class F(D, E): pass
3279        for cls in C, D, E, F:
3280            for cls2 in C, D, E, F:
3281                x = cls()
3282                x.__class__ = cls2
3283                self.assertIs(x.__class__, cls2)
3284                x.__class__ = cls
3285                self.assertIs(x.__class__, cls)
3286        def cant(x, C):
3287            try:
3288                x.__class__ = C
3289            except TypeError:
3290                pass
3291            else:
3292                self.fail("shouldn't allow %r.__class__ = %r" % (x, C))
3293            try:
3294                delattr(x, "__class__")
3295            except (TypeError, AttributeError):
3296                pass
3297            else:
3298                self.fail("shouldn't allow del %r.__class__" % x)
3299        cant(C(), list)
3300        cant(list(), C)
3301        cant(C(), 1)
3302        cant(C(), object)
3303        cant(object(), list)
3304        cant(list(), object)
3305        class Int(int): __slots__ = []
3306        cant(True, int)
3307        cant(2, bool)
3308        o = object()
3309        cant(o, int)
3310        cant(o, type(None))
3311        del o
3312        class G(object):
3313            __slots__ = ["a", "b"]
3314        class H(object):
3315            __slots__ = ["b", "a"]
3316        class I(object):
3317            __slots__ = ["a", "b"]
3318        class J(object):
3319            __slots__ = ["c", "b"]
3320        class K(object):
3321            __slots__ = ["a", "b", "d"]
3322        class L(H):
3323            __slots__ = ["e"]
3324        class M(I):
3325            __slots__ = ["e"]
3326        class N(J):
3327            __slots__ = ["__weakref__"]
3328        class P(J):
3329            __slots__ = ["__dict__"]
3330        class Q(J):
3331            pass
3332        class R(J):
3333            __slots__ = ["__dict__", "__weakref__"]
3334
3335        for cls, cls2 in ((G, H), (G, I), (I, H), (Q, R), (R, Q)):
3336            x = cls()
3337            x.a = 1
3338            x.__class__ = cls2
3339            self.assertIs(x.__class__, cls2,
3340                   "assigning %r as __class__ for %r silently failed" % (cls2, x))
3341            self.assertEqual(x.a, 1)
3342            x.__class__ = cls
3343            self.assertIs(x.__class__, cls,
3344                   "assigning %r as __class__ for %r silently failed" % (cls, x))
3345            self.assertEqual(x.a, 1)
3346        for cls in G, J, K, L, M, N, P, R, list, Int:
3347            for cls2 in G, J, K, L, M, N, P, R, list, Int:
3348                if cls is cls2:
3349                    continue
3350                cant(cls(), cls2)
3351
3352        # Issue5283: when __class__ changes in __del__, the wrong
3353        # type gets DECREF'd.
3354        class O(object):
3355            pass
3356        class A(object):
3357            def __del__(self):
3358                self.__class__ = O
3359        l = [A() for x in range(100)]
3360        del l
3361
3362    def test_set_dict(self):
3363        # Testing __dict__ assignment...
3364        class C(object): pass
3365        a = C()
3366        a.__dict__ = {'b': 1}
3367        self.assertEqual(a.b, 1)
3368        def cant(x, dict):
3369            try:
3370                x.__dict__ = dict
3371            except (AttributeError, TypeError):
3372                pass
3373            else:
3374                self.fail("shouldn't allow %r.__dict__ = %r" % (x, dict))
3375        cant(a, None)
3376        cant(a, [])
3377        cant(a, 1)
3378        del a.__dict__ # Deleting __dict__ is allowed
3379
3380        class Base(object):
3381            pass
3382        def verify_dict_readonly(x):
3383            """
3384            x has to be an instance of a class inheriting from Base.
3385            """
3386            cant(x, {})
3387            try:
3388                del x.__dict__
3389            except (AttributeError, TypeError):
3390                pass
3391            else:
3392                self.fail("shouldn't allow del %r.__dict__" % x)
3393            dict_descr = Base.__dict__["__dict__"]
3394            try:
3395                dict_descr.__set__(x, {})
3396            except (AttributeError, TypeError):
3397                pass
3398            else:
3399                self.fail("dict_descr allowed access to %r's dict" % x)
3400
3401        # Classes don't allow __dict__ assignment and have readonly dicts
3402        class Meta1(type, Base):
3403            pass
3404        class Meta2(Base, type):
3405            pass
3406        class D(object, metaclass=Meta1):
3407            pass
3408        class E(object, metaclass=Meta2):
3409            pass
3410        for cls in C, D, E:
3411            verify_dict_readonly(cls)
3412            class_dict = cls.__dict__
3413            try:
3414                class_dict["spam"] = "eggs"
3415            except TypeError:
3416                pass
3417            else:
3418                self.fail("%r's __dict__ can be modified" % cls)
3419
3420        # Modules also disallow __dict__ assignment
3421        class Module1(types.ModuleType, Base):
3422            pass
3423        class Module2(Base, types.ModuleType):
3424            pass
3425        for ModuleType in Module1, Module2:
3426            mod = ModuleType("spam")
3427            verify_dict_readonly(mod)
3428            mod.__dict__["spam"] = "eggs"
3429
3430        # Exception's __dict__ can be replaced, but not deleted
3431        # (at least not any more than regular exception's __dict__ can
3432        # be deleted; on CPython it is not the case, whereas on PyPy they
3433        # can, just like any other new-style instance's __dict__.)
3434        def can_delete_dict(e):
3435            try:
3436                del e.__dict__
3437            except (TypeError, AttributeError):
3438                return False
3439            else:
3440                return True
3441        class Exception1(Exception, Base):
3442            pass
3443        class Exception2(Base, Exception):
3444            pass
3445        for ExceptionType in Exception, Exception1, Exception2:
3446            e = ExceptionType()
3447            e.__dict__ = {"a": 1}
3448            self.assertEqual(e.a, 1)
3449            self.assertEqual(can_delete_dict(e), can_delete_dict(ValueError()))
3450
3451    def test_binary_operator_override(self):
3452        # Testing overrides of binary operations...
3453        class I(int):
3454            def __repr__(self):
3455                return "I(%r)" % int(self)
3456            def __add__(self, other):
3457                return I(int(self) + int(other))
3458            __radd__ = __add__
3459            def __pow__(self, other, mod=None):
3460                if mod is None:
3461                    return I(pow(int(self), int(other)))
3462                else:
3463                    return I(pow(int(self), int(other), int(mod)))
3464            def __rpow__(self, other, mod=None):
3465                if mod is None:
3466                    return I(pow(int(other), int(self), mod))
3467                else:
3468                    return I(pow(int(other), int(self), int(mod)))
3469
3470        self.assertEqual(repr(I(1) + I(2)), "I(3)")
3471        self.assertEqual(repr(I(1) + 2), "I(3)")
3472        self.assertEqual(repr(1 + I(2)), "I(3)")
3473        self.assertEqual(repr(I(2) ** I(3)), "I(8)")
3474        self.assertEqual(repr(2 ** I(3)), "I(8)")
3475        self.assertEqual(repr(I(2) ** 3), "I(8)")
3476        self.assertEqual(repr(pow(I(2), I(3), I(5))), "I(3)")
3477        class S(str):
3478            def __eq__(self, other):
3479                return self.lower() == other.lower()
3480
3481    def test_subclass_propagation(self):
3482        # Testing propagation of slot functions to subclasses...
3483        class A(object):
3484            pass
3485        class B(A):
3486            pass
3487        class C(A):
3488            pass
3489        class D(B, C):
3490            pass
3491        d = D()
3492        orig_hash = hash(d) # related to id(d) in platform-dependent ways
3493        A.__hash__ = lambda self: 42
3494        self.assertEqual(hash(d), 42)
3495        C.__hash__ = lambda self: 314
3496        self.assertEqual(hash(d), 314)
3497        B.__hash__ = lambda self: 144
3498        self.assertEqual(hash(d), 144)
3499        D.__hash__ = lambda self: 100
3500        self.assertEqual(hash(d), 100)
3501        D.__hash__ = None
3502        self.assertRaises(TypeError, hash, d)
3503        del D.__hash__
3504        self.assertEqual(hash(d), 144)
3505        B.__hash__ = None
3506        self.assertRaises(TypeError, hash, d)
3507        del B.__hash__
3508        self.assertEqual(hash(d), 314)
3509        C.__hash__ = None
3510        self.assertRaises(TypeError, hash, d)
3511        del C.__hash__
3512        self.assertEqual(hash(d), 42)
3513        A.__hash__ = None
3514        self.assertRaises(TypeError, hash, d)
3515        del A.__hash__
3516        self.assertEqual(hash(d), orig_hash)
3517        d.foo = 42
3518        d.bar = 42
3519        self.assertEqual(d.foo, 42)
3520        self.assertEqual(d.bar, 42)
3521        def __getattribute__(self, name):
3522            if name == "foo":
3523                return 24
3524            return object.__getattribute__(self, name)
3525        A.__getattribute__ = __getattribute__
3526        self.assertEqual(d.foo, 24)
3527        self.assertEqual(d.bar, 42)
3528        def __getattr__(self, name):
3529            if name in ("spam", "foo", "bar"):
3530                return "hello"
3531            raise AttributeError(name)
3532        B.__getattr__ = __getattr__
3533        self.assertEqual(d.spam, "hello")
3534        self.assertEqual(d.foo, 24)
3535        self.assertEqual(d.bar, 42)
3536        del A.__getattribute__
3537        self.assertEqual(d.foo, 42)
3538        del d.foo
3539        self.assertEqual(d.foo, "hello")
3540        self.assertEqual(d.bar, 42)
3541        del B.__getattr__
3542        try:
3543            d.foo
3544        except AttributeError:
3545            pass
3546        else:
3547            self.fail("d.foo should be undefined now")
3548
3549        # Test a nasty bug in recurse_down_subclasses()
3550        class A(object):
3551            pass
3552        class B(A):
3553            pass
3554        del B
3555        support.gc_collect()
3556        A.__setitem__ = lambda *a: None # crash
3557
3558    def test_buffer_inheritance(self):
3559        # Testing that buffer interface is inherited ...
3560
3561        import binascii
3562        # SF bug [#470040] ParseTuple t# vs subclasses.
3563
3564        class MyBytes(bytes):
3565            pass
3566        base = b'abc'
3567        m = MyBytes(base)
3568        # b2a_hex uses the buffer interface to get its argument's value, via
3569        # PyArg_ParseTuple 't#' code.
3570        self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base))
3571
3572        class MyInt(int):
3573            pass
3574        m = MyInt(42)
3575        try:
3576            binascii.b2a_hex(m)
3577            self.fail('subclass of int should not have a buffer interface')
3578        except TypeError:
3579            pass
3580
3581    def test_str_of_str_subclass(self):
3582        # Testing __str__ defined in subclass of str ...
3583        import binascii
3584
3585        class octetstring(str):
3586            def __str__(self):
3587                return binascii.b2a_hex(self.encode('ascii')).decode("ascii")
3588            def __repr__(self):
3589                return self + " repr"
3590
3591        o = octetstring('A')
3592        self.assertEqual(type(o), octetstring)
3593        self.assertEqual(type(str(o)), str)
3594        self.assertEqual(type(repr(o)), str)
3595        self.assertEqual(ord(o), 0x41)
3596        self.assertEqual(str(o), '41')
3597        self.assertEqual(repr(o), 'A repr')
3598        self.assertEqual(o.__str__(), '41')
3599        self.assertEqual(o.__repr__(), 'A repr')
3600
3601    def test_repr_with_module_str_subclass(self):
3602        # gh-98783
3603        class StrSub(str):
3604            pass
3605        class Some:
3606            pass
3607        Some.__module__ = StrSub('example')
3608        self.assertIsInstance(repr(Some), str)  # should not crash
3609        self.assertIsInstance(repr(Some()), str)  # should not crash
3610
3611    def test_keyword_arguments(self):
3612        # Testing keyword arguments to __init__, __call__...
3613        def f(a): return a
3614        self.assertEqual(f.__call__(a=42), 42)
3615        ba = bytearray()
3616        bytearray.__init__(ba, 'abc\xbd\u20ac',
3617                           encoding='latin1', errors='replace')
3618        self.assertEqual(ba, b'abc\xbd?')
3619
3620    def test_recursive_call(self):
3621        # Testing recursive __call__() by setting to instance of class...
3622        class A(object):
3623            pass
3624
3625        A.__call__ = A()
3626        with self.assertRaises(RecursionError):
3627            A()()
3628
3629    def test_delete_hook(self):
3630        # Testing __del__ hook...
3631        log = []
3632        class C(object):
3633            def __del__(self):
3634                log.append(1)
3635        c = C()
3636        self.assertEqual(log, [])
3637        del c
3638        support.gc_collect()
3639        self.assertEqual(log, [1])
3640
3641        class D(object): pass
3642        d = D()
3643        try: del d[0]
3644        except TypeError: pass
3645        else: self.fail("invalid del() didn't raise TypeError")
3646
3647    def test_hash_inheritance(self):
3648        # Testing hash of mutable subclasses...
3649
3650        class mydict(dict):
3651            pass
3652        d = mydict()
3653        try:
3654            hash(d)
3655        except TypeError:
3656            pass
3657        else:
3658            self.fail("hash() of dict subclass should fail")
3659
3660        class mylist(list):
3661            pass
3662        d = mylist()
3663        try:
3664            hash(d)
3665        except TypeError:
3666            pass
3667        else:
3668            self.fail("hash() of list subclass should fail")
3669
3670    def test_str_operations(self):
3671        try: 'a' + 5
3672        except TypeError: pass
3673        else: self.fail("'' + 5 doesn't raise TypeError")
3674
3675        try: ''.split('')
3676        except ValueError: pass
3677        else: self.fail("''.split('') doesn't raise ValueError")
3678
3679        try: ''.join([0])
3680        except TypeError: pass
3681        else: self.fail("''.join([0]) doesn't raise TypeError")
3682
3683        try: ''.rindex('5')
3684        except ValueError: pass
3685        else: self.fail("''.rindex('5') doesn't raise ValueError")
3686
3687        try: '%(n)s' % None
3688        except TypeError: pass
3689        else: self.fail("'%(n)s' % None doesn't raise TypeError")
3690
3691        try: '%(n' % {}
3692        except ValueError: pass
3693        else: self.fail("'%(n' % {} '' doesn't raise ValueError")
3694
3695        try: '%*s' % ('abc')
3696        except TypeError: pass
3697        else: self.fail("'%*s' % ('abc') doesn't raise TypeError")
3698
3699        try: '%*.*s' % ('abc', 5)
3700        except TypeError: pass
3701        else: self.fail("'%*.*s' % ('abc', 5) doesn't raise TypeError")
3702
3703        try: '%s' % (1, 2)
3704        except TypeError: pass
3705        else: self.fail("'%s' % (1, 2) doesn't raise TypeError")
3706
3707        try: '%' % None
3708        except ValueError: pass
3709        else: self.fail("'%' % None doesn't raise ValueError")
3710
3711        self.assertEqual('534253'.isdigit(), 1)
3712        self.assertEqual('534253x'.isdigit(), 0)
3713        self.assertEqual('%c' % 5, '\x05')
3714        self.assertEqual('%c' % '5', '5')
3715
3716    def test_deepcopy_recursive(self):
3717        # Testing deepcopy of recursive objects...
3718        class Node:
3719            pass
3720        a = Node()
3721        b = Node()
3722        a.b = b
3723        b.a = a
3724        z = deepcopy(a) # This blew up before
3725
3726    def test_uninitialized_modules(self):
3727        # Testing uninitialized module objects...
3728        from types import ModuleType as M
3729        m = M.__new__(M)
3730        str(m)
3731        self.assertNotHasAttr(m, "__name__")
3732        self.assertNotHasAttr(m, "__file__")
3733        self.assertNotHasAttr(m, "foo")
3734        self.assertFalse(m.__dict__)   # None or {} are both reasonable answers
3735        m.foo = 1
3736        self.assertEqual(m.__dict__, {"foo": 1})
3737
3738    def test_funny_new(self):
3739        # Testing __new__ returning something unexpected...
3740        class C(object):
3741            def __new__(cls, arg):
3742                if isinstance(arg, str): return [1, 2, 3]
3743                elif isinstance(arg, int): return object.__new__(D)
3744                else: return object.__new__(cls)
3745        class D(C):
3746            def __init__(self, arg):
3747                self.foo = arg
3748        self.assertEqual(C("1"), [1, 2, 3])
3749        self.assertEqual(D("1"), [1, 2, 3])
3750        d = D(None)
3751        self.assertEqual(d.foo, None)
3752        d = C(1)
3753        self.assertIsInstance(d, D)
3754        self.assertEqual(d.foo, 1)
3755        d = D(1)
3756        self.assertIsInstance(d, D)
3757        self.assertEqual(d.foo, 1)
3758
3759        class C(object):
3760            @staticmethod
3761            def __new__(*args):
3762                return args
3763        self.assertEqual(C(1, 2), (C, 1, 2))
3764        class D(C):
3765            pass
3766        self.assertEqual(D(1, 2), (D, 1, 2))
3767
3768        class C(object):
3769            @classmethod
3770            def __new__(*args):
3771                return args
3772        self.assertEqual(C(1, 2), (C, C, 1, 2))
3773        class D(C):
3774            pass
3775        self.assertEqual(D(1, 2), (D, D, 1, 2))
3776
3777    def test_imul_bug(self):
3778        # Testing for __imul__ problems...
3779        # SF bug 544647
3780        class C(object):
3781            def __imul__(self, other):
3782                return (self, other)
3783        x = C()
3784        y = x
3785        y *= 1.0
3786        self.assertEqual(y, (x, 1.0))
3787        y = x
3788        y *= 2
3789        self.assertEqual(y, (x, 2))
3790        y = x
3791        y *= 3
3792        self.assertEqual(y, (x, 3))
3793        y = x
3794        y *= 1<<100
3795        self.assertEqual(y, (x, 1<<100))
3796        y = x
3797        y *= None
3798        self.assertEqual(y, (x, None))
3799        y = x
3800        y *= "foo"
3801        self.assertEqual(y, (x, "foo"))
3802
3803    def test_copy_setstate(self):
3804        # Testing that copy.*copy() correctly uses __setstate__...
3805        import copy
3806        class C(object):
3807            def __init__(self, foo=None):
3808                self.foo = foo
3809                self.__foo = foo
3810            def setfoo(self, foo=None):
3811                self.foo = foo
3812            def getfoo(self):
3813                return self.__foo
3814            def __getstate__(self):
3815                return [self.foo]
3816            def __setstate__(self_, lst):
3817                self.assertEqual(len(lst), 1)
3818                self_.__foo = self_.foo = lst[0]
3819        a = C(42)
3820        a.setfoo(24)
3821        self.assertEqual(a.foo, 24)
3822        self.assertEqual(a.getfoo(), 42)
3823        b = copy.copy(a)
3824        self.assertEqual(b.foo, 24)
3825        self.assertEqual(b.getfoo(), 24)
3826        b = copy.deepcopy(a)
3827        self.assertEqual(b.foo, 24)
3828        self.assertEqual(b.getfoo(), 24)
3829
3830    def test_slices(self):
3831        # Testing cases with slices and overridden __getitem__ ...
3832
3833        # Strings
3834        self.assertEqual("hello"[:4], "hell")
3835        self.assertEqual("hello"[slice(4)], "hell")
3836        self.assertEqual(str.__getitem__("hello", slice(4)), "hell")
3837        class S(str):
3838            def __getitem__(self, x):
3839                return str.__getitem__(self, x)
3840        self.assertEqual(S("hello")[:4], "hell")
3841        self.assertEqual(S("hello")[slice(4)], "hell")
3842        self.assertEqual(S("hello").__getitem__(slice(4)), "hell")
3843        # Tuples
3844        self.assertEqual((1,2,3)[:2], (1,2))
3845        self.assertEqual((1,2,3)[slice(2)], (1,2))
3846        self.assertEqual(tuple.__getitem__((1,2,3), slice(2)), (1,2))
3847        class T(tuple):
3848            def __getitem__(self, x):
3849                return tuple.__getitem__(self, x)
3850        self.assertEqual(T((1,2,3))[:2], (1,2))
3851        self.assertEqual(T((1,2,3))[slice(2)], (1,2))
3852        self.assertEqual(T((1,2,3)).__getitem__(slice(2)), (1,2))
3853        # Lists
3854        self.assertEqual([1,2,3][:2], [1,2])
3855        self.assertEqual([1,2,3][slice(2)], [1,2])
3856        self.assertEqual(list.__getitem__([1,2,3], slice(2)), [1,2])
3857        class L(list):
3858            def __getitem__(self, x):
3859                return list.__getitem__(self, x)
3860        self.assertEqual(L([1,2,3])[:2], [1,2])
3861        self.assertEqual(L([1,2,3])[slice(2)], [1,2])
3862        self.assertEqual(L([1,2,3]).__getitem__(slice(2)), [1,2])
3863        # Now do lists and __setitem__
3864        a = L([1,2,3])
3865        a[slice(1, 3)] = [3,2]
3866        self.assertEqual(a, [1,3,2])
3867        a[slice(0, 2, 1)] = [3,1]
3868        self.assertEqual(a, [3,1,2])
3869        a.__setitem__(slice(1, 3), [2,1])
3870        self.assertEqual(a, [3,2,1])
3871        a.__setitem__(slice(0, 2, 1), [2,3])
3872        self.assertEqual(a, [2,3,1])
3873
3874    def test_subtype_resurrection(self):
3875        # Testing resurrection of new-style instance...
3876
3877        class C(object):
3878            container = []
3879
3880            def __del__(self):
3881                # resurrect the instance
3882                C.container.append(self)
3883
3884        c = C()
3885        c.attr = 42
3886
3887        # The most interesting thing here is whether this blows up, due to
3888        # flawed GC tracking logic in typeobject.c's call_finalizer() (a 2.2.1
3889        # bug).
3890        del c
3891
3892        support.gc_collect()
3893        self.assertEqual(len(C.container), 1)
3894
3895        # Make c mortal again, so that the test framework with -l doesn't report
3896        # it as a leak.
3897        del C.__del__
3898
3899    def test_slots_trash(self):
3900        # Testing slot trash...
3901        # Deallocating deeply nested slotted trash caused stack overflows
3902        class trash(object):
3903            __slots__ = ['x']
3904            def __init__(self, x):
3905                self.x = x
3906        o = None
3907        for i in range(50000):
3908            o = trash(o)
3909        del o
3910
3911    def test_slots_multiple_inheritance(self):
3912        # SF bug 575229, multiple inheritance w/ slots dumps core
3913        class A(object):
3914            __slots__=()
3915        class B(object):
3916            pass
3917        class C(A,B) :
3918            __slots__=()
3919        if support.check_impl_detail():
3920            self.assertEqual(C.__basicsize__, B.__basicsize__)
3921        self.assertHasAttr(C, '__dict__')
3922        self.assertHasAttr(C, '__weakref__')
3923        C().x = 2
3924
3925    def test_rmul(self):
3926        # Testing correct invocation of __rmul__...
3927        # SF patch 592646
3928        class C(object):
3929            def __mul__(self, other):
3930                return "mul"
3931            def __rmul__(self, other):
3932                return "rmul"
3933        a = C()
3934        self.assertEqual(a*2, "mul")
3935        self.assertEqual(a*2.2, "mul")
3936        self.assertEqual(2*a, "rmul")
3937        self.assertEqual(2.2*a, "rmul")
3938
3939    def test_ipow(self):
3940        # Testing correct invocation of __ipow__...
3941        # [SF bug 620179]
3942        class C(object):
3943            def __ipow__(self, other):
3944                pass
3945        a = C()
3946        a **= 2
3947
3948    def test_ipow_returns_not_implemented(self):
3949        class A:
3950            def __ipow__(self, other):
3951                return NotImplemented
3952
3953        class B(A):
3954            def __rpow__(self, other):
3955                return 1
3956
3957        class C(A):
3958            def __pow__(self, other):
3959                return 2
3960        a = A()
3961        b = B()
3962        c = C()
3963
3964        a **= b
3965        self.assertEqual(a, 1)
3966
3967        c **= b
3968        self.assertEqual(c, 2)
3969
3970    def test_no_ipow(self):
3971        class B:
3972            def __rpow__(self, other):
3973                return 1
3974
3975        a = object()
3976        b = B()
3977        a **= b
3978        self.assertEqual(a, 1)
3979
3980    def test_ipow_exception_text(self):
3981        x = None
3982        with self.assertRaises(TypeError) as cm:
3983            x **= 2
3984        self.assertIn('unsupported operand type(s) for **=', str(cm.exception))
3985
3986        with self.assertRaises(TypeError) as cm:
3987            y = x ** 2
3988        self.assertIn('unsupported operand type(s) for **', str(cm.exception))
3989
3990    def test_mutable_bases(self):
3991        # Testing mutable bases...
3992
3993        # stuff that should work:
3994        class C(object):
3995            pass
3996        class C2(object):
3997            def __getattribute__(self, attr):
3998                if attr == 'a':
3999                    return 2
4000                else:
4001                    return super(C2, self).__getattribute__(attr)
4002            def meth(self):
4003                return 1
4004        class D(C):
4005            pass
4006        class E(D):
4007            pass
4008        d = D()
4009        e = E()
4010        D.__bases__ = (C,)
4011        D.__bases__ = (C2,)
4012        self.assertEqual(d.meth(), 1)
4013        self.assertEqual(e.meth(), 1)
4014        self.assertEqual(d.a, 2)
4015        self.assertEqual(e.a, 2)
4016        self.assertEqual(C2.__subclasses__(), [D])
4017
4018        try:
4019            del D.__bases__
4020        except (TypeError, AttributeError):
4021            pass
4022        else:
4023            self.fail("shouldn't be able to delete .__bases__")
4024
4025        try:
4026            D.__bases__ = ()
4027        except TypeError as msg:
4028            if str(msg) == "a new-style class can't have only classic bases":
4029                self.fail("wrong error message for .__bases__ = ()")
4030        else:
4031            self.fail("shouldn't be able to set .__bases__ to ()")
4032
4033        try:
4034            D.__bases__ = (D,)
4035        except TypeError:
4036            pass
4037        else:
4038            # actually, we'll have crashed by here...
4039            self.fail("shouldn't be able to create inheritance cycles")
4040
4041        try:
4042            D.__bases__ = (C, C)
4043        except TypeError:
4044            pass
4045        else:
4046            self.fail("didn't detect repeated base classes")
4047
4048        try:
4049            D.__bases__ = (E,)
4050        except TypeError:
4051            pass
4052        else:
4053            self.fail("shouldn't be able to create inheritance cycles")
4054
4055    def test_builtin_bases(self):
4056        # Make sure all the builtin types can have their base queried without
4057        # segfaulting. See issue #5787.
4058        builtin_types = [tp for tp in builtins.__dict__.values()
4059                         if isinstance(tp, type)]
4060        for tp in builtin_types:
4061            object.__getattribute__(tp, "__bases__")
4062            if tp is not object:
4063                if tp is ExceptionGroup:
4064                    num_bases = 2
4065                else:
4066                    num_bases = 1
4067                self.assertEqual(len(tp.__bases__), num_bases, tp)
4068
4069        class L(list):
4070            pass
4071
4072        class C(object):
4073            pass
4074
4075        class D(C):
4076            pass
4077
4078        try:
4079            L.__bases__ = (dict,)
4080        except TypeError:
4081            pass
4082        else:
4083            self.fail("shouldn't turn list subclass into dict subclass")
4084
4085        try:
4086            list.__bases__ = (dict,)
4087        except TypeError:
4088            pass
4089        else:
4090            self.fail("shouldn't be able to assign to list.__bases__")
4091
4092        try:
4093            D.__bases__ = (C, list)
4094        except TypeError:
4095            pass
4096        else:
4097            self.fail("best_base calculation found wanting")
4098
4099    def test_unsubclassable_types(self):
4100        with self.assertRaises(TypeError):
4101            class X(type(None)):
4102                pass
4103        with self.assertRaises(TypeError):
4104            class X(object, type(None)):
4105                pass
4106        with self.assertRaises(TypeError):
4107            class X(type(None), object):
4108                pass
4109        class O(object):
4110            pass
4111        with self.assertRaises(TypeError):
4112            class X(O, type(None)):
4113                pass
4114        with self.assertRaises(TypeError):
4115            class X(type(None), O):
4116                pass
4117
4118        class X(object):
4119            pass
4120        with self.assertRaises(TypeError):
4121            X.__bases__ = type(None),
4122        with self.assertRaises(TypeError):
4123            X.__bases__ = object, type(None)
4124        with self.assertRaises(TypeError):
4125            X.__bases__ = type(None), object
4126        with self.assertRaises(TypeError):
4127            X.__bases__ = O, type(None)
4128        with self.assertRaises(TypeError):
4129            X.__bases__ = type(None), O
4130
4131    def test_mutable_bases_with_failing_mro(self):
4132        # Testing mutable bases with failing mro...
4133        class WorkOnce(type):
4134            def __new__(self, name, bases, ns):
4135                self.flag = 0
4136                return super(WorkOnce, self).__new__(WorkOnce, name, bases, ns)
4137            def mro(self):
4138                if self.flag > 0:
4139                    raise RuntimeError("bozo")
4140                else:
4141                    self.flag += 1
4142                    return type.mro(self)
4143
4144        class WorkAlways(type):
4145            def mro(self):
4146                # this is here to make sure that .mro()s aren't called
4147                # with an exception set (which was possible at one point).
4148                # An error message will be printed in a debug build.
4149                # What's a good way to test for this?
4150                return type.mro(self)
4151
4152        class C(object):
4153            pass
4154
4155        class C2(object):
4156            pass
4157
4158        class D(C):
4159            pass
4160
4161        class E(D):
4162            pass
4163
4164        class F(D, metaclass=WorkOnce):
4165            pass
4166
4167        class G(D, metaclass=WorkAlways):
4168            pass
4169
4170        # Immediate subclasses have their mro's adjusted in alphabetical
4171        # order, so E's will get adjusted before adjusting F's fails.  We
4172        # check here that E's gets restored.
4173
4174        E_mro_before = E.__mro__
4175        D_mro_before = D.__mro__
4176
4177        try:
4178            D.__bases__ = (C2,)
4179        except RuntimeError:
4180            self.assertEqual(E.__mro__, E_mro_before)
4181            self.assertEqual(D.__mro__, D_mro_before)
4182        else:
4183            self.fail("exception not propagated")
4184
4185    def test_mutable_bases_catch_mro_conflict(self):
4186        # Testing mutable bases catch mro conflict...
4187        class A(object):
4188            pass
4189
4190        class B(object):
4191            pass
4192
4193        class C(A, B):
4194            pass
4195
4196        class D(A, B):
4197            pass
4198
4199        class E(C, D):
4200            pass
4201
4202        try:
4203            C.__bases__ = (B, A)
4204        except TypeError:
4205            pass
4206        else:
4207            self.fail("didn't catch MRO conflict")
4208
4209    def test_mutable_names(self):
4210        # Testing mutable names...
4211        class C(object):
4212            pass
4213
4214        # C.__module__ could be 'test_descr' or '__main__'
4215        mod = C.__module__
4216
4217        C.__name__ = 'D'
4218        self.assertEqual((C.__module__, C.__name__), (mod, 'D'))
4219
4220        C.__name__ = 'D.E'
4221        self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))
4222
4223    def test_evil_type_name(self):
4224        # A badly placed Py_DECREF in type_set_name led to arbitrary code
4225        # execution while the type structure was not in a sane state, and a
4226        # possible segmentation fault as a result.  See bug #16447.
4227        class Nasty(str):
4228            def __del__(self):
4229                C.__name__ = "other"
4230
4231        class C:
4232            pass
4233
4234        C.__name__ = Nasty("abc")
4235        C.__name__ = "normal"
4236
4237    def test_subclass_right_op(self):
4238        # Testing correct dispatch of subclass overloading __r<op>__...
4239
4240        # This code tests various cases where right-dispatch of a subclass
4241        # should be preferred over left-dispatch of a base class.
4242
4243        # Case 1: subclass of int; this tests code in abstract.c::binary_op1()
4244
4245        class B(int):
4246            def __floordiv__(self, other):
4247                return "B.__floordiv__"
4248            def __rfloordiv__(self, other):
4249                return "B.__rfloordiv__"
4250
4251        self.assertEqual(B(1) // 1, "B.__floordiv__")
4252        self.assertEqual(1 // B(1), "B.__rfloordiv__")
4253
4254        # Case 2: subclass of object; this is just the baseline for case 3
4255
4256        class C(object):
4257            def __floordiv__(self, other):
4258                return "C.__floordiv__"
4259            def __rfloordiv__(self, other):
4260                return "C.__rfloordiv__"
4261
4262        self.assertEqual(C() // 1, "C.__floordiv__")
4263        self.assertEqual(1 // C(), "C.__rfloordiv__")
4264
4265        # Case 3: subclass of new-style class; here it gets interesting
4266
4267        class D(C):
4268            def __floordiv__(self, other):
4269                return "D.__floordiv__"
4270            def __rfloordiv__(self, other):
4271                return "D.__rfloordiv__"
4272
4273        self.assertEqual(D() // C(), "D.__floordiv__")
4274        self.assertEqual(C() // D(), "D.__rfloordiv__")
4275
4276        # Case 4: this didn't work right in 2.2.2 and 2.3a1
4277
4278        class E(C):
4279            pass
4280
4281        self.assertEqual(E.__rfloordiv__, C.__rfloordiv__)
4282
4283        self.assertEqual(E() // 1, "C.__floordiv__")
4284        self.assertEqual(1 // E(), "C.__rfloordiv__")
4285        self.assertEqual(E() // C(), "C.__floordiv__")
4286        self.assertEqual(C() // E(), "C.__floordiv__") # This one would fail
4287
4288    @support.impl_detail("testing an internal kind of method object")
4289    def test_meth_class_get(self):
4290        # Testing __get__ method of METH_CLASS C methods...
4291        # Full coverage of descrobject.c::classmethod_get()
4292
4293        # Baseline
4294        arg = [1, 2, 3]
4295        res = {1: None, 2: None, 3: None}
4296        self.assertEqual(dict.fromkeys(arg), res)
4297        self.assertEqual({}.fromkeys(arg), res)
4298
4299        # Now get the descriptor
4300        descr = dict.__dict__["fromkeys"]
4301
4302        # More baseline using the descriptor directly
4303        self.assertEqual(descr.__get__(None, dict)(arg), res)
4304        self.assertEqual(descr.__get__({})(arg), res)
4305
4306        # Now check various error cases
4307        try:
4308            descr.__get__(None, None)
4309        except TypeError:
4310            pass
4311        else:
4312            self.fail("shouldn't have allowed descr.__get__(None, None)")
4313        try:
4314            descr.__get__(42)
4315        except TypeError:
4316            pass
4317        else:
4318            self.fail("shouldn't have allowed descr.__get__(42)")
4319        try:
4320            descr.__get__(None, 42)
4321        except TypeError:
4322            pass
4323        else:
4324            self.fail("shouldn't have allowed descr.__get__(None, 42)")
4325        try:
4326            descr.__get__(None, int)
4327        except TypeError:
4328            pass
4329        else:
4330            self.fail("shouldn't have allowed descr.__get__(None, int)")
4331
4332    def test_isinst_isclass(self):
4333        # Testing proxy isinstance() and isclass()...
4334        class Proxy(object):
4335            def __init__(self, obj):
4336                self.__obj = obj
4337            def __getattribute__(self, name):
4338                if name.startswith("_Proxy__"):
4339                    return object.__getattribute__(self, name)
4340                else:
4341                    return getattr(self.__obj, name)
4342        # Test with a classic class
4343        class C:
4344            pass
4345        a = C()
4346        pa = Proxy(a)
4347        self.assertIsInstance(a, C)  # Baseline
4348        self.assertIsInstance(pa, C) # Test
4349        # Test with a classic subclass
4350        class D(C):
4351            pass
4352        a = D()
4353        pa = Proxy(a)
4354        self.assertIsInstance(a, C)  # Baseline
4355        self.assertIsInstance(pa, C) # Test
4356        # Test with a new-style class
4357        class C(object):
4358            pass
4359        a = C()
4360        pa = Proxy(a)
4361        self.assertIsInstance(a, C)  # Baseline
4362        self.assertIsInstance(pa, C) # Test
4363        # Test with a new-style subclass
4364        class D(C):
4365            pass
4366        a = D()
4367        pa = Proxy(a)
4368        self.assertIsInstance(a, C)  # Baseline
4369        self.assertIsInstance(pa, C) # Test
4370
4371    def test_proxy_super(self):
4372        # Testing super() for a proxy object...
4373        class Proxy(object):
4374            def __init__(self, obj):
4375                self.__obj = obj
4376            def __getattribute__(self, name):
4377                if name.startswith("_Proxy__"):
4378                    return object.__getattribute__(self, name)
4379                else:
4380                    return getattr(self.__obj, name)
4381
4382        class B(object):
4383            def f(self):
4384                return "B.f"
4385
4386        class C(B):
4387            def f(self):
4388                return super(C, self).f() + "->C.f"
4389
4390        obj = C()
4391        p = Proxy(obj)
4392        self.assertEqual(C.__dict__["f"](p), "B.f->C.f")
4393
4394    def test_carloverre(self):
4395        # Testing prohibition of Carlo Verre's hack...
4396        try:
4397            object.__setattr__(str, "foo", 42)
4398        except TypeError:
4399            pass
4400        else:
4401            self.fail("Carlo Verre __setattr__ succeeded!")
4402        try:
4403            object.__delattr__(str, "lower")
4404        except TypeError:
4405            pass
4406        else:
4407            self.fail("Carlo Verre __delattr__ succeeded!")
4408
4409    def test_carloverre_multi_inherit_valid(self):
4410        class A(type):
4411            def __setattr__(cls, key, value):
4412                type.__setattr__(cls, key, value)
4413
4414        class B:
4415            pass
4416
4417        class C(B, A):
4418            pass
4419
4420        obj = C('D', (object,), {})
4421        try:
4422            obj.test = True
4423        except TypeError:
4424            self.fail("setattr through direct base types should be legal")
4425
4426    def test_carloverre_multi_inherit_invalid(self):
4427        class A(type):
4428            def __setattr__(cls, key, value):
4429                object.__setattr__(cls, key, value)  # this should fail!
4430
4431        class B:
4432            pass
4433
4434        class C(B, A):
4435            pass
4436
4437        obj = C('D', (object,), {})
4438        try:
4439            obj.test = True
4440        except TypeError:
4441            pass
4442        else:
4443            self.fail("setattr through indirect base types should be rejected")
4444
4445    def test_weakref_segfault(self):
4446        # Testing weakref segfault...
4447        # SF 742911
4448        import weakref
4449
4450        class Provoker:
4451            def __init__(self, referrent):
4452                self.ref = weakref.ref(referrent)
4453
4454            def __del__(self):
4455                x = self.ref()
4456
4457        class Oops(object):
4458            pass
4459
4460        o = Oops()
4461        o.whatever = Provoker(o)
4462        del o
4463
4464    @support.requires_resource('cpu')
4465    def test_wrapper_segfault(self):
4466        # SF 927248: deeply nested wrappers could cause stack overflow
4467        f = lambda:None
4468        for i in range(1000000):
4469            f = f.__call__
4470        f = None
4471
4472    def test_file_fault(self):
4473        # Testing sys.stdout is changed in getattr...
4474        class StdoutGuard:
4475            def __getattr__(self, attr):
4476                sys.stdout = sys.__stdout__
4477                raise RuntimeError(f"Premature access to sys.stdout.{attr}")
4478
4479        with redirect_stdout(StdoutGuard()):
4480            with self.assertRaises(RuntimeError):
4481                print("Oops!")
4482
4483    def test_vicious_descriptor_nonsense(self):
4484        # Testing vicious_descriptor_nonsense...
4485
4486        # A potential segfault spotted by Thomas Wouters in mail to
4487        # python-dev 2003-04-17, turned into an example & fixed by Michael
4488        # Hudson just less than four months later...
4489
4490        class Evil(object):
4491            def __hash__(self):
4492                return hash('attr')
4493            def __eq__(self, other):
4494                try:
4495                    del C.attr
4496                except AttributeError:
4497                    # possible race condition
4498                    pass
4499                return 0
4500
4501        class Descr(object):
4502            def __get__(self, ob, type=None):
4503                return 1
4504
4505        class C(object):
4506            attr = Descr()
4507
4508        c = C()
4509        c.__dict__[Evil()] = 0
4510
4511        self.assertEqual(c.attr, 1)
4512        # this makes a crash more likely:
4513        support.gc_collect()
4514        self.assertNotHasAttr(c, 'attr')
4515
4516    def test_init(self):
4517        # SF 1155938
4518        class Foo(object):
4519            def __init__(self):
4520                return 10
4521        try:
4522            Foo()
4523        except TypeError:
4524            pass
4525        else:
4526            self.fail("did not test __init__() for None return")
4527
4528    def assertNotOrderable(self, a, b):
4529        with self.assertRaises(TypeError):
4530            a < b
4531        with self.assertRaises(TypeError):
4532            a > b
4533        with self.assertRaises(TypeError):
4534            a <= b
4535        with self.assertRaises(TypeError):
4536            a >= b
4537
4538    def test_method_wrapper(self):
4539        # Testing method-wrapper objects...
4540        # <type 'method-wrapper'> did not support any reflection before 2.5
4541        l = []
4542        self.assertTrue(l.__add__ == l.__add__)
4543        self.assertFalse(l.__add__ != l.__add__)
4544        self.assertFalse(l.__add__ == [].__add__)
4545        self.assertTrue(l.__add__ != [].__add__)
4546        self.assertFalse(l.__add__ == l.__mul__)
4547        self.assertTrue(l.__add__ != l.__mul__)
4548        self.assertNotOrderable(l.__add__, l.__add__)
4549        self.assertEqual(l.__add__.__name__, '__add__')
4550        self.assertIs(l.__add__.__self__, l)
4551        self.assertIs(l.__add__.__objclass__, list)
4552        self.assertEqual(l.__add__.__doc__, list.__add__.__doc__)
4553        # hash([].__add__) should not be based on hash([])
4554        hash(l.__add__)
4555
4556    def test_builtin_function_or_method(self):
4557        # Not really belonging to test_descr, but introspection and
4558        # comparison on <type 'builtin_function_or_method'> seems not
4559        # to be tested elsewhere
4560        l = []
4561        self.assertTrue(l.append == l.append)
4562        self.assertFalse(l.append != l.append)
4563        self.assertFalse(l.append == [].append)
4564        self.assertTrue(l.append != [].append)
4565        self.assertFalse(l.append == l.pop)
4566        self.assertTrue(l.append != l.pop)
4567        self.assertNotOrderable(l.append, l.append)
4568        self.assertEqual(l.append.__name__, 'append')
4569        self.assertIs(l.append.__self__, l)
4570        # self.assertIs(l.append.__objclass__, list) --- could be added?
4571        self.assertEqual(l.append.__doc__, list.append.__doc__)
4572        # hash([].append) should not be based on hash([])
4573        hash(l.append)
4574
4575    def test_special_unbound_method_types(self):
4576        # Testing objects of <type 'wrapper_descriptor'>...
4577        self.assertTrue(list.__add__ == list.__add__)
4578        self.assertFalse(list.__add__ != list.__add__)
4579        self.assertFalse(list.__add__ == list.__mul__)
4580        self.assertTrue(list.__add__ != list.__mul__)
4581        self.assertNotOrderable(list.__add__, list.__add__)
4582        self.assertEqual(list.__add__.__name__, '__add__')
4583        self.assertIs(list.__add__.__objclass__, list)
4584
4585        # Testing objects of <type 'method_descriptor'>...
4586        self.assertTrue(list.append == list.append)
4587        self.assertFalse(list.append != list.append)
4588        self.assertFalse(list.append == list.pop)
4589        self.assertTrue(list.append != list.pop)
4590        self.assertNotOrderable(list.append, list.append)
4591        self.assertEqual(list.append.__name__, 'append')
4592        self.assertIs(list.append.__objclass__, list)
4593
4594    def test_not_implemented(self):
4595        # Testing NotImplemented...
4596        # all binary methods should be able to return a NotImplemented
4597
4598        def specialmethod(self, other):
4599            return NotImplemented
4600
4601        def check(expr, x, y):
4602            with (
4603                self.subTest(expr=expr, x=x, y=y),
4604                self.assertRaises(TypeError),
4605            ):
4606                exec(expr, {'x': x, 'y': y})
4607
4608        N1 = sys.maxsize + 1    # might trigger OverflowErrors instead of
4609                                # TypeErrors
4610        N2 = sys.maxsize         # if sizeof(int) < sizeof(long), might trigger
4611                                #   ValueErrors instead of TypeErrors
4612        for name, expr, iexpr in [
4613                ('__add__',      'x + y',                   'x += y'),
4614                ('__sub__',      'x - y',                   'x -= y'),
4615                ('__mul__',      'x * y',                   'x *= y'),
4616                ('__matmul__',   'x @ y',                   'x @= y'),
4617                ('__truediv__',  'x / y',                   'x /= y'),
4618                ('__floordiv__', 'x // y',                  'x //= y'),
4619                ('__mod__',      'x % y',                   'x %= y'),
4620                ('__divmod__',   'divmod(x, y)',            None),
4621                ('__pow__',      'x ** y',                  'x **= y'),
4622                ('__lshift__',   'x << y',                  'x <<= y'),
4623                ('__rshift__',   'x >> y',                  'x >>= y'),
4624                ('__and__',      'x & y',                   'x &= y'),
4625                ('__or__',       'x | y',                   'x |= y'),
4626                ('__xor__',      'x ^ y',                   'x ^= y')]:
4627            # Defines 'left' magic method:
4628            A = type('A', (), {name: specialmethod})
4629            a = A()
4630            check(expr, a, a)
4631            check(expr, a, N1)
4632            check(expr, a, N2)
4633            # Defines 'right' magic method:
4634            rname = '__r' + name[2:]
4635            B = type('B', (), {rname: specialmethod})
4636            b = B()
4637            check(expr, b, b)
4638            check(expr, a, b)
4639            check(expr, b, a)
4640            check(expr, b, N1)
4641            check(expr, b, N2)
4642            check(expr, N1, b)
4643            check(expr, N2, b)
4644            if iexpr:
4645                check(iexpr, a, a)
4646                check(iexpr, a, N1)
4647                check(iexpr, a, N2)
4648                iname = '__i' + name[2:]
4649                C = type('C', (), {iname: specialmethod})
4650                c = C()
4651                check(iexpr, c, a)
4652                check(iexpr, c, N1)
4653                check(iexpr, c, N2)
4654
4655    def test_assign_slice(self):
4656        # ceval.c's assign_slice used to check for
4657        # tp->tp_as_sequence->sq_slice instead of
4658        # tp->tp_as_sequence->sq_ass_slice
4659
4660        class C(object):
4661            def __setitem__(self, idx, value):
4662                self.value = value
4663
4664        c = C()
4665        c[1:2] = 3
4666        self.assertEqual(c.value, 3)
4667
4668    def test_set_and_no_get(self):
4669        # See
4670        # http://mail.python.org/pipermail/python-dev/2010-January/095637.html
4671        class Descr(object):
4672
4673            def __init__(self, name):
4674                self.name = name
4675
4676            def __set__(self, obj, value):
4677                obj.__dict__[self.name] = value
4678        descr = Descr("a")
4679
4680        class X(object):
4681            a = descr
4682
4683        x = X()
4684        self.assertIs(x.a, descr)
4685        x.a = 42
4686        self.assertEqual(x.a, 42)
4687
4688        # Also check type_getattro for correctness.
4689        class Meta(type):
4690            pass
4691        class X(metaclass=Meta):
4692            pass
4693        X.a = 42
4694        Meta.a = Descr("a")
4695        self.assertEqual(X.a, 42)
4696
4697    def test_getattr_hooks(self):
4698        # issue 4230
4699
4700        class Descriptor(object):
4701            counter = 0
4702            def __get__(self, obj, objtype=None):
4703                def getter(name):
4704                    self.counter += 1
4705                    raise AttributeError(name)
4706                return getter
4707
4708        descr = Descriptor()
4709        class A(object):
4710            __getattribute__ = descr
4711        class B(object):
4712            __getattr__ = descr
4713        class C(object):
4714            __getattribute__ = descr
4715            __getattr__ = descr
4716
4717        self.assertRaises(AttributeError, getattr, A(), "attr")
4718        self.assertEqual(descr.counter, 1)
4719        self.assertRaises(AttributeError, getattr, B(), "attr")
4720        self.assertEqual(descr.counter, 2)
4721        self.assertRaises(AttributeError, getattr, C(), "attr")
4722        self.assertEqual(descr.counter, 4)
4723
4724        class EvilGetattribute(object):
4725            # This used to segfault
4726            def __getattr__(self, name):
4727                raise AttributeError(name)
4728            def __getattribute__(self, name):
4729                del EvilGetattribute.__getattr__
4730                for i in range(5):
4731                    gc.collect()
4732                raise AttributeError(name)
4733
4734        self.assertRaises(AttributeError, getattr, EvilGetattribute(), "attr")
4735
4736    def test_type___getattribute__(self):
4737        self.assertRaises(TypeError, type.__getattribute__, list, type)
4738
4739    def test_abstractmethods(self):
4740        # type pretends not to have __abstractmethods__.
4741        self.assertRaises(AttributeError, getattr, type, "__abstractmethods__")
4742        class meta(type):
4743            pass
4744        self.assertRaises(AttributeError, getattr, meta, "__abstractmethods__")
4745        class X(object):
4746            pass
4747        with self.assertRaises(AttributeError):
4748            del X.__abstractmethods__
4749
4750    def test_gh55664(self):
4751        # gh-55664: issue a warning when the
4752        # __dict__ of a class contains non-string keys
4753        with self.assertWarnsRegex(RuntimeWarning, 'MyClass'):
4754            MyClass = type('MyClass', (), {1: 2})
4755
4756        class meta(type):
4757            def __new__(mcls, name, bases, ns):
4758                ns[1] = 2
4759                return super().__new__(mcls, name, bases, ns)
4760
4761        with self.assertWarnsRegex(RuntimeWarning, 'MyClass'):
4762            MyClass = meta('MyClass', (), {})
4763
4764    def test_proxy_call(self):
4765        class FakeStr:
4766            __class__ = str
4767
4768        fake_str = FakeStr()
4769        # isinstance() reads __class__
4770        self.assertIsInstance(fake_str, str)
4771
4772        # call a method descriptor
4773        with self.assertRaises(TypeError):
4774            str.split(fake_str)
4775
4776        # call a slot wrapper descriptor
4777        with self.assertRaises(TypeError):
4778            str.__add__(fake_str, "abc")
4779
4780    def test_specialized_method_calls_check_types(self):
4781        # https://github.com/python/cpython/issues/92063
4782        class Thing:
4783            pass
4784        thing = Thing()
4785        for i in range(20):
4786            with self.assertRaises(TypeError):
4787                # CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS
4788                list.sort(thing)
4789        for i in range(20):
4790            with self.assertRaises(TypeError):
4791                # CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS
4792                str.split(thing)
4793        for i in range(20):
4794            with self.assertRaises(TypeError):
4795                # CALL_METHOD_DESCRIPTOR_NOARGS
4796                str.upper(thing)
4797        for i in range(20):
4798            with self.assertRaises(TypeError):
4799                # CALL_METHOD_DESCRIPTOR_FAST
4800                str.strip(thing)
4801        from collections import deque
4802        for i in range(20):
4803            with self.assertRaises(TypeError):
4804                # CALL_METHOD_DESCRIPTOR_O
4805                deque.append(thing, thing)
4806
4807    def test_repr_as_str(self):
4808        # Issue #11603: crash or infinite loop when rebinding __str__ as
4809        # __repr__.
4810        class Foo:
4811            pass
4812        Foo.__repr__ = Foo.__str__
4813        foo = Foo()
4814        self.assertRaises(RecursionError, str, foo)
4815        self.assertRaises(RecursionError, repr, foo)
4816
4817    def test_mixing_slot_wrappers(self):
4818        class X(dict):
4819            __setattr__ = dict.__setitem__
4820            __neg__ = dict.copy
4821        x = X()
4822        x.y = 42
4823        self.assertEqual(x["y"], 42)
4824        self.assertEqual(x, -x)
4825
4826    def test_wrong_class_slot_wrapper(self):
4827        # Check bpo-37619: a wrapper descriptor taken from the wrong class
4828        # should raise an exception instead of silently being ignored
4829        class A(int):
4830            __eq__ = str.__eq__
4831            __add__ = str.__add__
4832        a = A()
4833        with self.assertRaises(TypeError):
4834            a == a
4835        with self.assertRaises(TypeError):
4836            a + a
4837
4838    def test_slot_shadows_class_variable(self):
4839        with self.assertRaises(ValueError) as cm:
4840            class X:
4841                __slots__ = ["foo"]
4842                foo = None
4843        m = str(cm.exception)
4844        self.assertEqual("'foo' in __slots__ conflicts with class variable", m)
4845
4846    def test_set_doc(self):
4847        class X:
4848            "elephant"
4849        X.__doc__ = "banana"
4850        self.assertEqual(X.__doc__, "banana")
4851
4852        with self.assertRaises(TypeError) as cm:
4853            type(list).__dict__["__doc__"].__set__(list, "blah")
4854        self.assertIn("cannot set '__doc__' attribute of immutable type 'list'", str(cm.exception))
4855
4856        with self.assertRaises(TypeError) as cm:
4857            type(X).__dict__["__doc__"].__delete__(X)
4858        self.assertIn("cannot delete '__doc__' attribute of immutable type 'X'", str(cm.exception))
4859        self.assertEqual(X.__doc__, "banana")
4860
4861    def test_qualname(self):
4862        descriptors = [str.lower, complex.real, float.real, int.__add__]
4863        types = ['method', 'member', 'getset', 'wrapper']
4864
4865        # make sure we have an example of each type of descriptor
4866        for d, n in zip(descriptors, types):
4867            self.assertEqual(type(d).__name__, n + '_descriptor')
4868
4869        for d in descriptors:
4870            qualname = d.__objclass__.__qualname__ + '.' + d.__name__
4871            self.assertEqual(d.__qualname__, qualname)
4872
4873        self.assertEqual(str.lower.__qualname__, 'str.lower')
4874        self.assertEqual(complex.real.__qualname__, 'complex.real')
4875        self.assertEqual(float.real.__qualname__, 'float.real')
4876        self.assertEqual(int.__add__.__qualname__, 'int.__add__')
4877
4878        class X:
4879            pass
4880        with self.assertRaises(TypeError):
4881            del X.__qualname__
4882
4883        self.assertRaises(TypeError, type.__dict__['__qualname__'].__set__,
4884                          str, 'Oink')
4885
4886        global Y
4887        class Y:
4888            class Inside:
4889                pass
4890        self.assertEqual(Y.__qualname__, 'Y')
4891        self.assertEqual(Y.Inside.__qualname__, 'Y.Inside')
4892
4893    def test_qualname_dict(self):
4894        ns = {'__qualname__': 'some.name'}
4895        tp = type('Foo', (), ns)
4896        self.assertEqual(tp.__qualname__, 'some.name')
4897        self.assertNotIn('__qualname__', tp.__dict__)
4898        self.assertEqual(ns, {'__qualname__': 'some.name'})
4899
4900        ns = {'__qualname__': 1}
4901        self.assertRaises(TypeError, type, 'Foo', (), ns)
4902
4903    def test_cycle_through_dict(self):
4904        # See bug #1469629
4905        class X(dict):
4906            def __init__(self):
4907                dict.__init__(self)
4908                self.__dict__ = self
4909        x = X()
4910        x.attr = 42
4911        wr = weakref.ref(x)
4912        del x
4913        support.gc_collect()
4914        self.assertIsNone(wr())
4915        for o in gc.get_objects():
4916            self.assertIsNot(type(o), X)
4917
4918    def test_object_new_and_init_with_parameters(self):
4919        # See issue #1683368
4920        class OverrideNeither:
4921            pass
4922        self.assertRaises(TypeError, OverrideNeither, 1)
4923        self.assertRaises(TypeError, OverrideNeither, kw=1)
4924        class OverrideNew:
4925            def __new__(cls, foo, kw=0, *args, **kwds):
4926                return object.__new__(cls, *args, **kwds)
4927        class OverrideInit:
4928            def __init__(self, foo, kw=0, *args, **kwargs):
4929                return object.__init__(self, *args, **kwargs)
4930        class OverrideBoth(OverrideNew, OverrideInit):
4931            pass
4932        for case in OverrideNew, OverrideInit, OverrideBoth:
4933            case(1)
4934            case(1, kw=2)
4935            self.assertRaises(TypeError, case, 1, 2, 3)
4936            self.assertRaises(TypeError, case, 1, 2, foo=3)
4937
4938    def test_subclassing_does_not_duplicate_dict_descriptors(self):
4939        class Base:
4940            pass
4941        class Sub(Base):
4942            pass
4943        self.assertIn("__dict__", Base.__dict__)
4944        self.assertNotIn("__dict__", Sub.__dict__)
4945
4946    def test_bound_method_repr(self):
4947        class Foo:
4948            def method(self):
4949                pass
4950        self.assertRegex(repr(Foo().method),
4951            r"<bound method .*Foo\.method of <.*Foo object at .*>>")
4952
4953
4954        class Base:
4955            def method(self):
4956                pass
4957        class Derived1(Base):
4958            pass
4959        class Derived2(Base):
4960            def method(self):
4961                pass
4962        base = Base()
4963        derived1 = Derived1()
4964        derived2 = Derived2()
4965        super_d2 = super(Derived2, derived2)
4966        self.assertRegex(repr(base.method),
4967            r"<bound method .*Base\.method of <.*Base object at .*>>")
4968        self.assertRegex(repr(derived1.method),
4969            r"<bound method .*Base\.method of <.*Derived1 object at .*>>")
4970        self.assertRegex(repr(derived2.method),
4971            r"<bound method .*Derived2\.method of <.*Derived2 object at .*>>")
4972        self.assertRegex(repr(super_d2.method),
4973            r"<bound method .*Base\.method of <.*Derived2 object at .*>>")
4974
4975        class Foo:
4976            @classmethod
4977            def method(cls):
4978                pass
4979        foo = Foo()
4980        self.assertRegex(repr(foo.method), # access via instance
4981            r"<bound method .*Foo\.method of <class '.*Foo'>>")
4982        self.assertRegex(repr(Foo.method), # access via the class
4983            r"<bound method .*Foo\.method of <class '.*Foo'>>")
4984
4985
4986        class MyCallable:
4987            def __call__(self, arg):
4988                pass
4989        func = MyCallable() # func has no __name__ or __qualname__ attributes
4990        instance = object()
4991        method = types.MethodType(func, instance)
4992        self.assertRegex(repr(method),
4993            r"<bound method \? of <object object at .*>>")
4994        func.__name__ = "name"
4995        self.assertRegex(repr(method),
4996            r"<bound method name of <object object at .*>>")
4997        func.__qualname__ = "qualname"
4998        self.assertRegex(repr(method),
4999            r"<bound method qualname of <object object at .*>>")
5000
5001    @unittest.skipIf(_testcapi is None, 'need the _testcapi module')
5002    def test_bpo25750(self):
5003        # bpo-25750: calling a descriptor (implemented as built-in
5004        # function with METH_FASTCALL) should not crash CPython if the
5005        # descriptor deletes itself from the class.
5006        class Descr:
5007            __get__ = _testcapi.bad_get
5008
5009        class X:
5010            descr = Descr()
5011            def __new__(cls):
5012                cls.descr = None
5013                # Create this large list to corrupt some unused memory
5014                cls.lst = [2**i for i in range(10000)]
5015        X.descr
5016
5017    @support.suppress_immortalization()
5018    def test_remove_subclass(self):
5019        # bpo-46417: when the last subclass of a type is deleted,
5020        # remove_subclass() clears the internal dictionary of subclasses:
5021        # set PyTypeObject.tp_subclasses to NULL. remove_subclass() is called
5022        # when a type is deallocated.
5023        class Parent:
5024            pass
5025        self.assertEqual(Parent.__subclasses__(), [])
5026
5027        class Child(Parent):
5028            pass
5029        self.assertEqual(Parent.__subclasses__(), [Child])
5030
5031        del Child
5032        gc.collect()
5033        self.assertEqual(Parent.__subclasses__(), [])
5034
5035    def test_instance_method_get_behavior(self):
5036        # test case for gh-113157
5037
5038        class A:
5039            def meth(self):
5040                return self
5041
5042        class B:
5043            pass
5044
5045        a = A()
5046        b = B()
5047        b.meth = a.meth.__get__(b, B)
5048        self.assertEqual(b.meth(), a)
5049
5050    def test_attr_raise_through_property(self):
5051        # test case for gh-103272
5052        class A:
5053            def __getattr__(self, name):
5054                raise ValueError("FOO")
5055
5056            @property
5057            def foo(self):
5058                return self.__getattr__("asdf")
5059
5060        with self.assertRaisesRegex(ValueError, "FOO"):
5061            A().foo
5062
5063        # test case for gh-103551
5064        class B:
5065            @property
5066            def __getattr__(self, name):
5067                raise ValueError("FOO")
5068
5069            @property
5070            def foo(self):
5071                raise NotImplementedError("BAR")
5072
5073        with self.assertRaisesRegex(NotImplementedError, "BAR"):
5074            B().foo
5075
5076
5077class DictProxyTests(unittest.TestCase):
5078    def setUp(self):
5079        class C(object):
5080            def meth(self):
5081                pass
5082        self.C = C
5083
5084    @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
5085                        'trace function introduces __local__')
5086    def test_iter_keys(self):
5087        # Testing dict-proxy keys...
5088        it = self.C.__dict__.keys()
5089        self.assertNotIsInstance(it, list)
5090        keys = list(it)
5091        keys.sort()
5092        self.assertEqual(keys, ['__dict__', '__doc__', '__firstlineno__',
5093                                '__module__',
5094                                '__static_attributes__', '__weakref__',
5095                                'meth'])
5096
5097    @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
5098                        'trace function introduces __local__')
5099    def test_iter_values(self):
5100        # Testing dict-proxy values...
5101        it = self.C.__dict__.values()
5102        self.assertNotIsInstance(it, list)
5103        values = list(it)
5104        self.assertEqual(len(values), 7)
5105
5106    @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
5107                        'trace function introduces __local__')
5108    def test_iter_items(self):
5109        # Testing dict-proxy iteritems...
5110        it = self.C.__dict__.items()
5111        self.assertNotIsInstance(it, list)
5112        keys = [item[0] for item in it]
5113        keys.sort()
5114        self.assertEqual(keys, ['__dict__', '__doc__', '__firstlineno__',
5115                                '__module__',
5116                                '__static_attributes__', '__weakref__',
5117                                'meth'])
5118
5119    def test_dict_type_with_metaclass(self):
5120        # Testing type of __dict__ when metaclass set...
5121        class B(object):
5122            pass
5123        class M(type):
5124            pass
5125        class C(metaclass=M):
5126            # In 2.3a1, C.__dict__ was a real dict rather than a dict proxy
5127            pass
5128        self.assertEqual(type(C.__dict__), type(B.__dict__))
5129
5130    def test_repr(self):
5131        # Testing mappingproxy.__repr__.
5132        # We can't blindly compare with the repr of another dict as ordering
5133        # of keys and values is arbitrary and may differ.
5134        r = repr(self.C.__dict__)
5135        self.assertTrue(r.startswith('mappingproxy('), r)
5136        self.assertTrue(r.endswith(')'), r)
5137        for k, v in self.C.__dict__.items():
5138            self.assertIn('{!r}: {!r}'.format(k, v), r)
5139
5140
5141class AAAPTypesLongInitTest(unittest.TestCase):
5142    # This is in its own TestCase so that it can be run before any other tests.
5143    # (Hence the 'AAA' in the test class name: to make it the first
5144    # item in a list sorted by name, like
5145    # unittest.TestLoader.getTestCaseNames() does.)
5146    def test_pytype_long_ready(self):
5147        # Testing SF bug 551412 ...
5148
5149        # This dumps core when SF bug 551412 isn't fixed --
5150        # but only when test_descr.py is run separately.
5151        # (That can't be helped -- as soon as PyType_Ready()
5152        # is called for PyLong_Type, the bug is gone.)
5153        class UserLong(object):
5154            def __pow__(self, *args):
5155                pass
5156        try:
5157            pow(0, UserLong(), 0)
5158        except:
5159            pass
5160
5161        # Another segfault only when run early
5162        # (before PyType_Ready(tuple) is called)
5163        type.mro(tuple)
5164
5165
5166class MiscTests(unittest.TestCase):
5167    def test_type_lookup_mro_reference(self):
5168        # Issue #14199: _PyType_Lookup() has to keep a strong reference to
5169        # the type MRO because it may be modified during the lookup, if
5170        # __bases__ is set during the lookup for example.
5171        class MyKey(object):
5172            def __hash__(self):
5173                return hash('mykey')
5174
5175            def __eq__(self, other):
5176                X.__bases__ = (Base2,)
5177
5178        class Base(object):
5179            mykey = 'from Base'
5180            mykey2 = 'from Base'
5181
5182        class Base2(object):
5183            mykey = 'from Base2'
5184            mykey2 = 'from Base2'
5185
5186        with self.assertWarnsRegex(RuntimeWarning, 'X'):
5187            X = type('X', (Base,), {MyKey(): 5})
5188        # mykey is read from Base
5189        self.assertEqual(X.mykey, 'from Base')
5190        # mykey2 is read from Base2 because MyKey.__eq__ has set __bases__
5191        self.assertEqual(X.mykey2, 'from Base2')
5192
5193
5194class PicklingTests(unittest.TestCase):
5195
5196    def _check_reduce(self, proto, obj, args=(), kwargs={}, state=None,
5197                      listitems=None, dictitems=None):
5198        if proto >= 2:
5199            reduce_value = obj.__reduce_ex__(proto)
5200            if kwargs:
5201                self.assertEqual(reduce_value[0], copyreg.__newobj_ex__)
5202                self.assertEqual(reduce_value[1], (type(obj), args, kwargs))
5203            else:
5204                self.assertEqual(reduce_value[0], copyreg.__newobj__)
5205                self.assertEqual(reduce_value[1], (type(obj),) + args)
5206            self.assertEqual(reduce_value[2], state)
5207            if listitems is not None:
5208                self.assertListEqual(list(reduce_value[3]), listitems)
5209            else:
5210                self.assertIsNone(reduce_value[3])
5211            if dictitems is not None:
5212                self.assertDictEqual(dict(reduce_value[4]), dictitems)
5213            else:
5214                self.assertIsNone(reduce_value[4])
5215        else:
5216            base_type = type(obj).__base__
5217            reduce_value = (copyreg._reconstructor,
5218                            (type(obj),
5219                             base_type,
5220                             None if base_type is object else base_type(obj)))
5221            if state is not None:
5222                reduce_value += (state,)
5223            self.assertEqual(obj.__reduce_ex__(proto), reduce_value)
5224            self.assertEqual(obj.__reduce__(), reduce_value)
5225
5226    def test_reduce(self):
5227        protocols = range(pickle.HIGHEST_PROTOCOL + 1)
5228        args = (-101, "spam")
5229        kwargs = {'bacon': -201, 'fish': -301}
5230        state = {'cheese': -401}
5231
5232        class C1:
5233            def __getnewargs__(self):
5234                return args
5235        obj = C1()
5236        for proto in protocols:
5237            self._check_reduce(proto, obj, args)
5238
5239        for name, value in state.items():
5240            setattr(obj, name, value)
5241        for proto in protocols:
5242            self._check_reduce(proto, obj, args, state=state)
5243
5244        class C2:
5245            def __getnewargs__(self):
5246                return "bad args"
5247        obj = C2()
5248        for proto in protocols:
5249            if proto >= 2:
5250                with self.assertRaises(TypeError):
5251                    obj.__reduce_ex__(proto)
5252
5253        class C3:
5254            def __getnewargs_ex__(self):
5255                return (args, kwargs)
5256        obj = C3()
5257        for proto in protocols:
5258            if proto >= 2:
5259                self._check_reduce(proto, obj, args, kwargs)
5260
5261        class C4:
5262            def __getnewargs_ex__(self):
5263                return (args, "bad dict")
5264        class C5:
5265            def __getnewargs_ex__(self):
5266                return ("bad tuple", kwargs)
5267        class C6:
5268            def __getnewargs_ex__(self):
5269                return ()
5270        class C7:
5271            def __getnewargs_ex__(self):
5272                return "bad args"
5273        for proto in protocols:
5274            for cls in C4, C5, C6, C7:
5275                obj = cls()
5276                if proto >= 2:
5277                    with self.assertRaises((TypeError, ValueError)):
5278                        obj.__reduce_ex__(proto)
5279
5280        class C9:
5281            def __getnewargs_ex__(self):
5282                return (args, {})
5283        obj = C9()
5284        for proto in protocols:
5285            self._check_reduce(proto, obj, args)
5286
5287        class C10:
5288            def __getnewargs_ex__(self):
5289                raise IndexError
5290        obj = C10()
5291        for proto in protocols:
5292            if proto >= 2:
5293                with self.assertRaises(IndexError):
5294                    obj.__reduce_ex__(proto)
5295
5296        class C11:
5297            def __getstate__(self):
5298                return state
5299        obj = C11()
5300        for proto in protocols:
5301            self._check_reduce(proto, obj, state=state)
5302
5303        class C12:
5304            def __getstate__(self):
5305                return "not dict"
5306        obj = C12()
5307        for proto in protocols:
5308            self._check_reduce(proto, obj, state="not dict")
5309
5310        class C13:
5311            def __getstate__(self):
5312                raise IndexError
5313        obj = C13()
5314        for proto in protocols:
5315            with self.assertRaises(IndexError):
5316                obj.__reduce_ex__(proto)
5317            if proto < 2:
5318                with self.assertRaises(IndexError):
5319                    obj.__reduce__()
5320
5321        class C14:
5322            __slots__ = tuple(state)
5323            def __init__(self):
5324                for name, value in state.items():
5325                    setattr(self, name, value)
5326
5327        obj = C14()
5328        for proto in protocols:
5329            if proto >= 2:
5330                self._check_reduce(proto, obj, state=(None, state))
5331            else:
5332                with self.assertRaises(TypeError):
5333                    obj.__reduce_ex__(proto)
5334                with self.assertRaises(TypeError):
5335                    obj.__reduce__()
5336
5337        class C15(dict):
5338            pass
5339        obj = C15({"quebec": -601})
5340        for proto in protocols:
5341            self._check_reduce(proto, obj, dictitems=dict(obj))
5342
5343        class C16(list):
5344            pass
5345        obj = C16(["yukon"])
5346        for proto in protocols:
5347            self._check_reduce(proto, obj, listitems=list(obj))
5348
5349    def test_special_method_lookup(self):
5350        protocols = range(pickle.HIGHEST_PROTOCOL + 1)
5351        class Picky:
5352            def __getstate__(self):
5353                return {}
5354
5355            def __getattr__(self, attr):
5356                if attr in ("__getnewargs__", "__getnewargs_ex__"):
5357                    raise AssertionError(attr)
5358                return None
5359        for protocol in protocols:
5360            state = {} if protocol >= 2 else None
5361            self._check_reduce(protocol, Picky(), state=state)
5362
5363    def _assert_is_copy(self, obj, objcopy, msg=None):
5364        """Utility method to verify if two objects are copies of each others.
5365        """
5366        if msg is None:
5367            msg = "{!r} is not a copy of {!r}".format(obj, objcopy)
5368        if type(obj).__repr__ is object.__repr__:
5369            # We have this limitation for now because we use the object's repr
5370            # to help us verify that the two objects are copies. This allows
5371            # us to delegate the non-generic verification logic to the objects
5372            # themselves.
5373            raise ValueError("object passed to _assert_is_copy must " +
5374                             "override the __repr__ method.")
5375        self.assertIsNot(obj, objcopy, msg=msg)
5376        self.assertIs(type(obj), type(objcopy), msg=msg)
5377        if hasattr(obj, '__dict__'):
5378            self.assertDictEqual(obj.__dict__, objcopy.__dict__, msg=msg)
5379            self.assertIsNot(obj.__dict__, objcopy.__dict__, msg=msg)
5380        if hasattr(obj, '__slots__'):
5381            self.assertListEqual(obj.__slots__, objcopy.__slots__, msg=msg)
5382            for slot in obj.__slots__:
5383                self.assertEqual(
5384                    hasattr(obj, slot), hasattr(objcopy, slot), msg=msg)
5385                self.assertEqual(getattr(obj, slot, None),
5386                                 getattr(objcopy, slot, None), msg=msg)
5387        self.assertEqual(repr(obj), repr(objcopy), msg=msg)
5388
5389    @staticmethod
5390    def _generate_pickle_copiers():
5391        """Utility method to generate the many possible pickle configurations.
5392        """
5393        class PickleCopier:
5394            "This class copies object using pickle."
5395            def __init__(self, proto, dumps, loads):
5396                self.proto = proto
5397                self.dumps = dumps
5398                self.loads = loads
5399            def copy(self, obj):
5400                return self.loads(self.dumps(obj, self.proto))
5401            def __repr__(self):
5402                # We try to be as descriptive as possible here since this is
5403                # the string which we will allow us to tell the pickle
5404                # configuration we are using during debugging.
5405                return ("PickleCopier(proto={}, dumps={}.{}, loads={}.{})"
5406                        .format(self.proto,
5407                                self.dumps.__module__, self.dumps.__qualname__,
5408                                self.loads.__module__, self.loads.__qualname__))
5409        return (PickleCopier(*args) for args in
5410                   itertools.product(range(pickle.HIGHEST_PROTOCOL + 1),
5411                                     {pickle.dumps, pickle._dumps},
5412                                     {pickle.loads, pickle._loads}))
5413
5414    def test_pickle_slots(self):
5415        # Tests pickling of classes with __slots__.
5416
5417        # Pickling of classes with __slots__ but without __getstate__ should
5418        # fail (if using protocol 0 or 1)
5419        global C
5420        class C:
5421            __slots__ = ['a']
5422        with self.assertRaises(TypeError):
5423            pickle.dumps(C(), 0)
5424
5425        global D
5426        class D(C):
5427            pass
5428        with self.assertRaises(TypeError):
5429            pickle.dumps(D(), 0)
5430
5431        class C:
5432            "A class with __getstate__ and __setstate__ implemented."
5433            __slots__ = ['a']
5434            def __getstate__(self):
5435                state = getattr(self, '__dict__', {}).copy()
5436                for cls in type(self).__mro__:
5437                    for slot in cls.__dict__.get('__slots__', ()):
5438                        try:
5439                            state[slot] = getattr(self, slot)
5440                        except AttributeError:
5441                            pass
5442                return state
5443            def __setstate__(self, state):
5444                for k, v in state.items():
5445                    setattr(self, k, v)
5446            def __repr__(self):
5447                return "%s()<%r>" % (type(self).__name__, self.__getstate__())
5448
5449        class D(C):
5450            "A subclass of a class with slots."
5451            pass
5452
5453        global E
5454        class E(C):
5455            "A subclass with an extra slot."
5456            __slots__ = ['b']
5457
5458        # Now it should work
5459        for pickle_copier in self._generate_pickle_copiers():
5460            with self.subTest(pickle_copier=pickle_copier):
5461                x = C()
5462                y = pickle_copier.copy(x)
5463                self._assert_is_copy(x, y)
5464
5465                x.a = 42
5466                y = pickle_copier.copy(x)
5467                self._assert_is_copy(x, y)
5468
5469                x = D()
5470                x.a = 42
5471                x.b = 100
5472                y = pickle_copier.copy(x)
5473                self._assert_is_copy(x, y)
5474
5475                x = E()
5476                x.a = 42
5477                x.b = "foo"
5478                y = pickle_copier.copy(x)
5479                self._assert_is_copy(x, y)
5480
5481    def test_reduce_copying(self):
5482        # Tests pickling and copying new-style classes and objects.
5483        global C1
5484        class C1:
5485            "The state of this class is copyable via its instance dict."
5486            ARGS = (1, 2)
5487            NEED_DICT_COPYING = True
5488            def __init__(self, a, b):
5489                super().__init__()
5490                self.a = a
5491                self.b = b
5492            def __repr__(self):
5493                return "C1(%r, %r)" % (self.a, self.b)
5494
5495        global C2
5496        class C2(list):
5497            "A list subclass copyable via __getnewargs__."
5498            ARGS = (1, 2)
5499            NEED_DICT_COPYING = False
5500            def __new__(cls, a, b):
5501                self = super().__new__(cls)
5502                self.a = a
5503                self.b = b
5504                return self
5505            def __init__(self, *args):
5506                super().__init__()
5507                # This helps testing that __init__ is not called during the
5508                # unpickling process, which would cause extra appends.
5509                self.append("cheese")
5510            @classmethod
5511            def __getnewargs__(cls):
5512                return cls.ARGS
5513            def __repr__(self):
5514                return "C2(%r, %r)<%r>" % (self.a, self.b, list(self))
5515
5516        global C3
5517        class C3(list):
5518            "A list subclass copyable via __getstate__."
5519            ARGS = (1, 2)
5520            NEED_DICT_COPYING = False
5521            def __init__(self, a, b):
5522                self.a = a
5523                self.b = b
5524                # This helps testing that __init__ is not called during the
5525                # unpickling process, which would cause extra appends.
5526                self.append("cheese")
5527            @classmethod
5528            def __getstate__(cls):
5529                return cls.ARGS
5530            def __setstate__(self, state):
5531                a, b = state
5532                self.a = a
5533                self.b = b
5534            def __repr__(self):
5535                return "C3(%r, %r)<%r>" % (self.a, self.b, list(self))
5536
5537        global C4
5538        class C4(int):
5539            "An int subclass copyable via __getnewargs__."
5540            ARGS = ("hello", "world", 1)
5541            NEED_DICT_COPYING = False
5542            def __new__(cls, a, b, value):
5543                self = super().__new__(cls, value)
5544                self.a = a
5545                self.b = b
5546                return self
5547            @classmethod
5548            def __getnewargs__(cls):
5549                return cls.ARGS
5550            def __repr__(self):
5551                return "C4(%r, %r)<%r>" % (self.a, self.b, int(self))
5552
5553        global C5
5554        class C5(int):
5555            "An int subclass copyable via __getnewargs_ex__."
5556            ARGS = (1, 2)
5557            KWARGS = {'value': 3}
5558            NEED_DICT_COPYING = False
5559            def __new__(cls, a, b, *, value=0):
5560                self = super().__new__(cls, value)
5561                self.a = a
5562                self.b = b
5563                return self
5564            @classmethod
5565            def __getnewargs_ex__(cls):
5566                return (cls.ARGS, cls.KWARGS)
5567            def __repr__(self):
5568                return "C5(%r, %r)<%r>" % (self.a, self.b, int(self))
5569
5570        test_classes = (C1, C2, C3, C4, C5)
5571        # Testing copying through pickle
5572        pickle_copiers = self._generate_pickle_copiers()
5573        for cls, pickle_copier in itertools.product(test_classes, pickle_copiers):
5574            with self.subTest(cls=cls, pickle_copier=pickle_copier):
5575                kwargs = getattr(cls, 'KWARGS', {})
5576                obj = cls(*cls.ARGS, **kwargs)
5577                proto = pickle_copier.proto
5578                objcopy = pickle_copier.copy(obj)
5579                self._assert_is_copy(obj, objcopy)
5580                # For test classes that supports this, make sure we didn't go
5581                # around the reduce protocol by simply copying the attribute
5582                # dictionary. We clear attributes using the previous copy to
5583                # not mutate the original argument.
5584                if proto >= 2 and not cls.NEED_DICT_COPYING:
5585                    objcopy.__dict__.clear()
5586                    objcopy2 = pickle_copier.copy(objcopy)
5587                    self._assert_is_copy(obj, objcopy2)
5588
5589        # Testing copying through copy.deepcopy()
5590        for cls in test_classes:
5591            with self.subTest(cls=cls):
5592                kwargs = getattr(cls, 'KWARGS', {})
5593                obj = cls(*cls.ARGS, **kwargs)
5594                objcopy = deepcopy(obj)
5595                self._assert_is_copy(obj, objcopy)
5596                # For test classes that supports this, make sure we didn't go
5597                # around the reduce protocol by simply copying the attribute
5598                # dictionary. We clear attributes using the previous copy to
5599                # not mutate the original argument.
5600                if not cls.NEED_DICT_COPYING:
5601                    objcopy.__dict__.clear()
5602                    objcopy2 = deepcopy(objcopy)
5603                    self._assert_is_copy(obj, objcopy2)
5604
5605    def test_issue24097(self):
5606        # Slot name is freed inside __getattr__ and is later used.
5607        class S(str):  # Not interned
5608            pass
5609        class A:
5610            __slotnames__ = [S('spam')]
5611            def __getattr__(self, attr):
5612                if attr == 'spam':
5613                    A.__slotnames__[:] = [S('spam')]
5614                    return 42
5615                else:
5616                    raise AttributeError
5617
5618        import copyreg
5619        expected = (copyreg.__newobj__, (A,), (None, {'spam': 42}), None, None)
5620        self.assertEqual(A().__reduce_ex__(2), expected)  # Shouldn't crash
5621
5622    def test_object_reduce(self):
5623        # Issue #29914
5624        # __reduce__() takes no arguments
5625        object().__reduce__()
5626        with self.assertRaises(TypeError):
5627            object().__reduce__(0)
5628        # __reduce_ex__() takes one integer argument
5629        object().__reduce_ex__(0)
5630        with self.assertRaises(TypeError):
5631            object().__reduce_ex__()
5632        with self.assertRaises(TypeError):
5633            object().__reduce_ex__(None)
5634
5635
5636class SharedKeyTests(unittest.TestCase):
5637
5638    @support.cpython_only
5639    def test_subclasses(self):
5640        # Verify that subclasses can share keys (per PEP 412)
5641        class A:
5642            pass
5643        class B(A):
5644            pass
5645
5646        #Shrink keys by repeatedly creating instances
5647        [(A(), B()) for _ in range(30)]
5648
5649        a, b = A(), B()
5650        self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
5651        self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({"a":1}))
5652        # Initial hash table can contain only one or two elements.
5653        # Set 6 attributes to cause internal resizing.
5654        a.x, a.y, a.z, a.w, a.v, a.u = range(6)
5655        self.assertNotEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
5656        a2 = A()
5657        self.assertGreater(sys.getsizeof(vars(a)), sys.getsizeof(vars(a2)))
5658        self.assertLess(sys.getsizeof(vars(a2)), sys.getsizeof({"a":1}))
5659        self.assertLess(sys.getsizeof(vars(b)), sys.getsizeof({"a":1}))
5660
5661
5662class DebugHelperMeta(type):
5663    """
5664    Sets default __doc__ and simplifies repr() output.
5665    """
5666    def __new__(mcls, name, bases, attrs):
5667        if attrs.get('__doc__') is None:
5668            attrs['__doc__'] = name  # helps when debugging with gdb
5669        return type.__new__(mcls, name, bases, attrs)
5670    def __repr__(cls):
5671        return repr(cls.__name__)
5672
5673
5674class MroTest(unittest.TestCase):
5675    """
5676    Regressions for some bugs revealed through
5677    mcsl.mro() customization (typeobject.c: mro_internal()) and
5678    cls.__bases__ assignment (typeobject.c: type_set_bases()).
5679    """
5680
5681    def setUp(self):
5682        self.step = 0
5683        self.ready = False
5684
5685    def step_until(self, limit):
5686        ret = (self.step < limit)
5687        if ret:
5688            self.step += 1
5689        return ret
5690
5691    def test_incomplete_set_bases_on_self(self):
5692        """
5693        type_set_bases must be aware that type->tp_mro can be NULL.
5694        """
5695        class M(DebugHelperMeta):
5696            def mro(cls):
5697                if self.step_until(1):
5698                    assert cls.__mro__ is None
5699                    cls.__bases__ += ()
5700
5701                return type.mro(cls)
5702
5703        class A(metaclass=M):
5704            pass
5705
5706    def test_reent_set_bases_on_base(self):
5707        """
5708        Deep reentrancy must not over-decref old_mro.
5709        """
5710        class M(DebugHelperMeta):
5711            def mro(cls):
5712                if cls.__mro__ is not None and cls.__name__ == 'B':
5713                    # 4-5 steps are usually enough to make it crash somewhere
5714                    if self.step_until(10):
5715                        A.__bases__ += ()
5716
5717                return type.mro(cls)
5718
5719        class A(metaclass=M):
5720            pass
5721        class B(A):
5722            pass
5723        B.__bases__ += ()
5724
5725    def test_reent_set_bases_on_direct_base(self):
5726        """
5727        Similar to test_reent_set_bases_on_base, but may crash differently.
5728        """
5729        class M(DebugHelperMeta):
5730            def mro(cls):
5731                base = cls.__bases__[0]
5732                if base is not object:
5733                    if self.step_until(5):
5734                        base.__bases__ += ()
5735
5736                return type.mro(cls)
5737
5738        class A(metaclass=M):
5739            pass
5740        class B(A):
5741            pass
5742        class C(B):
5743            pass
5744
5745    def test_reent_set_bases_tp_base_cycle(self):
5746        """
5747        type_set_bases must check for an inheritance cycle not only through
5748        MRO of the type, which may be not yet updated in case of reentrance,
5749        but also through tp_base chain, which is assigned before diving into
5750        inner calls to mro().
5751
5752        Otherwise, the following snippet can loop forever:
5753            do {
5754                // ...
5755                type = type->tp_base;
5756            } while (type != NULL);
5757
5758        Functions that rely on tp_base (like solid_base and PyType_IsSubtype)
5759        would not be happy in that case, causing a stack overflow.
5760        """
5761        class M(DebugHelperMeta):
5762            def mro(cls):
5763                if self.ready:
5764                    if cls.__name__ == 'B1':
5765                        B2.__bases__ = (B1,)
5766                    if cls.__name__ == 'B2':
5767                        B1.__bases__ = (B2,)
5768                return type.mro(cls)
5769
5770        class A(metaclass=M):
5771            pass
5772        class B1(A):
5773            pass
5774        class B2(A):
5775            pass
5776
5777        self.ready = True
5778        with self.assertRaises(TypeError):
5779            B1.__bases__ += ()
5780
5781    def test_tp_subclasses_cycle_in_update_slots(self):
5782        """
5783        type_set_bases must check for reentrancy upon finishing its job
5784        by updating tp_subclasses of old/new bases of the type.
5785        Otherwise, an implicit inheritance cycle through tp_subclasses
5786        can break functions that recurse on elements of that field
5787        (like recurse_down_subclasses and mro_hierarchy) eventually
5788        leading to a stack overflow.
5789        """
5790        class M(DebugHelperMeta):
5791            def mro(cls):
5792                if self.ready and cls.__name__ == 'C':
5793                    self.ready = False
5794                    C.__bases__ = (B2,)
5795                return type.mro(cls)
5796
5797        class A(metaclass=M):
5798            pass
5799        class B1(A):
5800            pass
5801        class B2(A):
5802            pass
5803        class C(A):
5804            pass
5805
5806        self.ready = True
5807        C.__bases__ = (B1,)
5808        B1.__bases__ = (C,)
5809
5810        self.assertEqual(C.__bases__, (B2,))
5811        self.assertEqual(B2.__subclasses__(), [C])
5812        self.assertEqual(B1.__subclasses__(), [])
5813
5814        self.assertEqual(B1.__bases__, (C,))
5815        self.assertEqual(C.__subclasses__(), [B1])
5816
5817    def test_tp_subclasses_cycle_error_return_path(self):
5818        """
5819        The same as test_tp_subclasses_cycle_in_update_slots, but tests
5820        a code path executed on error (goto bail).
5821        """
5822        class E(Exception):
5823            pass
5824        class M(DebugHelperMeta):
5825            def mro(cls):
5826                if self.ready and cls.__name__ == 'C':
5827                    if C.__bases__ == (B2,):
5828                        self.ready = False
5829                    else:
5830                        C.__bases__ = (B2,)
5831                        raise E
5832                return type.mro(cls)
5833
5834        class A(metaclass=M):
5835            pass
5836        class B1(A):
5837            pass
5838        class B2(A):
5839            pass
5840        class C(A):
5841            pass
5842
5843        self.ready = True
5844        with self.assertRaises(E):
5845            C.__bases__ = (B1,)
5846        B1.__bases__ = (C,)
5847
5848        self.assertEqual(C.__bases__, (B2,))
5849        self.assertEqual(C.__mro__, tuple(type.mro(C)))
5850
5851    def test_incomplete_extend(self):
5852        """
5853        Extending an uninitialized type with type->tp_mro == NULL must
5854        throw a reasonable TypeError exception, instead of failing
5855        with PyErr_BadInternalCall.
5856        """
5857        class M(DebugHelperMeta):
5858            def mro(cls):
5859                if cls.__mro__ is None and cls.__name__ != 'X':
5860                    with self.assertRaises(TypeError):
5861                        class X(cls):
5862                            pass
5863
5864                return type.mro(cls)
5865
5866        class A(metaclass=M):
5867            pass
5868
5869    def test_incomplete_super(self):
5870        """
5871        Attribute lookup on a super object must be aware that
5872        its target type can be uninitialized (type->tp_mro == NULL).
5873        """
5874        class M(DebugHelperMeta):
5875            def mro(cls):
5876                if cls.__mro__ is None:
5877                    with self.assertRaises(AttributeError):
5878                        super(cls, cls).xxx
5879
5880                return type.mro(cls)
5881
5882        class A(metaclass=M):
5883            pass
5884
5885    def test_disappearing_custom_mro(self):
5886        """
5887        gh-92112: A custom mro() returning a result conflicting with
5888        __bases__ and deleting itself caused a double free.
5889        """
5890        class B:
5891            pass
5892
5893        class M(DebugHelperMeta):
5894            def mro(cls):
5895                del M.mro
5896                return (B,)
5897
5898        with self.assertRaises(TypeError):
5899            class A(metaclass=M):
5900                pass
5901
5902
5903if __name__ == "__main__":
5904    unittest.main()
5905