--- origsrc/Python-3.6.1/Python/thread_pthread.h	2017-03-25 08:10:30.784601800 +0900
+++ src/Python-3.6.1/Python/thread_pthread.h	2017-03-25 08:43:12.070906900 +0900
@@ -603,6 +603,67 @@
 
 #define Py_HAVE_NATIVE_TLS
 
+#ifdef __CYGWIN__
+/* Cygwin pthread TLS key type is a pointer, whereas Python 3 assumes
+ * int type. So wrapper functions are used instead.
+ */
+static unsigned long keybase = ULONG_MAX;
+
+static int
+py_pthread_key_create(unsigned long *key, void(*func)(void *))
+{
+    int ret;
+    const unsigned long mask = ULONG_MAX & ~(unsigned long)INT_MAX;
+    unsigned long keybase_new;
+    pthread_key_t key_cyg;
+    ret = pthread_key_create(&key_cyg, func);
+    if (ret) {
+        /* Error */
+        *key = 0;
+        return ret;
+    }
+    keybase_new = (unsigned long)key_cyg & mask;
+    /* If base address is different, treat as error */
+    if (keybase != ULONG_MAX && keybase_new != keybase) {
+        pthread_key_delete(key_cyg);
+        *key = 0;
+        errno = ENOMEM;
+        return ENOMEM;
+    }
+    keybase = keybase_new;
+    *key = (unsigned long)key_cyg & ~mask;
+    return 0;
+}
+
+static int
+py_pthread_key_delete(unsigned long key)
+{
+    pthread_key_t key_cyg = (pthread_key_t)(keybase | key);
+    return pthread_key_delete(key_cyg);
+}
+
+static int
+py_pthread_setspecific(unsigned long key, const void *p)
+{
+    pthread_key_t key_cyg = (pthread_key_t)(keybase | key);
+    return pthread_setspecific(key_cyg, p);
+}
+
+static void *
+py_pthread_getspecific(unsigned long key)
+{
+    pthread_key_t key_cyg = (pthread_key_t)(keybase | key);
+    return pthread_getspecific(key_cyg);
+}
+
+#define pthread_key_t unsigned long
+#define pthread_key_create py_pthread_key_create
+#define pthread_key_delete py_pthread_key_delete
+#define pthread_setspecific py_pthread_setspecific
+#define pthread_getspecific py_pthread_getspecific
+
+#endif /* __CYGWIN__ */
+
 long
 PyThread_create_key(void)
 {
@@ -610,15 +671,12 @@
     int fail = pthread_key_create(&key, NULL);
     if (fail)
         return -1L;
-#ifndef __CYGWIN__
-    /* Cygwin pthread types are pointers, which may "overflow" signed long */
     if (key > LONG_MAX) {
         /* Issue #22206: handle integer overflow */
         pthread_key_delete(key);
         errno = ENOMEM;
         return -1L;
     }
-#endif
     return (long)key;
 }
 
