[issue37022] pdb: do_p and do_pp swallow exceptions from __repr__

daniel hahler report at bugs.python.org
Thu May 23 12:02:14 EDT 2019


New submission from daniel hahler <python-bugs at thequod.de>:

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
```

----------
components: Library (Lib)
messages: 343306
nosy: blueyed
priority: normal
severity: normal
status: open
title: pdb: do_p and do_pp swallow exceptions from __repr__
type: behavior
versions: Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37022>
_______________________________________


More information about the Python-bugs-list mailing list