diff --git a/docs/cli-reference.rst b/docs/cli-reference.rst index ece7193..19d8ed9 100644 --- a/docs/cli-reference.rst +++ b/docs/cli-reference.rst @@ -648,6 +648,7 @@ See :ref:`cli_convert`. --drop Drop original column afterwards --no-skip-false Don't skip falsey values -s, --silent Don't show a progress bar + --pdb Open pdb debugger on first error -h, --help Show this message and exit. diff --git a/docs/cli.rst b/docs/cli.rst index 3024992..268b0e3 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -1732,6 +1732,44 @@ This supports nested imports as well, for example to use `ElementTree .../python3.11/xml/etree/ElementTree.py(1338)XML() + -> parser.feed(text) + (Pdb) args + text = 'This is not XML' + parser = + (Pdb) q + +``args`` here shows the arguments to the current function in the stack. The Python `pdb documentation `__ has full details on the other available commands. + .. _cli_convert_complex: Defining a convert() function diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index d5cdb7c..9a571cb 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -16,6 +16,7 @@ import io import itertools import json import os +import pdb import sys import csv as csv_std import tabulate @@ -2901,6 +2902,7 @@ def _generate_convert_help(): @click.option("--drop", is_flag=True, help="Drop original column afterwards") @click.option("--no-skip-false", is_flag=True, help="Don't skip falsey values") @click.option("-s", "--silent", is_flag=True, help="Don't show a progress bar") +@click.option("pdb_", "--pdb", is_flag=True, help="Open pdb debugger on first error") def convert( db_path, table, @@ -2916,6 +2918,7 @@ def convert( drop, no_skip_false, silent, + pdb_, ): sqlite3.enable_callback_tracebacks(True) db = sqlite_utils.Database(db_path) @@ -2968,6 +2971,19 @@ def convert( ) click.echo("Would affect {} row{}".format(count, "" if count == 1 else "s")) else: + # Wrap fn with a thing that will catch errors and optionally drop into pdb + if pdb_: + fn_ = fn + + def wrapped_fn(value): + try: + return fn_(value) + except Exception as ex: + print("\nException raised, dropping into pdb...:", ex) + pdb.post_mortem(ex.__traceback__) + sys.exit(1) + + fn = wrapped_fn try: db[table].convert( columns,