Index: Lib/test/test_exceptions.py =================================================================== --- Lib/test/test_exceptions.py (revision 82327) +++ Lib/test/test_exceptions.py (working copy) @@ -3,12 +3,13 @@ import os import sys import unittest -import pickle import weakref from test.support import (TESTFN, unlink, run_unittest, captured_output, - gc_collect) + gc_collect, import_module_implementations) +pickle_implementations = import_module_implementations('pickle') + # XXX This is not really enough, each *operation* should be tested! class ExceptionTests(unittest.TestCase): @@ -302,7 +303,7 @@ value, expected[checkArgName])) # test for pickling support - for p in [pickle]: + for p in pickle_implementations: for protocol in range(p.HIGHEST_PROTOCOL + 1): s = p.dumps(e, protocol) new = p.loads(s) Index: Lib/test/test_set.py =================================================================== --- Lib/test/test_set.py (revision 82327) +++ Lib/test/test_set.py (working copy) @@ -4,12 +4,14 @@ import weakref import operator import copy -import pickle from random import randrange, shuffle import sys import warnings import collections +pickle_implementations = support.import_module_implementations('pickle') + + class PassThru(Exception): pass @@ -224,15 +226,16 @@ self.assertFalse(set('cbs').issuperset('a')) def test_pickling(self): - for i in range(pickle.HIGHEST_PROTOCOL + 1): - p = pickle.dumps(self.s, i) - dup = pickle.loads(p) - self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup)) - if type(self.s) not in (set, frozenset): - self.s.x = 10 - p = pickle.dumps(self.s) + for pickle in pickle_implementations: + for i in range(pickle.HIGHEST_PROTOCOL + 1): + p = pickle.dumps(self.s, i) dup = pickle.loads(p) - self.assertEqual(self.s.x, dup.x) + self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup)) + if type(self.s) not in (set, frozenset): + self.s.x = 10 + p = pickle.dumps(self.s) + dup = pickle.loads(p) + self.assertEqual(self.s.x, dup.x) def test_deepcopy(self): class Tracer: @@ -821,10 +824,11 @@ self.assertEqual(setiter.__length_hint__(), len(self.set)) def test_pickling(self): - p = pickle.dumps(self.set) - copy = pickle.loads(p) - self.assertEqual(self.set, copy, - "%s != %s" % (self.set, copy)) + for pickle in pickle_implementations: + p = pickle.dumps(self.set) + copy = pickle.loads(p) + self.assertEqual(self.set, copy, + "%s != %s" % (self.set, copy)) #------------------------------------------------------------------------------ Index: Lib/test/support.py =================================================================== --- Lib/test/support.py (revision 82327) +++ Lib/test/support.py (working copy) @@ -83,6 +83,14 @@ except ImportError as msg: raise unittest.SkipTest(str(msg)) +def import_module_implementations(name): + """Return a list of one or two modules depending on availability + native optimizations. When optimizations are available, the + second module in the returned list will not include them.""" + result = [importlib.import_module(name)] + if ('_' + name) in sys.modules: + result.append(import_fresh_module(name, blocked=['_' + name])) + return result def _save_and_remove_module(name, orig_modules): """Helper function to save and remove a module from sys.modules Index: Lib/test/test_collections.py =================================================================== --- Lib/test/test_collections.py (revision 82327) +++ Lib/test/test_collections.py (working copy) @@ -17,6 +17,7 @@ from collections import Sequence, MutableSequence from collections import ByteString +pickle_implementations = support.import_module_implementations('pickle') TestNT = namedtuple('TestNT', 'x y z') # type used for pickle tests class TestNamedTuple(unittest.TestCase): @@ -165,7 +166,7 @@ def test_pickle(self): p = TestNT(x=10, y=20, z=30) - for module in (pickle,): + for module in pickle_implementations: loads = getattr(module, 'loads') dumps = getattr(module, 'dumps') for protocol in -1, 0, 1, 2: