summaryrefslogtreecommitdiff
path: root/Lib/test/test_descr.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_descr.py')
-rw-r--r--Lib/test/test_descr.py131
1 files changed, 130 insertions, 1 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index eec4e40f5b..aba1c74701 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -773,6 +773,22 @@ def metaclass():
except TypeError: pass
else: raise TestFailed, "calling object w/o call method should raise TypeError"
+ # Testing code to find most derived baseclass
+ class A(type):
+ def __new__(*args, **kwargs):
+ return type.__new__(*args, **kwargs)
+
+ class B(object):
+ pass
+
+ class C(object, metaclass=A):
+ pass
+
+ # The most derived metaclass of D is A rather than type.
+ class D(B, C):
+ pass
+
+
def pymods():
if verbose: print("Testing Python subclass of module...")
log = []
@@ -1072,6 +1088,45 @@ def slots():
raise TestFailed, "[''] slots not caught"
class C(object):
__slots__ = ["a", "a_b", "_a", "A0123456789Z"]
+ # XXX(nnorwitz): was there supposed to be something tested
+ # from the class above?
+
+ # Test a single string is not expanded as a sequence.
+ class C(object):
+ __slots__ = "abc"
+ c = C()
+ c.abc = 5
+ vereq(c.abc, 5)
+
+ # Test unicode slot names
+ try:
+ unicode
+ except NameError:
+ pass
+ else:
+ # Test a single unicode string is not expanded as a sequence.
+ class C(object):
+ __slots__ = unicode("abc")
+ c = C()
+ c.abc = 5
+ vereq(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
+ vereq(x.foo, 5)
+ veris(type(slots[0]), unicode)
+ # this used to leak references
+ try:
+ class C(object):
+ __slots__ = [unichr(128)]
+ except (TypeError, UnicodeEncodeError):
+ pass
+ else:
+ raise TestFailed, "[unichr(128)] slots not caught"
# Test leaks
class Counted(object):
@@ -1318,6 +1373,22 @@ def errors():
else:
verify(0, "__slots__ = [1] should be illegal")
+ class M1(type):
+ pass
+ class M2(type):
+ pass
+ class A1(object, metaclass=M1):
+ pass
+ class A2(object, metaclass=M2):
+ pass
+ try:
+ class B(A1, A2):
+ pass
+ except TypeError:
+ pass
+ else:
+ verify(0, "finding the most derived metaclass should have failed")
+
def classmethods():
if verbose: print("Testing class methods...")
class C(object):
@@ -2092,7 +2163,6 @@ def inherits():
__slots__ = ['prec']
def __init__(self, value=0.0, prec=12):
self.prec = int(prec)
- float.__init__(self, value)
def __repr__(self):
return "%.*g" % (self.prec, self)
vereq(repr(precfloat(1.1)), "1.1")
@@ -2644,6 +2714,51 @@ def setclass():
cant(o, type(1))
cant(o, type(None))
del o
+ class G(object):
+ __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 J(object):
+ __slots__ = ["c", "b"]
+ class K(object):
+ __slots__ = ["a", "b", "d"]
+ class L(H):
+ __slots__ = ["e"]
+ class M(I):
+ __slots__ = ["e"]
+ class N(J):
+ __slots__ = ["__weakref__"]
+ class P(J):
+ __slots__ = ["__dict__"]
+ class Q(J):
+ pass
+ class R(J):
+ __slots__ = ["__dict__", "__weakref__"]
+
+ for cls, cls2 in ((G, H), (G, I), (I, H), (Q, R), (R, Q)):
+ x = cls()
+ x.a = 1
+ x.__class__ = cls2
+ verify(x.__class__ is cls2,
+ "assigning %r as __class__ for %r silently failed" % (cls2, x))
+ vereq(x.a, 1)
+ x.__class__ = cls
+ verify(x.__class__ is cls,
+ "assigning %r as __class__ for %r silently failed" % (cls, x))
+ vereq(x.a, 1)
+ for cls in G, J, K, L, M, N, P, R, list, Int:
+ for cls2 in G, J, K, L, M, N, P, R, list, Int:
+ if cls is cls2:
+ continue
+ cant(cls(), cls2)
def setdict():
if verbose: print("Testing __dict__ assignment...")
@@ -3999,6 +4114,19 @@ def notimplemented():
check(iexpr, c, N1)
check(iexpr, c, N2)
+def test_assign_slice():
+ # ceval.c's assign_slice used to check for
+ # tp->tp_as_sequence->sq_slice instead of
+ # tp->tp_as_sequence->sq_ass_slice
+
+ class C(object):
+ def __setslice__(self, start, stop, value):
+ self.value = value
+
+ c = C()
+ c[1:2] = 3
+ vereq(c.value, 3)
+
def test_main():
weakref_segfault() # Must be first, somehow
wrapper_segfault()
@@ -4094,6 +4222,7 @@ def test_main():
test_init()
methodwrapper()
notimplemented()
+ test_assign_slice()
if verbose: print("All OK")