我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用inspect.getframeinfo()。
def _called_from_setup(run_frame): """ Attempt to detect whether run() was called from setup() or by another command. If called by setup(), the parent caller will be the 'run_command' method in 'distutils.dist', and *its* caller will be the 'run_commands' method. If called any other way, the immediate caller *might* be 'run_command', but it won't have been called by 'run_commands'. Return True in that case or if a call stack is unavailable. Return False otherwise. """ if run_frame is None: msg = "Call stack not available. bdist_* commands may fail." warnings.warn(msg) if platform.python_implementation() == 'IronPython': msg = "For best results, pass -X:Frames to enable call stack." warnings.warn(msg) return True res = inspect.getouterframes(run_frame)[2] caller, = res[:1] info = inspect.getframeinfo(caller) caller_module = caller.f_globals.get('__name__', '') return ( caller_module == 'distutils.dist' and info.function == 'run_commands' )
def __init__(self, *msg): message = "" for arg in msg: message += str(arg) try: ln = sys.exc_info()[-1].tb_lineno file = traceback.extract_tb(sys.exc_info()[-1]) if isinstance(file, list): file = file[0].filename except AttributeError: ln = inspect.currentframe().f_back.f_lineno file = inspect.getframeinfo(inspect.currentframe().f_back).filename if file is not None: file = re.compile("[\\\/]+").split(file) file = file[-1] self.args = "Error ({3}:{1}): {2}".format(type(self), ln, message, file), sys.exit(self) # def __init__(self, *args, **kwargs): # RuntimeError.__init__(self, args, kwargs)
def _logError(error, frame, verbose = None): """ Called if an exception occurs. Logs exception to file, if a log file is specified in the _Logfile global. """ frameinfo = getframeinfo(frame) filename = frameinfo.filename lineno = frameinfo.lineno if verbose is None and _ErrorVerbosity == True: verbose = True if verbose: print("Exception in {}<{}>: {}".format(filename, lineno, error)) if _Logfile != "": with open(_Logfile,'a+') as f: f.write("{}<{}> : {}".format(filename, lineno, error))
def __init__(self, **kwargs): # TODO This is a good first cut for debugging info, but it would be nice to # TODO be able to reliably walk the stack back to user code rather than just # TODO back past this constructor super(DebugInfo, self).__init__(**kwargs) frame = None try: frame = inspect.currentframe() while frame.f_locals.get('self', None) is self: frame = frame.f_back while frame: filename, lineno, function, code_context, index = inspect.getframeinfo( frame) if -1 == filename.find('ngraph/op_graph'): break frame = frame.f_back self.filename = filename self.lineno = lineno self.code_context = code_context finally: del frame
def getcallernames(stackindex): import os, inspect stack = inspect.stack() if stackindex >= len(stack): return None callerframerecord = stack[stackindex] frame = callerframerecord[0] info = inspect.getframeinfo(frame) head, file_name = os.path.split(info.filename) app_name = os.path.split(head)[1] return app_name, file_name ## # Count view counter by key. # # @param[in] {str} app_name # @param[in] {str} file_name # @param[in] {str} view_name #
def create_code_info(frame): """Return code information about a frame. Returns a :class:`CodeInfo` instance. """ frameinfo = inspect.getframeinfo(frame) try: sourceline = frameinfo.code_context[0].strip() except: # if no source file exists, e.g., due to eval sourceline = frameinfo.code_context return CodeInfo( path=frameinfo.filename, lineno=frameinfo.lineno, sourceline=sourceline)