To: vim_dev@googlegroups.com Subject: Patch 8.2.1381 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1381 Problem: MS-Windows: crash with Python 3.5 when stdin is redirected. Solution: Reconnect stdin. (Yasuhiro Matsumoto, Ken Takata, closes #6641) Files: src/Make_cyg_ming.mak, src/Make_mvc.mak, src/if_python3.c *** ../vim-8.2.1380/src/Make_cyg_ming.mak 2020-07-22 19:10:59.877072059 +0200 --- src/Make_cyg_ming.mak 2020-08-06 21:41:31.157707858 +0200 *************** *** 588,593 **** --- 588,595 ---- CFLAGS += -DFEAT_PYTHON3 ifeq (yes, $(DYNAMIC_PYTHON3)) CFLAGS += -DDYNAMIC_PYTHON3 -DDYNAMIC_PYTHON3_DLL=\"$(DYNAMIC_PYTHON3_DLL)\" + else + CFLAGS += -DPYTHON3_DLL=\"$(DYNAMIC_PYTHON3_DLL)\" endif endif *** ../vim-8.2.1380/src/Make_mvc.mak 2020-07-22 19:10:59.877072059 +0200 --- src/Make_mvc.mak 2020-08-06 21:41:31.157707858 +0200 *************** *** 1026,1031 **** --- 1026,1034 ---- ! ifndef PYTHON3_VER PYTHON3_VER = 36 ! endif + ! ifndef DYNAMIC_PYTHON3_DLL + DYNAMIC_PYTHON3_DLL = python$(PYTHON3_VER).dll + ! endif ! message Python3 requested (version $(PYTHON3_VER)) - root dir is "$(PYTHON3)" ! if "$(DYNAMIC_PYTHON3)" == "yes" ! message Python3 DLL will be loaded dynamically *************** *** 1035,1043 **** PYTHON3_INC = /I "$(PYTHON3)\Include" /I "$(PYTHON3)\PC" ! if "$(DYNAMIC_PYTHON3)" == "yes" CFLAGS = $(CFLAGS) -DDYNAMIC_PYTHON3 \ ! -DDYNAMIC_PYTHON3_DLL=\"python$(PYTHON3_VER).dll\" PYTHON3_LIB = /nodefaultlib:python$(PYTHON3_VER).lib ! else PYTHON3_LIB = $(PYTHON3)\libs\python$(PYTHON3_VER).lib ! endif !endif --- 1038,1047 ---- PYTHON3_INC = /I "$(PYTHON3)\Include" /I "$(PYTHON3)\PC" ! if "$(DYNAMIC_PYTHON3)" == "yes" CFLAGS = $(CFLAGS) -DDYNAMIC_PYTHON3 \ ! -DDYNAMIC_PYTHON3_DLL=\"$(DYNAMIC_PYTHON3_DLL)\" PYTHON3_LIB = /nodefaultlib:python$(PYTHON3_VER).lib ! else + CFLAGS = $(CFLAGS) -DPYTHON3_DLL=\"$(DYNAMIC_PYTHON3_DLL)\" PYTHON3_LIB = $(PYTHON3)\libs\python$(PYTHON3_VER).lib ! endif !endif *** ../vim-8.2.1380/src/if_python3.c 2020-07-16 22:15:48.944513013 +0200 --- src/if_python3.c 2020-08-06 21:41:31.157707858 +0200 *************** *** 907,912 **** --- 907,953 ---- static wchar_t *py_home_buf = NULL; + #if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0) + // Python 3.5 or later will abort inside Py_Initialize() when stdin is + // redirected. Reconnect stdin to CONIN$. + // Note that the python DLL is linked to its own stdio DLL which can be + // differ from Vim's stdio. + static void + reset_stdin(void) + { + FILE *(*py__acrt_iob_func)(unsigned) = NULL; + FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL; + HINSTANCE hinst; + + # ifdef DYNAMIC_PYTHON3 + hinst = hinstPy3; + # else + hinst = GetModuleHandle(PYTHON3_DLL); + # endif + if (hinst == NULL) + return; + + // Get "freopen" and "stdin" which are used in the python DLL. + // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later. + py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func"); + if (py__acrt_iob_func) + { + HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst, + "__acrt_iob_func"); + if (hpystdiodll) + pyfreopen = (void*)GetProcAddress(hpystdiodll, "freopen"); + } + + // Reconnect stdin to CONIN$. + if (pyfreopen) + pyfreopen("CONIN$", "r", py__acrt_iob_func(0)); + else + freopen("CONIN$", "r", stdin); + } + #else + # define reset_stdin() + #endif + static int Python3_Init(void) { *************** *** 939,944 **** --- 980,986 ---- PyImport_AppendInittab("vim", Py3Init_vim); + reset_stdin(); Py_Initialize(); // Initialise threads, and below save the state using *** ../vim-8.2.1380/src/version.c 2020-08-06 21:26:54.436005498 +0200 --- src/version.c 2020-08-06 21:43:48.425328087 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1381, /**/ -- hundred-and-one symptoms of being an internet addict: 136. You decide to stay in a low-paying job teaching just for the free Internet access. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///