File: pyedit-products/unzipped/docetc/examples/Assorted-demos/trimmed-string-mode-code.py

            """
            ------------------------------------------------------------------
            # redef for main module scope
            def input(prompt=''):
                if prompt:
                    sys.stdout.write(prompt)                # no \n here: console
                    sys.stdout.flush()
                return sys.stdin.readline().rstrip('\n')    # readline blocks GUI

            # redef globally for imported modules
            import builtins
            builtins.input = input
           
            # save/restore run state
            mycwd   = os.getcwd()                           # cwd poss launcher
            mypath  = sys.path.copy()                       # sets __file__ too
            myargv  = sys.argv.copy()                       # tbd: os.envion?

            thefile = self.getFileName()                    # possibly None 
            if thefile != None:                             # iff saved once
                dirname = os.path.dirname(thefile)
                os.chdir(dirname or mycwd)                  # cd for filenames
                sys.path.insert(0, dirname)                 # path for imports
            else:                                           
                # not in PyEdit's dir!
                os.chdir(self.startfiledir)                 # user's home dir

            print('[PyEdit: Run Code]')                     # separate output
            namespace = {'__name__': '__main__'}            # run as top-level
            sys.argv  = [str(thefile)] + cmdargs.split()    # could use threads
            namespace['__file__'] = str(thefile)            # in code's scope
            try:
                name = thefile or '<string>'                # compile for msgs
                text = self.getAllText() + '\n'             # map to file if any
                code = compile(text, name, 'exec')                  
                exec(code, namespace)                       # handle code's excs:
            except Exception:                               # syntax + others
                traceback.print_exc()                       # send to console too
            finally:
                sys.argv = myargv                           # always restore
                sys.path = mypath                           # reset imports path 
                os.chdir(mycwd)                             # go back to my dir
            ---------------------------------------------------------------------
            """



[Home page] Books Code Blog Python Author Train Find ©M.Lutz