File: apptest_class.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (189 lines) | stat: -rw-r--r-- 4,585 bytes parent folder | download | duplicates (2)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import pytest

def test_class():
    class C(object):
        pass
    assert C.__class__ == type
    c = C()
    assert c.__class__ == C

def test_metaclass_explicit():
    class M(type):
        pass
    class C(metaclass=M):
        pass
    assert C.__class__ == M
    c = C()
    assert c.__class__ == C

def test_metaclass_inherited():
    class M(type):
        pass
    class B(metaclass=M):
        pass
    class C(B):
        pass
    assert C.__class__ == M
    c = C()
    assert c.__class__ == C

def test_method():
    class C(object):
        def meth(self):
            return 1
    c = C()
    assert c.meth() == 1

def test_method_exc():
    class C(object):
        def meth(self):
            raise RuntimeError
    c = C()
    pytest.raises(RuntimeError, c.meth)

def test_class_attr():
    class C(object):
        a = 42
    c = C()
    assert c.a == 42
    assert C.a == 42

def test_class_attr_inherited():
    class C(object):
        a = 42
    class D(C):
        pass
    d = D()
    assert d.a == 42
    assert D.a == 42

def test___new__():
    class A(object):
        pass
    assert isinstance(A(), A)
    assert isinstance(object.__new__(A), A)
    assert isinstance(A.__new__(A), A)

def test_int_subclass():
    class R(int):
        pass
    x = R(5)
    assert type(x) == R
    assert x == 5
    assert type(int(x)) == int
    assert int(x) == 5

def test_float_subclass():
    class R(float):
        pass
    x = R(5.5)
    assert type(x) == R
    assert x == 5.5
    assert type(float(x)) == float
    assert float(x) == 5.5

def test_meth_doc():
    class C(object):
        def meth_no_doc(self):
            pass
        def meth_doc(self):
            """this is a docstring"""
            pass
    c = C()
    assert C.meth_no_doc.__doc__ == None
    assert c.meth_no_doc.__doc__ == None
    assert C.meth_doc.__doc__ == """this is a docstring"""
    assert c.meth_doc.__doc__ == """this is a docstring"""

def test_getattribute():
    class C(object):
        def __getattribute__(self, attr):
            if attr == 'one':
                return 'two'
            return super(C, self).__getattribute__(attr)
    c = C()
    assert c.one == "two"
    pytest.raises(AttributeError, getattr, c, "two")

def test___class__():
    class C(object):
        def get_class(self):
            return __class__
    assert C().get_class()

def test_qualname():
    class C:
        class D:
            pass
    assert C.__qualname__ == 'test_qualname.<locals>.C'
    assert C.D.__qualname__ == 'test_qualname.<locals>.C.D'
    assert not hasattr(C(), '__qualname__')

def test_nonsensical_base_error_message():
    with raises(TypeError) as exc:
        class Foo('base'):
            pass
    assert str(exc.value).startswith(
        "metaclass found to be 'str', but calling <class 'str'> "
        "with args ('Foo', ('base',), ...) raised ")
    #
    def foo_func(): pass
    with pytest.raises(TypeError) as exc:
        class Foo(foo_func):
            pass
    assert str(exc.value).startswith(
        "metaclass found to be 'function', but calling <class 'function'> "
        "with args ('Foo', (<function test_nonsensical_base_error_message"
        ".<locals>.foo_func at ")

    with pytest.raises(TypeError) as exc:
        class Foo(object, object):
            pass
    assert str(exc.value).startswith(
        "duplicate base class 'object'")

def test_setclass_recursive():
    class Self:
        ready = False
    self = Self()
    # taken from lib-python/3/test/test_descr.py's
    # test_tp_subclasses_cycle_in_update_slots 
    class DebugHelperMeta(type):
        """
        Sets default __doc__ and simplifies repr() output.
        """
        def __new__(mcls, name, bases, attrs):
            if attrs.get('__doc__') is None:
                attrs['__doc__'] = name  # helps when debugging with gdb
            return type.__new__(mcls, name, bases, attrs)
        def __repr__(cls):
            return repr(cls.__name__)

    class M(DebugHelperMeta):
        def mro(cls):
            if self.ready and cls.__name__ == 'C':
                self.ready = False
                C.__bases__ = (B2,)
            return type.mro(cls)

    class A(metaclass=M):
        pass
    class B1(A):
        pass
    class B2(A):
        pass
    class C(A):
        pass

    self.ready = True
    C.__bases__ = (B1,)
    B1.__bases__ = (C,)

    assert C.__bases__ == (B2,)
    assert B2.__subclasses__() == [C]
    assert B1.__subclasses__() == []

    assert B1.__bases__ == (C,)
    assert C.__subclasses__() == [B1]