mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
bpo-22831: Use "with" to avoid possible fd leaks in tests (part 2). (GH-10929)
This commit is contained in:
parent
9e4861f523
commit
5b10b98247
25 changed files with 253 additions and 312 deletions
|
|
@ -205,31 +205,28 @@ def make_script(script_dir, script_basename, source, omit_suffix=False):
|
|||
script_filename += os.extsep + 'py'
|
||||
script_name = os.path.join(script_dir, script_filename)
|
||||
# The script should be encoded to UTF-8, the default string encoding
|
||||
script_file = open(script_name, 'w', encoding='utf-8')
|
||||
script_file.write(source)
|
||||
script_file.close()
|
||||
with open(script_name, 'w', encoding='utf-8') as script_file:
|
||||
script_file.write(source)
|
||||
importlib.invalidate_caches()
|
||||
return script_name
|
||||
|
||||
def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
|
||||
zip_filename = zip_basename+os.extsep+'zip'
|
||||
zip_name = os.path.join(zip_dir, zip_filename)
|
||||
zip_file = zipfile.ZipFile(zip_name, 'w')
|
||||
if name_in_zip is None:
|
||||
parts = script_name.split(os.sep)
|
||||
if len(parts) >= 2 and parts[-2] == '__pycache__':
|
||||
legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
|
||||
name_in_zip = os.path.basename(legacy_pyc)
|
||||
script_name = legacy_pyc
|
||||
else:
|
||||
name_in_zip = os.path.basename(script_name)
|
||||
zip_file.write(script_name, name_in_zip)
|
||||
zip_file.close()
|
||||
with zipfile.ZipFile(zip_name, 'w') as zip_file:
|
||||
if name_in_zip is None:
|
||||
parts = script_name.split(os.sep)
|
||||
if len(parts) >= 2 and parts[-2] == '__pycache__':
|
||||
legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
|
||||
name_in_zip = os.path.basename(legacy_pyc)
|
||||
script_name = legacy_pyc
|
||||
else:
|
||||
name_in_zip = os.path.basename(script_name)
|
||||
zip_file.write(script_name, name_in_zip)
|
||||
#if test.support.verbose:
|
||||
# zip_file = zipfile.ZipFile(zip_name, 'r')
|
||||
# print 'Contents of %r:' % zip_name
|
||||
# zip_file.printdir()
|
||||
# zip_file.close()
|
||||
# with zipfile.ZipFile(zip_name, 'r') as zip_file:
|
||||
# print 'Contents of %r:' % zip_name
|
||||
# zip_file.printdir()
|
||||
return zip_name, os.path.join(zip_name, name_in_zip)
|
||||
|
||||
def make_pkg(pkg_dir, init_source=''):
|
||||
|
|
@ -252,17 +249,15 @@ def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
|
|||
script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
|
||||
zip_filename = zip_basename+os.extsep+'zip'
|
||||
zip_name = os.path.join(zip_dir, zip_filename)
|
||||
zip_file = zipfile.ZipFile(zip_name, 'w')
|
||||
for name in pkg_names:
|
||||
init_name_in_zip = os.path.join(name, init_basename)
|
||||
zip_file.write(init_name, init_name_in_zip)
|
||||
zip_file.write(script_name, script_name_in_zip)
|
||||
zip_file.close()
|
||||
with zipfile.ZipFile(zip_name, 'w') as zip_file:
|
||||
for name in pkg_names:
|
||||
init_name_in_zip = os.path.join(name, init_basename)
|
||||
zip_file.write(init_name, init_name_in_zip)
|
||||
zip_file.write(script_name, script_name_in_zip)
|
||||
for name in unlink:
|
||||
os.unlink(name)
|
||||
#if test.support.verbose:
|
||||
# zip_file = zipfile.ZipFile(zip_name, 'r')
|
||||
# print 'Contents of %r:' % zip_name
|
||||
# zip_file.printdir()
|
||||
# zip_file.close()
|
||||
# with zipfile.ZipFile(zip_name, 'r') as zip_file:
|
||||
# print 'Contents of %r:' % zip_name
|
||||
# zip_file.printdir()
|
||||
return zip_name, os.path.join(zip_name, script_name_in_zip)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue