From ca5c405d0fd2c68de96e0b9781e7ec63c76b565c Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 11 Sep 2020 11:37:55 -0700 Subject: [PATCH] New 'datasette --pdb' option, closes #962 --- datasette/app.py | 7 +++++++ datasette/cli.py | 3 +++ docs/datasette-serve-help.txt | 1 + tests/test_cli.py | 1 + 4 files changed, 12 insertions(+) diff --git a/datasette/app.py b/datasette/app.py index a6b1d854..6beb4549 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -191,10 +191,12 @@ class Datasette: secret=None, version_note=None, config_dir=None, + pdb=False, ): assert config_dir is None or isinstance( config_dir, Path ), "config_dir= should be a pathlib.Path" + self.pdb = pdb self._secret = secret or secrets.token_hex(32) self.files = tuple(files) + tuple(immutables or []) if config_dir: @@ -1057,6 +1059,11 @@ class DatasetteRouter: await self.handle_500(request, send, exception or NotFound("404")) async def handle_500(self, request, send, exception): + if self.ds.pdb: + import pdb + + pdb.post_mortem(exception.__traceback__) + title = None if isinstance(exception, NotFound): status = 404 diff --git a/datasette/cli.py b/datasette/cli.py index 0c5f68a0..8b67048a 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -352,6 +352,7 @@ def uninstall(packages, yes): ) @click.option("--version-note", help="Additional note to show on /-/versions") @click.option("--help-config", is_flag=True, help="Show available config options") +@click.option("--pdb", is_flag=True, help="Launch debugger on any errors") def serve( files, immutable, @@ -373,6 +374,7 @@ def serve( get, version_note, help_config, + pdb, return_instance=False, ): """Serve up specified SQLite database files with a web UI""" @@ -418,6 +420,7 @@ def serve( memory=memory, secret=secret, version_note=version_note, + pdb=pdb, ) # if files is a single directory, use that as config_dir= diff --git a/docs/datasette-serve-help.txt b/docs/datasette-serve-help.txt index 11b14b2d..536e43b0 100644 --- a/docs/datasette-serve-help.txt +++ b/docs/datasette-serve-help.txt @@ -38,4 +38,5 @@ Options: --version-note TEXT Additional note to show on /-/versions --help-config Show available config options + --pdb Launch debugger on any errors --help Show this message and exit. diff --git a/tests/test_cli.py b/tests/test_cli.py index dc5229cd..b1c50282 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -104,6 +104,7 @@ def test_metadata_yaml(): version_note=None, get=None, help_config=False, + pdb=False, return_instance=True, ) client = _TestClient(ds.app())