8000 gh-65052: Prevent pdb from crashing when trying to display objects by gaogaotiantian · Pull Request #110578 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-65052: Prevent pdb from crashing when trying to display objects #110578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Improve error message for _safe_repr
  • Loading branch information
gaogaotiantian committed Oct 11, 2023
commit 605d43c452e900cfcc00b6a834fa186fe54be3b6
16 changes: 8 additions & 8 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ def preloop(self):
if newvalue is not oldvalue and newvalue != oldvalue:
displaying[expr] = newvalue
self.message('display %s: %s [old: %s]' %
(expr, self._safe_repr(newvalue),
self._safe_repr(oldvalue)))
(expr, self._safe_repr(newvalue, expr),
self._safe_repr(oldvalue, expr)))

def _get_tb_and_exceptions(self, tb_or_exc):
"""
Expand Down Expand Up @@ -1461,7 +1461,7 @@ def do_args(self, arg):
for i in range(n):
name = co.co_varnames[i]
if name in dict:
self.message('%s = %s' % (name, self._safe_repr(dict[name])))
self.message('%s = %s' % (name, self._safe_repr(dict[name], name)))
else:
self.message('%s = *** undefined ***' % (name,))
do_a = do_args
Expand All @@ -1475,7 +1475,7 @@ def do_retval(self, arg):
self._print_invalid_arg(arg)
return
if '__return__' in self.curframe_locals:
self.message(self._safe_repr(self.curframe_locals['__return__']))
self.message(self._safe_repr(self.curframe_locals['__return__'], "retval"))
else:
self.error('Not yet returned!')
do_rv = do_retval
Expand Down Expand Up @@ -1510,11 +1510,11 @@ def _msg_val_func(self, arg, func):
except:
self._error_exc()

def _safe_repr(self, obj):
def _safe_repr(self, obj, expr):
try:
return repr(obj)
except Exception as e:
return _rstr(f"*** {self._format_exc(e)} ***")
return _rstr(f"*** repr({expr}) failed: {self._format_exc(e)} ***")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use f-strings or % in this script? Or a mixture of the two?

10000 Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using f-strings for all new code. There are plenty of usages with % as pdb is old. Do you think it would be helpful to have a PR to update all strings to f-strings? It would eliminate the mixture.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not worth the risk of breaking something.


def do_p(self, arg):
"""p expression
Expand Down Expand Up @@ -1696,7 +1696,7 @@ def do_display(self, arg):
if self.displaying:
self.message('Currently displaying:')
for key, val in self.displaying.get(self.curframe, {}).items():
self.message('%s: %s' % (key, self._safe_repr(val)))
self.message('%s: %s' % (key, self._safe_repr(val, key)))
else:
self.message('No expression is being displayed')
else:
Expand All @@ -1705,7 +1705,7 @@ def do_display(self, arg):
else:
val = self._getval_except(arg)
self.displaying.setdefault(self.curframe, {})[arg] = val
self.message('display %s: %s' % (arg, self._safe_repr(val)))
self.message('display %s: %s' % (arg, self._safe_repr(val, arg)))

complete_display = _complete_expression

Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2383,17 +2383,17 @@ def test_pdb_issue_gh_65052():
> <doctest test.test_pdb.test_pdb_issue_gh_65052[0]>(4)__new__()-><A instance at ...>
-> return object.__new__(cls)
(Pdb) retval
*** AttributeError: 'A' object has no attribute 'a' ***
*** repr(retval) failed: AttributeError: 'A' object has no attribute 'a' ***
(Pdb) continue
> <doctest test.test_pdb.test_pdb_issue_gh_65052[0]>(7)__init__()
-> self.a = 1
(Pdb) args
self = *** AttributeError: 'A' object has no attribute 'a' ***
self = *** repr(self) failed: AttributeError: 'A' object has no attribute 'a' ***
(Pdb) display self
display self: *** AttributeError: 'A' object has no attribute 'a' ***
display self: *** repr(self) failed: AttributeError: 'A' object has no attribute 'a' ***
(P 4F77 db) display
Currently displaying:
self: *** AttributeError: 'A' object has no attribute 'a' ***
self: *** repr(self) failed: AttributeError: 'A' object has no attribute 'a' ***
(Pdb) continue
"""

Expand Down
0