1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport __builtin__
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport gc
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport types
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport weakref
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom copy import deepcopy
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass OperatorsTest(unittest.TestCase):
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, *args, **kwargs):
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unittest.TestCase.__init__(self, *args, **kwargs)
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binops = {
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'add': '+',
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'sub': '-',
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'mul': '*',
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'div': '/',
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'divmod': 'divmod',
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'pow': '**',
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'lshift': '<<',
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'rshift': '>>',
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'and': '&',
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'xor': '^',
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'or': '|',
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'cmp': 'cmp',
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'lt': '<',
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'le': '<=',
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'eq': '==',
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'ne': '!=',
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'gt': '>',
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'ge': '>=',
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        }
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for name, expr in self.binops.items():
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if expr.islower():
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expr = expr + "(a, b)"
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expr = 'a %s b' % expr
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.binops[name] = expr
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.unops = {
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'pos': '+',
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'neg': '-',
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'abs': 'abs',
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'invert': '~',
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'int': 'int',
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'long': 'long',
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'float': 'float',
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'oct': 'oct',
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            'hex': 'hex',
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        }
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for name, expr in self.unops.items():
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if expr.islower():
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expr = expr + "(a)"
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expr = '%s a' % expr
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.unops[name] = expr
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def unop_test(self, a, res, expr="len(a)", meth="__len__"):
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {'a': a}
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(eval(expr, d), res)
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = type(a)
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = getattr(t, meth)
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Find method in parent class
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while meth not in t.__dict__:
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = t.__bases__[0]
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method object; the getattr() below obtains its underlying function.
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m(a), res)
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm = getattr(a, meth)
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(bm(), res)
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def binop_test(self, a, b, res, expr="a+b", meth="__add__"):
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {'a': a, 'b': b}
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX Hack so this passes before 2.3 when -Qnew is specified.
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if meth == "__div__" and 1/2 == 0.5:
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            meth = "__truediv__"
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if meth == '__divmod__': pass
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(eval(expr, d), res)
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = type(a)
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = getattr(t, meth)
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while meth not in t.__dict__:
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = t.__bases__[0]
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method object; the getattr() below obtains its underlying function.
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m(a, b), res)
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm = getattr(a, meth)
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(bm(b), res)
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def ternop_test(self, a, b, c, res, expr="a[b:c]", meth="__getslice__"):
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {'a': a, 'b': b, 'c': c}
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(eval(expr, d), res)
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = type(a)
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = getattr(t, meth)
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while meth not in t.__dict__:
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = t.__bases__[0]
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method object; the getattr() below obtains its underlying function.
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m(a, b, c), res)
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm = getattr(a, meth)
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(bm(b, c), res)
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setop_test(self, a, b, res, stmt="a+=b", meth="__iadd__"):
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {'a': deepcopy(a), 'b': b}
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        exec stmt in d
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d['a'], res)
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = type(a)
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = getattr(t, meth)
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while meth not in t.__dict__:
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = t.__bases__[0]
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method object; the getattr() below obtains its underlying function.
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d['a'] = deepcopy(a)
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m(d['a'], b)
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d['a'], res)
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d['a'] = deepcopy(a)
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm = getattr(d['a'], meth)
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm(b)
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d['a'], res)
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def set2op_test(self, a, b, c, res, stmt="a[b]=c", meth="__setitem__"):
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {'a': deepcopy(a), 'b': b, 'c': c}
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        exec stmt in d
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d['a'], res)
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = type(a)
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = getattr(t, meth)
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while meth not in t.__dict__:
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = t.__bases__[0]
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method object; the getattr() below obtains its underlying function.
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d['a'] = deepcopy(a)
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m(d['a'], b, c)
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d['a'], res)
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d['a'] = deepcopy(a)
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm = getattr(d['a'], meth)
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm(b, c)
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d['a'], res)
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def set3op_test(self, a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"):
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dictionary = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d}
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        exec stmt in dictionary
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dictionary['a'], res)
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = type(a)
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while meth not in t.__dict__:
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = t.__bases__[0]
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = getattr(t, meth)
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # in some implementations (e.g. PyPy), 'm' can be a regular unbound
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method object; the getattr() below obtains its underlying function.
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dictionary['a'] = deepcopy(a)
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m(dictionary['a'], b, c, d)
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dictionary['a'], res)
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dictionary['a'] = deepcopy(a)
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm = getattr(dictionary['a'], meth)
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bm(b, c, d)
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dictionary['a'], res)
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_lists(self):
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing list operations...
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Asserts are within individual test methods
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test([1], [2], [1,2], "a+b", "__add__")
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test([1,2,3], 2, 1, "b in a", "__contains__")
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test([1,2,3], 4, 0, "b in a", "__contains__")
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test([1,2,3], 1, 2, "a[b]", "__getitem__")
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.ternop_test([1,2,3], 0, 2, [1,2], "a[b:c]", "__getslice__")
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.setop_test([1], [2], [1,2], "a+=b", "__iadd__")
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.setop_test([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__")
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.unop_test([1,2,3], 3, "len(a)", "__len__")
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__")
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__")
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.set2op_test([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__")
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.set3op_test([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d",
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "__setslice__")
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dicts(self):
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing dict operations...
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if hasattr(dict, '__cmp__'):   # PyPy has only rich comparison on dicts
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.binop_test({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__")
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.binop_test({1:2}, {2:1}, True, "a < b", "__lt__")
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test({1:2,3:4}, 1, 1, "b in a", "__contains__")
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test({1:2,3:4}, 2, 0, "b in a", "__contains__")
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test({1:2,3:4}, 1, 2, "a[b]", "__getitem__")
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {1:2, 3:4}
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l1 = []
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in d.keys():
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            l1.append(i)
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in iter(d):
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            l.append(i)
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l, l1)
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in d.__iter__():
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            l.append(i)
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l, l1)
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in dict.__iter__(d):
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            l.append(i)
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l, l1)
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {1:2, 3:4}
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.unop_test(d, 2, "len(a)", "__len__")
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(eval(repr(d), {}), d)
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(eval(d.__repr__(), {}), d)
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.set2op_test({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c",
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "__setitem__")
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Tests for unary and binary operators
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def number_operators(self, a, b, skip=[]):
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dict = {'a': a, 'b': b}
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for name, expr in self.binops.items():
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if name not in skip:
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                name = "__%s__" % name
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if hasattr(a, name):
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    res = eval(expr, dict)
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.binop_test(a, b, res, expr, name)
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for name, expr in self.unops.items():
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if name not in skip:
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                name = "__%s__" % name
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if hasattr(a, name):
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    res = eval(expr, dict)
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.unop_test(a, res, expr, name)
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_ints(self):
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing int operations...
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.number_operators(100, 3)
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # The following crashes in Python 2.2
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((1).__nonzero__(), 1)
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((0).__nonzero__(), 0)
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # This returns 'NotImplemented' in Python 2.2
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(int):
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __add__(self, other):
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return NotImplemented
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C(5L), 5)
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            C() + ""
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("NotImplemented should have caused TypeError")
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            C(sys.maxint+1)
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except OverflowError:
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("should have raised OverflowError")
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_longs(self):
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing long operations...
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.number_operators(100L, 3L)
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_floats(self):
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing float operations...
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.number_operators(100.0, 3.0)
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_complexes(self):
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing complex operations...
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge',
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                                  'int', 'long', 'float'])
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Number(complex):
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['prec']
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(cls, *args, **kwds):
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                result = complex.__new__(cls, *args)
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                result.prec = kwds.get('prec', 12)
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return result
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                prec = self.prec
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if self.imag == 0.0:
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return "%.*g" % (prec, self.real)
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if self.real == 0.0:
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return "%.*gj" % (prec, self.imag)
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag)
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __str__ = __repr__
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = Number(3.14, prec=6)
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(a), "3.14")
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.prec, 6)
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = Number(a, prec=2)
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(a), "3.1")
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.prec, 2)
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = Number(234.5)
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(a), "234.5")
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.prec, 12)
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @test_support.impl_detail("the module 'xxsubtype' is internal")
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_spam_lists(self):
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing spamlist operations...
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import copy, xxsubtype as spam
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def spamlist(l, memo=None):
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import xxsubtype as spam
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return spam.spamlist(l)
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # This is an ugly hack:
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        copy._deepcopy_dispatch[spam.spamlist] = spamlist
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b",
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       "__add__")
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamlist([1,2,3]), 2, 1, "b in a", "__contains__")
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamlist([1,2,3]), 4, 0, "b in a", "__contains__")
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__")
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.ternop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]",
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "__getslice__")
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.setop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b",
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       "__iadd__")
324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.setop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b",
325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       "__imul__")
326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.unop_test(spamlist([1,2,3]), 3, "len(a)", "__len__")
327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b",
328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       "__mul__")
329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a",
330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       "__rmul__")
331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.set2op_test(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c",
332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "__setitem__")
333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.set3op_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]),
334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                   spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__")
335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test subclassing
336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(spam.spamlist):
337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self): return 1
338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, [])
340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.foo(), 1)
341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.append(100)
342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, [100])
343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 0)
344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.setstate(42)
345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 42)
346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @test_support.impl_detail("the module 'xxsubtype' is internal")
348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_spam_dicts(self):
349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing spamdict operations...
350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import copy, xxsubtype as spam
351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def spamdict(d, memo=None):
352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import xxsubtype as spam
353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sd = spam.spamdict()
354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for k, v in d.items():
355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                sd[k] = v
356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return sd
357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # This is an ugly hack:
358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        copy._deepcopy_dispatch[spam.spamdict] = spamdict
359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)",
361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       "__cmp__")
362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.binop_test(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__")
365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = spamdict({1:2,3:4})
366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l1 = []
367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in d.keys():
368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            l1.append(i)
369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in iter(d):
371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            l.append(i)
372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l, l1)
373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in d.__iter__():
375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            l.append(i)
376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l, l1)
377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in type(spamdict({})).__iter__(d):
379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            l.append(i)
380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l, l1)
381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        straightd = {1:2, 3:4}
382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        spamd = spamdict(straightd)
383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.unop_test(spamd, 2, "len(a)", "__len__")
384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.unop_test(spamd, repr(straightd), "repr(a)", "__repr__")
385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.set2op_test(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}),
386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                   "a[b]=c", "__setitem__")
387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test subclassing
388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(spam.spamdict):
389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self): return 1
390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.items(), [])
392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.foo(), 1)
393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a['foo'] = 'bar'
394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.items(), [('foo', 'bar')])
395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 0)
396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.setstate(100)
397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 100)
398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ClassPropertiesAndMethods(unittest.TestCase):
400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_python_dicts(self):
402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing Python subclass of dict...
403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(dict, dict))
404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance({}, dict)
405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = dict()
406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, {})
407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(d.__class__ is dict)
408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(d, dict)
409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(dict):
410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            state = -1
411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self_local, *a, **kw):
412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if a:
413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertEqual(len(a), 1)
414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self_local.state = a[0]
415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if kw:
416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for k, v in kw.items():
417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        self_local[v] = k
418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, key):
419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.get(key, 0)
420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setitem__(self_local, key, value):
421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertIsInstance(key, type(0))
422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                dict.__setitem__(self_local, key, value)
423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def setstate(self, state):
424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.state = state
425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getstate(self):
426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.state
427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(issubclass(C, dict))
428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a1 = C(12)
429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a1.state, 12)
430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a2 = C(foo=1, bar=2)
431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a2[1] == 'foo' and a2[2], 'bar')
432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.state, -1)
434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), -1)
435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.setstate(0)
436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.state, 0)
437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 0)
438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.setstate(10)
439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.state, 10)
440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 10)
441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[42], 0)
442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a[42] = 24
443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[42], 24)
444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        N = 50
445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in range(N):
446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            a[i] = C()
447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for j in range(N):
448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                a[i][j] = i*j
449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in range(N):
450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for j in range(N):
451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(a[i][j], i*j)
452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_python_lists(self):
454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing Python subclass of list...
455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(list):
456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, i):
457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return list.__getitem__(self, i) + 100
458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getslice__(self, i, j):
459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return (i, j)
460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.extend([0,1,2])
462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[0], 100)
463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[1], 101)
464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[2], 102)
465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[100:200], (100,200))
466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_metaclass(self):
468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __metaclass__...
469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = type
471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__state = 0
473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getstate(self):
474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.__state
475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def setstate(self, state):
476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__state = state
477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 0)
479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.setstate(10)
480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 10)
481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D:
482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class __metaclass__(type):
483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def myself(cls): return cls
484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.myself(), D)
485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.__class__, D)
487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M1(type):
488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(cls, name, bases, dict):
489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                dict['__spam__'] = 1
490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return type.__new__(cls, name, bases, dict)
491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = M1
493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.__spam__, 1)
494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.__spam__, 1)
496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class _instance(object):
498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M2(object):
500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @staticmethod
501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(cls, name, bases, dict):
502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self = object.__new__(cls)
503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.name = name
504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.bases = bases
505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.dict = dict
506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self
507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __call__(self):
508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                it = _instance()
509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # Early binding of methods
510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for key in self.dict:
511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if key.startswith("__"):
512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        continue
513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    setattr(it, key, self.dict[key].__get__(it, self))
514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return it
515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = M2
517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def spam(self):
518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 42
519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.name, 'C')
520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.bases, ())
521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('spam', C.dict)
522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.spam(), 42)
524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # More metaclass examples
526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class autosuper(type):
528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Automatically add __super to the class
529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # This trick only works for dynamic classes
530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(metaclass, name, bases, dict):
531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                cls = super(autosuper, metaclass).__new__(metaclass,
532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                                          name, bases, dict)
533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # Name mangling for __super removes leading underscores
534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                while name[:1] == "_":
535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    name = name[1:]
536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if name:
537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    name = "_%s__super" % name
538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    name = "__super"
540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                setattr(cls, name, super(cls))
541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return cls
542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A:
543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = autosuper
544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self):
545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "A"
546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A):
547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self):
548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "B" + self.__super.meth()
549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A):
550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self):
551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C" + self.__super.meth()
552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C, B):
553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self):
554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "D" + self.__super.meth()
555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D().meth(), "DCBA")
556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(B, C):
557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self):
558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "E" + self.__super.meth()
559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E().meth(), "EBCA")
560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class autoproperty(type):
562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Automatically create property attributes when methods
563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # named _get_x and/or _set_x are found
564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(metaclass, name, bases, dict):
565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                hits = {}
566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for key, val in dict.iteritems():
567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if key.startswith("_get_"):
568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        key = key[5:]
569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        get, set = hits.get(key, (None, None))
570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        get = val
571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        hits[key] = get, set
572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    elif key.startswith("_set_"):
573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        key = key[5:]
574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        get, set = hits.get(key, (None, None))
575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        set = val
576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        hits[key] = get, set
577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for key, (get, set) in hits.iteritems():
578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    dict[key] = property(get, set)
579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return super(autoproperty, metaclass).__new__(metaclass,
580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                                            name, bases, dict)
581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A:
582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = autoproperty
583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def _get_x(self):
584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return -self.__x
585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def _set_x(self, x):
586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__x = -x
587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = A()
588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not hasattr(a, "x"))
589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.x = 12
590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.x, 12)
591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a._A__x, -12)
592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class multimetaclass(autoproperty, autosuper):
594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Merge of multiple cooperating metaclasses
595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A:
597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = multimetaclass
598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def _get_x(self):
599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "A"
600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A):
601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def _get_x(self):
602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "B" + self.__super._get_x()
603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A):
604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def _get_x(self):
605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C" + self.__super._get_x()
606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C, B):
607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def _get_x(self):
608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "D" + self.__super._get_x()
609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D().x, "DCBA")
610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Make sure type(x) doesn't call x.__class__.__init__
612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class T(type):
613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            counter = 0
614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, *args):
615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                T.counter += 1
616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = T
618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(T.counter, 1)
619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(a), C)
621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(T.counter, 1)
622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object): pass
624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: c()
626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError: pass
627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("calling object w/o call method should raise "
628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        "TypeError")
629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing code to find most derived baseclass
631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(type):
632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(*args, **kwargs):
633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return type.__new__(*args, **kwargs)
634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object):
636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = A
640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # The most derived metaclass of D is A rather than type.
642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(B, C):
643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_module_subclasses(self):
646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing Python subclass of module...
647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        log = []
648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        MT = type(sys)
649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MM(MT):
650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, name):
651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                MT.__init__(self, name)
652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattribute__(self, name):
653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                log.append(("getattr", name))
654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return MT.__getattribute__(self, name)
655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setattr__(self, name, value):
656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                log.append(("setattr", name, value))
657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                MT.__setattr__(self, name, value)
658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __delattr__(self, name):
659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                log.append(("delattr", name))
660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                MT.__delattr__(self, name)
661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = MM("a")
662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.foo = 12
663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = a.foo
664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del a.foo
665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(log, [("setattr", "foo", 12),
666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                               ("getattr", "foo"),
667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                               ("delattr", "foo")])
668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # http://python.org/sf/1174712
670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class Module(types.ModuleType, str):
672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("inheriting from ModuleType and str at the same time "
677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                      "should fail")
678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_multiple_inheritence(self):
680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing multiple inheritance...
681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__state = 0
684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getstate(self):
685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.__state
686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def setstate(self, state):
687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__state = state
688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 0)
690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.setstate(10)
691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getstate(), 10)
692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(dict, C):
693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                type({}).__init__(self)
695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                C.__init__(self)
696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.keys(), [])
698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d["hello"] = "world"
699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.items(), [("hello", "world")])
700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d["hello"], "world")
701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.getstate(), 0)
702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d.setstate(10)
703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.getstate(), 10)
704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.__mro__, (D, dict, C, object))
705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF bug #442833
707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Node(object):
708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __int__(self):
709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return int(self.foo())
710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self):
711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "23"
712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Frag(Node, list):
713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self):
714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "42"
715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Node().__int__(), 23)
716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(int(Node()), 23)
717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Frag().__int__(), 42)
718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(int(Frag()), 42)
719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # MI mixing classic and new-style classes.
721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A:
723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = 1
724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A):
726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A):
729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = 2
730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(B, C):
732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.x, 1)
734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Classic MRO is preserved for a classic base class.
736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(D, object):
737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E.__mro__, (E, D, B, A, C, object))
739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E.x, 1)
740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # But with a mix of classic bases, their MROs are combined using
742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # new-style MRO.
743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class F(B, C, object):
744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(F.__mro__, (F, B, C, A, object))
746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(F.x, 2)
747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Try something else.
749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def cmethod(self):
751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C a"
752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def all_method(self):
753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C b"
754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M1(C, object):
756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def m1method(self):
757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "M1 a"
758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def all_method(self):
759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "M1 b"
760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(M1.__mro__, (M1, C, object))
762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = M1()
763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.cmethod(), "C a")
764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.m1method(), "M1 a")
765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.all_method(), "M1 b")
766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def dmethod(self):
769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "D a"
770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def all_method(self):
771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "D b"
772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M2(D, object):
774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def m2method(self):
775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "M2 a"
776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def all_method(self):
777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "M2 b"
778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(M2.__mro__, (M2, D, C, object))
780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = M2()
781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.cmethod(), "C a")
782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.dmethod(), "D a")
783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.m2method(), "M2 a")
784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.all_method(), "M2 b")
785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M3(M1, M2, object):
787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def m3method(self):
788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "M3 a"
789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def all_method(self):
790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "M3 b"
791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(M3.__mro__, (M3, M1, M2, D, C, object))
792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = M3()
793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.cmethod(), "C a")
794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.dmethod(), "D a")
795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.m1method(), "M1 a")
796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.m2method(), "M2 a")
797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.m3method(), "M3 a")
798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.all_method(), "M3 b")
799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Classic:
801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class New(Classic):
804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __metaclass__ = type
805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("new class with only classic bases - shouldn't be")
809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_diamond_inheritence(self):
811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing multiple inheritance special cases...
812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def spam(self): return "A"
814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(A().spam(), "A")
815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A):
816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def boo(self): return "B"
817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def spam(self): return "B"
818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(B().spam(), "B")
819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(B().boo(), "B")
820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A):
821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def boo(self): return "C"
822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C().spam(), "A")
823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C().boo(), "C")
824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(B, C): pass
825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D().spam(), "B")
826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D().boo(), "B")
827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.__mro__, (D, B, C, A, object))
828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(C, B): pass
829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E().spam(), "B")
830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E().boo(), "C")
831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E.__mro__, (E, C, B, A, object))
832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # MRO order disagreement
833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class F(D, E): pass
835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("expected MRO order disagreement (F)")
839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class G(E, D): pass
841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("expected MRO order disagreement (G)")
845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # see thread python-dev/2002-October/029035.html
847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_ex5_from_c3_switch(self):
848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing ex5 from C3 switch discussion...
849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object): pass
850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object): pass
851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object): pass
852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(A): pass
853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Y(A): pass
854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Z(X,B,Y,C): pass
855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Z.__mro__, (Z, X, B, Y, A, C, object))
856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # see "A Monotonic Superclass Linearization for Dylan",
858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # by Kim Barrett et al. (OOPSLA 1996)
859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_monotonicity(self):
860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing MRO monotonicity...
861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Boat(object): pass
862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class DayBoat(Boat): pass
863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class WheelBoat(Boat): pass
864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class EngineLess(DayBoat): pass
865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class SmallMultihull(DayBoat): pass
866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class PedalWheelBoat(EngineLess,WheelBoat): pass
867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class SmallCatamaran(SmallMultihull): pass
868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Pedalo(PedalWheelBoat,SmallCatamaran): pass
869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(PedalWheelBoat.__mro__,
871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep              (PedalWheelBoat, EngineLess, DayBoat, WheelBoat, Boat, object))
872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(SmallCatamaran.__mro__,
873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep              (SmallCatamaran, SmallMultihull, DayBoat, Boat, object))
874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Pedalo.__mro__,
875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep              (Pedalo, PedalWheelBoat, EngineLess, SmallCatamaran,
876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               SmallMultihull, DayBoat, WheelBoat, Boat, object))
877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # see "A Monotonic Superclass Linearization for Dylan",
879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # by Kim Barrett et al. (OOPSLA 1996)
880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_consistency_with_epg(self):
881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing consistency with EPG...
882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Pane(object): pass
883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class ScrollingMixin(object): pass
884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class EditingMixin(object): pass
885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class ScrollablePane(Pane,ScrollingMixin): pass
886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class EditablePane(Pane,EditingMixin): pass
887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class EditableScrollablePane(ScrollablePane,EditablePane): pass
888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(EditableScrollablePane.__mro__,
890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep              (EditableScrollablePane, ScrollablePane, EditablePane, Pane,
891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                ScrollingMixin, EditingMixin, object))
892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_mro_disagreement(self):
894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing error messages for MRO disagreement...
895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        mro_err_msg = """Cannot create a consistent method resolution
896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeporder (MRO) for bases """
897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def raises(exc, expected, callable, *args):
899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                callable(*args)
901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except exc, msg:
902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # the exact msg is generally considered an impl detail
903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if test_support.check_impl_detail():
904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if not str(msg).startswith(expected):
905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        self.fail("Message %r, expected %r" %
906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                  (str(msg), expected))
907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
908edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("Expected %s" % exc)
909edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
910edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object): pass
911edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A): pass
912edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object): pass
913edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
914edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test some very simple errors
915edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raises(TypeError, "duplicate base class A",
916edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               type, "X", (A, A), {})
917edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raises(TypeError, mro_err_msg,
918edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               type, "X", (A, B), {})
919edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raises(TypeError, mro_err_msg,
920edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               type, "X", (A, C, B), {})
921edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test a slightly more complex error
922edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class GridLayout(object): pass
923edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class HorizontalGrid(GridLayout): pass
924edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class VerticalGrid(GridLayout): pass
925edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class HVGrid(HorizontalGrid, VerticalGrid): pass
926edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class VHGrid(VerticalGrid, HorizontalGrid): pass
927edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raises(TypeError, mro_err_msg,
928edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               type, "ConfusedGrid", (HVGrid, VHGrid), {})
929edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
930edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_object_class(self):
931edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing object class...
932edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = object()
933edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.__class__, object)
934edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(a), object)
935edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b = object()
936edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(a, b)
937edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "foo"))
938edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
939edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            a.foo = 12
940edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except (AttributeError, TypeError):
941edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
942edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
943edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("object() should not allow setting a foo attribute")
944edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(object(), "__dict__"))
945edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
946edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Cdict(object):
947edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
948edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = Cdict()
949edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.__dict__, {})
950edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.foo = 1
951edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.foo, 1)
952edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.__dict__, {'foo': 1})
953edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
954edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slots(self):
955edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __slots__...
956edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C0(object):
957edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = []
958edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = C0()
959edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, "__dict__"))
960edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, "foo"))
961edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
962edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C1(object):
963edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['a']
964edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = C1()
965edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, "__dict__"))
966edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, "a"))
967edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.a = 1
968edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.a, 1)
969edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.a = None
970edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.a, None)
971edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del x.a
972edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, "a"))
973edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
974edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C3(object):
975edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['a', 'b', 'c']
976edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = C3()
977edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, "__dict__"))
978edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, 'a'))
979edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, 'b'))
980edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, 'c'))
981edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.a = 1
982edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.b = 2
983edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.c = 3
984edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.a, 1)
985edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.b, 2)
986edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.c, 3)
987edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
988edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C4(object):
989edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """Validate name mangling"""
990edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['__a']
991edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, value):
992edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__a = value
993edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def get(self):
994edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.__a
995edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = C4(5)
996edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, '__dict__'))
997edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(x, '__a'))
998edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.get(), 5)
999edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1000edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.__a = 6
1001edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except AttributeError:
1002edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1003edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1004edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Double underscored names not mangled")
1005edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1006edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Make sure slot names are proper identifiers
1007edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1008edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1009edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = [None]
1010edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1011edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1012edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1013edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("[None] slots not caught")
1014edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1015edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1016edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = ["foo bar"]
1017edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1018edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1019edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1020edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("['foo bar'] slots not caught")
1021edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1022edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1023edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = ["foo\0bar"]
1024edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1025edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1026edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1027edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("['foo\\0bar'] slots not caught")
1028edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1029edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1030edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = ["1"]
1031edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1032edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1033edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1034edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("['1'] slots not caught")
1035edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1036edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1037edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = [""]
1038edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1039edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1040edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1041edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("[''] slots not caught")
1042edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1043edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["a", "a_b", "_a", "A0123456789Z"]
1044edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX(nnorwitz): was there supposed to be something tested
1045edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # from the class above?
1046edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1047edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test a single string is not expanded as a sequence.
1048edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1049edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = "abc"
1050edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
1051edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c.abc = 5
1052edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.abc, 5)
1053edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1054edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test unicode slot names
1055edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1056edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            unicode
1057edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except NameError:
1058edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1059edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1060edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Test a single unicode string is not expanded as a sequence.
1061edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1062edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = unicode("abc")
1063edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c = C()
1064edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c.abc = 5
1065edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(c.abc, 5)
1066edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1067edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # _unicode_to_string used to modify slots in certain circumstances
1068edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            slots = (unicode("foo"), unicode("bar"))
1069edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1070edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = slots
1071edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = C()
1072edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.foo = 5
1073edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.foo, 5)
1074edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(type(slots[0]), unicode)
1075edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # this used to leak references
1076edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
1077edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class C(object):
1078edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    __slots__ = [unichr(128)]
1079edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except (TypeError, UnicodeEncodeError):
1080edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1081edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
1082edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("[unichr(128)] slots not caught")
1083edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1084edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test leaks
1085edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Counted(object):
1086edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            counter = 0    # counts the number of instances alive
1087edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
1088edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Counted.counter += 1
1089edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __del__(self):
1090edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Counted.counter -= 1
1091edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1092edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['a', 'b', 'c']
1093edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = C()
1094edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.a = Counted()
1095edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.b = Counted()
1096edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.c = Counted()
1097edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Counted.counter, 3)
1098edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del x
1099edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
1100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Counted.counter, 0)
1101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
1102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = D()
1104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.a = Counted()
1105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.z = Counted()
1106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Counted.counter, 2)
1107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del x
1108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
1109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Counted.counter, 0)
1110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(D):
1111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['e']
1112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = E()
1113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.a = Counted()
1114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.z = Counted()
1115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.e = Counted()
1116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Counted.counter, 3)
1117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del x
1118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
1119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Counted.counter, 0)
1120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test cyclical leaks [SF bug 519621]
1122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class F(object):
1123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['a', 'b']
1124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = F()
1125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s.a = [Counted(), s]
1126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Counted.counter, 1)
1127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = None
1128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
1129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Counted.counter, 0)
1130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test lookup leaks [SF bug 572567]
1132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if hasattr(gc, 'get_objects'):
1133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class G(object):
1134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __cmp__(self, other):
1135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return 0
1136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __hash__ = None # Silence Py3k warning
1137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            g = G()
1138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            orig_objects = len(gc.get_objects())
1139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for i in xrange(10):
1140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                g==g
1141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            new_objects = len(gc.get_objects())
1142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(orig_objects, new_objects)
1143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class H(object):
1145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['a', 'b']
1146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
1147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.a = 1
1148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.b = 2
1149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __del__(self_):
1150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(self_.a, 1)
1151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(self_.b, 2)
1152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with test_support.captured_output('stderr') as s:
1153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            h = H()
1154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            del h
1155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.getvalue(), '')
1156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(object):
1158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = "a"
1159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with self.assertRaises(AttributeError):
1160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            del X().a
1161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slots_special(self):
1163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __dict__ and __weakref__ in __slots__...
1164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(object):
1165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["__dict__"]
1166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = D()
1167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(a, "__dict__"))
1168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "__weakref__"))
1169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.foo = 42
1170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.__dict__, {"foo": 42})
1171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class W(object):
1173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["__weakref__"]
1174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = W()
1175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(a, "__weakref__"))
1176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "__dict__"))
1177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            a.foo = 42
1179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except AttributeError:
1180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't be allowed to set a.foo")
1183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C1(W, D):
1185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = []
1186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C1()
1187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(a, "__dict__"))
1188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(a, "__weakref__"))
1189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.foo = 42
1190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.__dict__, {"foo": 42})
1191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C2(D, W):
1193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = []
1194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C2()
1195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(a, "__dict__"))
1196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(a, "__weakref__"))
1197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.foo = 42
1198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.__dict__, {"foo": 42})
1199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slots_descriptor(self):
1201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Issue2115: slot descriptors did not correctly check
1202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # the type of the given object
1203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import abc
1204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MyABC:
1205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = abc.ABCMeta
1206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = "a"
1207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Unrelated(object):
1209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        MyABC.register(Unrelated)
1211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        u = Unrelated()
1213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(u, MyABC)
1214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # This used to crash
1216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
1217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_metaclass_cmp(self):
1219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # See bug 7491.
1220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M(type):
1221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __cmp__(self, other):
1222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return -1
1223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(object):
1224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = M
1225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(X < M)
1226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dynamics(self):
1228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing class attribute propagation...
1229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(object):
1230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(D):
1232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class F(D):
1234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        D.foo = 1
1236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.foo, 1)
1237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test that dynamic attributes are inherited
1238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E.foo, 1)
1239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(F.foo, 1)
1240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test dynamic instances
1241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
1244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "foobar"))
1245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.foobar = 2
1246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.foobar, 2)
1247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.method = lambda self: 42
1248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.method(), 42)
1249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__repr__ = lambda self: "C()"
1250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(a), "C()")
1251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__int__ = lambda self: 100
1252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(int(a), 100)
1253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.foobar, 2)
1254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "spam"))
1255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def mygetattr(self, name):
1256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if name == "spam":
1257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "spam"
1258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raise AttributeError
1259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__getattr__ = mygetattr
1260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.spam, "spam")
1261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.new = 12
1262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.new, 12)
1263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def mysetattr(self, name, value):
1264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if name == "spam":
1265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise AttributeError
1266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return object.__setattr__(self, name, value)
1267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__setattr__ = mysetattr
1268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            a.spam = "not spam"
1270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except AttributeError:
1271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("expected AttributeError")
1274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.spam, "spam")
1275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
1276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
1278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d.foo = 1
1279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, 1)
1280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test handling of int*seq and seq*int
1282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class I(int):
1283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual("a"*I(2), "aa")
1285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(I(2)*"a", "aa")
1286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(2*I(3), 6)
1287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(I(3)*2, 6)
1288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(I(3)*I(2), 6)
1289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test handling of long*seq and seq*long
1291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class L(long):
1292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual("a"*L(2L), "aa")
1294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(L(2L)*"a", "aa")
1295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(2*L(3), 6)
1296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(L(3)*2, 6)
1297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(L(3)*L(2), 6)
1298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test comparison of classes with dynamic metaclasses
1300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class dynamicmetaclass(type):
1301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class someclass:
1303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = dynamicmetaclass
1304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(someclass, object)
1305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_errors(self):
1307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing errors...
1308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(list, dict):
1310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("inheritance from both list and dict should be illegal")
1315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object, None):
1318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("inheritance from non-type should be illegal")
1323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Classic:
1324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(type(len)):
1328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("inheritance from CFunction should be illegal")
1333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = 1
1337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("__slots__ = 1 should be illegal")
1341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(object):
1344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = [1]
1345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("__slots__ = [1] should be illegal")
1349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M1(type):
1351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M2(type):
1353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A1(object):
1355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = M1
1356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A2(object):
1357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = M2
1358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class B(A1, A2):
1360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("finding the most derived metaclass should have failed")
1365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_classmethods(self):
1367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing class methods...
1368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(*a): return a
1370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            goo = classmethod(foo)
1371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
1372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.goo(1), (C, 1))
1373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.goo(1), (C, 1))
1374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.foo(1), (c, 1))
1375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
1376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
1378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.goo(1), (D, 1))
1379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.goo(1), (D, 1))
1380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo(1), (d, 1))
1381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.foo(d, 1), (d, 1))
1382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test for a specific crash (SF bug 528132)
1383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def f(cls, arg): return (cls, arg)
1384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ff = classmethod(f)
1385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(ff.__get__(0, int)(42), (int, 42))
1386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(ff.__get__(0)(42), (int, 42))
1387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test super() with classmethods (SF bug 535444)
1389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.goo.im_self, C)
1390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.goo.im_self, D)
1391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(super(D,D).goo.im_self, D)
1392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(super(D,d).goo.im_self, D)
1393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(super(D,D).goo(), (D,))
1394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(super(D,d).goo(), (D,))
1395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that a non-callable will raise
1397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        meth = classmethod(1).__get__(1)
1398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, meth)
1399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that classmethod() doesn't allow keyword args
1401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            classmethod(f, kw=1)
1403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("classmethod shouldn't accept keyword args")
1407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @test_support.impl_detail("the module 'xxsubtype' is internal")
1409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_classmethods_in_c(self):
1410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing C-based class methods...
1411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import xxsubtype as spam
1412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = (1, 2, 3)
1413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {'abc': 123}
1414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x, a1, d1 = spam.spamlist.classmeth(*a, **d)
1415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x, spam.spamlist)
1416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, a1)
1417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, d1)
1418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x, a1, d1 = spam.spamlist().classmeth(*a, **d)
1419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x, spam.spamlist)
1420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, a1)
1421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, d1)
1422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        spam_cm = spam.spamlist.__dict__['classmeth']
1423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x2, a2, d2 = spam_cm(spam.spamlist, *a, **d)
1424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x2, spam.spamlist)
1425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a2, a1)
1426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d2, d1)
1427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class SubSpam(spam.spamlist): pass
1428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x2, a2, d2 = spam_cm(SubSpam, *a, **d)
1429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x2, SubSpam)
1430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a2, a1)
1431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d2, d1)
1432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with self.assertRaises(TypeError):
1433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            spam_cm()
1434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with self.assertRaises(TypeError):
1435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            spam_cm(spam.spamlist())
1436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with self.assertRaises(TypeError):
1437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            spam_cm(list)
1438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_staticmethods(self):
1440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing static methods...
1441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(*a): return a
1443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            goo = staticmethod(foo)
1444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
1445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.goo(1), (1,))
1446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.goo(1), (1,))
1447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.foo(1), (c, 1,))
1448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
1449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
1451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.goo(1), (1,))
1452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.goo(1), (1,))
1453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo(1), (d, 1))
1454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.foo(d, 1), (d, 1))
1455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @test_support.impl_detail("the module 'xxsubtype' is internal")
1457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_staticmethods_in_c(self):
1458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing C-based static methods...
1459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import xxsubtype as spam
1460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = (1, 2, 3)
1461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {"abc": 123}
1462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x, a1, d1 = spam.spamlist.staticmeth(*a, **d)
1463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x, None)
1464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, a1)
1465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, d1)
1466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x, a1, d2 = spam.spamlist().staticmeth(*a, **d)
1467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x, None)
1468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, a1)
1469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, d1)
1470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_classic(self):
1472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing classic classes...
1473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
1474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(*a): return a
1475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            goo = classmethod(foo)
1476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
1477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.goo(1), (C, 1))
1478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.goo(1), (C, 1))
1479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.foo(1), (c, 1))
1480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
1481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
1483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.goo(1), (D, 1))
1484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.goo(1), (D, 1))
1485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo(1), (d, 1))
1486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.foo(d, 1), (d, 1))
1487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E: # *not* subclassing from C
1488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            foo = C.foo
1489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E().foo, C.foo) # i.e., unbound
1490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(repr(C.foo.__get__(C())).startswith("<bound method "))
1491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_compattr(self):
1493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing computed attributes...
1494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class computed_attribute(object):
1496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __init__(self, get, set=None, delete=None):
1497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.__get = get
1498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.__set = set
1499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.__delete = delete
1500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __get__(self, obj, type=None):
1501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return self.__get(obj)
1502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __set__(self, obj, value):
1503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return self.__set(obj, value)
1504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __delete__(self, obj):
1505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return self.__delete(obj)
1506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
1507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__x = 0
1508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __get_x(self):
1509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x = self.__x
1510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__x = x+1
1511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return x
1512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __set_x(self, x):
1513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__x = x
1514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __delete_x(self):
1515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del self.__x
1516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = computed_attribute(__get_x, __set_x, __delete_x)
1517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
1518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.x, 0)
1519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.x, 1)
1520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.x = 10
1521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.x, 10)
1522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.x, 11)
1523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del a.x
1524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hasattr(a, 'x'), 0)
1525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_newslots(self):
1527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __new__ slot override...
1528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(list):
1529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(cls):
1530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self = list.__new__(cls)
1531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.foo = 1
1532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self
1533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
1534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.foo = self.foo + 2
1535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
1536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.foo, 3)
1537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.__class__, C)
1538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
1539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b = D()
1541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(b.foo, 3)
1542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(b.__class__, D)
1543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_altmro(self):
1545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing mro() and overriding it...
1546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
1547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def f(self): return "A"
1548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A):
1549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A):
1551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def f(self): return "C"
1552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(B, C):
1553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.mro(), [D, B, C, A, object])
1555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D.__mro__, (D, B, C, A, object))
1556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D().f(), "C")
1557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class PerverseMetaType(type):
1559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def mro(cls):
1560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                L = type.mro(cls)
1561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                L.reverse()
1562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return L
1563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(D,B,C,A):
1564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = PerverseMetaType
1565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(X.__mro__, (object, A, C, B, D, X))
1566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(X().f(), "A")
1567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class X(object):
1570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class __metaclass__(type):
1571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    def mro(self):
1572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return [self, dict, object]
1573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # In CPython, the class creation above already raises
1574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # TypeError, as a protection against the fact that
1575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # instances of X would segfault it.  In other Python
1576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # implementations it would be ok to let the class X
1577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # be created, but instead get a clean TypeError on the
1578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # __setitem__ below.
1579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = object.__new__(X)
1580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x[5] = 6
1581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("devious mro() return not caught")
1585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class X(object):
1588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class __metaclass__(type):
1589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    def mro(self):
1590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return [1]
1591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("non-class mro() return not caught")
1595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class X(object):
1598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class __metaclass__(type):
1599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    def mro(self):
1600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return 1
1601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
1602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("non-sequence mro() return not caught")
1605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_overloading(self):
1607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing operator overloading...
1608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object):
1610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            "Intermediate class because object doesn't have a __setattr__"
1611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(B):
1613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattr__(self, name):
1614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if name == "foo":
1615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return ("getattr", name)
1616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
1617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise AttributeError
1618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setattr__(self, name, value):
1619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if name == "foo":
1620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.setattr = (name, value)
1621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
1622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return B.__setattr__(self, name, value)
1623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __delattr__(self, name):
1624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if name == "foo":
1625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.delattr = name
1626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
1627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return B.__delattr__(self, name)
1628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, key):
1630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return ("getitem", key)
1631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setitem__(self, key, value):
1632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.setitem = (key, value)
1633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __delitem__(self, key):
1634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.delitem = key
1635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getslice__(self, i, j):
1637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return ("getslice", i, j)
1638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setslice__(self, i, j, value):
1639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.setslice = (i, j, value)
1640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __delslice__(self, i, j):
1641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.delslice = (i, j)
1642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
1644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.foo, ("getattr", "foo"))
1645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.foo = 12
1646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.setattr, ("foo", 12))
1647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del a.foo
1648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.delattr, "foo")
1649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[12], ("getitem", 12))
1651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a[12] = 21
1652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.setitem, (12, 21))
1653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del a[12]
1654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.delitem, 12)
1655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[0:10], ("getslice", 0, 10))
1657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a[0:10] = "foo"
1658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.setslice, (0, 10, "foo"))
1659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del a[0:10]
1660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.delslice, (0, 10))
1661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_methods(self):
1663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing methods...
1664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, x):
1666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.x = x
1667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self):
1668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.x
1669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c1 = C(1)
1670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c1.foo(), 1)
1671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
1672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            boo = C.foo
1673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            goo = c1.foo
1674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d2 = D(2)
1675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d2.foo(), 2)
1676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d2.boo(), 2)
1677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d2.goo(), 1)
1678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(object):
1679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            foo = C.foo
1680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E().foo, C.foo) # i.e., unbound
1681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(repr(C.foo.__get__(C(1))).startswith("<bound method "))
1682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_special_method_lookup(self):
1684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # The lookup of special methods bypasses __getattr__ and
1685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # __getattribute__, but they still can be descriptors.
1686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def run_context(manager):
1688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            with manager:
1689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def iden(self):
1691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return self
1692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def hello(self):
1693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return "hello"
1694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def empty_seq(self):
1695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return []
1696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def zero(self):
1697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return 0
1698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def complex_num(self):
1699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return 1j
1700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def stop(self):
1701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raise StopIteration
1702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def return_true(self, thing=None):
1703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return True
1704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def do_isinstance(obj):
1705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return isinstance(int, obj)
1706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def do_issubclass(obj):
1707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return issubclass(int, obj)
1708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def swallow(*args):
1709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def do_dict_missing(checker):
1711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class DictSub(checker.__class__, dict):
1712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(DictSub()["hi"], 4)
1714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def some_number(self_, key):
1715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(key, "hi")
1716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return 4
1717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def format_impl(self, spec):
1718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return "hello"
1719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # It would be nice to have every special method tested here, but I'm
1721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # only listing the ones I can remember outside of typeobject.c, since it
1722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # does it right.
1723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        specials = [
1724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__unicode__", unicode, hello, set(), {}),
1725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__reversed__", reversed, empty_seq, set(), {}),
1726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__length_hint__", list, zero, set(),
1727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             {"__iter__" : iden, "next" : stop}),
1728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__sizeof__", sys.getsizeof, zero, set(), {}),
1729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__instancecheck__", do_isinstance, return_true, set(), {}),
1730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__missing__", do_dict_missing, some_number,
1731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             set(("__class__",)), {}),
1732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__subclasscheck__", do_issubclass, return_true,
1733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             set(("__bases__",)), {}),
1734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__enter__", run_context, iden, set(), {"__exit__" : swallow}),
1735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__exit__", run_context, swallow, set(), {"__enter__" : iden}),
1736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__complex__", complex, complex_num, set(), {}),
1737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__format__", format, format_impl, set(), {}),
1738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("__dir__", dir, empty_seq, set(), {}),
1739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ]
1740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Checker(object):
1742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattr__(self, attr, test=self):
1743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                test.fail("__getattr__ called with {0}".format(attr))
1744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattribute__(self, attr, test=self):
1745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if attr not in ok:
1746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    test.fail("__getattribute__ called with {0}".format(attr))
1747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return object.__getattribute__(self, attr)
1748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class SpecialDescr(object):
1749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, impl):
1750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.impl = impl
1751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __get__(self, obj, owner):
1752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                record.append(1)
1753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.impl.__get__(obj, owner)
1754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MyException(Exception):
1755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class ErrDescr(object):
1757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __get__(self, obj, owner):
1758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise MyException
1759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for name, runner, meth_impl, ok, env in specials:
1761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class X(Checker):
1762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for attr, obj in env.iteritems():
1764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                setattr(X, attr, obj)
1765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            setattr(X, name, meth_impl)
1766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            runner(X())
1767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            record = []
1769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class X(Checker):
1770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for attr, obj in env.iteritems():
1772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                setattr(X, attr, obj)
1773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            setattr(X, name, SpecialDescr(meth_impl))
1774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            runner(X())
1775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(record, [1], name)
1776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class X(Checker):
1778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for attr, obj in env.iteritems():
1780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                setattr(X, attr, obj)
1781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            setattr(X, name, ErrDescr())
1782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
1783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                runner(X())
1784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except MyException:
1785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
1787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("{0!r} didn't raise".format(name))
1788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_specials(self):
1790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing special operators...
1791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test operators like __hash__ for which a built-in default exists
1792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test the default behavior for static classes
1794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, i):
1796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if 0 <= i < 10: return i
1797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise IndexError
1798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c1 = C()
1799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c2 = C()
1800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not not c1) # What?
1801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(id(c1), id(c2))
1802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hash(c1)
1803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hash(c2)
1804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cmp(c1, c2), cmp(id(c1), id(c2)))
1805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c1, c1)
1806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(c1 != c2)
1807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not c1 != c1)
1808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not c1 == c2)
1809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Note that the module name appears in str/repr, and that varies
1810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # depending on whether this test is run standalone or from a framework.
1811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(str(c1).find('C object at ') >= 0)
1812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(c1), repr(c1))
1813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn(-1, c1)
1814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in range(10):
1815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIn(i, c1)
1816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn(10, c1)
1817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test the default behavior for dynamic classes
1818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(object):
1819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, i):
1820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if 0 <= i < 10: return i
1821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise IndexError
1822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d1 = D()
1823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d2 = D()
1824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not not d1)
1825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(id(d1), id(d2))
1826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hash(d1)
1827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hash(d2)
1828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cmp(d1, d2), cmp(id(d1), id(d2)))
1829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d1, d1)
1830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(d1, d2)
1831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not d1 != d1)
1832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not d1 == d2)
1833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Note that the module name appears in str/repr, and that varies
1834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # depending on whether this test is run standalone or from a framework.
1835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(str(d1).find('D object at ') >= 0)
1836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(d1), repr(d1))
1837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn(-1, d1)
1838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in range(10):
1839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIn(i, d1)
1840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn(10, d1)
1841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test overridden behavior for static classes
1842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Proxy(object):
1843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, x):
1844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.x = x
1845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __nonzero__(self):
1846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return not not self.x
1847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __hash__(self):
1848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return hash(self.x)
1849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __eq__(self, other):
1850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.x == other
1851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __ne__(self, other):
1852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.x != other
1853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __cmp__(self, other):
1854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return cmp(self.x, other.x)
1855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __str__(self):
1856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "Proxy:%s" % self.x
1857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
1858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "Proxy(%r)" % self.x
1859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __contains__(self, value):
1860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return value in self.x
1861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p0 = Proxy(0)
1862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p1 = Proxy(1)
1863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p_1 = Proxy(-1)
1864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(p0)
1865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not not p1)
1866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(p0), hash(0))
1867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(p0, p0)
1868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(p0, p1)
1869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not p0 != p0)
1870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(not p0, p1)
1871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cmp(p0, p1), -1)
1872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cmp(p0, p0), 0)
1873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cmp(p0, p_1), 1)
1874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(p0), "Proxy:0")
1875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(p0), "Proxy(0)")
1876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p10 = Proxy(range(10))
1877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn(-1, p10)
1878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in range(10):
1879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIn(i, p10)
1880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn(10, p10)
1881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test overridden behavior for dynamic classes
1882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class DProxy(object):
1883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, x):
1884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.x = x
1885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __nonzero__(self):
1886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return not not self.x
1887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __hash__(self):
1888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return hash(self.x)
1889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __eq__(self, other):
1890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.x == other
1891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __ne__(self, other):
1892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.x != other
1893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __cmp__(self, other):
1894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return cmp(self.x, other.x)
1895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __str__(self):
1896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "DProxy:%s" % self.x
1897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
1898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "DProxy(%r)" % self.x
1899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __contains__(self, value):
1900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return value in self.x
1901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p0 = DProxy(0)
1902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p1 = DProxy(1)
1903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p_1 = DProxy(-1)
1904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(p0)
1905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(not not p1)
1906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(p0), hash(0))
1907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(p0, p0)
1908edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(p0, p1)
1909edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(not p0, p0)
1910edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(not p0, p1)
1911edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cmp(p0, p1), -1)
1912edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cmp(p0, p0), 0)
1913edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cmp(p0, p_1), 1)
1914edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(p0), "DProxy:0")
1915edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(p0), "DProxy(0)")
1916edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p10 = DProxy(range(10))
1917edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn(-1, p10)
1918edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in range(10):
1919edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIn(i, p10)
1920edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotIn(10, p10)
1921edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1922edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Safety test for __cmp__
1923edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def unsafecmp(a, b):
1924edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if not hasattr(a, '__cmp__'):
1925edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return   # some types don't have a __cmp__ any more (so the
1926edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         # test doesn't make sense any more), or maybe they
1927edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         # never had a __cmp__ at all, e.g. in PyPy
1928edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
1929edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                a.__class__.__cmp__(a, b)
1930edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
1931edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
1932edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
1933edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("shouldn't allow %s.__cmp__(%r, %r)" % (
1934edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    a.__class__, a, b))
1935edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1936edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unsafecmp(u"123", "123")
1937edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unsafecmp("123", u"123")
1938edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unsafecmp(1, 1.0)
1939edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unsafecmp(1.0, 1)
1940edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unsafecmp(1, 1L)
1941edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unsafecmp(1L, 1)
1942edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1943edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @test_support.impl_detail("custom logic for printing to real file objects")
1944edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursions_1(self):
1945edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing recursion checks ...
1946edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Letter(str):
1947edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(cls, letter):
1948edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if letter == 'EPS':
1949edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return str.__new__(cls)
1950edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return str.__new__(cls, letter)
1951edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __str__(self):
1952edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if not self:
1953edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return 'EPS'
1954edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self
1955edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # sys.stdout needs to be the original to trigger the recursion bug
1956edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_stdout = sys.stdout
1957edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.stdout = test_support.get_original_stdout()
1958edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1959edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # nothing should actually be printed, this should raise an exception
1960edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            print Letter('w')
1961edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except RuntimeError:
1962edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1963edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1964edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("expected a RuntimeError for print recursion")
1965edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
1966edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.stdout = test_stdout
1967edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1968edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursions_2(self):
1969edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Bug #1202533.
1970edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
1971edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1972edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A.__mul__ = types.MethodType(lambda self, x: self * x, None, A)
1973edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1974edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            A()*2
1975edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except RuntimeError:
1976edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1977edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1978edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("expected a RuntimeError")
1979edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1980edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_weakrefs(self):
1981edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing weak references...
1982edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import weakref
1983edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
1984edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
1985edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
1986edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        r = weakref.ref(c)
1987edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(r(), c)
1988edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del c
1989edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
1990edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(r(), None)
1991edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del r
1992edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class NoWeak(object):
1993edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['foo']
1994edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        no = NoWeak()
1995edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1996edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            weakref.ref(no)
1997edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError, msg:
1998edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(str(msg).find("weak reference") >= 0)
1999edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2000edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("weakref.ref(no) should be illegal")
2001edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Weak(object):
2002edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['foo', '__weakref__']
2003edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        yes = Weak()
2004edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        r = weakref.ref(yes)
2005edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(r(), yes)
2006edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del yes
2007edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
2008edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(r(), None)
2009edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del r
2010edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2011edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_properties(self):
2012edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing property...
2013edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
2014edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getx(self):
2015edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.__x
2016edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def setx(self, value):
2017edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__x = value
2018edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def delx(self):
2019edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del self.__x
2020edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = property(getx, setx, delx, doc="I'm the x property.")
2021edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
2022edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "x"))
2023edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.x = 42
2024edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a._C__x, 42)
2025edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.x, 42)
2026edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del a.x
2027edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "x"))
2028edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "_C__x"))
2029edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.x.__set__(a, 100)
2030edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.x.__get__(a), 100)
2031edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.x.__delete__(a)
2032edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(a, "x"))
2033edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2034edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raw = C.__dict__['x']
2035edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(raw, property)
2036edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2037edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        attrs = dir(raw)
2038edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn("__doc__", attrs)
2039edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn("fget", attrs)
2040edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn("fset", attrs)
2041edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn("fdel", attrs)
2042edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2043edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(raw.__doc__, "I'm the x property.")
2044edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(raw.fget is C.__dict__['getx'])
2045edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(raw.fset is C.__dict__['setx'])
2046edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(raw.fdel is C.__dict__['delx'])
2047edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2048edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for attr in "__doc__", "fget", "fset", "fdel":
2049edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
2050edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                setattr(raw, attr, 42)
2051edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError, msg:
2052edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if str(msg).find('readonly') < 0:
2053edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.fail("when setting readonly attr %r on a property, "
2054edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                     "got unexpected TypeError msg %r" % (attr, str(msg)))
2055edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
2056edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("expected TypeError from trying to set readonly %r "
2057edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                 "attr on a property" % attr)
2058edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2059edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(object):
2060edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __getitem__ = property(lambda s: 1/0)
2061edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2062edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
2063edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2064edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for i in d:
2065edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                str(i)
2066edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ZeroDivisionError:
2067edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2068edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2069edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("expected ZeroDivisionError from bad property")
2070edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2071edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @unittest.skipIf(sys.flags.optimize >= 2,
2072edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "Docstrings are omitted with -O2 and above")
2073edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_properties_doc_attrib(self):
2074edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(object):
2075edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getter(self):
2076edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "getter method"
2077edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 0
2078edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def setter(self_, value):
2079edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "setter method"
2080edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
2081edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            prop = property(getter)
2082edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(prop.__doc__, "getter method")
2083edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            prop2 = property(fset=setter)
2084edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(prop2.__doc__, None)
2085edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2086edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_testcapi_no_segfault(self):
2087edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # this segfaulted in 2.5b2
2088edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2089edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            import _testcapi
2090edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ImportError:
2091edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2092edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2093edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class X(object):
2094edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                p = property(_testcapi.test_with_docstring)
2095edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2096edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_properties_plus(self):
2097edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
2098edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            foo = property(doc="hello")
2099edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @foo.getter
2100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self):
2101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self._foo
2102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @foo.setter
2103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self, value):
2104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._foo = abs(value)
2105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @foo.deleter
2106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self):
2107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del self._foo
2108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
2109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.foo.__doc__, "hello")
2110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(c, "foo"))
2111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c.foo = -42
2112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(c, '_foo'))
2113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c._foo, 42)
2114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.foo, 42)
2115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del c.foo
2116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(c, '_foo'))
2117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(hasattr(c, "foo"))
2118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
2120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @C.foo.deleter
2121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self):
2122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
2123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    del self._foo
2124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                except AttributeError:
2125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    pass
2126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
2127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d.foo = 24
2128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, 24)
2129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del d.foo
2130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del d.foo
2131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(object):
2133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @property
2134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self):
2135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self._foo
2136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @foo.setter
2137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self, value):
2138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise RuntimeError
2139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @foo.setter
2140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self, value):
2141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._foo = abs(value)
2142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @foo.deleter
2143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self, value=None):
2144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del self._foo
2145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        e = E()
2147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        e.foo = -42
2148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(e.foo, 42)
2149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del e.foo
2150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class F(E):
2152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @E.foo.deleter
2153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self):
2154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del self._foo
2155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @foo.setter
2156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def foo(self, value):
2157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._foo = max(0, value)
2158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = F()
2159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.foo = -10
2160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(f.foo, 0)
2161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del f.foo
2162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dict_constructors(self):
2164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing dict constructor ...
2165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = dict()
2166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, {})
2167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = dict({})
2168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, {})
2169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = dict({1: 2, 'a': 'b'})
2170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, {1: 2, 'a': 'b'})
2171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict(d.items()))
2172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict(d.iteritems()))
2173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = dict({'one':1, 'two':2})
2174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict(one=1, two=2))
2175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict(**d))
2176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict({"one": 1}, two=2))
2177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict([("two", 2)], one=1))
2178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict([("one", 100), ("two", 200)], **d))
2179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict(**d))
2180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for badarg in 0, 0L, 0j, "0", [0], (0,):
2182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
2183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                dict(badarg)
2184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
2185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
2186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except ValueError:
2187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if badarg == "0":
2188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # It's a sequence, and its elements are also sequences (gotta
2189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # love strings <wink>), but they aren't of length 2, so this
2190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # one seemed better as a ValueError than a TypeError.
2191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    pass
2192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
2193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.fail("no TypeError from dict(%r)" % badarg)
2194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
2195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("no TypeError from dict(%r)" % badarg)
2196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dict({}, {})
2199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
2200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("no TypeError from dict({}, {})")
2203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Mapping:
2205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Lacks a .keys() method; will be added later.
2206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dict = {1:2, 3:4, 'a':1j}
2207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dict(Mapping())
2210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
2211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("no TypeError from dict(incomplete mapping)")
2214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Mapping.keys = lambda self: self.dict.keys()
2216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Mapping.__getitem__ = lambda self, i: self.dict[i]
2217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = dict(Mapping())
2218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, Mapping.dict)
2219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Init from sequence of iterable objects, each producing a 2-sequence.
2221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class AddressBookEntry:
2222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, first, last):
2223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.first = first
2224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.last = last
2225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __iter__(self):
2226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return iter([self.first, self.last])
2227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = dict([AddressBookEntry('Tim', 'Warsaw'),
2229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                  AddressBookEntry('Barry', 'Peters'),
2230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                  AddressBookEntry('Tim', 'Peters'),
2231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                  AddressBookEntry('Barry', 'Warsaw')])
2232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, {'Barry': 'Warsaw', 'Tim': 'Peters'})
2233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = dict(zip(range(4), range(1, 5)))
2235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d, dict([(i, i+1) for i in range(4)]))
2236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Bad sequence lengths.
2238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for bad in [('tooshort',)], [('too', 'long', 'by 1')]:
2239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
2240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                dict(bad)
2241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except ValueError:
2242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
2243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
2244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("no ValueError from dict(%r)" % bad)
2245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dir(self):
2247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing dir() ...
2248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        junk = 12
2249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dir(), ['junk', 'self'])
2250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del junk
2251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Just make sure these don't blow up!
2253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for arg in 2, 2L, 2j, 2e0, [2], "2", u"2", (2,), {2:2}, type, self.test_dir:
2254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dir(arg)
2255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Try classic classes.
2257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
2258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            Cdata = 1
2259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def Cmethod(self): pass
2260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cstuff = ['Cdata', 'Cmethod', '__doc__', '__module__']
2262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dir(C), cstuff)
2263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('im_self', dir(C.Cmethod))
2264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()  # c.__doc__ is an odd thing to see here; ditto c.__module__.
2266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dir(c), cstuff)
2267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c.cdata = 2
2269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c.cmethod = lambda self: 0
2270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dir(c), cstuff + ['cdata', 'cmethod'])
2271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('im_self', dir(c.Cmethod))
2272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(C):
2274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            Adata = 1
2275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def Amethod(self): pass
2276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        astuff = ['Adata', 'Amethod'] + cstuff
2278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dir(A), astuff)
2279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('im_self', dir(A.Amethod))
2280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = A()
2281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dir(a), astuff)
2282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('im_self', dir(a.Amethod))
2283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.adata = 42
2284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.amethod = lambda self: 3
2285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dir(a), astuff + ['adata', 'amethod'])
2286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # The same, but with new-style classes.  Since these have object as a
2288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # base class, a lot more gets sucked in.
2289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def interesting(strings):
2290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return [s for s in strings if not s.startswith('_')]
2291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
2293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            Cdata = 1
2294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def Cmethod(self): pass
2295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cstuff = ['Cdata', 'Cmethod']
2297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(interesting(dir(C)), cstuff)
2298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
2300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(interesting(dir(c)), cstuff)
2301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('im_self', dir(C.Cmethod))
2302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c.cdata = 2
2304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c.cmethod = lambda self: 0
2305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(interesting(dir(c)), cstuff + ['cdata', 'cmethod'])
2306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('im_self', dir(c.Cmethod))
2307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(C):
2309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            Adata = 1
2310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def Amethod(self): pass
2311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        astuff = ['Adata', 'Amethod'] + cstuff
2313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(interesting(dir(A)), astuff)
2314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('im_self', dir(A.Amethod))
2315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = A()
2316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(interesting(dir(a)), astuff)
2317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.adata = 42
2318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.amethod = lambda self: 3
2319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(interesting(dir(a)), astuff + ['adata', 'amethod'])
2320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('im_self', dir(a.Amethod))
2321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Try a module subclass.
2323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M(type(sys)):
2324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        minstance = M("m")
2326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        minstance.b = 2
2327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        minstance.a = 1
2328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        names = [x for x in dir(minstance) if x not in ["__name__", "__doc__"]]
2329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(names, ['a', 'b'])
2330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M2(M):
2332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getdict(self):
2333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "Not a dict!"
2334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __dict__ = property(getdict)
2335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m2instance = M2("m2")
2337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m2instance.b = 2
2338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m2instance.a = 1
2339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m2instance.__dict__, "Not a dict!")
2340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dir(m2instance)
2342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
2343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Two essentially featureless objects, just inheriting stuff from
2346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # object.
2347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dir(NotImplemented), dir(Ellipsis))
2348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if test_support.check_impl_detail():
2349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # None differs in PyPy: it has a __nonzero__
2350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(dir(None), dir(Ellipsis))
2351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Nasty test case for proxied objects
2353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Wrapper(object):
2354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, obj):
2355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__obj = obj
2356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
2357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "Wrapper(%s)" % repr(self.__obj)
2358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, key):
2359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return Wrapper(self.__obj[key])
2360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __len__(self):
2361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return len(self.__obj)
2362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattr__(self, name):
2363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return Wrapper(getattr(self.__obj, name))
2364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
2366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getclass(self):
2367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return Wrapper(type(self))
2368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __class__ = property(__getclass)
2369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dir(C()) # This used to segfault
2371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_supers(self):
2373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing super...
2374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
2376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self, a):
2377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "A(%r)" % a
2378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(A().meth(1), "A(1)")
2380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A):
2382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
2383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__super = super(B, self)
2384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self, a):
2385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "B(%r)" % a + self.__super.meth(a)
2386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(B().meth(2), "B(2)A(2)")
2388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A):
2390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self, a):
2391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C(%r)" % a + self.__super.meth(a)
2392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C._C__super = super(C)
2393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C().meth(3), "C(3)A(3)")
2395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C, B):
2397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self, a):
2398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "D(%r)" % a + super(D, self).meth(a)
2399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D().meth(4), "D(4)C(4)B(4)A(4)")
2401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test for subclassing super
2403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class mysuper(super):
2405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, *args):
2406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return super(mysuper, self).__init__(*args)
2407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(D):
2409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self, a):
2410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "E(%r)" % a + mysuper(E, self).meth(a)
2411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E().meth(5), "E(5)D(5)C(5)B(5)A(5)")
2413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class F(E):
2415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self, a):
2416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = self.__super # == mysuper(F, self)
2417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "F(%r)[%s]" % (a, s.__class__.__name__) + s.meth(a)
2418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        F._F__super = mysuper(F)
2419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(F().meth(6), "F(6)[mysuper]E(6)D(6)C(6)B(6)A(6)")
2421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Make sure certain errors are raised
2423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            super(D, 42)
2426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
2427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't allow super(D, 42)")
2430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            super(D, C())
2433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
2434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't allow super(D, C())")
2437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            super(D).__get__(12)
2440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
2441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't allow super(D).__get__(12)")
2444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            super(D).__get__(C())
2447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
2448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't allow super(D).__get__(C())")
2451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Make sure data descriptors can be overridden and accessed via super
2453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # (new feature in Python 2.3)
2454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class DDbase(object):
2456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getx(self): return 42
2457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = property(getx)
2458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class DDsub(DDbase):
2460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getx(self): return "hello"
2461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = property(getx)
2462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        dd = DDsub()
2464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dd.x, "hello")
2465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(super(DDsub, dd).x, 42)
2466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Ensure that super() lookup of descriptor from classmethod
2468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # works (SF ID# 743627)
2469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Base(object):
2471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            aProp = property(lambda self: "foo")
2472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Sub(Base):
2474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            @classmethod
2475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def test(klass):
2476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return super(Sub,klass).aProp
2477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(Sub.test(), Base.aProp)
2479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that super() doesn't allow keyword args
2481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            super(Base, kw=1)
2483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
2484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
2486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual("super shouldn't accept keyword args")
2487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_basic_inheritance(self):
2489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing inheritance from basic types...
2490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class hexint(int):
2492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
2493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return hex(self)
2494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __add__(self, other):
2495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return hexint(int.__add__(self, other))
2496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # (Note that overriding __radd__ doesn't work,
2497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # because the int type gets first dibs.)
2498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(hexint(7) + 9), "0x10")
2499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(hexint(1000) + 7), "0x3ef")
2500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = hexint(12345)
2501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, 12345)
2502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(int(a), 12345)
2503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(int(a).__class__ is int)
2504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(a), hash(12345))
2505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((+a).__class__ is int)
2506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a >> 0).__class__ is int)
2507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a << 0).__class__ is int)
2508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((hexint(0) << 12).__class__ is int)
2509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((hexint(0) >> 12).__class__ is int)
2510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class octlong(long):
2512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = []
2513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __str__(self):
2514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = oct(self)
2515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if s[-1] == 'L':
2516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    s = s[:-1]
2517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return s
2518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __add__(self, other):
2519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.__class__(super(octlong, self).__add__(other))
2520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __radd__ = __add__
2521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(octlong(3) + 5), "010")
2522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # (Note that overriding __radd__ here only seems to work
2523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # because the example uses a short int left argument.)
2524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(5 + octlong(3000)), "05675")
2525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = octlong(12345)
2526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, 12345L)
2527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(a), 12345L)
2528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(a), hash(12345L))
2529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(long(a).__class__ is long)
2530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((+a).__class__ is long)
2531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((-a).__class__ is long)
2532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((-octlong(0)).__class__ is long)
2533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a >> 0).__class__ is long)
2534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a << 0).__class__ is long)
2535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a - 0).__class__ is long)
2536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a * 1).__class__ is long)
2537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a ** 1).__class__ is long)
2538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a // 1).__class__ is long)
2539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((1 * a).__class__ is long)
2540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a | 0).__class__ is long)
2541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a ^ 0).__class__ is long)
2542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a & -1L).__class__ is long)
2543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((octlong(0) << 12).__class__ is long)
2544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((octlong(0) >> 12).__class__ is long)
2545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(abs(octlong(0)).__class__ is long)
2546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Because octlong overrides __add__, we can't check the absence of +0
2548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # optimizations using octlong.
2549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class longclone(long):
2550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = longclone(1)
2552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a + 0).__class__ is long)
2553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((0 + a).__class__ is long)
2554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Check that negative clones don't segfault
2556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = longclone(-1)
2557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.__dict__, {})
2558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(a), -1)  # self.assertTrue PyNumber_Long() copies the sign bit
2559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class precfloat(float):
2561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['prec']
2562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, value=0.0, prec=12):
2563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.prec = int(prec)
2564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
2565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "%.*g" % (self.prec, self)
2566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(precfloat(1.1)), "1.1")
2567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = precfloat(12345)
2568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, 12345.0)
2569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(float(a), 12345.0)
2570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(float(a).__class__ is float)
2571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(a), hash(12345.0))
2572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((+a).__class__ is float)
2573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class madcomplex(complex):
2575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
2576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "%.17gj%+.17g" % (self.imag, self.real)
2577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = madcomplex(-3, 4)
2578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(a), "4j-3")
2579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        base = complex(-3, 4)
2580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(base.__class__, complex)
2581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, base)
2582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(complex(a), base)
2583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(complex(a).__class__, complex)
2584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = madcomplex(a)  # just trying another form of the constructor
2585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(a), "4j-3")
2586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, base)
2587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(complex(a), base)
2588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(complex(a).__class__, complex)
2589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(a), hash(base))
2590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((+a).__class__, complex)
2591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((a + 0).__class__, complex)
2592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a + 0, base)
2593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((a - 0).__class__, complex)
2594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a - 0, base)
2595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((a * 1).__class__, complex)
2596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a * 1, base)
2597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((a / 1).__class__, complex)
2598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a / 1, base)
2599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class madtuple(tuple):
2601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            _rev = None
2602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def rev(self):
2603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if self._rev is not None:
2604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return self._rev
2605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                L = list(self)
2606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                L.reverse()
2607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rev = self.__class__(L)
2608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self._rev
2609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = madtuple((1,2,3,4,5,6,7,8,9,0))
2610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, (1,2,3,4,5,6,7,8,9,0))
2611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1)))
2612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.rev().rev(), madtuple((1,2,3,4,5,6,7,8,9,0)))
2613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in range(512):
2614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = madtuple(range(i))
2615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            u = t.rev()
2616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            v = u.rev()
2617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(v, t)
2618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = madtuple((1,2,3,4,5))
2619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(tuple(a), (1,2,3,4,5))
2620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(tuple(a).__class__ is tuple)
2621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(a), hash((1,2,3,4,5)))
2622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(a[:].__class__ is tuple)
2623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a * 1).__class__ is tuple)
2624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a * 0).__class__ is tuple)
2625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a + ()).__class__ is tuple)
2626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = madtuple(())
2627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(tuple(a), ())
2628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(tuple(a).__class__ is tuple)
2629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a + a).__class__ is tuple)
2630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a * 0).__class__ is tuple)
2631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a * 1).__class__ is tuple)
2632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((a * 2).__class__ is tuple)
2633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(a[:].__class__ is tuple)
2634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class madstring(str):
2636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            _rev = None
2637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def rev(self):
2638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if self._rev is not None:
2639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return self._rev
2640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                L = list(self)
2641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                L.reverse()
2642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rev = self.__class__("".join(L))
2643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self._rev
2644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = madstring("abcdefghijklmnopqrstuvwxyz")
2645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, "abcdefghijklmnopqrstuvwxyz")
2646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba"))
2647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz"))
2648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in range(256):
2649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = madstring("".join(map(chr, range(i))))
2650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = s.rev()
2651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            u = t.rev()
2652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(u, s)
2653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = madstring("12345")
2654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(s), "12345")
2655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(str(s).__class__ is str)
2656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        base = "\x00" * 5
2658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = madstring(base)
2659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s, base)
2660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(s), base)
2661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(str(s).__class__ is str)
2662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(s), hash(base))
2663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual({s: 1}[base], 1)
2664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual({base: 1}[s], 1)
2665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((s + "").__class__ is str)
2666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s + "", base)
2667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(("" + s).__class__ is str)
2668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual("" + s, base)
2669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((s * 0).__class__ is str)
2670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s * 0, "")
2671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((s * 1).__class__ is str)
2672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s * 1, base)
2673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((s * 2).__class__ is str)
2674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s * 2, base + base)
2675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s[:].__class__ is str)
2676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s[:], base)
2677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s[0:0].__class__ is str)
2678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s[0:0], "")
2679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.strip().__class__ is str)
2680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.strip(), base)
2681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.lstrip().__class__ is str)
2682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.lstrip(), base)
2683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.rstrip().__class__ is str)
2684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.rstrip(), base)
2685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        identitytab = ''.join([chr(i) for i in range(256)])
2686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.translate(identitytab).__class__ is str)
2687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.translate(identitytab), base)
2688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.translate(identitytab, "x").__class__ is str)
2689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.translate(identitytab, "x"), base)
2690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.translate(identitytab, "\x00"), "")
2691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.replace("x", "x").__class__ is str)
2692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.replace("x", "x"), base)
2693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.ljust(len(s)).__class__ is str)
2694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.ljust(len(s)), base)
2695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.rjust(len(s)).__class__ is str)
2696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.rjust(len(s)), base)
2697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.center(len(s)).__class__ is str)
2698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.center(len(s)), base)
2699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(s.lower().__class__ is str)
2700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(s.lower(), base)
2701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class madunicode(unicode):
2703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            _rev = None
2704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def rev(self):
2705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if self._rev is not None:
2706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return self._rev
2707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                L = list(self)
2708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                L.reverse()
2709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self._rev = self.__class__(u"".join(L))
2710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self._rev
2711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        u = madunicode("ABCDEF")
2712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u, u"ABCDEF")
2713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.rev(), madunicode(u"FEDCBA"))
2714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.rev().rev(), madunicode(u"ABCDEF"))
2715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        base = u"12345"
2716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        u = madunicode(base)
2717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(unicode(u), base)
2718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(unicode(u).__class__ is unicode)
2719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(u), hash(base))
2720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual({u: 1}[base], 1)
2721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual({base: 1}[u], 1)
2722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.strip().__class__ is unicode)
2723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.strip(), base)
2724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.lstrip().__class__ is unicode)
2725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.lstrip(), base)
2726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.rstrip().__class__ is unicode)
2727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.rstrip(), base)
2728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.replace(u"x", u"x").__class__ is unicode)
2729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.replace(u"x", u"x"), base)
2730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.replace(u"xy", u"xy").__class__ is unicode)
2731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.replace(u"xy", u"xy"), base)
2732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.center(len(u)).__class__ is unicode)
2733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.center(len(u)), base)
2734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.ljust(len(u)).__class__ is unicode)
2735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.ljust(len(u)), base)
2736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.rjust(len(u)).__class__ is unicode)
2737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.rjust(len(u)), base)
2738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.lower().__class__ is unicode)
2739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.lower(), base)
2740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.upper().__class__ is unicode)
2741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.upper(), base)
2742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.capitalize().__class__ is unicode)
2743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.capitalize(), base)
2744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u.title().__class__ is unicode)
2745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u.title(), base)
2746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((u + u"").__class__ is unicode)
2747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u + u"", base)
2748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((u"" + u).__class__ is unicode)
2749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u"" + u, base)
2750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((u * 0).__class__ is unicode)
2751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u * 0, u"")
2752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((u * 1).__class__ is unicode)
2753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u * 1, base)
2754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue((u * 2).__class__ is unicode)
2755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u * 2, base + base)
2756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u[:].__class__ is unicode)
2757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u[:], base)
2758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(u[0:0].__class__ is unicode)
2759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(u[0:0], u"")
2760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class sublist(list):
2762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = sublist(range(5))
2764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, range(5))
2765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.append("hello")
2766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, range(5) + ["hello"])
2767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a[5] = 5
2768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, range(6))
2769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.extend(range(6, 20))
2770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, range(20))
2771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a[-5:] = []
2772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, range(15))
2773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del a[10:15]
2774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(a), 10)
2775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, range(10))
2776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(list(a), range(10))
2777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[0], 0)
2778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[9], 9)
2779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[-10], 0)
2780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[-1], 9)
2781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a[:5], range(5))
2782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class CountedInput(file):
2784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """Counts lines read by self.readline().
2785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.lineno is the 0-based ordinal of the last line read, up to
2787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            a maximum of one greater than the number of lines in the file.
2788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.ateof is true if and only if the final "" line has been read,
2790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            at which point self.lineno stops incrementing, and further calls
2791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            to readline() continue to return "".
2792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """
2793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            lineno = 0
2795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ateof = 0
2796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def readline(self):
2797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if self.ateof:
2798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return ""
2799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = file.readline(self)
2800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # Next line works too.
2801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # s = super(CountedInput, self).readline()
2802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.lineno += 1
2803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if s == "":
2804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.ateof = 1
2805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return s
2806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = file(name=test_support.TESTFN, mode='w')
2808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lines = ['a\n', 'b\n', 'c\n']
2809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
2810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            f.writelines(lines)
2811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            f.close()
2812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            f = CountedInput(test_support.TESTFN)
2813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]):
2814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                got = f.readline()
2815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(expected, got)
2816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(f.lineno, i)
2817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(f.ateof, (i > len(lines)))
2818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            f.close()
2819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
2820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
2821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                f.close()
2822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
2823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
2824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            test_support.unlink(test_support.TESTFN)
2825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_keywords(self):
2827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing keyword args to basic type constructors ...
2828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(int(x=1), 1)
2829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(float(x=2), 2.0)
2830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(x=3), 3L)
2831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(complex(imag=42, real=666), complex(666, 42))
2832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(object=500), '500')
2833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(unicode(string='abc', errors='strict'), u'abc')
2834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(tuple(sequence=range(3)), (0, 1, 2))
2835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(list(sequence=(0, 1, 2)), range(3))
2836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # note: as of Python 2.3, dict() no longer has an "items" keyword arg
2837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for constructor in (int, float, long, complex, str, unicode,
2839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            tuple, list, file):
2840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
2841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                constructor(bogus_keyword_arg=1)
2842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
2843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
2844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
2845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("expected TypeError from bogus keyword argument to %r"
2846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            % constructor)
2847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_str_subclass_as_dict_key(self):
2849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing a str subclass used as dict key ..
2850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class cistr(str):
2852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """Sublcass of str that computes __eq__ case-insensitively.
2853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            Also computes a hash code of the string in canonical form.
2855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """
2856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, value):
2858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.canonical = value.lower()
2859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.hashcode = hash(self.canonical)
2860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __eq__(self, other):
2862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if not isinstance(other, cistr):
2863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    other = cistr(other)
2864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.canonical == other.canonical
2865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __hash__(self):
2867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.hashcode
2868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(cistr('ABC'), 'abc')
2870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual('aBc', cistr('ABC'))
2871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(cistr('ABC')), 'ABC')
2872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3}
2874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d[cistr('one')], 1)
2875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d[cistr('tWo')], 2)
2876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d[cistr('THrEE')], 3)
2877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn(cistr('ONe'), d)
2878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.get(cistr('thrEE')), 3)
2879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_classic_comparisons(self):
2881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing classic comparisons...
2882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class classic:
2883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for base in (classic, int, object):
2886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(base):
2887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __init__(self, value):
2888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.value = int(value)
2889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __cmp__(self, other):
2890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, C):
2891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return cmp(self.value, other.value)
2892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, int) or isinstance(other, long):
2893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return cmp(self.value, other)
2894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return NotImplemented
2895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __hash__ = None # Silence Py3k warning
2896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c1 = C(1)
2898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c2 = C(2)
2899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c3 = C(3)
2900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(c1, 1)
2901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c = {1: c1, 2: c2, 3: c3}
2902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for x in 1, 2, 3:
2903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for y in 1, 2, 3:
2904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertTrue(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
2905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for op in "<", "<=", "==", "!=", ">", ">=":
2906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        self.assertTrue(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
2907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                               "x=%d, y=%d" % (x, y))
2908edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertTrue(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y))
2909edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertTrue(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
2910edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2911edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_rich_comparisons(self):
2912edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing rich comparisons...
2913edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Z(complex):
2914edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2915edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        z = Z(1)
2916edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(z, 1+0j)
2917edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(1+0j, z)
2918edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class ZZ(complex):
2919edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __eq__(self, other):
2920edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
2921edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return abs(self - other) <= 1e-6
2922edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                except:
2923edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return NotImplemented
2924edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __hash__ = None # Silence Py3k warning
2925edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        zz = ZZ(1.0000003)
2926edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(zz, 1+0j)
2927edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(1+0j, zz)
2928edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2929edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class classic:
2930edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
2931edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for base in (classic, int, object, list):
2932edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(base):
2933edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __init__(self, value):
2934edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.value = int(value)
2935edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __cmp__(self_, other):
2936edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.fail("shouldn't call __cmp__")
2937edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __hash__ = None # Silence Py3k warning
2938edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __eq__(self, other):
2939edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, C):
2940edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value == other.value
2941edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, int) or isinstance(other, long):
2942edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value == other
2943edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return NotImplemented
2944edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __ne__(self, other):
2945edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, C):
2946edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value != other.value
2947edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, int) or isinstance(other, long):
2948edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value != other
2949edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return NotImplemented
2950edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __lt__(self, other):
2951edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, C):
2952edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value < other.value
2953edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, int) or isinstance(other, long):
2954edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value < other
2955edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return NotImplemented
2956edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __le__(self, other):
2957edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, C):
2958edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value <= other.value
2959edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, int) or isinstance(other, long):
2960edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value <= other
2961edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return NotImplemented
2962edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __gt__(self, other):
2963edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, C):
2964edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value > other.value
2965edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, int) or isinstance(other, long):
2966edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value > other
2967edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return NotImplemented
2968edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __ge__(self, other):
2969edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, C):
2970edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value >= other.value
2971edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if isinstance(other, int) or isinstance(other, long):
2972edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return self.value >= other
2973edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return NotImplemented
2974edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c1 = C(1)
2975edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c2 = C(2)
2976edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c3 = C(3)
2977edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(c1, 1)
2978edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            c = {1: c1, 2: c2, 3: c3}
2979edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for x in 1, 2, 3:
2980edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for y in 1, 2, 3:
2981edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for op in "<", "<=", "==", "!=", ">", ">=":
2982edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        self.assertTrue(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
2983edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                               "x=%d, y=%d" % (x, y))
2984edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        self.assertTrue(eval("c[x] %s y" % op) == eval("x %s y" % op),
2985edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                               "x=%d, y=%d" % (x, y))
2986edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        self.assertTrue(eval("x %s c[y]" % op) == eval("x %s y" % op),
2987edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                               "x=%d, y=%d" % (x, y))
2988edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
2989edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_coercions(self):
2990edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing coercions...
2991edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class I(int): pass
2992edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(I(0), 0)
2993edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0, I(0))
2994edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class L(long): pass
2995edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(L(0), 0)
2996edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(L(0), 0L)
2997edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0, L(0))
2998edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0L, L(0))
2999edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class F(float): pass
3000edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(F(0), 0)
3001edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(F(0), 0L)
3002edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(F(0), 0.)
3003edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0, F(0))
3004edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0L, F(0))
3005edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0., F(0))
3006edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(complex): pass
3007edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(C(0), 0)
3008edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(C(0), 0L)
3009edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(C(0), 0.)
3010edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(C(0), 0j)
3011edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0, C(0))
3012edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0L, C(0))
3013edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0., C(0))
3014edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coerce(0j, C(0))
3015edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3016edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_descrdoc(self):
3017edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing descriptor doc strings...
3018edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def check(descr, what):
3019edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(descr.__doc__, what)
3020edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(file.closed, "True if the file is closed") # getset descriptor
3021edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        check(file.name, "file name") # member descriptor
3022edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3023edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_doc_descriptor(self):
3024edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __doc__ descriptor...
3025edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF bug 542984
3026edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class DocDescr(object):
3027edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __get__(self, object, otype):
3028edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if object:
3029edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    object = object.__class__.__name__ + ' instance'
3030edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if otype:
3031edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    otype = otype.__name__
3032edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 'object=%s; type=%s' % (object, otype)
3033edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class OldClass:
3034edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __doc__ = DocDescr()
3035edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class NewClass(object):
3036edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __doc__ = DocDescr()
3037edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(OldClass.__doc__, 'object=None; type=OldClass')
3038edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(OldClass().__doc__, 'object=OldClass instance; type=OldClass')
3039edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(NewClass.__doc__, 'object=None; type=NewClass')
3040edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(NewClass().__doc__, 'object=NewClass instance; type=NewClass')
3041edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3042edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_set_class(self):
3043edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __class__ assignment...
3044edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object): pass
3045edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(object): pass
3046edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(object): pass
3047edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class F(D, E): pass
3048edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for cls in C, D, E, F:
3049edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for cls2 in C, D, E, F:
3050edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x = cls()
3051edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x.__class__ = cls2
3052edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertTrue(x.__class__ is cls2)
3053edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x.__class__ = cls
3054edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertTrue(x.__class__ is cls)
3055edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def cant(x, C):
3056edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3057edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x.__class__ = C
3058edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
3059edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3060edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3061edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("shouldn't allow %r.__class__ = %r" % (x, C))
3062edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3063edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                delattr(x, "__class__")
3064edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except (TypeError, AttributeError):
3065edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3066edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3067edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("shouldn't allow del %r.__class__" % x)
3068edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(C(), list)
3069edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(list(), C)
3070edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(C(), 1)
3071edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(C(), object)
3072edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(object(), list)
3073edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(list(), object)
3074edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Int(int): __slots__ = []
3075edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(2, Int)
3076edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(Int(), int)
3077edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(True, int)
3078edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(2, bool)
3079edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        o = object()
3080edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(o, type(1))
3081edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(o, type(None))
3082edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del o
3083edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class G(object):
3084edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["a", "b"]
3085edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class H(object):
3086edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["b", "a"]
3087edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3088edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            unicode
3089edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except NameError:
3090edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class I(object):
3091edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = ["a", "b"]
3092edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3093edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class I(object):
3094edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = [unicode("a"), unicode("b")]
3095edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class J(object):
3096edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["c", "b"]
3097edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class K(object):
3098edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["a", "b", "d"]
3099edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class L(H):
3100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["e"]
3101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M(I):
3102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["e"]
3103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class N(J):
3104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["__weakref__"]
3105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class P(J):
3106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["__dict__"]
3107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Q(J):
3108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class R(J):
3110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ["__dict__", "__weakref__"]
3111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for cls, cls2 in ((G, H), (G, I), (I, H), (Q, R), (R, Q)):
3113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = cls()
3114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.a = 1
3115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.__class__ = cls2
3116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(x.__class__ is cls2,
3117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                   "assigning %r as __class__ for %r silently failed" % (cls2, x))
3118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.a, 1)
3119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.__class__ = cls
3120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(x.__class__ is cls,
3121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                   "assigning %r as __class__ for %r silently failed" % (cls, x))
3122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.a, 1)
3123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for cls in G, J, K, L, M, N, P, R, list, Int:
3124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for cls2 in G, J, K, L, M, N, P, R, list, Int:
3125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if cls is cls2:
3126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    continue
3127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                cant(cls(), cls2)
3128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Issue5283: when __class__ changes in __del__, the wrong
3130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # type gets DECREF'd.
3131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class O(object):
3132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
3134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __del__(self):
3135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__class__ = O
3136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = [A() for x in range(100)]
3137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del l
3138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_set_dict(self):
3140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __dict__ assignment...
3141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object): pass
3142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
3143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.__dict__ = {'b': 1}
3144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.b, 1)
3145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def cant(x, dict):
3146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x.__dict__ = dict
3148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except (AttributeError, TypeError):
3149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("shouldn't allow %r.__dict__ = %r" % (x, dict))
3152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(a, None)
3153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(a, [])
3154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cant(a, 1)
3155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del a.__dict__ # Deleting __dict__ is allowed
3156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Base(object):
3158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def verify_dict_readonly(x):
3160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """
3161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x has to be an instance of a class inheriting from Base.
3162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            """
3163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cant(x, {})
3164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del x.__dict__
3166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except (AttributeError, TypeError):
3167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("shouldn't allow del %r.__dict__" % x)
3170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dict_descr = Base.__dict__["__dict__"]
3171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                dict_descr.__set__(x, {})
3173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except (AttributeError, TypeError):
3174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("dict_descr allowed access to %r's dict" % x)
3177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Classes don't allow __dict__ assignment and have readonly dicts
3179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Meta1(type, Base):
3180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Meta2(Base, type):
3182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(object):
3184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = Meta1
3185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(object):
3186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = Meta2
3187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for cls in C, D, E:
3188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            verify_dict_readonly(cls)
3189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class_dict = cls.__dict__
3190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class_dict["spam"] = "eggs"
3192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
3193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("%r's __dict__ can be modified" % cls)
3196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Modules also disallow __dict__ assignment
3198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Module1(types.ModuleType, Base):
3199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Module2(Base, types.ModuleType):
3201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for ModuleType in Module1, Module2:
3203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            mod = ModuleType("spam")
3204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            verify_dict_readonly(mod)
3205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            mod.__dict__["spam"] = "eggs"
3206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Exception's __dict__ can be replaced, but not deleted
3208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # (at least not any more than regular exception's __dict__ can
3209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # be deleted; on CPython it is not the case, whereas on PyPy they
3210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # can, just like any other new-style instance's __dict__.)
3211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def can_delete_dict(e):
3212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del e.__dict__
3214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except (TypeError, AttributeError):
3215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return False
3216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return True
3218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Exception1(Exception, Base):
3219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Exception2(Base, Exception):
3221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for ExceptionType in Exception, Exception1, Exception2:
3223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            e = ExceptionType()
3224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            e.__dict__ = {"a": 1}
3225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(e.a, 1)
3226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(can_delete_dict(e), can_delete_dict(ValueError()))
3227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_pickles(self):
3229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing pickling and copying new-style classes and objects...
3230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import pickle, cPickle
3231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def sorteditems(d):
3233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            L = d.items()
3234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            L.sort()
3235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return L
3236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        global C
3238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, a, b):
3240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                super(C, self).__init__()
3241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.a = a
3242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.b = b
3243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
3244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C(%r, %r)" % (self.a, self.b)
3245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        global C1
3247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C1(list):
3248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(cls, a, b):
3249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return super(C1, cls).__new__(cls)
3250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getnewargs__(self):
3251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return (self.a, self.b)
3252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, a, b):
3253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.a = a
3254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.b = b
3255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
3256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C1(%r, %r)<%r>" % (self.a, self.b, list(self))
3257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        global C2
3259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C2(int):
3260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(cls, a, b, val=0):
3261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return super(C2, cls).__new__(cls, val)
3262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getnewargs__(self):
3263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return (self.a, self.b, int(self))
3264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, a, b, val=0):
3265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.a = a
3266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.b = b
3267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
3268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C2(%r, %r)<%r>" % (self.a, self.b, int(self))
3269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        global C3
3271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C3(object):
3272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, foo):
3273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.foo = foo
3274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getstate__(self):
3275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.foo
3276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setstate__(self, foo):
3277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.foo = foo
3278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        global C4classic, C4
3280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C4classic: # classic
3281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C4(C4classic, object): # mixed inheritance
3283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for p in pickle, cPickle:
3286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for bin in 0, 1:
3287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for cls in C, C1, C2:
3288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    s = p.dumps(cls, bin)
3289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    cls2 = p.loads(s)
3290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertTrue(cls2 is cls)
3291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                a = C1(1, 2); a.append(42); a.append(24)
3293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                b = C2("hello", "world", 42)
3294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = p.dumps((a, b), bin)
3295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x, y = p.loads(s)
3296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(x.__class__, a.__class__)
3297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(sorteditems(x.__dict__), sorteditems(a.__dict__))
3298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(y.__class__, b.__class__)
3299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(sorteditems(y.__dict__), sorteditems(b.__dict__))
3300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(repr(x), repr(a))
3301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(repr(y), repr(b))
3302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # Test for __getstate__ and __setstate__ on new style class
3303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                u = C3(42)
3304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = p.dumps(u, bin)
3305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                v = p.loads(s)
3306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(u.__class__, v.__class__)
3307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(u.foo, v.foo)
3308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # Test for picklability of hybrid class
3309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                u = C4()
3310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                u.foo = 42
3311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = p.dumps(u, bin)
3312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                v = p.loads(s)
3313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(u.__class__, v.__class__)
3314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(u.foo, v.foo)
3315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing copy.deepcopy()
3317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import copy
3318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for cls in C, C1, C2:
3319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cls2 = copy.deepcopy(cls)
3320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(cls2 is cls)
3321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C1(1, 2); a.append(42); a.append(24)
3323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b = C2("hello", "world", 42)
3324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x, y = copy.deepcopy((a, b))
3325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.__class__, a.__class__)
3326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sorteditems(x.__dict__), sorteditems(a.__dict__))
3327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(y.__class__, b.__class__)
3328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(sorteditems(y.__dict__), sorteditems(b.__dict__))
3329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(x), repr(a))
3330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(y), repr(b))
3331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_pickle_slots(self):
3333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing pickling of classes with __slots__ ...
3334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import pickle, cPickle
3335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Pickling of classes with __slots__ but without __getstate__ should fail
3336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        global B, C, D, E
3337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object):
3338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for base in [object, B]:
3340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(base):
3341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = ['a']
3342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class D(C):
3343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pickle.dumps(C())
3346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
3347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("should fail: pickle C instance - %s" % base)
3350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                cPickle.dumps(C())
3352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
3353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("should fail: cPickle C instance - %s" % base)
3356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pickle.dumps(C())
3358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
3359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("should fail: pickle D instance - %s" % base)
3362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
3363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                cPickle.dumps(D())
3364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
3365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
3367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("should fail: cPickle D instance - %s" % base)
3368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Give C a nice generic __getstate__ and __setstate__
3369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class C(base):
3370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = ['a']
3371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __getstate__(self):
3372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    try:
3373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        d = self.__dict__.copy()
3374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    except AttributeError:
3375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        d = {}
3376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for cls in self.__class__.__mro__:
3377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        for sn in cls.__dict__.get('__slots__', ()):
3378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            try:
3379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                d[sn] = getattr(self, sn)
3380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            except AttributeError:
3381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                pass
3382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return d
3383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __setstate__(self, d):
3384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for k, v in d.items():
3385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        setattr(self, k, v)
3386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class D(C):
3387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Now it should work
3389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = C()
3390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = pickle.loads(pickle.dumps(x))
3391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(hasattr(y, 'a'), 0)
3392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = cPickle.loads(cPickle.dumps(x))
3393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(hasattr(y, 'a'), 0)
3394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.a = 42
3395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = pickle.loads(pickle.dumps(x))
3396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.a, 42)
3397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = cPickle.loads(cPickle.dumps(x))
3398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.a, 42)
3399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = D()
3400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.a = 42
3401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.b = 100
3402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = pickle.loads(pickle.dumps(x))
3403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.a + y.b, 142)
3404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = cPickle.loads(cPickle.dumps(x))
3405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.a + y.b, 142)
3406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # A subclass that adds a slot should also work
3407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class E(C):
3408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                __slots__ = ['b']
3409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = E()
3410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.a = 42
3411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.b = "foo"
3412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = pickle.loads(pickle.dumps(x))
3413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.a, x.a)
3414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.b, x.b)
3415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = cPickle.loads(cPickle.dumps(x))
3416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.a, x.a)
3417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.b, x.b)
3418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_binary_operator_override(self):
3420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing overrides of binary operations...
3421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class I(int):
3422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
3423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "I(%r)" % int(self)
3424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __add__(self, other):
3425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return I(int(self) + int(other))
3426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __radd__ = __add__
3427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __pow__(self, other, mod=None):
3428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if mod is None:
3429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return I(pow(int(self), int(other)))
3430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
3431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return I(pow(int(self), int(other), int(mod)))
3432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __rpow__(self, other, mod=None):
3433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if mod is None:
3434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return I(pow(int(other), int(self), mod))
3435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
3436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return I(pow(int(other), int(self), int(mod)))
3437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(I(1) + I(2)), "I(3)")
3439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(I(1) + 2), "I(3)")
3440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(1 + I(2)), "I(3)")
3441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(I(2) ** I(3)), "I(8)")
3442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(2 ** I(3)), "I(8)")
3443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(I(2) ** 3), "I(8)")
3444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(pow(I(2), I(3), I(5))), "I(3)")
3445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class S(str):
3446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __eq__(self, other):
3447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.lower() == other.lower()
3448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __hash__ = None # Silence Py3k warning
3449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_subclass_propagation(self):
3451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing propagation of slot functions to subclasses...
3452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
3453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A):
3455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A):
3457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(B, C):
3459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
3461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        orig_hash = hash(d) # related to id(d) in platform-dependent ways
3462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A.__hash__ = lambda self: 42
3463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(d), 42)
3464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__hash__ = lambda self: 314
3465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(d), 314)
3466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        B.__hash__ = lambda self: 144
3467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(d), 144)
3468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        D.__hash__ = lambda self: 100
3469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(d), 100)
3470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        D.__hash__ = None
3471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, hash, d)
3472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del D.__hash__
3473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(d), 144)
3474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        B.__hash__ = None
3475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, hash, d)
3476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del B.__hash__
3477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(d), 314)
3478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__hash__ = None
3479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, hash, d)
3480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del C.__hash__
3481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(d), 42)
3482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A.__hash__ = None
3483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, hash, d)
3484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del A.__hash__
3485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(d), orig_hash)
3486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d.foo = 42
3487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d.bar = 42
3488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, 42)
3489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.bar, 42)
3490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def __getattribute__(self, name):
3491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if name == "foo":
3492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 24
3493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return object.__getattribute__(self, name)
3494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A.__getattribute__ = __getattribute__
3495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, 24)
3496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.bar, 42)
3497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def __getattr__(self, name):
3498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if name in ("spam", "foo", "bar"):
3499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "hello"
3500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            raise AttributeError, name
3501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        B.__getattr__ = __getattr__
3502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.spam, "hello")
3503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, 24)
3504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.bar, 42)
3505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del A.__getattribute__
3506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, 42)
3507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del d.foo
3508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, "hello")
3509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.bar, 42)
3510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del B.__getattr__
3511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            d.foo
3513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except AttributeError:
3514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("d.foo should be undefined now")
3517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test a nasty bug in recurse_down_subclasses()
3519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
3520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(A):
3522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del B
3524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
3525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A.__setitem__ = lambda *a: None # crash
3526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_buffer_inheritance(self):
3528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing that buffer interface is inherited ...
3529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import binascii
3531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF bug [#470040] ParseTuple t# vs subclasses.
3532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MyStr(str):
3534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        base = 'abc'
3536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = MyStr(base)
3537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # b2a_hex uses the buffer interface to get its argument's value, via
3538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # PyArg_ParseTuple 't#' code.
3539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base))
3540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # It's not clear that unicode will continue to support the character
3542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # buffer interface, and this test will fail if that's taken away.
3543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MyUni(unicode):
3544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        base = u'abc'
3546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = MyUni(base)
3547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base))
3548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MyInt(int):
3550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = MyInt(42)
3552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            binascii.b2a_hex(m)
3554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail('subclass of int should not have a buffer interface')
3555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
3556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_str_of_str_subclass(self):
3559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __str__ defined in subclass of str ...
3560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import binascii
3561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import cStringIO
3562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class octetstring(str):
3564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __str__(self):
3565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return binascii.b2a_hex(self)
3566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __repr__(self):
3567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self + " repr"
3568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        o = octetstring('A')
3570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(o), octetstring)
3571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(str(o)), str)
3572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(repr(o)), str)
3573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(ord(o), 0x41)
3574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str(o), '41')
3575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(repr(o), 'A repr')
3576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(o.__str__(), '41')
3577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(o.__repr__(), 'A repr')
3578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        capture = cStringIO.StringIO()
3580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Calling str() or not exercises different internal paths.
3581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print >> capture, o
3582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print >> capture, str(o)
3583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(capture.getvalue(), '41\n41\n')
3584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        capture.close()
3585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_keyword_arguments(self):
3587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing keyword arguments to __init__, __call__...
3588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def f(a): return a
3589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(f.__call__(a=42), 42)
3590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = []
3591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        list.__init__(a, sequence=[0, 1, 2])
3592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, [0, 1, 2])
3593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursive_call(self):
3595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing recursive __call__() by setting to instance of class...
3596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
3597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A.__call__ = A()
3600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            A()()
3602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except RuntimeError:
3603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Recursion limit should have been reached for __call__()")
3606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_delete_hook(self):
3608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __del__ hook...
3609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        log = []
3610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __del__(self):
3612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                log.append(1)
3613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
3614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(log, [])
3615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del c
3616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
3617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(log, [1])
3618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(object): pass
3620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
3621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: del d[0]
3622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError: pass
3623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("invalid del() didn't raise TypeError")
3624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_hash_inheritance(self):
3626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing hash of mutable subclasses...
3627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class mydict(dict):
3629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = mydict()
3631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            hash(d)
3633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
3634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("hash() of dict subclass should fail")
3637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class mylist(list):
3639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = mylist()
3641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            hash(d)
3643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
3644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("hash() of list subclass should fail")
3647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_str_operations(self):
3649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: 'a' + 5
3650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError: pass
3651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("'' + 5 doesn't raise TypeError")
3652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: ''.split('')
3654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError: pass
3655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("''.split('') doesn't raise ValueError")
3656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: ''.join([0])
3658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError: pass
3659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("''.join([0]) doesn't raise TypeError")
3660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: ''.rindex('5')
3662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError: pass
3663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("''.rindex('5') doesn't raise ValueError")
3664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: '%(n)s' % None
3666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError: pass
3667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("'%(n)s' % None doesn't raise TypeError")
3668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: '%(n' % {}
3670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError: pass
3671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("'%(n' % {} '' doesn't raise ValueError")
3672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: '%*s' % ('abc')
3674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError: pass
3675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("'%*s' % ('abc') doesn't raise TypeError")
3676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: '%*.*s' % ('abc', 5)
3678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError: pass
3679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("'%*.*s' % ('abc', 5) doesn't raise TypeError")
3680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: '%s' % (1, 2)
3682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError: pass
3683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("'%s' % (1, 2) doesn't raise TypeError")
3684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try: '%' % None
3686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError: pass
3687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else: self.fail("'%' % None doesn't raise ValueError")
3688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual('534253'.isdigit(), 1)
3690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual('534253x'.isdigit(), 0)
3691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual('%c' % 5, '\x05')
3692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual('%c' % '5', '5')
3693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_deepcopy_recursive(self):
3695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing deepcopy of recursive objects...
3696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Node:
3697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = Node()
3699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b = Node()
3700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.b = b
3701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b.a = a
3702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        z = deepcopy(a) # This blew up before
3703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_unintialized_modules(self):
3705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing uninitialized module objects...
3706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from types import ModuleType as M
3707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m = M.__new__(M)
3708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        str(m)
3709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hasattr(m, "__name__"), 0)
3710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hasattr(m, "__file__"), 0)
3711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hasattr(m, "foo"), 0)
3712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(m.__dict__)   # None or {} are both reasonable answers
3713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        m.foo = 1
3714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(m.__dict__, {"foo": 1})
3715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_funny_new(self):
3717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __new__ returning something unexpected...
3718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(cls, arg):
3720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if isinstance(arg, str): return [1, 2, 3]
3721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                elif isinstance(arg, int): return object.__new__(D)
3722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else: return object.__new__(cls)
3723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
3724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, arg):
3725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.foo = arg
3726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C("1"), [1, 2, 3])
3727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D("1"), [1, 2, 3])
3728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D(None)
3729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, None)
3730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = C(1)
3731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(isinstance(d, D), True)
3732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, 1)
3733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D(1)
3734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(isinstance(d, D), True)
3735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.foo, 1)
3736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_imul_bug(self):
3738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing for __imul__ problems...
3739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF bug 544647
3740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __imul__(self, other):
3742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return (self, other)
3743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = C()
3744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y = x
3745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y *= 1.0
3746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(y, (x, 1.0))
3747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y = x
3748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y *= 2
3749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(y, (x, 2))
3750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y = x
3751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y *= 3L
3752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(y, (x, 3L))
3753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y = x
3754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y *= 1L<<100
3755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(y, (x, 1L<<100))
3756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y = x
3757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y *= None
3758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(y, (x, None))
3759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y = x
3760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y *= "foo"
3761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(y, (x, "foo"))
3762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_copy_setstate(self):
3764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing that copy.*copy() correctly uses __setstate__...
3765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import copy
3766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, foo=None):
3768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.foo = foo
3769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__foo = foo
3770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def setfoo(self, foo=None):
3771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.foo = foo
3772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def getfoo(self):
3773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self.__foo
3774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getstate__(self):
3775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return [self.foo]
3776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setstate__(self_, lst):
3777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(len(lst), 1)
3778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self_.__foo = self_.foo = lst[0]
3779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C(42)
3780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.setfoo(24)
3781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.foo, 24)
3782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a.getfoo(), 42)
3783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b = copy.copy(a)
3784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(b.foo, 24)
3785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(b.getfoo(), 24)
3786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b = copy.deepcopy(a)
3787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(b.foo, 24)
3788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(b.getfoo(), 24)
3789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slices(self):
3791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing cases with slices and overridden __getitem__ ...
3792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Strings
3794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual("hello"[:4], "hell")
3795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual("hello"[slice(4)], "hell")
3796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(str.__getitem__("hello", slice(4)), "hell")
3797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class S(str):
3798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, x):
3799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return str.__getitem__(self, x)
3800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(S("hello")[:4], "hell")
3801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(S("hello")[slice(4)], "hell")
3802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(S("hello").__getitem__(slice(4)), "hell")
3803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Tuples
3804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((1,2,3)[:2], (1,2))
3805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((1,2,3)[slice(2)], (1,2))
3806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(tuple.__getitem__((1,2,3), slice(2)), (1,2))
3807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class T(tuple):
3808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, x):
3809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return tuple.__getitem__(self, x)
3810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(T((1,2,3))[:2], (1,2))
3811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(T((1,2,3))[slice(2)], (1,2))
3812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(T((1,2,3)).__getitem__(slice(2)), (1,2))
3813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Lists
3814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual([1,2,3][:2], [1,2])
3815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual([1,2,3][slice(2)], [1,2])
3816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(list.__getitem__([1,2,3], slice(2)), [1,2])
3817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class L(list):
3818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getitem__(self, x):
3819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return list.__getitem__(self, x)
3820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(L([1,2,3])[:2], [1,2])
3821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(L([1,2,3])[slice(2)], [1,2])
3822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(L([1,2,3]).__getitem__(slice(2)), [1,2])
3823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Now do lists and __setitem__
3824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = L([1,2,3])
3825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a[slice(1, 3)] = [3,2]
3826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, [1,3,2])
3827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a[slice(0, 2, 1)] = [3,1]
3828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, [3,1,2])
3829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.__setitem__(slice(1, 3), [2,1])
3830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, [3,2,1])
3831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a.__setitem__(slice(0, 2, 1), [2,3])
3832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a, [2,3,1])
3833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_subtype_resurrection(self):
3835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing resurrection of new-style instance...
3836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            container = []
3839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __del__(self):
3841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # resurrect the instance
3842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                C.container.append(self)
3843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
3845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c.attr = 42
3846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # The most interesting thing here is whether this blows up, due to
3848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # flawed GC tracking logic in typeobject.c's call_finalizer() (a 2.2.1
3849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # bug).
3850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del c
3851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # If that didn't blow up, it's also interesting to see whether clearing
3853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # the last container slot works: that will attempt to delete c again,
3854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # which will cause c to get appended back to the container again
3855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # "during" the del.  (On non-CPython implementations, however, __del__
3856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # is typically not called again.)
3857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
3858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(C.container), 1)
3859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del C.container[-1]
3860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if test_support.check_impl_detail():
3861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            test_support.gc_collect()
3862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(len(C.container), 1)
3863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(C.container[-1].attr, 42)
3864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Make c mortal again, so that the test framework with -l doesn't report
3866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # it as a leak.
3867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del C.__del__
3868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slots_trash(self):
3870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing slot trash...
3871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Deallocating deeply nested slotted trash caused stack overflows
3872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class trash(object):
3873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__ = ['x']
3874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, x):
3875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.x = x
3876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        o = None
3877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in xrange(50000):
3878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            o = trash(o)
3879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del o
3880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_slots_multiple_inheritance(self):
3882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF bug 575229, multiple inheritance w/ slots dumps core
3883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
3884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__=()
3885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object):
3886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A,B) :
3888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __slots__=()
3889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if test_support.check_impl_detail():
3890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(C.__basicsize__, B.__basicsize__)
3891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(C, '__dict__'))
3892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(hasattr(C, '__weakref__'))
3893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C().x = 2
3894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_rmul(self):
3896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing correct invocation of __rmul__...
3897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF patch 592646
3898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __mul__(self, other):
3900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "mul"
3901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __rmul__(self, other):
3902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "rmul"
3903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
3904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a*2, "mul")
3905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(a*2.2, "mul")
3906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(2*a, "rmul")
3907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(2.2*a, "rmul")
3908edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3909edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_ipow(self):
3910edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing correct invocation of __ipow__...
3911edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # [SF bug 620179]
3912edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3913edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __ipow__(self, other):
3914edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
3915edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
3916edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a **= 2
3917edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3918edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_mutable_bases(self):
3919edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing mutable bases...
3920edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3921edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # stuff that should work:
3922edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
3923edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3924edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C2(object):
3925edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattribute__(self, attr):
3926edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if attr == 'a':
3927edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return 2
3928edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
3929edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return super(C2, self).__getattribute__(attr)
3930edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self):
3931edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 1
3932edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
3933edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3934edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(D):
3935edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3936edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = D()
3937edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        e = E()
3938edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        D.__bases__ = (C,)
3939edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        D.__bases__ = (C2,)
3940edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.meth(), 1)
3941edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(e.meth(), 1)
3942edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.a, 2)
3943edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(e.a, 2)
3944edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C2.__subclasses__(), [D])
3945edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3946edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3947edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            del D.__bases__
3948edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except (TypeError, AttributeError):
3949edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3950edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3951edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't be able to delete .__bases__")
3952edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3953edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3954edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            D.__bases__ = ()
3955edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError, msg:
3956edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if str(msg) == "a new-style class can't have only classic bases":
3957edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("wrong error message for .__bases__ = ()")
3958edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3959edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't be able to set .__bases__ to ()")
3960edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3961edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3962edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            D.__bases__ = (D,)
3963edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
3964edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3965edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3966edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # actually, we'll have crashed by here...
3967edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't be able to create inheritance cycles")
3968edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3969edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3970edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            D.__bases__ = (C, C)
3971edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
3972edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3973edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3974edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("didn't detect repeated base classes")
3975edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3976edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3977edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            D.__bases__ = (E,)
3978edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
3979edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3980edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3981edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't be able to create inheritance cycles")
3982edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3983edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # let's throw a classic class into the mix:
3984edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Classic:
3985edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth2(self):
3986edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 3
3987edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3988edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        D.__bases__ = (C, Classic)
3989edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3990edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(d.meth2(), 3)
3991edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(e.meth2(), 3)
3992edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
3993edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            d.a
3994edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except AttributeError:
3995edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
3996edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
3997edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("attribute should have vanished")
3998edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3999edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4000edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            D.__bases__ = (Classic,)
4001edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4002edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4003edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4004edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("new-style class must have a new-style base")
4005edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4006edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_builtin_bases(self):
4007edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Make sure all the builtin types can have their base queried without
4008edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # segfaulting. See issue #5787.
4009edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        builtin_types = [tp for tp in __builtin__.__dict__.itervalues()
4010edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         if isinstance(tp, type)]
4011edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for tp in builtin_types:
4012edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            object.__getattribute__(tp, "__bases__")
4013edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if tp is not object:
4014edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(len(tp.__bases__), 1, tp)
4015edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4016edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class L(list):
4017edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4018edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4019edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4020edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4021edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4022edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
4023edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4024edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4025edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4026edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            L.__bases__ = (dict,)
4027edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4028edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4029edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4030edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't turn list subclass into dict subclass")
4031edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4032edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4033edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            list.__bases__ = (dict,)
4034edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4035edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4036edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4037edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't be able to assign to list.__bases__")
4038edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4039edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4040edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            D.__bases__ = (C, list)
4041edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4042edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4043edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4044edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            assert 0, "best_base calculation found wanting"
4045edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4046edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4047edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_mutable_bases_with_failing_mro(self):
4048edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing mutable bases with failing mro...
4049edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class WorkOnce(type):
4050edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __new__(self, name, bases, ns):
4051edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.flag = 0
4052edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return super(WorkOnce, self).__new__(WorkOnce, name, bases, ns)
4053edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def mro(self):
4054edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if self.flag > 0:
4055edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise RuntimeError, "bozo"
4056edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
4057edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.flag += 1
4058edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return type.mro(self)
4059edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4060edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class WorkAlways(type):
4061edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def mro(self):
4062edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # this is here to make sure that .mro()s aren't called
4063edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # with an exception set (which was possible at one point).
4064edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # An error message will be printed in a debug build.
4065edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # What's a good way to test for this?
4066edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return type.mro(self)
4067edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4068edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4069edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4070edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4071edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C2(object):
4072edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4073edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4074edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
4075edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4076edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4077edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(D):
4078edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4079edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4080edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class F(D):
4081edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = WorkOnce
4082edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4083edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class G(D):
4084edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = WorkAlways
4085edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4086edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Immediate subclasses have their mro's adjusted in alphabetical
4087edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # order, so E's will get adjusted before adjusting F's fails.  We
4088edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # check here that E's gets restored.
4089edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4090edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        E_mro_before = E.__mro__
4091edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        D_mro_before = D.__mro__
4092edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4093edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4094edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            D.__bases__ = (C2,)
4095edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except RuntimeError:
4096edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(E.__mro__, E_mro_before)
4097edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(D.__mro__, D_mro_before)
4098edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4099edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("exception not propagated")
4100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_mutable_bases_catch_mro_conflict(self):
4102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing mutable bases catch mro conflict...
4103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
4104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object):
4107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(A, B):
4110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(A, B):
4113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(C, D):
4116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            C.__bases__ = (B, A)
4120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("didn't catch MRO conflict")
4124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_mutable_names(self):
4126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing mutable names...
4127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # C.__module__ could be 'test_descr' or '__main__'
4131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        mod = C.__module__
4132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__name__ = 'D'
4134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((C.__module__, C.__name__), (mod, 'D'))
4135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__name__ = 'D.E'
4137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))
4138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_evil_type_name(self):
4140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # A badly placed Py_DECREF in type_set_name led to arbitrary code
4141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # execution while the type structure was not in a sane state, and a
4142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # possible segmentation fault as a result.  See bug #16447.
4143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Nasty(str):
4144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __del__(self):
4145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                C.__name__ = "other"
4146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__name__ = Nasty("abc")
4151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        C.__name__ = "normal"
4152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_subclass_right_op(self):
4154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing correct dispatch of subclass overloading __r<op>__...
4155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # This code tests various cases where right-dispatch of a subclass
4157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # should be preferred over left-dispatch of a base class.
4158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Case 1: subclass of int; this tests code in abstract.c::binary_op1()
4160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(int):
4162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __floordiv__(self, other):
4163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "B.__floordiv__"
4164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __rfloordiv__(self, other):
4165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "B.__rfloordiv__"
4166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(B(1) // 1, "B.__floordiv__")
4168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(1 // B(1), "B.__rfloordiv__")
4169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Case 2: subclass of object; this is just the baseline for case 3
4171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __floordiv__(self, other):
4174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C.__floordiv__"
4175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __rfloordiv__(self, other):
4176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "C.__rfloordiv__"
4177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C() // 1, "C.__floordiv__")
4179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(1 // C(), "C.__rfloordiv__")
4180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Case 3: subclass of new-style class; here it gets interesting
4182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
4184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __floordiv__(self, other):
4185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "D.__floordiv__"
4186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __rfloordiv__(self, other):
4187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "D.__rfloordiv__"
4188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(D() // C(), "D.__floordiv__")
4190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C() // D(), "D.__rfloordiv__")
4191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Case 4: this didn't work right in 2.2.2 and 2.3a1
4193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class E(C):
4195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E.__rfloordiv__, C.__rfloordiv__)
4198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E() // 1, "C.__floordiv__")
4200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(1 // E(), "C.__rfloordiv__")
4201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(E() // C(), "C.__floordiv__")
4202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C() // E(), "C.__floordiv__") # This one would fail
4203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @test_support.impl_detail("testing an internal kind of method object")
4205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_meth_class_get(self):
4206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing __get__ method of METH_CLASS C methods...
4207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Full coverage of descrobject.c::classmethod_get()
4208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Baseline
4210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        arg = [1, 2, 3]
4211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        res = {1: None, 2: None, 3: None}
4212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(dict.fromkeys(arg), res)
4213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual({}.fromkeys(arg), res)
4214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Now get the descriptor
4216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        descr = dict.__dict__["fromkeys"]
4217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # More baseline using the descriptor directly
4219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(descr.__get__(None, dict)(arg), res)
4220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(descr.__get__({})(arg), res)
4221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Now check various error cases
4223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            descr.__get__(None, None)
4225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't have allowed descr.__get__(None, None)")
4229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            descr.__get__(42)
4231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't have allowed descr.__get__(42)")
4235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            descr.__get__(None, 42)
4237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't have allowed descr.__get__(None, 42)")
4241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            descr.__get__(None, int)
4243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("shouldn't have allowed descr.__get__(None, int)")
4247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_isinst_isclass(self):
4249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing proxy isinstance() and isclass()...
4250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Proxy(object):
4251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, obj):
4252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__obj = obj
4253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattribute__(self, name):
4254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if name.startswith("_Proxy__"):
4255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return object.__getattribute__(self, name)
4256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
4257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return getattr(self.__obj, name)
4258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test with a classic class
4259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
4260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
4262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pa = Proxy(a)
4263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(a, C)  # Baseline
4264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(pa, C) # Test
4265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test with a classic subclass
4266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
4267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = D()
4269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pa = Proxy(a)
4270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(a, C)  # Baseline
4271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(pa, C) # Test
4272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test with a new-style class
4273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = C()
4276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pa = Proxy(a)
4277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(a, C)  # Baseline
4278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(pa, C) # Test
4279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test with a new-style subclass
4280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(C):
4281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = D()
4283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pa = Proxy(a)
4284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(a, C)  # Baseline
4285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(pa, C) # Test
4286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_proxy_super(self):
4288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing super() for a proxy object...
4289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Proxy(object):
4290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, obj):
4291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__obj = obj
4292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattribute__(self, name):
4293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if name.startswith("_Proxy__"):
4294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return object.__getattribute__(self, name)
4295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
4296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return getattr(self.__obj, name)
4297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object):
4299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def f(self):
4300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "B.f"
4301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(B):
4303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def f(self):
4304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return super(C, self).f() + "->C.f"
4305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        obj = C()
4307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        p = Proxy(obj)
4308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(C.__dict__["f"](p), "B.f->C.f")
4309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_carloverre(self):
4311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing prohibition of Carlo Verre's hack...
4312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            object.__setattr__(str, "foo", 42)
4314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Carlo Verre __setattr__ succeeded!")
4318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            object.__delattr__(str, "lower")
4320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("Carlo Verre __delattr__ succeeded!")
4324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_weakref_segfault(self):
4326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing weakref segfault...
4327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF 742911
4328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import weakref
4329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Provoker:
4331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, referrent):
4332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.ref = weakref.ref(referrent)
4333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __del__(self):
4335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x = self.ref()
4336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Oops(object):
4338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        o = Oops()
4341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        o.whatever = Provoker(o)
4342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del o
4343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_wrapper_segfault(self):
4345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF 927248: deeply nested wrappers could cause stack overflow
4346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = lambda:None
4347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in xrange(1000000):
4348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            f = f.__call__
4349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = None
4350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_file_fault(self):
4352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing sys.stdout is changed in getattr...
4353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_stdout = sys.stdout
4354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class StdoutGuard:
4355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattr__(self, attr):
4356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                sys.stdout = sys.__stdout__
4357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise RuntimeError("Premature access to sys.stdout.%s" % attr)
4358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sys.stdout = StdoutGuard()
4359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            print "Oops!"
4361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except RuntimeError:
4362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
4364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sys.stdout = test_stdout
4365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_vicious_descriptor_nonsense(self):
4367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing vicious_descriptor_nonsense...
4368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # A potential segfault spotted by Thomas Wouters in mail to
4370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # python-dev 2003-04-17, turned into an example & fixed by Michael
4371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Hudson just less than four months later...
4372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Evil(object):
4374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __hash__(self):
4375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return hash('attr')
4376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __eq__(self, other):
4377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del C.attr
4378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 0
4379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Descr(object):
4381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __get__(self, ob, type=None):
4382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 1
4383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            attr = Descr()
4386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
4388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c.__dict__[Evil()] = 0
4389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.attr, 1)
4391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # this makes a crash more likely:
4392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
4393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hasattr(c, 'attr'), False)
4394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_init(self):
4396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF 1155938
4397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo(object):
4398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
4399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 10
4400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            Foo()
4402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("did not test __init__() for None return")
4406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_method_wrapper(self):
4408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing method-wrapper objects...
4409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # <type 'method-wrapper'> did not support any reflection before 2.5
4410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
4412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l.__add__, l.__add__)
4413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l.__add__, [].__add__)
4414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(l.__add__ != [5].__add__)
4415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(l.__add__ != l.__mul__)
4416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(l.__add__.__name__ == '__add__')
4417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if hasattr(l.__add__, '__self__'):
4418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # CPython
4419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(l.__add__.__self__ is l)
4420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(l.__add__.__objclass__ is list)
4421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Python implementations where [].__add__ is a normal bound method
4423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(l.__add__.im_self is l)
4424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(l.__add__.im_class is list)
4425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(l.__add__.__doc__, list.__add__.__doc__)
4426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            hash(l.__add__)
4428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except TypeError:
4429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
4431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("no TypeError from hash([].__add__)")
4432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = ()
4434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t += (7,)
4435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(t.__add__, (7,).__add__)
4436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hash(t.__add__), hash((7,).__add__))
4437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_not_implemented(self):
4439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing NotImplemented...
4440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # all binary methods should be able to return a NotImplemented
4441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import operator
4442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def specialmethod(self, other):
4444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return NotImplemented
4445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def check(expr, x, y):
4447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
4448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                exec expr in {'x': x, 'y': y, 'operator': operator}
4449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except TypeError:
4450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
4451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
4452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.fail("no TypeError from %r" % (expr,))
4453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        N1 = sys.maxint + 1L    # might trigger OverflowErrors instead of
4455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                # TypeErrors
4456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        N2 = sys.maxint         # if sizeof(int) < sizeof(long), might trigger
4457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                #   ValueErrors instead of TypeErrors
4458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for metaclass in [type, types.ClassType]:
4459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for name, expr, iexpr in [
4460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__add__',      'x + y',                   'x += y'),
4461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__sub__',      'x - y',                   'x -= y'),
4462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__mul__',      'x * y',                   'x *= y'),
4463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__truediv__',  'operator.truediv(x, y)',  None),
4464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__floordiv__', 'operator.floordiv(x, y)', None),
4465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__div__',      'x / y',                   'x /= y'),
4466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__mod__',      'x % y',                   'x %= y'),
4467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__divmod__',   'divmod(x, y)',            None),
4468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__pow__',      'x ** y',                  'x **= y'),
4469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__lshift__',   'x << y',                  'x <<= y'),
4470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__rshift__',   'x >> y',                  'x >>= y'),
4471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__and__',      'x & y',                   'x &= y'),
4472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__or__',       'x | y',                   'x |= y'),
4473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__xor__',      'x ^ y',                   'x ^= y'),
4474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ('__coerce__',   'coerce(x, y)',            None)]:
4475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if name == '__coerce__':
4476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    rname = name
4477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
4478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    rname = '__r' + name[2:]
4479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                A = metaclass('A', (), {name: specialmethod})
4480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                B = metaclass('B', (), {rname: specialmethod})
4481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                a = A()
4482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                b = B()
4483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(expr, a, a)
4484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(expr, a, b)
4485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(expr, b, a)
4486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(expr, b, b)
4487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(expr, a, N1)
4488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(expr, a, N2)
4489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(expr, N1, b)
4490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                check(expr, N2, b)
4491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if iexpr:
4492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, a, a)
4493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, a, b)
4494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, b, a)
4495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, b, b)
4496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, a, N1)
4497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, a, N2)
4498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    iname = '__i' + name[2:]
4499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    C = metaclass('C', (), {iname: specialmethod})
4500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    c = C()
4501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, c, a)
4502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, c, b)
4503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, c, N1)
4504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    check(iexpr, c, N2)
4505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_assign_slice(self):
4507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # ceval.c's assign_slice used to check for
4508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # tp->tp_as_sequence->sq_slice instead of
4509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # tp->tp_as_sequence->sq_ass_slice
4510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __setslice__(self, start, stop, value):
4513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.value = value
4514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = C()
4516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c[1:2] = 3
4517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(c.value, 3)
4518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_set_and_no_get(self):
4520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # See
4521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # http://mail.python.org/pipermail/python-dev/2010-January/095637.html
4522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Descr(object):
4523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, name):
4525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.name = name
4526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __set__(self, obj, value):
4528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                obj.__dict__[self.name] = value
4529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        descr = Descr("a")
4530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(object):
4532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            a = descr
4533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = X()
4535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIs(x.a, descr)
4536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.a = 42
4537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.a, 42)
4538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Also check type_getattro for correctness.
4540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Meta(type):
4541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(object):
4543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = Meta
4544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        X.a = 42
4545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Meta.a = Descr("a")
4546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(X.a, 42)
4547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_getattr_hooks(self):
4549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # issue 4230
4550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Descriptor(object):
4552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            counter = 0
4553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __get__(self, obj, objtype=None):
4554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def getter(name):
4555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.counter += 1
4556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise AttributeError(name)
4557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return getter
4558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        descr = Descriptor()
4560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class A(object):
4561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __getattribute__ = descr
4562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object):
4563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __getattr__ = descr
4564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __getattribute__ = descr
4566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __getattr__ = descr
4567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(AttributeError, getattr, A(), "attr")
4569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(descr.counter, 1)
4570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(AttributeError, getattr, B(), "attr")
4571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(descr.counter, 2)
4572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(AttributeError, getattr, C(), "attr")
4573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(descr.counter, 4)
4574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class EvilGetattribute(object):
4576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # This used to segfault
4577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattr__(self, name):
4578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise AttributeError(name)
4579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getattribute__(self, name):
4580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                del EvilGetattribute.__getattr__
4581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for i in range(5):
4582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    gc.collect()
4583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise AttributeError(name)
4584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(AttributeError, getattr, EvilGetattribute(), "attr")
4586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_type___getattribute__(self):
4588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, type.__getattribute__, list, type)
4589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_abstractmethods(self):
4591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # type pretends not to have __abstractmethods__.
4592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(AttributeError, getattr, type, "__abstractmethods__")
4593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class meta(type):
4594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(AttributeError, getattr, meta, "__abstractmethods__")
4596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(object):
4597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with self.assertRaises(AttributeError):
4599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            del X.__abstractmethods__
4600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_proxy_call(self):
4602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class FakeStr(object):
4603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __class__ = str
4604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        fake_str = FakeStr()
4606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # isinstance() reads __class__ on new style classes
4607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(isinstance(fake_str, str))
4608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # call a method descriptor
4610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with self.assertRaises(TypeError):
4611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            str.split(fake_str)
4612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # call a slot wrapper descriptor
4614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with self.assertRaises(TypeError):
4615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            str.__add__(fake_str, "abc")
4616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_repr_as_str(self):
4618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Issue #11603: crash or infinite loop when rebinding __str__ as
4619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # __repr__.
4620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo(object):
4621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Foo.__repr__ = Foo.__str__
4623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        foo = Foo()
4624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(RuntimeError, str, foo)
4625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(RuntimeError, repr, foo)
4626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_mixing_slot_wrappers(self):
4628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(dict):
4629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __setattr__ = dict.__setitem__
4630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = X()
4631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.y = 42
4632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x["y"], 42)
4633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_cycle_through_dict(self):
4635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # See bug #1469629
4636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(dict):
4637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self):
4638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                dict.__init__(self)
4639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.__dict__ = self
4640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = X()
4641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.attr = 42
4642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        wr = weakref.ref(x)
4643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del x
4644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.gc_collect()
4645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsNone(wr())
4646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for o in gc.get_objects():
4647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIsNot(type(o), X)
4648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass DictProxyTests(unittest.TestCase):
4650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
4651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
4652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def meth(self):
4653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
4654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.C = C
4655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_repr(self):
4657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn('dict_proxy({', repr(vars(self.C)))
4658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIn("'meth':", repr(vars(self.C)))
4659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_iter_keys(self):
4661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing dict-proxy iterkeys...
4662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        keys = [ key for key in self.C.__dict__.iterkeys() ]
4663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        keys.sort()
4664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(keys, ['__dict__', '__doc__', '__module__',
4665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            '__weakref__', 'meth'])
4666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_iter_values(self):
4668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing dict-proxy itervalues...
4669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        values = [ values for values in self.C.__dict__.itervalues() ]
4670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(len(values), 5)
4671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_iter_items(self):
4673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing dict-proxy iteritems...
4674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        keys = [ key for (key, value) in self.C.__dict__.iteritems() ]
4675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        keys.sort()
4676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(keys, ['__dict__', '__doc__', '__module__',
4677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            '__weakref__', 'meth'])
4678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dict_type_with_metaclass(self):
4680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing type of __dict__ when __metaclass__ set...
4681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class B(object):
4682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class M(type):
4684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C:
4686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # In 2.3a1, C.__dict__ was a real dict rather than a dict proxy
4687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            __metaclass__ = M
4688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(C.__dict__), type(B.__dict__))
4689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass PTypesLongInitTest(unittest.TestCase):
4692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # This is in its own TestCase so that it can be run before any other tests.
4693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_pytype_long_ready(self):
4694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Testing SF bug 551412 ...
4695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # This dumps core when SF bug 551412 isn't fixed --
4697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # but only when test_descr.py is run separately.
4698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # (That can't be helped -- as soon as PyType_Ready()
4699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # is called for PyLong_Type, the bug is gone.)
4700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class UserLong(object):
4701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __pow__(self, *args):
4702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
4703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
4704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pow(0L, UserLong(), 0L)
4705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
4706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
4707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Another segfault only when run early
4709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # (before PyType_Ready(tuple) is called)
4710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        type.mro(tuple)
4711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main():
4714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    deprecations = [(r'complex divmod\(\), // and % are deprecated$',
4715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     DeprecationWarning)]
4716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if sys.py3kwarning:
4717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        deprecations += [
4718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("classic (int|long) division", DeprecationWarning),
4719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("coerce.. not supported", DeprecationWarning),
4720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            (".+__(get|set|del)slice__ has been removed", DeprecationWarning)]
4721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    with test_support.check_warnings(*deprecations):
4722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Run all local test cases, with PTypesLongInitTest first.
4723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_support.run_unittest(PTypesLongInitTest, OperatorsTest,
4724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                  ClassPropertiesAndMethods, DictProxyTests)
4725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
4727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_main()
4728