"""
module containing a parametrized tests testing cross-python
serialization via the pickle module.
"""importshutilimportsubprocessimporttextwrapimportpytestpythonlist=["python3.5","python3.6","python3.7"]@pytest.fixture(params=pythonlist)defpython1(request,tmpdir):picklefile=tmpdir.join("data.pickle")returnPython(request.param,picklefile)@pytest.fixture(params=pythonlist)defpython2(request,python1):returnPython(request.param,python1.picklefile)classPython:def__init__(self,version,picklefile):self.pythonpath=shutil.which(version)ifnotself.pythonpath:pytest.skip("{!r} not found".format(version))self.picklefile=picklefiledefdumps(self,obj):dumpfile=self.picklefile.dirpath("dump.py")dumpfile.write(textwrap.dedent(r"""
import pickle
f = open({!r}, 'wb')
s = pickle.dump({!r}, f, protocol=2)
f.close()
""".format(str(self.picklefile),obj)))subprocess.check_call((self.pythonpath,str(dumpfile)))defload_and_is_true(self,expression):loadfile=self.picklefile.dirpath("load.py")loadfile.write(textwrap.dedent(r"""
import pickle
f = open({!r}, 'rb')
obj = pickle.load(f)
f.close()
res = eval({!r})
if not res:
raise SystemExit(1)
""".format(str(self.picklefile),expression)))print(loadfile)subprocess.check_call((self.pythonpath,str(loadfile)))@user3ize("obj",[42,{},{1:3}])deftest_basic_objects(python1,python2,obj):python1.dumps(obj)python2.load_and_is_true("obj == {}".format(obj))