Skip to content

Commit 30badd1

Browse files
committed
tests: Add tests for calling super and loading a method directly.
1 parent dd11af2 commit 30badd1

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

tests/basics/class_super.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ class A:
2020
def p(self):
2121
print(str(super())[:18])
2222
A().p()
23+
24+
25+
# test compiler's handling of long expressions with super
26+
class A:
27+
bar = 123
28+
def foo(self):
29+
print('A foo')
30+
return [1, 2, 3]
31+
class B(A):
32+
def foo(self):
33+
print('B foo')
34+
print(super().bar) # accessing attribute after super()
35+
return super().foo().count(2) # calling a subsequent method
36+
print(B().foo())

tests/cmdline/cmd_showbc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,7 @@ class Class:
150150

151151
# delete name
152152
del Class
153+
154+
# load super method
155+
def f(self):
156+
super().f()

tests/cmdline/cmd_showbc.py.exp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ arg names:
77
(N_EXC_STACK 0)
88
bc=-1 line=1
99
########
10-
bc=\\d\+ line=152
10+
bc=\\d\+ line=155
1111
00 MAKE_FUNCTION \.\+
1212
\\d\+ STORE_NAME f
1313
\\d\+ MAKE_FUNCTION \.\+
@@ -25,6 +25,8 @@ arg names:
2525
\\d\+ CALL_FUNCTION n=2 nkw=0
2626
\\d\+ STORE_NAME Class
2727
\\d\+ DELETE_NAME Class
28+
\\d\+ MAKE_FUNCTION \.\+
29+
\\d\+ STORE_NAME f
2830
\\d\+ LOAD_CONST_NONE
2931
\\d\+ RETURN_VALUE
3032
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ bytes)
@@ -428,6 +430,23 @@ arg names:
428430
10 STORE_NAME __qualname__
429431
13 LOAD_CONST_NONE
430432
14 RETURN_VALUE
433+
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ bytes)
434+
Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
435+
########
436+
\.\+5b
437+
arg names: self
438+
(N_STATE 4)
439+
(N_EXC_STACK 0)
440+
bc=-1 line=1
441+
bc=0 line=156
442+
00 LOAD_GLOBAL super (cache=0)
443+
\\d\+ LOAD_GLOBAL __class__ (cache=0)
444+
\\d\+ LOAD_FAST 0
445+
\\d\+ LOAD_SUPER_METHOD f
446+
\\d\+ CALL_METHOD n=0 nkw=0
447+
\\d\+ POP_TOP
448+
\\d\+ LOAD_CONST_NONE
449+
\\d\+ RETURN_VALUE
431450
File cmdline/cmd_showbc.py, code block '<genexpr>' (descriptor: \.\+, bytecode @\.\+ bytes)
432451
Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
433452
########
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# test super() operations which don't require allocation
2+
import micropython
3+
4+
class A:
5+
def foo(self):
6+
print('A foo')
7+
return 42
8+
class B(A):
9+
def foo(self):
10+
print('B foo')
11+
print(super().foo())
12+
13+
b = B()
14+
15+
micropython.heap_lock()
16+
b.foo()
17+
micropython.heap_unlock()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
B foo
2+
A foo
3+
42

0 commit comments

Comments
 (0)