diff setup.py @ 52357:40f649592ba9

setup_py: simplify cancompile with 'with open(...)'
author paugier <pierre.augier@univ-grenoble-alpes.fr>
date Thu, 24 Oct 2024 15:49:33 +0200
parents dd8e7d4fe8c3
children 599b6ce304a7
line wrap: on
line diff
--- a/setup.py	Sat Nov 23 06:07:40 2024 +0100
+++ b/setup.py	Thu Oct 24 15:49:33 2024 +0200
@@ -121,19 +121,18 @@
 
 def cancompile(cc, code):
     tmpdir = tempfile.mkdtemp(prefix='hg-install-')
-    devnull = oldstderr = None
+    oldstderr = None
     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.
-        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)