Skip to content

windows console: -i parameter and redirected output gives error in 3.13 #132962

Closed as duplicate

Description

@johns1c

Bug report

Bug description:

# Add a code block here, if required

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Activity

  1. johns1c commented on Apr 25, 2025

    @johns1c
    Author

    A simple hello world program crashes if you mix the -i parameter with output redirection. I am using the Windows 11 WindowsTerminal.exe.

    it did not happen using python 3.12 and can be prevented by setting the environment variable PYTHON_BASIC_REPL. e.g.

    set PYTHON_BASIC_REPL=anyvalue
    

    The failure gives the following output:-

    
    (z313) C:\Users\Chris Johnson\Python\z313>py -i test0.py > t.t
    Traceback (most recent call last):
      File "C:\Python313\Lib\_pyrepl\main.py", line 59, in interactive_console
        run_multiline_interactive_console(console)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
      File "C:\Python313\Lib\_pyrepl\simple_interact.py", line 143, in run_multiline_interactive_console
        statement = multiline_input(more_lines, ps1, ps2)
      File "C:\Python313\Lib\_pyrepl\readline.py", line 389, in multiline_input
        return reader.readline()
               ~~~~~~~~~~~~~~~^^
      File "C:\Python313\Lib\_pyrepl\reader.py", line 796, in readline
        self.prepare()
        ~~~~~~~~~~~~^^
      File "C:\Python313\Lib\_pyrepl\historical_reader.py", line 306, in prepare
        super().prepare()
        ~~~~~~~~~~~~~~~^^
      File "C:\Python313\Lib\_pyrepl\reader.py", line 636, in prepare
        self.console.prepare()
        ~~~~~~~~~~~~~~~~~~~~^^
      File "C:\Python313\Lib\_pyrepl\windows_console.py", line 317, in prepare
        self.height, self.width = self.getheightwidth()
                                  ~~~~~~~~~~~~~~~~~~~^^
      File "C:\Python313\Lib\_pyrepl\windows_console.py", line 361, in getheightwidth
        raise WinError(GetLastError())
    OSError: [WinError 0] The operation completed successfully.
    
    
  2. StanFromIreland commented on Apr 25, 2025

    @StanFromIreland
    Member

    Unable to reproduce with main branch on linux.

    The provided traceback is odd, 0 exit code?:
    OSError: [WinError 0] The operation completed successfully.

    Also by the traceback this is a repl issue.

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    topic-replRelated to the interactive shell
    on Apr 25, 2025
  4. sergey-miryanov commented on Apr 25, 2025

    @sergey-miryanov
    Contributor

    I can confirm it is reproduced on latest main on Windows.
    Also, it is a bit interleaved with @chris-eibl PR https://github.com/python/cpython/pull/132884/files in the GetLastError part - if we use get_last_error we get following error:

    Traceback (most recent call last):
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\main.py", line 59, in interactive_console
        run_multiline_interactive_console(console)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\simple_interact.py", line 137, in run_multiline_interactive_console
        statement = multiline_input(more_lines, ps1, ps2)
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\readline.py", line 396, in multiline_input
        return reader.readline()
               ~~~~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\reader.py", line 744, in readline
        self.prepare()
        ~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\historical_reader.py", line 306, in prepare
        super().prepare()
        ~~~~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\reader.py", line 584, in prepare
        self.console.prepare()
        ~~~~~~~~~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\windows_console.py", line 347, in prepare
        self.height, self.width = self.getheightwidth()
                                  ~~~~~~~~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\windows_console.py", line 400, in getheightwidth
        raise WinError(get_last_error())
    OSError: [WinError 6] The handle is invalid.
    

    And thus, we have a clue about OutHandle can't be used when redirect enabled.
    If we add following check:

    
        GetFileType = _KERNEL32.GetFileType
        GetFileType.argtypes = [HANDLE]
        GetFileType.restype = DWORD
    
        FILE_TYPE_CHAR = 0x0002
        IsOutputRedirected = FILE_TYPE_CHAR != GetFileType(OutHandle)
    

    and following fix

        def getheightwidth(self) -> tuple[int, int]:
            """Return (height, width) where height and width are the height
            and width of the terminal window in characters."""
            info = CONSOLE_SCREEN_BUFFER_INFO()
            if IsOutputRedirected:
                return (1,1) # FIXME:
            if not GetConsoleScreenBufferInfo(OutHandle, info):
                raise WinError(get_last_error())
            return (
                info.srWindow.Bottom - info.srWindow.Top + 1,
                info.srWindow.Right - info.srWindow.Left + 1,
            )
    

    we get removed original error, but if we try to input something we get another one (but very same):

    Traceback (most recent call last):
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\main.py", line 59, in interactive_console
        run_multiline_interactive_console(console)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\simple_interact.py", line 137, in run_multiline_interactive_console
        statement = multiline_input(more_lines, ps1, ps2)
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\readline.py", line 396, in multiline_input
        return reader.readline()
               ~~~~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\reader.py", line 750, in readline
        self.handle1()
        ~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\reader.py", line 733, in handle1
        self.do_cmd(cmd)
        ~~~~~~~~~~~^^^^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\reader.py", line 663, in do_cmd
        self.refresh()
        ~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\reader.py", line 641, in refresh
        self.console.refresh(self.screen, self.cxy)
        ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\windows_console.py", line 193, in refresh
        self._scroll(scroll_lines, self._getscrollbacksize())
                                   ~~~~~~~~~~~~~~~~~~~~~~~^^
      File "D:\Sources\_pythonish\cpython\Lib\_pyrepl\windows_console.py", line 409, in _getscrollbacksize
        raise WinError(GetLastError())
    OSError: [WinError 0] The operation completed successfully.
    

    And if we fix _getscrollbacksize and _scroll then we can exit from repl and get following text in t.t:

    Running Release|x64 interpreter...
    test
    �[?2004h�[?25l�[1A
    �[?25l�[?25l>>> �[?25h�[?25l�[?25l�[4D�[K\�[4D�[1B�[?25h�[1C�[1A�[?25l�[?25h�[?25l�[?25h�[?25l�[?25h�[?25l�[?25h�[?25l�[?25h�[?25l�[?25h�[1D
    �[?2004l
    
  5. chris-eibl commented on Apr 25, 2025

    @chris-eibl
    Member

    The reason why we see

    OSError: [WinError 0] The operation completed successfully.
    

    using raise WinError(GetLastError()): #132888.

  6. added 4 commits that reference this issue on Jun 13, 2025
  7. added a commit that references this issue on Jun 16, 2025
  8. added a commit that references this issue on Jun 16, 2025
  9. vstinner commented on Feb 3, 2026

    @vstinner
    Member

    This issue has been fixed by the change #144248.

    Example:

    C:> python -i test0.py >t.t
    warning: can't use pyrepl: [WinError 6] Invalid descriptor
    >>>
    
  10. StanFromIreland commented on Feb 3, 2026

    @StanFromIreland
    Member

    Closing as duplicate of the attached issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsstdlibStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions