classification
Title: pdb: do_p and do_pp swallow exceptions from __repr__
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: blueyed, iritkatriel, miss-islington, remi.lapeyre
Priority: normal Keywords: patch

Created on 2019-05-23 16:02 by blueyed, last changed 2021-06-10 21:24 by iritkatriel. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18180 merged blueyed, 2020-01-25 12:49
PR 26650 merged miss-islington, 2021-06-10 20:32
PR 26651 merged miss-islington, 2021-06-10 20:32
Messages (7)
msg343306 - (view) Author: daniel hahler (blueyed) * Date: 2019-05-23 16:02
Given:

```
class BadRepr:
    def __repr__(self):
        raise Exception('repr_exc')


obj = BadRepr()

__import__('pdb').set_trace()
```

```
(Pdb) p obj
(Pdb) pp obj
(Pdb)
```

Possible patch - clumsy due to `self._getval` both printing any error already, and raising the exception:

```
diff --git i/Lib/pdb.py w/Lib/pdb.py
index f5d33c27fc..59a419d961 100755
--- i/Lib/pdb.py
+++ w/Lib/pdb.py
@@ -1177,18 +1177,28 @@ def do_p(self, arg):
         Print the value of the expression.
         """
         try:
-            self.message(repr(self._getval(arg)))
+            val = self._getval(arg)
         except:
-            pass
+            return
+        try:
+            self.message(repr(val))
+        except:
+            exc_info = sys.exc_info()[:2]
+            self.error(traceback.format_exception_only(*exc_info)[-1].strip())

     def do_pp(self, arg):
         """pp expression
         Pretty-print the value of the expression.
         """
         try:
-            self.message(pprint.pformat(self._getval(arg)))
+            val = self._getval(arg)
         except:
-            pass
+            return
+        try:
+            self.message(pprint.pformat(val))
+        except:
+            exc_info = sys.exc_info()[:2]
+            self.error(traceback.format_exception_only(*exc_info)[-1].strip())

     complete_print = _complete_expression
     complete_p = _complete_expression
```
msg343391 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-05-24 14:42
Hi Daniel, this is indeed unexpected, I don't see how to have a better patch since the fact that _getval() raise an exception is used in do_source() and do_whatis().

Could you convert your patch as a PR and add a test?
msg343392 - (view) Author: daniel hahler (blueyed) * Date: 2019-05-24 14:58
Thanks for the feedback.

What do you think of refactoring the common block (also used in other places) into a method?

+        except:
+            exc_info = sys.exc_info()[:2]
+            self.error(traceback.format_exception_only(*exc_info)[-1].strip())

This could/should be done separately probably then, just want your opinion on it.  This would also allow to customize/override it in a single place then (e.g. with pdbpp I am displaying tracebacks for errors, which is done via error() currently then).
msg343395 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-05-24 15:10
This part of the code is already used in three places and your patch would add two occurrences of of it, I think it would be great to put the part that print the exception in a private method, to avoid duplicating it all over the place.

Doing this seems small enough to me that I think it could be done in the same PR to avoid the overhead of having two separates PRs.
msg395571 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-10 20:32
New changeset 6544b2532df82d137b71323445a07a6e29bcdec0 by Daniel Hahler in branch 'main':
bpo-37022: Fix bug where pdb's do_p/do_pp commands swallow exceptions from repr (GH-18180)
https://github.com/python/cpython/commit/6544b2532df82d137b71323445a07a6e29bcdec0
msg395574 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-10 20:57
New changeset e3bc32fc1ad5537b476b34062b07a040533c913a by Miss Islington (bot) in branch '3.10':
bpo-37022: Fix bug where pdb's do_p/do_pp commands swallow exceptions from repr (GH-18180) (GH-26650)
https://github.com/python/cpython/commit/e3bc32fc1ad5537b476b34062b07a040533c913a
msg395575 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-10 21:24
New changeset 175ebc60d52f2e88cf5cba5224c15074d2623c10 by Miss Islington (bot) in branch '3.9':
bpo-37022: Fix bug where pdb's do_p/do_pp commands swallow exceptions from repr (GH-18180) (GH-26651)
https://github.com/python/cpython/commit/175ebc60d52f2e88cf5cba5224c15074d2623c10
History
Date User Action Args
2021-06-10 21:24:31iritkatrielsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-06-10 21:24:16iritkatrielsetmessages: + msg395575
2021-06-10 20:57:00iritkatrielsetmessages: + msg395574
2021-06-10 20:32:20miss-islingtonsetpull_requests: + pull_request25237
2021-06-10 20:32:15miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request25236
2021-06-10 20:32:12iritkatrielsetnosy: + iritkatriel
messages: + msg395571
2021-06-10 15:12:53iritkatrielsetversions: + Python 3.10, Python 3.11, - Python 3.7, Python 3.8
2020-01-25 12:49:41blueyedsetkeywords: + patch
stage: patch review
pull_requests: + pull_request17564
2019-05-24 15:10:12remi.lapeyresetmessages: + msg343395
2019-05-24 14:58:58blueyedsetmessages: + msg343392
2019-05-24 14:42:14remi.lapeyresetnosy: + remi.lapeyre
messages: + msg343391
2019-05-23 16:02:14blueyedcreate