Debian Codegraph @

pypy/objspace/std/test/apptest_callmethod.py

raw symbols SCIP
1 # The exec hacking is needed to have the code snippets compiled
2 # by our own compiler, not CPython's
3
4 def test_call_method():
5 exec("""if 1:
6 class C(object):
7 def m(*args, **kwds):
8 return args, kwds
9 sm = staticmethod(m)
10 cm = classmethod(m)
11
12 c = C()
13 assert c.m() == ((c,), {})
14 assert c.sm() == ((), {})
15 assert c.cm() == ((C,), {})
16 assert c.m(5) == ((c, 5), {})
17 assert c.sm(6) == ((6,), {})
18 assert c.cm(7) == ((C, 7), {})
19 assert c.m(5, x=3) == ((c, 5), {'x': 3})
20 assert c.m(*range(5)) == ((c, 0, 1, 2, 3, 4), {})
21 assert c.m(**{'u': 4}) == ((c,), {'u': 4})
22 """)
23
24 def test_call_attribute():
25 exec("""if 1:
26 class C(object):
27 def m(*args, **kwds):
28 return args, kwds
29 def myfunc(*args):
30 return args
31
32 c = C()
33 c.m = c.m2 = myfunc
34 assert c.m() == ()
35 assert c.m(5, 2) == (5, 2)
36 assert c.m2(4) == (4,)
37 assert c.m2("h", "e", "l", "l", "o") == tuple("hello")
38 """)
39
40 def test_call_module():
41 exec("""if 1:
42 import sys
43 try:
44 sys.exit(5)
45 except SystemExit as e:
46 assert e.args == (5,)
47 else:
48 raise Exception("did not raise?")
49 """)
50
51 def test_custom_getattr():
52 exec("""if 1:
53 class C(object):
54 def __getattr__(self, name):
55 if name == 'bla':
56 return myfunc
57 raise AttributeError
58 def myfunc(*args):
59 return args
60
61 c = C()
62 assert c.bla(1, 2, 3) == (1, 2, 3)
63 """, {})
64
65 def test_custom_getattribute():
66 exec("""if 1:
67 class C(object):
68 def __getattribute__(self, name):
69 if name == 'bla':
70 return myfunc
71 raise AttributeError
72 def bla(self):
73 Boom
74 def myfunc(*args):
75 return args
76
77 c = C()
78 assert c.bla(1, 2, 3) == (1, 2, 3)
79 """, {})
80
81 def test_builtin():
82 exec("""if 1:
83 class C(object):
84 foobar = len
85 c = C()
86 assert c.foobar("hello") == 5
87 """)
88
89 def test_builtin_no_self_prepend():
90 # builtin_function (e.g. len/repr) must NOT use the fast method path.
91 # If it did, LOAD_METHOD would push (len, c) and CALL_METHOD would call
92 # len(c, "hello") instead of len("hello"), causing a TypeError.
93 exec("""if 1:
94 class C(object):
95 describe = repr
96 count = len
97 c = C()
98 assert c.describe("x") == repr("x")
99 assert c.count("hello") == 5
100 """)
101
102 def test_method_descriptor_direct_call_contract():
103 # For any type used via the fast method path (flag_method_descriptor=True),
104 # calling descriptor(self, *args) must equal descriptor.__get__(self, T)(*args).
105 # Verifies the behavioral contract the LOAD_METHOD optimization relies on.
106 exec("""if 1:
107 class C(object):
108 def f(self, x):
109 return (self, x)
110 def g(self, x, y=0):
111 return (self, x, y)
112
113 c = C()
114 # fast path calls C.f(c, 1) directly; bound method calls c.f(1)
115 assert C.f(c, 1) == c.f(1)
116 assert C.g(c, 2, y=3) == c.g(2, y=3)
117
118 # staticmethod: __get__ strips self, direct call must NOT prepend self
119 class D(object):
120 sm = staticmethod(lambda x: x * 2)
121 d = D()
122 assert d.sm(4) == 8
123
124 # classmethod: __get__ prepends the class, not the instance
125 class E(object):
126 cm = classmethod(lambda cls, x: (cls, x))
127 e = E()
128 assert e.cm(5) == (E, 5)
129 """)
130
131 def test_attributeerror():
132 exec("""if 1:
133 assert 5 .__add__(6) == 11
134 try:
135 6 .foobar(7)
136 except AttributeError:
137 pass
138 else:
139 raise Exception("did not raise?")
140 """)
141
142 def test_kwargs():
143 exec("""if 1:
144 class C(object):
145 def f(self, a):
146 return a + 2
147
148 assert C().f(a=3) == 5
149 """)