本文整理汇总了Python中tornado.util.raise_exc_info函数的典型用法代码示例。如果您正苦于以下问题:Python raise_exc_info函数的具体用法?Python raise_exc_info怎么用?Python raise_exc_info使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了raise_exc_info函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
def handle(request):
# kwargs for this function's response_cls constructor
response_kwargs = {}
status = OK
try:
response = yield gen.maybe_future(handler(request))
except Exception as e:
response = Response()
for exc_spec in response_spec.exception_specs:
# Each exc_spec is a thriftrw.spec.FieldSpec. The spec
# attribute on that is the TypeSpec for the Exception class
# and the surface on the TypeSpec is the exception class.
exc_cls = exc_spec.spec.surface
if isinstance(e, exc_cls):
status = FAILED
response_kwargs[exc_spec.name] = e
break
else:
raise_exc_info(sys.exc_info())
else:
response = response_from_mixed(response)
if response_spec.return_spec is not None:
assert response.body is not None, (
'Expected a value to be returned for %s, '
'but recieved None - only void procedures can '
'return None.' % function.endpoint
)
response_kwargs['success'] = response.body
response.status = status
response.body = response_cls(**response_kwargs)
raise gen.Return(response)
示例2: wrapper
def wrapper(*args, **kwargs):
future = Future()
callback, args, kwargs = replacer.replace(future.set_result,
args, kwargs)
if callback is not None:
future.add_done_callback(callback)
def handle_error(typ, value, tb):
future.set_exception(value)
return True
exc_info = None
with ExceptionStackContext(handle_error):
result = None
try:
result = f(*args, **kwargs)
except:
exc_info = sys.exc_info()
assert result is None, ("@return_future should not be used with "
"functions that return values")
if exc_info is not None:
# If the initial synchronous part of f() raised an exception,
# go ahead and raise it to the caller directly without waiting
# for them to inspect the Future.
raise_exc_info(exc_info)
return future
示例3: wrapper
def wrapper(*args, **kwargs):
future = Future()
callback, args, kwargs = replacer.replace(future.set_result,
args, kwargs)
def handle_error(typ, value, tb):
future.set_exception(value)
return True
exc_info = None
with ExceptionStackContext(handle_error):
try:
result = f(*args, **kwargs)
if result is not None:
raise ReturnValueIgnoredError(
"@return_future should not be used with functions "
"that return values")
except:
exc_info = sys.exc_info()
raise
if exc_info is not None:
# If the initial synchronous part of f() raised an exception,
# go ahead and raise it to the caller directly without waiting
# for them to inspect the Future.
raise_exc_info(exc_info)
# If the caller passed in a callback, schedule it to be called
# when the future resolves. It is important that this happens
# just before we return the future, or else we risk confusing
# stack contexts with multiple exceptions (one here with the
# immediate exception, and again when the future resolves and
# the callback triggers its exception by calling future.result()).
if callback is not None:
future.add_done_callback(wrap(callback))
return future
示例4: _nested
def _nested(*managers):
""" 支持将多个contextmanager放到单个with语句中。
Copied from the python 2.6 standard library. It's no longer present in python 3 because the with statement natively supports multiple
context managers, but that doesn't help if the list of context managers is not known until runtime. """
exits = []
vars = []
exc = (None, None, None)
try:
for mgr in managers:
exit = mgr.__exit__
enter = mgr.__enter__
vars.append(enter()) # 依次对所有的manager调用__enter__并把结果放入vars中
exits.append(exit)
yield vars # 一次性把所有enter()的结果以list的形式yield出去
except:
exc = sys.exc_info()
finally:
while exits:
exit = exits.pop()
# 如果某个contextmanager(cm)镇压了异常,则在它之后的所有cm接收不到exc_info,否则之前的exc_info会被发送给所有的cm
# 如果某个cm的__exit__产生了异常,则该异常会代替之前产生的异常
try:
if exit(*exc):
exc = (None, None, None)
except:
exc = sys.exc_info()
if exc != (None, None, None): # 如果有任何异常产生则抛出
raise_exc_info(exc) # 不要信任sys.exc_info()依然包含正确的信息,其他的异常可能已经在某个exit方法中捕获了
示例5: _nested
def _nested(*managers):
"""Support multiple context managers in a single with-statement.
Copied from the python 2.6 standard library. It's no longer present
in python 3 because the with statement natively supports multiple
context managers, but that doesn't help if the list of context
managers is not known until runtime.
"""
exits = []
vars = []
exc = (None, None, None)
try:
for mgr in managers:
exit = mgr.__exit__
enter = mgr.__enter__
vars.append(enter())
exits.append(exit)
yield vars
except:
exc = sys.exc_info()
finally:
while exits:
exit = exits.pop()
try:
if exit(*exc):
exc = (None, None, None)
except:
exc = sys.exc_info()
if exc != (None, None, None):
# Don't rely on sys.exc_info() still containing
# the right information. Another exception may
# have been raised and caught by an exit method
raise_exc_info(exc)
示例6: wrapper
def wrapper(*args, **kwargs):
future = Future()
if callback_pos is not None and len(args) > callback_pos:
# The callback argument is being passed positionally
if args[callback_pos] is not None:
future.add_done_callback(args[callback_pos])
args = list(args) # *args is normally a tuple
args[callback_pos] = future.set_result
else:
# The callback argument is either omitted or passed by keyword.
if kwargs.get('callback') is not None:
future.add_done_callback(kwargs.pop('callback'))
kwargs['callback'] = future.set_result
def handle_error(typ, value, tb):
future.set_exception(value)
return True
exc_info = None
with ExceptionStackContext(handle_error):
try:
result = f(*args, **kwargs)
except:
exc_info = sys.exc_info()
assert result is None, ("@return_future should not be used with "
"functions that return values")
if exc_info is not None:
# If the initial synchronous part of f() raised an exception,
# go ahead and raise it to the caller directly without waiting
# for them to inspect the Future.
raise_exc_info(exc_info)
return future
示例7: wrapped
def wrapped(*args, **kwargs):
try:
# Force local state - switch to new stack chain
current_state = _state.contexts
_state.contexts = contexts
# Current exception
exc = (None, None, None)
top = None
# Apply stack contexts
last_ctx = 0
stack = contexts[0]
# Apply state
for n in stack:
try:
n.enter()
last_ctx += 1
except:
# Exception happened. Record exception info and store top-most handler
exc = sys.exc_info()
top = n.old_contexts[1]
# Execute callback if no exception happened while restoring state
if top is None:
try:
fn(*args, **kwargs)
except:
exc = sys.exc_info()
top = contexts[1]
# If there was exception, try to handle it by going through the exception chain
if top is not None:
exc = _handle_exception(top, exc)
else:
# Otherwise take shorter path and run stack contexts in reverse order
while last_ctx > 0:
last_ctx -= 1
c = stack[last_ctx]
try:
c.exit(*exc)
except:
exc = sys.exc_info()
top = c.old_contexts[1]
break
else:
top = None
# If if exception happened while unrolling, take longer exception handler path
if top is not None:
exc = _handle_exception(top, exc)
# If exception was not handled, raise it
if exc != (None, None, None):
raise_exc_info(exc)
finally:
_state.contexts = current_state
示例8: result
def result(self, timeout=None):
self._clear_tb_log()
if self._result is not None:
return self._result
if self._exc_info is not None:
raise_exc_info(self._exc_info)
self._check_done()
return self._result
示例9: result
def result(self, timeout=None):
if self._result is not None:
return self._result
if self._exc_info is not None:
raise_exc_info(self._exc_info)
elif self._exception is not None:
raise self._exception
self._check_done()
return self._result
示例10: begin
def begin(self):
connection = yield self.Connection()
try:
yield connection.begin()
except:
exc_info = sys.exc_info()
connection.close()
raise_exc_info(exc_info)
transaction = Transaction(self, connection)
raise Return(transaction)
示例11: result
def result(self, timeout=None):
"""If the operation succeeded, return its result. If it failed,
re-raise its exception.
"""
self._clear_tb_log()
if self._result is not None:
return self._result
if self._exc_info is not None:
raise_exc_info(self._exc_info)
self._check_done()
return self._result
示例12: wrapped
def wrapped(*args, **kwargs):
ret = None
try:
current_state = _state.contexts
cap_contexts[0] = contexts = _remove_deactivated(cap_contexts[0])
_state.contexts = contexts
exc = (None, None, None)
top = None
last_ctx = 0
stack = contexts[0]
for n in stack:
try:
n.enter()
last_ctx += 1
except:
exc = sys.exc_info()
top = n.old_contexts[1]
if top is None:
try:
ret = fn(*args, **kwargs)
except:
exc = sys.exc_info()
top = contexts[1]
if top is not None:
exc = _handle_exception(top, exc)
else:
while last_ctx > 0:
last_ctx -= 1
c = stack[last_ctx]
try:
c.exit(*exc)
except:
exc = sys.exc_info()
top = c.old_contexts[1]
break
else:
top = None
if top is not None:
exc = _handle_exception(top, exc)
if exc != (None, None, None):
raise_exc_info(exc)
finally:
_state.contexts = current_state
return ret
示例13: rollback
def rollback(self):
self._ensure_conn()
try:
yield self._connection.rollback()
except:
exc_info = sys.exc_info()
self._connection.close(True)
raise_exc_info(exc_info)
else:
self._connection.close()
finally:
self._connection = None
示例14: executemany
def executemany(self, query, params=None, cursor_cls=None):
with (yield self.Connection()) as connection:
cursor = connection.cursor(cursor_cls)
try:
yield cursor.executemany(query, params)
if not connection._connection.autocommit_mode:
yield connection.commit()
except:
exc_info = sys.exc_info()
if not connection._connection.autocommit_mode:
yield connection.rollback()
raise_exc_info(exc_info)
finally:
yield cursor.close()
raise Return(cursor)
示例15: result
def result(self, timeout=None):
"""If the operation succeeded, return its result. If it failed,
re-raise its exception.
This method takes a ``timeout`` argument for compatibility with
`concurrent.futures.Future` but it is an error to call it
before the `Future` is done, so the ``timeout`` is never used.
"""
self._clear_tb_log()
if self._result is not None:
return self._result
if self._exc_info is not None:
raise_exc_info(self._exc_info)
self._check_done()
return self._result