From 6408ef5a98c402e390adb736adc2b824883a5d0a Mon Sep 17 00:00:00 2001 From: Evan Sultanik Date: Thu, 13 Jan 2022 10:33:05 -0500 Subject: [PATCH] Adds a command line option to not debug with PDB --- polyfile/__main__.py | 7 ++++++- polyfile/magic_debugger.py | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/polyfile/__main__.py b/polyfile/__main__.py index 171be146..55dc9338 100644 --- a/polyfile/__main__.py +++ b/polyfile/__main__.py @@ -58,6 +58,9 @@ def main(argv=None): parser.add_argument('--trace', '-dd', action='store_true', help='print extra verbose debug information') parser.add_argument('--debugger', '-db', action='store_true', help='drop into an interactive debugger for libmagic ' 'file definition matching') + parser.add_argument('--no-debug-python', action='store_true', help='by default, the `--debugger` option will break ' + 'on custom matchers and prompt to debug using ' + 'PDB. This option will suppress those prompts.') parser.add_argument('--quiet', '-q', action='store_true', help='suppress all log output (overrides --debug)') parser.add_argument('--version', '-v', action='store_true', help='print PolyFile\'s version information to STDERR') parser.add_argument('-dumpversion', action='store_true', @@ -116,7 +119,9 @@ def main(argv=None): with path_or_stdin as file_path, ExitStack() as stack: if args.debugger: - stack.enter_context(Debugger()) + stack.enter_context(Debugger(break_on_submatching=not args.no_debug_python)) + elif args.no_debug_python: + log.warning("Ignoring `--no-debug-python`; it can only be used with the --debugger option.") matches = [] try: if args.only_match_mime: diff --git a/polyfile/magic_debugger.py b/polyfile/magic_debugger.py index 73c17af5..241095a1 100644 --- a/polyfile/magic_debugger.py +++ b/polyfile/magic_debugger.py @@ -2,7 +2,7 @@ from enum import Enum from pdb import Pdb import sys -from typing import Any, Callable, Iterator, List, Optional, Type, TypeVar, Union +from typing import Any, Callable, ContextManager, Iterator, List, Optional, Type, TypeVar, Union from .polyfile import __copyright__, __license__, __version__, CUSTOM_MATCHERS, Match, Submatch from .logger import getStatusLogger @@ -241,7 +241,7 @@ class StepMode(Enum): NEXT = 2 -class Debugger: +class Debugger(ContextManager["Debugger"]): def __init__(self, break_on_submatching: bool = True): self.instrumented_tests: List[InstrumentedTest] = [] self.breakpoints: List[Breakpoint] = []