setup.py
changeset 52322 40f649592ba9
parent 52321 dd8e7d4fe8c3
child 52323 599b6ce304a7
equal deleted inserted replaced
52321:dd8e7d4fe8c3 52322:40f649592ba9
   119     scripts.append('contrib/win32/hg.bat')
   119     scripts.append('contrib/win32/hg.bat')
   120 
   120 
   121 
   121 
   122 def cancompile(cc, code):
   122 def cancompile(cc, code):
   123     tmpdir = tempfile.mkdtemp(prefix='hg-install-')
   123     tmpdir = tempfile.mkdtemp(prefix='hg-install-')
   124     devnull = oldstderr = None
   124     oldstderr = None
   125     try:
   125     try:
   126         fname = os.path.join(tmpdir, 'testcomp.c')
   126         fname = os.path.join(tmpdir, 'testcomp.c')
   127         f = open(fname, 'w')
   127         with open(fname, 'w') as file:
   128         f.write(code)
   128             file.write(code)
   129         f.close()
   129         oldstderr = os.dup(sys.stderr.fileno())
   130         # Redirect stderr to /dev/null to hide any error messages
   130         # Redirect stderr to /dev/null to hide any error messages
   131         # from the compiler.
   131         # from the compiler.
   132         # This will have to be changed if we ever have to check
   132         # This will have to be changed if we ever have to check
   133         # for a function on Windows.
   133         # for a function on Windows.
   134         devnull = open('/dev/null', 'w')
   134         with open('/dev/null', 'w') as devnull:
   135         oldstderr = os.dup(sys.stderr.fileno())
   135             os.dup2(devnull.fileno(), sys.stderr.fileno())
   136         os.dup2(devnull.fileno(), sys.stderr.fileno())
       
   137         objects = cc.compile([fname], output_dir=tmpdir)
   136         objects = cc.compile([fname], output_dir=tmpdir)
   138         cc.link_executable(objects, os.path.join(tmpdir, "a.out"))
   137         cc.link_executable(objects, os.path.join(tmpdir, "a.out"))
   139         return True
   138         return True
   140     except Exception:
   139     except Exception:
   141         return False
   140         return False
   142     finally:
   141     finally:
   143         if oldstderr is not None:
   142         if oldstderr is not None:
   144             os.dup2(oldstderr, sys.stderr.fileno())
   143             os.dup2(oldstderr, sys.stderr.fileno())
   145         if devnull is not None:
       
   146             devnull.close()
       
   147         shutil.rmtree(tmpdir)
   144         shutil.rmtree(tmpdir)
   148 
   145 
   149 
   146 
   150 # simplified version of distutils.ccompiler.CCompiler.has_function
   147 # simplified version of distutils.ccompiler.CCompiler.has_function
   151 # that actually removes its temporary files.
   148 # that actually removes its temporary files.