Skip to content

IDLE - clear and restart the shell window #50393

Open

Description

@serwy
mannequin
BPO 6143
Nosy @rhettinger, @terryjreedy, @serwy, @rovitotv, @alex, @ZackerySpytz, @PythonCoderAS
PRs
  • gh-50393: "Clear and restart" IDLE shell windows #21682
  • Files
  • ClearWindow.py: ClearWindow extension
  • ClearWindow.py: Added Undo functionality
  • taleinat.20140219.IDLE_ClearWindow_extension.patch: Updated version of the ClearWindow extensions, with tests
  • taleinat.20140219.IDLE_ClearWindow_Squeezer_integration.patch
  • Squeezer Screenshot.PNG
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/terryjreedy'
    closed_at = None
    created_at = <Date 2009-05-29.21:09:50.429>
    labels = ['expert-IDLE', 'type-feature', '3.11']
    title = 'IDLE - clear and restart the shell window'
    updated_at = <Date 2022-03-02.03:49:24.032>
    user = 'https://github.com/serwy'

    bugs.python.org fields:

    activity = <Date 2022-03-02.03:49:24.032>
    actor = 'terry.reedy'
    assignee = 'terry.reedy'
    closed = False
    closed_date = None
    closer = None
    components = ['IDLE']
    creation = <Date 2009-05-29.21:09:50.429>
    creator = 'roger.serwy'
    dependencies = []
    files = ['14116', '14303', '34145', '34146', '34200']
    hgrepos = []
    issue_num = 6143
    keywords = ['patch']
    message_count = 31.0
    messages = ['88539', '89319', '89387', '185485', '186033', '211032', '211044', '211056', '211066', '211067', '211073', '211074', '211310', '211641', '211642', '211644', '211663', '212020', '212051', '260912', '272835', '272843', '287735', '287791', '319869', '333251', '359256', '359265', '374596', '374626', '414323']
    nosy_count = 8.0
    nosy_names = ['rhettinger', 'terry.reedy', 'roger.serwy', 'THRlWiTi', 'Todd.Rovito', 'Alex', 'ZackerySpytz', 'ThePokestarFan']
    pr_nums = ['21682']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue6143'
    versions = ['Python 3.11']

    Activity

    1. serwy commented on May 29, 2009

      serwymannequin
      MannequinAuthor

      I noticed that the feature to clear the shell window has been a
      discussion topic in the IDLE-dev archives for a few years. Here's an
      extension that can clear the contents of the shell window. It provides
      "Clear Shell Window" under "Options" and a <Ctrl-L> key binding.

      The source code provides two methods to do this. The
      ModifiedUndoDelegator prevents changing the contents of the text widget
      before "iomark". One solution is to disable the filter, make the
      changes, then re-enable it. Another solution is move "iomark" to the
      beginning of the document. Any thoughts?

    2. taleinat commented on Jun 13, 2009

      @taleinat
      Contributor

      First of all I think that the Squeezer (issue bpo-1529353) extension is
      more useful and solves most of the problems that this proposed feature
      is set to solve, e.g. issue bpo-1442493.

      IMO the second method you offer - temporarily moving "iomark" - is
      preferable.

      One reason is that it will allow using the undo feature to undo deleting
      the entire shell history. I think this is important since deleting the
      history by accident without a way to retrieve it would be very annoying.

      I propose going with the clear_window2 method, wrapping it with
      undo_block_start() and undo_block_stop().

    3. serwy commented on Jun 15, 2009

      serwymannequin
      MannequinAuthor

      I just tried Squeezer. It's pretty neat and it solves a different
      problem. Clearing the contents of the shell window should be a simple
      operation.

      The undo operation doesn't restore iomark properly, nor does it restore
      tags. I've uploaded a newer version of the extension to handle undo.

      Binding to the "<<undo>>" virtual event with add="+" didn't work as
      expected, so the new undo event handler explicitly calls the older one.
      I admit that this code is not ideal.

      Should I move this extension to the Cheese Shop and close out this issue?

    4. added
      type-featureA feature request or enhancement
      and removed
      type-bugAn unexpected behavior, bug, or error
      on Aug 3, 2010
    5. serwy commented on Mar 29, 2013

      serwymannequin
      MannequinAuthor

      The ClearWindow.py extension as included in the IdleX package contains a more recent version that allows undo with full color tag restoration.

    6. self-assigned this
      on Mar 29, 2013
    7. serwy commented on Apr 4, 2013

      serwymannequin
      MannequinAuthor

      In bpo-17632, Raymond suggests adding a "Restart and Clear" menu item under the Shell menu.

    8. taleinat commented on Feb 11, 2014

      @taleinat
      Contributor

      Removing text before "iomark" can be done by using the underlying Text widget directly. See how the Squeezer extension does this in bpo-1529353. This should simplify the implementation significantly.

      I also took a look at the version of the extension in IdleX. I'm thinking of adapting this for CPython's IDLE. Some questions:

      1. Is there a reason that text.dump() isn't called inside ClearWindowDeleteCommand.do()?
      2. What is the purpose of strip_ansi()?
      3. Does IDLE support multi-line prompts, or is that just IdleX? As a side note, it seems that in IDLE the debugger will override the prompt with '>>> ' whenever it is shut down (see PyShell.close_debugger); perhaps this is a bug?
      4. I see ClearWindowDeleteCommand just calls text.delete() and text.insert to modify text before the iomark. Will this work on regular IDLE as well, or this is possible only in IdleX?
    9. serwy commented on Feb 11, 2014

      serwymannequin
      MannequinAuthor

      Removing text before "iomark" can be done by using the underlying Text widget directly. See how the Squeezer extension does this in bpo-1529353. This should simplify the implementation significantly.

      I agree that the underlying .delete can be called directly, bypassing
      the ModifiedUndoDelegator in the percolator chain. I have mixed opinions
      about the Percolator, but that's another issue.

      1. Is there a reason that text.dump() isn't called inside ClearWindowDeleteCommand.do()?

      There is no compelling reason. I agree that it should be moved to .do()

      1. What is the purpose of strip_ansi()?

      That's an artifact from earlier code for IPython compatibility. The
      colored text prompts in IPython use ANSI escape codes, and the older
      undo code reinserted the text without restoring tags.

      Using text.dump() and the restore routine in undo() now restore the tags
      and eliminates the need for strip_ansi(). I haven't removed that old
      code yet.

      1. Does IDLE support multi-line prompts, or is that just IdleX? As a side note, it seems that in IDLE the debugger will override the prompt with '>>> ' whenever it is shut down (see PyShell.close_debugger); perhaps this is a bug?

      IDLE itself does support multi-line prompts. The debugger is a whole
      other can of worms.

      1. I see ClearWindowDeleteCommand just calls text.delete() and text.insert to modify text before the iomark. Will this work on regular IDLE as well, or this is possible only in IdleX?

      It should work on regular IDLE. IdleX wraps the existing CPython IDLE.

    10. taleinat commented on Feb 12, 2014

      @taleinat
      Contributor

      Roger, thanks for the quick response!

      I'll try to get to working on this in the coming days.

    11. taleinat commented on Feb 12, 2014

      @taleinat
      Contributor

      Terry, if I wrote an appropriate patch, would it have a chance of being accepted?

    12. terryjreedy commented on Feb 12, 2014

      @terryjreedy
      Member

      Without having read the dialog to know what you mean by appropriate, and how a new patch would be different from Roger's 5 year old patch, or how this issue relates toSqueezer, yes. I have occasionally closed the shell and reopened (perhaps by running a file) to clear the shell. I would like to reduce the need for that by putting help output in a separate read-only output window. But there is still the occasional 2000 line traceback for hitting the recursion limit, and other mishaps.

    13. 30 remaining items

    14. terryjreedy commented on Jul 31, 2020

      @terryjreedy
      Member

      @PokerStar: First we have to agree on several things.

      1. What is the need, what are the use cases? When do *you* feel a need to clear the shell?

      2. What is the basic strategy?
        a. Delete selections: make delete, backspace, and cut work in the shell, as in editor.
        I prefer this. No new doc or menu entries needed.
        People can delele less than everything.
        b. New feature. All or nothing. Changes 8 files. There was discussin of how to delete, but seems not difficult.

      3. Details need to be agreed on.
        If new feature, menu entry and shortcut: I thing 'Clear shell' is enough. Patch disagrees with discussion on shortcut.
        Should header be deleted or left? (Delete selection lets user decide.)
        Should deleted text be undoable? We do not currently allow any editing of shell history? Undo amounts to pasting or retyping, and if we do not allow pasting or typing in general, why with undo?
        Should squeezes text be expanded? I think not. help(tkinter) produces over 22000 lines.
        Should the current prompt and any code be deleted? I think not. If prompt gets deleted, it has to be re-written, but without current in-process entry.
        Should remote process be restarted? Why? This can easily be done independently and might not be wanted.
        <probably something else>

      4. New behavior must be tested. The is usually the hardest part of a patch, which is why it is neglected. Incomplete testing may result in adding a new bug. It was just reported that closing IDLE while the Shell is waiting for a response to input(prompt) leads to incomplete shutdown and a zombie process. On macOS, this can lead to a crash and possibly a need to reboot.
        So, should we allow clearing, deletion while user code is executing? While user code is requesting input? Or must we prohibit? I don't know, other than this must be tested at least on Windows and macOS.

      Overall, I prefer to use the deletion mechanism we have, rather than add a new one.

    15. terryjreedy commented on Mar 2, 2022

      @terryjreedy
      Member

      https://stackoverflow.com/questions/71290382/how-do-i-adjust-python-idle-shell-settings-so-that-the-next-line-of-code-i-type
      is from someone who, for whatever reason, likes having blank space in the shell below the input prompt. It is not clear whether deleting lines to make that happen would be satisfactory, or if all lines must be accessible.

    16. added
      3.11only security fixes
      and removed on Mar 2, 2022
    17. transferred this issue fromon Apr 10, 2022
    18. moved this to In Progress in IDLE Issueson Jul 27, 2022
    19. jamesderlin commented on Jun 16, 2025

      @jamesderlin

      For whatever my opinions are worth:

      @PokerStar: First we have to agree on several things.

      1. What is the need, what are the use cases? When do you feel a need to clear the shell?

      My use case would be for introducing someone to programming where the IDLE shell showing them old error messages and output is potentially confusing, especially if they're primarily using the editor window and choosing "Run" rather than using the shell window interactively.

      Currently I suppose people can close and reopen the shell window if an editor window is left open, but closing/reopening the shell window is awkward, especially since it currently does not preserve its previous window placement.

      1. What is the basic strategy?
        a. Delete selections: make delete, backspace, and cut work in the shell, as in editor.
        I prefer this. No new doc or menu entries needed.
        People can delele less than everything.
        b. New feature. All or nothing. Changes 8 files. There was discussin of how to delete, but seems not difficult.

      I do not like (A). Allowing delete or backspace to work directly in the shell window would make it more like the editor window, and that could be more confusing. It also would be confusing if someone accidentally deleted text from the shell window without realizing it.

      1. Details need to be agreed on.
        If new feature, menu entry and shortcut: I thing 'Clear shell' is enough. Patch disagrees with discussion on shortcut.

      A "Clear shell" menu sounds fine to me, but I think "Clear and restart shell" probably would be much simpler (see point 4 below) and would be sufficient for most people.

      I don't see a need for a keyboard shortcut/hotkey since people can use still use the keyboard with menu accelerators.

      Should header be deleted or left? (Delete selection lets user decide.)

      If clearing would also restart the shell, then IMO the shell window should look the same as when it was initially started.

      Should deleted text be undoable? We do not currently allow any editing of shell history? Undo amounts to pasting or retyping, and if we do not allow pasting or typing in general, why with undo?

      I'd prefer an all-or-nothing clearing ability not allowing arbitrary deletion of text from the shell window, and for that scenario I think it'd be fine to not have any undo capability.

      Should squeezes text be expanded? I think not. help(tkinter) produces over 22000 lines.

      With an all-or-nothing clearing ability, everything would be cleared anyway.

      1. New behavior must be tested. The is usually the hardest part of a patch, which is why it is neglected. Incomplete testing may result in adding a new bug. It was just reported that closing IDLE while the Shell is waiting for a response to input(prompt) leads to incomplete shutdown and a zombie process. On macOS, this can lead to a crash and possibly a need to reboot.
        So, should we allow clearing, deletion while user code is executing? While user code is requesting input? Or must we prohibit? I don't know, other than this must be tested at least on Windows and macOS.

      If clearing is tied to restarting the shell (i.e., if the menu item is "Clear and restart shell" menu or if it's some checkbox when restarting the shell), then the testing scenarios should be the same as for restarting the shell.

    20. added
      stdlibStandard Library Python modules in the Lib/ directory
      and removed
      3.11only security fixes
      on Oct 6, 2025
    21. terryjreedy commented on Oct 6, 2025

      @terryjreedy
      Member

      @jamesderlin Thanks for the comments.

      I think there are two main uses cases, which would have different solutions.

      1. Clear shell - likely with restart and likely without undo. In other words, same as current close and restart but easier. Option: popup with "This is irreversible. Continue?" with 'yes' the default. So ^L, .
      2. Remove last output (likely error message) and continue editing last entry. Needed when have clean interaction and make a mistake and want to redo. Would undo (erase) current entry first. No need for undo/redo of erased output because without editing would repeat the error. This could be repeatable. Perhaps erased entries would be redo-able.

      Anything else could likely be handled be saving with prompts (or save with output commented -- to be added) and editing in editor.

    22. terryjreedy commented on Oct 6, 2025

      @terryjreedy
      Member

      Before doing anything, I would likely open an 'Ideas' thread on discuss.python.org.

    23. removed
      stdlibStandard Library Python modules in the Lib/ directory
      on Jan 15, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions