Skip to content

Commit 57faf34

Browse files
bpo-33334: Support NOP and EXTENDED_ARG in dis.stack_effect(). (#6566)
Added tests to ensure that all defined opcodes are supported.
1 parent e9d9494 commit 57faf34

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/test/test__opcode.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ def test_stack_effect(self):
1515
self.assertRaises(ValueError, _opcode.stack_effect, 30000)
1616
self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['BUILD_SLICE'])
1717
self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['POP_TOP'], 0)
18+
# All defined opcodes
19+
for name, code in dis.opmap.items():
20+
with self.subTest(opname=name):
21+
if code < dis.HAVE_ARGUMENT:
22+
_opcode.stack_effect(code)
23+
self.assertRaises(ValueError, _opcode.stack_effect, code, 0)
24+
else:
25+
_opcode.stack_effect(code, 0)
26+
self.assertRaises(ValueError, _opcode.stack_effect, code)
27+
# All not defined opcodes
28+
for code in set(range(256)) - set(dis.opmap.values()):
29+
with self.subTest(opcode=code):
30+
self.assertRaises(ValueError, _opcode.stack_effect, code)
31+
self.assertRaises(ValueError, _opcode.stack_effect, code, 0)
32+
1833

1934
if __name__ == "__main__":
2035
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`dis.stack_effect` now supports all defined opcodes including NOP and
2+
EXTENDED_ARG.

Python/compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,10 @@ static int
859859
stack_effect(int opcode, int oparg, int jump)
860860
{
861861
switch (opcode) {
862+
case NOP:
863+
case EXTENDED_ARG:
864+
return 0;
865+
862866
/* Stack manipulation */
863867
case POP_TOP:
864868
return -1;

0 commit comments

Comments
 (0)