Skip to content

Commit 15d3d98

Browse files
committed
Improve Windows Python 3.12 compatibility
This fixes crash when running menuconfig on Windows with Python 3.12+ due to windows-curses bug. The setupterm() function in windows-curses crashes on Python 3.12, making menuconfig unusable. Reference: zephyrproject-rtos/windows-curses#50
1 parent ee5b93c commit 15d3d98

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

menuconfig.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,49 @@ def menuconfig(kconf):
804804

805805
# Enter curses mode. _menuconfig() returns a string to print on exit, after
806806
# curses has been de-initialized.
807-
print(curses.wrapper(_menuconfig))
807+
print(_wrapper(_menuconfig))
808+
809+
810+
def _wrapper(func):
811+
# Workaround for windows-curses bug on Python 3.12+
812+
# See: https://github.com/zephyrproject-rtos/windows-curses/issues/50
813+
if os.name == "nt" and sys.version_info >= (3, 12):
814+
stdscr = None
815+
try:
816+
import _curses
817+
except ImportError:
818+
# _curses not available, fall back to standard wrapper
819+
return curses.wrapper(func)
820+
821+
try:
822+
# setupterm() crashes on Python 3.12 with windows-curses
823+
stdscr = _curses.initscr()
824+
825+
# Copy ACS_* and LINES/COLS to curses module
826+
for key, value in _curses.__dict__.items():
827+
if key.startswith("ACS_") or key in ("LINES", "COLS"):
828+
setattr(curses, key, value)
829+
830+
curses.noecho()
831+
curses.cbreak()
832+
833+
try:
834+
curses.start_color()
835+
except curses.error:
836+
# Color support not available
837+
pass
838+
839+
if stdscr is not None:
840+
stdscr.keypad(True)
841+
return func(stdscr)
842+
finally:
843+
if stdscr is not None:
844+
stdscr.keypad(False)
845+
curses.echo()
846+
curses.nocbreak()
847+
curses.endwin()
848+
else:
849+
return curses.wrapper(func)
808850

809851

810852
def _load_config():

0 commit comments

Comments
 (0)