| 23 | |
| 24 | |
| 25 | class Python: |
| 26 | def __init__(self, version, picklefile): |
| 27 | self.pythonpath = shutil.which(version) |
| 28 | if not self.pythonpath: |
| 29 | pytest.skip(f"{version!r} not found") |
| 30 | self.picklefile = picklefile |
| 31 | |
| 32 | def dumps(self, obj): |
| 33 | dumpfile = self.picklefile.with_name("dump.py") |
| 34 | dumpfile.write_text( |
| 35 | textwrap.dedent( |
| 36 | r""" |
| 37 | import pickle |
| 38 | f = open({!r}, 'wb') |
| 39 | s = pickle.dump({!r}, f, protocol=2) |
| 40 | f.close() |
| 41 | """.format( |
| 42 | str(self.picklefile), obj |
| 43 | ) |
| 44 | ) |
| 45 | ) |
| 46 | subprocess.check_call((self.pythonpath, str(dumpfile))) |
| 47 | |
| 48 | def load_and_is_true(self, expression): |
| 49 | loadfile = self.picklefile.with_name("load.py") |
| 50 | loadfile.write_text( |
| 51 | textwrap.dedent( |
| 52 | r""" |
| 53 | import pickle |
| 54 | f = open({!r}, 'rb') |
| 55 | obj = pickle.load(f) |
| 56 | f.close() |
| 57 | res = eval({!r}) |
| 58 | if not res: |
| 59 | raise SystemExit(1) |
| 60 | """.format( |
| 61 | str(self.picklefile), expression |
| 62 | ) |
| 63 | ) |
| 64 | ) |
| 65 | print(loadfile) |
| 66 | subprocess.check_call((self.pythonpath, str(loadfile))) |
| 67 | |
| 68 | |
| 69 | @pytest.mark.parametrize("obj", [42, {}, {1: 3}]) |