Cleanup unnecessary curframe_locals usage (#124369)

This commit is contained in:
Tian Gao 2024-09-26 09:35:13 -07:00 committed by GitHub
parent d7248cdbc3
commit 986a4e1b6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 19 deletions

View File

@ -403,13 +403,6 @@ class Pdb(bdb.Bdb, cmd.Cmd):
self.tb_lineno[tb.tb_frame] = lineno self.tb_lineno[tb.tb_frame] = lineno
tb = tb.tb_next tb = tb.tb_next
self.curframe = self.stack[self.curindex][0] self.curframe = self.stack[self.curindex][0]
# The f_locals dictionary used to be updated from the actual frame
# locals whenever the .f_locals accessor was called, so it was
# cached here to ensure that modifications were not overwritten. While
# the caching is no longer required now that f_locals is a direct proxy
# on optimized frames, it's also harmless, so the code structure has
# been left unchanged.
self.curframe_locals = self.curframe.f_locals
self.set_convenience_variable(self.curframe, '_frame', self.curframe) self.set_convenience_variable(self.curframe, '_frame', self.curframe)
if self._chained_exceptions: if self._chained_exceptions:
@ -732,7 +725,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
def default(self, line): def default(self, line):
if line[:1] == '!': line = line[1:].strip() if line[:1] == '!': line = line[1:].strip()
locals = self.curframe_locals locals = self.curframe.f_locals
globals = self.curframe.f_globals globals = self.curframe.f_globals
try: try:
buffer = line buffer = line
@ -960,7 +953,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
# Collect globals and locals. It is usually not really sensible to also # Collect globals and locals. It is usually not really sensible to also
# complete builtins, and they clutter the namespace quite heavily, so we # complete builtins, and they clutter the namespace quite heavily, so we
# leave them out. # leave them out.
ns = {**self.curframe.f_globals, **self.curframe_locals} ns = {**self.curframe.f_globals, **self.curframe.f_locals}
if text.startswith("$"): if text.startswith("$"):
# Complete convenience variables # Complete convenience variables
conv_vars = self.curframe.f_globals.get('__pdb_convenience_variables', {}) conv_vars = self.curframe.f_globals.get('__pdb_convenience_variables', {})
@ -991,7 +984,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
# Use rlcompleter to do the completion # Use rlcompleter to do the completion
state = 0 state = 0
matches = [] matches = []
completer = Completer(self.curframe.f_globals | self.curframe_locals) completer = Completer(self.curframe.f_globals | self.curframe.f_locals)
while (match := completer.complete(text, state)) is not None: while (match := completer.complete(text, state)) is not None:
matches.append(match) matches.append(match)
state += 1 state += 1
@ -1153,7 +1146,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
try: try:
func = eval(arg, func = eval(arg,
self.curframe.f_globals, self.curframe.f_globals,
self.curframe_locals) self.curframe.f_locals)
except: except:
func = arg func = arg
try: try:
@ -1458,7 +1451,6 @@ class Pdb(bdb.Bdb, cmd.Cmd):
assert 0 <= number < len(self.stack) assert 0 <= number < len(self.stack)
self.curindex = number self.curindex = number
self.curframe = self.stack[self.curindex][0] self.curframe = self.stack[self.curindex][0]
self.curframe_locals = self.curframe.f_locals
self.set_convenience_variable(self.curframe, '_frame', self.curframe) self.set_convenience_variable(self.curframe, '_frame', self.curframe)
self.print_stack_entry(self.stack[self.curindex]) self.print_stack_entry(self.stack[self.curindex])
self.lineno = None self.lineno = None
@ -1704,7 +1696,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
""" """
sys.settrace(None) sys.settrace(None)
globals = self.curframe.f_globals globals = self.curframe.f_globals
locals = self.curframe_locals locals = self.curframe.f_locals
p = Pdb(self.completekey, self.stdin, self.stdout) p = Pdb(self.completekey, self.stdin, self.stdout)
p.prompt = "(%s) " % self.prompt.strip() p.prompt = "(%s) " % self.prompt.strip()
self.message("ENTERING RECURSIVE DEBUGGER") self.message("ENTERING RECURSIVE DEBUGGER")
@ -1749,7 +1741,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
self._print_invalid_arg(arg) self._print_invalid_arg(arg)
return return
co = self.curframe.f_code co = self.curframe.f_code
dict = self.curframe_locals dict = self.curframe.f_locals
n = co.co_argcount + co.co_kwonlyargcount n = co.co_argcount + co.co_kwonlyargcount
if co.co_flags & inspect.CO_VARARGS: n = n+1 if co.co_flags & inspect.CO_VARARGS: n = n+1
if co.co_flags & inspect.CO_VARKEYWORDS: n = n+1 if co.co_flags & inspect.CO_VARKEYWORDS: n = n+1
@ -1769,15 +1761,15 @@ class Pdb(bdb.Bdb, cmd.Cmd):
if arg: if arg:
self._print_invalid_arg(arg) self._print_invalid_arg(arg)
return return
if '__return__' in self.curframe_locals: if '__return__' in self.curframe.f_locals:
self.message(self._safe_repr(self.curframe_locals['__return__'], "retval")) self.message(self._safe_repr(self.curframe.f_locals['__return__'], "retval"))
else: else:
self.error('Not yet returned!') self.error('Not yet returned!')
do_rv = do_retval do_rv = do_retval
def _getval(self, arg): def _getval(self, arg):
try: try:
return eval(arg, self.curframe.f_globals, self.curframe_locals) return eval(arg, self.curframe.f_globals, self.curframe.f_locals)
except: except:
self._error_exc() self._error_exc()
raise raise
@ -1785,7 +1777,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
def _getval_except(self, arg, frame=None): def _getval_except(self, arg, frame=None):
try: try:
if frame is None: if frame is None:
return eval(arg, self.curframe.f_globals, self.curframe_locals) return eval(arg, self.curframe.f_globals, self.curframe.f_locals)
else: else:
return eval(arg, frame.f_globals, frame.f_locals) return eval(arg, frame.f_globals, frame.f_locals)
except BaseException as exc: except BaseException as exc:
@ -2029,7 +2021,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
Start an interactive interpreter whose global namespace Start an interactive interpreter whose global namespace
contains all the (global and local) names found in the current scope. contains all the (global and local) names found in the current scope.
""" """
ns = {**self.curframe.f_globals, **self.curframe_locals} ns = {**self.curframe.f_globals, **self.curframe.f_locals}
console = _PdbInteractiveConsole(ns, message=self.message) console = _PdbInteractiveConsole(ns, message=self.message)
console.interact(banner="*pdb interact start*", console.interact(banner="*pdb interact start*",
exitmsg="*exit from pdb interact command*") exitmsg="*exit from pdb interact command*")