Skip to content

Commit 8618d32

Browse files
jholgitster
authored andcommitted
git-p4: use with statements to close files after use in patchRCSKeywords
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e773545 commit 8618d32

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

git-p4.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,14 +1757,11 @@ def patchRCSKeywords(self, file, pattern):
17571757
# Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
17581758
(handle, outFileName) = tempfile.mkstemp(dir='.')
17591759
try:
1760-
outFile = os.fdopen(handle, "w+")
1761-
inFile = open(file, "r")
1762-
regexp = re.compile(pattern, re.VERBOSE)
1763-
for line in inFile.readlines():
1764-
line = regexp.sub(r'$\1$', line)
1765-
outFile.write(line)
1766-
inFile.close()
1767-
outFile.close()
1760+
with os.fdopen(handle, "w+") as outFile, open(file, "r") as inFile:
1761+
regexp = re.compile(pattern, re.VERBOSE)
1762+
for line in inFile.readlines():
1763+
line = regexp.sub(r'$\1$', line)
1764+
outFile.write(line)
17681765
# Forcibly overwrite the original file
17691766
os.unlink(file)
17701767
shutil.move(outFileName, file)

0 commit comments

Comments
 (0)