File: test_compat.py

package info (click to toggle)
python-rarfile 4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,308 kB
  • sloc: python: 4,180; makefile: 201; sh: 88; awk: 14
file content (88 lines) | stat: -rw-r--r-- 1,944 bytes parent folder | download
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Test zipfile compat.
"""

import inspect
import sys
import zipfile

import pytest

import rarfile

# dont fail on new python by default
_VERS = [(3, 6), (3, 7), (3, 8)]

_UNSUPPORTED = sys.version_info[:2] not in _VERS

_ignore = set([
    "detach",
    "peek",
    "read1",
    "readinto1",

    "seek",

    # no kwargs
    "readinto",
    "readline",
    "truncate",
    "write",

    # random
    "FileHeader",
    "from_file",
    "testzip",
    "writestr",
])


def load_cls_names(maincls):
    assert inspect.isclass(maincls)
    res = {}
    for cls in inspect.getmro(maincls):
        for name, val in inspect.getmembers(cls):
            if name not in res:
                res[name] = val
    return res


def cleansig(sig):
    res = str(sig).replace(", /", "")
    if "*" in res:
        res = res.split(", *", 1)[0] + ")"
    return res


def compare(rmaincls, zmaincls):
    znames = load_cls_names(zmaincls)
    rnames = load_cls_names(rmaincls)
    for name, zval in znames.items():
        if not inspect.isroutine(zval) or name[0] == "_" or name in _ignore:
            continue
        assert name in rnames, "member not found: \"%s\"" % name

        rval = rnames[name]
        zsig = inspect.signature(zval)
        rsig = inspect.signature(rval)

        zsigstr = cleansig(zsig)
        rsigstr = cleansig(rsig)
        assert zsigstr == rsigstr, "sig differs: %s.%s%s != %s.%s%s" % (
            rmaincls.__name__, name, rsigstr,
            zmaincls.__name__, name, zsigstr)


@pytest.mark.skipif(_UNSUPPORTED, reason="Unsupported for sig checks")
def test_cmp_zipfile():
    compare(rarfile.RarFile, zipfile.ZipFile)


@pytest.mark.skipif(_UNSUPPORTED, reason="Unsupported for sig checks")
def test_cmp_zipextfile():
    compare(rarfile.RarExtFile, zipfile.ZipExtFile)


@pytest.mark.skipif(_UNSUPPORTED, reason="Unsupported for sig checks")
def test_cmp_zipinfo():
    compare(rarfile.RarInfo, zipfile.ZipInfo)