@@ -101,7 +101,65 @@ Copyright (C) 1994 Steen Lumholt.
101101#ifdef MS_WINDOWS
102102#include <conio.h>
103103#define WAIT_FOR_STDIN
104+
105+ static PyObject *
106+ _get_tcl_lib_path ()
107+ {
108+ static PyObject * tcl_library_path = NULL ;
109+ static int already_checked = 0 ;
110+
111+ if (already_checked == 0 ) {
112+ PyObject * prefix ;
113+ struct stat stat_buf ;
114+ int stat_return_value ;
115+
116+ prefix = PyUnicode_FromWideChar (Py_GetPrefix (), -1 );
117+ if (prefix == NULL ) {
118+ return NULL ;
119+ }
120+
121+ /* Check expected location for an installed Python first */
122+ tcl_library_path = PyUnicode_FromString ("\\tcl\\tcl" TCL_VERSION );
123+ if (tcl_library_path == NULL ) {
124+ return NULL ;
125+ }
126+ tcl_library_path = PyUnicode_Concat (prefix , tcl_library_path );
127+ if (tcl_library_path == NULL ) {
128+ return NULL ;
129+ }
130+ stat_return_value = _Py_stat (tcl_library_path , & stat_buf );
131+ if (stat_return_value == -2 ) {
132+ return NULL ;
133+ }
134+ if (stat_return_value == -1 ) {
135+ /* install location doesn't exist, reset errno and see if
136+ we're a repository build */
137+ errno = 0 ;
138+ #ifdef Py_TCLTK_DIR
139+ tcl_library_path = PyUnicode_FromString (
140+ Py_TCLTK_DIR "\\lib\\tcl" TCL_VERSION );
141+ if (tcl_library_path == NULL ) {
142+ return NULL ;
143+ }
144+ stat_return_value = _Py_stat (tcl_library_path , & stat_buf );
145+ if (stat_return_value == -2 ) {
146+ return NULL ;
147+ }
148+ if (stat_return_value == -1 ) {
149+ /* tcltkDir for a repository build doesn't exist either,
150+ reset errno and leave Tcl to its own devices */
151+ errno = 0 ;
152+ tcl_library_path = NULL ;
153+ }
154+ #else
155+ tcl_library_path = NULL ;
104156#endif
157+ }
158+ already_checked = 1 ;
159+ }
160+ return tcl_library_path ;
161+ }
162+ #endif /* MS_WINDOWS */
105163
106164#ifdef WITH_THREAD
107165
@@ -681,6 +739,33 @@ Tkapp_New(const char *screenName, const char *className,
681739 PyMem_Free (args );
682740 }
683741
742+ #ifdef MS_WINDOWS
743+ {
744+ PyObject * str_path ;
745+ PyObject * utf8_path ;
746+ DWORD ret ;
747+
748+ ret = GetEnvironmentVariableW (L"TCL_LIBRARY" , NULL , 0 );
749+ if (!ret && GetLastError () == ERROR_ENVVAR_NOT_FOUND ) {
750+ str_path = _get_tcl_lib_path ();
751+ if (str_path == NULL && PyErr_Occurred ()) {
752+ return NULL ;
753+ }
754+ if (str_path != NULL ) {
755+ utf8_path = PyUnicode_AsUTF8String (str_path );
756+ if (utf8_path == NULL ) {
757+ return NULL ;
758+ }
759+ Tcl_SetVar (v -> interp ,
760+ "tcl_library" ,
761+ PyBytes_AsString (utf8_path ),
762+ TCL_GLOBAL_ONLY );
763+ Py_DECREF (utf8_path );
764+ }
765+ }
766+ }
767+ #endif
768+
684769 if (Tcl_AppInit (v -> interp ) != TCL_OK ) {
685770 PyObject * result = Tkinter_Error ((PyObject * )v );
686771#ifdef TKINTER_PROTECT_LOADTK
@@ -3517,8 +3602,40 @@ PyInit__tkinter(void)
35173602 uexe = PyUnicode_FromWideChar (Py_GetProgramName (), -1 );
35183603 if (uexe ) {
35193604 cexe = PyUnicode_EncodeFSDefault (uexe );
3520- if (cexe )
3605+ if (cexe ) {
3606+ #ifdef MS_WINDOWS
3607+ int set_var = 0 ;
3608+ PyObject * str_path ;
3609+ wchar_t * wcs_path ;
3610+ DWORD ret ;
3611+
3612+ ret = GetEnvironmentVariableW (L"TCL_LIBRARY" , NULL , 0 );
3613+
3614+ if (!ret && GetLastError () == ERROR_ENVVAR_NOT_FOUND ) {
3615+ str_path = _get_tcl_lib_path ();
3616+ if (str_path == NULL && PyErr_Occurred ()) {
3617+ return NULL ;
3618+ }
3619+ if (str_path != NULL ) {
3620+ wcs_path = PyUnicode_AsWideCharString (str_path , NULL );
3621+ if (wcs_path == NULL ) {
3622+ return NULL ;
3623+ }
3624+ SetEnvironmentVariableW (L"TCL_LIBRARY" , wcs_path );
3625+ set_var = 1 ;
3626+ }
3627+ }
3628+
35213629 Tcl_FindExecutable (PyBytes_AsString (cexe ));
3630+
3631+ if (set_var ) {
3632+ SetEnvironmentVariableW (L"TCL_LIBRARY" , NULL );
3633+ PyMem_Free (wcs_path );
3634+ }
3635+ #else
3636+ Tcl_FindExecutable (PyBytes_AsString (cexe ));
3637+ #endif /* MS_WINDOWS */
3638+ }
35223639 Py_XDECREF (cexe );
35233640 Py_DECREF (uexe );
35243641 }
0 commit comments