summaryrefslogtreecommitdiff
path: root/Lib/test/test_descr.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-05-01 23:31:36 +0200
committerVictor Stinner <victor.stinner@haypocalc.com>2011-05-01 23:31:36 +0200
commit39469e8aecc257c113ce60324aa3ffd39339b361 (patch)
treef74727c85de819c2eadb6f91f6f35f2a91b2b107 /Lib/test/test_descr.py
parent0066c0d3e7ff44fbbc368c39b5b2eaf722b8deed (diff)
parentc4ea7d786ce307bde1c7591e853d0c73c2ee4747 (diff)
downloadcpython-39469e8aecc257c113ce60324aa3ffd39339b361.tar.gz
(Merge 3.1) Issue #9756: When calling a method descriptor or a slot wrapper
descriptor, the check of the object type doesn't read the __class__ attribute anymore. Fix a crash if a class override its __class__ attribute (e.g. a proxy of the str type).
Diffstat (limited to 'Lib/test/test_descr.py')
-rw-r--r--Lib/test/test_descr.py152
1 files changed, 79 insertions, 73 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index aafe428bd0..6638116394 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1,8 +1,8 @@
import builtins
import sys
import types
+import math
import unittest
-import warnings
from copy import deepcopy
from test import support
@@ -248,10 +248,6 @@ class OperatorsTest(unittest.TestCase):
else:
self.fail("NotImplemented should have caused TypeError")
- def test_longs(self):
- # Testing long operations...
- self.number_operators(100, 3)
-
def test_floats(self):
# Testing float operations...
self.number_operators(100.0, 3.0)
@@ -259,7 +255,7 @@ 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):
@@ -393,11 +389,11 @@ class ClassPropertiesAndMethods(unittest.TestCase):
def test_python_dicts(self):
# Testing Python subclass of dict...
self.assertTrue(issubclass(dict, dict))
- self.assertTrue(isinstance({}, dict))
+ self.assertIsInstance({}, dict)
d = dict()
self.assertEqual(d, {})
self.assertTrue(d.__class__ is dict)
- self.assertTrue(isinstance(d, dict))
+ self.assertIsInstance(d, dict)
class C(dict):
state = -1
def __init__(self_local, *a, **kw):
@@ -410,7 +406,7 @@ class ClassPropertiesAndMethods(unittest.TestCase):
def __getitem__(self, key):
return self.get(key, 0)
def __setitem__(self_local, key, value):
- self.assertTrue(isinstance(key, type(0)))
+ self.assertIsInstance(key, type(0))
dict.__setitem__(self_local, key, value)
def setstate(self, state):
self.state = state
@@ -509,7 +505,7 @@ class ClassPropertiesAndMethods(unittest.TestCase):
return 42
self.assertEqual(C.name, 'C')
self.assertEqual(C.bases, ())
- self.assertTrue('spam' in C.dict)
+ self.assertIn('spam', C.dict)
c = C()
self.assertEqual(c.spam(), 42)
@@ -633,7 +629,6 @@ class ClassPropertiesAndMethods(unittest.TestCase):
def test_module_subclasses(self):
# Testing Python subclass of module...
log = []
- import types, sys
MT = type(sys)
class MM(MT):
def __init__(self, name):
@@ -1015,7 +1010,6 @@ order (MRO) for bases """
# Test cyclical leaks [SF bug 519621]
class F(object):
__slots__ = ['a', 'b']
- log = []
s = F()
s.a = [Counted(), s]
self.assertEqual(Counted.counter, 1)
@@ -1024,11 +1018,11 @@ order (MRO) for bases """
self.assertEqual(Counted.counter, 0)
# Test lookup leaks [SF bug 572567]
- import sys,gc
+ import gc
if hasattr(gc, 'get_objects'):
class G(object):
- def __cmp__(self, other):
- return 0
+ def __eq__(self, other):
+ return False
g = G()
orig_objects = len(gc.get_objects())
for i in range(10):
@@ -1104,7 +1098,7 @@ order (MRO) for bases """
MyABC.register(Unrelated)
u = Unrelated()
- self.assertTrue(isinstance(u, MyABC))
+ self.assertIsInstance(u, MyABC)
# This used to crash
self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
@@ -1172,15 +1166,6 @@ 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(int):
- pass
- self.assertEqual("a"*L(2), "aa")
- self.assertEqual(L(2)*"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
@@ -1560,6 +1545,8 @@ order (MRO) for bases """
return []
def zero(self):
return 0
+ def complex_num(self):
+ return 1j
def stop(self):
raise StopIteration
def return_true(self, thing=None):
@@ -1575,6 +1562,9 @@ 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"
# It would be nice to have every special method tested here, but I'm
# only listing the ones I can remember outside of typeobject.c, since it
@@ -1590,11 +1580,13 @@ order (MRO) for bases """
set(("__class__",)), {}),
("__subclasscheck__", do_issubclass, return_true,
set(("__bases__",)), {}),
- # These two fail because the compiler generates LOAD_ATTR to look
- # them up. We'd have to add a new opcode to fix this, and it's
- # probably not worth it.
- # ("__enter__", run_context, iden),
- # ("__exit__", run_context, iden),
+ ("__enter__", run_context, iden, set(), {"__exit__" : swallow}),
+ ("__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(), {}),
]
class Checker(object):
@@ -1668,10 +1660,10 @@ order (MRO) for bases """
# depending on whether this test is run standalone or from a framework.
self.assertTrue(str(c1).find('C object at ') >= 0)
self.assertEqual(str(c1), repr(c1))
- self.assertTrue(-1 not in c1)
+ self.assertNotIn(-1, c1)
for i in range(10):
- self.assertTrue(i in c1)
- self.assertFalse(10 in c1)
+ self.assertIn(i, c1)
+ self.assertNotIn(10, c1)
# Test the default behavior for dynamic classes
class D(object):
def __getitem__(self, i):
@@ -1691,10 +1683,10 @@ order (MRO) for bases """
# depending on whether this test is run standalone or from a framework.
self.assertTrue(str(d1).find('D object at ') >= 0)
self.assertEqual(str(d1), repr(d1))
- self.assertTrue(-1 not in d1)
+ self.assertNotIn(-1, d1)
for i in range(10):
- self.assertTrue(i in d1)
- self.assertFalse(10 in d1)
+ self.assertIn(i, d1)
+ self.assertNotIn(10, d1)
# Test overridden behavior
class Proxy(object):
def __init__(self, x):
@@ -1738,10 +1730,10 @@ order (MRO) for bases """
self.assertEqual(str(p0), "Proxy:0")
self.assertEqual(repr(p0), "Proxy(0)")
p10 = Proxy(range(10))
- self.assertFalse(-1 in p10)
+ self.assertNotIn(-1, p10)
for i in range(10):
- self.assertTrue(i in p10)
- self.assertFalse(10 in p10)
+ self.assertIn(i, p10)
+ self.assertNotIn(10, p10)
def test_weakrefs(self):
# Testing weak references...
@@ -1798,13 +1790,13 @@ order (MRO) for bases """
self.assertFalse(hasattr(a, "x"))
raw = C.__dict__['x']
- self.assertTrue(isinstance(raw, property))
+ self.assertIsInstance(raw, property)
attrs = dir(raw)
- self.assertTrue("__doc__" in attrs)
- self.assertTrue("fget" in attrs)
- self.assertTrue("fset" in attrs)
- self.assertTrue("fdel" in attrs)
+ self.assertIn("__doc__", attrs)
+ self.assertIn("fget", attrs)
+ self.assertIn("fset", attrs)
+ self.assertIn("fdel", attrs)
self.assertEqual(raw.__doc__, "I'm the x property.")
self.assertTrue(raw.fget is C.__dict__['getx'])
@@ -1834,6 +1826,9 @@ order (MRO) for bases """
else:
self.fail("expected ZeroDivisionError from bad property")
+ @unittest.skipIf(sys.flags.optimize >= 2,
+ "Docstrings are omitted with -O2 and above")
+ def test_properties_doc_attrib(self):
class E(object):
def getter(self):
"getter method"
@@ -1846,6 +1841,7 @@ order (MRO) for bases """
prop2 = property(fset=setter)
self.assertEqual(prop2.__doc__, None)
+ def test_testcapi_no_segfault(self):
# this segfaulted in 2.5b2
try:
import _testcapi
@@ -2029,12 +2025,12 @@ order (MRO) for bases """
c = C()
self.assertEqual(interesting(dir(c)), cstuff)
- ## self.assertTrue('__self__' in 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.assertTrue('__self__' in dir(c.Cmethod))
+ ## self.assertIn('__self__', dir(c.Cmethod))
class A(C):
Adata = 1
@@ -2042,16 +2038,15 @@ order (MRO) for bases """
astuff = ['Adata', 'Amethod'] + cstuff
self.assertEqual(interesting(dir(A)), astuff)
- ## self.assertTrue('__self__' in 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.assertTrue('__self__' in dir(a.Amethod))
+ ## self.assertIn('__self__', dir(a.Amethod))
# Try a module subclass.
- import sys
class M(type(sys)):
pass
minstance = M("m")
@@ -2243,10 +2238,7 @@ order (MRO) for bases """
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__
@@ -2603,7 +2595,7 @@ order (MRO) for bases """
self.assertEqual(d[cistr('one')], 1)
self.assertEqual(d[cistr('tWo')], 2)
self.assertEqual(d[cistr('THrEE')], 3)
- self.assertTrue(cistr('ONe') in d)
+ self.assertIn(cistr('ONe'), d)
self.assertEqual(d.get(cistr('thrEE')), 3)
def test_classic_comparisons(self):
@@ -3222,7 +3214,6 @@ order (MRO) for bases """
self.fail("d.foo should be undefined now")
# Test a nasty bug in recurse_down_subclasses()
- import gc
class A(object):
pass
class B(A):
@@ -3427,10 +3418,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):
@@ -3922,29 +3913,29 @@ order (MRO) for bases """
pass
a = C()
pa = Proxy(a)
- self.assertTrue(isinstance(a, C)) # Baseline
- self.assertTrue(isinstance(pa, C)) # Test
+ self.assertIsInstance(a, C) # Baseline
+ self.assertIsInstance(pa, C) # Test
# Test with a classic subclass
class D(C):
pass
a = D()
pa = Proxy(a)
- self.assertTrue(isinstance(a, C)) # Baseline
- self.assertTrue(isinstance(pa, C)) # Test
+ self.assertIsInstance(a, C) # Baseline
+ self.assertIsInstance(pa, C) # Test
# Test with a new-style class
class C(object):
pass
a = C()
pa = Proxy(a)
- self.assertTrue(isinstance(a, C)) # Baseline
- self.assertTrue(isinstance(pa, C)) # Test
+ self.assertIsInstance(a, C) # Baseline
+ self.assertIsInstance(pa, C) # Test
# Test with a new-style subclass
class D(C):
pass
a = D()
pa = Proxy(a)
- self.assertTrue(isinstance(a, C)) # Baseline
- self.assertTrue(isinstance(pa, C)) # Test
+ self.assertIsInstance(a, C) # Baseline
+ self.assertIsInstance(pa, C) # Test
def test_proxy_super(self):
# Testing super() for a proxy object...
@@ -4012,7 +4003,7 @@ order (MRO) for bases """
def test_file_fault(self):
# Testing sys.stdout is changed in getattr...
- import sys
+ test_stdout = sys.stdout
class StdoutGuard:
def __getattr__(self, attr):
sys.stdout = sys.__stdout__
@@ -4022,6 +4013,8 @@ order (MRO) for bases """
print("Oops!")
except RuntimeError:
pass
+ finally:
+ sys.stdout = test_stdout
def test_vicious_descriptor_nonsense(self):
# Testing vicious_descriptor_nonsense...
@@ -4100,8 +4093,6 @@ order (MRO) for bases """
def test_not_implemented(self):
# Testing NotImplemented...
# all binary methods should be able to return a NotImplemented
- import sys
- import types
import operator
def specialmethod(self, other):
@@ -4184,6 +4175,15 @@ order (MRO) for bases """
x.a = 42
self.assertEqual(x.a, 42)
+ # Also check type_getattro for correctness.
+ class Meta(type):
+ pass
+ class X(object):
+ __metaclass__ = Meta
+ X.a = 42
+ Meta.a = Descr("a")
+ self.assertEqual(X.a, 42)
+
def test_getattr_hooks(self):
# issue 4230
@@ -4260,20 +4260,26 @@ class DictProxyTests(unittest.TestCase):
self.C = C
def test_iter_keys(self):
- # Testing dict-proxy iterkeys...
- keys = [ key for key in self.C.__dict__.keys() ]
+ # 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__.values() ]
+ # 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__.items() ]
+ 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'])