Skip to content

Commit 55e09c5

Browse files
authored
Merge pull request #842 from nipreps/enh/pdb-utility
ENH: Upstream debugging hook from MRIQC
2 parents fe9052e + d8390b8 commit 55e09c5

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

niworkflows/utils/debug.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
#
4+
# Copyright 2021 The NiPreps Developers <[email protected]>
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# We support and encourage derived works from this project, please read
19+
# about our expectations at
20+
#
21+
# https://www.nipreps.org/community/licensing/
22+
#
23+
# STATEMENT OF CHANGES: This file is derived from sources licensed under the Apache-2.0 terms,
24+
# and uses the following portion of the original code:
25+
# https://github.com/dandi/dandi-cli/blob/da3b7a726c4a352dfb53a0c6bee59e660de827e6/dandi/utils.py#L49-L82
26+
#
27+
#
28+
# ORIGINAL WORK'S ATTRIBUTION NOTICE:
29+
#
30+
# Copyright 2020 DANDI Client Developers
31+
#
32+
# Licensed under the Apache License, Version 2.0 (the "License");
33+
# you may not use this file except in compliance with the License.
34+
# You may obtain a copy of the License at
35+
#
36+
# http://www.apache.org/licenses/LICENSE-2.0
37+
#
38+
# Unless required by applicable law or agreed to in writing, software
39+
# distributed under the License is distributed on an "AS IS" BASIS,
40+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41+
# See the License for the specific language governing permissions and
42+
# limitations under the License.
43+
#
44+
import sys
45+
46+
47+
def is_interactive():
48+
"""Return True if all in/outs are tty"""
49+
# TODO: check on windows if hasattr check would work correctly and add value:
50+
#
51+
return sys.stdin.isatty() and sys.stdout.isatty() and sys.stderr.isatty()
52+
53+
54+
def setup_exceptionhook(ipython=False):
55+
"""Overloads default sys.excepthook with our exceptionhook handler.
56+
If interactive, our exceptionhook handler will invoke
57+
pdb.post_mortem; if not interactive, then invokes default handler.
58+
"""
59+
60+
def _pdb_excepthook(type, value, tb):
61+
import traceback
62+
63+
traceback.print_exception(type, value, tb)
64+
print()
65+
if is_interactive():
66+
import pdb
67+
68+
pdb.post_mortem(tb)
69+
70+
if ipython:
71+
from IPython.core import ultratb
72+
73+
sys.excepthook = ultratb.FormattedTB(
74+
mode="Verbose",
75+
# color_scheme='Linux',
76+
call_pdb=is_interactive(),
77+
)
78+
else:
79+
sys.excepthook = _pdb_excepthook

0 commit comments

Comments
 (0)