File: test_dictproxy.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (82 lines) | stat: -rw-r--r-- 2,728 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
class AppTestUserObject:
    def test_dictproxy(self):
        class NotEmpty(object):
            a = 1
        NotEmpty.a = 1
        NotEmpty.a = 1
        NotEmpty.a = 1
        NotEmpty.a = 1
        assert 'a' in NotEmpty.__dict__
        assert 'a' in NotEmpty.__dict__.keys()
        assert 'b' not in NotEmpty.__dict__
        assert NotEmpty.__dict__.get("b") is None
        raises(TypeError, "NotEmpty.__dict__['b'] = 4")
        raises(TypeError, 'NotEmpty.__dict__[15] = "y"')
        raises(TypeError, 'del NotEmpty.__dict__[15]')

        raises(AttributeError, 'NotEmpty.__dict__.setdefault')

    def test_dictproxy_getitem(self):
        class NotEmpty(object):
            a = 1
        assert 'a' in NotEmpty.__dict__
        class substr(str):
            pass
        assert substr('a') in NotEmpty.__dict__

    def test_dictproxyeq(self):
        class a(object):
            pass
        class b(a):
            stuff = 42
        class c(a):
            stuff = 42
        assert a.__dict__ == a.__dict__
        assert a.__dict__ != b.__dict__
        assert a.__dict__ != {'123': '456'}
        assert {'123': '456'} != a.__dict__
        assert b.__dict__ == c.__dict__

    def test_str_repr(self):
        class a(object):
            pass
        s1 = repr(a.__dict__)
        assert s1.startswith('mappingproxy({') and s1.endswith('})')
        s2 = str(a.__dict__)
        assert s1 == 'mappingproxy(%s)' % s2

    def test_immutable_dict_on_builtin_type(self):
        raises(TypeError, "int.__dict__['a'] = 1")
        raises((AttributeError, TypeError), "int.__dict__.popitem()")
        raises((AttributeError, TypeError), "int.__dict__.clear()")

    def test_mappingproxy(self):
        dictproxy = type(int.__dict__)
        assert dictproxy is not dict
        assert dictproxy.__name__ == 'mappingproxy'
        raises(TypeError, dictproxy)
        mapping = dict(a=1, b=2, c=3)
        proxy = dictproxy(mapping)
        assert proxy['a'] == 1
        assert 'a' in proxy
        assert 'z' not in proxy
        assert repr(proxy) == 'mappingproxy(%r)' % mapping
        assert proxy.keys() == mapping.keys()
        raises(TypeError, "proxy['a'] = 4")
        raises(TypeError, "del proxy['a']")
        raises(AttributeError, "proxy.clear()")
        raises(TypeError, reversed, proxy)
        #
        class D(dict):
            def copy(self): return 3
        proxy = dictproxy(D(a=1, b=2, c=3))
        assert proxy.copy() == 3
        #
        raises(TypeError, dictproxy, 3)
        raises(TypeError, dictproxy, [3])
        #
        {}.update(proxy)


class AppTestUserObjectMethodCache(AppTestUserObject):
    spaceconfig = {"objspace.std.withmethodcachecounter": True}