File: apptest_descriptor.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 (70 lines) | stat: -rw-r--r-- 2,018 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
import pytest

def test_property_name_in_error():
    class A:
        @property
        def my_funny_attribute(self):
            return 1

        unreadable = property()
        @unreadable.setter
        def even_funnier(self, value):
            pass

    with pytest.raises(AttributeError) as info:
        A().my_funny_attribute = 1
    assert "my_funny_attribute" in str(info.value)

    with pytest.raises(AttributeError) as info:
        del A().my_funny_attribute
    assert "my_funny_attribute" in str(info.value)

    with pytest.raises(AttributeError) as info:
        A().even_funnier
    assert "even_funnier" in str(info.value)

def test_property_name_in_error_setter():
    class A:
        pass
    p = property(lambda self: 1)
    p.__set_name__(A, "it_propagates")
    p = p.setter(lambda self, value: None)
    with pytest.raises(AttributeError) as info:
        p.__delete__(A())
    assert "it_propagates" in str(info.value)

def test_property_class_qualname_in_error():
    class ThisNiceNewFeature:
        @property
        def my_funny_attribute(self):
            return 1

        unreadable = property()
        @unreadable.setter
        def even_funnier(self, value):
            pass

    with pytest.raises(AttributeError) as info:
        ThisNiceNewFeature().my_funny_attribute = 1
    assert str(info.value) == "property 'my_funny_attribute' of 'test_property_class_qualname_in_error.<locals>.ThisNiceNewFeature' object has no setter"

    with pytest.raises(AttributeError) as info:
        del ThisNiceNewFeature().my_funny_attribute
    assert "ThisNiceNewFeature" in str(info.value)

    with pytest.raises(AttributeError) as info:
        ThisNiceNewFeature().unreadable
    assert "ThisNiceNewFeature" in str(info.value)


def test_dont_segfault():

    class pro(property):
        def __new__(typ, *args, **kwargs):
            return "abcdef"
    class A:
        pass

    p = property.__new__(pro)
    p.__set_name__(A, 1)
    p.getter(lambda self: 1) # must not crash