2023-07-20 14:41:16 +08:00
|
|
|
"""Module containing a parametrized tests testing cross-python serialization
|
|
|
|
via the pickle module."""
|
2024-01-31 03:34:23 +08:00
|
|
|
|
2019-06-28 02:08:31 +08:00
|
|
|
import shutil
|
2019-01-22 11:46:57 +08:00
|
|
|
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
|
|
|
|
2024-02-01 04:12:33 +08:00
|
|
|
|
2023-07-20 14:41:16 +08:00
|
|
|
pythonlist = ["python3.9", "python3.10", "python3.11"]
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=pythonlist)
|
2021-03-14 03:22:54 +08:00
|
|
|
def python1(request, tmp_path):
|
|
|
|
picklefile = tmp_path / "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
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class Python:
|
2009-10-01 00:36:04 +08:00
|
|
|
def __init__(self, version, picklefile):
|
2019-06-28 02:08:31 +08:00
|
|
|
self.pythonpath = shutil.which(version)
|
2009-10-01 00:36:04 +08:00
|
|
|
if not self.pythonpath:
|
2020-10-03 04:16:22 +08:00
|
|
|
pytest.skip(f"{version!r} not found")
|
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):
|
2021-03-14 03:22:54 +08:00
|
|
|
dumpfile = self.picklefile.with_name("dump.py")
|
|
|
|
dumpfile.write_text(
|
2018-08-23 09:30:42 +08:00
|
|
|
textwrap.dedent(
|
2024-02-01 04:12:33 +08:00
|
|
|
rf"""
|
2018-08-23 09:30:42 +08:00
|
|
|
import pickle
|
2024-02-01 04:12:33 +08:00
|
|
|
f = open({str(self.picklefile)!r}, 'wb')
|
|
|
|
s = pickle.dump({obj!r}, f, protocol=2)
|
2018-08-23 09:30:42 +08:00
|
|
|
f.close()
|
2024-02-01 04:12:33 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
)
|
2023-07-20 14:41:16 +08:00
|
|
|
subprocess.run((self.pythonpath, str(dumpfile)), check=True)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-10-01 00:36:04 +08:00
|
|
|
def load_and_is_true(self, expression):
|
2021-03-14 03:22:54 +08:00
|
|
|
loadfile = self.picklefile.with_name("load.py")
|
|
|
|
loadfile.write_text(
|
2018-08-23 09:30:42 +08:00
|
|
|
textwrap.dedent(
|
2024-02-01 04:12:33 +08:00
|
|
|
rf"""
|
2018-08-23 09:30:42 +08:00
|
|
|
import pickle
|
2024-02-01 04:12:33 +08:00
|
|
|
f = open({str(self.picklefile)!r}, 'rb')
|
2018-08-23 09:30:42 +08:00
|
|
|
obj = pickle.load(f)
|
|
|
|
f.close()
|
2024-02-01 04:12:33 +08:00
|
|
|
res = eval({expression!r})
|
2018-08-23 09:30:42 +08:00
|
|
|
if not res:
|
|
|
|
raise SystemExit(1)
|
2024-02-01 04:12:33 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
print(loadfile)
|
2023-07-20 14:41:16 +08:00
|
|
|
subprocess.run((self.pythonpath, str(loadfile)), check=True)
|
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)
|
2020-10-03 04:16:22 +08:00
|
|
|
python2.load_and_is_true(f"obj == {obj}")
|