[Python-checkins] r46705 - python/trunk/Lib/test/test_exceptions.py

tim.peters python-checkins at python.org
Wed Jun 7 08:57:52 CEST 2006


Author: tim.peters
Date: Wed Jun  7 08:57:51 2006
New Revision: 46705

Modified:
   python/trunk/Lib/test/test_exceptions.py
Log:
SF patch 1501987:  Remove randomness from test_exceptions,
from ?iga Seilnacht (sorry about the name, but Firefox
on my box can't display the first character of the name --
the SF "Unix name" is zseil).

This appears to cure the oddball intermittent leaks across
runs when running test_exceptions under -R.  I'm not sure
why, but I'm too sleepy to care ;-)

The thrust of the SF patch was to remove randomness in the
pickle protocol used.  I changed the patch to use
range(pickle.HIGHEST_PROTOCOL + 1), to try both pickle and
cPickle, and randomly mucked with other test lines to put
statements on their own lines.

Not a bugfix candidate (this is fiddling new-in-2.5 code).


Modified: python/trunk/Lib/test/test_exceptions.py
==============================================================================
--- python/trunk/Lib/test/test_exceptions.py	(original)
+++ python/trunk/Lib/test/test_exceptions.py	Wed Jun  7 08:57:51 2006
@@ -1,9 +1,12 @@
 # Python test set -- part 5, built-in exceptions
 
-from test.test_support import TESTFN, unlink, run_unittest
-import warnings
-import sys, traceback, os
+import os
+import sys
 import unittest
+import warnings
+import pickle, cPickle
+
+from test.test_support import TESTFN, unlink, run_unittest
 
 # XXX This is not really enough, each *operation* should be tested!
 
@@ -182,11 +185,15 @@
 
     def testAttributes(self):
         # test that exception attributes are happy
-        try: str(u'Hello \u00E1')
-        except Exception, e: sampleUnicodeEncodeError = e
+        try:
+            str(u'Hello \u00E1')
+        except Exception, e:
+            sampleUnicodeEncodeError = e
 
-        try: unicode('\xff')
-        except Exception, e: sampleUnicodeDecodeError = e
+        try:
+            unicode('\xff')
+        except Exception, e:
+            sampleUnicodeDecodeError = e
 
         exceptionList = [
             (BaseException, (), {'message' : '', 'args' : ()}),
@@ -251,19 +258,20 @@
                      'strerror' : 'strErrorStr', 'winerror' : 1,
                      'errno' : 22, 'filename' : 'filenameStr'})
             )
-        except NameError: pass
-
-        import pickle, random
+        except NameError:
+            pass
 
         for args in exceptionList:
             expected = args[-1]
             try:
                 exc = args[0]
-                if len(args) == 2: raise exc
-                else: raise exc(*args[1])
+                if len(args) == 2:
+                    raise exc
+                else:
+                    raise exc(*args[1])
             except BaseException, e:
                 if (e is not exc and     # needed for sampleUnicode errors
-                    type(e) is not exc):
+                        type(e) is not exc):
                     raise
                 # Verify no ref leaks in Exc_str()
                 s = str(e)
@@ -274,12 +282,15 @@
                                        (repr(e), checkArgName))
 
                 # test for pickling support
-                new = pickle.loads(pickle.dumps(e, random.randint(0, 2)))
-                for checkArgName in expected:
-                    self.assertEquals(repr(getattr(e, checkArgName)),
-                                      repr(expected[checkArgName]),
-                                      'pickled exception "%s", attribute "%s' %
-                                      (repr(e), checkArgName))
+                for p in pickle, cPickle:
+                    for protocol in range(p.HIGHEST_PROTOCOL + 1):
+                        new = p.loads(p.dumps(e, protocol))
+                        for checkArgName in expected:
+                            got = repr(getattr(new, checkArgName))
+                            want = repr(expected[checkArgName])
+                            self.assertEquals(got, want,
+                                              'pickled "%r", attribute "%s' %
+                                              (e, checkArgName))
 
     def testKeywordArgs(self):
         # test that builtin exception don't take keyword args,


More information about the Python-checkins mailing list