Skip to content

Commit

Permalink
python3: Use is_cygpty() to detect a terminal; disable readline with …
Browse files Browse the repository at this point in the history
…a real Windows console (#2675)

CPython uses isatty() to detect a terminal and change some settings
like line buffering and interactive mode. Use is_cygpty() to make
this also work under mintty.
See https://github.com/Alexpux/MINGW-packages/issues/2645

This also removes the bash script which forced the interactive mode
when python3 was started without arguments. This is no longer needed as
Python now detects the terminal output and does this automatically.

Also use is_cygpty() to detect when not under mintty and disable the readline
module there, as using it breaks input of certain characters and
leads to errors on shutdown when it tries to save the readline history.
(The readline module is not available in the official Python build)
See https://github.com/Alexpux/MINGW-packages/issues/2656
  • Loading branch information
lazka authored and Alexpux committed Aug 14, 2017
1 parent a3bba15 commit eaa55b6
Show file tree
Hide file tree
Showing 2 changed files with 392 additions and 13 deletions.
382 changes: 382 additions & 0 deletions mingw-w64-python3/1700-cygpty-isatty-disable-readline.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
diff -Nur Python-3.6.2rc1/Include/iscygpty.h Python-3.6.2rc1_pty/Include/iscygpty.h
--- Python-3.6.2rc1/Include/iscygpty.h 1970-01-01 01:00:00.000000000 +0100
+++ Python-3.6.2rc1_pty/Include/iscygpty.h 2017-06-01 15:06:06.000000000 +0200
@@ -0,0 +1,41 @@
+/*
+ * iscygpty.h -- part of ptycheck
+ * https://github.com/k-takata/ptycheck
+ *
+ * Copyright (c) 2015-2017 K.Takata
+ *
+ * You can redistribute it and/or modify it under the terms of either
+ * the MIT license (as described below) or the Vim license.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef _ISCYGPTY_H
+#define _ISCYGPTY_H
+
+#ifdef _WIN32
+int is_cygpty(int fd);
+int is_cygpty_used(void);
+#else
+#define is_cygpty(fd) 0
+#define is_cygpty_used() 0
+#endif
+
+#endif /* _ISCYGPTY_H */
diff -Nur Python-3.6.2rc1/Makefile.pre.in Python-3.6.2rc1_pty/Makefile.pre.in
--- Python-3.6.2rc1/Makefile.pre.in 2017-07-07 15:33:14.892371300 +0200
+++ Python-3.6.2rc1_pty/Makefile.pre.in 2017-07-07 14:39:37.110324600 +0200
@@ -106,7 +106,7 @@
# Extra C flags added for building the interpreter object files.
CFLAGSFORSHARED=@CFLAGSFORSHARED@
# C flags used for building the interpreter object files
-PY_CORE_CFLAGS= $(PY_CFLAGS) $(PY_CFLAGS_NODIST) $(PY_CPPFLAGS) $(CFLAGSFORSHARED) -DPy_BUILD_CORE
+PY_CORE_CFLAGS= $(PY_CFLAGS) $(PY_CFLAGS_NODIST) $(PY_CPPFLAGS) $(CFLAGSFORSHARED) -DPy_BUILD_CORE -D_WIN32_WINNT=0x0600
# Strict or non-strict aliasing flags used to compile dtoa.c, see above
CFLAGS_ALIASING=@CFLAGS_ALIASING@

@@ -343,6 +343,7 @@
Python/graminit.o \
Python/import.o \
Python/importdl.o \
+ Python/iscygpty.o \
Python/marshal.o \
Python/modsupport.o \
Python/mystrtoul.o \
@@ -953,6 +954,7 @@
$(srcdir)/Include/genobject.h \
$(srcdir)/Include/import.h \
$(srcdir)/Include/intrcheck.h \
+ $(srcdir)/Include/iscygpty.h \
$(srcdir)/Include/iterobject.h \
$(srcdir)/Include/listobject.h \
$(srcdir)/Include/longintrepr.h \
diff -Nur Python-3.6.2rc1/Modules/_io/fileio.c Python-3.6.2rc1_pty/Modules/_io/fileio.c
--- Python-3.6.2rc1/Modules/_io/fileio.c 2017-07-07 15:33:04.837796200 +0200
+++ Python-3.6.2rc1_pty/Modules/_io/fileio.c 2017-07-07 14:38:31.805589400 +0200
@@ -17,6 +17,7 @@
#endif
#include <stddef.h> /* For offsetof */
#include "_iomodule.h"
+#include "iscygpty.h"

/*
* Known likely problems:
@@ -1116,7 +1117,7 @@
return err_closed();
Py_BEGIN_ALLOW_THREADS
_Py_BEGIN_SUPPRESS_IPH
- res = isatty(self->fd);
+ res = isatty(self->fd) || is_cygpty(self->fd);
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
return PyBool_FromLong(res);
diff -Nur Python-3.6.2rc1/Modules/posixmodule.c Python-3.6.2rc1_pty/Modules/posixmodule.c
--- Python-3.6.2rc1/Modules/posixmodule.c 2017-07-07 15:33:02.160643000 +0200
+++ Python-3.6.2rc1_pty/Modules/posixmodule.c 2017-07-07 14:40:46.610299800 +0200
@@ -31,6 +31,7 @@
#else
#include "winreparse.h"
#endif
+#include "iscygpty.h"

/* On android API level 21, 'AT_EACCESS' is not declared although
* HAVE_FACCESSAT is defined. */
@@ -8357,7 +8358,7 @@
{
int return_value;
_Py_BEGIN_SUPPRESS_IPH
- return_value = isatty(fd);
+ return_value = isatty(fd) || is_cygpty(fd);
_Py_END_SUPPRESS_IPH
return return_value;
}
diff -Nur Python-3.6.2rc1/Modules/readline.c Python-3.6.2rc1_pty/Modules/readline.c
--- Python-3.6.2rc1/Modules/readline.c 2017-07-07 15:32:52.081066500 +0200
+++ Python-3.6.2rc1_pty/Modules/readline.c 2017-07-07 15:30:59.697638600 +0200
@@ -6,6 +6,7 @@

/* Standard definitions */
#include "Python.h"
+#include "iscygpty.h"
#include <stddef.h>
#include <setjmp.h>
#include <signal.h>
@@ -1404,6 +1405,11 @@
PyObject *m;
readlinestate *mod_state;

+ if (!is_cygpty(STDOUT_FILENO)) {
+ PyErr_SetString(PyExc_ImportError, "Not a cygwin terminal");
+ return NULL;
+ }
+
#ifdef __APPLE__
if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
using_libedit_emulation = 1;
diff -Nur Python-3.6.2rc1/Python/frozenmain.c Python-3.6.2rc1_pty/Python/frozenmain.c
--- Python-3.6.2rc1/Python/frozenmain.c 2017-07-07 15:33:11.848197100 +0200
+++ Python-3.6.2rc1_pty/Python/frozenmain.c 2017-07-07 14:32:32.147018100 +0200
@@ -3,6 +3,7 @@

#include "Python.h"
#include <locale.h>
+#include "iscygpty.h"

#ifdef MS_WINDOWS
extern void PyWinFreeze_ExeInit(void);
@@ -93,7 +94,7 @@
else
sts = 0;

- if (inspect && isatty((int)fileno(stdin)))
+ if (inspect && (isatty((int)fileno(stdin)) || is_cygpty((int)fileno(stdin))))
sts = PyRun_AnyFile(stdin, "<stdin>") != 0;

#ifdef MS_WINDOWS
diff -Nur Python-3.6.2rc1/Python/iscygpty.c Python-3.6.2rc1_pty/Python/iscygpty.c
--- Python-3.6.2rc1/Python/iscygpty.c 1970-01-01 01:00:00.000000000 +0100
+++ Python-3.6.2rc1_pty/Python/iscygpty.c 2017-07-07 14:26:41.145942000 +0200
@@ -0,0 +1,185 @@
+/*
+ * iscygpty.c -- part of ptycheck
+ * https://github.com/k-takata/ptycheck
+ *
+ * Copyright (c) 2015-2017 K.Takata
+ *
+ * You can redistribute it and/or modify it under the terms of either
+ * the MIT license (as described below) or the Vim license.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef _WIN32
+
+#include <ctype.h>
+#include <io.h>
+#include <wchar.h>
+#include <windows.h>
+
+#ifdef USE_FILEEXTD
+/* VC 7.1 or earlier doesn't support SAL. */
+# if !defined(_MSC_VER) || (_MSC_VER < 1400)
+# define __out
+# define __in
+# define __in_opt
+# endif
+/* Win32 FileID API Library:
+ * http://www.microsoft.com/en-us/download/details.aspx?id=22599
+ * Needed for WinXP. */
+# include <fileextd.h>
+#else /* USE_FILEEXTD */
+/* VC 8 or earlier. */
+# if defined(_MSC_VER) && (_MSC_VER < 1500)
+# ifdef ENABLE_STUB_IMPL
+# define STUB_IMPL
+# else
+# error "Win32 FileID API Library is required for VC2005 or earlier."
+# endif
+# endif
+#endif /* USE_FILEEXTD */
+
+
+#include "iscygpty.h"
+
+//#define USE_DYNFILEID
+#ifdef USE_DYNFILEID
+typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
+ HANDLE hFile,
+ FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
+ LPVOID lpFileInformation,
+ DWORD dwBufferSize
+);
+static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
+
+# ifndef USE_FILEEXTD
+static BOOL WINAPI stub_GetFileInformationByHandleEx(
+ HANDLE hFile,
+ FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
+ LPVOID lpFileInformation,
+ DWORD dwBufferSize
+ )
+{
+ return FALSE;
+}
+# endif
+
+static void setup_fileid_api(void)
+{
+ if (pGetFileInformationByHandleEx != NULL) {
+ return;
+ }
+ pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx)
+ GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
+ "GetFileInformationByHandleEx");
+ if (pGetFileInformationByHandleEx == NULL) {
+# ifdef USE_FILEEXTD
+ pGetFileInformationByHandleEx = GetFileInformationByHandleEx;
+# else
+ pGetFileInformationByHandleEx = stub_GetFileInformationByHandleEx;
+# endif
+ }
+}
+#else
+# define pGetFileInformationByHandleEx GetFileInformationByHandleEx
+# define setup_fileid_api()
+#endif
+
+
+#define is_wprefix(s, prefix) \
+ (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)
+
+/* Check if the fd is a cygwin/msys's pty. */
+int is_cygpty(int fd)
+{
+#ifdef STUB_IMPL
+ return 0;
+#else
+ HANDLE h;
+ int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH;
+ FILE_NAME_INFO *nameinfo;
+ WCHAR *p = NULL;
+
+ setup_fileid_api();
+
+ h = (HANDLE) _get_osfhandle(fd);
+ if (h == INVALID_HANDLE_VALUE) {
+ return 0;
+ }
+ /* Cygwin/msys's pty is a pipe. */
+ if (GetFileType(h) != FILE_TYPE_PIPE) {
+ return 0;
+ }
+ nameinfo = malloc(size);
+ if (nameinfo == NULL) {
+ return 0;
+ }
+ /* Check the name of the pipe:
+ * '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master' */
+ if (pGetFileInformationByHandleEx(h, FileNameInfo, nameinfo, size)) {
+ nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0';
+ p = nameinfo->FileName;
+ if (is_wprefix(p, L"\\cygwin-")) { /* Cygwin */
+ p += 8;
+ } else if (is_wprefix(p, L"\\msys-")) { /* MSYS and MSYS2 */
+ p += 6;
+ } else {
+ p = NULL;
+ }
+ if (p != NULL) {
+ while (*p && isxdigit(*p)) /* Skip 16-digit hexadecimal. */
+ ++p;
+ if (is_wprefix(p, L"-pty")) {
+ p += 4;
+ } else {
+ p = NULL;
+ }
+ }
+ if (p != NULL) {
+ while (*p && isdigit(*p)) /* Skip pty number. */
+ ++p;
+ if (is_wprefix(p, L"-from-master")) {
+ //p += 12;
+ } else if (is_wprefix(p, L"-to-master")) {
+ //p += 10;
+ } else {
+ p = NULL;
+ }
+ }
+ }
+ free(nameinfo);
+ return (p != NULL);
+#endif /* STUB_IMPL */
+}
+
+/* Check if at least one cygwin/msys pty is used. */
+int is_cygpty_used(void)
+{
+ int fd, ret = 0;
+
+ for (fd = 0; fd < 3; fd++) {
+ ret |= is_cygpty(fd);
+ }
+ return ret;
+}
+
+#endif /* _WIN32 */
+
+/* vim: set ts=4 sw=4: */
diff -Nur Python-3.6.2rc1/Python/pylifecycle.c Python-3.6.2rc1_pty/Python/pylifecycle.c
--- Python-3.6.2rc1/Python/pylifecycle.c 2017-07-07 15:33:17.815538400 +0200
+++ Python-3.6.2rc1_pty/Python/pylifecycle.c 2017-07-07 14:20:38.085176100 +0200
@@ -14,6 +14,7 @@
#include "ast.h"
#include "marshal.h"
#include "osdefs.h"
+#include "iscygpty.h"
#include <locale.h>

#ifdef HAVE_SIGNAL_H
@@ -1705,7 +1706,7 @@
int
Py_FdIsInteractive(FILE *fp, const char *filename)
{
- if (isatty((int)fileno(fp)))
+ if (isatty((int)fileno(fp)) || is_cygpty((int)fileno(fp)))
return 1;
if (!Py_InteractiveFlag)
return 0;
diff -Nur Python-3.6.2rc1/setup.py Python-3.6.2rc1_pty/setup.py
--- Python-3.6.2rc1/setup.py 2017-07-07 15:32:54.857225300 +0200
+++ Python-3.6.2rc1_pty/setup.py 2017-07-07 14:55:12.937850900 +0200
@@ -818,10 +818,11 @@
['/usr/lib/termcap'],
'termcap'):
readline_libs.append('termcap')
- exts.append( Extension('readline', ['readline.c'],
+ exts.append( Extension('readline', ['readline.c', '../Python/iscygpty.c'],
library_dirs=['/usr/lib/termcap'],
extra_link_args=readline_extra_link_args,
- libraries=readline_libs) )
+ libraries=readline_libs,
+ extra_compile_args=["-D_WIN32_WINNT=0x0600"]) )
else:
missing.append('readline')

Loading

0 comments on commit eaa55b6

Please sign in to comment.