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
|
|
|
"""
|
2014-01-18 19:31:33 +08:00
|
|
|
import py
|
|
|
|
import pytest
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2009-10-01 00:36:04 +08:00
|
|
|
|
2016-08-04 04:48:49 +08:00
|
|
|
pythonlist = ['python2.6', '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
|
|
|
|
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
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Python(object):
|
2009-10-01 00:36:04 +08:00
|
|
|
def __init__(self, version, picklefile):
|
|
|
|
self.pythonpath = py.path.local.sysfind(version)
|
|
|
|
if not self.pythonpath:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.skip("%r not found" %(version,))
|
2009-10-01 00:36:04 +08:00
|
|
|
self.picklefile = picklefile
|
|
|
|
def dumps(self, obj):
|
|
|
|
dumpfile = self.picklefile.dirpath("dump.py")
|
2015-11-27 22:43:01 +08:00
|
|
|
dumpfile.write(_pytest._code.Source("""
|
2009-10-01 00:36:04 +08:00
|
|
|
import pickle
|
|
|
|
f = open(%r, 'wb')
|
2015-03-26 16:34:10 +08:00
|
|
|
s = pickle.dump(%r, f, protocol=2)
|
2009-10-01 00:36:04 +08:00
|
|
|
f.close()
|
|
|
|
""" % (str(self.picklefile), obj)))
|
|
|
|
py.process.cmdexec("%s %s" %(self.pythonpath, 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")
|
2015-11-27 22:43:01 +08:00
|
|
|
loadfile.write(_pytest._code.Source("""
|
2009-10-01 00:36:04 +08:00
|
|
|
import pickle
|
|
|
|
f = open(%r, 'rb')
|
|
|
|
obj = pickle.load(f)
|
|
|
|
f.close()
|
|
|
|
res = eval(%r)
|
|
|
|
if not res:
|
|
|
|
raise SystemExit(1)
|
|
|
|
""" % (str(self.picklefile), expression)))
|
2010-01-17 17:54:36 +08:00
|
|
|
print (loadfile)
|
2009-10-01 00:36:04 +08:00
|
|
|
py.process.cmdexec("%s %s" %(self.pythonpath, loadfile))
|
2012-08-13 18:58:08 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("obj", [42, {}, {1:3},])
|
|
|
|
def test_basic_objects(python1, python2, obj):
|
|
|
|
python1.dumps(obj)
|
|
|
|
python2.load_and_is_true("obj == %s" % obj)
|