diff --git a/setup.py b/setup.py
index dd8e7d4fe8c307a7c42c1dab5103292bf8e02975_c2V0dXAucHk=..40f649592ba94f62402e25dacbcd1bebf5d5714b_c2V0dXAucHk= 100644
--- a/setup.py
+++ b/setup.py
@@ -121,6 +121,6 @@
 
 def cancompile(cc, code):
     tmpdir = tempfile.mkdtemp(prefix='hg-install-')
-    devnull = oldstderr = None
+    oldstderr = None
     try:
         fname = os.path.join(tmpdir, 'testcomp.c')
@@ -125,9 +125,9 @@
     try:
         fname = os.path.join(tmpdir, 'testcomp.c')
-        f = open(fname, 'w')
-        f.write(code)
-        f.close()
+        with open(fname, 'w') as file:
+            file.write(code)
+        oldstderr = os.dup(sys.stderr.fileno())
         # 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.
@@ -130,10 +130,9 @@
         # 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())
+        with open('/dev/null', 'w') as devnull:
+            os.dup2(devnull.fileno(), sys.stderr.fileno())
         objects = cc.compile([fname], output_dir=tmpdir)
         cc.link_executable(objects, os.path.join(tmpdir, "a.out"))
         return True
@@ -142,8 +141,6 @@
     finally:
         if oldstderr is not None:
             os.dup2(oldstderr, sys.stderr.fileno())
-        if devnull is not None:
-            devnull.close()
         shutil.rmtree(tmpdir)