multipython.py

Overview

The `multipython.py` module provides a set of parametrized tests designed to verify cross-version Python serialization compatibility using the `pickle` module. Specifically, it tests whether Python objects serialized (pickled) with one Python interpreter version can be correctly deserialized (unpickled) and validated with another Python interpreter version. This is critical for ensuring backward and forward compatibility of pickled data across multiple Python environments.

The tests leverage pytest fixtures to dynamically run serialization and deserialization steps across multiple Python versions (3.9, 3.10, 3.11) available on the system. The core logic is encapsulated in the `Python` class, which abstracts the invocation of different Python executables to perform pickling and unpickling subprocess operations.


Classes and Functions

Class: Python

Represents a Python interpreter instance for running serialization and deserialization subprocesses with a specified Python version.

__init__(self, version: str, picklefile: pathlib.Path)

dumps(self, obj: Any) -> None

load_and_is_true(self, expression: str) -> None


Pytest Fixtures


Test Function

test_basic_objects(python1: Python, python2: Python, obj: Any)


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class Python {
        -pythonpath: str
        -picklefile: pathlib.Path
        +__init__(version: str, picklefile: Path)
        +dumps(obj: Any) void
        +load_and_is_true(expression: str) void
    }

    class pytest_fixture_python1 {
        +params: list[str]
        +python1(request, tmp_path) -> Python
    }

    class pytest_fixture_python2 {
        +params: list[str]
        +python2(request, python1) -> Python
    }

    class test_basic_objects {
        +test_basic_objects(python1: Python, python2: Python, obj: Any)
    }

    pytest_fixture_python1 --> Python : creates instance
    pytest_fixture_python2 --> Python : creates instance (shares picklefile)
    test_basic_objects --> pytest_fixture_python1 : uses
    test_basic_objects --> pytest_fixture_python2 : uses
    test_basic_objects --> Python : calls dumps() and load_and_is_true()

Summary

The `multipython.py` file is a specialized pytest test module that verifies the compatibility of pickled data across multiple Python interpreter versions. It uses subprocess-driven scripts to serialize and deserialize objects in different Python environments and asserts correctness by evaluating expressions in those contexts. This ensures that serialized Python data can be safely shared or migrated between environments running different Python versions.