Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDLE: cleanup use of sys.ps1 and never set it. #76039

terryjreedy opened this issue Oct 24, 2017 · 3 comments

IDLE: cleanup use of sys.ps1 and never set it. #76039

terryjreedy opened this issue Oct 24, 2017 · 3 comments
3.7 (EOL) end of life topic-IDLE type-bug An unexpected behavior, bug, or error


Copy link

BPO 31858
Nosy @terryjreedy
  • bpo-31858: IDLE -- Restrict shell prompt manipulaton to the shell. #4143
  • [3.6] bpo-31858: IDLE -- Restrict shell prompt manipulaton to the shell. (GH-4143) #4155
  • 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 = ''
    closed_at = <Date 2017-10-28.01:43:38.637>
    created_at = <Date 2017-10-24.05:58:28.400>
    labels = ['expert-IDLE', 'type-bug', '3.7']
    title = 'IDLE: cleanup use of sys.ps1 and never set it.'
    updated_at = <Date 2018-12-29.04:48:34.856>
    user = '' fields:

    activity = <Date 2018-12-29.04:48:34.856>
    actor = 'terry.reedy'
    assignee = 'terry.reedy'
    closed = True
    closed_date = <Date 2017-10-28.01:43:38.637>
    closer = 'terry.reedy'
    components = ['IDLE']
    creation = <Date 2017-10-24.05:58:28.400>
    creator = 'terry.reedy'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31858
    keywords = ['patch', 'patch']
    message_count = 3.0
    messages = ['304860', '305095', '332699']
    nosy_count = 1.0
    nosy_names = ['terry.reedy']
    pr_nums = ['4143', '4155']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = ''
    versions = ['Python 3.6', 'Python 3.7']

    Copy link
    Member Author

    This issue is about cleaning up IDLE's use of sys.ps1 for its prompt (sys.ps2 is not used). A. This will make for cleaner code and fix some bugs. B. This will be better for testing. (Some possible changes to pyshell might make sys.ps1 irrelevant, but that is for the future.)

    Part1. editor.EditorWindow.__init__ sets sys.ps1 to '>>> ' if not set.
            except AttributeError:
                sys.ps1 = '>>> '
    IDLE initially respects a user setting of sys.ps1 in a startup file.
    pyshell.PyShell.open_debugger hasand .close_debugger has these lines
            sys.ps1 = "[DEBUG ON]\n>>> "
                sys.ps1 = ">>> "
    These overwrite any user setting of sys.ps1.  As long as IDLE pays attention to the initial value of sys.ps1, I consider this a bug.

    pyshell.PyShell.show_prompt starts with
    s = str(sys.ps1)
    s = ""

    I suspect that this is a holdover from when IDLE executed user code in its own process, as it still does with the deprecated '-n' option.  However, if a -n user deletes sys.ps1, the replacement should be '>>> ', not '' (bug 2).

    In the current default subprocess mode, users cannot change the IDLE process sys module (see bpo-13657), so rereading sys.ps1 for every editor window and prompt is nonsensical.

    Patch 1: replace the EditorWindow.__init__ code with module level code
    sys_ps1 = sys.ps1 if hasattr(sys, 'ps1') else '>>> '
    prompt = sys_ps1

    Fix pyshell to import editor rather than two of its objects. In its debugger methods, set editor.prompt, using editor.sys_ps1, thus preserving any user setting. In print_prompt, print the current editor.prompt. (This will disable users resetting ps1 in deprecated -n mode, but I consider that okay as it matches the normal mode.)

    Part 2. The reason the prompt is set in EditorWindow, instead of where is it obviously needed, the PyShell subclass of the OutputWindow subclass of EditorWindow, is that it is currently used in EditorWindow.smart_backspace_event and .newline_and_indent_event, while pyshell imports editor (and must), and not the other way around.

    Treating the prompt text as special in an editor lead to a bug in smart_backspace that was fixed in python/cpython#57248 by guarding it use with self.context_use_ps1.  There is still a nearly inconsequential bug in the newline method where the prompt use is not guarded.  (Hitting newline with the cursor before the 't' in '>>> test' leaves the preceding space instead of deleting it.)

    Patch 2: Only the last line of the prompt is relevant in either method. I believe that replacing self.context_use_ps1 = False in an editor, = True in Shell with self.last_prompt_line = '' in an editor, = whatever it is in Shell, will allow moving sys_ps1 and prompt to pyshell. This would simplify patch 1.

    @terryjreedy terryjreedy added the 3.7 (EOL) end of life label Oct 24, 2017
    @terryjreedy terryjreedy self-assigned this Oct 24, 2017
    @terryjreedy terryjreedy added topic-IDLE type-bug An unexpected behavior, bug, or error labels Oct 24, 2017
    Copy link
    Member Author

    I put the new shell variables into PyShell itself. There is usually only one instance created in a session.

    I tested the patch manually in both shell and editor with both the default prompt and with sys.ps1 set before importing idlelib.idle. Beginning to automate tests for editor and shell is a project in itself.

    Copy link
    Member Author

    By mistake, I deleted the setting of context_use_ps1 in PyShell while still in use in several places. Fixed in bpo-34055.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    3.7 (EOL) end of life topic-IDLE type-bug An unexpected behavior, bug, or error
    None yet

    No branches or pull requests

    1 participant