forked from aniani/vim
patch 8.2.1830: MS-Windows: Python3 issue with stdin
Problem: MS-Windows: Python3 issue with stdin. Solution: Check if stdin is readable. (Ken Takata, closes #7106)
This commit is contained in:
parent
bd6428b9e7
commit
c6ed254d9f
@ -908,8 +908,27 @@ python3_loaded(void)
|
|||||||
static wchar_t *py_home_buf = NULL;
|
static wchar_t *py_home_buf = NULL;
|
||||||
|
|
||||||
#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
|
#if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0)
|
||||||
// Python 3.5 or later will abort inside Py_Initialize() when stdin is
|
/*
|
||||||
// redirected. Reconnect stdin to NUL.
|
* Return TRUE if stdin is readable from Python 3.
|
||||||
|
*/
|
||||||
|
static BOOL
|
||||||
|
is_stdin_readable(void)
|
||||||
|
{
|
||||||
|
DWORD mode, eventnum;
|
||||||
|
struct _stat st;
|
||||||
|
int fd = fileno(stdin);
|
||||||
|
HANDLE hstdin = (HANDLE)_get_osfhandle(fd);
|
||||||
|
|
||||||
|
// Check if stdin is connected to the console.
|
||||||
|
if (GetConsoleMode(hstdin, &mode))
|
||||||
|
// Check if it is opened as input.
|
||||||
|
return GetNumberOfConsoleInputEvents(hstdin, &eventnum);
|
||||||
|
|
||||||
|
return _fstat(fd, &st) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Python 3.5 or later will abort inside Py_Initialize() when stdin has
|
||||||
|
// been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$.
|
||||||
// Note that the python DLL is linked to its own stdio DLL which can be
|
// Note that the python DLL is linked to its own stdio DLL which can be
|
||||||
// differ from Vim's stdio.
|
// differ from Vim's stdio.
|
||||||
static void
|
static void
|
||||||
@ -917,7 +936,6 @@ reset_stdin(void)
|
|||||||
{
|
{
|
||||||
FILE *(*py__acrt_iob_func)(unsigned) = NULL;
|
FILE *(*py__acrt_iob_func)(unsigned) = NULL;
|
||||||
FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
|
FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL;
|
||||||
char *stdin_name = "NUL";
|
|
||||||
HINSTANCE hinst;
|
HINSTANCE hinst;
|
||||||
|
|
||||||
# ifdef DYNAMIC_PYTHON3
|
# ifdef DYNAMIC_PYTHON3
|
||||||
@ -925,7 +943,7 @@ reset_stdin(void)
|
|||||||
# else
|
# else
|
||||||
hinst = GetModuleHandle(PYTHON3_DLL);
|
hinst = GetModuleHandle(PYTHON3_DLL);
|
||||||
# endif
|
# endif
|
||||||
if (hinst == NULL)
|
if (hinst == NULL || is_stdin_readable())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Get "freopen" and "stdin" which are used in the python DLL.
|
// Get "freopen" and "stdin" which are used in the python DLL.
|
||||||
@ -938,14 +956,12 @@ reset_stdin(void)
|
|||||||
if (hpystdiodll)
|
if (hpystdiodll)
|
||||||
pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
|
pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen");
|
||||||
}
|
}
|
||||||
if (isatty(fileno(stdin)))
|
|
||||||
stdin_name = "CONIN$";
|
|
||||||
|
|
||||||
// Reconnect stdin to NUL or CONIN$.
|
// Reconnect stdin to CONIN$.
|
||||||
if (pyfreopen != NULL)
|
if (pyfreopen != NULL)
|
||||||
pyfreopen(stdin_name, "r", py__acrt_iob_func(0));
|
pyfreopen("CONIN$", "r", py__acrt_iob_func(0));
|
||||||
else
|
else
|
||||||
freopen(stdin_name, "r", stdin);
|
freopen("CONIN$", "r", stdin);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
# define reset_stdin()
|
# define reset_stdin()
|
||||||
|
@ -750,6 +750,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
1830,
|
||||||
/**/
|
/**/
|
||||||
1829,
|
1829,
|
||||||
/**/
|
/**/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user