File: apptest_callmethod.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 (107 lines) | stat: -rw-r--r-- 2,636 bytes parent folder | download | duplicates (4)
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
# The exec hacking is needed to have the code snippets compiled
# by our own compiler, not CPython's

def test_call_method():
    exec("""if 1:
        class C(object):
            def m(*args, **kwds):
                return args, kwds
            sm = staticmethod(m)
            cm = classmethod(m)

        c = C()
        assert c.m() == ((c,), {})
        assert c.sm() == ((), {})
        assert c.cm() == ((C,), {})
        assert c.m(5) == ((c, 5), {})
        assert c.sm(6) == ((6,), {})
        assert c.cm(7) == ((C, 7), {})
        assert c.m(5, x=3) == ((c, 5), {'x': 3})
        assert c.m(*range(5)) == ((c, 0, 1, 2, 3, 4), {})
        assert c.m(**{'u': 4}) == ((c,), {'u': 4})
    """)

def test_call_attribute():
    exec("""if 1:
        class C(object):
            def m(*args, **kwds):
                return args, kwds
        def myfunc(*args):
            return args

        c = C()
        c.m = c.m2 = myfunc
        assert c.m() == ()
        assert c.m(5, 2) == (5, 2)
        assert c.m2(4) == (4,)
        assert c.m2("h", "e", "l", "l", "o") == tuple("hello")
    """)

def test_call_module():
    exec("""if 1:
        import sys
        try:
            sys.exit(5)
        except SystemExit as e:
            assert e.args == (5,)
        else:
            raise Exception("did not raise?")
    """)

def test_custom_getattr():
    exec("""if 1:
        class C(object):
            def __getattr__(self, name):
                if name == 'bla':
                    return myfunc
                raise AttributeError
        def myfunc(*args):
            return args

        c = C()
        assert c.bla(1, 2, 3) == (1, 2, 3)
    """, {})

def test_custom_getattribute():
    exec("""if 1:
        class C(object):
            def __getattribute__(self, name):
                if name == 'bla':
                    return myfunc
                raise AttributeError
            def bla(self):
                Boom
        def myfunc(*args):
            return args

        c = C()
        assert c.bla(1, 2, 3) == (1, 2, 3)
    """, {})

def test_builtin():
    exec("""if 1:
        class C(object):
            foobar = len
        c = C()
        assert c.foobar("hello") == 5
    """)

def test_attributeerror():
    exec("""if 1:
        assert 5 .__add__(6) == 11
        try:
            6 .foobar(7)
        except AttributeError:
            pass
        else:
            raise Exception("did not raise?")
    """)

def test_kwargs():
    exec("""if 1:
        class C(object):
            def f(self, a):
                return a + 2
        
        assert C().f(a=3) == 5
    """)