setup.py: hide compiler error messages while searching for inotify
authorAlexis S. L. Carvalho <alexis@cecm.usp.br>
Sun, 23 Mar 2008 21:35:57 -0300
changeset 6373 7010e4557963
parent 6372 8f79820443a4
child 6374 31a01e3d99cc
setup.py: hide compiler error messages while searching for inotify
setup.py
--- a/setup.py	Sun Mar 23 23:18:20 2008 +0100
+++ b/setup.py	Sun Mar 23 21:35:57 2008 -0300
@@ -24,20 +24,32 @@
 # that actually removes its temporary files.
 def has_function(cc, funcname):
     tmpdir = tempfile.mkdtemp(prefix='hg-install-')
+    devnull = oldstderr = None
     try:
-        fname = os.path.join(tmpdir, 'funcname.c')
-        f = open(fname, 'w')
-        f.write('int main(void) {\n')
-        f.write('    %s();\n' % funcname)
-        f.write('}\n')
-        f.close()
         try:
+            fname = os.path.join(tmpdir, 'funcname.c')
+            f = open(fname, 'w')
+            f.write('int main(void) {\n')
+            f.write('    %s();\n' % funcname)
+            f.write('}\n')
+            f.close()
+            # Redirect stderr to /dev/null to hide any error messages
+            # from the compiler.
+            # This will have to be changed if we ever have to check
+            # for a function on Windows.
+            devnull = open('/dev/null', 'w')
+            oldstderr = os.dup(sys.stderr.fileno())
+            os.dup2(devnull.fileno(), sys.stderr.fileno())
             objects = cc.compile([fname])
             cc.link_executable(objects, os.path.join(tmpdir, "a.out"))
         except:
             return False
         return True
     finally:
+        if oldstderr is not None:
+            os.dup2(oldstderr, sys.stderr.fileno())
+        if devnull is not None:
+            devnull.close()
         shutil.rmtree(tmpdir)
 
 # py2exe needs to be installed to work