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