Skip to content

Commit 99cc99f

Browse files
move debug_exceptions function to loggers
Signed-off-by: thiswillbeyourgithub <[email protected]>
1 parent 847a4a2 commit 99cc99f

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

wdoc/utils/logger.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import sys
66
import warnings
7+
import pdb
8+
import traceback
9+
import faulthandler
710
from pathlib import Path
811
from textwrap import dedent
912

@@ -133,3 +136,46 @@ def set_parse_doc_help_md_as_docstring(
133136
parsedochelp = "Parse doc help documentation is empty. Please refer to online documentation."
134137
obj.__doc__ = "# Content of wdoc/docs/parse_doc_help.md\n\n" + parsedochelp
135138
return obj
139+
140+
141+
def debug_exceptions(instance: Optional["wdoc"] = None) -> None:
142+
"open a debugger if --debug is set"
143+
144+
def handle_exception(exc_type, exc_value, exc_traceback):
145+
if not issubclass(exc_type, KeyboardInterrupt):
146+
147+
def p(message: str) -> None:
148+
"print error, in red if possible"
149+
if instance:
150+
logger.exception(instance.ntfy(message))
151+
else:
152+
try:
153+
logger.warning(message)
154+
except Exception:
155+
print(message)
156+
157+
p(
158+
"\n--verbose was used so opening debug console at the "
159+
"appropriate frame. Press 'c' to continue to the frame "
160+
"of this print."
161+
)
162+
p(
163+
"Please open an issue on github and include the trace. It's "
164+
"tremendously useful for me as there are many small bugs that "
165+
"can be quickly squashed if users just told me about it :)"
166+
)
167+
[p(line) for line in traceback.format_tb(exc_traceback)]
168+
p(str(exc_type) + " : " + str(exc_value))
169+
if hasattr(exc_value, "__cause__") and hasattr(
170+
exc_value.__cause__, "__traceback__"
171+
):
172+
p("Detected a cause to the exception, opening the cause first")
173+
pdb.post_mortem(exc_value.__cause__.__traceback__)
174+
p("Out of the __cause__, now debugging the higher traceback:")
175+
pdb.post_mortem(exc_traceback)
176+
p("You are now in the exception handling frame.")
177+
breakpoint()
178+
sys.exit(1)
179+
180+
sys.excepthook = handle_exception
181+
faulthandler.enable()

wdoc/wdoc.py

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import inspect
99
import json
1010
import os
11-
import pdb
1211
import re
1312
import sys
1413
import time
@@ -54,6 +53,7 @@
5453
md_printer,
5554
set_help_md_as_docstring,
5655
set_parse_doc_help_md_as_docstring,
56+
debug_exceptions,
5757
)
5858
from wdoc.utils.misc import ( # debug_chain,
5959
cache_dir,
@@ -1857,46 +1857,3 @@ def parse_doc(
18571857
f.write(file_content)
18581858

18591859
return result
1860-
1861-
1862-
def debug_exceptions(instance: Optional[wdoc] = None) -> None:
1863-
"open a debugger if --debug is set"
1864-
1865-
def handle_exception(exc_type, exc_value, exc_traceback):
1866-
if not issubclass(exc_type, KeyboardInterrupt):
1867-
1868-
def p(message: str) -> None:
1869-
"print error, in red if possible"
1870-
if instance:
1871-
logger.exception(instance.ntfy(message))
1872-
else:
1873-
try:
1874-
logger.warning(message)
1875-
except Exception:
1876-
print(message)
1877-
1878-
p(
1879-
"\n--verbose was used so opening debug console at the "
1880-
"appropriate frame. Press 'c' to continue to the frame "
1881-
"of this print."
1882-
)
1883-
p(
1884-
"Please open an issue on github and include the trace. It's "
1885-
"tremendously useful for me as there are many small bugs that "
1886-
"can be quickly squashed if users just told me about it :)"
1887-
)
1888-
[p(line) for line in traceback.format_tb(exc_traceback)]
1889-
p(str(exc_type) + " : " + str(exc_value))
1890-
if hasattr(exc_value, "__cause__") and hasattr(
1891-
exc_value.__cause__, "__traceback__"
1892-
):
1893-
p("Detected a cause to the exception, opening the cause first")
1894-
pdb.post_mortem(exc_value.__cause__.__traceback__)
1895-
p("Out of the __cause__, now debugging the higher traceback:")
1896-
pdb.post_mortem(exc_traceback)
1897-
p("You are now in the exception handling frame.")
1898-
breakpoint()
1899-
sys.exit(1)
1900-
1901-
sys.excepthook = handle_exception
1902-
faulthandler.enable()

0 commit comments

Comments
 (0)