diff -r ce89c5c3c786 Lib/test/test___all__.py --- a/Lib/test/test___all__.py Wed Dec 18 11:27:21 2013 -0800 +++ b/Lib/test/test___all__.py Fri Dec 20 10:26:22 2013 -0600 @@ -112,6 +112,14 @@ ignored) print('Following modules failed to be imported:', failed_imports) +def tearDownModule(): + # suppress warning about os.environ changing due to tkinter being imported + try: + from tkinter import _fix + _fix.unfix_environ() + except Exception as e: + if support.verbose: + print('unfixing environ for tkinter failed: %s' % e) if __name__ == "__main__": unittest.main() diff -r ce89c5c3c786 Lib/test/test_idle.py --- a/Lib/test/test_idle.py Wed Dec 18 11:27:21 2013 -0800 +++ b/Lib/test/test_idle.py Fri Dec 20 10:26:22 2013 -0600 @@ -23,6 +23,14 @@ # load_tests() if it finds it. (Unittest.main does the same.) load_tests = idletest.load_tests +def setUpModule(): + # make sure init.tcl can be found + tk._fix.fix_environ() + +def tearDownModule(): + # suppress warning about os.environ changing + tk._fix.unfix_environ() + if __name__ == '__main__': # Until unittest supports resources, we emulate regrtest's -ugui # so loaded tests run the same as if textually present here. diff -r ce89c5c3c786 Lib/test/test_tcl.py --- a/Lib/test/test_tcl.py Wed Dec 18 11:27:21 2013 -0800 +++ b/Lib/test/test_tcl.py Fri Dec 20 10:26:22 2013 -0600 @@ -9,8 +9,7 @@ # Skip this test if the _tkinter module wasn't built. _tkinter = support.import_module('_tkinter') -# Make sure tkinter._fix runs to set up the environment -support.import_fresh_module('tkinter') +from tkinter import _fix from tkinter import Tcl from _tkinter import TclError @@ -269,13 +268,14 @@ def setUpModule(): + _fix.fix_environ() if support.verbose: tcl = Tcl() print('patchlevel =', tcl.call('info', 'patchlevel')) +def tearDownModule(): + _fix.unfix_environ() -def test_main(): - support.run_unittest(TclTest, TkinterTest, BigmemTclTest) if __name__ == "__main__": - test_main() + unittest.main() diff -r ce89c5c3c786 Lib/test/test_tk.py --- a/Lib/test/test_tk.py Wed Dec 18 11:27:21 2013 -0800 +++ b/Lib/test/test_tk.py Fri Dec 20 10:26:22 2013 -0600 @@ -2,8 +2,7 @@ # Skip test if _tkinter wasn't built. support.import_module('_tkinter') -# Make sure tkinter._fix runs to set up the environment -support.import_fresh_module('tkinter') +from tkinter import _fix # Skip test if tk cannot be initialized. from tkinter.test.support import check_tk_availability @@ -18,8 +17,13 @@ elif 'gui' not in support.use_resources: support.use_resources.append('gui') + # make sure init.tcl can be found + _fix.fix_environ() support.run_unittest( *runtktests.get_tests(text=False, packages=['test_tkinter'])) + # suppress warning about os.environ changing + _fix.unfix_environ() + if __name__ == '__main__': test_main(enable_gui=True) diff -r ce89c5c3c786 Lib/test/test_ttk_guionly.py --- a/Lib/test/test_ttk_guionly.py Wed Dec 18 11:27:21 2013 -0800 +++ b/Lib/test/test_ttk_guionly.py Fri Dec 20 10:26:22 2013 -0600 @@ -5,8 +5,7 @@ # Skip this test if _tkinter wasn't built. support.import_module('_tkinter') -# Make sure tkinter._fix runs to set up the environment -support.import_fresh_module('tkinter') +from tkinter import _fix # Skip test if tk cannot be initialized. from tkinter.test.support import check_tk_availability @@ -30,11 +29,17 @@ elif 'gui' not in support.use_resources: support.use_resources.append('gui') + # make sure init.tcl can be found + _fix.fix_environ() try: support.run_unittest( *runtktests.get_tests(text=False, packages=['test_ttk'])) finally: + # don't leave a window hanging around get_tk_root().destroy() + # suppress warning about os.environ changing + _fix.unfix_environ() + if __name__ == '__main__': test_main(enable_gui=True) diff -r ce89c5c3c786 Lib/test/test_ttk_textonly.py --- a/Lib/test/test_ttk_textonly.py Wed Dec 18 11:27:21 2013 -0800 +++ b/Lib/test/test_ttk_textonly.py Fri Dec 20 10:26:22 2013 -0600 @@ -4,14 +4,17 @@ # Skip this test if _tkinter does not exist. support.import_module('_tkinter') -# Make sure tkinter._fix runs to set up the environment -support.import_fresh_module('tkinter') - +from tkinter import _fix from tkinter.test import runtktests def test_main(): + # make sure init.tcl can be found + _fix.fix_environ() support.run_unittest( *runtktests.get_tests(gui=False, packages=['test_ttk'])) + # suppress warning about os.environ changing + _fix.unfix_environ() + if __name__ == '__main__': test_main() diff -r ce89c5c3c786 Lib/tkinter/_fix.py --- a/Lib/tkinter/_fix.py Wed Dec 18 11:27:21 2013 -0800 +++ b/Lib/tkinter/_fix.py Fri Dec 20 10:26:22 2013 -0600 @@ -46,33 +46,48 @@ s = "\\" + s[3:] return s -prefix = os.path.join(sys.base_prefix,"tcl") -if not os.path.exists(prefix): - # devdir/../tcltk/lib - prefix = os.path.join(sys.base_prefix, os.path.pardir, "tcltk", "lib") - prefix = os.path.abspath(prefix) -# if this does not exist, no further search is needed -if os.path.exists(prefix): - prefix = convert_path(prefix) - if "TCL_LIBRARY" not in os.environ: - for name in os.listdir(prefix): - if name.startswith("tcl"): - tcldir = os.path.join(prefix,name) - if os.path.isdir(tcldir): - os.environ["TCL_LIBRARY"] = tcldir - # Compute TK_LIBRARY, knowing that it has the same version - # as Tcl - import _tkinter - ver = str(_tkinter.TCL_VERSION) - if "TK_LIBRARY" not in os.environ: - v = os.path.join(prefix, 'tk'+ver) - if os.path.exists(os.path.join(v, "tclIndex")): - os.environ['TK_LIBRARY'] = v - # We don't know the Tix version, so we must search the entire - # directory - if "TIX_LIBRARY" not in os.environ: - for name in os.listdir(prefix): - if name.startswith("tix"): - tixdir = os.path.join(prefix,name) - if os.path.isdir(tixdir): - os.environ["TIX_LIBRARY"] = tixdir +orig_environ = os.environ.copy() + +def fix_environ(): + # Add (TCL|TK|TIX)_LIBRARY vars to os.environ so init.tcl can be found + prefix = os.path.join(sys.base_prefix,"tcl") + if not os.path.exists(prefix): + # devdir/../tcltk/lib + prefix = os.path.join(sys.base_prefix, os.path.pardir, "tcltk", "lib") + prefix = os.path.abspath(prefix) + # if this does not exist, no further search is needed + if os.path.exists(prefix): + prefix = convert_path(prefix) + if "TCL_LIBRARY" not in os.environ: + for name in os.listdir(prefix): + if name.startswith("tcl"): + tcldir = os.path.join(prefix,name) + if os.path.isdir(tcldir): + os.environ["TCL_LIBRARY"] = tcldir + # Compute TK_LIBRARY, knowing that it has the same version + # as Tcl + import _tkinter + ver = str(_tkinter.TCL_VERSION) + if "TK_LIBRARY" not in os.environ: + v = os.path.join(prefix, 'tk'+ver) + if os.path.exists(os.path.join(v, "tclIndex")): + os.environ['TK_LIBRARY'] = v + # We don't know the Tix version, so we must search the entire + # directory + if "TIX_LIBRARY" not in os.environ: + for name in os.listdir(prefix): + if name.startswith("tix"): + tixdir = os.path.join(prefix,name) + if os.path.isdir(tixdir): + os.environ["TIX_LIBRARY"] = tixdir + +def unfix_environ(orig_environ=orig_environ): + # Use this function to undo the changes made by fix_environ() + for var in ['TCL_LIBRARY', 'TK_LIBRARY', 'TIX_LIBRARY']: + if var not in orig_environ: + os.environ.pop(var, None) + +del orig_environ + +# call unconditionally to match old behavior on import +fix_environ()