Restarting Python

Terry Reedy tjreedy at udel.edu
Wed Oct 1 13:52:57 EDT 2014


On 10/1/2014 10:47 AM, Steven D'Aprano wrote:

> Inside the interactive interpreter, I can restart the interpreter with four
> keystrokes:
>
> - Ctrl-D
> - UP-ARROW
> - ENTER
>
> Ctrl-D exits Python and returns me to the shell, UP-ARROW fetches the
> previous command ("python"), and ENTER runs that command. On Windows, I
> *think* you have to type Ctrl-Z ENTER instead of Ctrl-D, so that will be
> five keystrokes.

For the console interpreter, this is still true even in 3.5.0a0
 >>> ^D
   File "<stdin>", line 1
     ♦
     ^
SyntaxError: invalid syntax

Idle now quits on ^D on all systems. ^Z (unlike other control chars) is 
rejected with an immediate error beep.

Either way, restart is a single click if one have the console or Idle 
interpreter icon pinned on the task bar.

Even this is unnecessary with Idle as it has a Restart Shell command on 
the Shell menu (hotkey ^F6).  (This is automatically invoked when 
running an edited file (F5)).


Some problems with restarting are unwinding the call stack, undoing 
what has been done, and doing something else so as to not run into the 
same problem.  We can view every raise -- except pair as a partial 
restart of some sort.  A minimal startup script something like the 
following allows a nearly global restart that addresses all three 
problems listed above.

from appmain import main, cleanup, RestartError
restart = False
while True:
   try:
     main(restart)
   except RestartError as err:
     cleanup()
     restart = err
     continue

The main and cleanup functions, switching on the restart arg, and 
possibly the class RestartError depend on the app.

-- 
Terry Jan Reedy





More information about the Python-list mailing list