-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use workaround to fix python3.12 compatibility error
- Loading branch information
1 parent
2b67b1f
commit 66ed3bd
Showing
4 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
For some reason curses initscr doesn't work on python3.12, | ||
presumably due to a compilation bug or something. This | ||
module provides a functional workaround. | ||
Thanks to https://github.com/zephyrproject-rtos/windows-curses/issues/50 | ||
for the inspiration. | ||
""" | ||
|
||
import curses | ||
from typing import Any, Callable | ||
|
||
import _curses | ||
|
||
|
||
def initscr() -> Any: | ||
"""Return a stdscr that should be properly initialized""" | ||
stdscr = _curses.initscr() # pyright: ignore | ||
for key, value in _curses.__dict__.items(): | ||
if key[0:4] == "ACS_" or key in ("LINES", "COLS"): | ||
setattr(curses, key, value) | ||
return stdscr # pyright: ignore | ||
|
||
|
||
def wrapper(func: Callable[..., Any], /, *args: Any, **kwds: Any) -> Any: | ||
""" | ||
Wrapper function that initializes curses and calls another function, | ||
restoring normal keyboard/screen behavior on error. | ||
The callable object 'func' is then passed the main window 'stdscr' | ||
as its first argument, followed by any other arguments passed to | ||
wrapper(). | ||
""" | ||
|
||
try: | ||
stdscr = initscr() | ||
|
||
curses.noecho() | ||
curses.cbreak() | ||
|
||
stdscr.keypad(1) | ||
|
||
_curses.start_color() # pyright: ignore | ||
if hasattr(_curses, 'COLORS'): | ||
curses.COLORS = _curses.COLORS # pyright: ignore | ||
if hasattr(_curses, 'COLOR_PAIRS'): | ||
curses.COLOR_PAIRS = _curses.COLOR_PAIRS # pyright: ignore | ||
|
||
return func(stdscr, *args, **kwds) | ||
finally: | ||
if 'stdscr' in locals(): | ||
stdscr.keypad(0) # pyright: ignore | ||
curses.echo() | ||
curses.nocbreak() | ||
curses.endwin() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters