Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_enumerate.TestReversed Class Reference

List of all members.

Public Member Functions

def test_simple
def test_range_optimization
def test_len
def test_gc
def test_args
def test_bug1229429
def test_objmethods

Public Attributes

 called

Detailed Description

Definition at line 129 of file test_enumerate.py.


Member Function Documentation

Definition at line 181 of file test_enumerate.py.

00181 
00182     def test_args(self):
00183         self.assertRaises(TypeError, reversed)
00184         self.assertRaises(TypeError, reversed, [], 'extra')

Here is the call graph for this function:

Definition at line 185 of file test_enumerate.py.

00185 
00186     def test_bug1229429(self):
00187         # this bug was never in reversed, it was in
00188         # PyObject_CallMethod, and reversed_new calls that sometimes.
00189         if not hasattr(sys, "getrefcount"):
00190             return
00191         def f():
00192             pass
00193         r = f.__reversed__ = object()
00194         rc = sys.getrefcount(r)
00195         for i in range(10):
00196             try:
00197                 reversed(f)
00198             except TypeError:
00199                 pass
00200             else:
00201                 self.fail("non-callable __reversed__ didn't raise!")
00202         self.assertEqual(rc, sys.getrefcount(r))

Definition at line 171 of file test_enumerate.py.

00171 
00172     def test_gc(self):
00173         class Seq:
00174             def __len__(self):
00175                 return 10
00176             def __getitem__(self, index):
00177                 return index
00178         s = Seq()
00179         r = reversed(s)
00180         s.r = r

Here is the call graph for this function:

Definition at line 150 of file test_enumerate.py.

00150 
00151     def test_len(self):
00152         # This is an implementation detail, not an interface requirement
00153         from test.test_iterlen import len
00154         for s in ('hello', tuple('hello'), list('hello'), range(5)):
00155             self.assertEqual(len(reversed(s)), len(s))
00156             r = reversed(s)
00157             list(r)
00158             self.assertEqual(len(r), 0)
00159         class SeqWithWeirdLen:
00160             called = False
00161             def __len__(self):
00162                 if not self.called:
00163                     self.called = True
00164                     return 10
00165                 raise ZeroDivisionError
00166             def __getitem__(self, index):
00167                 return index
00168         r = reversed(SeqWithWeirdLen())
00169         self.assertRaises(ZeroDivisionError, len, r)
00170 

Here is the call graph for this function:

Definition at line 203 of file test_enumerate.py.

00203 
00204     def test_objmethods(self):
00205         # Objects must have __len__() and __getitem__() implemented.
00206         class NoLen(object):
00207             def __getitem__(self): return 1
00208         nl = NoLen()
00209         self.assertRaises(TypeError, reversed, nl)
00210 
00211         class NoGetItem(object):
00212             def __len__(self): return 2
00213         ngi = NoGetItem()
00214         self.assertRaises(TypeError, reversed, ngi)
00215 

Here is the call graph for this function:

Definition at line 145 of file test_enumerate.py.

00145 
00146     def test_range_optimization(self):
00147         x = range(1)
00148         self.assertEqual(type(reversed(x)), type(iter(x)))

Here is the call graph for this function:

Definition at line 131 of file test_enumerate.py.

00131 
00132     def test_simple(self):
00133         class A:
00134             def __getitem__(self, i):
00135                 if i < 5:
00136                     return str(i)
00137                 raise StopIteration
00138             def __len__(self):
00139                 return 5
00140         for data in 'abc', range(5), tuple(enumerate('abc')), A(), range(1,17,5):
00141             self.assertEqual(list(data)[::-1], list(reversed(data)))
00142         self.assertRaises(TypeError, reversed, {})
00143         # don't allow keyword arguments
00144         self.assertRaises(TypeError, reversed, [], a=1)

Here is the call graph for this function:


Member Data Documentation

Definition at line 162 of file test_enumerate.py.


The documentation for this class was generated from the following file: