A specialized version of the python debugger that redirects stdout to a given stream when interacting with the user. Stdout is *not* redirected when traced code is executed.
| 373 | return msg[start: end] |
| 374 | |
| 375 | class _OutputRedirectingPdb(pdb.Pdb): |
| 376 | """ |
| 377 | A specialized version of the python debugger that redirects stdout |
| 378 | to a given stream when interacting with the user. Stdout is *not* |
| 379 | redirected when traced code is executed. |
| 380 | """ |
| 381 | def __init__(self, out): |
| 382 | self.__out = out |
| 383 | self.__debugger_used = False |
| 384 | # do not play signal games in the pdb |
| 385 | super().__init__(stdout=out, nosigint=True) |
| 386 | # still use input() to get user input |
| 387 | self.use_rawinput = 1 |
| 388 | |
| 389 | def set_trace(self, frame=None, *, commands=None): |
| 390 | self.__debugger_used = True |
| 391 | if frame is None: |
| 392 | frame = sys._getframe().f_back |
| 393 | pdb.Pdb.set_trace(self, frame, commands=commands) |
| 394 | |
| 395 | def set_continue(self): |
| 396 | # Calling set_continue unconditionally would break unit test |
| 397 | # coverage reporting, as Bdb.set_continue calls sys.settrace(None). |
| 398 | if self.__debugger_used: |
| 399 | pdb.Pdb.set_continue(self) |
| 400 | |
| 401 | def trace_dispatch(self, *args): |
| 402 | # Redirect stdout to the given stream. |
| 403 | save_stdout = sys.stdout |
| 404 | sys.stdout = self.__out |
| 405 | # Call Pdb's trace dispatch method. |
| 406 | try: |
| 407 | return pdb.Pdb.trace_dispatch(self, *args) |
| 408 | finally: |
| 409 | sys.stdout = save_stdout |
| 410 | |
| 411 | # [XX] Normalize with respect to os.path.pardir? |
| 412 | def _module_relative_path(module, test_path): |