|
| 1 | +diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c |
| 2 | +index 6b5fcb8a365..99d44ccf1d8 100644 |
| 3 | +--- a/Modules/_tkinter.c |
| 4 | ++++ b/Modules/_tkinter.c |
| 5 | +@@ -28,9 +28,7 @@ Copyright (C) 1994 Steen Lumholt. |
| 6 | + |
| 7 | + #include "Python.h" |
| 8 | + #include <ctype.h> |
| 9 | +-#ifdef MS_WINDOWS |
| 10 | +-# include "pycore_fileutils.h" // _Py_stat() |
| 11 | +-#endif |
| 12 | ++#include "pycore_fileutils.h" // _Py_stat() |
| 13 | + |
| 14 | + #include "pycore_long.h" |
| 15 | + |
| 16 | +@@ -134,6 +132,7 @@ typedef int Tcl_Size; |
| 17 | + #ifdef MS_WINDOWS |
| 18 | + #include <conio.h> |
| 19 | + #define WAIT_FOR_STDIN |
| 20 | ++#endif |
| 21 | + |
| 22 | + static PyObject * |
| 23 | + _get_tcl_lib_path(void) |
| 24 | +@@ -151,6 +150,7 @@ _get_tcl_lib_path(void) |
| 25 | + return NULL; |
| 26 | + } |
| 27 | + |
| 28 | ++#ifdef MS_WINDOWS |
| 29 | + /* Check expected location for an installed Python first */ |
| 30 | + tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION); |
| 31 | + if (tcl_library_path == NULL) { |
| 32 | +@@ -188,11 +188,31 @@ _get_tcl_lib_path(void) |
| 33 | + tcl_library_path = NULL; |
| 34 | + #endif |
| 35 | + } |
| 36 | ++#else |
| 37 | ++ /* Check expected location for an installed Python first */ |
| 38 | ++ tcl_library_path = PyUnicode_FromString("/lib/tcl" TCL_VERSION); |
| 39 | ++ if (tcl_library_path == NULL) { |
| 40 | ++ return NULL; |
| 41 | ++ } |
| 42 | ++ tcl_library_path = PyUnicode_Concat(prefix, tcl_library_path); |
| 43 | ++ if (tcl_library_path == NULL) { |
| 44 | ++ return NULL; |
| 45 | ++ } |
| 46 | ++ stat_return_value = _Py_stat(tcl_library_path, &stat_buf); |
| 47 | ++ if (stat_return_value == -2) { |
| 48 | ++ return NULL; |
| 49 | ++ } |
| 50 | ++ if (stat_return_value == -1) { |
| 51 | ++ /* install location doesn't exist, reset errno and leave Tcl |
| 52 | ++ to its own devices */ |
| 53 | ++ errno = 0; |
| 54 | ++ tcl_library_path = NULL; |
| 55 | ++ } |
| 56 | ++#endif |
| 57 | + already_checked = 1; |
| 58 | + } |
| 59 | + return tcl_library_path; |
| 60 | + } |
| 61 | +-#endif /* MS_WINDOWS */ |
| 62 | + |
| 63 | + /* The threading situation is complicated. Tcl is not thread-safe, except |
| 64 | + when configured with --enable-threads. |
| 65 | +@@ -713,6 +733,30 @@ Tkapp_New(const char *screenName, const char *className, |
| 66 | + |
| 67 | + ret = GetEnvironmentVariableW(L"TCL_LIBRARY", NULL, 0); |
| 68 | + if (!ret && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { |
| 69 | ++ str_path = _get_tcl_lib_path(); |
| 70 | ++ if (str_path == NULL && PyErr_Occurred()) { |
| 71 | ++ return NULL; |
| 72 | ++ } |
| 73 | ++ if (str_path != NULL) { |
| 74 | ++ utf8_path = PyUnicode_AsUTF8String(str_path); |
| 75 | ++ if (utf8_path == NULL) { |
| 76 | ++ return NULL; |
| 77 | ++ } |
| 78 | ++ Tcl_SetVar(v->interp, |
| 79 | ++ "tcl_library", |
| 80 | ++ PyBytes_AS_STRING(utf8_path), |
| 81 | ++ TCL_GLOBAL_ONLY); |
| 82 | ++ Py_DECREF(utf8_path); |
| 83 | ++ } |
| 84 | ++ } |
| 85 | ++ } |
| 86 | ++#else |
| 87 | ++ { |
| 88 | ++ const char *env_val = getenv("TCL_LIBRARY"); |
| 89 | ++ if (!env_val) { |
| 90 | ++ PyObject *str_path; |
| 91 | ++ PyObject *utf8_path; |
| 92 | ++ |
| 93 | + str_path = _get_tcl_lib_path(); |
| 94 | + if (str_path == NULL && PyErr_Occurred()) { |
| 95 | + return NULL; |
| 96 | +@@ -3542,7 +3586,27 @@ PyInit__tkinter(void) |
| 97 | + PyMem_Free(wcs_path); |
| 98 | + } |
| 99 | + #else |
| 100 | ++ int set_var = 0; |
| 101 | ++ PyObject *str_path; |
| 102 | ++ char *path; |
| 103 | ++ |
| 104 | ++ if (!getenv("TCL_LIBRARY")) { |
| 105 | ++ str_path = _get_tcl_lib_path(); |
| 106 | ++ if (str_path == NULL && PyErr_Occurred()) { |
| 107 | ++ Py_DECREF(m); |
| 108 | ++ return NULL; |
| 109 | ++ } |
| 110 | ++ if (str_path != NULL) { |
| 111 | ++ setenv("TCL_LIBRARY", path, 1); |
| 112 | ++ set_var = 1; |
| 113 | ++ } |
| 114 | ++ } |
| 115 | ++ |
| 116 | + Tcl_FindExecutable(PyBytes_AS_STRING(cexe)); |
| 117 | ++ |
| 118 | ++ if (set_var) { |
| 119 | ++ unsetenv("TCL_LIBRARY"); |
| 120 | ++ } |
| 121 | + #endif /* MS_WINDOWS */ |
| 122 | + } |
| 123 | + Py_XDECREF(cexe); |
0 commit comments