本文整理匯總了Python中inspect.FrameInfo方法的典型用法代碼示例。如果您正苦於以下問題:Python inspect.FrameInfo方法的具體用法?Python inspect.FrameInfo怎麽用?Python inspect.FrameInfo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類inspect
的用法示例。
在下文中一共展示了inspect.FrameInfo方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_python_file_from_previous_stack_frame
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def get_python_file_from_previous_stack_frame():
'''inspect.stack() lets us introspect the call stack; inspect.stack()[1] is the previous
stack frame.
In Python < 3.5, this is just a tuple, of which the python file of the previous frame is the 1st
element.
In Python 3.5+, this is a FrameInfo namedtuple instance; the python file of the previous frame
remains the 1st element.
'''
# Since this is now a function in this file, we need to go back two hops to find the
# callsite file.
previous_stack_frame = inspect.stack(0)[2]
# See: https://docs.python.org/3/library/inspect.html
if sys.version_info.major == 3 and sys.version_info.minor >= 5:
check.inst(previous_stack_frame, inspect.FrameInfo)
else:
check.inst(previous_stack_frame, tuple)
python_file = previous_stack_frame[1]
return os.path.abspath(python_file)
示例2: dump_error
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def dump_error(self, exception: Exception, trace: List[inspect.FrameInfo]) -> None:
warn_msg = """
>>> PYQUIL_PROTECT <<<
An uncaught exception was raised in a function wrapped in pyquil_protect. We are writing out a
log file to "{}".
Along with a description of what you were doing when the error occurred, send this file to
Rigetti Computing support by email at support@rigetti.com for assistance.
>>> PYQUIL_PROTECT <<<
""".format(
os.path.abspath(self.filename)
)
_log.warning(warn_msg)
report = self.generate_report(exception, trace)
# overwrite any existing log file
fh = open(self.filename, "w")
fh.write(json.dumps(report, default=json_serialization_helper))
fh.close()
示例3: _get_trace
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def _get_trace(_self: _Failure) -> Optional[List[FrameInfo]]:
"""
Function to be used on Monkey Patching.
This function is the substitute for '_get_trace' method from ``_Failure``
class on Monkey Patching promoted by
:func:`returns.primitives.tracing.collect_traces` function.
We get all the call stack from the current call and return it from the
third position, to avoid two non-useful calls on the call stack.
Those non-useful calls are a call to this function and a call to `__init__`
method from ``_Failure`` class. We're just interested in the call stack
ending on ``Failure`` function call!
See also:
https://github.com/dry-python/returns/issues/409
"""
current_stack = stack()
return current_stack[2:]
示例4: real_frame_extract
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def real_frame_extract(subframe, filepath, lineno):
"""
:type subframe: inspect.FrameInfo
:rtype: inspect.FrameInfo
"""
frames = inspect.getouterframes(subframe)
for frame in frames:
if PY2:
if frame[1] == filepath and frame[2] == lineno:
return frame[0] # type: inspect.FrameInfo
elif frame.filename == filepath and frame.lineno == lineno:
return frame.frame # type: inspect.FrameInfo
return None
示例5: generate_report
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def generate_report(self, exception: Exception, trace: List[inspect.FrameInfo]) -> ErrorReport:
"""
Handle an error generated in a routine decorated with the pyQuil error handler.
:param exception: Exception object that generated this error.
:param trace: inspect.trace object from the frame that caught the error.
:return: ErrorReport object
"""
stack_trace = [
StacktraceFrame(
name=item.function,
filename=item.filename,
line_number=item.lineno,
locals={
k: serialize_object_for_logging(v) for (k, v) in item.frame.f_locals.items()
},
)
for item in trace
]
system_info = generate_system_info()
report = ErrorReport(
stack_trace=stack_trace,
timestamp=datetime.utcnow(),
exception=exception,
system_info=system_info,
call_log=flatten_log(self.log),
)
return report
示例6: trace
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def trace(self) -> Optional[List[FrameInfo]]:
"""Returns a list with stack trace when :func:`~Failure` was called."""
return self._trace
示例7: _get_trace
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def _get_trace(self) -> Optional[List[FrameInfo]]:
"""Method that will be monkey patched when trace is active."""
return None # noqa: WPS324
示例8: trace
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def trace(self) -> Optional[List[FrameInfo]]:
"""Returns a list with stack trace when :func:`~Failure` was called."""
return self._inner_value.trace
示例9: collect_traces
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def collect_traces(
function: Optional[_FunctionType] = None,
) -> Union[_FunctionType, ContextManager[None]]: # noqa: DAR101, DAR201, DAR301
"""
Context Manager/Decorator to active traces collect to the Failures.
.. code:: python
>>> from inspect import FrameInfo
>>> from returns.io import IOResult
>>> from returns.result import Result
>>> from returns.primitives.tracing import collect_traces
>>> with collect_traces():
... traced_failure = Result.from_failure('Traced Failure')
>>> non_traced_failure = IOResult.from_failure('Non Traced Failure')
>>> assert non_traced_failure.trace is None
>>> assert isinstance(traced_failure.trace, list)
>>> assert all(isinstance(trace_line, FrameInfo) for trace_line in traced_failure.trace)
>>> for trace_line in traced_failure.trace:
... print(f'{trace_line.filename}:{trace_line.lineno} in `{trace_line.function}`') # doctest: +SKIP
...
/returns/returns/result.py:525 in `Failure`
/returns/returns/result.py:322 in `from_failure`
/example_folder/example.py:1 in `<module>`
# doctest: # noqa: DAR301, E501
"""
@contextmanager
def factory() -> Iterator[None]:
unpatched_get_trace = getattr(_Failure, '_get_trace') # noqa: B009
substitute_get_trace = types.MethodType(_get_trace, _Failure)
setattr(_Failure, '_get_trace', substitute_get_trace) # noqa: B010
try: # noqa: WPS501
yield
finally:
setattr(_Failure, '_get_trace', unpatched_get_trace) # noqa: B010
return factory()(function) if function else factory()
示例10: _set_filename
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import FrameInfo [as 別名]
def _set_filename(self, filename):
"""Mock the NApp's main.py file path."""
# Put the filename in the call stack
frame = FrameInfo(None, filename, None, None, None, None)
self._inspect_patcher = patch('kytos.core.logs.inspect')
inspect = self._inspect_patcher.start()
inspect.stack.return_value = [frame]