Index: Doc/lib/libpdb.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libpdb.tex,v retrieving revision 1.33 diff -c -r1.33 libpdb.tex *** Doc/lib/libpdb.tex 5 Nov 2002 22:41:16 -0000 1.33 --- Doc/lib/libpdb.tex 25 Nov 2002 22:59:08 -0000 *************** *** 255,260 **** --- 255,264 ---- Continue execution, only stop when a breakpoint is encountered. + \item[j(ump) \var{lineno}] + + Set the next line that will be executed. + \item[l(ist) \optional{\var{first\optional{, last}}}] List source code for the current file. Without arguments, list 11 Index: Doc/ref/ref3.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/ref/ref3.tex,v retrieving revision 1.95 diff -c -r1.95 ref3.tex *** Doc/ref/ref3.tex 24 Sep 2002 21:09:13 -0000 1.95 --- Doc/ref/ref3.tex 25 Nov 2002 22:59:18 -0000 *************** *** 812,819 **** variables; \member{f_globals} is used for global variables; \member{f_builtins} is used for built-in (intrinsic) names; \member{f_restricted} is a flag indicating whether the function is ! executing in restricted execution mode; ! \member{f_lineno} gives the line number and \member{f_lasti} gives the precise instruction (this is an index into the bytecode string of the code object). \withsubitem{(frame attribute)}{ --- 812,818 ---- variables; \member{f_globals} is used for global variables; \member{f_builtins} is used for built-in (intrinsic) names; \member{f_restricted} is a flag indicating whether the function is ! executing in restricted execution mode; \member{f_lasti} gives the precise instruction (this is an index into the bytecode string of the code object). \withsubitem{(frame attribute)}{ *************** *** 821,838 **** \ttindex{f_code} \ttindex{f_globals} \ttindex{f_locals} - \ttindex{f_lineno} \ttindex{f_lasti} \ttindex{f_builtins} \ttindex{f_restricted}} Special writable attributes: \member{f_trace}, if not \code{None}, is a function called at the start of each source code line (this is used by ! the debugger); \member{f_exc_type}, \member{f_exc_value}, ! \member{f_exc_traceback} represent the most recent exception caught in ! this frame. \withsubitem{(frame attribute)}{ \ttindex{f_trace} \ttindex{f_exc_type} \ttindex{f_exc_value} \ttindex{f_exc_traceback}} --- 820,838 ---- \ttindex{f_code} \ttindex{f_globals} \ttindex{f_locals} \ttindex{f_lasti} \ttindex{f_builtins} \ttindex{f_restricted}} Special writable attributes: \member{f_trace}, if not \code{None}, is a function called at the start of each source code line (this is used by ! the debugger); \member{f_lineno} is the current line number of the frame ! --- writing to this jumps to the given line; \member{f_exc_type}, ! \member{f_exc_value}, \member{f_exc_traceback} represent the most ! recent exception caught in this frame. \withsubitem{(frame attribute)}{ \ttindex{f_trace} + \ttindex{f_lineno} \ttindex{f_exc_type} \ttindex{f_exc_value} \ttindex{f_exc_traceback}} Index: Lib/pdb.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/pdb.py,v retrieving revision 1.58 diff -c -r1.58 pdb.py *** Lib/pdb.py 5 Nov 2002 22:40:20 -0000 1.58 --- Lib/pdb.py 25 Nov 2002 22:59:23 -0000 *************** *** 506,511 **** --- 506,527 ---- return 1 do_c = do_cont = do_continue + def do_jump(self, arg): + try: + arg = int(arg) + except ValueError: + print "*** The 'jump' command requires a line number." + else: + try: + # Do the jump, fix up our copy of the stack, and display the + # new position + self.curframe.f_lineno = arg + self.stack[self.curindex] = self.stack[self.curindex][0], arg + self.print_stack_entry(self.stack[self.curindex]) + except ValueError, e: + print '*** Jump failed:', e + do_j = do_jump + def do_quit(self, arg): self.set_quit() return 1 *************** *** 804,809 **** --- 820,832 ---- def help_c(self): print """c(ont(inue)) Continue execution, only stop when a breakpoint is encountered.""" + + def help_jump(self): + self.help_j() + + def help_j(self): + print """j(ump) lineno + Set the next line that will be executed.""" def help_list(self): self.help_l() Index: Lib/test/test_trace.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_trace.py,v retrieving revision 1.6 diff -c -r1.6 test_trace.py *** Lib/test/test_trace.py 9 Nov 2002 05:26:15 -0000 1.6 --- Lib/test/test_trace.py 25 Nov 2002 22:59:30 -0000 *************** *** 125,130 **** --- 125,291 ---- (4, 'line'), (4, 'return')] + + # 'Jump' tests: assigning to frame.f_lineno within a trace function + # moves the execution position - it's how debuggers implement a Jump + # command (aka. "Set next statement"). + + class JumpTracer: + """Defines a trace function that jumps from one place to another, + with the source and destination lines of the jump being defined by + the 'jump' property of the function under test.""" + + def __init__(self, function): + self.function = function + self.jumpFrom = function.jump[0] + self.jumpTo = function.jump[1] + self.done = False + + def trace(self, frame, event, arg): + if not self.done and frame.f_code == self.function.func_code: + firstLine = frame.f_code.co_firstlineno + if frame.f_lineno == firstLine + self.jumpFrom: + frame.f_lineno = firstLine + self.jumpTo + self.done = True + return self.trace + + # The first set of 'jump' tests are for things that are allowed: + + def jump_simple_forwards(output): + output.append(1) + output.append(2) + output.append(3) + + jump_simple_forwards.jump = (1, 3) + jump_simple_forwards.output = [3] + + def jump_simple_backwards(output): + output.append(1) + output.append(2) + + jump_simple_backwards.jump = (2, 1) + jump_simple_backwards.output = [1, 1, 2] + + def jump_out_of_block_forwards(output): + for i in 1, 2: + output.append(2) + for j in [3]: # Also tests jumping over a block + output.append(4) + output.append(5) + + jump_out_of_block_forwards.jump = (3, 5) + jump_out_of_block_forwards.output = [2, 5] + + def jump_out_of_block_backwards(output): + output.append(1) + for i in [1]: + output.append(3) + for j in [2]: # Also tests jumping over a block + output.append(5) + output.append(6) + output.append(7) + + jump_out_of_block_backwards.jump = (6, 1) + jump_out_of_block_backwards.output = [1, 3, 5, 1, 3, 5, 6, 7] + + def jump_to_codeless_line(output): + output.append(1) + # Jumping to this line should skip to the next one. + output.append(3) + + jump_to_codeless_line.jump = (1, 2) + jump_to_codeless_line.output = [3] + + def jump_to_same_line(output): + output.append(1) + output.append(2) + output.append(3) + + jump_to_same_line.jump = (2, 2) + jump_to_same_line.output = [1, 2, 3] + + # The second set of 'jump' tests are for things that are not allowed: + + def no_jump_too_far_forwards(output): + try: + output.append(2) + output.append(3) + except ValueError, e: + output.append('after' in str(e)) + + no_jump_too_far_forwards.jump = (3, 6) + no_jump_too_far_forwards.output = [2, True] + + def no_jump_too_far_backwards(output): + try: + output.append(2) + output.append(3) + except ValueError, e: + output.append('before' in str(e)) + + no_jump_too_far_backwards.jump = (3, -1) + no_jump_too_far_backwards.output = [2, True] + + def no_jump_to_except(output): + try: + output.append(2) + except ValueError, e: + output.append('except' in str(e)) + + no_jump_to_except.jump = (2, 3) + no_jump_to_except.output = [True] + + def no_jump_forwards_into_block(output): + try: + output.append(2) + for i in 1, 2: + output.append(4) + except ValueError, e: + output.append('into' in str(e)) + + no_jump_forwards_into_block.jump = (2, 4) + no_jump_forwards_into_block.output = [True] + + def no_jump_backwards_into_block(output): + try: + for i in 1, 2: + output.append(3) + output.append(4) + except ValueError, e: + output.append('into' in str(e)) + + no_jump_backwards_into_block.jump = (4, 3) + no_jump_backwards_into_block.output = [3, 3, True] + + def no_jump_into_finally_block(output): + try: + try: + output.append(3) + x = 1 + finally: + output.append(6) + except ValueError, e: + output.append('finally' in str(e)) + + no_jump_into_finally_block.jump = (4, 6) + no_jump_into_finally_block.output = [3, 6, True] # The 'finally' still runs + + def no_jump_out_of_finally_block(output): + try: + try: + output.append(3) + finally: + x = 1 + output.append(6) + except ValueError, e: + output.append('finally' in str(e)) + + no_jump_out_of_finally_block.jump = (5, 1) + no_jump_out_of_finally_block.output = [3, True] + + # That's the end of the 'jump' tests. + + class Tracer: def __init__(self): self.events = [] *************** *** 141,146 **** --- 302,312 ---- "\n".join(difflib.ndiff(map(str, expected_events), map(str, events)))) + def compare_jump_output(self, expected, received): + if received != expected: + self.fail( "Outputs don't match:\n" + + "Expected: " + repr(expected) + "\n" + + "Received: " + repr(received)) def run_test(self, func): tracer = Tracer() *************** *** 157,181 **** self.compare_events(func.func_code.co_firstlineno, tracer.events, func.events) ! def test_1_basic(self): self.run_test(basic) ! def test_2_arigo(self): self.run_test(arigo_example) ! def test_3_one_instr(self): self.run_test(one_instr_line) ! def test_4_no_pop_blocks(self): self.run_test(no_pop_blocks) ! def test_5_no_pop_tops(self): self.run_test(no_pop_tops) ! def test_6_call(self): self.run_test(call) ! def test_7_raise(self): self.run_test(test_raise) ! def test_8_settrace_and_return(self): self.run_test2(settrace_and_return) ! def test_9_settrace_and_raise(self): self.run_test2(settrace_and_raise) class RaisingTraceFuncTestCase(unittest.TestCase): def trace(self, frame, event, arg): --- 323,382 ---- self.compare_events(func.func_code.co_firstlineno, tracer.events, func.events) ! def run_jump_test(self, func): ! tracer = JumpTracer(func) ! sys.settrace(tracer.trace) ! output = [] ! func(output) ! sys.settrace(None) ! self.compare_jump_output(func.output, output) ! ! def test_01_basic(self): self.run_test(basic) ! def test_02_arigo(self): self.run_test(arigo_example) ! def test_03_one_instr(self): self.run_test(one_instr_line) ! def test_04_no_pop_blocks(self): self.run_test(no_pop_blocks) ! def test_05_no_pop_tops(self): self.run_test(no_pop_tops) ! def test_06_call(self): self.run_test(call) ! def test_07_raise(self): self.run_test(test_raise) ! def test_08_settrace_and_return(self): self.run_test2(settrace_and_return) ! def test_09_settrace_and_raise(self): self.run_test2(settrace_and_raise) + + def test_10_jump_simple_forwards(self): + self.run_jump_test(jump_simple_forwards) + def test_11_jump_simple_backwards(self): + self.run_jump_test(jump_simple_backwards) + def test_12_jump_out_of_block_forwards(self): + self.run_jump_test(jump_out_of_block_forwards) + def test_13_jump_out_of_block_backwards(self): + self.run_jump_test(jump_out_of_block_backwards) + def test_14_jump_to_codeless_line(self): + self.run_jump_test(jump_to_codeless_line) + def test_15_jump_to_same_line(self): + self.run_jump_test(jump_to_same_line) + def test_16_no_jump_too_far_forwards(self): + self.run_jump_test(no_jump_too_far_forwards) + def test_17_no_jump_too_far_backwards(self): + self.run_jump_test(no_jump_too_far_backwards) + def test_18_no_jump_to_except(self): + self.run_jump_test(no_jump_to_except) + def test_19_no_jump_forwards_into_block(self): + self.run_jump_test(no_jump_forwards_into_block) + def test_20_no_jump_backwards_into_block(self): + self.run_jump_test(no_jump_backwards_into_block) + def test_21_no_jump_into_finally_block(self): + self.run_jump_test(no_jump_into_finally_block) + def test_22_no_jump_out_of_finally_block(self): + self.run_jump_test(no_jump_out_of_finally_block) class RaisingTraceFuncTestCase(unittest.TestCase): def trace(self, frame, event, arg): Index: Objects/frameobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/frameobject.c,v retrieving revision 2.67 diff -c -r2.67 frameobject.c *** Objects/frameobject.c 11 Sep 2002 15:36:32 -0000 2.67 --- Objects/frameobject.c 25 Nov 2002 22:59:43 -0000 *************** *** 8,13 **** --- 8,16 ---- #include "opcode.h" #include "structmember.h" + #define MIN(a, b) ((a) < (b) ? (a) : (b)) + #define MAX(a, b) ((a) > (b) ? (a) : (b)) + #define OFF(x) offsetof(PyFrameObject, x) static PyMemberDef frame_memberlist[] = { *************** *** 44,49 **** --- 47,297 ---- return PyInt_FromLong(lineno); } + /* Setter for f_lineno - you can set f_lineno from within a trace function in + * order to jump to a given line of code, subject to some restrictions. Most + * lines are OK to jump to because they don't make any assumptions about the + * state of the stack (obvious because you could remove the line and the code + * would still work without any stack errors), but there are some constructs + * that limit jumping: + * + * o Lines with an 'except' statement on them can't be jumped to, because + * they expect an exception to be on the top of the stack. + * o Lines that live in a 'finally' block can't be jumped from or to, since + * the END_FINALLY expects to clean up the stack after the 'try' block. + * o 'try'/'for'/'while' blocks can't be jumped into because the blockstack + * needs to be set up before their code runs, and for 'for' loops the + * iterator needs to be on the stack. + */ + static int + frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno) + { + int new_lineno = 0; /* The new value of f_lineno */ + int new_lasti = 0; /* The new value of f_lasti */ + int new_iblock = 0; /* The new value of f_iblock */ + unsigned char *code = NULL; /* The bytecode for the frame... */ + int code_len = 0; /* ...and its length */ + unsigned char *lnotab = NULL; /* Iterating over co_lnotab */ + int lnotab_len = 0; /* (ditto) */ + int offset = 0; /* (ditto) */ + int line = 0; /* (ditto) */ + int addr = 0; /* (ditto) */ + int min_addr = 0; /* Scanning the SETUPs and POPs */ + int max_addr = 0; /* (ditto) */ + int delta_iblock = 0; /* (ditto) */ + int min_delta_iblock = 0; /* (ditto) */ + int min_iblock = 0; /* (ditto) */ + int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */ + int new_lasti_setup_addr = 0; /* (ditto) */ + int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */ + int in_finally[CO_MAXBLOCKS]; /* (ditto) */ + int blockstack_top = 0; /* (ditto) */ + int setup_op = 0; /* (ditto) */ + + /* f_lineno must be an integer. */ + if (!PyInt_Check(p_new_lineno)) { + PyErr_SetString(PyExc_ValueError, + "lineno must be an integer"); + return -1; + } + + /* Fail if the line comes before the start of the code block. */ + new_lineno = (int) PyInt_AsLong(p_new_lineno); + if (new_lineno < f->f_code->co_firstlineno) { + PyErr_Format(PyExc_ValueError, + "line %d comes before the current code block", + new_lineno); + return -1; + } + + /* Find the bytecode offset for the start of the given line, or the + * first code-owning line after it. */ + PyString_AsStringAndSize(f->f_code->co_lnotab, &lnotab, &lnotab_len); + addr = 0; + line = f->f_code->co_firstlineno; + new_lasti = -1; + for (offset = 0; offset < lnotab_len; offset += 2) { + addr += lnotab[offset]; + line += lnotab[offset+1]; + if (line >= new_lineno) { + new_lasti = addr; + new_lineno = line; + break; + } + } + + /* If we didn't reach the requested line, return an error. */ + if (new_lasti == -1) { + PyErr_Format(PyExc_ValueError, + "line %d comes after the current code block", + new_lineno); + return -1; + } + + /* We're now ready to look at the bytecode. */ + PyString_AsStringAndSize(f->f_code->co_code, &code, &code_len); + min_addr = MIN(new_lasti, f->f_lasti); + max_addr = MAX(new_lasti, f->f_lasti); + + /* You can't jump onto a line with an 'except' statement on it - + * they expect to have an exception on the top of the stack, which + * won't be true if you jump to them. They always start with code + * that either pops the exception using POP_TOP (plain 'except:' + * lines do this) or duplicates the exception on the stack using + * DUP_TOP (if there's an exception type specified). See compile.c, + * 'com_try_except' for the full details. There aren't any other + * cases (AFAIK) where a line's code can start with DUP_TOP or + * POP_TOP, but if any ever appear, they'll be subject to the same + * restriction (but with a different error message). */ + if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) { + PyErr_SetString(PyExc_ValueError, + "can't jump to 'except' line as there's no exception"); + return -1; + } + + /* You can't jump into or out of a 'finally' block because the 'try' + * block leaves something on the stack for the END_FINALLY to clean + * up. So we walk the bytecode, maintaining a simulated blockstack. + * When we reach the old or new address and it's in a 'finally' block + * we note the address of the corresponding SETUP_FINALLY. The jump + * is only legal if neither address is in a 'finally' block or + * they're both in the same one. 'blockstack' is a stack of the + * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks + * whether we're in a 'finally' block at each blockstack level. */ + f_lasti_setup_addr = -1; + new_lasti_setup_addr = -1; + memset(blockstack, '\0', sizeof(blockstack)); + memset(in_finally, '\0', sizeof(in_finally)); + blockstack_top = 0; + for (addr = 0; addr < code_len; addr++) { + unsigned char op = code[addr]; + switch (op) { + case SETUP_LOOP: + case SETUP_EXCEPT: + case SETUP_FINALLY: + blockstack[blockstack_top++] = addr; + in_finally[blockstack_top-1] = 0; + break; + + case POP_BLOCK: + setup_op = code[blockstack[blockstack_top-1]]; + if (setup_op == SETUP_FINALLY) { + in_finally[blockstack_top-1] = 1; + } + else { + blockstack_top--; + } + break; + + case END_FINALLY: + /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist + * in the bytecode but don't correspond to an actual + * 'finally' block. */ + setup_op = code[blockstack[blockstack_top-1]]; + if (setup_op == SETUP_FINALLY) { + blockstack_top--; + } + break; + } + + /* For the addresses we're interested in, see whether they're + * within a 'finally' block and if so, remember the address + * of the SETUP_FINALLY. */ + if (addr == new_lasti || addr == f->f_lasti) { + int i = 0; + int setup_addr = -1; + for (i = blockstack_top-1; i >= 0; i--) { + if (in_finally[i]) { + setup_addr = blockstack[i]; + break; + } + } + + if (setup_addr != -1) { + if (addr == new_lasti) { + new_lasti_setup_addr = setup_addr; + } + + if (addr == f->f_lasti) { + f_lasti_setup_addr = setup_addr; + } + } + } + + if (op >= HAVE_ARGUMENT) { + addr += 2; + } + } + + if (new_lasti_setup_addr != f_lasti_setup_addr) { + PyErr_SetString(PyExc_ValueError, + "can't jump into or out of a 'finally' block"); + return -1; + } + + + /* Police block-jumping (you can't jump into the middle of a block) + * and ensure that the blockstack finishes up in a sensible state (by + * popping any blocks we're jumping out of). We look at all the + * blockstack operations between the current position and the new + * one, and keep track of how many blocks we drop out of on the way. + * By also keeping track of the lowest blockstack position we see, we + * can tell whether the jump goes into any blocks without coming out + * again - in that case we raise an exception below. */ + delta_iblock = 0; + for (addr = min_addr; addr < max_addr; addr++) { + unsigned char op = code[addr]; + switch (op) { + case SETUP_LOOP: + case SETUP_EXCEPT: + case SETUP_FINALLY: + delta_iblock++; + break; + + case POP_BLOCK: + delta_iblock--; + break; + } + + min_delta_iblock = MIN(min_delta_iblock, delta_iblock); + + if (op >= HAVE_ARGUMENT) { + addr += 2; + } + } + + /* Derive the absolute iblock values from the deltas. */ + min_iblock = f->f_iblock + min_delta_iblock; + if (new_lasti > f->f_lasti) { + /* Forwards jump. */ + new_iblock = f->f_iblock + delta_iblock; + } + else { + /* Backwards jump. */ + new_iblock = f->f_iblock - delta_iblock; + } + + /* Are we jumping into a block? */ + if (new_iblock > min_iblock) { + PyErr_SetString(PyExc_ValueError, + "can't jump into the middle of a block"); + return -1; + } + + /* Pop any blocks that we're jumping out of. */ + while (f->f_iblock > new_iblock) { + PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; + while ((f->f_stacktop - f->f_valuestack) > b->b_level) { + PyObject *v = (*--f->f_stacktop); + Py_DECREF(v); + } + } + + /* Finally set the new f_lineno and f_lasti and return OK. */ + f->f_lineno = new_lineno; + f->f_lasti = new_lasti; + return 0; + } + static PyObject * frame_gettrace(PyFrameObject *f, void *closure) { *************** *** 77,83 **** static PyGetSetDef frame_getsetlist[] = { {"f_locals", (getter)frame_getlocals, NULL, NULL}, ! {"f_lineno", (getter)frame_getlineno, NULL, NULL}, {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL}, {0} }; --- 325,332 ---- static PyGetSetDef frame_getsetlist[] = { {"f_locals", (getter)frame_getlocals, NULL, NULL}, ! {"f_lineno", (getter)frame_getlineno, ! (setter)frame_setlineno, NULL}, {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL}, {0} };