Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5dbbf1a

Browse files
committed
Issue #23735: Add SIGWINCH handler for Readline 6.3+ support, by Eric Price
1 parent acc0319 commit 5dbbf1a

6 files changed

Lines changed: 87 additions & 0 deletions

File tree

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,7 @@ Florian Preinstorfer
11551155
Amrit Prem
11561156
Paul Prescod
11571157
Donovan Preston
1158+
Eric Price
11581159
Paul Price
11591160
Iuliia Proskurnia
11601161
Dorian Pula

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ Core and Builtins
9999
Library
100100
-------
101101

102+
- Issue #23735: Handle terminal resizing with Readline 6.3+ by installing our
103+
own SIGWINCH handler. Patch by Eric Price.
104+
102105
- Issue #26586: In http.server, respond with "413 Request header fields too
103106
large" if there are too many header fields to parse, rather than killing
104107
the connection and raising an unhandled exception. Patch by Xiang Zhang.

Modules/readline.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,26 @@ on_completion_display_matches_hook(char **matches,
928928

929929
#endif
930930

931+
#ifdef HAVE_RL_RESIZE_TERMINAL
932+
static volatile sig_atomic_t sigwinch_received;
933+
static sighandler_t sigwinch_ohandler;
934+
935+
static void
936+
readline_sigwinch_handler(int signum)
937+
{
938+
sigwinch_received = 1;
939+
if (sigwinch_ohandler &&
940+
sigwinch_ohandler != SIG_IGN && sigwinch_ohandler != SIG_DFL)
941+
sigwinch_ohandler(signum);
942+
943+
#ifndef HAVE_SIGACTION
944+
/* If the handler was installed with signal() rather than sigaction(),
945+
we need to reinstall it. */
946+
PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
947+
#endif
948+
}
949+
#endif
950+
931951
/* C function to call the Python completer. */
932952

933953
static char *
@@ -1033,6 +1053,10 @@ setup_readline(readlinestate *mod_state)
10331053
/* Bind both ESC-TAB and ESC-ESC to the completion function */
10341054
rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
10351055
rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
1056+
#ifdef HAVE_RL_RESIZE_TERMINAL
1057+
/* Set up signal handler for window resize */
1058+
sigwinch_ohandler = PyOS_setsig(SIGWINCH, readline_sigwinch_handler);
1059+
#endif
10361060
/* Set our hook functions */
10371061
rl_startup_hook = on_startup_hook;
10381062
#ifdef HAVE_RL_PRE_INPUT_HOOK
@@ -1118,6 +1142,13 @@ readline_until_enter_or_signal(const char *prompt, int *signal)
11181142
struct timeval *timeoutp = NULL;
11191143
if (PyOS_InputHook)
11201144
timeoutp = &timeout;
1145+
#ifdef HAVE_RL_RESIZE_TERMINAL
1146+
/* Update readline's view of the window size after SIGWINCH */
1147+
if (sigwinch_received) {
1148+
sigwinch_received = 0;
1149+
rl_resize_terminal();
1150+
}
1151+
#endif
11211152
FD_SET(fileno(rl_instream), &selectset);
11221153
/* select resets selectset if no input was available */
11231154
has_input = select(fileno(rl_instream) + 1, &selectset,

configure

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14929,6 +14929,50 @@ $as_echo "#define HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK 1" >>confdefs.h
1492914929
fi
1493014930

1493114931

14932+
# also in 4.0, but not in editline
14933+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for rl_resize_terminal in -lreadline" >&5
14934+
$as_echo_n "checking for rl_resize_terminal in -lreadline... " >&6; }
14935+
if ${ac_cv_lib_readline_rl_resize_terminal+:} false; then :
14936+
$as_echo_n "(cached) " >&6
14937+
else
14938+
ac_check_lib_save_LIBS=$LIBS
14939+
LIBS="-lreadline $READLINE_LIBS $LIBS"
14940+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
14941+
/* end confdefs.h. */
14942+
14943+
/* Override any GCC internal prototype to avoid an error.
14944+
Use char because int might match the return type of a GCC
14945+
builtin and then its argument prototype would still apply. */
14946+
#ifdef __cplusplus
14947+
extern "C"
14948+
#endif
14949+
char rl_resize_terminal ();
14950+
int
14951+
main ()
14952+
{
14953+
return rl_resize_terminal ();
14954+
;
14955+
return 0;
14956+
}
14957+
_ACEOF
14958+
if ac_fn_c_try_link "$LINENO"; then :
14959+
ac_cv_lib_readline_rl_resize_terminal=yes
14960+
else
14961+
ac_cv_lib_readline_rl_resize_terminal=no
14962+
fi
14963+
rm -f core conftest.err conftest.$ac_objext \
14964+
conftest$ac_exeext conftest.$ac_ext
14965+
LIBS=$ac_check_lib_save_LIBS
14966+
fi
14967+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_readline_rl_resize_terminal" >&5
14968+
$as_echo "$ac_cv_lib_readline_rl_resize_terminal" >&6; }
14969+
if test "x$ac_cv_lib_readline_rl_resize_terminal" = xyes; then :
14970+
14971+
$as_echo "#define HAVE_RL_RESIZE_TERMINAL 1" >>confdefs.h
14972+
14973+
fi
14974+
14975+
1493214976
# check for readline 4.2
1493314977
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for rl_completion_matches in -lreadline" >&5
1493414978
$as_echo_n "checking for rl_completion_matches in -lreadline... " >&6; }

configure.ac

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4527,6 +4527,11 @@ AC_CHECK_LIB(readline, rl_completion_display_matches_hook,
45274527
AC_DEFINE(HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK, 1,
45284528
[Define if you have readline 4.0]), ,$READLINE_LIBS)
45294529

4530+
# also in 4.0, but not in editline
4531+
AC_CHECK_LIB(readline, rl_resize_terminal,
4532+
AC_DEFINE(HAVE_RL_RESIZE_TERMINAL, 1,
4533+
[Define if you have readline 4.0]), ,$READLINE_LIBS)
4534+
45304535
# check for readline 4.2
45314536
AC_CHECK_LIB(readline, rl_completion_matches,
45324537
AC_DEFINE(HAVE_RL_COMPLETION_MATCHES, 1,

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,9 @@
735735
/* Define if you have readline 4.0 */
736736
#undef HAVE_RL_PRE_INPUT_HOOK
737737

738+
/* Define if you have readline 4.0 */
739+
#undef HAVE_RL_RESIZE_TERMINAL
740+
738741
/* Define to 1 if you have the `round' function. */
739742
#undef HAVE_ROUND
740743

0 commit comments

Comments
 (0)