Index: Doc/library/idle.rst =================================================================== --- Doc/library/idle.rst (revision 78993) +++ Doc/library/idle.rst (working copy) @@ -256,14 +256,19 @@ Startup ------- -Upon startup with the ``-s`` option, IDLE will execute the file referenced by -the environment variables :envvar:`IDLESTARTUP` or :envvar:`PYTHONSTARTUP`. -Idle first checks for ``IDLESTARTUP``; if ``IDLESTARTUP`` is present the file +Upon startup, IDLE will execute the file referenced by the environment +variables :envvar:`IDLESTARTUP` or :envvar:`PYTHONSTARTUP`. Idle first +checks for ``IDLESTARTUP``; if ``IDLESTARTUP`` is present the file referenced is run. If ``IDLESTARTUP`` is not present, Idle checks for -``PYTHONSTARTUP``. Files referenced by these environment variables are -convenient places to store functions that are used frequently from the Idle -shell, or for executing import statements to import common modules. +``PYTHONSTARTUP``. A file referenced by one of these environment +variables is a convenient place to store functions that are used +frequently from the Idle shell, or for executing import statements to +import common modules. They'll be run on every shell restart. +The ``-s`` command-line option can be used to override these +enviroment variables and specify a particular startup file; the ``-q`` +option supresses running any startup file. + In addition, ``Tk`` also loads a startup file if it is present. Note that the Tk file is loaded unconditionally. This additional file is ``.Idle.py`` and is looked for in the user's home directory. Statements in this file will be @@ -276,12 +281,15 @@ :: - idle.py [-c command] [-d] [-e] [-s] [-t title] [arg] ... + idle.py [-c command] [-d] [-e] [-q | -s file] [-t title] [arg] ... -c command run this command -d enable debugger -e edit mode; arguments are files to be edited - -s run $IDLESTARTUP or $PYTHONSTARTUP first + -q do NOT run any startup file, ignore $IDLESTARTUP or $PYTHONSTARTUP + -s file run file in place of $IDLESTARTUP or $PYTHONSTARTUP before + anything else and whenever the interpreter restarts + -q do NOT run $IDLESTARTUP or $PYTHONSTARTUP -t title set title of shell window If there are arguments: Index: Lib/idlelib/PyShell.py =================================================================== --- Lib/idlelib/PyShell.py (revision 78993) +++ Lib/idlelib/PyShell.py (working copy) @@ -345,6 +345,7 @@ self.restarting = False self.subprocess_arglist = None self.port = PORT + self.startup_filename = None # for restarts rpcclt = None rpcpid = None @@ -453,6 +454,7 @@ console.write(halfbar + ' RESTART ' + halfbar) console.text.mark_set("restart", "end-1c") console.text.mark_gravity("restart", "left") + self.exec_startup() console.showprompt() # restart subprocess debugger if debug: @@ -463,6 +465,10 @@ self.restarting = False return self.rpcclt + def exec_startup(self): + if self.startup_filename and os.path.isfile(self.startup_filename): + self.execfile(self.startup_filename) + def __request_interrupt(self): self.rpcclt.remotecall("exec", "interrupt_the_server", (), {}) @@ -1258,9 +1264,9 @@ usage_msg = """\ -USAGE: idle [-deins] [-t title] [file]* - idle [-dns] [-t title] (-c cmd | -r file) [arg]* - idle [-dns] [-t title] - [arg]* +USAGE: idle [-dein] [-q | -s file] [-t title] [file]* + idle [-dn [-q | -s file] [-t title] (-c cmd | -r file) [arg]* + idle [-dn] [-q | -s file] [-t title] - [arg]* -h print this help message and exit -n run IDLE without a subprocess (see Help/IDLE Help for details) @@ -1276,7 +1282,9 @@ -r file run script from file -d enable the debugger - -s run $IDLESTARTUP or $PYTHONSTARTUP before anything else + -s file run file in place of $IDLESTARTUP or $PYTHONSTARTUP before + anything else and whenever the interpreter restarts + -q do NOT run $IDLESTARTUP or $PYTHONSTARTUP -t title set title of shell window A default edit window will be bypassed when -c, -r, or - are used. @@ -1285,24 +1293,26 @@ Examples: +[All run $IDLESTARTUP or $PYTHONSTARTUP before anything else unless +-q or -s is specified.] + idle Open an edit window or shell depending on IDLE's configuration. idle foo.py foobar.py Edit the files, also open a shell if configured to start with shell. -idle -est "Baz" foo.py - Run $IDLESTARTUP or $PYTHONSTARTUP, edit foo.py, and open a shell - window with the title "Baz". +idle -et "Baz" foo.py + edit foo.py, and open a shell window with the title "Baz". idle -c "import sys; print sys.argv" "foo" Open a shell window and run the command, passing "-c" in sys.argv[0] and "foo" in sys.argv[1]. -idle -d -s -r foo.py "Hello World" - Open a shell window, run a startup script, enable the debugger, and - run foo.py, passing "foo.py" in sys.argv[0] and "Hello World" in - sys.argv[1]. +idle -d -s startup.py -r foo.py "Hello World" + Open a shell window, run startup.py instead of $IDLESTARTUP or + $PYTHONSTARTUP, enable the debugger, and run foo.py, passing + "foo.py" in sys.argv[0] and "Hello World" in sys.argv[1]. echo "import sys; print sys.argv" | idle - "foobar" Open a shell window, run the script piped in, passing '' in sys.argv[0] @@ -1318,9 +1328,11 @@ debug = False cmd = None script = None - startup = False + # -q suppresses; -s specifies alternate (can't be used together) + startup_filename = (os.environ.get("IDLESTARTUP") or + os.environ.get("PYTHONSTARTUP")) try: - opts, args = getopt.getopt(sys.argv[1:], "c:deihnr:st:") + opts, args = getopt.getopt(sys.argv[1:], "c:deihnqr:s:t:") except getopt.error, msg: sys.stderr.write("Error: %s\n" % str(msg)) sys.stderr.write(usage_msg) @@ -1342,6 +1354,10 @@ enable_shell = True if o == '-n': use_subprocess = False + if o == '-q': + if '-s' in sys.argv: + raise GetOptError('options -q and -s cannot both be specified') + startup_filename = None if o == '-r': script = a if os.path.isfile(script): @@ -1351,8 +1367,9 @@ sys.exit() enable_shell = True if o == '-s': - startup = True - enable_shell = True + if '-q' in sys.argv: + raise GetOptError('options -s and -q cannot both be specified') + startup_filename = a if o == '-t': PyShell.shell_title = a enable_shell = True @@ -1415,11 +1432,13 @@ # handle remaining options: if debug: shell.open_debugger() - if startup: - filename = os.environ.get("IDLESTARTUP") or \ - os.environ.get("PYTHONSTARTUP") - if filename and os.path.isfile(filename): - shell.interp.execfile(filename) + shell.interp.startup_filename = startup_filename + # XXX Startup code executes before script, so it doesn't run in + # clean environment. A clean Python doesn't execute startup at + # all when running a script, and this difference is bad. However, + # it's desirable for $IDLESTARTUP to be able to e.g. set the + # excepthook. + shell.interp.exec_startup() if shell and cmd or script: shell.interp.runcommand("""if 1: import sys as _sys Index: Lib/idlelib/NEWS.txt =================================================================== --- Lib/idlelib/NEWS.txt (revision 78993) +++ Lib/idlelib/NEWS.txt (working copy) @@ -1,8 +1,12 @@ -What's New in IDLE 2.7a0? +What's New in IDLE 2.7aX? ========================= *Release date: XX-XXX-2010* +- $IDLESTARTUP / $PYTHONSTARTUP are now enabled by default and are run + after every shell restart, not just once. (-q suppresses this, -s + specifies a different file) + - idle.py modified and simplified to better support developing experimental versions of IDLE which are not installed in the standard location.