[3.15] gh-151695: Fix use-after-free of the curses screen encoding (GH-151696) (GH-151703)
The module-global curses_screen_encoding stored a borrowed pointer to the
encoding owned by the window returned by the first initscr() call. That
window can be deallocated while unctrl() and ungetch(), which have no window
of their own, still use the pointer to encode non-ASCII characters.
Keep a private copy of the encoding instead.
(cherry picked from commit
551f8e16f8bb38a1e9c6df259a2a0969493de070)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>