Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFC Explicitly close file descriptors in emcc.py #14074

Merged
merged 18 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from tools import colored_logger, diagnostics, building
from tools.shared import unsuffixed, unsuffixed_basename, WINDOWS, safe_copy
from tools.shared import run_process, read_and_preprocess, exit_with_error, DEBUG
from tools.shared import read_file, write_file, read_binary
from tools.shared import do_replace, strip_prefix
from tools.response_file import substitute_response_files
from tools.minimal_runtime_shell import generate_minimal_runtime_html
Expand Down Expand Up @@ -357,7 +358,7 @@ def standardize_setting_change(key, value):
filename = strip_prefix(value, '@')
if not os.path.exists(filename):
exit_with_error('%s: file not found parsing argument: %s=%s' % (filename, key, value))
value = open(filename).read().strip()
value = read_file(filename).strip()
else:
value = value.replace('\\', '\\\\')

Expand Down Expand Up @@ -589,7 +590,7 @@ def check_human_readable_list(items):


def make_js_executable(script):
src = open(script).read()
src = read_file(script)
cmd = shared.shlex_join(config.JS_ENGINE)
if not os.path.isabs(config.JS_ENGINE[0]):
# TODO: use whereis etc. And how about non-*NIX?
Expand Down Expand Up @@ -1350,19 +1351,19 @@ def phase_linker_setup(options, state, newargs, settings_map):
add_link_flag(state, sys.maxsize, f)

if options.emrun:
options.pre_js += open(shared.path_from_root('src', 'emrun_prejs.js')).read() + '\n'
options.post_js += open(shared.path_from_root('src', 'emrun_postjs.js')).read() + '\n'
options.pre_js += read_file(shared.path_from_root('src', 'emrun_prejs.js')) + '\n'
options.post_js += read_file(shared.path_from_root('src', 'emrun_postjs.js')) + '\n'
# emrun mode waits on program exit
settings.EXIT_RUNTIME = 1

if options.cpu_profiler:
options.post_js += open(shared.path_from_root('src', 'cpuprofiler.js')).read() + '\n'
options.post_js += read_file(shared.path_from_root('src', 'cpuprofiler.js')) + '\n'

if options.memory_profiler:
settings.MEMORYPROFILER = 1

if options.thread_profiler:
options.post_js += open(shared.path_from_root('src', 'threadprofiler.js')).read() + '\n'
options.post_js += read_file(shared.path_from_root('src', 'threadprofiler.js')) + '\n'

if options.memory_init_file is None:
options.memory_init_file = settings.OPT_LEVEL >= 2
Expand Down Expand Up @@ -2527,7 +2528,7 @@ def phase_source_transforms(options, target):
# Apply pre and postjs files
if final_js and (options.pre_js or options.post_js):
logger.debug('applying pre/postjses')
src = open(final_js).read()
src = read_file(final_js)
final_js += '.pp.js'
with open(final_js, 'w') as f:
# pre-js code goes right after the Module integration code (so it
Expand All @@ -2553,9 +2554,9 @@ def phase_memory_initializer(memfile):
# is set the memory initializer url.
global final_js

src = open(final_js).read()
src = read_file(final_js)
src = do_replace(src, '// {{MEM_INITIALIZER}}', 'var memoryInitializer = "%s";' % os.path.basename(memfile))
open(final_js + '.mem.js', 'w').write(src)
write_file(final_js + '.mem.js', src)
final_js += '.mem.js'


Expand All @@ -2565,9 +2566,9 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):

# Remove some trivial whitespace
# TODO: do not run when compress has already been done on all parts of the code
# src = open(final_js).read()
# src = read_file(final_js)
# src = re.sub(r'\n+[ \n]*\n+', '\n', src)
# open(final_js, 'w').write(src)
# write_file(final_js, src)

if settings.USE_PTHREADS:
target_dir = os.path.dirname(os.path.abspath(target))
Expand All @@ -2578,7 +2579,7 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
# Minify the worker.js file in optimized builds
if (settings.OPT_LEVEL >= 1 or settings.SHRINK_LEVEL >= 1) and not settings.DEBUG_LEVEL:
minified_worker = building.acorn_optimizer(worker_output, ['minifyWhitespace'], return_output=True)
open(worker_output, 'w').write(minified_worker)
write_file(worker_output, minified_worker)

# track files that will need native eols
generated_text_files_with_native_eols = []
Expand All @@ -2599,16 +2600,15 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
# Unmangle previously mangled `import.meta` references in both main code and libraries.
# See also: `preprocess` in parseTools.js.
if settings.EXPORT_ES6 and settings.USE_ES6_IMPORT_META:
src = open(final_js).read()
src = read_file(final_js)
final_js += '.esmeta.js'
with open(final_js, 'w') as f:
f.write(src.replace('EMSCRIPTEN$IMPORT$META', 'import.meta'))
write_file(final_js, src.replace('EMSCRIPTEN$IMPORT$META', 'import.meta'))
save_intermediate('es6-import-meta')

# Apply pre and postjs files
if options.extern_pre_js or options.extern_post_js:
logger.debug('applying extern pre/postjses')
src = open(final_js).read()
src = read_file(final_js)
final_js += '.epp.js'
with open(final_js, 'w') as f:
f.write(fix_windows_newlines(options.extern_pre_js))
Expand Down Expand Up @@ -2753,13 +2753,13 @@ def consume_arg_file():
elif check_arg('--js-transform'):
options.js_transform = consume_arg()
elif check_arg('--pre-js'):
options.pre_js += open(consume_arg_file()).read() + '\n'
options.pre_js += read_file(consume_arg_file()) + '\n'
elif check_arg('--post-js'):
options.post_js += open(consume_arg_file()).read() + '\n'
options.post_js += read_file(consume_arg_file()) + '\n'
elif check_arg('--extern-pre-js'):
options.extern_pre_js += open(consume_arg_file()).read() + '\n'
options.extern_pre_js += read_file(consume_arg_file()) + '\n'
elif check_arg('--extern-post-js'):
options.extern_post_js += open(consume_arg_file()).read() + '\n'
options.extern_post_js += read_file(consume_arg_file()) + '\n'
elif check_arg('--compiler-wrapper'):
config.COMPILER_WRAPPER = consume_arg()
elif check_flag('--post-link'):
Expand Down Expand Up @@ -3177,10 +3177,10 @@ def run_closure_compiler():

# replace placeholder strings with correct subresource locations
if final_js and settings.SINGLE_FILE and not settings.WASM2JS:
js = open(final_js).read()
js = read_file(final_js)

if settings.MINIMAL_RUNTIME:
js = do_replace(js, '<<< WASM_BINARY_DATA >>>', base64_encode(open(wasm_target, 'rb').read()))
js = do_replace(js, '<<< WASM_BINARY_DATA >>>', base64_encode(read_binary(wasm_target)))
else:
js = do_replace(js, '<<< WASM_BINARY_FILE >>>', shared.JS.get_subresource_location(wasm_target))
shared.try_delete(wasm_target)
Expand All @@ -3191,7 +3191,7 @@ def run_closure_compiler():
def modularize():
global final_js
logger.debug('Modularizing, assigning to var ' + settings.EXPORT_NAME)
src = open(final_js).read()
src = read_file(final_js)

return_value = settings.EXPORT_NAME
if settings.WASM_ASYNC_COMPILATION:
Expand Down Expand Up @@ -3386,7 +3386,7 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
if settings.SINGLE_FILE:
js_contents = script.inline or ''
if script.src:
js_contents += open(js_target).read()
js_contents += read_file(js_target)
shared.try_delete(js_target)
script.src = None
script.inline = js_contents
Expand Down Expand Up @@ -3479,13 +3479,13 @@ def generate_worker_js(target, js_target, target_basename):
proxy_worker_filename = (settings.PROXY_TO_WORKER_FILENAME or worker_target_basename) + '.js'

target_contents = worker_js_script(proxy_worker_filename)
open(target, 'w').write(target_contents)
write_file(target, target_contents)


def worker_js_script(proxy_worker_filename):
web_gl_client_src = open(shared.path_from_root('src', 'webGLClient.js')).read()
idb_store_src = open(shared.path_from_root('src', 'IDBStore.js')).read()
proxy_client_src = open(shared.path_from_root('src', 'proxyClient.js')).read()
web_gl_client_src = read_file(shared.path_from_root('src', 'webGLClient.js'))
idb_store_src = read_file(shared.path_from_root('src', 'IDBStore.js'))
proxy_client_src = read_file(shared.path_from_root('src', 'proxyClient.js'))
proxy_client_src = do_replace(proxy_client_src, '{{{ filename }}}', proxy_worker_filename)
proxy_client_src = do_replace(proxy_client_src, '{{{ IDBStore.js }}}', idb_store_src)
return web_gl_client_src + '\n' + proxy_client_src
Expand Down
9 changes: 6 additions & 3 deletions emrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ def do_POST(self):
except OSError:
pass
filename = os.path.join(dump_out_directory, os.path.normpath(filename))
open(filename, 'wb').write(data)
with open(filename, 'wb') as fh:
fh.write(data)
logi('Wrote ' + str(len(data)) + ' bytes to file "' + filename + '".')
have_received_messages = True
elif path == '/system_info':
Expand Down Expand Up @@ -1060,7 +1061,8 @@ def get_computer_model():
model = check_output(cmd)
model = re.search('<configCode>(.*)</configCode>', model)
model = model.group(1).strip()
open(os.path.join(os.getenv("HOME"), '.emrun.hwmodel.cached'), 'w').write(model) # Cache the hardware model to disk
with open(os.path.join(os.getenv("HOME"), '.emrun.hwmodel.cached'), 'w') as fh:
fh.write(model) # Cache the hardware model to disk
return model
except Exception:
hwmodel = check_output(['sysctl', 'hw.model'])
Expand Down Expand Up @@ -1385,7 +1387,8 @@ def get_system_info(format_json):
return info.strip()
else:
try:
unique_system_id = open(os.path.expanduser('~/.emrun.generated.guid'), 'r').read().strip()
sbc100 marked this conversation as resolved.
Show resolved Hide resolved
with open(os.path.expanduser('~/.emrun.generated.guid')) as fh:
unique_system_id = fh.read().strip()
except Exception:
import uuid
unique_system_id = str(uuid.uuid4())
Expand Down
4 changes: 2 additions & 2 deletions site/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import sys
import os
from pathlib import Path


# At the top. #HamishW https://pypi.python.org/pypi/sphinx-bootstrap-theme/ ...
Expand Down Expand Up @@ -93,8 +94,7 @@
# |version| and |release|, also used in various other places throughout the
# built documents.
#

emscripten_version = open(os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'emscripten-version.txt'))).read().strip().replace('"', '')
rth marked this conversation as resolved.
Show resolved Hide resolved
emscripten_version = Path(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'emscripten-version.txt').resolve().read_text().strip().replace('"', '')

# The short X.Y version.
version = emscripten_version[:emscripten_version.rindex('.')]
Expand Down
3 changes: 2 additions & 1 deletion site/source/get_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import stat
import sys
import time
from pathlib import Path

import api_items

Expand Down Expand Up @@ -91,7 +92,7 @@ def ConvertFilesToRst():
continue

inputfilename = wiki_checkout + file
markdown = open(inputfilename).read()
rth marked this conversation as resolved.
Show resolved Hide resolved
markdown = Path(inputfilename).read_text()
if 'This article has moved from the wiki to the new site' in markdown:
continue
if 'This page has been migrated to the main site' in markdown:
Expand Down
8 changes: 6 additions & 2 deletions tools/clean_webconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import re
import sys
from pathlib import Path

__rootpath__ = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -21,8 +22,11 @@ def nice(x):
return '0x' + ('0' * (len(x) - 6)) + x[2:].upper()


repdata = open(path_from_root('system', 'include', 'GL', 'gl.h')).readlines() + ['\n'] + \
open(path_from_root('system', 'include', 'GL', 'glext.h')).readlines()
repdata = (
Path(path_from_root('system', 'include', 'GL', 'gl.h')).read_text().splitline(keepends=True) +
['\n'] +
Path(path_from_root('system', 'include', 'GL', 'glext.h')).read_text().splitlines(keepends=True)
)
reps = {}
for rep in repdata:
rep = rep.replace('\t', ' ').replace('\n', '')
Expand Down
7 changes: 5 additions & 2 deletions tools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def parse_config_file():
Also check EM_<KEY> environment variables to override specific config keys.
"""
config = {}
config_text = open(EM_CONFIG, 'r').read()
with open(EM_CONFIG) as fh:
config_text = fh.read()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you put the helper functions inutils.py you can use them from here.

try:
exec(config_text, config)
except Exception as e:
Expand Down Expand Up @@ -171,7 +172,9 @@ def parse_config_file():
def generate_config(path, first_time=False):
# Note: repr is used to ensure the paths are escaped correctly on Windows.
# The full string is replaced so that the template stays valid Python.
config_data = open(path_from_root('tools', 'settings_template.py')).read().splitlines()

with open(path_from_root('tools', 'settings_template.py')) as fh:
config_data = fh.read().splitlines()
config_data = config_data[3:] # remove the initial comment
config_data = '\n'.join(config_data)
# autodetect some default paths
Expand Down
4 changes: 2 additions & 2 deletions tools/ctor_evaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def eval_ctors(js, wasm_file, num):

# main
def main():
js = open(js_file).read()
js = shared.read_file(js_file)
ctors_start, ctors_end = find_ctors(js)
if ctors_start < 0:
logger.debug('ctor_evaller: no ctors')
Expand All @@ -114,7 +114,7 @@ def main():
logger.debug('ctor_evaller: not successful')
sys.exit(0)
logger.debug('ctor_evaller: we managed to remove %d ctors' % num_successful)
open(js_file, 'w').write(new_js)
shared.write_file(js_file, new_js)


if __name__ == '__main__':
Expand Down
6 changes: 4 additions & 2 deletions tools/debug/autodediffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from __future__ import print_function
import os, sys

from pathlib import Path


def process_line(line):
#AD:2041,0.900000
Expand All @@ -20,8 +22,8 @@ def process_line(line):
num, val = line.split(',')
return [int(num), float(val)]

a = open(sys.argv[1], 'r').readlines()
b = open(sys.argv[2], 'r').readlines()
a = Path(sys.argv[1]).read_text().splitlines(keepends=True)
b = Path(sys.argv[2]).read_text().splitlines(keepends=True)
MIN = 0.0001 if len(sys.argv) < 4 else sys.argv[3]

ai = 0
Expand Down
17 changes: 7 additions & 10 deletions tools/debug/bisect_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@

from __future__ import print_function
import os, sys, shutil
from pathlib import Path
from subprocess import Popen, PIPE, STDOUT

__rootpath__ = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))


def path_from_root(*pathelems):
return os.path.join(__rootpath__, *pathelems)
exec(open(path_from_root('tools', 'shared.py'), 'r').read())

file1 = open(sys.argv[1]).read()
file2 = open(sys.argv[2]).read()

exec(Path(path_from_root('tools', 'shared.py').read_text()))

leftf = open('left', 'w')
leftf.write(file1)
leftf.close()
shutil.copyfile(sys.argv[1], 'left')
shutil.copyfile(sys.argv[2], 'right')

rightf = open('right', 'w')
rightf.write(file2)
rightf.close()

def run_code(name):
ret = run_js(name, stderr=PIPE, full_output=True, assert_returncode=None, engine=SPIDERMONKEY_ENGINE)
Expand Down
17 changes: 6 additions & 11 deletions tools/debug/bisect_pair_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@

from __future__ import print_function
import os, sys, shutil
from pathlib import Path
from subprocess import Popen, PIPE, STDOUT

__rootpath__ = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
def path_from_root(*pathelems):
return os.path.join(__rootpath__, *pathelems)
exec(open(path_from_root('tools', 'shared.py'), 'r').read())

exec(Path(path_from_root('tools', 'shared.py').read())

file1 = open(sys.argv[1]).read()
file2 = open(sys.argv[2]).read()
shutil.copyfile(sys.argv[1], 'left')
shutil.copyfile(sys.argv[2], 'right')

leftf = open('left', 'w')
leftf.write(file1)
leftf.close()

rightf = open('right', 'w')
rightf.write(file2)
rightf.close()

def run_code(name):
ret = run_js(name, stderr=PIPE, full_output=True)
Expand All @@ -53,7 +48,7 @@ def run_code(name):
while True:
mid = int((low + high)/2)
print(low, high, ' current: %d' % mid, end=' ')
open('middle', 'w').write('\n'.join(left_lines[:mid] + right_lines[mid:]))
Path('middle').write_text('\n'.join(left_lines[:mid] + right_lines[mid:]))
shutil.copyfile('middle', 'middle' + str(mid))
result = run_code('middle')
print(result == left_result, result == right_result)#, 'XXX', left_result, 'YYY', result, 'ZZZ', right_result
Expand Down
Loading