5
from copy import deepcopy
6
from test import test_support
9
class OperatorsTest(unittest.TestCase):
11
def __init__(self, *args, **kwargs):
12
unittest.TestCase.__init__(self, *args, **kwargs)
34
for name, expr in self.binops.items():
36
expr = expr + "(a, b)"
38
expr = 'a %s b' % expr
39
self.binops[name] = expr
53
for name, expr in self.unops.items():
58
self.unops[name] = expr
61
self.original_filters = warnings.filters[:]
62
warnings.filterwarnings("ignore",
63
r'complex divmod\(\), // and % are deprecated$',
64
DeprecationWarning, r'(<string>|%s)$' % __name__)
67
warnings.filters = self.original_filters
69
def unop_test(self, a, res, expr="len(a)", meth="__len__"):
71
self.assertEqual(eval(expr, d), res)
75
# Find method in parent class
76
while meth not in t.__dict__:
79
self.assertEqual(m, t.__dict__[meth])
80
self.assertEqual(m(a), res)
82
self.assertEqual(bm(), res)
84
def binop_test(self, a, b, res, expr="a+b", meth="__add__"):
87
# XXX Hack so this passes before 2.3 when -Qnew is specified.
88
if meth == "__div__" and 1/2 == 0.5:
91
if meth == '__divmod__': pass
93
self.assertEqual(eval(expr, d), res)
96
while meth not in t.__dict__:
98
self.assertEqual(m, t.__dict__[meth])
99
self.assertEqual(m(a, b), res)
100
bm = getattr(a, meth)
101
self.assertEqual(bm(b), res)
103
def ternop_test(self, a, b, c, res, expr="a[b:c]", meth="__getslice__"):
104
d = {'a': a, 'b': b, 'c': c}
105
self.assertEqual(eval(expr, d), res)
108
while meth not in t.__dict__:
110
self.assertEqual(m, t.__dict__[meth])
111
self.assertEqual(m(a, b, c), res)
112
bm = getattr(a, meth)
113
self.assertEqual(bm(b, c), res)
115
def setop_test(self, a, b, res, stmt="a+=b", meth="__iadd__"):
116
d = {'a': deepcopy(a), 'b': b}
118
self.assertEqual(d['a'], res)
121
while meth not in t.__dict__:
123
self.assertEqual(m, t.__dict__[meth])
126
self.assertEqual(d['a'], res)
128
bm = getattr(d['a'], meth)
130
self.assertEqual(d['a'], res)
132
def set2op_test(self, a, b, c, res, stmt="a[b]=c", meth="__setitem__"):
133
d = {'a': deepcopy(a), 'b': b, 'c': c}
135
self.assertEqual(d['a'], res)
138
while meth not in t.__dict__:
140
self.assertEqual(m, t.__dict__[meth])
143
self.assertEqual(d['a'], res)
145
bm = getattr(d['a'], meth)
147
self.assertEqual(d['a'], res)
149
def set3op_test(self, a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"):
150
dictionary = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d}
151
exec stmt in dictionary
152
self.assertEqual(dictionary['a'], res)
154
while meth not in t.__dict__:
157
self.assertEqual(m, t.__dict__[meth])
158
dictionary['a'] = deepcopy(a)
159
m(dictionary['a'], b, c, d)
160
self.assertEqual(dictionary['a'], res)
161
dictionary['a'] = deepcopy(a)
162
bm = getattr(dictionary['a'], meth)
164
self.assertEqual(dictionary['a'], res)
166
def test_lists(self):
167
# Testing list operations...
168
# Asserts are within individual test methods
169
self.binop_test([1], [2], [1,2], "a+b", "__add__")
170
self.binop_test([1,2,3], 2, 1, "b in a", "__contains__")
171
self.binop_test([1,2,3], 4, 0, "b in a", "__contains__")
172
self.binop_test([1,2,3], 1, 2, "a[b]", "__getitem__")
173
self.ternop_test([1,2,3], 0, 2, [1,2], "a[b:c]", "__getslice__")
174
self.setop_test([1], [2], [1,2], "a+=b", "__iadd__")
175
self.setop_test([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__")
176
self.unop_test([1,2,3], 3, "len(a)", "__len__")
177
self.binop_test([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__")
178
self.binop_test([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__")
179
self.set2op_test([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__")
180
self.set3op_test([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d",
183
def test_dicts(self):
184
# Testing dict operations...
185
self.binop_test({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__")
186
self.binop_test({1:2,3:4}, 1, 1, "b in a", "__contains__")
187
self.binop_test({1:2,3:4}, 2, 0, "b in a", "__contains__")
188
self.binop_test({1:2,3:4}, 1, 2, "a[b]", "__getitem__")
197
self.assertEqual(l, l1)
199
for i in d.__iter__():
201
self.assertEqual(l, l1)
203
for i in dict.__iter__(d):
205
self.assertEqual(l, l1)
207
self.unop_test(d, 2, "len(a)", "__len__")
208
self.assertEqual(eval(repr(d), {}), d)
209
self.assertEqual(eval(d.__repr__(), {}), d)
210
self.set2op_test({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c",
213
# Tests for unary and binary operators
214
def number_operators(self, a, b, skip=[]):
215
dict = {'a': a, 'b': b}
217
for name, expr in self.binops.items():
219
name = "__%s__" % name
221
res = eval(expr, dict)
222
self.binop_test(a, b, res, expr, name)
224
for name, expr in self.unops.items():
226
name = "__%s__" % name
228
res = eval(expr, dict)
229
self.unop_test(a, res, expr, name)
232
# Testing int operations...
233
self.number_operators(100, 3)
234
# The following crashes in Python 2.2
235
self.assertEqual((1).__nonzero__(), 1)
236
self.assertEqual((0).__nonzero__(), 0)
237
# This returns 'NotImplemented' in Python 2.2
239
def __add__(self, other):
240
return NotImplemented
241
self.assertEqual(C(5L), 5)
247
self.fail("NotImplemented should have caused TypeError")
251
except OverflowError:
254
self.fail("should have raised OverflowError")
256
def test_longs(self):
257
# Testing long operations...
258
self.number_operators(100L, 3L)
260
def test_floats(self):
261
# Testing float operations...
262
self.number_operators(100.0, 3.0)
264
def test_complexes(self):
265
# Testing complex operations...
266
self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge',
267
'int', 'long', 'float'])
269
class Number(complex):
271
def __new__(cls, *args, **kwds):
272
result = complex.__new__(cls, *args)
273
result.prec = kwds.get('prec', 12)
278
return "%.*g" % (prec, self.real)
280
return "%.*gj" % (prec, self.imag)
281
return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag)
284
a = Number(3.14, prec=6)
285
self.assertEqual(repr(a), "3.14")
286
self.assertEqual(a.prec, 6)
288
a = Number(a, prec=2)
289
self.assertEqual(repr(a), "3.1")
290
self.assertEqual(a.prec, 2)
293
self.assertEqual(repr(a), "234.5")
294
self.assertEqual(a.prec, 12)
296
def test_spam_lists(self):
297
# Testing spamlist operations...
298
import copy, xxsubtype as spam
300
def spamlist(l, memo=None):
301
import xxsubtype as spam
302
return spam.spamlist(l)
304
# This is an ugly hack:
305
copy._deepcopy_dispatch[spam.spamlist] = spamlist
307
self.binop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b",
309
self.binop_test(spamlist([1,2,3]), 2, 1, "b in a", "__contains__")
310
self.binop_test(spamlist([1,2,3]), 4, 0, "b in a", "__contains__")
311
self.binop_test(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__")
312
self.ternop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]",
314
self.setop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b",
316
self.setop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b",
318
self.unop_test(spamlist([1,2,3]), 3, "len(a)", "__len__")
319
self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b",
321
self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a",
323
self.set2op_test(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c",
325
self.set3op_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]),
326
spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__")
328
class C(spam.spamlist):
329
def foo(self): return 1
331
self.assertEqual(a, [])
332
self.assertEqual(a.foo(), 1)
334
self.assertEqual(a, [100])
335
self.assertEqual(a.getstate(), 0)
337
self.assertEqual(a.getstate(), 42)
339
def test_spam_dicts(self):
340
# Testing spamdict operations...
341
import copy, xxsubtype as spam
342
def spamdict(d, memo=None):
343
import xxsubtype as spam
345
for k, v in d.items():
348
# This is an ugly hack:
349
copy._deepcopy_dispatch[spam.spamdict] = spamdict
351
self.binop_test(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)",
353
self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
354
self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
355
self.binop_test(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__")
356
d = spamdict({1:2,3:4})
363
self.assertEqual(l, l1)
365
for i in d.__iter__():
367
self.assertEqual(l, l1)
369
for i in type(spamdict({})).__iter__(d):
371
self.assertEqual(l, l1)
372
straightd = {1:2, 3:4}
373
spamd = spamdict(straightd)
374
self.unop_test(spamd, 2, "len(a)", "__len__")
375
self.unop_test(spamd, repr(straightd), "repr(a)", "__repr__")
376
self.set2op_test(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}),
377
"a[b]=c", "__setitem__")
379
class C(spam.spamdict):
380
def foo(self): return 1
382
self.assertEqual(a.items(), [])
383
self.assertEqual(a.foo(), 1)
385
self.assertEqual(a.items(), [('foo', 'bar')])
386
self.assertEqual(a.getstate(), 0)
388
self.assertEqual(a.getstate(), 100)
390
class ClassPropertiesAndMethods(unittest.TestCase):
392
def test_python_dicts(self):
393
# Testing Python subclass of dict...
394
self.assert_(issubclass(dict, dict))
395
self.assert_(isinstance({}, dict))
397
self.assertEqual(d, {})
398
self.assert_(d.__class__ is dict)
399
self.assert_(isinstance(d, dict))
402
def __init__(self_local, *a, **kw):
404
self.assertEqual(len(a), 1)
405
self_local.state = a[0]
407
for k, v in kw.items():
409
def __getitem__(self, key):
410
return self.get(key, 0)
411
def __setitem__(self_local, key, value):
412
self.assert_(isinstance(key, type(0)))
413
dict.__setitem__(self_local, key, value)
414
def setstate(self, state):
418
self.assert_(issubclass(C, dict))
420
self.assertEqual(a1.state, 12)
422
self.assertEqual(a2[1] == 'foo' and a2[2], 'bar')
424
self.assertEqual(a.state, -1)
425
self.assertEqual(a.getstate(), -1)
427
self.assertEqual(a.state, 0)
428
self.assertEqual(a.getstate(), 0)
430
self.assertEqual(a.state, 10)
431
self.assertEqual(a.getstate(), 10)
432
self.assertEqual(a[42], 0)
434
self.assertEqual(a[42], 24)
442
self.assertEqual(a[i][j], i*j)
444
def test_python_lists(self):
445
# Testing Python subclass of list...
447
def __getitem__(self, i):
448
return list.__getitem__(self, i) + 100
449
def __getslice__(self, i, j):
453
self.assertEqual(a[0], 100)
454
self.assertEqual(a[1], 101)
455
self.assertEqual(a[2], 102)
456
self.assertEqual(a[100:200], (100,200))
458
def test_metaclass(self):
459
# Testing __metaclass__...
466
def setstate(self, state):
469
self.assertEqual(a.getstate(), 0)
471
self.assertEqual(a.getstate(), 10)
473
class __metaclass__(type):
474
def myself(cls): return cls
475
self.assertEqual(D.myself(), D)
477
self.assertEqual(d.__class__, D)
479
def __new__(cls, name, bases, dict):
481
return type.__new__(cls, name, bases, dict)
484
self.assertEqual(C.__spam__, 1)
486
self.assertEqual(c.__spam__, 1)
488
class _instance(object):
492
def __new__(cls, name, bases, dict):
493
self = object.__new__(cls)
500
# Early binding of methods
501
for key in self.dict:
502
if key.startswith("__"):
504
setattr(it, key, self.dict[key].__get__(it, self))
510
self.assertEqual(C.name, 'C')
511
self.assertEqual(C.bases, ())
512
self.assert_('spam' in C.dict)
514
self.assertEqual(c.spam(), 42)
516
# More metaclass examples
518
class autosuper(type):
519
# Automatically add __super to the class
520
# This trick only works for dynamic classes
521
def __new__(metaclass, name, bases, dict):
522
cls = super(autosuper, metaclass).__new__(metaclass,
524
# Name mangling for __super removes leading underscores
525
while name[:1] == "_":
528
name = "_%s__super" % name
531
setattr(cls, name, super(cls))
534
__metaclass__ = autosuper
539
return "B" + self.__super.meth()
542
return "C" + self.__super.meth()
545
return "D" + self.__super.meth()
546
self.assertEqual(D().meth(), "DCBA")
549
return "E" + self.__super.meth()
550
self.assertEqual(E().meth(), "EBCA")
552
class autoproperty(type):
553
# Automatically create property attributes when methods
554
# named _get_x and/or _set_x are found
555
def __new__(metaclass, name, bases, dict):
557
for key, val in dict.iteritems():
558
if key.startswith("_get_"):
560
get, set = hits.get(key, (None, None))
563
elif key.startswith("_set_"):
565
get, set = hits.get(key, (None, None))
568
for key, (get, set) in hits.iteritems():
569
dict[key] = property(get, set)
570
return super(autoproperty, metaclass).__new__(metaclass,
573
__metaclass__ = autoproperty
579
self.assert_(not hasattr(a, "x"))
581
self.assertEqual(a.x, 12)
582
self.assertEqual(a._A__x, -12)
584
class multimetaclass(autoproperty, autosuper):
585
# Merge of multiple cooperating metaclasses
588
__metaclass__ = multimetaclass
593
return "B" + self.__super._get_x()
596
return "C" + self.__super._get_x()
599
return "D" + self.__super._get_x()
600
self.assertEqual(D().x, "DCBA")
602
# Make sure type(x) doesn't call x.__class__.__init__
605
def __init__(self, *args):
609
self.assertEqual(T.counter, 1)
611
self.assertEqual(type(a), C)
612
self.assertEqual(T.counter, 1)
614
class C(object): pass
617
except TypeError: pass
618
else: self.fail("calling object w/o call method should raise "
621
# Testing code to find most derived baseclass
623
def __new__(*args, **kwargs):
624
return type.__new__(*args, **kwargs)
632
# The most derived metaclass of D is A rather than type.
636
def test_module_subclasses(self):
637
# Testing Python subclass of module...
642
def __init__(self, name):
643
MT.__init__(self, name)
644
def __getattribute__(self, name):
645
log.append(("getattr", name))
646
return MT.__getattribute__(self, name)
647
def __setattr__(self, name, value):
648
log.append(("setattr", name, value))
649
MT.__setattr__(self, name, value)
650
def __delattr__(self, name):
651
log.append(("delattr", name))
652
MT.__delattr__(self, name)
657
self.assertEqual(log, [("setattr", "foo", 12),
661
# http://python.org/sf/1174712
663
class Module(types.ModuleType, str):
668
self.fail("inheriting from ModuleType and str at the same time "
671
def test_multiple_inheritence(self):
672
# Testing multiple inheritance...
678
def setstate(self, state):
681
self.assertEqual(a.getstate(), 0)
683
self.assertEqual(a.getstate(), 10)
686
type({}).__init__(self)
689
self.assertEqual(d.keys(), [])
691
self.assertEqual(d.items(), [("hello", "world")])
692
self.assertEqual(d["hello"], "world")
693
self.assertEqual(d.getstate(), 0)
695
self.assertEqual(d.getstate(), 10)
696
self.assertEqual(D.__mro__, (D, dict, C, object))
701
return int(self.foo())
704
class Frag(Node, list):
707
self.assertEqual(Node().__int__(), 23)
708
self.assertEqual(int(Node()), 23)
709
self.assertEqual(Frag().__int__(), 42)
710
self.assertEqual(int(Frag()), 42)
712
# MI mixing classic and new-style classes.
725
self.assertEqual(D.x, 1)
727
# Classic MRO is preserved for a classic base class.
730
self.assertEqual(E.__mro__, (E, D, B, A, C, object))
731
self.assertEqual(E.x, 1)
733
# But with a mix of classic bases, their MROs are combined using
735
class F(B, C, object):
737
self.assertEqual(F.__mro__, (F, B, C, A, object))
738
self.assertEqual(F.x, 2)
740
# Try something else.
744
def all_method(self):
750
def all_method(self):
753
self.assertEqual(M1.__mro__, (M1, C, object))
755
self.assertEqual(m.cmethod(), "C a")
756
self.assertEqual(m.m1method(), "M1 a")
757
self.assertEqual(m.all_method(), "M1 b")
762
def all_method(self):
768
def all_method(self):
771
self.assertEqual(M2.__mro__, (M2, D, C, object))
773
self.assertEqual(m.cmethod(), "C a")
774
self.assertEqual(m.dmethod(), "D a")
775
self.assertEqual(m.m2method(), "M2 a")
776
self.assertEqual(m.all_method(), "M2 b")
778
class M3(M1, M2, object):
781
def all_method(self):
783
self.assertEqual(M3.__mro__, (M3, M1, M2, D, C, object))
785
self.assertEqual(m.cmethod(), "C a")
786
self.assertEqual(m.dmethod(), "D a")
787
self.assertEqual(m.m1method(), "M1 a")
788
self.assertEqual(m.m2method(), "M2 a")
789
self.assertEqual(m.m3method(), "M3 a")
790
self.assertEqual(m.all_method(), "M3 b")
800
self.fail("new class with only classic bases - shouldn't be")
802
def test_diamond_inheritence(self):
803
# Testing multiple inheritance special cases...
805
def spam(self): return "A"
806
self.assertEqual(A().spam(), "A")
808
def boo(self): return "B"
809
def spam(self): return "B"
810
self.assertEqual(B().spam(), "B")
811
self.assertEqual(B().boo(), "B")
813
def boo(self): return "C"
814
self.assertEqual(C().spam(), "A")
815
self.assertEqual(C().boo(), "C")
817
self.assertEqual(D().spam(), "B")
818
self.assertEqual(D().boo(), "B")
819
self.assertEqual(D.__mro__, (D, B, C, A, object))
821
self.assertEqual(E().spam(), "B")
822
self.assertEqual(E().boo(), "C")
823
self.assertEqual(E.__mro__, (E, C, B, A, object))
824
# MRO order disagreement
830
self.fail("expected MRO order disagreement (F)")
836
self.fail("expected MRO order disagreement (G)")
838
# see thread python-dev/2002-October/029035.html
839
def test_ex5_from_c3_switch(self):
840
# Testing ex5 from C3 switch discussion...
841
class A(object): pass
842
class B(object): pass
843
class C(object): pass
846
class Z(X,B,Y,C): pass
847
self.assertEqual(Z.__mro__, (Z, X, B, Y, A, C, object))
849
# see "A Monotonic Superclass Linearization for Dylan",
850
# by Kim Barrett et al. (OOPSLA 1996)
851
def test_monotonicity(self):
852
# Testing MRO monotonicity...
853
class Boat(object): pass
854
class DayBoat(Boat): pass
855
class WheelBoat(Boat): pass
856
class EngineLess(DayBoat): pass
857
class SmallMultihull(DayBoat): pass
858
class PedalWheelBoat(EngineLess,WheelBoat): pass
859
class SmallCatamaran(SmallMultihull): pass
860
class Pedalo(PedalWheelBoat,SmallCatamaran): pass
862
self.assertEqual(PedalWheelBoat.__mro__,
863
(PedalWheelBoat, EngineLess, DayBoat, WheelBoat, Boat, object))
864
self.assertEqual(SmallCatamaran.__mro__,
865
(SmallCatamaran, SmallMultihull, DayBoat, Boat, object))
866
self.assertEqual(Pedalo.__mro__,
867
(Pedalo, PedalWheelBoat, EngineLess, SmallCatamaran,
868
SmallMultihull, DayBoat, WheelBoat, Boat, object))
870
# see "A Monotonic Superclass Linearization for Dylan",
871
# by Kim Barrett et al. (OOPSLA 1996)
872
def test_consistency_with_epg(self):
873
# Testing consistentcy with EPG...
874
class Pane(object): pass
875
class ScrollingMixin(object): pass
876
class EditingMixin(object): pass
877
class ScrollablePane(Pane,ScrollingMixin): pass
878
class EditablePane(Pane,EditingMixin): pass
879
class EditableScrollablePane(ScrollablePane,EditablePane): pass
881
self.assertEqual(EditableScrollablePane.__mro__,
882
(EditableScrollablePane, ScrollablePane, EditablePane, Pane,
883
ScrollingMixin, EditingMixin, object))
885
def test_mro_disagreement(self):
886
# Testing error messages for MRO disagreement...
887
mro_err_msg = """Cannot create a consistent method resolution
888
order (MRO) for bases """
890
def raises(exc, expected, callable, *args):
894
if not str(msg).startswith(expected):
895
self.fail("Message %r, expected %r" % (str(msg), expected))
897
self.fail("Expected %s" % exc)
899
class A(object): pass
901
class C(object): pass
903
# Test some very simple errors
904
raises(TypeError, "duplicate base class A",
905
type, "X", (A, A), {})
906
raises(TypeError, mro_err_msg,
907
type, "X", (A, B), {})
908
raises(TypeError, mro_err_msg,
909
type, "X", (A, C, B), {})
910
# Test a slightly more complex error
911
class GridLayout(object): pass
912
class HorizontalGrid(GridLayout): pass
913
class VerticalGrid(GridLayout): pass
914
class HVGrid(HorizontalGrid, VerticalGrid): pass
915
class VHGrid(VerticalGrid, HorizontalGrid): pass
916
raises(TypeError, mro_err_msg,
917
type, "ConfusedGrid", (HVGrid, VHGrid), {})
919
def test_object_class(self):
920
# Testing object class...
922
self.assertEqual(a.__class__, object)
923
self.assertEqual(type(a), object)
925
self.assertNotEqual(a, b)
926
self.assertFalse(hasattr(a, "foo"))
929
except (AttributeError, TypeError):
932
self.fail("object() should not allow setting a foo attribute")
933
self.assertFalse(hasattr(object(), "__dict__"))
938
self.assertEqual(x.__dict__, {})
940
self.assertEqual(x.foo, 1)
941
self.assertEqual(x.__dict__, {'foo': 1})
943
def test_slots(self):
944
# Testing __slots__...
948
self.assertFalse(hasattr(x, "__dict__"))
949
self.assertFalse(hasattr(x, "foo"))
954
self.assertFalse(hasattr(x, "__dict__"))
955
self.assertFalse(hasattr(x, "a"))
957
self.assertEqual(x.a, 1)
959
self.assertEqual(x.a, None)
961
self.assertFalse(hasattr(x, "a"))
964
__slots__ = ['a', 'b', 'c']
966
self.assertFalse(hasattr(x, "__dict__"))
967
self.assertFalse(hasattr(x, 'a'))
968
self.assertFalse(hasattr(x, 'b'))
969
self.assertFalse(hasattr(x, 'c'))
973
self.assertEqual(x.a, 1)
974
self.assertEqual(x.b, 2)
975
self.assertEqual(x.c, 3)
978
"""Validate name mangling"""
980
def __init__(self, value):
985
self.assertFalse(hasattr(x, '__dict__'))
986
self.assertFalse(hasattr(x, '__a'))
987
self.assertEqual(x.get(), 5)
990
except AttributeError:
993
self.fail("Double underscored names not mangled")
995
# Make sure slot names are proper identifiers
1002
self.fail("[None] slots not caught")
1005
__slots__ = ["foo bar"]
1009
self.fail("['foo bar'] slots not caught")
1012
__slots__ = ["foo\0bar"]
1016
self.fail("['foo\\0bar'] slots not caught")
1023
self.fail("['1'] slots not caught")
1030
self.fail("[''] slots not caught")
1032
__slots__ = ["a", "a_b", "_a", "A0123456789Z"]
1033
# XXX(nnorwitz): was there supposed to be something tested
1034
# from the class above?
1036
# Test a single string is not expanded as a sequence.
1041
self.assertEqual(c.abc, 5)
1043
# Test unicode slot names
1049
# Test a single unicode string is not expanded as a sequence.
1051
__slots__ = unicode("abc")
1054
self.assertEqual(c.abc, 5)
1056
# _unicode_to_string used to modify slots in certain circumstances
1057
slots = (unicode("foo"), unicode("bar"))
1062
self.assertEqual(x.foo, 5)
1063
self.assertEqual(type(slots[0]), unicode)
1064
# this used to leak references
1067
__slots__ = [unichr(128)]
1068
except (TypeError, UnicodeEncodeError):
1071
self.fail("[unichr(128)] slots not caught")
1074
class Counted(object):
1075
counter = 0 # counts the number of instances alive
1077
Counted.counter += 1
1079
Counted.counter -= 1
1081
__slots__ = ['a', 'b', 'c']
1086
self.assertEqual(Counted.counter, 3)
1088
self.assertEqual(Counted.counter, 0)
1094
self.assertEqual(Counted.counter, 2)
1096
self.assertEqual(Counted.counter, 0)
1103
self.assertEqual(Counted.counter, 3)
1105
self.assertEqual(Counted.counter, 0)
1107
# Test cyclical leaks [SF bug 519621]
1109
__slots__ = ['a', 'b']
1112
s.a = [Counted(), s]
1113
self.assertEqual(Counted.counter, 1)
1117
self.assertEqual(Counted.counter, 0)
1119
# Test lookup leaks [SF bug 572567]
1122
def __cmp__(self, other):
1124
__hash__ = None # Silence Py3k warning
1126
orig_objects = len(gc.get_objects())
1127
for i in xrange(10):
1129
new_objects = len(gc.get_objects())
1130
self.assertEqual(orig_objects, new_objects)
1132
__slots__ = ['a', 'b']
1137
self.assertEqual(self_.a, 1)
1138
self.assertEqual(self_.b, 2)
1139
with test_support.captured_output('stderr') as s:
1142
self.assertEqual(s.getvalue(), '')
1144
def test_slots_special(self):
1145
# Testing __dict__ and __weakref__ in __slots__...
1147
__slots__ = ["__dict__"]
1149
self.assert_(hasattr(a, "__dict__"))
1150
self.assertFalse(hasattr(a, "__weakref__"))
1152
self.assertEqual(a.__dict__, {"foo": 42})
1155
__slots__ = ["__weakref__"]
1157
self.assert_(hasattr(a, "__weakref__"))
1158
self.assertFalse(hasattr(a, "__dict__"))
1161
except AttributeError:
1164
self.fail("shouldn't be allowed to set a.foo")
1169
self.assert_(hasattr(a, "__dict__"))
1170
self.assert_(hasattr(a, "__weakref__"))
1172
self.assertEqual(a.__dict__, {"foo": 42})
1177
self.assert_(hasattr(a, "__dict__"))
1178
self.assert_(hasattr(a, "__weakref__"))
1180
self.assertEqual(a.__dict__, {"foo": 42})
1182
def test_slots_descriptor(self):
1183
# Issue2115: slot descriptors did not correctly check
1184
# the type of the given object
1187
__metaclass__ = abc.ABCMeta
1190
class Unrelated(object):
1192
MyABC.register(Unrelated)
1195
self.assert_(isinstance(u, MyABC))
1197
# This used to crash
1198
self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
1200
def test_dynamics(self):
1201
# Testing class attribute propagation...
1209
self.assertEqual(D.foo, 1)
1210
# Test that dynamic attributes are inherited
1211
self.assertEqual(E.foo, 1)
1212
self.assertEqual(F.foo, 1)
1213
# Test dynamic instances
1217
self.assertFalse(hasattr(a, "foobar"))
1219
self.assertEqual(a.foobar, 2)
1220
C.method = lambda self: 42
1221
self.assertEqual(a.method(), 42)
1222
C.__repr__ = lambda self: "C()"
1223
self.assertEqual(repr(a), "C()")
1224
C.__int__ = lambda self: 100
1225
self.assertEqual(int(a), 100)
1226
self.assertEqual(a.foobar, 2)
1227
self.assertFalse(hasattr(a, "spam"))
1228
def mygetattr(self, name):
1231
raise AttributeError
1232
C.__getattr__ = mygetattr
1233
self.assertEqual(a.spam, "spam")
1235
self.assertEqual(a.new, 12)
1236
def mysetattr(self, name, value):
1238
raise AttributeError
1239
return object.__setattr__(self, name, value)
1240
C.__setattr__ = mysetattr
1243
except AttributeError:
1246
self.fail("expected AttributeError")
1247
self.assertEqual(a.spam, "spam")
1252
self.assertEqual(d.foo, 1)
1254
# Test handling of int*seq and seq*int
1257
self.assertEqual("a"*I(2), "aa")
1258
self.assertEqual(I(2)*"a", "aa")
1259
self.assertEqual(2*I(3), 6)
1260
self.assertEqual(I(3)*2, 6)
1261
self.assertEqual(I(3)*I(2), 6)
1263
# Test handling of long*seq and seq*long
1266
self.assertEqual("a"*L(2L), "aa")
1267
self.assertEqual(L(2L)*"a", "aa")
1268
self.assertEqual(2*L(3), 6)
1269
self.assertEqual(L(3)*2, 6)
1270
self.assertEqual(L(3)*L(2), 6)
1272
# Test comparison of classes with dynamic metaclasses
1273
class dynamicmetaclass(type):
1276
__metaclass__ = dynamicmetaclass
1277
self.assertNotEqual(someclass, object)
1279
def test_errors(self):
1282
class C(list, dict):
1287
self.fail("inheritance from both list and dict should be illegal")
1290
class C(object, None):
1295
self.fail("inheritance from non-type should be illegal")
1305
self.fail("inheritance from CFunction should be illegal")
1313
self.fail("__slots__ = 1 should be illegal")
1321
self.fail("__slots__ = [1] should be illegal")
1337
self.fail("finding the most derived metaclass should have failed")
1339
def test_classmethods(self):
1340
# Testing class methods...
1342
def foo(*a): return a
1343
goo = classmethod(foo)
1345
self.assertEqual(C.goo(1), (C, 1))
1346
self.assertEqual(c.goo(1), (C, 1))
1347
self.assertEqual(c.foo(1), (c, 1))
1351
self.assertEqual(D.goo(1), (D, 1))
1352
self.assertEqual(d.goo(1), (D, 1))
1353
self.assertEqual(d.foo(1), (d, 1))
1354
self.assertEqual(D.foo(d, 1), (d, 1))
1355
# Test for a specific crash (SF bug 528132)
1356
def f(cls, arg): return (cls, arg)
1358
self.assertEqual(ff.__get__(0, int)(42), (int, 42))
1359
self.assertEqual(ff.__get__(0)(42), (int, 42))
1361
# Test super() with classmethods (SF bug 535444)
1362
self.assertEqual(C.goo.im_self, C)
1363
self.assertEqual(D.goo.im_self, D)
1364
self.assertEqual(super(D,D).goo.im_self, D)
1365
self.assertEqual(super(D,d).goo.im_self, D)
1366
self.assertEqual(super(D,D).goo(), (D,))
1367
self.assertEqual(super(D,d).goo(), (D,))
1369
# Verify that argument is checked for callability (SF bug 753451)
1371
classmethod(1).__get__(1)
1375
self.fail("classmethod should check for callability")
1377
# Verify that classmethod() doesn't allow keyword args
1379
classmethod(f, kw=1)
1383
self.fail("classmethod shouldn't accept keyword args")
1385
def test_classmethods_in_c(self):
1386
# Testing C-based class methods...
1387
import xxsubtype as spam
1390
x, a1, d1 = spam.spamlist.classmeth(*a, **d)
1391
self.assertEqual(x, spam.spamlist)
1392
self.assertEqual(a, a1)
1393
self.assertEqual(d, d1)
1394
x, a1, d1 = spam.spamlist().classmeth(*a, **d)
1395
self.assertEqual(x, spam.spamlist)
1396
self.assertEqual(a, a1)
1397
self.assertEqual(d, d1)
1399
def test_staticmethods(self):
1400
# Testing static methods...
1402
def foo(*a): return a
1403
goo = staticmethod(foo)
1405
self.assertEqual(C.goo(1), (1,))
1406
self.assertEqual(c.goo(1), (1,))
1407
self.assertEqual(c.foo(1), (c, 1,))
1411
self.assertEqual(D.goo(1), (1,))
1412
self.assertEqual(d.goo(1), (1,))
1413
self.assertEqual(d.foo(1), (d, 1))
1414
self.assertEqual(D.foo(d, 1), (d, 1))
1416
def test_staticmethods_in_c(self):
1417
# Testing C-based static methods...
1418
import xxsubtype as spam
1421
x, a1, d1 = spam.spamlist.staticmeth(*a, **d)
1422
self.assertEqual(x, None)
1423
self.assertEqual(a, a1)
1424
self.assertEqual(d, d1)
1425
x, a1, d2 = spam.spamlist().staticmeth(*a, **d)
1426
self.assertEqual(x, None)
1427
self.assertEqual(a, a1)
1428
self.assertEqual(d, d1)
1430
def test_classic(self):
1431
# Testing classic classes...
1433
def foo(*a): return a
1434
goo = classmethod(foo)
1436
self.assertEqual(C.goo(1), (C, 1))
1437
self.assertEqual(c.goo(1), (C, 1))
1438
self.assertEqual(c.foo(1), (c, 1))
1442
self.assertEqual(D.goo(1), (D, 1))
1443
self.assertEqual(d.goo(1), (D, 1))
1444
self.assertEqual(d.foo(1), (d, 1))
1445
self.assertEqual(D.foo(d, 1), (d, 1))
1446
class E: # *not* subclassing from C
1448
self.assertEqual(E().foo, C.foo) # i.e., unbound
1449
self.assert_(repr(C.foo.__get__(C())).startswith("<bound method "))
1451
def test_compattr(self):
1452
# Testing computed attributes...
1454
class computed_attribute(object):
1455
def __init__(self, get, set=None, delete=None):
1458
self.__delete = delete
1459
def __get__(self, obj, type=None):
1460
return self.__get(obj)
1461
def __set__(self, obj, value):
1462
return self.__set(obj, value)
1463
def __delete__(self, obj):
1464
return self.__delete(obj)
1471
def __set_x(self, x):
1473
def __delete_x(self):
1475
x = computed_attribute(__get_x, __set_x, __delete_x)
1477
self.assertEqual(a.x, 0)
1478
self.assertEqual(a.x, 1)
1480
self.assertEqual(a.x, 10)
1481
self.assertEqual(a.x, 11)
1483
self.assertEqual(hasattr(a, 'x'), 0)
1485
def test_newslots(self):
1486
# Testing __new__ slot override...
1489
self = list.__new__(cls)
1493
self.foo = self.foo + 2
1495
self.assertEqual(a.foo, 3)
1496
self.assertEqual(a.__class__, C)
1500
self.assertEqual(b.foo, 3)
1501
self.assertEqual(b.__class__, D)
1503
def test_altmro(self):
1504
# Testing mro() and overriding it...
1506
def f(self): return "A"
1510
def f(self): return "C"
1513
self.assertEqual(D.mro(), [D, B, C, A, object])
1514
self.assertEqual(D.__mro__, (D, B, C, A, object))
1515
self.assertEqual(D().f(), "C")
1517
class PerverseMetaType(type):
1523
__metaclass__ = PerverseMetaType
1524
self.assertEqual(X.__mro__, (object, A, C, B, D, X))
1525
self.assertEqual(X().f(), "A")
1529
class __metaclass__(type):
1531
return [self, dict, object]
1535
self.fail("devious mro() return not caught")
1539
class __metaclass__(type):
1545
self.fail("non-class mro() return not caught")
1549
class __metaclass__(type):
1555
self.fail("non-sequence mro() return not caught")
1557
def test_overloading(self):
1558
# Testing operator overloading...
1561
"Intermediate class because object doesn't have a __setattr__"
1564
def __getattr__(self, name):
1566
return ("getattr", name)
1568
raise AttributeError
1569
def __setattr__(self, name, value):
1571
self.setattr = (name, value)
1573
return B.__setattr__(self, name, value)
1574
def __delattr__(self, name):
1578
return B.__delattr__(self, name)
1580
def __getitem__(self, key):
1581
return ("getitem", key)
1582
def __setitem__(self, key, value):
1583
self.setitem = (key, value)
1584
def __delitem__(self, key):
1587
def __getslice__(self, i, j):
1588
return ("getslice", i, j)
1589
def __setslice__(self, i, j, value):
1590
self.setslice = (i, j, value)
1591
def __delslice__(self, i, j):
1592
self.delslice = (i, j)
1595
self.assertEqual(a.foo, ("getattr", "foo"))
1597
self.assertEqual(a.setattr, ("foo", 12))
1599
self.assertEqual(a.delattr, "foo")
1601
self.assertEqual(a[12], ("getitem", 12))
1603
self.assertEqual(a.setitem, (12, 21))
1605
self.assertEqual(a.delitem, 12)
1607
self.assertEqual(a[0:10], ("getslice", 0, 10))
1609
self.assertEqual(a.setslice, (0, 10, "foo"))
1611
self.assertEqual(a.delslice, (0, 10))
1613
def test_methods(self):
1614
# Testing methods...
1616
def __init__(self, x):
1621
self.assertEqual(c1.foo(), 1)
1626
self.assertEqual(d2.foo(), 2)
1627
self.assertEqual(d2.boo(), 2)
1628
self.assertEqual(d2.goo(), 1)
1631
self.assertEqual(E().foo, C.foo) # i.e., unbound
1632
self.assert_(repr(C.foo.__get__(C(1))).startswith("<bound method "))
1634
def test_specials(self):
1635
# Testing special operators...
1636
# Test operators like __hash__ for which a built-in default exists
1638
# Test the default behavior for static classes
1640
def __getitem__(self, i):
1641
if 0 <= i < 10: return i
1645
self.assert_(not not c1) # What?
1646
self.assertNotEqual(id(c1), id(c2))
1649
self.assertEqual(cmp(c1, c2), cmp(id(c1), id(c2)))
1650
self.assertEqual(c1, c1)
1651
self.assert_(c1 != c2)
1652
self.assert_(not c1 != c1)
1653
self.assert_(not c1 == c2)
1654
# Note that the module name appears in str/repr, and that varies
1655
# depending on whether this test is run standalone or from a framework.
1656
self.assert_(str(c1).find('C object at ') >= 0)
1657
self.assertEqual(str(c1), repr(c1))
1658
self.assert_(-1 not in c1)
1660
self.assert_(i in c1)
1661
self.assertFalse(10 in c1)
1662
# Test the default behavior for dynamic classes
1664
def __getitem__(self, i):
1665
if 0 <= i < 10: return i
1669
self.assert_(not not d1)
1670
self.assertNotEqual(id(d1), id(d2))
1673
self.assertEqual(cmp(d1, d2), cmp(id(d1), id(d2)))
1674
self.assertEqual(d1, d1)
1675
self.assertNotEqual(d1, d2)
1676
self.assert_(not d1 != d1)
1677
self.assert_(not d1 == d2)
1678
# Note that the module name appears in str/repr, and that varies
1679
# depending on whether this test is run standalone or from a framework.
1680
self.assert_(str(d1).find('D object at ') >= 0)
1681
self.assertEqual(str(d1), repr(d1))
1682
self.assert_(-1 not in d1)
1684
self.assert_(i in d1)
1685
self.assertFalse(10 in d1)
1686
# Test overridden behavior for static classes
1687
class Proxy(object):
1688
def __init__(self, x):
1690
def __nonzero__(self):
1691
return not not self.x
1694
def __eq__(self, other):
1695
return self.x == other
1696
def __ne__(self, other):
1697
return self.x != other
1698
def __cmp__(self, other):
1699
return cmp(self.x, other.x)
1701
return "Proxy:%s" % self.x
1703
return "Proxy(%r)" % self.x
1704
def __contains__(self, value):
1705
return value in self.x
1709
self.assertFalse(p0)
1710
self.assert_(not not p1)
1711
self.assertEqual(hash(p0), hash(0))
1712
self.assertEqual(p0, p0)
1713
self.assertNotEqual(p0, p1)
1714
self.assert_(not p0 != p0)
1715
self.assertEqual(not p0, p1)
1716
self.assertEqual(cmp(p0, p1), -1)
1717
self.assertEqual(cmp(p0, p0), 0)
1718
self.assertEqual(cmp(p0, p_1), 1)
1719
self.assertEqual(str(p0), "Proxy:0")
1720
self.assertEqual(repr(p0), "Proxy(0)")
1721
p10 = Proxy(range(10))
1722
self.assertFalse(-1 in p10)
1724
self.assert_(i in p10)
1725
self.assertFalse(10 in p10)
1726
# Test overridden behavior for dynamic classes
1727
class DProxy(object):
1728
def __init__(self, x):
1730
def __nonzero__(self):
1731
return not not self.x
1734
def __eq__(self, other):
1735
return self.x == other
1736
def __ne__(self, other):
1737
return self.x != other
1738
def __cmp__(self, other):
1739
return cmp(self.x, other.x)
1741
return "DProxy:%s" % self.x
1743
return "DProxy(%r)" % self.x
1744
def __contains__(self, value):
1745
return value in self.x
1749
self.assertFalse(p0)
1750
self.assert_(not not p1)
1751
self.assertEqual(hash(p0), hash(0))
1752
self.assertEqual(p0, p0)
1753
self.assertNotEqual(p0, p1)
1754
self.assertNotEqual(not p0, p0)
1755
self.assertEqual(not p0, p1)
1756
self.assertEqual(cmp(p0, p1), -1)
1757
self.assertEqual(cmp(p0, p0), 0)
1758
self.assertEqual(cmp(p0, p_1), 1)
1759
self.assertEqual(str(p0), "DProxy:0")
1760
self.assertEqual(repr(p0), "DProxy(0)")
1761
p10 = DProxy(range(10))
1762
self.assertFalse(-1 in p10)
1764
self.assert_(i in p10)
1765
self.assertFalse(10 in p10)
1767
# Safety test for __cmp__
1768
def unsafecmp(a, b):
1770
a.__class__.__cmp__(a, b)
1774
self.fail("shouldn't allow %s.__cmp__(%r, %r)" % (
1777
unsafecmp(u"123", "123")
1778
unsafecmp("123", u"123")
1784
def test_recursions(self):
1785
# Testing recursion checks ...
1787
def __new__(cls, letter):
1789
return str.__new__(cls)
1790
return str.__new__(cls, letter)
1795
# sys.stdout needs to be the original to trigger the recursion bug
1797
test_stdout = sys.stdout
1798
sys.stdout = test_support.get_original_stdout()
1800
# nothing should actually be printed, this should raise an exception
1802
except RuntimeError:
1805
self.fail("expected a RuntimeError for print recursion")
1807
sys.stdout = test_stdout
1812
A.__mul__ = types.MethodType(lambda self, x: self * x, None, A)
1815
except RuntimeError:
1818
self.fail("expected a RuntimeError")
1820
def test_weakrefs(self):
1821
# Testing weak references...
1827
self.assertEqual(r(), c)
1829
self.assertEqual(r(), None)
1831
class NoWeak(object):
1836
except TypeError, msg:
1837
self.assert_(str(msg).find("weak reference") >= 0)
1839
self.fail("weakref.ref(no) should be illegal")
1841
__slots__ = ['foo', '__weakref__']
1843
r = weakref.ref(yes)
1844
self.assertEqual(r(), yes)
1846
self.assertEqual(r(), None)
1849
def test_properties(self):
1850
# Testing property...
1854
def setx(self, value):
1858
x = property(getx, setx, delx, doc="I'm the x property.")
1860
self.assertFalse(hasattr(a, "x"))
1862
self.assertEqual(a._C__x, 42)
1863
self.assertEqual(a.x, 42)
1865
self.assertFalse(hasattr(a, "x"))
1866
self.assertFalse(hasattr(a, "_C__x"))
1868
self.assertEqual(C.x.__get__(a), 100)
1870
self.assertFalse(hasattr(a, "x"))
1872
raw = C.__dict__['x']
1873
self.assert_(isinstance(raw, property))
1876
self.assert_("__doc__" in attrs)
1877
self.assert_("fget" in attrs)
1878
self.assert_("fset" in attrs)
1879
self.assert_("fdel" in attrs)
1881
self.assertEqual(raw.__doc__, "I'm the x property.")
1882
self.assert_(raw.fget is C.__dict__['getx'])
1883
self.assert_(raw.fset is C.__dict__['setx'])
1884
self.assert_(raw.fdel is C.__dict__['delx'])
1886
for attr in "__doc__", "fget", "fset", "fdel":
1888
setattr(raw, attr, 42)
1889
except TypeError, msg:
1890
if str(msg).find('readonly') < 0:
1891
self.fail("when setting readonly attr %r on a property, "
1892
"got unexpected TypeError msg %r" % (attr, str(msg)))
1894
self.fail("expected TypeError from trying to set readonly %r "
1895
"attr on a property" % attr)
1898
__getitem__ = property(lambda s: 1/0)
1904
except ZeroDivisionError:
1907
self.fail("expected ZeroDivisionError from bad property")
1913
def setter(self_, value):
1916
prop = property(getter)
1917
self.assertEqual(prop.__doc__, "getter method")
1918
prop2 = property(fset=setter)
1919
self.assertEqual(prop2.__doc__, None)
1921
# this segfaulted in 2.5b2
1928
p = property(_testcapi.test_with_docstring)
1930
def test_properties_plus(self):
1932
foo = property(doc="hello")
1937
def foo(self, value):
1938
self._foo = abs(value)
1943
self.assertEqual(C.foo.__doc__, "hello")
1944
self.assertFalse(hasattr(c, "foo"))
1946
self.assert_(hasattr(c, '_foo'))
1947
self.assertEqual(c._foo, 42)
1948
self.assertEqual(c.foo, 42)
1950
self.assertFalse(hasattr(c, '_foo'))
1951
self.assertFalse(hasattr(c, "foo"))
1958
except AttributeError:
1962
self.assertEqual(d.foo, 24)
1971
def foo(self, value):
1974
def foo(self, value):
1975
self._foo = abs(value)
1977
def foo(self, value=None):
1982
self.assertEqual(e.foo, 42)
1990
def foo(self, value):
1991
self._foo = max(0, value)
1994
self.assertEqual(f.foo, 0)
1997
def test_dict_constructors(self):
1998
# Testing dict constructor ...
2000
self.assertEqual(d, {})
2002
self.assertEqual(d, {})
2003
d = dict({1: 2, 'a': 'b'})
2004
self.assertEqual(d, {1: 2, 'a': 'b'})
2005
self.assertEqual(d, dict(d.items()))
2006
self.assertEqual(d, dict(d.iteritems()))
2007
d = dict({'one':1, 'two':2})
2008
self.assertEqual(d, dict(one=1, two=2))
2009
self.assertEqual(d, dict(**d))
2010
self.assertEqual(d, dict({"one": 1}, two=2))
2011
self.assertEqual(d, dict([("two", 2)], one=1))
2012
self.assertEqual(d, dict([("one", 100), ("two", 200)], **d))
2013
self.assertEqual(d, dict(**d))
2015
for badarg in 0, 0L, 0j, "0", [0], (0,):
2022
# It's a sequence, and its elements are also sequences (gotta
2023
# love strings <wink>), but they aren't of length 2, so this
2024
# one seemed better as a ValueError than a TypeError.
2027
self.fail("no TypeError from dict(%r)" % badarg)
2029
self.fail("no TypeError from dict(%r)" % badarg)
2036
self.fail("no TypeError from dict({}, {})")
2039
# Lacks a .keys() method; will be added later.
2040
dict = {1:2, 3:4, 'a':1j}
2047
self.fail("no TypeError from dict(incomplete mapping)")
2049
Mapping.keys = lambda self: self.dict.keys()
2050
Mapping.__getitem__ = lambda self, i: self.dict[i]
2052
self.assertEqual(d, Mapping.dict)
2054
# Init from sequence of iterable objects, each producing a 2-sequence.
2055
class AddressBookEntry:
2056
def __init__(self, first, last):
2060
return iter([self.first, self.last])
2062
d = dict([AddressBookEntry('Tim', 'Warsaw'),
2063
AddressBookEntry('Barry', 'Peters'),
2064
AddressBookEntry('Tim', 'Peters'),
2065
AddressBookEntry('Barry', 'Warsaw')])
2066
self.assertEqual(d, {'Barry': 'Warsaw', 'Tim': 'Peters'})
2068
d = dict(zip(range(4), range(1, 5)))
2069
self.assertEqual(d, dict([(i, i+1) for i in range(4)]))
2071
# Bad sequence lengths.
2072
for bad in [('tooshort',)], [('too', 'long', 'by 1')]:
2078
self.fail("no ValueError from dict(%r)" % bad)
2083
self.assertEqual(dir(), ['junk', 'self'])
2086
# Just make sure these don't blow up!
2087
for arg in 2, 2L, 2j, 2e0, [2], "2", u"2", (2,), {2:2}, type, self.test_dir:
2090
# Try classic classes.
2093
def Cmethod(self): pass
2095
cstuff = ['Cdata', 'Cmethod', '__doc__', '__module__']
2096
self.assertEqual(dir(C), cstuff)
2097
self.assert_('im_self' in dir(C.Cmethod))
2099
c = C() # c.__doc__ is an odd thing to see here; ditto c.__module__.
2100
self.assertEqual(dir(c), cstuff)
2103
c.cmethod = lambda self: 0
2104
self.assertEqual(dir(c), cstuff + ['cdata', 'cmethod'])
2105
self.assert_('im_self' in dir(c.Cmethod))
2109
def Amethod(self): pass
2111
astuff = ['Adata', 'Amethod'] + cstuff
2112
self.assertEqual(dir(A), astuff)
2113
self.assert_('im_self' in dir(A.Amethod))
2115
self.assertEqual(dir(a), astuff)
2116
self.assert_('im_self' in dir(a.Amethod))
2118
a.amethod = lambda self: 3
2119
self.assertEqual(dir(a), astuff + ['adata', 'amethod'])
2121
# The same, but with new-style classes. Since these have object as a
2122
# base class, a lot more gets sucked in.
2123
def interesting(strings):
2124
return [s for s in strings if not s.startswith('_')]
2128
def Cmethod(self): pass
2130
cstuff = ['Cdata', 'Cmethod']
2131
self.assertEqual(interesting(dir(C)), cstuff)
2134
self.assertEqual(interesting(dir(c)), cstuff)
2135
self.assert_('im_self' in dir(C.Cmethod))
2138
c.cmethod = lambda self: 0
2139
self.assertEqual(interesting(dir(c)), cstuff + ['cdata', 'cmethod'])
2140
self.assert_('im_self' in dir(c.Cmethod))
2144
def Amethod(self): pass
2146
astuff = ['Adata', 'Amethod'] + cstuff
2147
self.assertEqual(interesting(dir(A)), astuff)
2148
self.assert_('im_self' in dir(A.Amethod))
2150
self.assertEqual(interesting(dir(a)), astuff)
2152
a.amethod = lambda self: 3
2153
self.assertEqual(interesting(dir(a)), astuff + ['adata', 'amethod'])
2154
self.assert_('im_self' in dir(a.Amethod))
2156
# Try a module subclass.
2163
names = [x for x in dir(minstance) if x not in ["__name__", "__doc__"]]
2164
self.assertEqual(names, ['a', 'b'])
2168
return "Not a dict!"
2169
__dict__ = property(getdict)
2171
m2instance = M2("m2")
2174
self.assertEqual(m2instance.__dict__, "Not a dict!")
2180
# Two essentially featureless objects, just inheriting stuff from
2182
self.assertEqual(dir(None), dir(Ellipsis))
2184
# Nasty test case for proxied objects
2185
class Wrapper(object):
2186
def __init__(self, obj):
2189
return "Wrapper(%s)" % repr(self.__obj)
2190
def __getitem__(self, key):
2191
return Wrapper(self.__obj[key])
2193
return len(self.__obj)
2194
def __getattr__(self, name):
2195
return Wrapper(getattr(self.__obj, name))
2198
def __getclass(self):
2199
return Wrapper(type(self))
2200
__class__ = property(__getclass)
2202
dir(C()) # This used to segfault
2204
def test_supers(self):
2211
self.assertEqual(A().meth(1), "A(1)")
2215
self.__super = super(B, self)
2217
return "B(%r)" % a + self.__super.meth(a)
2219
self.assertEqual(B().meth(2), "B(2)A(2)")
2223
return "C(%r)" % a + self.__super.meth(a)
2224
C._C__super = super(C)
2226
self.assertEqual(C().meth(3), "C(3)A(3)")
2230
return "D(%r)" % a + super(D, self).meth(a)
2232
self.assertEqual(D().meth(4), "D(4)C(4)B(4)A(4)")
2234
# Test for subclassing super
2236
class mysuper(super):
2237
def __init__(self, *args):
2238
return super(mysuper, self).__init__(*args)
2242
return "E(%r)" % a + mysuper(E, self).meth(a)
2244
self.assertEqual(E().meth(5), "E(5)D(5)C(5)B(5)A(5)")
2248
s = self.__super # == mysuper(F, self)
2249
return "F(%r)[%s]" % (a, s.__class__.__name__) + s.meth(a)
2250
F._F__super = mysuper(F)
2252
self.assertEqual(F().meth(6), "F(6)[mysuper]E(6)D(6)C(6)B(6)A(6)")
2254
# Make sure certain errors are raised
2261
self.fail("shouldn't allow super(D, 42)")
2268
self.fail("shouldn't allow super(D, C())")
2271
super(D).__get__(12)
2275
self.fail("shouldn't allow super(D).__get__(12)")
2278
super(D).__get__(C())
2282
self.fail("shouldn't allow super(D).__get__(C())")
2284
# Make sure data descriptors can be overridden and accessed via super
2285
# (new feature in Python 2.3)
2287
class DDbase(object):
2288
def getx(self): return 42
2291
class DDsub(DDbase):
2292
def getx(self): return "hello"
2296
self.assertEqual(dd.x, "hello")
2297
self.assertEqual(super(DDsub, dd).x, 42)
2299
# Ensure that super() lookup of descriptor from classmethod
2300
# works (SF ID# 743627)
2303
aProp = property(lambda self: "foo")
2308
return super(Sub,klass).aProp
2310
self.assertEqual(Sub.test(), Base.aProp)
2312
# Verify that super() doesn't allow keyword args
2318
self.assertEqual("super shouldn't accept keyword args")
2320
def test_basic_inheritance(self):
2321
# Testing inheritance from basic types...
2326
def __add__(self, other):
2327
return hexint(int.__add__(self, other))
2328
# (Note that overriding __radd__ doesn't work,
2329
# because the int type gets first dibs.)
2330
self.assertEqual(repr(hexint(7) + 9), "0x10")
2331
self.assertEqual(repr(hexint(1000) + 7), "0x3ef")
2333
self.assertEqual(a, 12345)
2334
self.assertEqual(int(a), 12345)
2335
self.assert_(int(a).__class__ is int)
2336
self.assertEqual(hash(a), hash(12345))
2337
self.assert_((+a).__class__ is int)
2338
self.assert_((a >> 0).__class__ is int)
2339
self.assert_((a << 0).__class__ is int)
2340
self.assert_((hexint(0) << 12).__class__ is int)
2341
self.assert_((hexint(0) >> 12).__class__ is int)
2343
class octlong(long):
2350
def __add__(self, other):
2351
return self.__class__(super(octlong, self).__add__(other))
2353
self.assertEqual(str(octlong(3) + 5), "010")
2354
# (Note that overriding __radd__ here only seems to work
2355
# because the example uses a short int left argument.)
2356
self.assertEqual(str(5 + octlong(3000)), "05675")
2358
self.assertEqual(a, 12345L)
2359
self.assertEqual(long(a), 12345L)
2360
self.assertEqual(hash(a), hash(12345L))
2361
self.assert_(long(a).__class__ is long)
2362
self.assert_((+a).__class__ is long)
2363
self.assert_((-a).__class__ is long)
2364
self.assert_((-octlong(0)).__class__ is long)
2365
self.assert_((a >> 0).__class__ is long)
2366
self.assert_((a << 0).__class__ is long)
2367
self.assert_((a - 0).__class__ is long)
2368
self.assert_((a * 1).__class__ is long)
2369
self.assert_((a ** 1).__class__ is long)
2370
self.assert_((a // 1).__class__ is long)
2371
self.assert_((1 * a).__class__ is long)
2372
self.assert_((a | 0).__class__ is long)
2373
self.assert_((a ^ 0).__class__ is long)
2374
self.assert_((a & -1L).__class__ is long)
2375
self.assert_((octlong(0) << 12).__class__ is long)
2376
self.assert_((octlong(0) >> 12).__class__ is long)
2377
self.assert_(abs(octlong(0)).__class__ is long)
2379
# Because octlong overrides __add__, we can't check the absence of +0
2380
# optimizations using octlong.
2381
class longclone(long):
2384
self.assert_((a + 0).__class__ is long)
2385
self.assert_((0 + a).__class__ is long)
2387
# Check that negative clones don't segfault
2389
self.assertEqual(a.__dict__, {})
2390
self.assertEqual(long(a), -1) # self.assert_ PyNumber_Long() copies the sign bit
2392
class precfloat(float):
2393
__slots__ = ['prec']
2394
def __init__(self, value=0.0, prec=12):
2395
self.prec = int(prec)
2397
return "%.*g" % (self.prec, self)
2398
self.assertEqual(repr(precfloat(1.1)), "1.1")
2399
a = precfloat(12345)
2400
self.assertEqual(a, 12345.0)
2401
self.assertEqual(float(a), 12345.0)
2402
self.assert_(float(a).__class__ is float)
2403
self.assertEqual(hash(a), hash(12345.0))
2404
self.assert_((+a).__class__ is float)
2406
class madcomplex(complex):
2408
return "%.17gj%+.17g" % (self.imag, self.real)
2409
a = madcomplex(-3, 4)
2410
self.assertEqual(repr(a), "4j-3")
2411
base = complex(-3, 4)
2412
self.assertEqual(base.__class__, complex)
2413
self.assertEqual(a, base)
2414
self.assertEqual(complex(a), base)
2415
self.assertEqual(complex(a).__class__, complex)
2416
a = madcomplex(a) # just trying another form of the constructor
2417
self.assertEqual(repr(a), "4j-3")
2418
self.assertEqual(a, base)
2419
self.assertEqual(complex(a), base)
2420
self.assertEqual(complex(a).__class__, complex)
2421
self.assertEqual(hash(a), hash(base))
2422
self.assertEqual((+a).__class__, complex)
2423
self.assertEqual((a + 0).__class__, complex)
2424
self.assertEqual(a + 0, base)
2425
self.assertEqual((a - 0).__class__, complex)
2426
self.assertEqual(a - 0, base)
2427
self.assertEqual((a * 1).__class__, complex)
2428
self.assertEqual(a * 1, base)
2429
self.assertEqual((a / 1).__class__, complex)
2430
self.assertEqual(a / 1, base)
2432
class madtuple(tuple):
2435
if self._rev is not None:
2439
self._rev = self.__class__(L)
2441
a = madtuple((1,2,3,4,5,6,7,8,9,0))
2442
self.assertEqual(a, (1,2,3,4,5,6,7,8,9,0))
2443
self.assertEqual(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1)))
2444
self.assertEqual(a.rev().rev(), madtuple((1,2,3,4,5,6,7,8,9,0)))
2445
for i in range(512):
2446
t = madtuple(range(i))
2449
self.assertEqual(v, t)
2450
a = madtuple((1,2,3,4,5))
2451
self.assertEqual(tuple(a), (1,2,3,4,5))
2452
self.assert_(tuple(a).__class__ is tuple)
2453
self.assertEqual(hash(a), hash((1,2,3,4,5)))
2454
self.assert_(a[:].__class__ is tuple)
2455
self.assert_((a * 1).__class__ is tuple)
2456
self.assert_((a * 0).__class__ is tuple)
2457
self.assert_((a + ()).__class__ is tuple)
2459
self.assertEqual(tuple(a), ())
2460
self.assert_(tuple(a).__class__ is tuple)
2461
self.assert_((a + a).__class__ is tuple)
2462
self.assert_((a * 0).__class__ is tuple)
2463
self.assert_((a * 1).__class__ is tuple)
2464
self.assert_((a * 2).__class__ is tuple)
2465
self.assert_(a[:].__class__ is tuple)
2467
class madstring(str):
2470
if self._rev is not None:
2474
self._rev = self.__class__("".join(L))
2476
s = madstring("abcdefghijklmnopqrstuvwxyz")
2477
self.assertEqual(s, "abcdefghijklmnopqrstuvwxyz")
2478
self.assertEqual(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba"))
2479
self.assertEqual(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz"))
2480
for i in range(256):
2481
s = madstring("".join(map(chr, range(i))))
2484
self.assertEqual(u, s)
2485
s = madstring("12345")
2486
self.assertEqual(str(s), "12345")
2487
self.assert_(str(s).__class__ is str)
2491
self.assertEqual(s, base)
2492
self.assertEqual(str(s), base)
2493
self.assert_(str(s).__class__ is str)
2494
self.assertEqual(hash(s), hash(base))
2495
self.assertEqual({s: 1}[base], 1)
2496
self.assertEqual({base: 1}[s], 1)
2497
self.assert_((s + "").__class__ is str)
2498
self.assertEqual(s + "", base)
2499
self.assert_(("" + s).__class__ is str)
2500
self.assertEqual("" + s, base)
2501
self.assert_((s * 0).__class__ is str)
2502
self.assertEqual(s * 0, "")
2503
self.assert_((s * 1).__class__ is str)
2504
self.assertEqual(s * 1, base)
2505
self.assert_((s * 2).__class__ is str)
2506
self.assertEqual(s * 2, base + base)
2507
self.assert_(s[:].__class__ is str)
2508
self.assertEqual(s[:], base)
2509
self.assert_(s[0:0].__class__ is str)
2510
self.assertEqual(s[0:0], "")
2511
self.assert_(s.strip().__class__ is str)
2512
self.assertEqual(s.strip(), base)
2513
self.assert_(s.lstrip().__class__ is str)
2514
self.assertEqual(s.lstrip(), base)
2515
self.assert_(s.rstrip().__class__ is str)
2516
self.assertEqual(s.rstrip(), base)
2517
identitytab = ''.join([chr(i) for i in range(256)])
2518
self.assert_(s.translate(identitytab).__class__ is str)
2519
self.assertEqual(s.translate(identitytab), base)
2520
self.assert_(s.translate(identitytab, "x").__class__ is str)
2521
self.assertEqual(s.translate(identitytab, "x"), base)
2522
self.assertEqual(s.translate(identitytab, "\x00"), "")
2523
self.assert_(s.replace("x", "x").__class__ is str)
2524
self.assertEqual(s.replace("x", "x"), base)
2525
self.assert_(s.ljust(len(s)).__class__ is str)
2526
self.assertEqual(s.ljust(len(s)), base)
2527
self.assert_(s.rjust(len(s)).__class__ is str)
2528
self.assertEqual(s.rjust(len(s)), base)
2529
self.assert_(s.center(len(s)).__class__ is str)
2530
self.assertEqual(s.center(len(s)), base)
2531
self.assert_(s.lower().__class__ is str)
2532
self.assertEqual(s.lower(), base)
2534
class madunicode(unicode):
2537
if self._rev is not None:
2541
self._rev = self.__class__(u"".join(L))
2543
u = madunicode("ABCDEF")
2544
self.assertEqual(u, u"ABCDEF")
2545
self.assertEqual(u.rev(), madunicode(u"FEDCBA"))
2546
self.assertEqual(u.rev().rev(), madunicode(u"ABCDEF"))
2548
u = madunicode(base)
2549
self.assertEqual(unicode(u), base)
2550
self.assert_(unicode(u).__class__ is unicode)
2551
self.assertEqual(hash(u), hash(base))
2552
self.assertEqual({u: 1}[base], 1)
2553
self.assertEqual({base: 1}[u], 1)
2554
self.assert_(u.strip().__class__ is unicode)
2555
self.assertEqual(u.strip(), base)
2556
self.assert_(u.lstrip().__class__ is unicode)
2557
self.assertEqual(u.lstrip(), base)
2558
self.assert_(u.rstrip().__class__ is unicode)
2559
self.assertEqual(u.rstrip(), base)
2560
self.assert_(u.replace(u"x", u"x").__class__ is unicode)
2561
self.assertEqual(u.replace(u"x", u"x"), base)
2562
self.assert_(u.replace(u"xy", u"xy").__class__ is unicode)
2563
self.assertEqual(u.replace(u"xy", u"xy"), base)
2564
self.assert_(u.center(len(u)).__class__ is unicode)
2565
self.assertEqual(u.center(len(u)), base)
2566
self.assert_(u.ljust(len(u)).__class__ is unicode)
2567
self.assertEqual(u.ljust(len(u)), base)
2568
self.assert_(u.rjust(len(u)).__class__ is unicode)
2569
self.assertEqual(u.rjust(len(u)), base)
2570
self.assert_(u.lower().__class__ is unicode)
2571
self.assertEqual(u.lower(), base)
2572
self.assert_(u.upper().__class__ is unicode)
2573
self.assertEqual(u.upper(), base)
2574
self.assert_(u.capitalize().__class__ is unicode)
2575
self.assertEqual(u.capitalize(), base)
2576
self.assert_(u.title().__class__ is unicode)
2577
self.assertEqual(u.title(), base)
2578
self.assert_((u + u"").__class__ is unicode)
2579
self.assertEqual(u + u"", base)
2580
self.assert_((u"" + u).__class__ is unicode)
2581
self.assertEqual(u"" + u, base)
2582
self.assert_((u * 0).__class__ is unicode)
2583
self.assertEqual(u * 0, u"")
2584
self.assert_((u * 1).__class__ is unicode)
2585
self.assertEqual(u * 1, base)
2586
self.assert_((u * 2).__class__ is unicode)
2587
self.assertEqual(u * 2, base + base)
2588
self.assert_(u[:].__class__ is unicode)
2589
self.assertEqual(u[:], base)
2590
self.assert_(u[0:0].__class__ is unicode)
2591
self.assertEqual(u[0:0], u"")
2593
class sublist(list):
2595
a = sublist(range(5))
2596
self.assertEqual(a, range(5))
2598
self.assertEqual(a, range(5) + ["hello"])
2600
self.assertEqual(a, range(6))
2601
a.extend(range(6, 20))
2602
self.assertEqual(a, range(20))
2604
self.assertEqual(a, range(15))
2606
self.assertEqual(len(a), 10)
2607
self.assertEqual(a, range(10))
2608
self.assertEqual(list(a), range(10))
2609
self.assertEqual(a[0], 0)
2610
self.assertEqual(a[9], 9)
2611
self.assertEqual(a[-10], 0)
2612
self.assertEqual(a[-1], 9)
2613
self.assertEqual(a[:5], range(5))
2615
class CountedInput(file):
2616
"""Counts lines read by self.readline().
2618
self.lineno is the 0-based ordinal of the last line read, up to
2619
a maximum of one greater than the number of lines in the file.
2621
self.ateof is true if and only if the final "" line has been read,
2622
at which point self.lineno stops incrementing, and further calls
2623
to readline() continue to return "".
2631
s = file.readline(self)
2632
# Next line works too.
2633
# s = super(CountedInput, self).readline()
2639
f = file(name=test_support.TESTFN, mode='w')
2640
lines = ['a\n', 'b\n', 'c\n']
2644
f = CountedInput(test_support.TESTFN)
2645
for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]):
2647
self.assertEqual(expected, got)
2648
self.assertEqual(f.lineno, i)
2649
self.assertEqual(f.ateof, (i > len(lines)))
2656
test_support.unlink(test_support.TESTFN)
2658
def test_keywords(self):
2659
# Testing keyword args to basic type constructors ...
2660
self.assertEqual(int(x=1), 1)
2661
self.assertEqual(float(x=2), 2.0)
2662
self.assertEqual(long(x=3), 3L)
2663
self.assertEqual(complex(imag=42, real=666), complex(666, 42))
2664
self.assertEqual(str(object=500), '500')
2665
self.assertEqual(unicode(string='abc', errors='strict'), u'abc')
2666
self.assertEqual(tuple(sequence=range(3)), (0, 1, 2))
2667
self.assertEqual(list(sequence=(0, 1, 2)), range(3))
2668
# note: as of Python 2.3, dict() no longer has an "items" keyword arg
2670
for constructor in (int, float, long, complex, str, unicode,
2673
constructor(bogus_keyword_arg=1)
2677
self.fail("expected TypeError from bogus keyword argument to %r"
2680
def test_str_subclass_as_dict_key(self):
2681
# Testing a str subclass used as dict key ..
2684
"""Sublcass of str that computes __eq__ case-insensitively.
2686
Also computes a hash code of the string in canonical form.
2689
def __init__(self, value):
2690
self.canonical = value.lower()
2691
self.hashcode = hash(self.canonical)
2693
def __eq__(self, other):
2694
if not isinstance(other, cistr):
2695
other = cistr(other)
2696
return self.canonical == other.canonical
2699
return self.hashcode
2701
self.assertEqual(cistr('ABC'), 'abc')
2702
self.assertEqual('aBc', cistr('ABC'))
2703
self.assertEqual(str(cistr('ABC')), 'ABC')
2705
d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3}
2706
self.assertEqual(d[cistr('one')], 1)
2707
self.assertEqual(d[cistr('tWo')], 2)
2708
self.assertEqual(d[cistr('THrEE')], 3)
2709
self.assert_(cistr('ONe') in d)
2710
self.assertEqual(d.get(cistr('thrEE')), 3)
2712
def test_classic_comparisons(self):
2713
# Testing classic comparisons...
2717
for base in (classic, int, object):
2719
def __init__(self, value):
2720
self.value = int(value)
2721
def __cmp__(self, other):
2722
if isinstance(other, C):
2723
return cmp(self.value, other.value)
2724
if isinstance(other, int) or isinstance(other, long):
2725
return cmp(self.value, other)
2726
return NotImplemented
2727
__hash__ = None # Silence Py3k warning
2732
self.assertEqual(c1, 1)
2733
c = {1: c1, 2: c2, 3: c3}
2736
self.assert_(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
2737
for op in "<", "<=", "==", "!=", ">", ">=":
2738
self.assert_(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
2739
"x=%d, y=%d" % (x, y))
2740
self.assert_(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y))
2741
self.assert_(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
2743
def test_rich_comparisons(self):
2744
# Testing rich comparisons...
2748
self.assertEqual(z, 1+0j)
2749
self.assertEqual(1+0j, z)
2751
def __eq__(self, other):
2753
return abs(self - other) <= 1e-6
2755
return NotImplemented
2756
__hash__ = None # Silence Py3k warning
2758
self.assertEqual(zz, 1+0j)
2759
self.assertEqual(1+0j, zz)
2763
for base in (classic, int, object, list):
2765
def __init__(self, value):
2766
self.value = int(value)
2767
def __cmp__(self_, other):
2768
self.fail("shouldn't call __cmp__")
2769
__hash__ = None # Silence Py3k warning
2770
def __eq__(self, other):
2771
if isinstance(other, C):
2772
return self.value == other.value
2773
if isinstance(other, int) or isinstance(other, long):
2774
return self.value == other
2775
return NotImplemented
2776
def __ne__(self, other):
2777
if isinstance(other, C):
2778
return self.value != other.value
2779
if isinstance(other, int) or isinstance(other, long):
2780
return self.value != other
2781
return NotImplemented
2782
def __lt__(self, other):
2783
if isinstance(other, C):
2784
return self.value < other.value
2785
if isinstance(other, int) or isinstance(other, long):
2786
return self.value < other
2787
return NotImplemented
2788
def __le__(self, other):
2789
if isinstance(other, C):
2790
return self.value <= other.value
2791
if isinstance(other, int) or isinstance(other, long):
2792
return self.value <= other
2793
return NotImplemented
2794
def __gt__(self, other):
2795
if isinstance(other, C):
2796
return self.value > other.value
2797
if isinstance(other, int) or isinstance(other, long):
2798
return self.value > other
2799
return NotImplemented
2800
def __ge__(self, other):
2801
if isinstance(other, C):
2802
return self.value >= other.value
2803
if isinstance(other, int) or isinstance(other, long):
2804
return self.value >= other
2805
return NotImplemented
2809
self.assertEqual(c1, 1)
2810
c = {1: c1, 2: c2, 3: c3}
2813
for op in "<", "<=", "==", "!=", ">", ">=":
2814
self.assert_(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
2815
"x=%d, y=%d" % (x, y))
2816
self.assert_(eval("c[x] %s y" % op) == eval("x %s y" % op),
2817
"x=%d, y=%d" % (x, y))
2818
self.assert_(eval("x %s c[y]" % op) == eval("x %s y" % op),
2819
"x=%d, y=%d" % (x, y))
2821
def test_coercions(self):
2822
# Testing coercions...
2831
class F(float): pass
2838
class C(complex): pass
2848
def test_descrdoc(self):
2849
# Testing descriptor doc strings...
2850
def check(descr, what):
2851
self.assertEqual(descr.__doc__, what)
2852
check(file.closed, "True if the file is closed") # getset descriptor
2853
check(file.name, "file name") # member descriptor
2855
def test_doc_descriptor(self):
2856
# Testing __doc__ descriptor...
2858
class DocDescr(object):
2859
def __get__(self, object, otype):
2861
object = object.__class__.__name__ + ' instance'
2863
otype = otype.__name__
2864
return 'object=%s; type=%s' % (object, otype)
2866
__doc__ = DocDescr()
2867
class NewClass(object):
2868
__doc__ = DocDescr()
2869
self.assertEqual(OldClass.__doc__, 'object=None; type=OldClass')
2870
self.assertEqual(OldClass().__doc__, 'object=OldClass instance; type=OldClass')
2871
self.assertEqual(NewClass.__doc__, 'object=None; type=NewClass')
2872
self.assertEqual(NewClass().__doc__, 'object=NewClass instance; type=NewClass')
2874
def test_set_class(self):
2875
# Testing __class__ assignment...
2876
class C(object): pass
2877
class D(object): pass
2878
class E(object): pass
2880
for cls in C, D, E, F:
2881
for cls2 in C, D, E, F:
2884
self.assert_(x.__class__ is cls2)
2886
self.assert_(x.__class__ is cls)
2893
self.fail("shouldn't allow %r.__class__ = %r" % (x, C))
2895
delattr(x, "__class__")
2899
self.fail("shouldn't allow del %r.__class__" % x)
2904
cant(object(), list)
2905
cant(list(), object)
2906
class Int(int): __slots__ = []
2916
__slots__ = ["a", "b"]
2918
__slots__ = ["b", "a"]
2923
__slots__ = ["a", "b"]
2926
__slots__ = [unicode("a"), unicode("b")]
2928
__slots__ = ["c", "b"]
2930
__slots__ = ["a", "b", "d"]
2936
__slots__ = ["__weakref__"]
2938
__slots__ = ["__dict__"]
2942
__slots__ = ["__dict__", "__weakref__"]
2944
for cls, cls2 in ((G, H), (G, I), (I, H), (Q, R), (R, Q)):
2948
self.assert_(x.__class__ is cls2,
2949
"assigning %r as __class__ for %r silently failed" % (cls2, x))
2950
self.assertEqual(x.a, 1)
2952
self.assert_(x.__class__ is cls,
2953
"assigning %r as __class__ for %r silently failed" % (cls, x))
2954
self.assertEqual(x.a, 1)
2955
for cls in G, J, K, L, M, N, P, R, list, Int:
2956
for cls2 in G, J, K, L, M, N, P, R, list, Int:
2961
def test_set_dict(self):
2962
# Testing __dict__ assignment...
2963
class C(object): pass
2965
a.__dict__ = {'b': 1}
2966
self.assertEqual(a.b, 1)
2970
except (AttributeError, TypeError):
2973
self.fail("shouldn't allow %r.__dict__ = %r" % (x, dict))
2977
del a.__dict__ # Deleting __dict__ is allowed
2981
def verify_dict_readonly(x):
2983
x has to be an instance of a class inheriting from Base.
2988
except (AttributeError, TypeError):
2991
self.fail("shouldn't allow del %r.__dict__" % x)
2992
dict_descr = Base.__dict__["__dict__"]
2994
dict_descr.__set__(x, {})
2995
except (AttributeError, TypeError):
2998
self.fail("dict_descr allowed access to %r's dict" % x)
3000
# Classes don't allow __dict__ assignment and have readonly dicts
3001
class Meta1(type, Base):
3003
class Meta2(Base, type):
3006
__metaclass__ = Meta1
3008
__metaclass__ = Meta2
3010
verify_dict_readonly(cls)
3011
class_dict = cls.__dict__
3013
class_dict["spam"] = "eggs"
3017
self.fail("%r's __dict__ can be modified" % cls)
3019
# Modules also disallow __dict__ assignment
3020
class Module1(types.ModuleType, Base):
3022
class Module2(Base, types.ModuleType):
3024
for ModuleType in Module1, Module2:
3025
mod = ModuleType("spam")
3026
verify_dict_readonly(mod)
3027
mod.__dict__["spam"] = "eggs"
3029
# Exception's __dict__ can be replaced, but not deleted
3030
class Exception1(Exception, Base):
3032
class Exception2(Base, Exception):
3034
for ExceptionType in Exception, Exception1, Exception2:
3036
e.__dict__ = {"a": 1}
3037
self.assertEqual(e.a, 1)
3040
except (TypeError, AttributeError):
3043
self.fail("%r's __dict__ can be deleted" % e)
3045
def test_pickles(self):
3046
# Testing pickling and copying new-style classes and objects...
3047
import pickle, cPickle
3056
def __init__(self, a, b):
3057
super(C, self).__init__()
3061
return "C(%r, %r)" % (self.a, self.b)
3065
def __new__(cls, a, b):
3066
return super(C1, cls).__new__(cls)
3067
def __getnewargs__(self):
3068
return (self.a, self.b)
3069
def __init__(self, a, b):
3073
return "C1(%r, %r)<%r>" % (self.a, self.b, list(self))
3077
def __new__(cls, a, b, val=0):
3078
return super(C2, cls).__new__(cls, val)
3079
def __getnewargs__(self):
3080
return (self.a, self.b, int(self))
3081
def __init__(self, a, b, val=0):
3085
return "C2(%r, %r)<%r>" % (self.a, self.b, int(self))
3089
def __init__(self, foo):
3091
def __getstate__(self):
3093
def __setstate__(self, foo):
3096
global C4classic, C4
3097
class C4classic: # classic
3099
class C4(C4classic, object): # mixed inheritance
3102
for p in pickle, cPickle:
3104
for cls in C, C1, C2:
3105
s = p.dumps(cls, bin)
3107
self.assert_(cls2 is cls)
3109
a = C1(1, 2); a.append(42); a.append(24)
3110
b = C2("hello", "world", 42)
3111
s = p.dumps((a, b), bin)
3113
self.assertEqual(x.__class__, a.__class__)
3114
self.assertEqual(sorteditems(x.__dict__), sorteditems(a.__dict__))
3115
self.assertEqual(y.__class__, b.__class__)
3116
self.assertEqual(sorteditems(y.__dict__), sorteditems(b.__dict__))
3117
self.assertEqual(repr(x), repr(a))
3118
self.assertEqual(repr(y), repr(b))
3119
# Test for __getstate__ and __setstate__ on new style class
3123
self.assertEqual(u.__class__, v.__class__)
3124
self.assertEqual(u.foo, v.foo)
3125
# Test for picklability of hybrid class
3130
self.assertEqual(u.__class__, v.__class__)
3131
self.assertEqual(u.foo, v.foo)
3133
# Testing copy.deepcopy()
3135
for cls in C, C1, C2:
3136
cls2 = copy.deepcopy(cls)
3137
self.assert_(cls2 is cls)
3139
a = C1(1, 2); a.append(42); a.append(24)
3140
b = C2("hello", "world", 42)
3141
x, y = copy.deepcopy((a, b))
3142
self.assertEqual(x.__class__, a.__class__)
3143
self.assertEqual(sorteditems(x.__dict__), sorteditems(a.__dict__))
3144
self.assertEqual(y.__class__, b.__class__)
3145
self.assertEqual(sorteditems(y.__dict__), sorteditems(b.__dict__))
3146
self.assertEqual(repr(x), repr(a))
3147
self.assertEqual(repr(y), repr(b))
3149
def test_pickle_slots(self):
3150
# Testing pickling of classes with __slots__ ...
3151
import pickle, cPickle
3152
# Pickling of classes with __slots__ but without __getstate__ should fail
3156
for base in [object, B]:
3166
self.fail("should fail: pickle C instance - %s" % base)
3172
self.fail("should fail: cPickle C instance - %s" % base)
3178
self.fail("should fail: pickle D instance - %s" % base)
3184
self.fail("should fail: cPickle D instance - %s" % base)
3185
# Give C a nice generic __getstate__ and __setstate__
3188
def __getstate__(self):
3190
d = self.__dict__.copy()
3191
except AttributeError:
3193
for cls in self.__class__.__mro__:
3194
for sn in cls.__dict__.get('__slots__', ()):
3196
d[sn] = getattr(self, sn)
3197
except AttributeError:
3200
def __setstate__(self, d):
3201
for k, v in d.items():
3205
# Now it should work
3207
y = pickle.loads(pickle.dumps(x))
3208
self.assertEqual(hasattr(y, 'a'), 0)
3209
y = cPickle.loads(cPickle.dumps(x))
3210
self.assertEqual(hasattr(y, 'a'), 0)
3212
y = pickle.loads(pickle.dumps(x))
3213
self.assertEqual(y.a, 42)
3214
y = cPickle.loads(cPickle.dumps(x))
3215
self.assertEqual(y.a, 42)
3219
y = pickle.loads(pickle.dumps(x))
3220
self.assertEqual(y.a + y.b, 142)
3221
y = cPickle.loads(cPickle.dumps(x))
3222
self.assertEqual(y.a + y.b, 142)
3223
# A subclass that adds a slot should also work
3229
y = pickle.loads(pickle.dumps(x))
3230
self.assertEqual(y.a, x.a)
3231
self.assertEqual(y.b, x.b)
3232
y = cPickle.loads(cPickle.dumps(x))
3233
self.assertEqual(y.a, x.a)
3234
self.assertEqual(y.b, x.b)
3236
def test_binary_operator_override(self):
3237
# Testing overrides of binary operations...
3240
return "I(%r)" % int(self)
3241
def __add__(self, other):
3242
return I(int(self) + int(other))
3244
def __pow__(self, other, mod=None):
3246
return I(pow(int(self), int(other)))
3248
return I(pow(int(self), int(other), int(mod)))
3249
def __rpow__(self, other, mod=None):
3251
return I(pow(int(other), int(self), mod))
3253
return I(pow(int(other), int(self), int(mod)))
3255
self.assertEqual(repr(I(1) + I(2)), "I(3)")
3256
self.assertEqual(repr(I(1) + 2), "I(3)")
3257
self.assertEqual(repr(1 + I(2)), "I(3)")
3258
self.assertEqual(repr(I(2) ** I(3)), "I(8)")
3259
self.assertEqual(repr(2 ** I(3)), "I(8)")
3260
self.assertEqual(repr(I(2) ** 3), "I(8)")
3261
self.assertEqual(repr(pow(I(2), I(3), I(5))), "I(3)")
3263
def __eq__(self, other):
3264
return self.lower() == other.lower()
3265
__hash__ = None # Silence Py3k warning
3267
def test_subclass_propagation(self):
3268
# Testing propagation of slot functions to subclasses...
3278
orig_hash = hash(d) # related to id(d) in platform-dependent ways
3279
A.__hash__ = lambda self: 42
3280
self.assertEqual(hash(d), 42)
3281
C.__hash__ = lambda self: 314
3282
self.assertEqual(hash(d), 314)
3283
B.__hash__ = lambda self: 144
3284
self.assertEqual(hash(d), 144)
3285
D.__hash__ = lambda self: 100
3286
self.assertEqual(hash(d), 100)
3288
self.assertRaises(TypeError, hash, d)
3290
self.assertEqual(hash(d), 144)
3292
self.assertRaises(TypeError, hash, d)
3294
self.assertEqual(hash(d), 314)
3296
self.assertRaises(TypeError, hash, d)
3298
self.assertEqual(hash(d), 42)
3300
self.assertRaises(TypeError, hash, d)
3302
self.assertEqual(hash(d), orig_hash)
3305
self.assertEqual(d.foo, 42)
3306
self.assertEqual(d.bar, 42)
3307
def __getattribute__(self, name):
3310
return object.__getattribute__(self, name)
3311
A.__getattribute__ = __getattribute__
3312
self.assertEqual(d.foo, 24)
3313
self.assertEqual(d.bar, 42)
3314
def __getattr__(self, name):
3315
if name in ("spam", "foo", "bar"):
3317
raise AttributeError, name
3318
B.__getattr__ = __getattr__
3319
self.assertEqual(d.spam, "hello")
3320
self.assertEqual(d.foo, 24)
3321
self.assertEqual(d.bar, 42)
3322
del A.__getattribute__
3323
self.assertEqual(d.foo, 42)
3325
self.assertEqual(d.foo, "hello")
3326
self.assertEqual(d.bar, 42)
3330
except AttributeError:
3333
self.fail("d.foo should be undefined now")
3335
# Test a nasty bug in recurse_down_subclasses()
3343
A.__setitem__ = lambda *a: None # crash
3345
def test_buffer_inheritance(self):
3346
# Testing that buffer interface is inherited ...
3349
# SF bug [#470040] ParseTuple t# vs subclasses.
3355
# b2a_hex uses the buffer interface to get its argument's value, via
3356
# PyArg_ParseTuple 't#' code.
3357
self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base))
3359
# It's not clear that unicode will continue to support the character
3360
# buffer interface, and this test will fail if that's taken away.
3361
class MyUni(unicode):
3365
self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base))
3372
self.fail('subclass of int should not have a buffer interface')
3376
def test_str_of_str_subclass(self):
3377
# Testing __str__ defined in subclass of str ...
3381
class octetstring(str):
3383
return binascii.b2a_hex(self)
3385
return self + " repr"
3387
o = octetstring('A')
3388
self.assertEqual(type(o), octetstring)
3389
self.assertEqual(type(str(o)), str)
3390
self.assertEqual(type(repr(o)), str)
3391
self.assertEqual(ord(o), 0x41)
3392
self.assertEqual(str(o), '41')
3393
self.assertEqual(repr(o), 'A repr')
3394
self.assertEqual(o.__str__(), '41')
3395
self.assertEqual(o.__repr__(), 'A repr')
3397
capture = cStringIO.StringIO()
3398
# Calling str() or not exercises different internal paths.
3400
print >> capture, str(o)
3401
self.assertEqual(capture.getvalue(), '41\n41\n')
3404
def test_keyword_arguments(self):
3405
# Testing keyword arguments to __init__, __call__...
3407
self.assertEqual(f.__call__(a=42), 42)
3409
list.__init__(a, sequence=[0, 1, 2])
3410
self.assertEqual(a, [0, 1, 2])
3412
def test_recursive_call(self):
3413
# Testing recursive __call__() by setting to instance of class...
3420
except RuntimeError:
3423
self.fail("Recursion limit should have been reached for __call__()")
3425
def test_delete_hook(self):
3426
# Testing __del__ hook...
3432
self.assertEqual(log, [])
3434
self.assertEqual(log, [1])
3436
class D(object): pass
3439
except TypeError: pass
3440
else: self.fail("invalid del() didn't raise TypeError")
3442
def test_hash_inheritance(self):
3443
# Testing hash of mutable subclasses...
3453
self.fail("hash() of dict subclass should fail")
3463
self.fail("hash() of list subclass should fail")
3465
def test_str_operations(self):
3467
except TypeError: pass
3468
else: self.fail("'' + 5 doesn't raise TypeError")
3471
except ValueError: pass
3472
else: self.fail("''.split('') doesn't raise ValueError")
3475
except TypeError: pass
3476
else: self.fail("''.join([0]) doesn't raise TypeError")
3479
except ValueError: pass
3480
else: self.fail("''.rindex('5') doesn't raise ValueError")
3483
except TypeError: pass
3484
else: self.fail("'%(n)s' % None doesn't raise TypeError")
3487
except ValueError: pass
3488
else: self.fail("'%(n' % {} '' doesn't raise ValueError")
3490
try: '%*s' % ('abc')
3491
except TypeError: pass
3492
else: self.fail("'%*s' % ('abc') doesn't raise TypeError")
3494
try: '%*.*s' % ('abc', 5)
3495
except TypeError: pass
3496
else: self.fail("'%*.*s' % ('abc', 5) doesn't raise TypeError")
3499
except TypeError: pass
3500
else: self.fail("'%s' % (1, 2) doesn't raise TypeError")
3503
except ValueError: pass
3504
else: self.fail("'%' % None doesn't raise ValueError")
3506
self.assertEqual('534253'.isdigit(), 1)
3507
self.assertEqual('534253x'.isdigit(), 0)
3508
self.assertEqual('%c' % 5, '\x05')
3509
self.assertEqual('%c' % '5', '5')
3511
def test_deepcopy_recursive(self):
3512
# Testing deepcopy of recursive objects...
3519
z = deepcopy(a) # This blew up before
3521
def test_unintialized_modules(self):
3522
# Testing uninitialized module objects...
3523
from types import ModuleType as M
3526
self.assertEqual(hasattr(m, "__name__"), 0)
3527
self.assertEqual(hasattr(m, "__file__"), 0)
3528
self.assertEqual(hasattr(m, "foo"), 0)
3529
self.assertEqual(m.__dict__, None)
3531
self.assertEqual(m.__dict__, {"foo": 1})
3533
def test_funny_new(self):
3534
# Testing __new__ returning something unexpected...
3536
def __new__(cls, arg):
3537
if isinstance(arg, str): return [1, 2, 3]
3538
elif isinstance(arg, int): return object.__new__(D)
3539
else: return object.__new__(cls)
3541
def __init__(self, arg):
3543
self.assertEqual(C("1"), [1, 2, 3])
3544
self.assertEqual(D("1"), [1, 2, 3])
3546
self.assertEqual(d.foo, None)
3548
self.assertEqual(isinstance(d, D), True)
3549
self.assertEqual(d.foo, 1)
3551
self.assertEqual(isinstance(d, D), True)
3552
self.assertEqual(d.foo, 1)
3554
def test_imul_bug(self):
3555
# Testing for __imul__ problems...
3558
def __imul__(self, other):
3559
return (self, other)
3563
self.assertEqual(y, (x, 1.0))
3566
self.assertEqual(y, (x, 2))
3569
self.assertEqual(y, (x, 3L))
3572
self.assertEqual(y, (x, 1L<<100))
3575
self.assertEqual(y, (x, None))
3578
self.assertEqual(y, (x, "foo"))
3580
def test_copy_setstate(self):
3581
# Testing that copy.*copy() correctly uses __setstate__...
3584
def __init__(self, foo=None):
3587
def setfoo(self, foo=None):
3591
def __getstate__(self):
3593
def __setstate__(self_, lst):
3594
self.assertEqual(len(lst), 1)
3595
self_.__foo = self_.foo = lst[0]
3598
self.assertEqual(a.foo, 24)
3599
self.assertEqual(a.getfoo(), 42)
3601
self.assertEqual(b.foo, 24)
3602
self.assertEqual(b.getfoo(), 24)
3603
b = copy.deepcopy(a)
3604
self.assertEqual(b.foo, 24)
3605
self.assertEqual(b.getfoo(), 24)
3607
def test_slices(self):
3608
# Testing cases with slices and overridden __getitem__ ...
3611
self.assertEqual("hello"[:4], "hell")
3612
self.assertEqual("hello"[slice(4)], "hell")
3613
self.assertEqual(str.__getitem__("hello", slice(4)), "hell")
3615
def __getitem__(self, x):
3616
return str.__getitem__(self, x)
3617
self.assertEqual(S("hello")[:4], "hell")
3618
self.assertEqual(S("hello")[slice(4)], "hell")
3619
self.assertEqual(S("hello").__getitem__(slice(4)), "hell")
3621
self.assertEqual((1,2,3)[:2], (1,2))
3622
self.assertEqual((1,2,3)[slice(2)], (1,2))
3623
self.assertEqual(tuple.__getitem__((1,2,3), slice(2)), (1,2))
3625
def __getitem__(self, x):
3626
return tuple.__getitem__(self, x)
3627
self.assertEqual(T((1,2,3))[:2], (1,2))
3628
self.assertEqual(T((1,2,3))[slice(2)], (1,2))
3629
self.assertEqual(T((1,2,3)).__getitem__(slice(2)), (1,2))
3631
self.assertEqual([1,2,3][:2], [1,2])
3632
self.assertEqual([1,2,3][slice(2)], [1,2])
3633
self.assertEqual(list.__getitem__([1,2,3], slice(2)), [1,2])
3635
def __getitem__(self, x):
3636
return list.__getitem__(self, x)
3637
self.assertEqual(L([1,2,3])[:2], [1,2])
3638
self.assertEqual(L([1,2,3])[slice(2)], [1,2])
3639
self.assertEqual(L([1,2,3]).__getitem__(slice(2)), [1,2])
3640
# Now do lists and __setitem__
3642
a[slice(1, 3)] = [3,2]
3643
self.assertEqual(a, [1,3,2])
3644
a[slice(0, 2, 1)] = [3,1]
3645
self.assertEqual(a, [3,1,2])
3646
a.__setitem__(slice(1, 3), [2,1])
3647
self.assertEqual(a, [3,2,1])
3648
a.__setitem__(slice(0, 2, 1), [2,3])
3649
self.assertEqual(a, [2,3,1])
3651
def test_subtype_resurrection(self):
3652
# Testing resurrection of new-style instance...
3658
# resurrect the instance
3659
C.container.append(self)
3664
# The most interesting thing here is whether this blows up, due to flawed
3665
# GC tracking logic in typeobject.c's call_finalizer() (a 2.2.1 bug).
3668
# If that didn't blow up, it's also interesting to see whether clearing
3669
# the last container slot works: that will attempt to delete c again,
3670
# which will cause c to get appended back to the container again "during"
3673
self.assertEqual(len(C.container), 1)
3674
self.assertEqual(C.container[-1].attr, 42)
3676
# Make c mortal again, so that the test framework with -l doesn't report
3680
def test_slots_trash(self):
3681
# Testing slot trash...
3682
# Deallocating deeply nested slotted trash caused stack overflows
3683
class trash(object):
3685
def __init__(self, x):
3688
for i in xrange(50000):
3692
def test_slots_multiple_inheritance(self):
3693
# SF bug 575229, multiple inheritance w/ slots dumps core
3700
self.assertEqual(C.__basicsize__, B.__basicsize__)
3701
self.assert_(hasattr(C, '__dict__'))
3702
self.assert_(hasattr(C, '__weakref__'))
3705
def test_rmul(self):
3706
# Testing correct invocation of __rmul__...
3709
def __mul__(self, other):
3711
def __rmul__(self, other):
3714
self.assertEqual(a*2, "mul")
3715
self.assertEqual(a*2.2, "mul")
3716
self.assertEqual(2*a, "rmul")
3717
self.assertEqual(2.2*a, "rmul")
3719
def test_ipow(self):
3720
# Testing correct invocation of __ipow__...
3723
def __ipow__(self, other):
3728
def test_mutable_bases(self):
3729
# Testing mutable bases...
3731
# stuff that should work:
3735
def __getattribute__(self, attr):
3739
return super(C2, self).__getattribute__(attr)
3750
self.assertEqual(d.meth(), 1)
3751
self.assertEqual(e.meth(), 1)
3752
self.assertEqual(d.a, 2)
3753
self.assertEqual(e.a, 2)
3754
self.assertEqual(C2.__subclasses__(), [D])
3756
# stuff that shouldn't:
3761
L.__bases__ = (dict,)
3765
self.fail("shouldn't turn list subclass into dict subclass")
3768
list.__bases__ = (dict,)
3772
self.fail("shouldn't be able to assign to list.__bases__")
3775
D.__bases__ = (C2, list)
3779
assert 0, "best_base calculation found wanting"
3786
self.fail("shouldn't be able to delete .__bases__")
3790
except TypeError, msg:
3791
if str(msg) == "a new-style class can't have only classic bases":
3792
self.fail("wrong error message for .__bases__ = ()")
3794
self.fail("shouldn't be able to set .__bases__ to ()")
3801
# actually, we'll have crashed by here...
3802
self.fail("shouldn't be able to create inheritance cycles")
3805
D.__bases__ = (C, C)
3809
self.fail("didn't detect repeated base classes")
3816
self.fail("shouldn't be able to create inheritance cycles")
3818
# let's throw a classic class into the mix:
3823
D.__bases__ = (C, Classic)
3825
self.assertEqual(d.meth2(), 3)
3826
self.assertEqual(e.meth2(), 3)
3829
except AttributeError:
3832
self.fail("attribute should have vanished")
3835
D.__bases__ = (Classic,)
3839
self.fail("new-style class must have a new-style base")
3841
def test_mutable_bases_with_failing_mro(self):
3842
# Testing mutable bases with failing mro...
3843
class WorkOnce(type):
3844
def __new__(self, name, bases, ns):
3846
return super(WorkOnce, self).__new__(WorkOnce, name, bases, ns)
3849
raise RuntimeError, "bozo"
3852
return type.mro(self)
3854
class WorkAlways(type):
3856
# this is here to make sure that .mro()s aren't called
3857
# with an exception set (which was possible at one point).
3858
# An error message will be printed in a debug build.
3859
# What's a good way to test for this?
3860
return type.mro(self)
3875
__metaclass__ = WorkOnce
3878
__metaclass__ = WorkAlways
3880
# Immediate subclasses have their mro's adjusted in alphabetical
3881
# order, so E's will get adjusted before adjusting F's fails. We
3882
# check here that E's gets restored.
3884
E_mro_before = E.__mro__
3885
D_mro_before = D.__mro__
3889
except RuntimeError:
3890
self.assertEqual(E.__mro__, E_mro_before)
3891
self.assertEqual(D.__mro__, D_mro_before)
3893
self.fail("exception not propagated")
3895
def test_mutable_bases_catch_mro_conflict(self):
3896
# Testing mutable bases catch mro conflict...
3913
C.__bases__ = (B, A)
3917
self.fail("didn't catch MRO conflict")
3919
def test_mutable_names(self):
3920
# Testing mutable names...
3924
# C.__module__ could be 'test_descr' or '__main__'
3928
self.assertEqual((C.__module__, C.__name__), (mod, 'D'))
3931
self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))
3933
def test_subclass_right_op(self):
3934
# Testing correct dispatch of subclass overloading __r<op>__...
3936
# This code tests various cases where right-dispatch of a subclass
3937
# should be preferred over left-dispatch of a base class.
3939
# Case 1: subclass of int; this tests code in abstract.c::binary_op1()
3942
def __floordiv__(self, other):
3943
return "B.__floordiv__"
3944
def __rfloordiv__(self, other):
3945
return "B.__rfloordiv__"
3947
self.assertEqual(B(1) // 1, "B.__floordiv__")
3948
self.assertEqual(1 // B(1), "B.__rfloordiv__")
3950
# Case 2: subclass of object; this is just the baseline for case 3
3953
def __floordiv__(self, other):
3954
return "C.__floordiv__"
3955
def __rfloordiv__(self, other):
3956
return "C.__rfloordiv__"
3958
self.assertEqual(C() // 1, "C.__floordiv__")
3959
self.assertEqual(1 // C(), "C.__rfloordiv__")
3961
# Case 3: subclass of new-style class; here it gets interesting
3964
def __floordiv__(self, other):
3965
return "D.__floordiv__"
3966
def __rfloordiv__(self, other):
3967
return "D.__rfloordiv__"
3969
self.assertEqual(D() // C(), "D.__floordiv__")
3970
self.assertEqual(C() // D(), "D.__rfloordiv__")
3972
# Case 4: this didn't work right in 2.2.2 and 2.3a1
3977
self.assertEqual(E.__rfloordiv__, C.__rfloordiv__)
3979
self.assertEqual(E() // 1, "C.__floordiv__")
3980
self.assertEqual(1 // E(), "C.__rfloordiv__")
3981
self.assertEqual(E() // C(), "C.__floordiv__")
3982
self.assertEqual(C() // E(), "C.__floordiv__") # This one would fail
3984
def test_meth_class_get(self):
3985
# Testing __get__ method of METH_CLASS C methods...
3986
# Full coverage of descrobject.c::classmethod_get()
3990
res = {1: None, 2: None, 3: None}
3991
self.assertEqual(dict.fromkeys(arg), res)
3992
self.assertEqual({}.fromkeys(arg), res)
3994
# Now get the descriptor
3995
descr = dict.__dict__["fromkeys"]
3997
# More baseline using the descriptor directly
3998
self.assertEqual(descr.__get__(None, dict)(arg), res)
3999
self.assertEqual(descr.__get__({})(arg), res)
4001
# Now check various error cases
4003
descr.__get__(None, None)
4007
self.fail("shouldn't have allowed descr.__get__(None, None)")
4013
self.fail("shouldn't have allowed descr.__get__(42)")
4015
descr.__get__(None, 42)
4019
self.fail("shouldn't have allowed descr.__get__(None, 42)")
4021
descr.__get__(None, int)
4025
self.fail("shouldn't have allowed descr.__get__(None, int)")
4027
def test_isinst_isclass(self):
4028
# Testing proxy isinstance() and isclass()...
4029
class Proxy(object):
4030
def __init__(self, obj):
4032
def __getattribute__(self, name):
4033
if name.startswith("_Proxy__"):
4034
return object.__getattribute__(self, name)
4036
return getattr(self.__obj, name)
4037
# Test with a classic class
4042
self.assert_(isinstance(a, C)) # Baseline
4043
self.assert_(isinstance(pa, C)) # Test
4044
# Test with a classic subclass
4049
self.assert_(isinstance(a, C)) # Baseline
4050
self.assert_(isinstance(pa, C)) # Test
4051
# Test with a new-style class
4056
self.assert_(isinstance(a, C)) # Baseline
4057
self.assert_(isinstance(pa, C)) # Test
4058
# Test with a new-style subclass
4063
self.assert_(isinstance(a, C)) # Baseline
4064
self.assert_(isinstance(pa, C)) # Test
4066
def test_proxy_super(self):
4067
# Testing super() for a proxy object...
4068
class Proxy(object):
4069
def __init__(self, obj):
4071
def __getattribute__(self, name):
4072
if name.startswith("_Proxy__"):
4073
return object.__getattribute__(self, name)
4075
return getattr(self.__obj, name)
4083
return super(C, self).f() + "->C.f"
4087
self.assertEqual(C.__dict__["f"](p), "B.f->C.f")
4089
def test_carloverre(self):
4090
# Testing prohibition of Carlo Verre's hack...
4092
object.__setattr__(str, "foo", 42)
4096
self.fail("Carlo Verre __setattr__ suceeded!")
4098
object.__delattr__(str, "lower")
4102
self.fail("Carlo Verre __delattr__ succeeded!")
4104
def test_weakref_segfault(self):
4105
# Testing weakref segfault...
4110
def __init__(self, referrent):
4111
self.ref = weakref.ref(referrent)
4120
o.whatever = Provoker(o)
4123
def test_wrapper_segfault(self):
4124
# SF 927248: deeply nested wrappers could cause stack overflow
4126
for i in xrange(1000000):
4130
def test_file_fault(self):
4131
# Testing sys.stdout is changed in getattr...
4134
def __getattr__(self, attr):
4135
sys.stdout = sys.__stdout__
4136
raise RuntimeError("Premature access to sys.stdout.%s" % attr)
4137
sys.stdout = StdoutGuard()
4140
except RuntimeError:
4143
def test_vicious_descriptor_nonsense(self):
4144
# Testing vicious_descriptor_nonsense...
4146
# A potential segfault spotted by Thomas Wouters in mail to
4147
# python-dev 2003-04-17, turned into an example & fixed by Michael
4148
# Hudson just less than four months later...
4153
def __eq__(self, other):
4157
class Descr(object):
4158
def __get__(self, ob, type=None):
4165
c.__dict__[Evil()] = 0
4167
self.assertEqual(c.attr, 1)
4168
# this makes a crash more likely:
4169
import gc; gc.collect()
4170
self.assertEqual(hasattr(c, 'attr'), False)
4172
def test_init(self):
4182
self.fail("did not test __init__() for None return")
4184
def test_method_wrapper(self):
4185
# Testing method-wrapper objects...
4186
# <type 'method-wrapper'> did not support any reflection before 2.5
4189
self.assertEqual(l.__add__, l.__add__)
4190
self.assertEqual(l.__add__, [].__add__)
4191
self.assert_(l.__add__ != [5].__add__)
4192
self.assert_(l.__add__ != l.__mul__)
4193
self.assert_(l.__add__.__name__ == '__add__')
4194
self.assert_(l.__add__.__self__ is l)
4195
self.assert_(l.__add__.__objclass__ is list)
4196
self.assertEqual(l.__add__.__doc__, list.__add__.__doc__)
4202
self.fail("no TypeError from hash([].__add__)")
4206
self.assertEqual(t.__add__, (7,).__add__)
4207
self.assertEqual(hash(t.__add__), hash((7,).__add__))
4209
def test_not_implemented(self):
4210
# Testing NotImplemented...
4211
# all binary methods should be able to return a NotImplemented
4216
def specialmethod(self, other):
4217
return NotImplemented
4219
def check(expr, x, y):
4221
exec expr in {'x': x, 'y': y, 'operator': operator}
4225
self.fail("no TypeError from %r" % (expr,))
4227
N1 = sys.maxint + 1L # might trigger OverflowErrors instead of
4229
N2 = sys.maxint # if sizeof(int) < sizeof(long), might trigger
4230
# ValueErrors instead of TypeErrors
4231
for metaclass in [type, types.ClassType]:
4232
for name, expr, iexpr in [
4233
('__add__', 'x + y', 'x += y'),
4234
('__sub__', 'x - y', 'x -= y'),
4235
('__mul__', 'x * y', 'x *= y'),
4236
('__truediv__', 'operator.truediv(x, y)', None),
4237
('__floordiv__', 'operator.floordiv(x, y)', None),
4238
('__div__', 'x / y', 'x /= y'),
4239
('__mod__', 'x % y', 'x %= y'),
4240
('__divmod__', 'divmod(x, y)', None),
4241
('__pow__', 'x ** y', 'x **= y'),
4242
('__lshift__', 'x << y', 'x <<= y'),
4243
('__rshift__', 'x >> y', 'x >>= y'),
4244
('__and__', 'x & y', 'x &= y'),
4245
('__or__', 'x | y', 'x |= y'),
4246
('__xor__', 'x ^ y', 'x ^= y'),
4247
('__coerce__', 'coerce(x, y)', None)]:
4248
if name == '__coerce__':
4251
rname = '__r' + name[2:]
4252
A = metaclass('A', (), {name: specialmethod})
4253
B = metaclass('B', (), {rname: specialmethod})
4271
iname = '__i' + name[2:]
4272
C = metaclass('C', (), {iname: specialmethod})
4279
def test_assign_slice(self):
4280
# ceval.c's assign_slice used to check for
4281
# tp->tp_as_sequence->sq_slice instead of
4282
# tp->tp_as_sequence->sq_ass_slice
4285
def __setslice__(self, start, stop, value):
4290
self.assertEqual(c.value, 3)
4292
def test_getattr_hooks(self):
4295
class Descriptor(object):
4297
def __get__(self, obj, objtype=None):
4300
raise AttributeError(name)
4303
descr = Descriptor()
4305
__getattribute__ = descr
4309
__getattribute__ = descr
4312
self.assertRaises(AttributeError, getattr, A(), "attr")
4313
self.assertEquals(descr.counter, 1)
4314
self.assertRaises(AttributeError, getattr, B(), "attr")
4315
self.assertEquals(descr.counter, 2)
4316
self.assertRaises(AttributeError, getattr, C(), "attr")
4317
self.assertEquals(descr.counter, 4)
4320
class EvilGetattribute(object):
4321
# This used to segfault
4322
def __getattr__(self, name):
4323
raise AttributeError(name)
4324
def __getattribute__(self, name):
4325
del EvilGetattribute.__getattr__
4328
raise AttributeError(name)
4330
self.assertRaises(AttributeError, getattr, EvilGetattribute(), "attr")
4333
class DictProxyTests(unittest.TestCase):
4340
def test_iter_keys(self):
4341
# Testing dict-proxy iterkeys...
4342
keys = [ key for key in self.C.__dict__.iterkeys() ]
4344
self.assertEquals(keys, ['__dict__', '__doc__', '__module__',
4345
'__weakref__', 'meth'])
4347
def test_iter_values(self):
4348
# Testing dict-proxy itervalues...
4349
values = [ values for values in self.C.__dict__.itervalues() ]
4350
self.assertEqual(len(values), 5)
4352
def test_iter_items(self):
4353
# Testing dict-proxy iteritems...
4354
keys = [ key for (key, value) in self.C.__dict__.iteritems() ]
4356
self.assertEqual(keys, ['__dict__', '__doc__', '__module__',
4357
'__weakref__', 'meth'])
4359
def test_dict_type_with_metaclass(self):
4360
# Testing type of __dict__ when __metaclass__ set...
4366
# In 2.3a1, C.__dict__ was a real dict rather than a dict proxy
4368
self.assertEqual(type(C.__dict__), type(B.__dict__))
4371
class PTypesLongInitTest(unittest.TestCase):
4372
# This is in its own TestCase so that it can be run before any other tests.
4373
def test_pytype_long_ready(self):
4374
# Testing SF bug 551412 ...
4376
# This dumps core when SF bug 551412 isn't fixed --
4377
# but only when test_descr.py is run separately.
4378
# (That can't be helped -- as soon as PyType_Ready()
4379
# is called for PyLong_Type, the bug is gone.)
4380
class UserLong(object):
4381
def __pow__(self, *args):
4384
pow(0L, UserLong(), 0L)
4388
# Another segfault only when run early
4389
# (before PyType_Ready(tuple) is called)
4394
# Run all local test cases, with PTypesLongInitTest first.
4395
test_support.run_unittest(PTypesLongInitTest, OperatorsTest,
4396
ClassPropertiesAndMethods, DictProxyTests)
4398
if __name__ == "__main__":