本文整理匯總了Python中sys.__excepthook__方法的典型用法代碼示例。如果您正苦於以下問題:Python sys.__excepthook__方法的具體用法?Python sys.__excepthook__怎麽用?Python sys.__excepthook__使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sys
的用法示例。
在下文中一共展示了sys.__excepthook__方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: log_excepthook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def log_excepthook(exc_class, exc_value, tb):
import traceback
tb_txt = ''.join(traceback.format_tb(tb))
try:
(filename, number, function, line_text) = traceback.extract_tb(tb)[-1]
exc_txt = "{} line {} function {} [{}]".format(
filename, number, function, line_text)
except Exception:
exc_txt = ""
logger.error("Unhandled exception '{}' at {}"
.format(exc_value, exc_txt),
traceback=tb_txt,
exc_class=repr(exc_class),
exc_value=repr(exc_value))
sys.__excepthook__(exc_class, exc_value, tb)
示例2: log_excepthook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def log_excepthook(exc_class, exc_value, tb):
# at this point we have substituted the module once,
# so we need to import all modules again
import traceback
import kano.logging # Don't think about this one too hard...
import sys
tb_txt = ''.join(traceback.format_tb(tb))
try:
(filename, number, function, line_text) = traceback.extract_tb(tb)[-1]
exc_txt = "{} line {} function {} [{}]".format(
filename, number, function, line_text)
except Exception:
exc_txt = ""
kano.logging.logger.error(
"Unhandled exception '{}' at {} (see logfile for full trace)".format(
exc_value, exc_txt
),
traceback=tb_txt,
exc_class=str(exc_class),
exc_value=str(exc_value)
)
sys.__excepthook__(exc_class, exc_value, tb)
示例3: test_unhandled
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def test_unhandled(self):
# Check for sensible reporting of unhandled exceptions
for exc_type in (ValueError, BrokenStrException):
try:
exc = exc_type("test message")
# The following line is included in the traceback report:
raise exc
except exc_type:
with captured_stderr() as stderr:
sys.__excepthook__(*sys.exc_info())
report = stderr.getvalue()
self.assertIn("test_exceptions.py", report)
self.assertIn("raise exc", report)
self.assertIn(exc_type.__name__, report)
if exc_type is BrokenStrException:
self.assertIn("<exception str() failed>", report)
else:
self.assertIn("test message", report)
self.assertTrue(report.endswith("\n"))
示例4: my_excepthook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def my_excepthook(type, value, tback):
"""When an exception occurs: Log the error and continue (doesn't kill the
GUI)
Parameters
----------
type :
value :
tback :
Returns
-------
"""
# log the exception here (Not needed with stderr redirect)
# then call the default handler
sys.__excepthook__(type, value, tback)
示例5: uncaught_exception_handler
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def uncaught_exception_handler(exception_type, exception_value,
exception_traceback):
"""Handles any exception that are uncaught by logging an error and calling
the sys.__excepthook__."""
# Ensure that we are not calling ourself. This shouldn't be needed since we
# are using sys.__excepthook__. Do this check anyway since if we are somehow
# calling ourself we might infinitely send errors to the logs, which would be
# quite bad.
global _is_already_handling_uncaught
if _is_already_handling_uncaught:
raise Exception('Loop in uncaught_exception_handler')
_is_already_handling_uncaught = True
# Use emit since log_error needs sys.exc_info() to return this function's
# arguments to call init properly.
# Don't worry about emit() throwing an Exception, python will let us know
# about that exception as well as the original one.
emit(
logging.ERROR,
'Uncaught exception',
exc_info=(exception_type, exception_value, exception_traceback))
sys.__excepthook__(exception_type, exception_value, exception_traceback)
示例6: runcode
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def runcode(self, code):
"""Execute a code object.
When an exception occurs, self.showtraceback() is called to
display a traceback. All exceptions are caught except
SystemExit, which is reraised.
A note about KeyboardInterrupt: this exception may occur
elsewhere in this code, and may not always be caught. The
caller should be prepared to deal with it.
"""
try:
Exec(code, self.frame.f_globals, self.frame.f_locals)
pydevd_save_locals.save_locals(self.frame)
except SystemExit:
raise
except:
# In case sys.excepthook called, use original excepthook #PyDev-877: Debug console freezes with Python 3.5+
# (showtraceback does it on python 3.5 onwards)
sys.excepthook = sys.__excepthook__
try:
self.showtraceback()
finally:
sys.__excepthook__ = sys.excepthook
示例7: register_debug_hook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def register_debug_hook():
import traceback
def info(type, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
sys.__excepthook__(type, value, tb)
else:
try:
import ipdb as pdb_api
except ImportError:
import pdb as pdb_api
traceback.print_exception(type, value, tb)
pdb_api.post_mortem(tb)
sys.excepthook = info
logging.basicConfig(level="DEBUG")
示例8: setup_logging
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def setup_logging(self):
from logbook.compat import redirect_logging
from logbook import INFO, Logger
redirect_logging()
self.components['log'].handler.level = INFO
self.components['log'].handler.push_application()
self._logger = Logger(self.name)
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
self._logger.error("Uncaught exception occurred",
exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = handle_exception
示例9: showtraceback
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def showtraceback(self):
"""Display the exception that just occurred.
We remove the first stack item because it is our own code.
The output is written by self.write(), below.
"""
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
sys.last_traceback = last_tb
try:
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
if sys.excepthook is sys.__excepthook__:
self.write(''.join(lines))
else:
# If someone has set sys.excepthook, we let that take precedence
# over self.write
sys.excepthook(ei[0], ei[1], last_tb)
finally:
last_tb = ei = None
示例10: interactive_exception
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def interactive_exception(e_class, e_value, tb):
sys.__excepthook__(e_class, e_value, tb)
tb_stack = extract_tb(tb)
locals_stack = []
while tb is not None:
locals_stack.append(tb.tb_frame.f_locals)
tb = tb.tb_next
while len(tb_stack) > 0:
frame = tb_stack.pop()
ls = locals_stack.pop()
print('\nInterpreter at file "{}", line {}, in {}:'.format(
frame.filename, frame.lineno, frame.name))
print(' {}'.format(frame.line.strip()))
interact(local=ls)
#sys.excepthook = interactive_exception
# check dirs
示例11: std_exceptions
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def std_exceptions(etype, value, tb):
sys.excepthook = sys.__excepthook__
if issubclass(etype, KeyboardInterrupt):
pass
elif issubclass(etype, IOError) and value.errno == errno.EPIPE:
pass
else:
sys.__excepthook__(etype, value, tb)
示例12: didyoumean_disablehook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def didyoumean_disablehook():
"""Function to set hooks to their normal value."""
sys.excepthook = sys.__excepthook__
set_ipython_custom_exc(None)
# NOTE: It could be funny to have a magic command in Python
# https://ipython.org/ipython-doc/dev/config/custommagics.html
示例13: register_excepthook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def register_excepthook(logger):
def log_excep(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
logger.debug(
"Traceback",
exc_info=(exc_type, exc_value, exc_traceback)
)
logger.critical(
"Uncaught exception",
exc_info=(exc_type, exc_value, None)
)
sys.excepthook = log_excep
示例14: except_hook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def except_hook(cls, exception, traceback):
QtWidgets.QMessageBox.critical(None, 'SimNIBS GUI Error', str(exception))
sys.__excepthook__(cls, exception, traceback)
#sys.exit(app.exec_())
示例15: debug_hook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import __excepthook__ [as 別名]
def debug_hook(type_, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
sys.__excepthook__(type_, value, tb)
else:
import traceback
import pdb
traceback.print_exception(type_, value, tb)
print(u"\n")
pdb.pm()