pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/53c3fb186a587e01f88ec9c6b365fd614668d214

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

Commit 53c3fb1

Browse files
Issue #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
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ Build
8989

9090
- Issue #23585: make patchcheck will ensure the interpreter is built.
9191

92+
Tools/Demos
93+
-----------
94+
95+
- Issue #22826: The result of open() in Tools/freeze/bkfile.py is now better
96+
compatible with regular files (in particular it now supports the context
97+
management protocol).
98+
99+
92100
What's New in Python 3.5 alpha 2?
93101
=================================
94102

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
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,34 @@ def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()):
3939
mangled = "__".join(mod.split("."))
4040
if m.__code__:
4141
file = 'M_' + mangled + '.c'
42-
outfp = bkfile.open(base + file, 'w')
43-
files.append(file)
44-
if debug:
45-
print("freezing", mod, "...")
46-
str = marshal.dumps(m.__code__)
47-
size = len(str)
48-
if m.__path__:
49-
# Indicate package by negative size
50-
size = -size
51-
done.append((mod, mangled, size))
52-
writecode(outfp, mangled, str)
53-
outfp.close()
42+
with bkfile.open(base + file, 'w') as outfp:
43+
files.append(file)
44+
if debug:
45+
print("freezing", mod, "...")
46+
str = marshal.dumps(m.__code__)
47+
size = len(str)
48+
if m.__path__:
49+
# Indicate package by negative size
50+
size = -size
51+
done.append((mod, mangled, size))
52+
writecode(outfp, mangled, str)
5453
if debug:
5554
print("generating table of frozen modules")
56-
outfp = bkfile.open(base + 'frozen.c', 'w')
57-
for mod, mangled, size in done:
58-
outfp.write('extern unsigned char M_%s[];\n' % mangled)
59-
outfp.write(header)
60-
for mod, mangled, size in done:
61-
outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size))
62-
outfp.write('\n')
63-
# The following modules have a NULL code pointer, indicating
64-
# that the frozen program should not search for them on the host
65-
# system. Importing them will *always* raise an ImportError.
66-
# The zero value size is never used.
67-
for mod in fail_import:
68-
outfp.write('\t{"%s", NULL, 0},\n' % (mod,))
69-
outfp.write(trailer)
70-
outfp.write(entry_point)
71-
outfp.close()
55+
with bkfile.open(base + 'frozen.c', 'w') as outfp:
56+
for mod, mangled, size in done:
57+
outfp.write('extern unsigned char M_%s[];\n' % mangled)
58+
outfp.write(header)
59+
for mod, mangled, size in done:
60+
outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size))
61+
outfp.write('\n')
62+
# The following modules have a NULL code pointer, indicating
63+
# that the frozen program should not search for them on the host
64+
# system. Importing them will *always* raise an ImportError.
65+
# The zero value size is never used.
66+
for mod in fail_import:
67+
outfp.write('\t{"%s", NULL, 0},\n' % (mod,))
68+
outfp.write(trailer)
69+
outfp.write(entry_point)
7270
return files
7371

7472

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy