Files
MINGW-packages/mingw-w64-python/1700-cygpty-isatty.patch
Christoph Reiter 55bb9c2d1d python: cygpty hack improvements
In case of cygpty we want to fake a tty and at the same time make
Python behave as if it was attached to a console.

This improves two thing in that regard:

* Update the is_cygpty patch to cover more isatty() checks (likely
  they were added since the patch was created)
* Default the stdio encoding to the console output encoding by default.
  This is only in effect if stdout isn't redirected (then is uses the ANSI code page)
  and no real console is used (then it forces utf-8). Python never considers the
  case were isatty() and no console, so we have to add this.

This makes printing unicode work again in MSYS2 bash.
2021-01-06 18:24:15 +01:00

418 lines
13 KiB
Diff

diff -Naur Python-3.8.0-orig/Include/iscygpty.h Python-3.8.0/Include/iscygpty.h
--- Python-3.8.0-orig/Include/iscygpty.h 1970-01-01 03:00:00.000000000 +0300
+++ Python-3.8.0/Include/iscygpty.h 2019-10-22 10:04:39.461927600 +0300
@@ -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 -Naur Python-3.8.0-orig/Makefile.pre.in Python-3.8.0/Makefile.pre.in
--- Python-3.8.0-orig/Makefile.pre.in 2019-10-22 10:04:25.063102300 +0300
+++ Python-3.8.0/Makefile.pre.in 2019-10-22 10:04:39.867528300 +0300
@@ -346,6 +346,7 @@
Python/import.o \
Python/importdl.o \
Python/initconfig.o \
+ Python/iscygpty.o \
Python/marshal.o \
Python/modsupport.o \
Python/mysnprintf.o \
@@ -1011,6 +1012,7 @@
$(srcdir)/Include/import.h \
$(srcdir)/Include/interpreteridobject.h \
$(srcdir)/Include/intrcheck.h \
+ $(srcdir)/Include/iscygpty.h \
$(srcdir)/Include/iterobject.h \
$(srcdir)/Include/listobject.h \
$(srcdir)/Include/longintrepr.h \
diff -Naur Python-3.8.0-orig/Modules/_io/fileio.c Python-3.8.0/Modules/_io/fileio.c
--- Python-3.8.0-orig/Modules/_io/fileio.c 2019-10-14 16:34:47.000000000 +0300
+++ Python-3.8.0/Modules/_io/fileio.c 2019-10-22 10:04:40.273129000 +0300
@@ -18,6 +18,7 @@
#endif
#include <stddef.h> /* For offsetof */
#include "_iomodule.h"
+#include "iscygpty.h"
/*
* Known likely problems:
@@ -1119,7 +1120,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 -Naur Python-3.8.0-orig/Modules/main.c Python-3.8.0/Modules/main.c
--- Python-3.8.0-orig/Modules/main.c 2019-10-14 16:34:47.000000000 +0300
+++ Python-3.8.0/Modules/main.c 2019-10-22 10:04:41.068730400 +0300
@@ -5,6 +5,7 @@
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
+#include "iscygpty.h"
#ifdef __FreeBSD__
# include <fenv.h> /* fedisableexcept() */
@@ -103,7 +104,7 @@
static int
stdin_is_interactive(const PyConfig *config)
{
- return (isatty(fileno(stdin)) || config->interactive);
+ return (isatty(fileno(stdin)) || config->interactive || is_cygpty(fileno(stdin)));
}
diff -Naur Python-3.8.0-orig/Modules/posixmodule.c Python-3.8.0/Modules/posixmodule.c
--- Python-3.8.0-orig/Modules/posixmodule.c 2019-10-22 10:02:48.171332100 +0300
+++ Python-3.8.0/Modules/posixmodule.c 2019-10-22 10:04:41.489931200 +0300
@@ -34,6 +34,7 @@
FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
# include <windows.h>
#endif
+#include "iscygpty.h"
#include "pycore_ceval.h" /* _PyEval_ReInitThreads() */
#include "pycore_pystate.h" /* _PyRuntime */
@@ -9258,7 +9259,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 -Naur Python-3.8.0-orig/Python/frozenmain.c Python-3.8.0/Python/frozenmain.c
--- Python-3.8.0-orig/Python/frozenmain.c 2019-10-14 16:34:47.000000000 +0300
+++ Python-3.8.0/Python/frozenmain.c 2019-10-22 10:04:41.895531900 +0300
@@ -4,6 +4,7 @@
#include "Python.h"
#include "pycore_pystate.h"
#include <locale.h>
+#include "iscygpty.h"
#ifdef MS_WINDOWS
extern void PyWinFreeze_ExeInit(void);
@@ -107,7 +108,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 -Naur Python-3.8.0-orig/Python/iscygpty.c Python-3.8.0/Python/iscygpty.c
--- Python-3.8.0-orig/Python/iscygpty.c 1970-01-01 03:00:00.000000000 +0300
+++ Python-3.8.0/Python/iscygpty.c 2019-10-22 10:04:42.301132600 +0300
@@ -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 - 1);
+ 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 + sizeof(WCHAR));
+ 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 -Naur Python-3.8.0-orig/Python/pylifecycle.c Python-3.8.0/Python/pylifecycle.c
--- Python-3.8.0-orig/Python/pylifecycle.c 2019-10-14 16:34:47.000000000 +0300
+++ Python-3.8.0/Python/pylifecycle.c 2019-10-22 10:04:42.691133300 +0300
@@ -24,6 +24,7 @@
#include "ast.h"
#include "marshal.h"
#include "osdefs.h"
+#include "iscygpty.h"
#include <locale.h>
#ifdef HAVE_SIGNAL_H
@@ -2349,7 +2350,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;
--- Python-3.8.7/Python/bltinmodule.c.orig 2020-12-21 17:25:24.000000000 +0100
+++ Python-3.8.7/Python/bltinmodule.c 2021-01-06 16:50:48.899457200 +0100
@@ -1,6 +1,7 @@
/* Built-in functions */
#include "Python.h"
+#include "iscygpty.h"
#include <ctype.h>
#include "ast.h"
#undef Yield /* undefine macro conflicting with <winbase.h> */
@@ -1980,7 +1981,7 @@
Py_DECREF(tmp);
if (fd < 0 && PyErr_Occurred())
return NULL;
- tty = fd == fileno(stdin) && isatty(fd);
+ tty = fd == fileno(stdin) && (isatty(fd) || is_cygpty(fd));
}
if (tty) {
tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL);
@@ -1993,7 +1994,7 @@
Py_DECREF(tmp);
if (fd < 0 && PyErr_Occurred())
return NULL;
- tty = fd == fileno(stdout) && isatty(fd);
+ tty = fd == fileno(stdout) && (isatty(fd) || is_cygpty(fd));
}
}
--- Python-3.8.7/Objects/fileobject.c.orig 2020-12-21 17:25:24.000000000 +0100
+++ Python-3.8.7/Objects/fileobject.c 2021-01-06 16:51:19.605061600 +0100
@@ -2,6 +2,7 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
+#include "iscygpty.h"
#include "pycore_pystate.h"
#if defined(HAVE_GETC_UNLOCKED) && !defined(_Py_MEMORY_SANITIZER)
@@ -434,7 +435,7 @@
}
Py_BEGIN_ALLOW_THREADS
- res = isatty(self->fd);
+ res = isatty(self->fd) || is_cygpty(self->fd);
Py_END_ALLOW_THREADS
return PyBool_FromLong(res);
--- Python-3.8.7/Python/fileutils.c.orig 2020-12-21 17:25:24.000000000 +0100
+++ Python-3.8.7/Python/fileutils.c 2021-01-06 16:50:56.153853200 +0100
@@ -1,4 +1,5 @@
#include "Python.h"
+#include "iscygpty.h"
#include "pycore_fileutils.h"
#include "osdefs.h"
#include <locale.h>
@@ -59,7 +60,7 @@
#endif
int valid;
_Py_BEGIN_SUPPRESS_IPH
- valid = isatty(fd);
+ valid = isatty(fd) || is_cygpty(fd);
_Py_END_SUPPRESS_IPH
if (!valid)
Py_RETURN_NONE;