[Python-checkins] bpo-33334: Support NOP and EXTENDED_ARG in dis.stack_effect(). (#6566)

Serhiy Storchaka webhook-mailer at python.org
Wed Apr 25 15:04:15 EDT 2018


https://github.com/python/cpython/commit/57faf348872d1d0af1808c82f535cf220d64b028
commit: 57faf348872d1d0af1808c82f535cf220d64b028
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2018-04-25T22:04:06+03:00
summary:

bpo-33334: Support NOP and EXTENDED_ARG in dis.stack_effect(). (#6566)

Added tests to ensure that all defined opcodes are supported.

files:
A Misc/NEWS.d/next/Library/2018-04-22-20-13-21.bpo-33334.19UMOC.rst
M Lib/test/test__opcode.py
M Python/compile.c

diff --git a/Lib/test/test__opcode.py b/Lib/test/test__opcode.py
index 1075decc2704..2af1ee35bff0 100644
--- a/Lib/test/test__opcode.py
+++ b/Lib/test/test__opcode.py
@@ -15,6 +15,21 @@ def test_stack_effect(self):
         self.assertRaises(ValueError, _opcode.stack_effect, 30000)
         self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['BUILD_SLICE'])
         self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['POP_TOP'], 0)
+        # All defined opcodes
+        for name, code in dis.opmap.items():
+            with self.subTest(opname=name):
+                if code < dis.HAVE_ARGUMENT:
+                    _opcode.stack_effect(code)
+                    self.assertRaises(ValueError, _opcode.stack_effect, code, 0)
+                else:
+                    _opcode.stack_effect(code, 0)
+                    self.assertRaises(ValueError, _opcode.stack_effect, code)
+        # All not defined opcodes
+        for code in set(range(256)) - set(dis.opmap.values()):
+            with self.subTest(opcode=code):
+                self.assertRaises(ValueError, _opcode.stack_effect, code)
+                self.assertRaises(ValueError, _opcode.stack_effect, code, 0)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2018-04-22-20-13-21.bpo-33334.19UMOC.rst b/Misc/NEWS.d/next/Library/2018-04-22-20-13-21.bpo-33334.19UMOC.rst
new file mode 100644
index 000000000000..1df274091863
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-04-22-20-13-21.bpo-33334.19UMOC.rst
@@ -0,0 +1,2 @@
+:func:`dis.stack_effect` now supports all defined opcodes including NOP and
+EXTENDED_ARG.
diff --git a/Python/compile.c b/Python/compile.c
index 5f5e65383f91..cc0988f68e25 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -859,6 +859,10 @@ static int
 stack_effect(int opcode, int oparg, int jump)
 {
     switch (opcode) {
+        case NOP:
+        case EXTENDED_ARG:
+            return 0;
+
         /* Stack manipulation */
         case POP_TOP:
             return -1;



More information about the Python-checkins mailing list