aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/multidict/tests/gen_pickles.py
blob: 618ce5b4d7a2a6e8768aec1d437984bb62513bc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import pickle
from importlib import import_module
from pathlib import Path
from typing import Union

from multidict import CIMultiDict, MultiDict, istr

TESTS_DIR = Path(__file__).parent.resolve()
_MD_Classes = Union[type[MultiDict[int]], type[CIMultiDict[int]]]


def write(tag: str, cls: _MD_Classes, proto: int) -> None:
    d = cls([("a", 1), ("a", 2)])
    file_basename = f"{cls.__name__.lower()}-{tag}"
    with (TESTS_DIR / f"{file_basename}.pickle.{proto}").open("wb") as f:
        pickle.dump(d, f, proto)


def write_istr(tag: str, cls: type[istr], proto: int) -> None:
    s = cls("str")
    file_basename = f"{cls.__name__.lower()}-{tag}"
    with (TESTS_DIR / f"{file_basename}.pickle.{proto}").open("wb") as f:
        pickle.dump(s, f, proto)


def generate() -> None:
    _impl_map = {
        "c-extension": "_multidict",
        "pure-python": "_multidict_py",
    }
    for proto in range(pickle.HIGHEST_PROTOCOL + 1):
        for tag, impl_name in _impl_map.items():
            impl = import_module(f"multidict.{impl_name}")
            for cls in impl.CIMultiDict, impl.MultiDict:
                write(tag, cls, proto)
            write_istr(tag, impl.istr, proto)


if __name__ == "__main__":
    generate()