diff options
Diffstat (limited to 'Lib/test/test_descr.py')
-rw-r--r-- | Lib/test/test_descr.py | 1478 |
1 files changed, 663 insertions, 815 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index bfb3552f55a..cdaf7d2aca0 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1,12 +1,13 @@ -import __builtin__ +import builtins import gc import sys import types +import math import unittest import weakref from copy import deepcopy -from test import test_support +from test import support class OperatorsTest(unittest.TestCase): @@ -34,7 +35,7 @@ class OperatorsTest(unittest.TestCase): 'ge': '>=', } - for name, expr in self.binops.items(): + for name, expr in list(self.binops.items()): if expr.islower(): expr = expr + "(a, b)" else: @@ -47,13 +48,12 @@ class OperatorsTest(unittest.TestCase): 'abs': 'abs', 'invert': '~', 'int': 'int', - 'long': 'long', 'float': 'float', 'oct': 'oct', 'hex': 'hex', } - for name, expr in self.unops.items(): + for name, expr in list(self.unops.items()): if expr.islower(): expr = expr + "(a)" else: @@ -97,7 +97,7 @@ class OperatorsTest(unittest.TestCase): bm = getattr(a, meth) self.assertEqual(bm(b), res) - def ternop_test(self, a, b, c, res, expr="a[b:c]", meth="__getslice__"): + def sliceop_test(self, a, b, c, res, expr="a[b:c]", meth="__getitem__"): d = {'a': a, 'b': b, 'c': c} self.assertEqual(eval(expr, d), res) t = type(a) @@ -107,13 +107,13 @@ class OperatorsTest(unittest.TestCase): # in some implementations (e.g. PyPy), 'm' can be a regular unbound # method object; the getattr() below obtains its underlying function. self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth]) - self.assertEqual(m(a, b, c), res) + self.assertEqual(m(a, slice(b, c)), res) bm = getattr(a, meth) - self.assertEqual(bm(b, c), res) + self.assertEqual(bm(slice(b, c)), res) def setop_test(self, a, b, res, stmt="a+=b", meth="__iadd__"): d = {'a': deepcopy(a), 'b': b} - exec stmt in d + exec(stmt, d) self.assertEqual(d['a'], res) t = type(a) m = getattr(t, meth) @@ -132,7 +132,7 @@ class OperatorsTest(unittest.TestCase): def set2op_test(self, a, b, c, res, stmt="a[b]=c", meth="__setitem__"): d = {'a': deepcopy(a), 'b': b, 'c': c} - exec stmt in d + exec(stmt, d) self.assertEqual(d['a'], res) t = type(a) m = getattr(t, meth) @@ -149,9 +149,9 @@ class OperatorsTest(unittest.TestCase): bm(b, c) self.assertEqual(d['a'], res) - def set3op_test(self, a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"): + def setsliceop_test(self, a, b, c, d, res, stmt="a[b:c]=d", meth="__setitem__"): dictionary = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d} - exec stmt in dictionary + exec(stmt, dictionary) self.assertEqual(dictionary['a'], res) t = type(a) while meth not in t.__dict__: @@ -161,11 +161,11 @@ class OperatorsTest(unittest.TestCase): # method object; the getattr() below obtains its underlying function. self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth]) dictionary['a'] = deepcopy(a) - m(dictionary['a'], b, c, d) + m(dictionary['a'], slice(b, c), d) self.assertEqual(dictionary['a'], res) dictionary['a'] = deepcopy(a) bm = getattr(dictionary['a'], meth) - bm(b, c, d) + bm(slice(b, c), d) self.assertEqual(dictionary['a'], res) def test_lists(self): @@ -175,29 +175,25 @@ class OperatorsTest(unittest.TestCase): self.binop_test([1,2,3], 2, 1, "b in a", "__contains__") self.binop_test([1,2,3], 4, 0, "b in a", "__contains__") self.binop_test([1,2,3], 1, 2, "a[b]", "__getitem__") - self.ternop_test([1,2,3], 0, 2, [1,2], "a[b:c]", "__getslice__") + self.sliceop_test([1,2,3], 0, 2, [1,2], "a[b:c]", "__getitem__") self.setop_test([1], [2], [1,2], "a+=b", "__iadd__") self.setop_test([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__") self.unop_test([1,2,3], 3, "len(a)", "__len__") self.binop_test([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__") self.binop_test([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__") self.set2op_test([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__") - self.set3op_test([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d", - "__setslice__") + self.setsliceop_test([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d", + "__setitem__") def test_dicts(self): # Testing dict operations... - if hasattr(dict, '__cmp__'): # PyPy has only rich comparison on dicts - self.binop_test({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__") - else: - self.binop_test({1:2}, {2:1}, True, "a < b", "__lt__") self.binop_test({1:2,3:4}, 1, 1, "b in a", "__contains__") self.binop_test({1:2,3:4}, 2, 0, "b in a", "__contains__") self.binop_test({1:2,3:4}, 1, 2, "a[b]", "__getitem__") d = {1:2, 3:4} l1 = [] - for i in d.keys(): + for i in list(d.keys()): l1.append(i) l = [] for i in iter(d): @@ -222,14 +218,14 @@ class OperatorsTest(unittest.TestCase): def number_operators(self, a, b, skip=[]): dict = {'a': a, 'b': b} - for name, expr in self.binops.items(): + for name, expr in list(self.binops.items()): if name not in skip: name = "__%s__" % name if hasattr(a, name): res = eval(expr, dict) self.binop_test(a, b, res, expr, name) - for name, expr in self.unops.items(): + for name, expr in list(self.unops.items()): if name not in skip: name = "__%s__" % name if hasattr(a, name): @@ -240,29 +236,19 @@ class OperatorsTest(unittest.TestCase): # Testing int operations... self.number_operators(100, 3) # The following crashes in Python 2.2 - self.assertEqual((1).__nonzero__(), 1) - self.assertEqual((0).__nonzero__(), 0) + self.assertEqual((1).__bool__(), 1) + self.assertEqual((0).__bool__(), 0) # This returns 'NotImplemented' in Python 2.2 class C(int): def __add__(self, other): return NotImplemented - self.assertEqual(C(5L), 5) + self.assertEqual(C(5), 5) try: C() + "" except TypeError: pass else: self.fail("NotImplemented should have caused TypeError") - try: - C(sys.maxint+1) - except OverflowError: - pass - else: - self.fail("should have raised OverflowError") - - def test_longs(self): - # Testing long operations... - self.number_operators(100L, 3L) def test_floats(self): # Testing float operations... @@ -271,7 +257,8 @@ class OperatorsTest(unittest.TestCase): def test_complexes(self): # Testing complex operations... self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge', - 'int', 'long', 'float']) + 'int', 'float', + 'divmod', 'mod']) class Number(complex): __slots__ = ['prec'] @@ -300,7 +287,12 @@ class OperatorsTest(unittest.TestCase): self.assertEqual(repr(a), "234.5") self.assertEqual(a.prec, 12) - @test_support.impl_detail("the module 'xxsubtype' is internal") + def test_explicit_reverse_methods(self): + # see issue 9930 + self.assertEqual(complex.__radd__(3j, 4.0), complex(4.0, 3.0)) + self.assertEqual(float.__rsub__(3.0, 1), -2.0) + + @support.impl_detail("the module 'xxsubtype' is internal") def test_spam_lists(self): # Testing spamlist operations... import copy, xxsubtype as spam @@ -317,21 +309,21 @@ class OperatorsTest(unittest.TestCase): self.binop_test(spamlist([1,2,3]), 2, 1, "b in a", "__contains__") self.binop_test(spamlist([1,2,3]), 4, 0, "b in a", "__contains__") self.binop_test(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__") - self.ternop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]", - "__getslice__") + self.sliceop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]", + "__getitem__") self.setop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b", - "__iadd__") + "__iadd__") self.setop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b", - "__imul__") + "__imul__") self.unop_test(spamlist([1,2,3]), 3, "len(a)", "__len__") self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b", - "__mul__") + "__mul__") self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a", - "__rmul__") + "__rmul__") self.set2op_test(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c", - "__setitem__") - self.set3op_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), - spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__") + "__setitem__") + self.setsliceop_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), + spamlist([1,5,6,4]), "a[b:c]=d", "__setitem__") # Test subclassing class C(spam.spamlist): def foo(self): return 1 @@ -344,27 +336,25 @@ class OperatorsTest(unittest.TestCase): a.setstate(42) self.assertEqual(a.getstate(), 42) - @test_support.impl_detail("the module 'xxsubtype' is internal") + @support.impl_detail("the module 'xxsubtype' is internal") def test_spam_dicts(self): # Testing spamdict operations... import copy, xxsubtype as spam def spamdict(d, memo=None): import xxsubtype as spam sd = spam.spamdict() - for k, v in d.items(): + for k, v in list(d.items()): sd[k] = v return sd # This is an ugly hack: copy._deepcopy_dispatch[spam.spamdict] = spamdict - self.binop_test(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", - "__cmp__") self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__") self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__") self.binop_test(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__") d = spamdict({1:2,3:4}) l1 = [] - for i in d.keys(): + for i in list(d.keys()): l1.append(i) l = [] for i in iter(d): @@ -388,10 +378,10 @@ class OperatorsTest(unittest.TestCase): class C(spam.spamdict): def foo(self): return 1 a = C() - self.assertEqual(a.items(), []) + self.assertEqual(list(a.items()), []) self.assertEqual(a.foo(), 1) a['foo'] = 'bar' - self.assertEqual(a.items(), [('foo', 'bar')]) + self.assertEqual(list(a.items()), [('foo', 'bar')]) self.assertEqual(a.getstate(), 0) a.setstate(100) self.assertEqual(a.getstate(), 100) @@ -413,7 +403,7 @@ class ClassPropertiesAndMethods(unittest.TestCase): self.assertEqual(len(a), 1) self_local.state = a[0] if kw: - for k, v in kw.items(): + for k, v in list(kw.items()): self_local[v] = k def __getitem__(self, key): return self.get(key, 0) @@ -454,9 +444,9 @@ class ClassPropertiesAndMethods(unittest.TestCase): # Testing Python subclass of list... class C(list): def __getitem__(self, i): + if isinstance(i, slice): + return i.start, i.stop return list.__getitem__(self, i) + 100 - def __getslice__(self, i, j): - return (i, j) a = C() a.extend([0,1,2]) self.assertEqual(a[0], 100) @@ -465,9 +455,8 @@ class ClassPropertiesAndMethods(unittest.TestCase): self.assertEqual(a[100:200], (100,200)) def test_metaclass(self): - # Testing __metaclass__... - class C: - __metaclass__ = type + # Testing metaclasses... + class C(metaclass=type): def __init__(self): self.__state = 0 def getstate(self): @@ -478,9 +467,10 @@ class ClassPropertiesAndMethods(unittest.TestCase): self.assertEqual(a.getstate(), 0) a.setstate(10) self.assertEqual(a.getstate(), 10) - class D: - class __metaclass__(type): - def myself(cls): return cls + class _metaclass(type): + def myself(cls): return cls + class D(metaclass=_metaclass): + pass self.assertEqual(D.myself(), D) d = D() self.assertEqual(d.__class__, D) @@ -488,8 +478,8 @@ class ClassPropertiesAndMethods(unittest.TestCase): def __new__(cls, name, bases, dict): dict['__spam__'] = 1 return type.__new__(cls, name, bases, dict) - class C: - __metaclass__ = M1 + class C(metaclass=M1): + pass self.assertEqual(C.__spam__, 1) c = C() self.assertEqual(c.__spam__, 1) @@ -512,8 +502,7 @@ class ClassPropertiesAndMethods(unittest.TestCase): continue setattr(it, key, self.dict[key].__get__(it, self)) return it - class C: - __metaclass__ = M2 + class C(metaclass=M2): def spam(self): return 42 self.assertEqual(C.name, 'C') @@ -539,8 +528,7 @@ class ClassPropertiesAndMethods(unittest.TestCase): name = "__super" setattr(cls, name, super(cls)) return cls - class A: - __metaclass__ = autosuper + class A(metaclass=autosuper): def meth(self): return "A" class B(A): @@ -563,7 +551,7 @@ class ClassPropertiesAndMethods(unittest.TestCase): # named _get_x and/or _set_x are found def __new__(metaclass, name, bases, dict): hits = {} - for key, val in dict.iteritems(): + for key, val in dict.items(): if key.startswith("_get_"): key = key[5:] get, set = hits.get(key, (None, None)) @@ -574,12 +562,11 @@ class ClassPropertiesAndMethods(unittest.TestCase): get, set = hits.get(key, (None, None)) set = val hits[key] = get, set - for key, (get, set) in hits.iteritems(): + for key, (get, set) in hits.items(): dict[key] = property(get, set) return super(autoproperty, metaclass).__new__(metaclass, name, bases, dict) - class A: - __metaclass__ = autoproperty + class A(metaclass=autoproperty): def _get_x(self): return -self.__x def _set_x(self, x): @@ -593,8 +580,7 @@ class ClassPropertiesAndMethods(unittest.TestCase): class multimetaclass(autoproperty, autosuper): # Merge of multiple cooperating metaclasses pass - class A: - __metaclass__ = multimetaclass + class A(metaclass=multimetaclass): def _get_x(self): return "A" class B(A): @@ -613,8 +599,8 @@ class ClassPropertiesAndMethods(unittest.TestCase): counter = 0 def __init__(self, *args): T.counter += 1 - class C: - __metaclass__ = T + class C(metaclass=T): + pass self.assertEqual(T.counter, 1) a = C() self.assertEqual(type(a), C) @@ -635,12 +621,180 @@ class ClassPropertiesAndMethods(unittest.TestCase): class B(object): pass - class C(object): - __metaclass__ = A + class C(object, metaclass=A): + pass # The most derived metaclass of D is A rather than type. class D(B, C): pass + self.assertIs(A, type(D)) + + # issue1294232: correct metaclass calculation + new_calls = [] # to check the order of __new__ calls + class AMeta(type): + @staticmethod + def __new__(mcls, name, bases, ns): + new_calls.append('AMeta') + return super().__new__(mcls, name, bases, ns) + @classmethod + def __prepare__(mcls, name, bases): + return {} + + class BMeta(AMeta): + @staticmethod + def __new__(mcls, name, bases, ns): + new_calls.append('BMeta') + return super().__new__(mcls, name, bases, ns) + @classmethod + def __prepare__(mcls, name, bases): + ns = super().__prepare__(name, bases) + ns['BMeta_was_here'] = True + return ns + + class A(metaclass=AMeta): + pass + self.assertEqual(['AMeta'], new_calls) + new_calls[:] = [] + + class B(metaclass=BMeta): + pass + # BMeta.__new__ calls AMeta.__new__ with super: + self.assertEqual(['BMeta', 'AMeta'], new_calls) + new_calls[:] = [] + + class C(A, B): + pass + # The most derived metaclass is BMeta: + self.assertEqual(['BMeta', 'AMeta'], new_calls) + new_calls[:] = [] + # BMeta.__prepare__ should've been called: + self.assertIn('BMeta_was_here', C.__dict__) + + # The order of the bases shouldn't matter: + class C2(B, A): + pass + self.assertEqual(['BMeta', 'AMeta'], new_calls) + new_calls[:] = [] + self.assertIn('BMeta_was_here', C2.__dict__) + + # Check correct metaclass calculation when a metaclass is declared: + class D(C, metaclass=type): + pass + self.assertEqual(['BMeta', 'AMeta'], new_calls) + new_calls[:] = [] + self.assertIn('BMeta_was_here', D.__dict__) + + class E(C, metaclass=AMeta): + pass + self.assertEqual(['BMeta', 'AMeta'], new_calls) + new_calls[:] = [] + self.assertIn('BMeta_was_here', E.__dict__) + + # Special case: the given metaclass isn't a class, + # so there is no metaclass calculation. + marker = object() + def func(*args, **kwargs): + return marker + class X(metaclass=func): + pass + class Y(object, metaclass=func): + pass + class Z(D, metaclass=func): + pass + self.assertIs(marker, X) + self.assertIs(marker, Y) + self.assertIs(marker, Z) + + # The given metaclass is a class, + # but not a descendant of type. + prepare_calls = [] # to track __prepare__ calls + class ANotMeta: + def __new__(mcls, *args, **kwargs): + new_calls.append('ANotMeta') + return super().__new__(mcls) + @classmethod + def __prepare__(mcls, name, bases): + prepare_calls.append('ANotMeta') + return {} + class BNotMeta(ANotMeta): + def __new__(mcls, *args, **kwargs): + new_calls.append('BNotMeta') + return super().__new__(mcls) + @classmethod + def __prepare__(mcls, name, bases): + prepare_calls.append('BNotMeta') + return super().__prepare__(name, bases) + + class A(metaclass=ANotMeta): + pass + self.assertIs(ANotMeta, type(A)) + self.assertEqual(['ANotMeta'], prepare_calls) + prepare_calls[:] = [] + self.assertEqual(['ANotMeta'], new_calls) + new_calls[:] = [] + + class B(metaclass=BNotMeta): + pass + self.assertIs(BNotMeta, type(B)) + self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls) + prepare_calls[:] = [] + self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls) + new_calls[:] = [] + + class C(A, B): + pass + self.assertIs(BNotMeta, type(C)) + self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls) + new_calls[:] = [] + self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls) + prepare_calls[:] = [] + + class C2(B, A): + pass + self.assertIs(BNotMeta, type(C2)) + self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls) + new_calls[:] = [] + self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls) + prepare_calls[:] = [] + + # This is a TypeError, because of a metaclass conflict: + # BNotMeta is neither a subclass, nor a superclass of type + with self.assertRaises(TypeError): + class D(C, metaclass=type): + pass + + class E(C, metaclass=ANotMeta): + pass + self.assertIs(BNotMeta, type(E)) + self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls) + new_calls[:] = [] + self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls) + prepare_calls[:] = [] + + class F(object(), C): + pass + self.assertIs(BNotMeta, type(F)) + self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls) + new_calls[:] = [] + self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls) + prepare_calls[:] = [] + + class F2(C, object()): + pass + self.assertIs(BNotMeta, type(F2)) + self.assertEqual(['BNotMeta', 'ANotMeta'], new_calls) + new_calls[:] = [] + self.assertEqual(['BNotMeta', 'ANotMeta'], prepare_calls) + prepare_calls[:] = [] + + # TypeError: BNotMeta is neither a + # subclass, nor a superclass of int + with self.assertRaises(TypeError): + class X(C, int()): + pass + with self.assertRaises(TypeError): + class X(int(), C): + pass def test_module_subclasses(self): # Testing Python subclass of module... @@ -676,7 +830,7 @@ class ClassPropertiesAndMethods(unittest.TestCase): self.fail("inheriting from ModuleType and str at the same time " "should fail") - def test_multiple_inheritence(self): + def test_multiple_inheritance(self): # Testing multiple inheritance... class C(object): def __init__(self): @@ -694,9 +848,9 @@ class ClassPropertiesAndMethods(unittest.TestCase): type({}).__init__(self) C.__init__(self) d = D() - self.assertEqual(d.keys(), []) + self.assertEqual(list(d.keys()), []) d["hello"] = "world" - self.assertEqual(d.items(), [("hello", "world")]) + self.assertEqual(list(d.items()), [("hello", "world")]) self.assertEqual(d["hello"], "world") self.assertEqual(d.getstate(), 0) d.setstate(10) @@ -717,96 +871,6 @@ class ClassPropertiesAndMethods(unittest.TestCase): self.assertEqual(Frag().__int__(), 42) self.assertEqual(int(Frag()), 42) - # MI mixing classic and new-style classes. - - class A: - x = 1 - - class B(A): - pass - - class C(A): - x = 2 - - class D(B, C): - pass - self.assertEqual(D.x, 1) - - # Classic MRO is preserved for a classic base class. - class E(D, object): - pass - self.assertEqual(E.__mro__, (E, D, B, A, C, object)) - self.assertEqual(E.x, 1) - - # But with a mix of classic bases, their MROs are combined using - # new-style MRO. - class F(B, C, object): - pass - self.assertEqual(F.__mro__, (F, B, C, A, object)) - self.assertEqual(F.x, 2) - - # Try something else. - class C: - def cmethod(self): - return "C a" - def all_method(self): - return "C b" - - class M1(C, object): - def m1method(self): - return "M1 a" - def all_method(self): - return "M1 b" - - self.assertEqual(M1.__mro__, (M1, C, object)) - m = M1() - self.assertEqual(m.cmethod(), "C a") - self.assertEqual(m.m1method(), "M1 a") - self.assertEqual(m.all_method(), "M1 b") - - class D(C): - def dmethod(self): - return "D a" - def all_method(self): - return "D b" - - class M2(D, object): - def m2method(self): - return "M2 a" - def all_method(self): - return "M2 b" - - self.assertEqual(M2.__mro__, (M2, D, C, object)) - m = M2() - self.assertEqual(m.cmethod(), "C a") - self.assertEqual(m.dmethod(), "D a") - self.assertEqual(m.m2method(), "M2 a") - self.assertEqual(m.all_method(), "M2 b") - - class M3(M1, M2, object): - def m3method(self): - return "M3 a" - def all_method(self): - return "M3 b" - self.assertEqual(M3.__mro__, (M3, M1, M2, D, C, object)) - m = M3() - self.assertEqual(m.cmethod(), "C a") - self.assertEqual(m.dmethod(), "D a") - self.assertEqual(m.m1method(), "M1 a") - self.assertEqual(m.m2method(), "M2 a") - self.assertEqual(m.m3method(), "M3 a") - self.assertEqual(m.all_method(), "M3 b") - - class Classic: - pass - try: - class New(Classic): - __metaclass__ = type - except TypeError: - pass - else: - self.fail("new class with only classic bases - shouldn't be") - def test_diamond_inheritence(self): # Testing multiple inheritance special cases... class A(object): @@ -898,9 +962,9 @@ order (MRO) for bases """ def raises(exc, expected, callable, *args): try: callable(*args) - except exc, msg: + except exc as msg: # the exact msg is generally considered an impl detail - if test_support.check_impl_detail(): + if support.check_impl_detail(): if not str(msg).startswith(expected): self.fail("Message %r, expected %r" % (str(msg), expected)) @@ -1052,34 +1116,29 @@ order (MRO) for bases """ self.assertEqual(c.abc, 5) # Test unicode slot names + # Test a single unicode string is not expanded as a sequence. + class C(object): + __slots__ = "abc" + c = C() + c.abc = 5 + self.assertEqual(c.abc, 5) + + # _unicode_to_string used to modify slots in certain circumstances + slots = ("foo", "bar") + class C(object): + __slots__ = slots + x = C() + x.foo = 5 + self.assertEqual(x.foo, 5) + self.assertTrue(type(slots[0]) is str) + # this used to leak references try: - unicode - except NameError: + class C(object): + __slots__ = [chr(128)] + except (TypeError, UnicodeEncodeError): pass else: - # Test a single unicode string is not expanded as a sequence. - class C(object): - __slots__ = unicode("abc") - c = C() - c.abc = 5 - self.assertEqual(c.abc, 5) - - # _unicode_to_string used to modify slots in certain circumstances - slots = (unicode("foo"), unicode("bar")) - class C(object): - __slots__ = slots - x = C() - x.foo = 5 - self.assertEqual(x.foo, 5) - self.assertEqual(type(slots[0]), unicode) - # this used to leak references - try: - class C(object): - __slots__ = [unichr(128)] - except (TypeError, UnicodeEncodeError): - pass - else: - self.fail("[unichr(128)] slots not caught") + raise TestFailed("[chr(128)] slots not caught") # Test leaks class Counted(object): @@ -1096,7 +1155,7 @@ order (MRO) for bases """ x.c = Counted() self.assertEqual(Counted.counter, 3) del x - test_support.gc_collect() + support.gc_collect() self.assertEqual(Counted.counter, 0) class D(C): pass @@ -1105,7 +1164,7 @@ order (MRO) for bases """ x.z = Counted() self.assertEqual(Counted.counter, 2) del x - test_support.gc_collect() + support.gc_collect() self.assertEqual(Counted.counter, 0) class E(D): __slots__ = ['e'] @@ -1115,7 +1174,7 @@ order (MRO) for bases """ x.e = Counted() self.assertEqual(Counted.counter, 3) del x - test_support.gc_collect() + support.gc_collect() self.assertEqual(Counted.counter, 0) # Test cyclical leaks [SF bug 519621] @@ -1125,18 +1184,17 @@ order (MRO) for bases """ s.a = [Counted(), s] self.assertEqual(Counted.counter, 1) s = None - test_support.gc_collect() + support.gc_collect() self.assertEqual(Counted.counter, 0) # Test lookup leaks [SF bug 572567] if hasattr(gc, 'get_objects'): class G(object): - def __cmp__(self, other): - return 0 - __hash__ = None # Silence Py3k warning + def __eq__(self, other): + return False g = G() orig_objects = len(gc.get_objects()) - for i in xrange(10): + for i in range(10): g==g new_objects = len(gc.get_objects()) self.assertEqual(orig_objects, new_objects) @@ -1149,7 +1207,7 @@ order (MRO) for bases """ def __del__(self_): self.assertEqual(self_.a, 1) self.assertEqual(self_.b, 2) - with test_support.captured_output('stderr') as s: + with support.captured_output('stderr') as s: h = H() del h self.assertEqual(s.getvalue(), '') @@ -1201,8 +1259,7 @@ order (MRO) for bases """ # Issue2115: slot descriptors did not correctly check # the type of the given object import abc - class MyABC: - __metaclass__ = abc.ABCMeta + class MyABC(metaclass=abc.ABCMeta): __slots__ = "a" class Unrelated(object): @@ -1215,15 +1272,6 @@ order (MRO) for bases """ # This used to crash self.assertRaises(TypeError, MyABC.a.__set__, u, 3) - def test_metaclass_cmp(self): - # See bug 7491. - class M(type): - def __cmp__(self, other): - return -1 - class X(object): - __metaclass__ = M - self.assertTrue(X < M) - def test_dynamics(self): # Testing class attribute propagation... class D(object): @@ -1287,20 +1335,11 @@ order (MRO) for bases """ self.assertEqual(I(3)*2, 6) self.assertEqual(I(3)*I(2), 6) - # Test handling of long*seq and seq*long - class L(long): - pass - self.assertEqual("a"*L(2L), "aa") - self.assertEqual(L(2L)*"a", "aa") - self.assertEqual(2*L(3), 6) - self.assertEqual(L(3)*2, 6) - self.assertEqual(L(3)*L(2), 6) - # Test comparison of classes with dynamic metaclasses class dynamicmetaclass(type): pass - class someclass: - __metaclass__ = dynamicmetaclass + class someclass(metaclass=dynamicmetaclass): + pass self.assertNotEqual(someclass, object) def test_errors(self): @@ -1351,10 +1390,10 @@ order (MRO) for bases """ pass class M2(type): pass - class A1(object): - __metaclass__ = M1 - class A2(object): - __metaclass__ = M2 + class A1(object, metaclass=M1): + pass + class A2(object, metaclass=M2): + pass try: class B(A1, A2): pass @@ -1386,10 +1425,10 @@ order (MRO) for bases """ self.assertEqual(ff.__get__(0)(42), (int, 42)) # Test super() with classmethods (SF bug 535444) - self.assertEqual(C.goo.im_self, C) - self.assertEqual(D.goo.im_self, D) - self.assertEqual(super(D,D).goo.im_self, D) - self.assertEqual(super(D,d).goo.im_self, D) + self.assertEqual(C.goo.__self__, C) + self.assertEqual(D.goo.__self__, D) + self.assertEqual(super(D,D).goo.__self__, D) + self.assertEqual(super(D,d).goo.__self__, D) self.assertEqual(super(D,D).goo(), (D,)) self.assertEqual(super(D,d).goo(), (D,)) @@ -1405,7 +1444,7 @@ order (MRO) for bases """ else: self.fail("classmethod shouldn't accept keyword args") - @test_support.impl_detail("the module 'xxsubtype' is internal") + @support.impl_detail("the module 'xxsubtype' is internal") def test_classmethods_in_c(self): # Testing C-based class methods... import xxsubtype as spam @@ -1453,7 +1492,7 @@ order (MRO) for bases """ self.assertEqual(d.foo(1), (d, 1)) self.assertEqual(D.foo(d, 1), (d, 1)) - @test_support.impl_detail("the module 'xxsubtype' is internal") + @support.impl_detail("the module 'xxsubtype' is internal") def test_staticmethods_in_c(self): # Testing C-based static methods... import xxsubtype as spam @@ -1486,7 +1525,7 @@ order (MRO) for bases """ self.assertEqual(D.foo(d, 1), (d, 1)) class E: # *not* subclassing from C foo = C.foo - self.assertEqual(E().foo, C.foo) # i.e., unbound + self.assertEqual(E().foo.__func__, C.foo) # i.e., unbound self.assertTrue(repr(C.foo.__get__(C())).startswith("<bound method ")) def test_compattr(self): @@ -1560,16 +1599,17 @@ order (MRO) for bases """ L = type.mro(cls) L.reverse() return L - class X(D,B,C,A): - __metaclass__ = PerverseMetaType + class X(D,B,C,A, metaclass=PerverseMetaType): + pass self.assertEqual(X.__mro__, (object, A, C, B, D, X)) self.assertEqual(X().f(), "A") try: - class X(object): - class __metaclass__(type): - def mro(self): - return [self, dict, object] + class _metaclass(type): + def mro(self): + return [self, dict, object] + class X(object, metaclass=_metaclass): + pass # In CPython, the class creation above already raises # TypeError, as a protection against the fact that # instances of X would segfault it. In other Python @@ -1584,20 +1624,22 @@ order (MRO) for bases """ self.fail("devious mro() return not caught") try: - class X(object): - class __metaclass__(type): - def mro(self): - return [1] + class _metaclass(type): + def mro(self): + return [1] + class X(object, metaclass=_metaclass): + pass except TypeError: pass else: self.fail("non-class mro() return not caught") try: - class X(object): - class __metaclass__(type): - def mro(self): - return 1 + class _metaclass(type): + def mro(self): + return 1 + class X(object, metaclass=_metaclass): + pass except TypeError: pass else: @@ -1633,13 +1675,6 @@ order (MRO) for bases """ def __delitem__(self, key): self.delitem = key - def __getslice__(self, i, j): - return ("getslice", i, j) - def __setslice__(self, i, j, value): - self.setslice = (i, j, value) - def __delslice__(self, i, j): - self.delslice = (i, j) - a = C() self.assertEqual(a.foo, ("getattr", "foo")) a.foo = 12 @@ -1653,11 +1688,11 @@ order (MRO) for bases """ del a[12] self.assertEqual(a.delitem, 12) - self.assertEqual(a[0:10], ("getslice", 0, 10)) + self.assertEqual(a[0:10], ("getitem", slice(0, 10))) a[0:10] = "foo" - self.assertEqual(a.setslice, (0, 10, "foo")) + self.assertEqual(a.setitem, (slice(0, 10), "foo")) del a[0:10] - self.assertEqual(a.delslice, (0, 10)) + self.assertEqual(a.delitem, (slice(0, 10))) def test_methods(self): # Testing methods... @@ -1677,7 +1712,7 @@ order (MRO) for bases """ self.assertEqual(d2.goo(), 1) class E(object): foo = C.foo - self.assertEqual(E().foo, C.foo) # i.e., unbound + self.assertEqual(E().foo.__func__, C.foo) # i.e., unbound self.assertTrue(repr(C.foo.__get__(C(1))).startswith("<bound method ")) def test_special_method_lookup(self): @@ -1690,7 +1725,7 @@ order (MRO) for bases """ def iden(self): return self def hello(self): - return "hello" + return b"hello" def empty_seq(self): return [] def zero(self): @@ -1705,8 +1740,6 @@ order (MRO) for bases """ return isinstance(int, obj) def do_issubclass(obj): return issubclass(int, obj) - def swallow(*args): - pass def do_dict_missing(checker): class DictSub(checker.__class__, dict): pass @@ -1714,6 +1747,7 @@ order (MRO) for bases """ def some_number(self_, key): self.assertEqual(key, "hi") return 4 + def swallow(*args): pass def format_impl(self, spec): return "hello" @@ -1721,10 +1755,10 @@ order (MRO) for bases """ # only listing the ones I can remember outside of typeobject.c, since it # does it right. specials = [ - ("__unicode__", unicode, hello, set(), {}), + ("__bytes__", bytes, hello, set(), {}), ("__reversed__", reversed, empty_seq, set(), {}), ("__length_hint__", list, zero, set(), - {"__iter__" : iden, "next" : stop}), + {"__iter__" : iden, "__next__" : stop}), ("__sizeof__", sys.getsizeof, zero, set(), {}), ("__instancecheck__", do_isinstance, return_true, set(), {}), ("__missing__", do_dict_missing, some_number, @@ -1735,6 +1769,9 @@ order (MRO) for bases """ ("__exit__", run_context, swallow, set(), {"__enter__" : iden}), ("__complex__", complex, complex_num, set(), {}), ("__format__", format, format_impl, set(), {}), + ("__floor__", math.floor, zero, set(), {}), + ("__trunc__", math.trunc, zero, set(), {}), + ("__ceil__", math.ceil, zero, set(), {}), ("__dir__", dir, empty_seq, set(), {}), ] @@ -1760,7 +1797,7 @@ order (MRO) for bases """ for name, runner, meth_impl, ok, env in specials: class X(Checker): pass - for attr, obj in env.iteritems(): + for attr, obj in env.items(): setattr(X, attr, obj) setattr(X, name, meth_impl) runner(X()) @@ -1768,7 +1805,7 @@ order (MRO) for bases """ record = [] class X(Checker): pass - for attr, obj in env.iteritems(): + for attr, obj in env.items(): setattr(X, attr, obj) setattr(X, name, SpecialDescr(meth_impl)) runner(X()) @@ -1776,7 +1813,7 @@ order (MRO) for bases """ class X(Checker): pass - for attr, obj in env.iteritems(): + for attr, obj in env.items(): setattr(X, attr, obj) setattr(X, name, ErrDescr()) try: @@ -1801,7 +1838,6 @@ order (MRO) for bases """ self.assertNotEqual(id(c1), id(c2)) hash(c1) hash(c2) - self.assertEqual(cmp(c1, c2), cmp(id(c1), id(c2))) self.assertEqual(c1, c1) self.assertTrue(c1 != c2) self.assertTrue(not c1 != c1) @@ -1825,7 +1861,6 @@ order (MRO) for bases """ self.assertNotEqual(id(d1), id(d2)) hash(d1) hash(d2) - self.assertEqual(cmp(d1, d2), cmp(id(d1), id(d2))) self.assertEqual(d1, d1) self.assertNotEqual(d1, d2) self.assertTrue(not d1 != d1) @@ -1838,11 +1873,11 @@ order (MRO) for bases """ for i in range(10): self.assertIn(i, d1) self.assertNotIn(10, d1) - # Test overridden behavior for static classes + # Test overridden behavior class Proxy(object): def __init__(self, x): self.x = x - def __nonzero__(self): + def __bool__(self): return not not self.x def __hash__(self): return hash(self.x) @@ -1850,8 +1885,14 @@ order (MRO) for bases """ return self.x == other def __ne__(self, other): return self.x != other - def __cmp__(self, other): - return cmp(self.x, other.x) + def __ge__(self, other): + return self.x >= other + def __gt__(self, other): + return self.x > other + def __le__(self, other): + return self.x <= other + def __lt__(self, other): + return self.x < other def __str__(self): return "Proxy:%s" % self.x def __repr__(self): @@ -1868,9 +1909,10 @@ order (MRO) for bases """ self.assertNotEqual(p0, p1) self.assertTrue(not p0 != p0) self.assertEqual(not p0, p1) - self.assertEqual(cmp(p0, p1), -1) - self.assertEqual(cmp(p0, p0), 0) - self.assertEqual(cmp(p0, p_1), 1) + self.assertTrue(p0 < p1) + self.assertTrue(p0 <= p1) + self.assertTrue(p1 > p0) + self.assertTrue(p1 >= p0) self.assertEqual(str(p0), "Proxy:0") self.assertEqual(repr(p0), "Proxy(0)") p10 = Proxy(range(10)) @@ -1878,104 +1920,6 @@ order (MRO) for bases """ for i in range(10): self.assertIn(i, p10) self.assertNotIn(10, p10) - # Test overridden behavior for dynamic classes - class DProxy(object): - def __init__(self, x): - self.x = x - def __nonzero__(self): - return not not self.x - def __hash__(self): - return hash(self.x) - def __eq__(self, other): - return self.x == other - def __ne__(self, other): - return self.x != other - def __cmp__(self, other): - return cmp(self.x, other.x) - def __str__(self): - return "DProxy:%s" % self.x - def __repr__(self): - return "DProxy(%r)" % self.x - def __contains__(self, value): - return value in self.x - p0 = DProxy(0) - p1 = DProxy(1) - p_1 = DProxy(-1) - self.assertFalse(p0) - self.assertTrue(not not p1) - self.assertEqual(hash(p0), hash(0)) - self.assertEqual(p0, p0) - self.assertNotEqual(p0, p1) - self.assertNotEqual(not p0, p0) - self.assertEqual(not p0, p1) - self.assertEqual(cmp(p0, p1), -1) - self.assertEqual(cmp(p0, p0), 0) - self.assertEqual(cmp(p0, p_1), 1) - self.assertEqual(str(p0), "DProxy:0") - self.assertEqual(repr(p0), "DProxy(0)") - p10 = DProxy(range(10)) - self.assertNotIn(-1, p10) - for i in range(10): - self.assertIn(i, p10) - self.assertNotIn(10, p10) - - # Safety test for __cmp__ - def unsafecmp(a, b): - if not hasattr(a, '__cmp__'): - return # some types don't have a __cmp__ any more (so the - # test doesn't make sense any more), or maybe they - # never had a __cmp__ at all, e.g. in PyPy - try: - a.__class__.__cmp__(a, b) - except TypeError: - pass - else: - self.fail("shouldn't allow %s.__cmp__(%r, %r)" % ( - a.__class__, a, b)) - - unsafecmp(u"123", "123") - unsafecmp("123", u"123") - unsafecmp(1, 1.0) - unsafecmp(1.0, 1) - unsafecmp(1, 1L) - unsafecmp(1L, 1) - - @test_support.impl_detail("custom logic for printing to real file objects") - def test_recursions_1(self): - # Testing recursion checks ... - class Letter(str): - def __new__(cls, letter): - if letter == 'EPS': - return str.__new__(cls) - return str.__new__(cls, letter) - def __str__(self): - if not self: - return 'EPS' - return self - # sys.stdout needs to be the original to trigger the recursion bug - test_stdout = sys.stdout - sys.stdout = test_support.get_original_stdout() - try: - # nothing should actually be printed, this should raise an exception - print Letter('w') - except RuntimeError: - pass - else: - self.fail("expected a RuntimeError for print recursion") - finally: - sys.stdout = test_stdout - - def test_recursions_2(self): - # Bug #1202533. - class A(object): - pass - A.__mul__ = types.MethodType(lambda self, x: self * x, None, A) - try: - A()*2 - except RuntimeError: - pass - else: - self.fail("expected a RuntimeError") def test_weakrefs(self): # Testing weak references... @@ -1986,7 +1930,7 @@ order (MRO) for bases """ r = weakref.ref(c) self.assertEqual(r(), c) del c - test_support.gc_collect() + support.gc_collect() self.assertEqual(r(), None) del r class NoWeak(object): @@ -1994,7 +1938,7 @@ order (MRO) for bases """ no = NoWeak() try: weakref.ref(no) - except TypeError, msg: + except TypeError as msg: self.assertTrue(str(msg).find("weak reference") >= 0) else: self.fail("weakref.ref(no) should be illegal") @@ -2004,7 +1948,7 @@ order (MRO) for bases """ r = weakref.ref(yes) self.assertEqual(r(), yes) del yes - test_support.gc_collect() + support.gc_collect() self.assertEqual(r(), None) del r @@ -2048,13 +1992,13 @@ order (MRO) for bases """ for attr in "__doc__", "fget", "fset", "fdel": try: setattr(raw, attr, 42) - except TypeError, msg: + except AttributeError as msg: if str(msg).find('readonly') < 0: self.fail("when setting readonly attr %r on a property, " - "got unexpected TypeError msg %r" % (attr, str(msg))) + "got unexpected AttributeError msg %r" % (attr, str(msg))) else: - self.fail("expected TypeError from trying to set readonly %r " - "attr on a property" % attr) + self.fail("expected AttributeError from trying to set readonly %r " + "attr on a property" % attr) class D(object): __getitem__ = property(lambda s: 1/0) @@ -2168,8 +2112,8 @@ order (MRO) for bases """ self.assertEqual(d, {}) d = dict({1: 2, 'a': 'b'}) self.assertEqual(d, {1: 2, 'a': 'b'}) - self.assertEqual(d, dict(d.items())) - self.assertEqual(d, dict(d.iteritems())) + self.assertEqual(d, dict(list(d.items()))) + self.assertEqual(d, dict(iter(d.items()))) d = dict({'one':1, 'two':2}) self.assertEqual(d, dict(one=1, two=2)) self.assertEqual(d, dict(**d)) @@ -2178,7 +2122,7 @@ order (MRO) for bases """ self.assertEqual(d, dict([("one", 100), ("two", 200)], **d)) self.assertEqual(d, dict(**d)) - for badarg in 0, 0L, 0j, "0", [0], (0,): + for badarg in 0, 0, 0j, "0", [0], (0,): try: dict(badarg) except TypeError: @@ -2212,7 +2156,7 @@ order (MRO) for bases """ else: self.fail("no TypeError from dict(incomplete mapping)") - Mapping.keys = lambda self: self.dict.keys() + Mapping.keys = lambda self: list(self.dict.keys()) Mapping.__getitem__ = lambda self, i: self.dict[i] d = dict(Mapping()) self.assertEqual(d, Mapping.dict) @@ -2250,41 +2194,10 @@ order (MRO) for bases """ del junk # Just make sure these don't blow up! - for arg in 2, 2L, 2j, 2e0, [2], "2", u"2", (2,), {2:2}, type, self.test_dir: + for arg in 2, 2, 2j, 2e0, [2], "2", b"2", (2,), {2:2}, type, self.test_dir: dir(arg) - # Try classic classes. - class C: - Cdata = 1 - def Cmethod(self): pass - - cstuff = ['Cdata', 'Cmethod', '__doc__', '__module__'] - self.assertEqual(dir(C), cstuff) - self.assertIn('im_self', dir(C.Cmethod)) - - c = C() # c.__doc__ is an odd thing to see here; ditto c.__module__. - self.assertEqual(dir(c), cstuff) - - c.cdata = 2 - c.cmethod = lambda self: 0 - self.assertEqual(dir(c), cstuff + ['cdata', 'cmethod']) - self.assertIn('im_self', dir(c.Cmethod)) - - class A(C): - Adata = 1 - def Amethod(self): pass - - astuff = ['Adata', 'Amethod'] + cstuff - self.assertEqual(dir(A), astuff) - self.assertIn('im_self', dir(A.Amethod)) - a = A() - self.assertEqual(dir(a), astuff) - self.assertIn('im_self', dir(a.Amethod)) - a.adata = 42 - a.amethod = lambda self: 3 - self.assertEqual(dir(a), astuff + ['adata', 'amethod']) - - # The same, but with new-style classes. Since these have object as a + # Test dir on new-style classes. Since these have object as a # base class, a lot more gets sucked in. def interesting(strings): return [s for s in strings if not s.startswith('_')] @@ -2298,12 +2211,12 @@ order (MRO) for bases """ c = C() self.assertEqual(interesting(dir(c)), cstuff) - self.assertIn('im_self', dir(C.Cmethod)) + ## self.assertIn('__self__', dir(C.Cmethod)) c.cdata = 2 c.cmethod = lambda self: 0 self.assertEqual(interesting(dir(c)), cstuff + ['cdata', 'cmethod']) - self.assertIn('im_self', dir(c.Cmethod)) + ## self.assertIn('__self__', dir(c.Cmethod)) class A(C): Adata = 1 @@ -2311,13 +2224,13 @@ order (MRO) for bases """ astuff = ['Adata', 'Amethod'] + cstuff self.assertEqual(interesting(dir(A)), astuff) - self.assertIn('im_self', dir(A.Amethod)) + ## self.assertIn('__self__', dir(A.Amethod)) a = A() self.assertEqual(interesting(dir(a)), astuff) a.adata = 42 a.amethod = lambda self: 3 self.assertEqual(interesting(dir(a)), astuff + ['adata', 'amethod']) - self.assertIn('im_self', dir(a.Amethod)) + ## self.assertIn('__self__', dir(a.Amethod)) # Try a module subclass. class M(type(sys)): @@ -2345,7 +2258,7 @@ order (MRO) for bases """ # Two essentially featureless objects, just inheriting stuff from # object. self.assertEqual(dir(NotImplemented), dir(Ellipsis)) - if test_support.check_impl_detail(): + if support.check_impl_detail(): # None differs in PyPy: it has a __nonzero__ self.assertEqual(dir(None), dir(Ellipsis)) @@ -2508,54 +2421,51 @@ order (MRO) for bases """ self.assertTrue((hexint(0) << 12).__class__ is int) self.assertTrue((hexint(0) >> 12).__class__ is int) - class octlong(long): + class octlong(int): __slots__ = [] def __str__(self): - s = oct(self) - if s[-1] == 'L': - s = s[:-1] - return s + return oct(self) def __add__(self, other): return self.__class__(super(octlong, self).__add__(other)) __radd__ = __add__ - self.assertEqual(str(octlong(3) + 5), "010") + self.assertEqual(str(octlong(3) + 5), "0o10") # (Note that overriding __radd__ here only seems to work # because the example uses a short int left argument.) - self.assertEqual(str(5 + octlong(3000)), "05675") + self.assertEqual(str(5 + octlong(3000)), "0o5675") a = octlong(12345) - self.assertEqual(a, 12345L) - self.assertEqual(long(a), 12345L) - self.assertEqual(hash(a), hash(12345L)) - self.assertTrue(long(a).__class__ is long) - self.assertTrue((+a).__class__ is long) - self.assertTrue((-a).__class__ is long) - self.assertTrue((-octlong(0)).__class__ is long) - self.assertTrue((a >> 0).__class__ is long) - self.assertTrue((a << 0).__class__ is long) - self.assertTrue((a - 0).__class__ is long) - self.assertTrue((a * 1).__class__ is long) - self.assertTrue((a ** 1).__class__ is long) - self.assertTrue((a // 1).__class__ is long) - self.assertTrue((1 * a).__class__ is long) - self.assertTrue((a | 0).__class__ is long) - self.assertTrue((a ^ 0).__class__ is long) - self.assertTrue((a & -1L).__class__ is long) - self.assertTrue((octlong(0) << 12).__class__ is long) - self.assertTrue((octlong(0) >> 12).__class__ is long) - self.assertTrue(abs(octlong(0)).__class__ is long) + self.assertEqual(a, 12345) + self.assertEqual(int(a), 12345) + self.assertEqual(hash(a), hash(12345)) + self.assertTrue(int(a).__class__ is int) + self.assertTrue((+a).__class__ is int) + self.assertTrue((-a).__class__ is int) + self.assertTrue((-octlong(0)).__class__ is int) + self.assertTrue((a >> 0).__class__ is int) + self.assertTrue((a << 0).__class__ is int) + self.assertTrue((a - 0).__class__ is int) + self.assertTrue((a * 1).__class__ is int) + self.assertTrue((a ** 1).__class__ is int) + self.assertTrue((a // 1).__class__ is int) + self.assertTrue((1 * a).__class__ is int) + self.assertTrue((a | 0).__class__ is int) + self.assertTrue((a ^ 0).__class__ is int) + self.assertTrue((a & -1).__class__ is int) + self.assertTrue((octlong(0) << 12).__class__ is int) + self.assertTrue((octlong(0) >> 12).__class__ is int) + self.assertTrue(abs(octlong(0)).__class__ is int) # Because octlong overrides __add__, we can't check the absence of +0 # optimizations using octlong. - class longclone(long): + class longclone(int): pass a = longclone(1) - self.assertTrue((a + 0).__class__ is long) - self.assertTrue((0 + a).__class__ is long) + self.assertTrue((a + 0).__class__ is int) + self.assertTrue((0 + a).__class__ is int) # Check that negative clones don't segfault a = longclone(-1) self.assertEqual(a.__dict__, {}) - self.assertEqual(long(a), -1) # self.assertTrue PyNumber_Long() copies the sign bit + self.assertEqual(int(a), -1) # self.assertTrue PyNumber_Long() copies the sign bit class precfloat(float): __slots__ = ['prec'] @@ -2682,12 +2592,9 @@ order (MRO) for bases """ self.assertEqual(s.lstrip(), base) self.assertTrue(s.rstrip().__class__ is str) self.assertEqual(s.rstrip(), base) - identitytab = ''.join([chr(i) for i in range(256)]) + identitytab = {} self.assertTrue(s.translate(identitytab).__class__ is str) self.assertEqual(s.translate(identitytab), base) - self.assertTrue(s.translate(identitytab, "x").__class__ is str) - self.assertEqual(s.translate(identitytab, "x"), base) - self.assertEqual(s.translate(identitytab, "\x00"), "") self.assertTrue(s.replace("x", "x").__class__ is str) self.assertEqual(s.replace("x", "x"), base) self.assertTrue(s.ljust(len(s)).__class__ is str) @@ -2699,144 +2606,144 @@ order (MRO) for bases """ self.assertTrue(s.lower().__class__ is str) self.assertEqual(s.lower(), base) - class madunicode(unicode): + class madunicode(str): _rev = None def rev(self): if self._rev is not None: return self._rev L = list(self) L.reverse() - self._rev = self.__class__(u"".join(L)) + self._rev = self.__class__("".join(L)) return self._rev u = madunicode("ABCDEF") - self.assertEqual(u, u"ABCDEF") - self.assertEqual(u.rev(), madunicode(u"FEDCBA")) - self.assertEqual(u.rev().rev(), madunicode(u"ABCDEF")) - base = u"12345" + self.assertEqual(u, "ABCDEF") + self.assertEqual(u.rev(), madunicode("FEDCBA")) + self.assertEqual(u.rev().rev(), madunicode("ABCDEF")) + base = "12345" u = madunicode(base) - self.assertEqual(unicode(u), base) - self.assertTrue(unicode(u).__class__ is unicode) + self.assertEqual(str(u), base) + self.assertTrue(str(u).__class__ is str) self.assertEqual(hash(u), hash(base)) self.assertEqual({u: 1}[base], 1) self.assertEqual({base: 1}[u], 1) - self.assertTrue(u.strip().__class__ is unicode) + self.assertTrue(u.strip().__class__ is str) self.assertEqual(u.strip(), base) - self.assertTrue(u.lstrip().__class__ is unicode) + self.assertTrue(u.lstrip().__class__ is str) self.assertEqual(u.lstrip(), base) - self.assertTrue(u.rstrip().__class__ is unicode) + self.assertTrue(u.rstrip().__class__ is str) self.assertEqual(u.rstrip(), base) - self.assertTrue(u.replace(u"x", u"x").__class__ is unicode) - self.assertEqual(u.replace(u"x", u"x"), base) - self.assertTrue(u.replace(u"xy", u"xy").__class__ is unicode) - self.assertEqual(u.replace(u"xy", u"xy"), base) - self.assertTrue(u.center(len(u)).__class__ is unicode) + self.assertTrue(u.replace("x", "x").__class__ is str) + self.assertEqual(u.replace("x", "x"), base) + self.assertTrue(u.replace("xy", "xy").__class__ is str) + self.assertEqual(u.replace("xy", "xy"), base) + self.assertTrue(u.center(len(u)).__class__ is str) self.assertEqual(u.center(len(u)), base) - self.assertTrue(u.ljust(len(u)).__class__ is unicode) + self.assertTrue(u.ljust(len(u)).__class__ is str) self.assertEqual(u.ljust(len(u)), base) - self.assertTrue(u.rjust(len(u)).__class__ is unicode) + self.assertTrue(u.rjust(len(u)).__class__ is str) self.assertEqual(u.rjust(len(u)), base) - self.assertTrue(u.lower().__class__ is unicode) + self.assertTrue(u.lower().__class__ is str) self.assertEqual(u.lower(), base) - self.assertTrue(u.upper().__class__ is unicode) + self.assertTrue(u.upper().__class__ is str) self.assertEqual(u.upper(), base) - self.assertTrue(u.capitalize().__class__ is unicode) + self.assertTrue(u.capitalize().__class__ is str) self.assertEqual(u.capitalize(), base) - self.assertTrue(u.title().__class__ is unicode) + self.assertTrue(u.title().__class__ is str) self.assertEqual(u.title(), base) - self.assertTrue((u + u"").__class__ is unicode) - self.assertEqual(u + u"", base) - self.assertTrue((u"" + u).__class__ is unicode) - self.assertEqual(u"" + u, base) - self.assertTrue((u * 0).__class__ is unicode) - self.assertEqual(u * 0, u"") - self.assertTrue((u * 1).__class__ is unicode) + self.assertTrue((u + "").__class__ is str) + self.assertEqual(u + "", base) + self.assertTrue(("" + u).__class__ is str) + self.assertEqual("" + u, base) + self.assertTrue((u * 0).__class__ is str) + self.assertEqual(u * 0, "") + self.assertTrue((u * 1).__class__ is str) self.assertEqual(u * 1, base) - self.assertTrue((u * 2).__class__ is unicode) + self.assertTrue((u * 2).__class__ is str) self.assertEqual(u * 2, base + base) - self.assertTrue(u[:].__class__ is unicode) + self.assertTrue(u[:].__class__ is str) self.assertEqual(u[:], base) - self.assertTrue(u[0:0].__class__ is unicode) - self.assertEqual(u[0:0], u"") + self.assertTrue(u[0:0].__class__ is str) + self.assertEqual(u[0:0], "") class sublist(list): pass a = sublist(range(5)) - self.assertEqual(a, range(5)) + self.assertEqual(a, list(range(5))) a.append("hello") - self.assertEqual(a, range(5) + ["hello"]) + self.assertEqual(a, list(range(5)) + ["hello"]) a[5] = 5 - self.assertEqual(a, range(6)) + self.assertEqual(a, list(range(6))) a.extend(range(6, 20)) - self.assertEqual(a, range(20)) + self.assertEqual(a, list(range(20))) a[-5:] = [] - self.assertEqual(a, range(15)) + self.assertEqual(a, list(range(15))) del a[10:15] self.assertEqual(len(a), 10) - self.assertEqual(a, range(10)) - self.assertEqual(list(a), range(10)) + self.assertEqual(a, list(range(10))) + self.assertEqual(list(a), list(range(10))) self.assertEqual(a[0], 0) self.assertEqual(a[9], 9) self.assertEqual(a[-10], 0) self.assertEqual(a[-1], 9) - self.assertEqual(a[:5], range(5)) - - class CountedInput(file): - """Counts lines read by self.readline(). - - self.lineno is the 0-based ordinal of the last line read, up to - a maximum of one greater than the number of lines in the file. - - self.ateof is true if and only if the final "" line has been read, - at which point self.lineno stops incrementing, and further calls - to readline() continue to return "". - """ - - lineno = 0 - ateof = 0 - def readline(self): - if self.ateof: - return "" - s = file.readline(self) - # Next line works too. - # s = super(CountedInput, self).readline() - self.lineno += 1 - if s == "": - self.ateof = 1 - return s - - f = file(name=test_support.TESTFN, mode='w') - lines = ['a\n', 'b\n', 'c\n'] - try: - f.writelines(lines) - f.close() - f = CountedInput(test_support.TESTFN) - for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]): - got = f.readline() - self.assertEqual(expected, got) - self.assertEqual(f.lineno, i) - self.assertEqual(f.ateof, (i > len(lines))) - f.close() - finally: - try: - f.close() - except: - pass - test_support.unlink(test_support.TESTFN) + self.assertEqual(a[:5], list(range(5))) + + ## class CountedInput(file): + ## """Counts lines read by self.readline(). + ## + ## self.lineno is the 0-based ordinal of the last line read, up to + ## a maximum of one greater than the number of lines in the file. + ## + ## self.ateof is true if and only if the final "" line has been read, + ## at which point self.lineno stops incrementing, and further calls + ## to readline() continue to return "". + ## """ + ## + ## lineno = 0 + ## ateof = 0 + ## def readline(self): + ## if self.ateof: + ## return "" + ## s = file.readline(self) + ## # Next line works too. + ## # s = super(CountedInput, self).readline() + ## self.lineno += 1 + ## if s == "": + ## self.ateof = 1 + ## return s + ## + ## f = file(name=support.TESTFN, mode='w') + ## lines = ['a\n', 'b\n', 'c\n'] + ## try: + ## f.writelines(lines) + ## f.close() + ## f = CountedInput(support.TESTFN) + ## for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]): + ## got = f.readline() + ## self.assertEqual(expected, got) + ## self.assertEqual(f.lineno, i) + ## self.assertEqual(f.ateof, (i > len(lines))) + ## f.close() + ## finally: + ## try: + ## f.close() + ## except: + ## pass + ## support.unlink(support.TESTFN) def test_keywords(self): # Testing keyword args to basic type constructors ... self.assertEqual(int(x=1), 1) self.assertEqual(float(x=2), 2.0) - self.assertEqual(long(x=3), 3L) + self.assertEqual(int(x=3), 3) self.assertEqual(complex(imag=42, real=666), complex(666, 42)) self.assertEqual(str(object=500), '500') - self.assertEqual(unicode(string='abc', errors='strict'), u'abc') + self.assertEqual(str(object=b'abc', errors='strict'), 'abc') self.assertEqual(tuple(sequence=range(3)), (0, 1, 2)) - self.assertEqual(list(sequence=(0, 1, 2)), range(3)) + self.assertEqual(list(sequence=(0, 1, 2)), list(range(3))) # note: as of Python 2.3, dict() no longer has an "items" keyword arg - for constructor in (int, float, long, complex, str, unicode, - tuple, list, file): + for constructor in (int, float, int, complex, str, str, + tuple, list): try: constructor(bogus_keyword_arg=1) except TypeError: @@ -2886,13 +2793,42 @@ order (MRO) for bases """ class C(base): def __init__(self, value): self.value = int(value) - def __cmp__(self, other): + def __eq__(self, other): + if isinstance(other, C): + return self.value == other.value + if isinstance(other, int) or isinstance(other, int): + return self.value == other + return NotImplemented + def __ne__(self, other): + if isinstance(other, C): + return self.value != other.value + if isinstance(other, int) or isinstance(other, int): + return self.value != other + return NotImplemented + def __lt__(self, other): + if isinstance(other, C): + return self.value < other.value + if isinstance(other, int) or isinstance(other, int): + return self.value < other + return NotImplemented + def __le__(self, other): + if isinstance(other, C): + return self.value <= other.value + if isinstance(other, int) or isinstance(other, int): + return self.value <= other + return NotImplemented + def __gt__(self, other): + if isinstance(other, C): + return self.value > other.value + if isinstance(other, int) or isinstance(other, int): + return self.value > other + return NotImplemented + def __ge__(self, other): if isinstance(other, C): - return cmp(self.value, other.value) - if isinstance(other, int) or isinstance(other, long): - return cmp(self.value, other) + return self.value >= other.value + if isinstance(other, int) or isinstance(other, int): + return self.value >= other return NotImplemented - __hash__ = None # Silence Py3k warning c1 = C(1) c2 = C(2) @@ -2901,12 +2837,16 @@ order (MRO) for bases """ c = {1: c1, 2: c2, 3: c3} for x in 1, 2, 3: for y in 1, 2, 3: - self.assertTrue(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) for op in "<", "<=", "==", "!=", ">", ">=": - self.assertTrue(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), - "x=%d, y=%d" % (x, y)) - self.assertTrue(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y)) - self.assertTrue(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) + self.assertTrue(eval("c[x] %s c[y]" % op) == + eval("x %s y" % op), + "x=%d, y=%d" % (x, y)) + self.assertTrue(eval("c[x] %s y" % op) == + eval("x %s y" % op), + "x=%d, y=%d" % (x, y)) + self.assertTrue(eval("x %s c[y]" % op) == + eval("x %s y" % op), + "x=%d, y=%d" % (x, y)) def test_rich_comparisons(self): # Testing rich comparisons... @@ -2921,7 +2861,6 @@ order (MRO) for bases """ return abs(self - other) <= 1e-6 except: return NotImplemented - __hash__ = None # Silence Py3k warning zz = ZZ(1.0000003) self.assertEqual(zz, 1+0j) self.assertEqual(1+0j, zz) @@ -2934,41 +2873,40 @@ order (MRO) for bases """ self.value = int(value) def __cmp__(self_, other): self.fail("shouldn't call __cmp__") - __hash__ = None # Silence Py3k warning def __eq__(self, other): if isinstance(other, C): return self.value == other.value - if isinstance(other, int) or isinstance(other, long): + if isinstance(other, int) or isinstance(other, int): return self.value == other return NotImplemented def __ne__(self, other): if isinstance(other, C): return self.value != other.value - if isinstance(other, int) or isinstance(other, long): + if isinstance(other, int) or isinstance(other, int): return self.value != other return NotImplemented def __lt__(self, other): if isinstance(other, C): return self.value < other.value - if isinstance(other, int) or isinstance(other, long): + if isinstance(other, int) or isinstance(other, int): return self.value < other return NotImplemented def __le__(self, other): if isinstance(other, C): return self.value <= other.value - if isinstance(other, int) or isinstance(other, long): + if isinstance(other, int) or isinstance(other, int): return self.value <= other return NotImplemented def __gt__(self, other): if isinstance(other, C): return self.value > other.value - if isinstance(other, int) or isinstance(other, long): + if isinstance(other, int) or isinstance(other, int): return self.value > other return NotImplemented def __ge__(self, other): if isinstance(other, C): return self.value >= other.value - if isinstance(other, int) or isinstance(other, long): + if isinstance(other, int) or isinstance(other, int): return self.value >= other return NotImplemented c1 = C(1) @@ -2986,39 +2924,13 @@ order (MRO) for bases """ self.assertTrue(eval("x %s c[y]" % op) == eval("x %s y" % op), "x=%d, y=%d" % (x, y)) - def test_coercions(self): - # Testing coercions... - class I(int): pass - coerce(I(0), 0) - coerce(0, I(0)) - class L(long): pass - coerce(L(0), 0) - coerce(L(0), 0L) - coerce(0, L(0)) - coerce(0L, L(0)) - class F(float): pass - coerce(F(0), 0) - coerce(F(0), 0L) - coerce(F(0), 0.) - coerce(0, F(0)) - coerce(0L, F(0)) - coerce(0., F(0)) - class C(complex): pass - coerce(C(0), 0) - coerce(C(0), 0L) - coerce(C(0), 0.) - coerce(C(0), 0j) - coerce(0, C(0)) - coerce(0L, C(0)) - coerce(0., C(0)) - coerce(0j, C(0)) - def test_descrdoc(self): # Testing descriptor doc strings... + from _io import FileIO def check(descr, what): self.assertEqual(descr.__doc__, what) - check(file.closed, "True if the file is closed") # getset descriptor - check(file.name, "file name") # member descriptor + check(FileIO.closed, "True if the file is closed") # getset descriptor + check(complex.real, "the real part of a complex number") # member descriptor def test_doc_descriptor(self): # Testing __doc__ descriptor... @@ -3084,14 +2996,8 @@ order (MRO) for bases """ __slots__ = ["a", "b"] class H(object): __slots__ = ["b", "a"] - try: - unicode - except NameError: - class I(object): - __slots__ = ["a", "b"] - else: - class I(object): - __slots__ = [unicode("a"), unicode("b")] + class I(object): + __slots__ = ["a", "b"] class J(object): __slots__ = ["c", "b"] class K(object): @@ -3180,10 +3086,10 @@ order (MRO) for bases """ pass class Meta2(Base, type): pass - class D(object): - __metaclass__ = Meta1 - class E(object): - __metaclass__ = Meta2 + class D(object, metaclass=Meta1): + pass + class E(object, metaclass=Meta2): + pass for cls in C, D, E: verify_dict_readonly(cls) class_dict = cls.__dict__ @@ -3227,10 +3133,10 @@ order (MRO) for bases """ def test_pickles(self): # Testing pickling and copying new-style classes and objects... - import pickle, cPickle + import pickle def sorteditems(d): - L = d.items() + L = list(d.items()) L.sort() return L @@ -3282,36 +3188,35 @@ order (MRO) for bases """ class C4(C4classic, object): # mixed inheritance pass - for p in pickle, cPickle: - for bin in 0, 1: - for cls in C, C1, C2: - s = p.dumps(cls, bin) - cls2 = p.loads(s) - self.assertTrue(cls2 is cls) - - a = C1(1, 2); a.append(42); a.append(24) - b = C2("hello", "world", 42) - s = p.dumps((a, b), bin) - x, y = p.loads(s) - self.assertEqual(x.__class__, a.__class__) - self.assertEqual(sorteditems(x.__dict__), sorteditems(a.__dict__)) - self.assertEqual(y.__class__, b.__class__) - self.assertEqual(sorteditems(y.__dict__), sorteditems(b.__dict__)) - self.assertEqual(repr(x), repr(a)) - self.assertEqual(repr(y), repr(b)) - # Test for __getstate__ and __setstate__ on new style class - u = C3(42) - s = p.dumps(u, bin) - v = p.loads(s) - self.assertEqual(u.__class__, v.__class__) - self.assertEqual(u.foo, v.foo) - # Test for picklability of hybrid class - u = C4() - u.foo = 42 - s = p.dumps(u, bin) - v = p.loads(s) - self.assertEqual(u.__class__, v.__class__) - self.assertEqual(u.foo, v.foo) + for bin in 0, 1: + for cls in C, C1, C2: + s = pickle.dumps(cls, bin) + cls2 = pickle.loads(s) + self.assertTrue(cls2 is cls) + + a = C1(1, 2); a.append(42); a.append(24) + b = C2("hello", "world", 42) + s = pickle.dumps((a, b), bin) + x, y = pickle.loads(s) + self.assertEqual(x.__class__, a.__class__) + self.assertEqual(sorteditems(x.__dict__), sorteditems(a.__dict__)) + self.assertEqual(y.__class__, b.__class__) + self.assertEqual(sorteditems(y.__dict__), sorteditems(b.__dict__)) + self.assertEqual(repr(x), repr(a)) + self.assertEqual(repr(y), repr(b)) + # Test for __getstate__ and __setstate__ on new style class + u = C3(42) + s = pickle.dumps(u, bin) + v = pickle.loads(s) + self.assertEqual(u.__class__, v.__class__) + self.assertEqual(u.foo, v.foo) + # Test for picklability of hybrid class + u = C4() + u.foo = 42 + s = pickle.dumps(u, bin) + v = pickle.loads(s) + self.assertEqual(u.__class__, v.__class__) + self.assertEqual(u.foo, v.foo) # Testing copy.deepcopy() import copy @@ -3331,8 +3236,9 @@ order (MRO) for bases """ def test_pickle_slots(self): # Testing pickling of classes with __slots__ ... - import pickle, cPickle + import pickle # Pickling of classes with __slots__ but without __getstate__ should fail + # (if using protocol 0 or 1) global B, C, D, E class B(object): pass @@ -3342,29 +3248,17 @@ order (MRO) for bases """ class D(C): pass try: - pickle.dumps(C()) + pickle.dumps(C(), 0) except TypeError: pass else: self.fail("should fail: pickle C instance - %s" % base) try: - cPickle.dumps(C()) - except TypeError: - pass - else: - self.fail("should fail: cPickle C instance - %s" % base) - try: - pickle.dumps(C()) + pickle.dumps(C(), 0) except TypeError: pass else: self.fail("should fail: pickle D instance - %s" % base) - try: - cPickle.dumps(D()) - except TypeError: - pass - else: - self.fail("should fail: cPickle D instance - %s" % base) # Give C a nice generic __getstate__ and __setstate__ class C(base): __slots__ = ['a'] @@ -3381,7 +3275,7 @@ order (MRO) for bases """ pass return d def __setstate__(self, d): - for k, v in d.items(): + for k, v in list(d.items()): setattr(self, k, v) class D(C): pass @@ -3389,20 +3283,14 @@ order (MRO) for bases """ x = C() y = pickle.loads(pickle.dumps(x)) self.assertEqual(hasattr(y, 'a'), 0) - y = cPickle.loads(cPickle.dumps(x)) - self.assertEqual(hasattr(y, 'a'), 0) x.a = 42 y = pickle.loads(pickle.dumps(x)) self.assertEqual(y.a, 42) - y = cPickle.loads(cPickle.dumps(x)) - self.assertEqual(y.a, 42) x = D() x.a = 42 x.b = 100 y = pickle.loads(pickle.dumps(x)) self.assertEqual(y.a + y.b, 142) - y = cPickle.loads(cPickle.dumps(x)) - self.assertEqual(y.a + y.b, 142) # A subclass that adds a slot should also work class E(C): __slots__ = ['b'] @@ -3412,9 +3300,6 @@ order (MRO) for bases """ y = pickle.loads(pickle.dumps(x)) self.assertEqual(y.a, x.a) self.assertEqual(y.b, x.b) - y = cPickle.loads(cPickle.dumps(x)) - self.assertEqual(y.a, x.a) - self.assertEqual(y.b, x.b) def test_binary_operator_override(self): # Testing overrides of binary operations... @@ -3445,7 +3330,6 @@ order (MRO) for bases """ class S(str): def __eq__(self, other): return self.lower() == other.lower() - __hash__ = None # Silence Py3k warning def test_subclass_propagation(self): # Testing propagation of slot functions to subclasses... @@ -3497,7 +3381,7 @@ order (MRO) for bases """ def __getattr__(self, name): if name in ("spam", "foo", "bar"): return "hello" - raise AttributeError, name + raise AttributeError(name) B.__getattr__ = __getattr__ self.assertEqual(d.spam, "hello") self.assertEqual(d.foo, 24) @@ -3521,7 +3405,7 @@ order (MRO) for bases """ class B(A): pass del B - test_support.gc_collect() + support.gc_collect() A.__setitem__ = lambda *a: None # crash def test_buffer_inheritance(self): @@ -3530,22 +3414,14 @@ order (MRO) for bases """ import binascii # SF bug [#470040] ParseTuple t# vs subclasses. - class MyStr(str): + class MyBytes(bytes): pass - base = 'abc' - m = MyStr(base) + base = b'abc' + m = MyBytes(base) # b2a_hex uses the buffer interface to get its argument's value, via # PyArg_ParseTuple 't#' code. self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base)) - # It's not clear that unicode will continue to support the character - # buffer interface, and this test will fail if that's taken away. - class MyUni(unicode): - pass - base = u'abc' - m = MyUni(base) - self.assertEqual(binascii.b2a_hex(m), binascii.b2a_hex(base)) - class MyInt(int): pass m = MyInt(42) @@ -3558,11 +3434,11 @@ order (MRO) for bases """ def test_str_of_str_subclass(self): # Testing __str__ defined in subclass of str ... import binascii - import cStringIO + import io class octetstring(str): def __str__(self): - return binascii.b2a_hex(self) + return binascii.b2a_hex(self.encode('ascii')).decode("ascii") def __repr__(self): return self + " repr" @@ -3576,10 +3452,10 @@ order (MRO) for bases """ self.assertEqual(o.__str__(), '41') self.assertEqual(o.__repr__(), 'A repr') - capture = cStringIO.StringIO() + capture = io.StringIO() # Calling str() or not exercises different internal paths. - print >> capture, o - print >> capture, str(o) + print(o, file=capture) + print(str(o), file=capture) self.assertEqual(capture.getvalue(), '41\n41\n') capture.close() @@ -3613,7 +3489,7 @@ order (MRO) for bases """ c = C() self.assertEqual(log, []) del c - test_support.gc_collect() + support.gc_collect() self.assertEqual(log, [1]) class D(object): pass @@ -3728,10 +3604,10 @@ order (MRO) for bases """ d = D(None) self.assertEqual(d.foo, None) d = C(1) - self.assertEqual(isinstance(d, D), True) + self.assertIsInstance(d, D) self.assertEqual(d.foo, 1) d = D(1) - self.assertEqual(isinstance(d, D), True) + self.assertIsInstance(d, D) self.assertEqual(d.foo, 1) def test_imul_bug(self): @@ -3748,11 +3624,11 @@ order (MRO) for bases """ y *= 2 self.assertEqual(y, (x, 2)) y = x - y *= 3L - self.assertEqual(y, (x, 3L)) + y *= 3 + self.assertEqual(y, (x, 3)) y = x - y *= 1L<<100 - self.assertEqual(y, (x, 1L<<100)) + y *= 1<<100 + self.assertEqual(y, (x, 1<<100)) y = x y *= None self.assertEqual(y, (x, None)) @@ -3854,11 +3730,11 @@ order (MRO) for bases """ # which will cause c to get appended back to the container again # "during" the del. (On non-CPython implementations, however, __del__ # is typically not called again.) - test_support.gc_collect() + support.gc_collect() self.assertEqual(len(C.container), 1) del C.container[-1] - if test_support.check_impl_detail(): - test_support.gc_collect() + if support.check_impl_detail(): + support.gc_collect() self.assertEqual(len(C.container), 1) self.assertEqual(C.container[-1].attr, 42) @@ -3874,7 +3750,7 @@ order (MRO) for bases """ def __init__(self, x): self.x = x o = None - for i in xrange(50000): + for i in range(50000): o = trash(o) del o @@ -3886,7 +3762,7 @@ order (MRO) for bases """ pass class C(A,B) : __slots__=() - if test_support.check_impl_detail(): + if support.check_impl_detail(): self.assertEqual(C.__basicsize__, B.__basicsize__) self.assertTrue(hasattr(C, '__dict__')) self.assertTrue(hasattr(C, '__weakref__')) @@ -3952,7 +3828,7 @@ order (MRO) for bases """ try: D.__bases__ = () - except TypeError, msg: + except TypeError as msg: if str(msg) == "a new-style class can't have only classic bases": self.fail("wrong error message for .__bases__ = ()") else: @@ -3980,33 +3856,10 @@ order (MRO) for bases """ else: self.fail("shouldn't be able to create inheritance cycles") - # let's throw a classic class into the mix: - class Classic: - def meth2(self): - return 3 - - D.__bases__ = (C, Classic) - - self.assertEqual(d.meth2(), 3) - self.assertEqual(e.meth2(), 3) - try: - d.a - except AttributeError: - pass - else: - self.fail("attribute should have vanished") - - try: - D.__bases__ = (Classic,) - except TypeError: - pass - else: - self.fail("new-style class must have a new-style base") - def test_builtin_bases(self): # Make sure all the builtin types can have their base queried without # segfaulting. See issue #5787. - builtin_types = [tp for tp in __builtin__.__dict__.itervalues() + builtin_types = [tp for tp in builtins.__dict__.values() if isinstance(tp, type)] for tp in builtin_types: object.__getattribute__(tp, "__bases__") @@ -4052,7 +3905,7 @@ order (MRO) for bases """ return super(WorkOnce, self).__new__(WorkOnce, name, bases, ns) def mro(self): if self.flag > 0: - raise RuntimeError, "bozo" + raise RuntimeError("bozo") else: self.flag += 1 return type.mro(self) @@ -4077,11 +3930,11 @@ order (MRO) for bases """ class E(D): pass - class F(D): - __metaclass__ = WorkOnce + class F(D, metaclass=WorkOnce): + pass - class G(D): - __metaclass__ = WorkAlways + class G(D, metaclass=WorkAlways): + pass # Immediate subclasses have their mro's adjusted in alphabetical # order, so E's will get adjusted before adjusting F's fails. We @@ -4187,7 +4040,7 @@ order (MRO) for bases """ self.assertEqual(E() // C(), "C.__floordiv__") self.assertEqual(C() // E(), "C.__floordiv__") # This one would fail - @test_support.impl_detail("testing an internal kind of method object") + @support.impl_detail("testing an internal kind of method object") def test_meth_class_get(self): # Testing __get__ method of METH_CLASS C methods... # Full coverage of descrobject.c::classmethod_get() @@ -4330,7 +4183,7 @@ order (MRO) for bases """ def test_wrapper_segfault(self): # SF 927248: deeply nested wrappers could cause stack overflow f = lambda:None - for i in xrange(1000000): + for i in range(1000000): f = f.__call__ f = None @@ -4343,7 +4196,7 @@ order (MRO) for bases """ raise RuntimeError("Premature access to sys.stdout.%s" % attr) sys.stdout = StdoutGuard() try: - print "Oops!" + print("Oops!") except RuntimeError: pass finally: @@ -4375,7 +4228,7 @@ order (MRO) for bases """ self.assertEqual(c.attr, 1) # this makes a crash more likely: - test_support.gc_collect() + support.gc_collect() self.assertEqual(hasattr(c, 'attr'), False) def test_init(self): @@ -4394,6 +4247,8 @@ order (MRO) for bases """ # Testing method-wrapper objects... # <type 'method-wrapper'> did not support any reflection before 2.5 + # XXX should methods really support __eq__? + l = [] self.assertEqual(l.__add__, l.__add__) self.assertEqual(l.__add__, [].__add__) @@ -4431,63 +4286,47 @@ order (MRO) for bases """ def check(expr, x, y): try: - exec expr in {'x': x, 'y': y, 'operator': operator} + exec(expr, {'x': x, 'y': y, 'operator': operator}) except TypeError: pass else: self.fail("no TypeError from %r" % (expr,)) - N1 = sys.maxint + 1L # might trigger OverflowErrors instead of + N1 = sys.maxsize + 1 # might trigger OverflowErrors instead of # TypeErrors - N2 = sys.maxint # if sizeof(int) < sizeof(long), might trigger + N2 = sys.maxsize # if sizeof(int) < sizeof(long), might trigger # ValueErrors instead of TypeErrors - for metaclass in [type, types.ClassType]: - for name, expr, iexpr in [ - ('__add__', 'x + y', 'x += y'), - ('__sub__', 'x - y', 'x -= y'), - ('__mul__', 'x * y', 'x *= y'), - ('__truediv__', 'operator.truediv(x, y)', None), - ('__floordiv__', 'operator.floordiv(x, y)', None), - ('__div__', 'x / y', 'x /= y'), - ('__mod__', 'x % y', 'x %= y'), - ('__divmod__', 'divmod(x, y)', None), - ('__pow__', 'x ** y', 'x **= y'), - ('__lshift__', 'x << y', 'x <<= y'), - ('__rshift__', 'x >> y', 'x >>= y'), - ('__and__', 'x & y', 'x &= y'), - ('__or__', 'x | y', 'x |= y'), - ('__xor__', 'x ^ y', 'x ^= y'), - ('__coerce__', 'coerce(x, y)', None)]: - if name == '__coerce__': - rname = name - else: - rname = '__r' + name[2:] - A = metaclass('A', (), {name: specialmethod}) - B = metaclass('B', (), {rname: specialmethod}) - a = A() - b = B() - check(expr, a, a) - check(expr, a, b) - check(expr, b, a) - check(expr, b, b) - check(expr, a, N1) - check(expr, a, N2) - check(expr, N1, b) - check(expr, N2, b) - if iexpr: - check(iexpr, a, a) - check(iexpr, a, b) - check(iexpr, b, a) - check(iexpr, b, b) - check(iexpr, a, N1) - check(iexpr, a, N2) - iname = '__i' + name[2:] - C = metaclass('C', (), {iname: specialmethod}) - c = C() - check(iexpr, c, a) - check(iexpr, c, b) - check(iexpr, c, N1) - check(iexpr, c, N2) + for name, expr, iexpr in [ + ('__add__', 'x + y', 'x += y'), + ('__sub__', 'x - y', 'x -= y'), + ('__mul__', 'x * y', 'x *= y'), + ('__truediv__', 'operator.truediv(x, y)', None), + ('__floordiv__', 'operator.floordiv(x, y)', None), + ('__div__', 'x / y', 'x /= y'), + ('__mod__', 'x % y', 'x %= y'), + ('__divmod__', 'divmod(x, y)', None), + ('__pow__', 'x ** y', 'x **= y'), + ('__lshift__', 'x << y', 'x <<= y'), + ('__rshift__', 'x >> y', 'x >>= y'), + ('__and__', 'x & y', 'x &= y'), + ('__or__', 'x | y', 'x |= y'), + ('__xor__', 'x ^ y', 'x ^= y')]: + rname = '__r' + name[2:] + A = type('A', (), {name: specialmethod}) + a = A() + check(expr, a, a) + check(expr, a, N1) + check(expr, a, N2) + if iexpr: + check(iexpr, a, a) + check(iexpr, a, N1) + check(iexpr, a, N2) + iname = '__i' + name[2:] + C = type('C', (), {iname: specialmethod}) + c = C() + check(iexpr, c, a) + check(iexpr, c, N1) + check(iexpr, c, N2) def test_assign_slice(self): # ceval.c's assign_slice used to check for @@ -4495,7 +4334,7 @@ order (MRO) for bases """ # tp->tp_as_sequence->sq_ass_slice class C(object): - def __setslice__(self, start, stop, value): + def __setitem__(self, idx, value): self.value = value c = C() @@ -4585,11 +4424,11 @@ order (MRO) for bases """ del X.__abstractmethods__ def test_proxy_call(self): - class FakeStr(object): + class FakeStr: __class__ = str fake_str = FakeStr() - # isinstance() reads __class__ on new style classes + # isinstance() reads __class__ self.assertTrue(isinstance(fake_str, str)) # call a method descriptor @@ -4603,7 +4442,7 @@ order (MRO) for bases """ def test_repr_as_str(self): # Issue #11603: crash or infinite loop when rebinding __str__ as # __repr__. - class Foo(object): + class Foo: pass Foo.__repr__ = Foo.__str__ foo = Foo() @@ -4627,7 +4466,7 @@ order (MRO) for bases """ x.attr = 42 wr = weakref.ref(x) del x - test_support.gc_collect() + support.gc_collect() self.assertIsNone(wr()) for o in gc.get_objects(): self.assertIsNot(type(o), X) @@ -4639,40 +4478,57 @@ class DictProxyTests(unittest.TestCase): pass self.C = C - def test_repr(self): - self.assertIn('dict_proxy({', repr(vars(self.C))) - self.assertIn("'meth':", repr(vars(self.C))) - def test_iter_keys(self): - # Testing dict-proxy iterkeys... - keys = [ key for key in self.C.__dict__.iterkeys() ] + # Testing dict-proxy keys... + it = self.C.__dict__.keys() + self.assertNotIsInstance(it, list) + keys = list(it) keys.sort() self.assertEqual(keys, ['__dict__', '__doc__', '__module__', '__weakref__', 'meth']) def test_iter_values(self): - # Testing dict-proxy itervalues... - values = [ values for values in self.C.__dict__.itervalues() ] + # Testing dict-proxy values... + it = self.C.__dict__.values() + self.assertNotIsInstance(it, list) + values = list(it) self.assertEqual(len(values), 5) def test_iter_items(self): # Testing dict-proxy iteritems... - keys = [ key for (key, value) in self.C.__dict__.iteritems() ] + it = self.C.__dict__.items() + self.assertNotIsInstance(it, list) + keys = [item[0] for item in it] keys.sort() self.assertEqual(keys, ['__dict__', '__doc__', '__module__', '__weakref__', 'meth']) def test_dict_type_with_metaclass(self): - # Testing type of __dict__ when __metaclass__ set... + # Testing type of __dict__ when metaclass set... class B(object): pass class M(type): pass - class C: + class C(metaclass=M): # In 2.3a1, C.__dict__ was a real dict rather than a dict proxy - __metaclass__ = M + pass self.assertEqual(type(C.__dict__), type(B.__dict__)) + def test_repr(self): + # Testing dict_proxy.__repr__ + def sorted_dict_repr(repr_): + # Given the repr of a dict, sort the keys + assert repr_.startswith('{') + assert repr_.endswith('}') + kvs = repr_[1:-1].split(', ') + return '{' + ', '.join(sorted(kvs)) + '}' + dict_ = {k: v for k, v in self.C.__dict__.items()} + repr_ = repr(self.C.__dict__) + self.assertTrue(repr_.startswith('dict_proxy(')) + self.assertTrue(repr_.endswith(')')) + self.assertEqual(sorted_dict_repr(repr_[len('dict_proxy('):-len(')')]), + sorted_dict_repr('{!r}'.format(dict_))) + class PTypesLongInitTest(unittest.TestCase): # This is in its own TestCase so that it can be run before any other tests. @@ -4687,7 +4543,7 @@ class PTypesLongInitTest(unittest.TestCase): def __pow__(self, *args): pass try: - pow(0L, UserLong(), 0L) + pow(0, UserLong(), 0) except: pass @@ -4697,17 +4553,9 @@ class PTypesLongInitTest(unittest.TestCase): def test_main(): - deprecations = [(r'complex divmod\(\), // and % are deprecated$', - DeprecationWarning)] - if sys.py3kwarning: - deprecations += [ - ("classic (int|long) division", DeprecationWarning), - ("coerce.. not supported", DeprecationWarning), - (".+__(get|set|del)slice__ has been removed", DeprecationWarning)] - with test_support.check_warnings(*deprecations): - # Run all local test cases, with PTypesLongInitTest first. - test_support.run_unittest(PTypesLongInitTest, OperatorsTest, - ClassPropertiesAndMethods, DictProxyTests) + # Run all local test cases, with PTypesLongInitTest first. + support.run_unittest(PTypesLongInitTest, OperatorsTest, + ClassPropertiesAndMethods, DictProxyTests) if __name__ == "__main__": test_main() |