From 240f0c8830aaf4054d78ac6eab1cfbdb31798ed5 Mon Sep 17 00:00:00 2001 From: Min RK Date: Tue, 3 Nov 2015 15:11:12 +0100 Subject: [PATCH] cleanup temporary files in ccompiler.has_function using TemporaryDirectory --- Lib/distutils/ccompiler.py | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/Lib/distutils/ccompiler.py b/Lib/distutils/ccompiler.py index 82fd7d5..d3b054f 100644 --- a/Lib/distutils/ccompiler.py +++ b/Lib/distutils/ccompiler.py @@ -775,30 +775,34 @@ class CCompiler: libraries = [] if library_dirs is None: library_dirs = [] - fd, fname = tempfile.mkstemp(".c", funcname, text=True) - f = os.fdopen(fd, "w") - try: - for incl in includes: - f.write("""#include "%s"\n""" % incl) - f.write("""\ + with tempfile.TemporaryDirectory() as td: + fd, fname = tempfile.mkstemp(".c", funcname, dir=td, text=True) + f = os.fdopen(fd, "w") + try: + for incl in includes: + f.write("""#include "%s"\n""" % incl) + f.write("""\ main (int argc, char **argv) { %s(); } """ % funcname) - finally: - f.close() - try: - objects = self.compile([fname], include_dirs=include_dirs) - except CompileError: - return False - - try: - self.link_executable(objects, "a.out", - libraries=libraries, - library_dirs=library_dirs) - except (LinkError, TypeError): - return False - return True + finally: + f.close() + try: + objects = self.compile([fname], + output_dir=td, + include_dirs=include_dirs) + except CompileError: + return False + + try: + self.link_executable(objects, "a.out", + output_dir=td, + libraries=libraries, + library_dirs=library_dirs) + except (LinkError, TypeError): + return False + return True def find_library_file (self, dirs, lib, debug=0): """Search the specified list of directories for a static or shared -- 2.6.2