diff -r ae76a1046bb9 Lib/distutils/ccompiler.py --- a/Lib/distutils/ccompiler.py Wed Mar 16 14:30:16 2016 +0100 +++ b/Lib/distutils/ccompiler.py Fri Apr 08 19:58:10 2016 +0200 @@ -775,30 +775,31 @@ 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: + fname = os.path.join(td, funcname + '.c') + with open(fname, 'w') as f: + 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: + objects = self.compile([fname], + output_dir=td, + 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 + 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