Beispiel #1
0
def setbreak(line=None, file=None, cond=None, temp=0, frame=None, throw=False):
    """set a breakpoint or a given line in file with conditional
    
    arguments:
        line - line number on which to break
        file - module or filename where the breakpoint should be set
        cond - string with conditional expression, which (if given) must
            evaluate to true to break
        temp - if true, create a temporary breakpoint
    
    example usage:
    
        setbreak(42, "/path/to/universe.py", "name == 'hitchhiker'")
        setbreak(35, "package.module")

    see: http://groups.google.com/group/comp.lang.python/browse_thread/thread/103326200285cb07#
    """
    if frame is None:
        frame = sys._getframe().f_back
    if file is None:
        file = frame.f_code.co_filename
    elif not file.startswith("file:") and os.path.sep not in file:
        try:
            mod = __import__(file, globals(), locals(), ["__file__"])
        except ImportError as err:
            if throw:
                raise
            sys.__stdout__.write("cannot set breakpoint: %s:%s : %s" %
                (file, line, err))
            return
        file = mod.__file__
        sys.__stdout__.write("breaking in: %s" % file)
    if file.endswith(".pyc"):
        file = file[:-1]
    pdb = Pdb(stdout=sys.__stdout__) # use sys.__stdout__ to work with nose tests
    pdb.reset()
    pdb.curframe = frame
    while frame:
        frame.f_trace = pdb.trace_dispatch
        pdb.botframe = frame
        frame = frame.f_back
    templine = line
    while templine < line + 10:
        error = pdb.set_break(file, templine, cond=cond, temporary=temp)
        if error:
            templine += 1
        else:
            break
    if error:
        error = pdb.set_break(file, line, cond=cond, temporary=temp)
        if throw:
            raise Error(error)
        sys.__stdout__.write("\n%s\n" % error)
        return
    sys.__stdout__.write("\n")
    pdb.do_break("") # print breakpoints
    pdb.set_continue()
    sys.settrace(pdb.trace_dispatch)