diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-20 07:21:59 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-20 07:21:59 (GMT) |
commit | 53c3fb186a587e01f88ec9c6b365fd614668d214 (patch) | |
tree | afa6312f355ee29e22ea3c940119200bde1845cd /Tools/freeze/makefreeze.py | |
parent | 8490f5acfee7269ac0eb41257a3a214dfb31a655 (diff) | |
download | cpython-53c3fb186a587e01f88ec9c6b365fd614668d214.zip cpython-53c3fb186a587e01f88ec9c6b365fd614668d214.tar.gz cpython-53c3fb186a587e01f88ec9c6b365fd614668d214.tar.bz2 |
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).
Diffstat (limited to 'Tools/freeze/makefreeze.py')
-rw-r--r-- | Tools/freeze/makefreeze.py | 54 |
1 files changed, 26 insertions, 28 deletions
diff --git a/Tools/freeze/makefreeze.py b/Tools/freeze/makefreeze.py index ef18ec7..64e3e6b 100644 --- a/Tools/freeze/makefreeze.py +++ b/Tools/freeze/makefreeze.py @@ -39,36 +39,34 @@ def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()): mangled = "__".join(mod.split(".")) if m.__code__: file = 'M_' + mangled + '.c' - outfp = bkfile.open(base + file, 'w') - files.append(file) - if debug: - print("freezing", mod, "...") - str = marshal.dumps(m.__code__) - size = len(str) - if m.__path__: - # Indicate package by negative size - size = -size - done.append((mod, mangled, size)) - writecode(outfp, mangled, str) - outfp.close() + with bkfile.open(base + file, 'w') as outfp: + files.append(file) + if debug: + print("freezing", mod, "...") + str = marshal.dumps(m.__code__) + size = len(str) + if m.__path__: + # Indicate package by negative size + size = -size + done.append((mod, mangled, size)) + writecode(outfp, mangled, str) if debug: print("generating table of frozen modules") - outfp = bkfile.open(base + 'frozen.c', 'w') - for mod, mangled, size in done: - outfp.write('extern unsigned char M_%s[];\n' % mangled) - outfp.write(header) - for mod, mangled, size in done: - outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size)) - outfp.write('\n') - # The following modules have a NULL code pointer, indicating - # that the frozen program should not search for them on the host - # system. Importing them will *always* raise an ImportError. - # The zero value size is never used. - for mod in fail_import: - outfp.write('\t{"%s", NULL, 0},\n' % (mod,)) - outfp.write(trailer) - outfp.write(entry_point) - outfp.close() + with bkfile.open(base + 'frozen.c', 'w') as outfp: + for mod, mangled, size in done: + outfp.write('extern unsigned char M_%s[];\n' % mangled) + outfp.write(header) + for mod, mangled, size in done: + outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size)) + outfp.write('\n') + # The following modules have a NULL code pointer, indicating + # that the frozen program should not search for them on the host + # system. Importing them will *always* raise an ImportError. + # The zero value size is never used. + for mod in fail_import: + outfp.write('\t{"%s", NULL, 0},\n' % (mod,)) + outfp.write(trailer) + outfp.write(entry_point) return files |