[Python-checkins] python/dist/src/Lib/test test_sort.py,1.8,1.9

mwh at users.sourceforge.net mwh at users.sourceforge.net
Thu Dec 4 06:25:48 EST 2003


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv21077/Lib/test

Modified Files:
	test_sort.py 
Log Message:
Fixes and tests for various "holding pointers when arbitrary Python code
can run" bugs as discussed in

[ 848856 ] couple of new list.sort bugs



Index: test_sort.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_sort.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** test_sort.py	28 Nov 2003 21:43:02 -0000	1.8
--- test_sort.py	4 Dec 2003 11:25:45 -0000	1.9
***************
*** 194,197 ****
--- 194,242 ----
          self.assertEqual(data, dup)
  
+     def test_key_with_mutation(self):
+         data = range(10)
+         def k(x):
+             del data[:]
+             data[:] = range(20)
+             return x
+         self.assertRaises(ValueError, data.sort, key=k)
+ 
+     def test_key_with_mutating_del(self):
+         data = range(10)
+         class SortKiller(object):
+             def __init__(self, x):
+                 pass
+             def __del__(self):
+                 del data[:]
+                 data[:] = range(20)
+         self.assertRaises(ValueError, data.sort, key=SortKiller)
+ 
+     def test_key_with_mutating_del_and_exception(self):
+         data = range(10)
+         ## dup = data[:]
+         class SortKiller(object):
+             def __init__(self, x):
+                 if x > 2:
+                     raise RuntimeError
+             def __del__(self):
+                 del data[:]
+                 data[:] = range(20)
+         self.assertRaises(RuntimeError, data.sort, key=SortKiller)
+         ## major honking subtlety: we *can't* do:
+         ##
+         ## self.assertEqual(data, dup)
+         ##
+         ## because there is a reference to a SortKiller in the
+         ## traceback and by the time it dies we're outside the call to
+         ## .sort() and so the list protection gimmicks are out of
+         ## date (this cost some brain cells to figure out...).
+ 
+     def test_key_with_exception(self):
+         # Verify that the wrapper has been removed
+         data = range(-2,2)
+         dup = data[:]
+         self.assertRaises(ZeroDivisionError, data.sort, None, lambda x: 1/x)
+         self.assertEqual(data, dup)
+ 
      def test_reverse(self):
          data = range(100)





More information about the Python-checkins mailing list