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
|
|
|
"""
|
2011-11-19 02:32:11 +08:00
|
|
|
import py, pytest
|
2009-10-01 00:36:04 +08:00
|
|
|
|
2010-11-21 04:35:55 +08:00
|
|
|
pythonlist = ['python2.4', 'python2.5', 'python2.6', 'python2.7', 'python2.8']
|
2012-08-13 18:58:08 +08:00
|
|
|
@pytest.factory(params=pythonlist)
|
|
|
|
def python1(testcontext, tmpdir):
|
2009-10-01 00:36:04 +08:00
|
|
|
picklefile = tmpdir.join("data.pickle")
|
2012-08-13 18:58:08 +08:00
|
|
|
return Python(testcontext.param, picklefile)
|
2009-10-01 00:36:04 +08:00
|
|
|
|
2012-08-13 18:58:08 +08:00
|
|
|
@pytest.factory(params=pythonlist)
|
|
|
|
def python2(testcontext, python1):
|
|
|
|
return Python(testcontext.param, python1.picklefile)
|
2009-10-01 00:36:04 +08:00
|
|
|
|
|
|
|
class Python:
|
|
|
|
def __init__(self, version, picklefile):
|
|
|
|
self.pythonpath = py.path.local.sysfind(version)
|
|
|
|
if not self.pythonpath:
|
|
|
|
py.test.skip("%r not found" %(version,))
|
|
|
|
self.picklefile = picklefile
|
|
|
|
def dumps(self, obj):
|
|
|
|
dumpfile = self.picklefile.dirpath("dump.py")
|
|
|
|
dumpfile.write(py.code.Source("""
|
|
|
|
import pickle
|
|
|
|
f = open(%r, 'wb')
|
|
|
|
s = pickle.dump(%r, f)
|
|
|
|
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")
|
|
|
|
loadfile.write(py.code.Source("""
|
|
|
|
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)
|