| 1744 | |
| 1745 | |
| 1746 | def main(): |
| 1747 | import getopt |
| 1748 | |
| 1749 | opts, args = getopt.getopt(sys.argv[1:], 'mhc:', ['help', 'command=']) |
| 1750 | |
| 1751 | if not args: |
| 1752 | print(_usage) |
| 1753 | sys.exit(2) |
| 1754 | |
| 1755 | if any(opt in ['-h', '--help'] for opt, optarg in opts): |
| 1756 | print(_usage) |
| 1757 | sys.exit() |
| 1758 | |
| 1759 | commands = [optarg for opt, optarg in opts if opt in ['-c', '--command']] |
| 1760 | |
| 1761 | module_indicated = any(opt in ['-m'] for opt, optarg in opts) |
| 1762 | cls = _ModuleTarget if module_indicated else _ScriptTarget |
| 1763 | target = cls(args[0]) |
| 1764 | |
| 1765 | target.check() |
| 1766 | |
| 1767 | sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list |
| 1768 | |
| 1769 | # Note on saving/restoring sys.argv: it's a good idea when sys.argv was |
| 1770 | # modified by the script being debugged. It's a bad idea when it was |
| 1771 | # changed by the user from the command line. There is a "restart" command |
| 1772 | # which allows explicit specification of command line arguments. |
| 1773 | pdb = Pdb() |
| 1774 | pdb.rcLines.extend(commands) |
| 1775 | while True: |
| 1776 | try: |
| 1777 | pdb._run(target) |
| 1778 | if pdb._user_requested_quit: |
| 1779 | break |
| 1780 | print("The program finished and will be restarted") |
| 1781 | except Restart: |
| 1782 | print("Restarting", target, "with arguments:") |
| 1783 | print("\t" + " ".join(sys.argv[1:])) |
| 1784 | except SystemExit: |
| 1785 | # In most cases SystemExit does not warrant a post-mortem session. |
| 1786 | print("The program exited via sys.exit(). Exit status:", end=' ') |
| 1787 | print(sys.exc_info()[1]) |
| 1788 | except SyntaxError: |
| 1789 | traceback.print_exc() |
| 1790 | sys.exit(1) |
| 1791 | except: |
| 1792 | traceback.print_exc() |
| 1793 | print("Uncaught exception. Entering post mortem debugging") |
| 1794 | print("Running 'cont' or 'step' will restart the program") |
| 1795 | t = sys.exc_info()[2] |
| 1796 | pdb.interaction(None, t) |
| 1797 | print("Post mortem debugger finished. The " + target + |
| 1798 | " will be restarted") |
| 1799 | |
| 1800 | |
| 1801 | # When invoked as main program, invoke the debugger on a script |