Issue #22826: The result of open() in Tools/freeze/bkfile.py is now b… · pythoncapi/cpython@53c3fb1 · GitHub
Skip to content

Commit 53c3fb1

Browse files
Issue python#22826: The result of open() in Tools/freeze/bkfile.py is now better
compatible with regular files (in particular it now supports the context management protocol).
1 parent 8490f5a commit 53c3fb1

4 files changed

Lines changed: 59 additions & 87 deletions

File tree

Misc/NEWS

Lines changed: 8 additions & 0 deletions

Tools/freeze/bkfile.py

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,26 @@
11
from builtins import open as _orig_open
22

3-
class _BkFile:
4-
def __init__(self, file, mode, bufsize):
5-
import os
6-
self.__filename = file
7-
self.__backup = file + '~'
8-
try:
9-
os.unlink(self.__backup)
10-
except OSError:
11-
pass
12-
try:
13-
os.rename(file, self.__backup)
14-
except OSError:
15-
self.__backup = None
16-
self.__file = _orig_open(file, mode, bufsize)
17-
self.closed = self.__file.closed
18-
self.fileno = self.__file.fileno
19-
self.flush = self.__file.flush
20-
self.isatty = self.__file.isatty
21-
self.mode = self.__file.mode
22-
self.name = self.__file.name
23-
self.read = self.__file.read
24-
try:
25-
self.readinto = self.__file.readinto
26-
except AttributeError:
27-
pass
28-
self.readline = self.__file.readline
29-
self.readlines = self.__file.readlines
30-
self.seek = self.__file.seek
31-
self.tell = self.__file.tell
32-
self.truncate = self.__file.truncate
33-
self.write = self.__file.write
34-
self.writelines = self.__file.writelines
35-
36-
def close(self):
37-
self.__file.close()
38-
if self.__backup is None:
39-
return
40-
import filecmp
41-
if filecmp.cmp(self.__backup, self.__filename, shallow = 0):
42-
import os
43-
os.unlink(self.__filename)
44-
os.rename(self.__backup, self.__filename)
45-
46-
def open(file, mode = 'r', bufsize = -1):
3+
def open(file, mode='r', bufsize=-1):
474
if 'w' not in mode:
485
return _orig_open(file, mode, bufsize)
49-
return _BkFile(file, mode, bufsize)
6+
import os
7+
backup = file + '~'
8+
try:
9+
os.unlink(backup)
10+
except OSError:
11+
pass
12+
try:
13+
os.rename(file, backup)
14+
except OSError:
15+
return _orig_open(file, mode, bufsize)
16+
f = _orig_open(file, mode, bufsize)
17+
_orig_close = f.close
18+
def close():
19+
_orig_close()
20+
import filecmp
21+
if filecmp.cmp(backup, file, shallow=False):
22+
import os
23+
os.unlink(file)
24+
os.rename(backup, file)
25+
f.close = close
26+
return f

Tools/freeze/freeze.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -439,25 +439,17 @@ def main():
439439
frozendllmain_c, os.path.basename(extensions_c)] + files
440440
maindefn = checkextensions_win32.CExtension( '__main__', xtras )
441441
frozen_extensions.append( maindefn )
442-
outfp = open(makefile, 'w')
443-
try:
442+
with open(makefile, 'w') as outfp:
444443
winmakemakefile.makemakefile(outfp,
445444
locals(),
446445
frozen_extensions,
447446
os.path.basename(target))
448-
finally:
449-
outfp.close()
450447
return
451448

452449
# generate config.c and Makefile
453450
builtins.sort()
454-
infp = open(config_c_in)
455-
outfp = bkfile.open(config_c, 'w')
456-
try:
451+
with open(config_c_in) as infp, bkfile.open(config_c, 'w') as outfp:
457452
makeconfig.makeconfig(infp, outfp, builtins)
458-
finally:
459-
outfp.close()
460-
infp.close()
461453

462454
cflags = ['$(OPT)']
463455
cppflags = defines + includes
@@ -475,11 +467,8 @@ def main():
475467
files + supp_sources + addfiles + libs + \
476468
['$(MODLIBS)', '$(LIBS)', '$(SYSLIBS)']
477469

478-
outfp = bkfile.open(makefile, 'w')
479-
try:
470+
with bkfile.open(makefile, 'w') as outfp:
480471
makemakefile.makemakefile(outfp, somevars, files, base_target)
481-
finally:
482-
outfp.close()
483472

484473
# Done!
485474

Tools/freeze/makefreeze.py

Lines changed: 26 additions & 28 deletions

0 commit comments

Comments
 (0)