Exemplo n.º 1
0
def execstring(pytext, globals, locals, filename="<string>", debugging=0,
                        modname="__main__", profiling=0):
    if debugging:
        import PyDebugger, bdb
        BdbQuit = bdb.BdbQuit
    else:
        BdbQuit = 'BdbQuitDummyException'
    pytext = string.split(pytext, '\r')
    pytext = string.join(pytext, '\n') + '\n'
    W.SetCursor("watch")
    globals['__name__'] = modname
    globals['__file__'] = filename
    sys.argv = [filename]
    try:
        code = compile(pytext, filename, "exec")
    except:
        # XXXX BAAAADDD.... We let tracebackwindow decide to treat SyntaxError
        # special. That's wrong because THIS case is special (could be literal
        # overflow!) and SyntaxError could mean we need a traceback (syntax error
        # in imported module!!!
        tracebackwindow.traceback(1, filename)
        return
    try:
        if debugging:
            PyDebugger.startfromhere()
        else:
            if hasattr(MacOS, 'EnableAppswitch'):
                MacOS.EnableAppswitch(0)
        try:
            if profiling:
                import profile, ProfileBrowser
                p = profile.Profile()
                p.set_cmd(filename)
                try:
                    p.runctx(code, globals, locals)
                finally:
                    import pstats

                    stats = pstats.Stats(p)
                    ProfileBrowser.ProfileBrowser(stats)
            else:
                exec code in globals, locals
        finally:
            if hasattr(MacOS, 'EnableAppswitch'):
                MacOS.EnableAppswitch(-1)
    except W.AlertError, detail:
        raise W.AlertError, detail