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

gh-111201: Support pyrepl on Windows #119559

Merged
merged 70 commits into from
May 31, 2024
Merged

gh-111201: Support pyrepl on Windows #119559

merged 70 commits into from
May 31, 2024

Conversation

DinoV
Copy link
Contributor

@DinoV DinoV commented May 25, 2024

This adds Windows support for the new pyrepl.

Support is a mix of vt100 codes, enabling the vt100 codes on the Windows console, and Windows console APIs.

Minimum version required in Windows 10.


📚 Documentation preview 📚: https://cpython-previews--119559.org.readthedocs.build/

Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
@lysnikolaou lysnikolaou added OS-windows topic-repl Related to the interactive shell labels May 30, 2024
Copy link

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey so sorry for coming out of nowhere to leave notes on this. I'm super excited to see this land! I took a spin through this PR just to see if I could find anything egregious in here from a Windows Console standpoint, and nothing really popped out.

I obviously don't really know the pyrepl codebase closely, so my review probably isn't the most valuable. I left my own notes-to-self with a "📝:", mostly just for things I found confusing. Definitely feel free to ignore those.

overall I'd say: I'm super glad y'all aren't going to be using COOKED_READ in the console anymore 🎉

info = CONSOLE_SCREEN_BUFFER_INFO()
if not GetConsoleScreenBufferInfo(OutHandle, info):
raise WinError(GetLastError())
return info.dwCursorPosition.X, info.dwCursorPosition.Y

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝: This is relative to the origin of the buffer, not of the viewport. is this supposed to be viewport relative in the unix version?

Lib/_pyrepl/windows_console.py Outdated Show resolved Hide resolved
Lib/_pyrepl/windows_console.py Outdated Show resolved Hide resolved

return Event(evt="key", data=key, raw=rec.Event.KeyEvent.uChar.UnicodeChar)

def push_char(self, char: int | bytes) -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I don't totally understand how this is supposed to be used, but there is WriteConsoleInput which sounds similar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is specifically about push_char? I don't think it's actually used but it does seem to technically be public API surface. The unix version calls this on inputted chars.


self.__posxy = 0, 0
self.__gone_tall = 0
self.__offset = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝: the unix version also:

  • enables bracketed paste
  • does a smkx (\x1b[?1h)
  • gets the original size (to restore on exit?)

I'm pretty sure the console supported ?1h back to RS1, and bracketed paste as of microsoft/terminal#15155 (not sure exactly where in Windows 11 that gets enabled)

You're probably fine continuing to not do them here, but they could maybe get added later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pablogsal
Copy link
Member

Thanks for your notes @zadjii-msft!

One problem we are still having I think is that resizing the terminal when there are wide Unicode characters such as emojis in many lines doesn't work (the characters don't get resposition after a resize makes the lines warp around and back to normal). This looks like it's a windows only problem because the unix terminal handles that correctly.

I think @ambv or @DinoV may be able to provide more information.

@ambv
Copy link
Contributor

ambv commented May 31, 2024

Unicode input and emojis work now. Amazing progress! I'm landing this, we can add further improvements like bracketed paste in future updates. Thanks, @DinoV! ✨ 🍰 ✨

@ambv ambv merged commit 0d07182 into python:main May 31, 2024
41 checks passed
@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 FreeBSD14 3.x has failed when building commit 0d07182.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/1232/builds/2462) and take a look at the build logs.
  4. Check if the failure is related to this commit (0d07182) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/all/#builders/1232/builds/2462

Failed tests:

  • test_pyrepl

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/test/libregrtest/single.py", line 181, in _runtest_env_changed_exc
    _load_run_test(result, runtests)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/test/libregrtest/single.py", line 138, in _load_run_test
    regrtest_runner(result, test_func, runtests)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/test/libregrtest/single.py", line 91, in regrtest_runner
    test_result = test_func()
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/test/libregrtest/single.py", line 135, in test_func
    return run_unittest(test_mod)
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/test/libregrtest/single.py", line 35, in run_unittest
    raise Exception("errors while loading tests")
Exception: errors while loading tests


Traceback (most recent call last):
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/unittest/loader.py", line 396, in _find_test_path
    module = self._get_module_from_name(name)
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/unittest/loader.py", line 339, in _get_module_from_name
    __import__(name)
    ~~~~~~~~~~^^^^^^
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/test/test_pyrepl/test_windows_console.py", line 5, in <module>
    from _pyrepl.windows_console import (
    ...<5 lines>...
    )
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/_pyrepl/windows_console.py", line 30, in <module>
    import ctypes
  File "/home/buildbot/buildarea/3.x.opsec-fbsd14/build/Lib/ctypes/__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'

@ambv
Copy link
Contributor

ambv commented May 31, 2024

Buildbot failure related. Fixing now.

@miss-islington-app
Copy link

Thanks @DinoV for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Thanks @DinoV for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 31, 2024
(cherry picked from commit 0d07182)

Co-authored-by: Dino Viehland <dinoviehland@gmail.com>
Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 31, 2024
(cherry picked from commit 0d07182)

Co-authored-by: Dino Viehland <dinoviehland@gmail.com>
Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
@bedevere-app
Copy link

bedevere-app bot commented May 31, 2024

GH-119850 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label May 31, 2024
@zadjii-msft
Copy link

zadjii-msft commented May 31, 2024

One problem we are still having I think is that resizing the terminal when there are wide Unicode characters such as emojis in many lines doesn't work (the characters don't get resposition after a resize makes the lines warp around and back to normal). This looks like it's a windows only problem because the unix terminal handles that correctly

Oh that for sure sounds like a us bug. We've been making pretty dramatic improvements to the unicode / emoji support over the last couple years, so I'm guessing you're seeing different behavior even depending on what version of Windows & the Terminal you're running.

If you've got specific bugs on your tracker for those, point me at 'em, and I can get the team to double check them ☺️


EDIT: derp I only read one mail, then replied to it before I saw #119559 (comment).

if sys.platform != "win32":
CAN_USE_PYREPL = True
else:
CAN_USE_PYREPL = sys.getwindowsversion().build >= 10586 # Windows 10 TH2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This merged as-is which is OK, but I wanted to chip in...

There is a minimal risk of cutting out terminals that would support pyrepl on earlier versions of Windows. The "most correct" (also most soapboxy, sorry!) way to determine support for this is to just try to set ENABLE_VIRTUAL_TERMINAL_PROCESSING and only fall back to basic console APIs if it fails. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DHowett sounds like an improvement we can still make! Care to PR this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try - at the very least, it would be a great opportunity for me to learn how to build and deploy Python 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DHowett see here and let me know if anything's missing:
https://devguide.python.org/getting-started/setup-building/#windows

ambv added a commit that referenced this pull request May 31, 2024
(cherry picked from commit 0d07182)

Co-authored-by: Dino Viehland <dinoviehland@gmail.com>
Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Co-authored-by: Anthony Shaw <anthony.p.shaw@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows topic-repl Related to the interactive shell
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants