@@ -2097,10 +2097,6 @@ code_returns_only_none(PyCodeObject *co)
20972097 int len = (int )Py_SIZE (co );
20982098 assert (len > 0 );
20992099
2100- // The last instruction either returns or raises. We can take advantage
2101- // of that for a quick exit.
2102- _Py_CODEUNIT final = _Py_GetBaseCodeUnit (co , len - 1 );
2103-
21042100 // Look up None in co_consts.
21052101 Py_ssize_t nconsts = PyTuple_Size (co -> co_consts );
21062102 int none_index = 0 ;
@@ -2113,29 +2109,22 @@ code_returns_only_none(PyCodeObject *co)
21132109 // None wasn't there, which means there was no implicit return,
21142110 // "return", or "return None".
21152111
2116- // That means there must be
2117- // an explicit return (non-None), or it only raises.
2112+ // The last instruction mostly either returns or raises.
2113+ // We can take advantage of that for a quick exit.
2114+ _Py_CODEUNIT final = _Py_GetBaseCodeUnit (co , len - 1 );
21182115 if (IS_RETURN_OPCODE (final .op .code )) {
21192116 // It was an explicit return (non-None).
21202117 return 0 ;
21212118 }
2122- // It must end with a raise then. We still have to walk the
2123- // bytecode to see if there's any explicit return (non-None).
2124- assert (IS_RAISE_OPCODE (final .op .code ));
2125- for (int i = 0 ; i < len ; i += _PyInstruction_GetLength (co , i )) {
2126- _Py_CODEUNIT inst = _Py_GetBaseCodeUnit (co , i );
2127- if (IS_RETURN_OPCODE (inst .op .code )) {
2128- // We alraedy know it isn't returning None.
2129- return 0 ;
2130- }
2131- }
2132- // It must only raise.
2119+
2120+ none_index = -1 ;
21332121 }
2134- else {
2135- // Walk the bytecode, looking for RETURN_VALUE.
2136- for (int i = 0 ; i < len ; i += _PyInstruction_GetLength (co , i )) {
2137- _Py_CODEUNIT inst = _Py_GetBaseCodeUnit (co , i );
2138- if (IS_RETURN_OPCODE (inst .op .code )) {
2122+
2123+ // Walk the bytecode, looking for RETURN_VALUE.
2124+ for (int i = 0 ; i < len ; i += _PyInstruction_GetLength (co , i )) {
2125+ _Py_CODEUNIT inst = _Py_GetBaseCodeUnit (co , i );
2126+ if (IS_RETURN_OPCODE (inst .op .code )) {
2127+ if (none_index >= 0 ) {
21392128 assert (i != 0 );
21402129 // Ignore it if it returns None.
21412130 _Py_CODEUNIT prev = _Py_GetBaseCodeUnit (co , i - 1 );
@@ -2145,8 +2134,8 @@ code_returns_only_none(PyCodeObject *co)
21452134 continue ;
21462135 }
21472136 }
2148- return 0 ;
21492137 }
2138+ return 0 ;
21502139 }
21512140 }
21522141 return 1 ;
0 commit comments