2009-10-01 00:36:04 +08:00
|
|
|
"""
|
|
|
|
module containing a parametrized tests testing cross-python
|
2010-07-27 03:15:15 +08:00
|
|
|
serialization via the pickle module.
|
2009-10-01 00:36:04 +08:00
|
|
|
"""
|
2019-01-22 11:46:57 +08:00
|
|
|
import distutils.spawn
|
|
|
|
import subprocess
|
2018-08-23 09:30:42 +08:00
|
|
|
import textwrap
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
import pytest
|
2009-10-01 00:36:04 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
pythonlist = ["python2.7", "python3.4", "python3.5"]
|
|
|
|
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=pythonlist)
|
2012-09-17 22:36:10 +08:00
|
|
|
def python1(request, tmpdir):
|
2009-10-01 00:36:04 +08:00
|
|
|
picklefile = tmpdir.join("data.pickle")
|
2012-09-17 22:36:10 +08:00
|
|
|
return Python(request.param, picklefile)
|
2009-10-01 00:36:04 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=pythonlist)
|
2012-09-17 22:36:10 +08:00
|
|
|
def python2(request, python1):
|
|
|
|
return Python(request.param, python1.picklefile)
|
2009-10-01 00:36:04 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Python(object):
|
2009-10-01 00:36:04 +08:00
|
|
|
def __init__(self, version, picklefile):
|
2019-01-22 11:46:57 +08:00
|
|
|
self.pythonpath = distutils.spawn.find_executable(version)
|
2009-10-01 00:36:04 +08:00
|
|
|
if not self.pythonpath:
|
2018-08-23 09:30:42 +08:00
|
|
|
pytest.skip("{!r} not found".format(version))
|
2009-10-01 00:36:04 +08:00
|
|
|
self.picklefile = picklefile
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2009-10-01 00:36:04 +08:00
|
|
|
def dumps(self, obj):
|
|
|
|
dumpfile = self.picklefile.dirpath("dump.py")
|
2018-05-23 22:48:46 +08:00
|
|
|
dumpfile.write(
|
2018-08-23 09:30:42 +08:00
|
|
|
textwrap.dedent(
|
2018-11-03 21:48:10 +08:00
|
|
|
r"""
|
2018-08-23 09:30:42 +08:00
|
|
|
import pickle
|
|
|
|
f = open({!r}, 'wb')
|
|
|
|
s = pickle.dump({!r}, f, protocol=2)
|
|
|
|
f.close()
|
|
|
|
""".format(
|
|
|
|
str(self.picklefile), obj
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
)
|
2019-01-22 11:46:57 +08:00
|
|
|
subprocess.check_call((self.pythonpath, str(dumpfile)))
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-10-01 00:36:04 +08:00
|
|
|
def load_and_is_true(self, expression):
|
|
|
|
loadfile = self.picklefile.dirpath("load.py")
|
2018-05-23 22:48:46 +08:00
|
|
|
loadfile.write(
|
2018-08-23 09:30:42 +08:00
|
|
|
textwrap.dedent(
|
2018-11-03 21:48:10 +08:00
|
|
|
r"""
|
2018-08-23 09:30:42 +08:00
|
|
|
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
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
print(loadfile)
|
2019-01-22 11:46:57 +08:00
|
|
|
subprocess.check_call((self.pythonpath, str(loadfile)))
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-08-13 18:58:08 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("obj", [42, {}, {1: 3}])
|
2012-08-13 18:58:08 +08:00
|
|
|
def test_basic_objects(python1, python2, obj):
|
|
|
|
python1.dumps(obj)
|
|
|
|
python2.load_and_is_true("obj == %s" % obj)
|