def _call_location(): # We want to get stack frame 2 frames up from current frame, # i.e. above _getattr__ and _call_location calls. stack = tf_stack.extract_stack_file_and_line(max_length=3) if not stack: # should never happen as we're in a function return 'UNKNOWN' frame = stack[0] return '{}:{}'.format(frame.file, frame.line)
def _call_location(): # We want to get stack frame 2 frames up from current frame, # i.e. above _getattr__ and _call_location calls. stack = tf_stack.extract_stack_file_and_line(max_length=3) if not stack: # should never happen as we're in a function return 'UNKNOWN' frame = stack[0] return '{}:{}'.format(frame.file, frame.line)
def _call_location(outer=False): """Returns call location given level up from current call.""" stack = tf_stack.extract_stack_file_and_line(max_length=4) length = len(stack) if length == 0: # should never happen as we're in a function return 'UNKNOWN' index = length - 4 if outer else length - 3 if index < 0: index = 0 frame = stack[index] return '{}:{}'.format(frame.file, frame.line)
def _call_location(outer=False): """Returns call location given level up from current call.""" stack = tf_stack.extract_stack_file_and_line(max_length=4) length = len(stack) if length == 0: # should never happen as we're in a function return 'UNKNOWN' index = length-4 if outer else length-3 if index < 0: index = 0 frame = stack[index] return '{}:{}'.format(frame.file, frame.line)
def set_filename_and_line_from_caller(self, offset=0): """Set filename and line using the caller's stack frame. If the requested stack information is not available, a heuristic may be applied and self.HEURISTIC USED will be returned. If the heuristic fails then no change will be made to the filename and lineno members (None by default) and self.FAILURE will be returned. Args: offset: Integer. If 0, the caller's stack frame is used. If 1, the caller's caller's stack frame is used. Larger values are permissible but if out-of-range (larger than the number of stack frames available) the outermost stack frame will be used. Returns: TraceableObject.SUCCESS if appropriate stack information was found, TraceableObject.HEURISTIC_USED if the offset was larger than the stack, and TraceableObject.FAILURE if the stack was empty. """ # Offset is defined in "Args" as relative to the caller. We are one frame # beyond the caller. local_offset = offset + 1 frame_records = tf_stack.extract_stack_file_and_line( max_length=local_offset + 1) if not frame_records: return self.FAILURE if len(frame_records) >= local_offset: # Negative indexing is one-indexed instead of zero-indexed. negative_offset = -(local_offset + 1) self.filename, self.lineno = frame_records[negative_offset] return self.SUCCESS else: # If the offset is too large then we use the largest offset possible, # meaning we use the outermost stack frame at index 0. self.filename, self.lineno = frame_records[0] return self.HEURISTIC_USED
def set_filename_and_line_from_caller(self, offset=0): """Set filename and line using the caller's stack frame. If the requested stack information is not available, a heuristic may be applied and self.HEURISTIC USED will be returned. If the heuristic fails then no change will be made to the filename and lineno members (None by default) and self.FAILURE will be returned. Args: offset: Integer. If 0, the caller's stack frame is used. If 1, the caller's caller's stack frame is used. Larger values are permissible but if out-of-range (larger than the number of stack frames available) the outermost stack frame will be used. Returns: TraceableObject.SUCCESS if appropriate stack information was found, TraceableObject.HEURISTIC_USED if the offset was larger than the stack, and TraceableObject.FAILURE if the stack was empty. """ # Offset is defined in "Args" as relative to the caller. We are one frame # beyond the caller. local_offset = offset + 1 frame_records = tf_stack.extract_stack_file_and_line( max_length=local_offset + 1) if not frame_records: return self.FAILURE if len(frame_records) > local_offset: # Negative indexing is one-indexed instead of zero-indexed. negative_offset = -(local_offset + 1) self.filename, self.lineno = frame_records[negative_offset] return self.SUCCESS else: # If the offset is too large then we use the largest offset possible, # meaning we use the outermost stack frame at index 0. self.filename, self.lineno = frame_records[0] return self.HEURISTIC_USED