Index: Lib/distutils/tests/test_build_ext.py =================================================================== --- Lib/distutils/tests/test_build_ext.py (Revision 67313) +++ Lib/distutils/tests/test_build_ext.py (Arbeitskopie) @@ -1,3 +1,4 @@ +import atexit import sys import os import tempfile @@ -11,29 +12,43 @@ import unittest from test import support +# We have to use a single tempdir here. Otherwise every +# iteration would keep references in Python/import.c:*extensions. +TMP_DIR = None + +def cleanup_tmpdir(): + if TMP_DIR is not None and os.path.isdir(TMP_DIR): + shutil.rmtree(TMP_DIR, os.name == 'nt' or sys.platform == 'cygwin') + +atexit.register(cleanup_tmpdir) + + class BuildExtTestCase(unittest.TestCase): def setUp(self): + global TMP_DIR # Create a simple test environment # Note that we're making changes to sys.path - self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_") + if TMP_DIR is None: + TMP_DIR = tempfile.mkdtemp(prefix="pythontest_") self.sys_path = sys.path[:] - sys.path.append(self.tmp_dir) + sys.path.append(TMP_DIR) xx_c = os.path.join(sysconfig.project_base, 'Modules', 'xxmodule.c') - shutil.copy(xx_c, self.tmp_dir) + shutil.copy(xx_c, TMP_DIR) def test_build_ext(self): - xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') + global ALREADY_TESTED + xx_c = os.path.join(TMP_DIR, 'xxmodule.c') xx_ext = Extension('xx', [xx_c]) dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) - dist.package_dir = self.tmp_dir + dist.package_dir = TMP_DIR cmd = build_ext(dist) if os.name == "nt": # On Windows, we must build a debug version iff running # a debug build of Python cmd.debug = sys.executable.endswith("_d.exe") - cmd.build_lib = self.tmp_dir - cmd.build_temp = self.tmp_dir + cmd.build_lib = TMP_DIR + cmd.build_temp = TMP_DIR old_stdout = sys.stdout if not support.verbose: @@ -62,9 +77,14 @@ # Get everything back to normal support.unload('xx') sys.path = self.sys_path - # XXX on Windows the test leaves a directory with xx module in TEMP - shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin') + # clean the tmp dir but don't remove it + for path in (os.path.join(TMP_DIR, f) for f in os.listdir(TMP_DIR)): + if os.path.isdir(path): + shutil.rmtree(path, os.name == 'nt' or sys.platform == 'cygwin') + else: + os.unlink(path) + def test_suite(): if not sysconfig.python_build: if support.verbose: