]> git.ipfire.org Git - thirdparty/git.git/commitdiff
git-p4: use with statements to close files after use in patchRCSKeywords
authorJoel Holdsworth <jholdsworth@nvidia.com>
Thu, 16 Dec 2021 13:46:15 +0000 (13:46 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Dec 2021 22:06:35 +0000 (14:06 -0800)
Python with statements are used to wrap the execution of a block of code
so that an object can be safely released when execution leaves the
scope.

They are desirable for improving code tidyness, and to ensure that
objects are properly destroyed even when exceptions are thrown.

Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-p4.py

index 2b4500226aa7a48d2b2644d3b1f351342cda20f4..226cdef424b9288862df063bb6d649a8c0517063 100755 (executable)
--- a/git-p4.py
+++ b/git-p4.py
@@ -1757,14 +1757,11 @@ class P4Submit(Command, P4UserMap):
         # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
         (handle, outFileName) = tempfile.mkstemp(dir='.')
         try:
-            outFile = os.fdopen(handle, "w+")
-            inFile = open(file, "r")
-            regexp = re.compile(pattern, re.VERBOSE)
-            for line in inFile.readlines():
-                line = regexp.sub(r'$\1$', line)
-                outFile.write(line)
-            inFile.close()
-            outFile.close()
+            with os.fdopen(handle, "w+") as outFile, open(file, "r") as inFile:
+                regexp = re.compile(pattern, re.VERBOSE)
+                for line in inFile.readlines():
+                    line = regexp.sub(r'$\1$', line)
+                    outFile.write(line)
             # Forcibly overwrite the original file
             os.unlink(file)
             shutil.move(outFileName, file)