_goto = type('goto', (object,), dict(__and__=lambda _, other: (_goto(other.line)))) goto = _goto() def _goto(lineno): frame = sys._getframe().f_back.f_back called_from = frame def hook(frame, event, _): if event == 'line' and frame == called_from: try: frame.f_lineno = lineno except ValueError as e: print("jump failed:", e) while frame: frame.f_trace = None frame = frame.f_back return None return hook # it doesn't think we actually need to set the hook for each frame # while frame: # frame.f_trace = hook # frame = frame.f_back called_from.f_trace = hook sys.settrace(hook)