[Python-checkins] python/dist/src/Lib/test test_sort.py,1.2,1.3 test_types.py,1.38,1.39

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Tue, 12 Nov 2002 14:08:11 -0800


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

Modified Files:
	test_sort.py test_types.py 
Log Message:
SF patch 637176: list.sort crasher

Armin Rigo's Draconian but effective fix for

SF bug 453523: list.sort crasher

slightly fiddled to catch more cases of list mutation.  The dreaded
internal "immutable list type" is gone!  OTOH, if you look at a list
*while* it's being sorted now, it will appear to be empty.  Better
than a core dump.


Index: test_sort.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_sort.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** test_sort.py	3 Aug 2002 02:17:41 -0000	1.2
--- test_sort.py	12 Nov 2002 22:08:08 -0000	1.3
***************
*** 117,120 ****
--- 117,149 ----
      check("stability", x, s)
  
+ def bug453523():
+     global nerrors
+     from random import random
+ 
+     # If this fails, the most likely outcome is a core dump.
+     if verbose:
+         print "Testing bug 453523 -- list.sort() crasher."
+ 
+     class C:
+         def __lt__(self, other):
+             if L and random() < 0.75:
+                 pop()
+             else:
+                 push(3)
+             return random() < 0.5
+ 
+     L = [C() for i in range(50)]
+     pop = L.pop
+     push = L.append
+     try:
+         L.sort()
+     except ValueError:
+         pass
+     else:
+         print "    Mutation during list.sort() wasn't caught."
+         nerrors += 1
+ 
+ bug453523()
+ 
  if nerrors:
      print "Test failed", nerrors

Index: test_types.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_types.py,v
retrieving revision 1.38
retrieving revision 1.39
diff -C2 -d -r1.38 -r1.39
*** test_types.py	5 Nov 2002 17:38:05 -0000	1.38
--- test_types.py	12 Nov 2002 22:08:08 -0000	1.39
***************
*** 368,375 ****
  
  def selfmodifyingComparison(x,y):
!     z[0] = 1
      return cmp(x, y)
  try: z.sort(selfmodifyingComparison)
! except TypeError: pass
  else: raise TestFailed, 'modifying list during sort'
  
--- 368,375 ----
  
  def selfmodifyingComparison(x,y):
!     z.append(1)
      return cmp(x, y)
  try: z.sort(selfmodifyingComparison)
! except ValueError: pass
  else: raise TestFailed, 'modifying list during sort'