[Python-checkins] CVS: python/dist/src/Lib/test test_operations.py,1.4,1.5

Tim Peters tim_one@users.sourceforge.net
Tue, 29 May 2001 14:14:34 -0700


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

Modified Files:
	test_operations.py 
Log Message:
BadDictKey test:  The output file expected "raising error" to be printed
exactly once.  But the test code can't know that, as the number of times
__cmp__ is called depends on internal details of the dict implementation.
This is especially nasty because the __hash__ method returns the address
of the class object, so the hash codes seen by the dict can vary across
runs, causing the dict to use a different probe order across runs.  I
just happened to see this test fail about 1 run in 7 today, but only
under a release build and when passing -O to Python.  So, changed the test
to be predictable across runs.


Index: test_operations.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_operations.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** test_operations.py	2001/05/23 23:33:57	1.4
--- test_operations.py	2001/05/29 21:14:32	1.5
***************
*** 12,15 ****
--- 12,17 ----
  
  class BadDictKey:
+     already_printed_raising_error = 0
+ 
      def __hash__(self):
          return hash(self.__class__)
***************
*** 17,21 ****
      def __cmp__(self, other):
          if isinstance(other, self.__class__):
!             print "raising error"
              raise RuntimeError, "gotcha"
          return other
--- 19,30 ----
      def __cmp__(self, other):
          if isinstance(other, self.__class__):
!             if not BadDictKey.already_printed_raising_error:
!                 # How many times __cmp__ gets called depends on the hash
!                 # code and the internals of the dict implementation; we
!                 # know it will be called at least once, but that's it.
!                 # already_printed_raising_error makes sure the expected-
!                 # output file prints the msg at most once.
!                 BadDictKey.already_printed_raising_error = 1
!                 print "raising error"
              raise RuntimeError, "gotcha"
          return other