]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
BadDictKey test: The output file expected "raising error" to be printed
authorTim Peters <tim.peters@gmail.com>
Tue, 29 May 2001 21:14:32 +0000 (21:14 +0000)
committerTim Peters <tim.peters@gmail.com>
Tue, 29 May 2001 21:14:32 +0000 (21:14 +0000)
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.

Lib/test/test_operations.py

index 3a9a37934c3fa47fdc555e5408a23c21f57622c4..b599c9da9e3fb135d1167c9fb1d8a8f0ff614cbf 100644 (file)
@@ -11,12 +11,21 @@ print '3.1 Dictionary lookups succeed even if __cmp__() raises an exception'
 # http://sourceforge.net/bugs/?func=detailbug&bug_id=112558&group_id=5470
 
 class BadDictKey:
+    already_printed_raising_error = 0
+
     def __hash__(self):
         return hash(self.__class__)
 
     def __cmp__(self, other):
         if isinstance(other, self.__class__):
-            print "raising error"
+            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