Don't remove LOAD_CONST RETURN_VALUE when...

the LOAD_CONST has a non-None value, or the LOAD_CONST
has a line associated with it.
This commit is contained in:
rocky
2024-11-28 07:27:13 -05:00
parent addddf82f5
commit 4ac5564df3
2 changed files with 16 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016-2021, 2023 by Rocky Bernstein # Copyright (c) 2016-2021, 2023-2024 by Rocky Bernstein
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com> # Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 1999 John Aycock # Copyright (c) 1999 John Aycock
# #

View File

@@ -1251,11 +1251,21 @@ class SourceWalker(GenericASTTraversal, NonterminalActions, ComprehensionMixin):
if tokens[-1].kind in ("RETURN_VALUE", "RETURN_VALUE_LAMBDA"): if tokens[-1].kind in ("RETURN_VALUE", "RETURN_VALUE_LAMBDA"):
# Python 3.4's classes can add a "return None" which is # Python 3.4's classes can add a "return None" which is
# invalid syntax. # invalid syntax.
if tokens[-2].kind == "LOAD_CONST": load_const = tokens[-2]
if is_top_level_module or tokens[-2].pattr is None: # We should have:
del tokens[-2:] # LOAD_CONST None
else: # with *no* line number associated the token.
tokens.append(Token("RETURN_LAST")) # A line number on the token or a non-None
# token value a token based on user source
# text.
if (
load_const.kind == "LOAD_CONST"
and load_const.linestart is None
and load_const.attr is None
or is_top_level_module
):
# Delete LOAD_CONST (None) RETURN_VALUE
del tokens[-2:]
else: else:
tokens.append(Token("RETURN_LAST")) tokens.append(Token("RETURN_LAST"))
if len(tokens) == 0: if len(tokens) == 0: