diff --git a/.github/workflows/gh-actions.yml b/.github/workflows/gh-actions.yml index aaa4c43fd4..33c16df78a 100644 --- a/.github/workflows/gh-actions.yml +++ b/.github/workflows/gh-actions.yml @@ -17,7 +17,8 @@ jobs: python-version: '3.10' - run: sudo apt update # TODO: update checkers to current versions available in ubuntu 22.04 -# - run: sudo apt install doxygen clang-format-10 cppcheck pylint python-serial +# - run: sudo apt install doxygen clang-format-10 cppcheck python-serial + - run: sudo apt install pylint - run: $RUNNER --check-signed-off=gh-actions if: ${{ always() }} # - run: $RUNNER --check-doxygen @@ -28,8 +29,8 @@ jobs: if: ${{ always() }} # - run: $RUNNER --check-strings # if: ${{ always() }} -# - run: $RUNNER --check-pylint -# if: ${{ always() }} + - run: $RUNNER --check-pylint + if: ${{ always() }} # - run: $RUNNER --check-cppcheck # if: ${{ always() }} diff --git a/jerry-debugger/jerry_client.py b/jerry-debugger/jerry_client.py index cb248c8f94..50526fc6ca 100755 --- a/jerry-debugger/jerry_client.py +++ b/jerry-debugger/jerry_client.py @@ -104,7 +104,7 @@ def do_next(self, args): if res_type == result.END: self.quit = True return - elif res_type == result.TEXT: + if res_type == result.TEXT: write(result.get_text()) elif res_type == result.PROMPT: break @@ -262,7 +262,7 @@ def src_check_args(args): print("Error: Non-negative integer number expected: %s" % (val_errno)) return -1 -# pylint: disable=too-many-branches,too-many-locals,too-many-statements +# pylint: disable=too-many-branches,too-many-locals,too-many-statements,import-outside-toplevel def main(): args = jerry_client_main.arguments_parse() @@ -325,7 +325,7 @@ def main(): if res_type == result.END: break - elif res_type == result.PROMPT: + if res_type == result.PROMPT: prompt.cmdloop() elif res_type == result.TEXT: write(result.get_text()) @@ -339,7 +339,7 @@ def main(): MSG = str(error_msg) if ERRNO == 111: sys.exit("Failed to connect to the JerryScript debugger.") - elif ERRNO == 32 or ERRNO == 104: + elif ERRNO in (32, 104): sys.exit("Connection closed.") else: sys.exit("Failed to connect to the JerryScript debugger.\nError: %s" % (MSG)) diff --git a/jerry-debugger/jerry_client_main.py b/jerry-debugger/jerry_client_main.py index 1ae7e49bd3..e6bcbc4b9d 100644 --- a/jerry-debugger/jerry_client_main.py +++ b/jerry-debugger/jerry_client_main.py @@ -146,7 +146,7 @@ def arguments_parse(): return args -class JerryBreakpoint(object): +class JerryBreakpoint: def __init__(self, line, offset, function): self.line = line @@ -168,7 +168,7 @@ def __repr__(self): return ("Breakpoint(line:%d, offset:%d, active_index:%d)" % (self.line, self.offset, self.active_index)) -class JerryPendingBreakpoint(object): +class JerryPendingBreakpoint: def __init__(self, line=None, source_name=None, function=None): self.function = function self.line = line @@ -185,7 +185,7 @@ def __str__(self): return result -class JerryFunction(object): +class JerryFunction: # pylint: disable=too-many-instance-attributes,too-many-arguments def __init__(self, is_func, byte_code_cp, source, source_name, line, column, name, lines, offsets): self.is_func = bool(is_func) @@ -205,9 +205,9 @@ def __init__(self, is_func, byte_code_cp, source, source_name, line, column, nam for i, _line in enumerate(lines): offset = offsets[i] - breakpoint = JerryBreakpoint(_line, offset, self) - self.lines[_line] = breakpoint - self.offsets[offset] = breakpoint + breakpt = JerryBreakpoint(_line, offset, self) + self.lines[_line] = breakpt + self.offsets[offset] = breakpt def __repr__(self): result = ("Function(byte_code_cp:0x%x, source_name:%r, name:%r, line:%d, column:%d { " @@ -218,7 +218,7 @@ def __repr__(self): return result + " })" -class Multimap(object): +class Multimap: def __init__(self): self.map = {} @@ -246,7 +246,7 @@ def __repr__(self): return "Multimap(%r)" % (self.map) -class DebuggerAction(object): +class DebuggerAction: END = 0 WAIT = 1 TEXT = 2 @@ -263,7 +263,7 @@ def get_text(self): return self.action_text -class JerryDebugger(object): +class JerryDebugger: # pylint: disable=too-many-instance-attributes,too-many-statements,too-many-public-methods,no-self-use def __init__(self, channel): self.prompt = False @@ -378,12 +378,12 @@ def breakpoint_list(self): result = '' if self.active_breakpoint_list: result += "=== %sActive breakpoints %s ===\n" % (self.green_bg, self.nocolor) - for breakpoint in self.active_breakpoint_list.values(): - result += " %d: %s\n" % (breakpoint.active_index, breakpoint) + for breakpt in self.active_breakpoint_list.values(): + result += " %d: %s\n" % (breakpt.active_index, breakpt) if self.pending_breakpoint_list: result += "=== %sPending breakpoints%s ===\n" % (self.yellow_bg, self.nocolor) - for breakpoint in self.pending_breakpoint_list.values(): - result += " %d: %s (pending)\n" % (breakpoint.index, breakpoint) + for breakpt in self.pending_breakpoint_list.values(): + result += " %d: %s (pending)\n" % (breakpt.index, breakpt) if not self.active_breakpoint_list and not self.pending_breakpoint_list: result += "No breakpoints\n" @@ -395,13 +395,13 @@ def delete(self, args): return "Error: Breakpoint index expected\n" \ "Delete the given breakpoint, use 'delete all|active|pending' " \ "to clear all the given breakpoints\n " - elif args in ['all', 'pending', 'active']: + if args in ['all', 'pending', 'active']: if args != "pending": for i in list(self.active_breakpoint_list.values()): - breakpoint = self.active_breakpoint_list[i.active_index] + breakpt = self.active_breakpoint_list[i.active_index] del self.active_breakpoint_list[i.active_index] - breakpoint.active_index = -1 - self._send_breakpoint(breakpoint) + breakpt.active_index = -1 + self._send_breakpoint(breakpt) if args != "active": if self.pending_breakpoint_list: @@ -415,18 +415,17 @@ def delete(self, args): return "Error: Integer number expected, %s\n" % (val_errno) if breakpoint_index in self.active_breakpoint_list: - breakpoint = self.active_breakpoint_list[breakpoint_index] + breakpt = self.active_breakpoint_list[breakpoint_index] del self.active_breakpoint_list[breakpoint_index] - breakpoint.active_index = -1 - self._send_breakpoint(breakpoint) + breakpt.active_index = -1 + self._send_breakpoint(breakpt) return "Breakpoint %d deleted\n" % (breakpoint_index) - elif breakpoint_index in self.pending_breakpoint_list: + if breakpoint_index in self.pending_breakpoint_list: del self.pending_breakpoint_list[breakpoint_index] if not self.pending_breakpoint_list: self._send_parser_config(0) return "Pending breakpoint %d deleted\n" % (breakpoint_index) - else: - return "Error: Breakpoint %d not found\n" % (breakpoint_index) + return "Error: Breakpoint %d not found\n" % (breakpoint_index) def next(self): self.prompt = False @@ -605,22 +604,22 @@ def _send_string(self, args, message_type, index=0): self.channel.send_message(self.byte_order, message + args[prev_offset:offset]) - def _breakpoint_pending_exists(self, breakpoint): + def _breakpoint_pending_exists(self, breakpt): for existing_bp in self.pending_breakpoint_list.values(): - if (breakpoint.line and existing_bp.source_name == breakpoint.source_name and \ - existing_bp.line == breakpoint.line) \ - or (not breakpoint.line and existing_bp.function == breakpoint.function): + if (breakpt.line and existing_bp.source_name == breakpt.source_name and \ + existing_bp.line == breakpt.line) \ + or (not breakpt.line and existing_bp.function == breakpt.function): return True return False - def _send_breakpoint(self, breakpoint): + def _send_breakpoint(self, breakpt): message = struct.pack(self.byte_order + "BBB" + self.cp_format + self.idx_format, 1 + 1 + self.cp_size + 4, JERRY_DEBUGGER_UPDATE_BREAKPOINT, - int(breakpoint.active_index >= 0), - breakpoint.function.byte_code_cp, - breakpoint.offset) + int(breakpt.active_index >= 0), + breakpt.function.byte_code_cp, + breakpt.offset) self.channel.send_message(self.byte_order, message) def _send_bytecode_cp(self, byte_code_cp): @@ -658,7 +657,7 @@ def send_client_source(self): sys.exit("Error: Javascript file expected!") return - with open(path, 'r') as src_file: + with open(path, 'r', encoding='utf8') as src_file: content = path + "\0" + src_file.read() self._send_string(content, JERRY_DEBUGGER_CLIENT_SOURCE) @@ -710,8 +709,8 @@ def process_messages(self): elif buffer_type in [JERRY_DEBUGGER_BREAKPOINT_HIT, JERRY_DEBUGGER_EXCEPTION_HIT]: breakpoint_data = struct.unpack(self.byte_order + self.cp_format + self.idx_format, data[1:]) - breakpoint = self._get_breakpoint(breakpoint_data) - self.last_breakpoint_hit = breakpoint[0] + breakpt = self._get_breakpoint(breakpoint_data) + self.last_breakpoint_hit = breakpt[0] if buffer_type == JERRY_DEBUGGER_EXCEPTION_HIT: result += "Exception throw detected (to disable automatic stop type exception 0)\n" @@ -719,15 +718,15 @@ def process_messages(self): result += "Exception hint: %s\n" % (self.exception_string) self.exception_string = "" - if breakpoint[1]: + if breakpt[1]: breakpoint_info = "at" else: breakpoint_info = "around" - if breakpoint[0].active_index >= 0: - breakpoint_info += " breakpoint:%s%d%s" % (self.red, breakpoint[0].active_index, self.nocolor) + if breakpt[0].active_index >= 0: + breakpoint_info += " breakpoint:%s%d%s" % (self.red, breakpt[0].active_index, self.nocolor) - result += "Stopped %s %s\n" % (breakpoint_info, breakpoint[0]) + result += "Stopped %s %s\n" % (breakpoint_info, breakpt[0]) if self.display > 0: result += self.print_source(self.display, self.src_offset) @@ -751,9 +750,9 @@ def process_messages(self): breakpoint_data = struct.unpack(self.byte_order + self.cp_format + self.idx_format, data[buffer_pos: buffer_pos + self.cp_size + 4]) - breakpoint = self._get_breakpoint(breakpoint_data) + breakpt = self._get_breakpoint(breakpoint_data) - result += "Frame %d: %s\n" % (frame_index, breakpoint[0]) + result += "Frame %d: %s\n" % (frame_index, breakpt[0]) frame_index += 1 buffer_pos += self.cp_size + 4 @@ -883,7 +882,7 @@ def _parse_source(self, data): logging.error("Syntax error found") return "" - elif buffer_type in [JERRY_DEBUGGER_SOURCE_CODE, JERRY_DEBUGGER_SOURCE_CODE_END]: + if buffer_type in [JERRY_DEBUGGER_SOURCE_CODE, JERRY_DEBUGGER_SOURCE_CODE_END]: source_code += data[1:] elif buffer_type in [JERRY_DEBUGGER_SOURCE_CODE_NAME, JERRY_DEBUGGER_SOURCE_CODE_NAME_END]: @@ -977,33 +976,33 @@ def _parse_source(self, data): self.function_list.update(new_function_list) for function in new_function_list.values(): - for line, breakpoint in function.lines.items(): - self.line_list.insert(line, breakpoint) + for line, breakpt in function.lines.items(): + self.line_list.insert(line, breakpt) # Try to set the pending breakpoints if self.pending_breakpoint_list: logging.debug("Pending breakpoints available") bp_list = self.pending_breakpoint_list - for breakpoint_index, breakpoint in list(bp_list.items()): + for breakpoint_index, breakpt in list(bp_list.items()): source_lines = 0 for src in new_function_list.values(): - if (src.source_name == breakpoint.source_name or - src.source_name.endswith("/" + breakpoint.source_name) or - src.source_name.endswith("\\" + breakpoint.source_name)): + if (src.source_name == breakpt.source_name or + src.source_name.endswith("/" + breakpt.source_name) or + src.source_name.endswith("\\" + breakpt.source_name)): source_lines = len(src.source) break - if breakpoint.line: - if breakpoint.line <= source_lines: - command = breakpoint.source_name + ":" + str(breakpoint.line) + if breakpt.line: + if breakpt.line <= source_lines: + command = breakpt.source_name + ":" + str(breakpt.line) set_result = self._set_breakpoint(command, True) if set_result: result += set_result del bp_list[breakpoint_index] - elif breakpoint.function: - command = breakpoint.function + elif breakpt.function: + command = breakpt.function set_result = self._set_breakpoint(command, True) if set_result: @@ -1024,39 +1023,39 @@ def _release_function(self, data): function = self.function_list[byte_code_cp] - for line, breakpoint in function.lines.items(): - self.line_list.delete(line, breakpoint) - if breakpoint.active_index >= 0: - del self.active_breakpoint_list[breakpoint.active_index] + for line, breakpt in function.lines.items(): + self.line_list.delete(line, breakpt) + if breakpt.active_index >= 0: + del self.active_breakpoint_list[breakpt.active_index] del self.function_list[byte_code_cp] self._send_bytecode_cp(byte_code_cp) logging.debug("Function {0x%x} byte-code released", byte_code_cp) - def _enable_breakpoint(self, breakpoint): - if isinstance(breakpoint, JerryPendingBreakpoint): - if self._breakpoint_pending_exists(breakpoint): + def _enable_breakpoint(self, breakpt): + if isinstance(breakpt, JerryPendingBreakpoint): + if self._breakpoint_pending_exists(breakpt): return "%sPending breakpoint%s already exists\n" % (self.yellow, self.nocolor) self.next_breakpoint_index += 1 - breakpoint.index = self.next_breakpoint_index - self.pending_breakpoint_list[self.next_breakpoint_index] = breakpoint + breakpt.index = self.next_breakpoint_index + self.pending_breakpoint_list[self.next_breakpoint_index] = breakpt return ("%sPending breakpoint %d%s at %s\n" % (self.yellow, - breakpoint.index, + breakpt.index, self.nocolor, - breakpoint)) + breakpt)) - if breakpoint.active_index < 0: + if breakpt.active_index < 0: self.next_breakpoint_index += 1 - self.active_breakpoint_list[self.next_breakpoint_index] = breakpoint - breakpoint.active_index = self.next_breakpoint_index - self._send_breakpoint(breakpoint) + self.active_breakpoint_list[self.next_breakpoint_index] = breakpt + breakpt.active_index = self.next_breakpoint_index + self._send_breakpoint(breakpt) return "%sBreakpoint %d%s at %s\n" % (self.green, - breakpoint.active_index, + breakpt.active_index, self.nocolor, - breakpoint) + breakpt) def _set_breakpoint(self, string, pending): @@ -1067,13 +1066,13 @@ def _set_breakpoint(self, string, pending): source_name = line.group(1) new_line = int(line.group(2)) - for breakpoint in self.line_list.get(new_line): - func_source = breakpoint.function.source_name + for breakpt in self.line_list.get(new_line): + func_source = breakpt.function.source_name if (source_name == func_source or func_source.endswith("/" + source_name) or func_source.endswith("\\" + source_name)): - result += self._enable_breakpoint(breakpoint) + result += self._enable_breakpoint(breakpt) else: functions_to_enable = [] @@ -1096,10 +1095,10 @@ def _set_breakpoint(self, string, pending): self._send_parser_config(1) if line: - breakpoint = JerryPendingBreakpoint(int(line.group(2)), line.group(1)) + breakpt = JerryPendingBreakpoint(int(line.group(2)), line.group(1)) else: - breakpoint = JerryPendingBreakpoint(function=string) - result += self._enable_breakpoint(breakpoint) + breakpt = JerryPendingBreakpoint(function=string) + result += self._enable_breakpoint(breakpt) return result @@ -1117,7 +1116,7 @@ def _get_breakpoint(self, breakpoint_data): nearest_offset = -1 for current_offset in function.offsets: - if current_offset <= offset and current_offset > nearest_offset: + if offset >= current_offset > nearest_offset: nearest_offset = current_offset return (function.offsets[nearest_offset], False) @@ -1131,8 +1130,7 @@ def _process_incoming_text(self, buffer_type, data): subtype = data[-1] message += data[1:-1].decode('utf8') break - else: - message += data[1:].decode('utf8') + message += data[1:].decode('utf8') data = self.channel.get_message(True) buffer_type = data[0] diff --git a/jerry-debugger/jerry_client_rawpacket.py b/jerry-debugger/jerry_client_rawpacket.py index aabad6c64f..945a57ad0e 100644 --- a/jerry-debugger/jerry_client_rawpacket.py +++ b/jerry-debugger/jerry_client_rawpacket.py @@ -18,7 +18,7 @@ MAX_BUFFER_SIZE = 256 -class RawPacket(object): +class RawPacket: """ Simplified transmission layer. """ def __init__(self, protocol): self.protocol = protocol diff --git a/jerry-debugger/jerry_client_serial.py b/jerry-debugger/jerry_client_serial.py index 9e77e0da0d..1853ed89c0 100644 --- a/jerry-debugger/jerry_client_serial.py +++ b/jerry-debugger/jerry_client_serial.py @@ -17,7 +17,7 @@ import select import serial -class Serial(object): +class Serial: """ Create a new socket using the given address family, socket type and protocol number. """ def __init__(self, serial_config): config = serial_config.split(',') diff --git a/jerry-debugger/jerry_client_tcp.py b/jerry-debugger/jerry_client_tcp.py index c241bfe920..72f419f8f1 100644 --- a/jerry-debugger/jerry_client_tcp.py +++ b/jerry-debugger/jerry_client_tcp.py @@ -18,7 +18,7 @@ import select # pylint: disable=too-many-arguments,superfluous-parens -class Socket(object): +class Socket: """ Create a new socket using the given address family, socket type and protocol number. """ def __init__(self, address, socket_family=socket.AF_INET, socket_type=socket.SOCK_STREAM, proto=0, fileno=None): self.address = address diff --git a/jerry-debugger/jerry_client_websocket.py b/jerry-debugger/jerry_client_websocket.py index 80f3c3e9c8..24569b46c8 100644 --- a/jerry-debugger/jerry_client_websocket.py +++ b/jerry-debugger/jerry_client_websocket.py @@ -20,7 +20,7 @@ WEBSOCKET_BINARY_FRAME = 2 WEBSOCKET_FIN_BIT = 0x80 -class WebSocket(object): +class WebSocket: def __init__(self, protocol): self.data_buffer = b"" diff --git a/tools/amalgam.py b/tools/amalgam.py index 5fcdfe6590..e0cc64a9c8 100755 --- a/tools/amalgam.py +++ b/tools/amalgam.py @@ -30,7 +30,7 @@ JERRY_MATH = os.path.join(ROOT_DIR, 'jerry-math') -class Amalgamator(object): +class Amalgamator: # pylint: disable=too-many-instance-attributes _RE_INCLUDE = re.compile(r'\s*#include ("|<)(.*?)("|>)\n$') @@ -92,7 +92,7 @@ def add_file(self, filename, file_level=0): self._emit_lineinfo(1, filename) line_idx = 0 - with open(filename, 'r') as input_file: + with open(filename, 'r', encoding='utf8') as input_file: in_copyright = False for line in input_file: line_idx += 1 @@ -242,7 +242,7 @@ def amalgamate(base_dir, input_files=(), output_file=None, for fname in sorted(c_files.values(), reverse=True): amalgam.add_file(fname) - with open(output_file, 'w') as output: + with open(output_file, 'w', encoding='utf8') as output: amalgam.write_output(output) diff --git a/tools/build.py b/tools/build.py index 1a80b8dfbc..b4daccf787 100755 --- a/tools/build.py +++ b/tools/build.py @@ -272,10 +272,8 @@ def make_jerry(arguments): env = dict(os.environ) env['CMAKE_BUILD_PARALLEL_LEVEL'] = str(arguments.jobs) env['MAKEFLAGS'] = '-j%d' % (arguments.jobs) # Workaround for CMake < 3.12 - proc = subprocess.Popen(make_cmd, env=env) - proc.wait() - - return proc.returncode + with subprocess.Popen(make_cmd, env=env) as proc: + return proc.returncode def install_jerry(arguments): install_target = 'INSTALL' if os.path.exists(os.path.join(arguments.builddir, 'Jerry.sln')) else 'install' diff --git a/tools/check-format.py b/tools/check-format.py index eaf5d7fe2c..e042eabf60 100755 --- a/tools/check-format.py +++ b/tools/check-format.py @@ -65,14 +65,13 @@ def check_clang_format(args, source_file_name): cmd.append(source_file_name) - proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - _, error = proc.communicate() + with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: + _, error = proc.communicate() - if proc.returncode == 0: - return 0 + if proc.returncode == 0: + return 0 - print(error.decode('utf8')) + print(error.decode('utf8')) return 1 @@ -115,22 +114,21 @@ def main(args): CLANG_FORMAT_MIN_VERSION) return 1 - pool = multiprocessing.Pool() - failed = 0 - - for folder in FOLDERS: - # pylint: disable=unexpected-keyword-arg - files = sum(([glob(path.join(PROJECT_DIR, folder, "**/*.%s" % e), recursive=True) - for e in ['c', 'h']]), []) + with multiprocessing.Pool() as pool: + failed = 0 - failed += run_pass(pool, check_clang_format, - [(args, sourece_file) for sourece_file in files]) - failed += run_pass(pool, check_comments, - [([RE_DIRECTIVE_COMMENT, RE_FUNCTION_NAME_COMMENT], sourece_file) for sourece_file in files]) + for folder in FOLDERS: + # pylint: disable=unexpected-keyword-arg + files = sum(([glob(path.join(PROJECT_DIR, folder, "**/*.%s" % e), recursive=True) + for e in ['c', 'h']]), []) - pool.close() + failed += run_pass(pool, check_clang_format, + [(args, sourece_file) for sourece_file in files]) + failed += run_pass(pool, check_comments, + [([RE_DIRECTIVE_COMMENT, RE_FUNCTION_NAME_COMMENT], sourece_file) for sourece_file in + files]) - return 1 if failed else 0 + return 1 if failed else 0 if __name__ == "__main__": diff --git a/tools/check-license.py b/tools/check-license.py index b3a2825d3b..47feb9395a 100755 --- a/tools/check-license.py +++ b/tools/check-license.py @@ -78,7 +78,7 @@ def main(): for fname in files: if any(fname.endswith(ext) for ext in EXTENSIONS): fpath = os.path.join(root, fname) - with io.open(fpath, 'r', errors='ignore') as curr_file: + with io.open(fpath, 'r', errors='ignore', encoding='utf8') as curr_file: if not LICENSE.search(curr_file.read()): print('%s: incorrect license' % fpath) is_ok = False diff --git a/tools/gen-doctest.py b/tools/gen-doctest.py index 02052736ab..a57014ef45 100755 --- a/tools/gen-doctest.py +++ b/tools/gen-doctest.py @@ -24,7 +24,7 @@ import sys -class DoctestExtractor(object): +class DoctestExtractor: """ An extractor to process Markdown files and find doctests inside. """ @@ -101,7 +101,7 @@ def _process_code_end(self, decl, code): if self._dry: print('%s %s' % (action, outname)) else: - with open(outname, 'w') as outfile: + with open(outname, 'w', encoding='utf8') as outfile: outfile.writelines(code) def process(self, infile): diff --git a/tools/gen-strings.py b/tools/gen-strings.py index dde642c02b..cf1d36c814 100755 --- a/tools/gen-strings.py +++ b/tools/gen-strings.py @@ -23,10 +23,11 @@ import argparse import fileinput -import subprocess import json import os import re +import subprocess +import sys from settings import FORMAT_SCRIPT, PROJECT_DIR @@ -75,7 +76,7 @@ def read_magic_string_defs(debug, ini_path, item_name): for str_ref, str_value in [x for x in defs if len(x[1]) > LIMIT_MAGIC_STR_LENGTH]: print("error: The maximum allowed magic string size is {limit} but {str_ref} is {str_len} long.".format( limit=LIMIT_MAGIC_STR_LENGTH, str_ref=str_ref, str_len=len(str_value))) - exit(1) + sys.exit(1) if debug: print('debug: magic string definitions: {dump}' @@ -294,7 +295,7 @@ def generate_magic_strings(args, ini_path, item_name, pattern, inc_h_path, def_m extended_defs = calculate_magic_string_guards(defs, uses, debug=args.debug) - with open(inc_h_path, 'w') as gen_file: + with open(inc_h_path, 'w', encoding='utf8') as gen_file: generate_header(gen_file, ini_path) generate_magic_string_defs(gen_file, extended_defs, def_macro) if with_size_macro: diff --git a/tools/gen-unicode.py b/tools/gen-unicode.py index 773cd61ab6..675a1c041b 100755 --- a/tools/gen-unicode.py +++ b/tools/gen-unicode.py @@ -45,7 +45,7 @@ # common code generation -class UnicodeBasicSource(object): +class UnicodeBasicSource: # pylint: disable=too-many-instance-attributes def __init__(self, filepath, character_type="uint16_t", length_type="uint8_t"): self._filepath = filepath @@ -114,7 +114,7 @@ def add_table(self, table, description, table_type, category, table_name): self._data.append("") # for an extra empty line def generate(self): - with open(self._filepath, 'w') as generated_source: + with open(self._filepath, 'w', encoding='utf8') as generated_source: generated_source.write("\n".join(self._header)) generated_source.write("\n".join(self._data)) @@ -127,14 +127,14 @@ def __init__(self, filepath): def add_whitepace_range(self, category, categorizer, units): self.add_range(category, categorizer.create_tables(units)) -class UnicodeBasicCategorizer(object): +class UnicodeBasicCategorizer: def __init__(self): self._length_limit = 0xff self.extra_id_continue_units = set([0x200C, 0x200D]) #pylint: disable=no-self-use def in_range(self, i): - return i >= 0x80 and i < 0x10000 + return 0x80 <= i < 0x10000 def _group_ranges(self, units): """ @@ -194,7 +194,7 @@ def read_units(self, file_path, categories, subcategories=None): # .. ; # matcher = r"(?P[\dA-F]+)(?:\.\.(?P[\dA-F]+))?\s+; (?P[\w]+) # (?P[\w&]{2})" - with open(file_path, "r") as src_file: + with open(file_path, "r", encoding='utf8') as src_file: for line in src_file: match = re.match(matcher, line) @@ -227,7 +227,7 @@ def read_case_mappings(self, unicode_data_file, special_casing_file): upper_case_mapping = {} # Add one-to-one mappings - with open(unicode_data_file) as unicode_data: + with open(unicode_data_file, encoding='utf8') as unicode_data: reader = csv.reader(unicode_data, delimiter=';') for line in reader: @@ -246,7 +246,7 @@ def read_case_mappings(self, unicode_data_file, special_casing_file): lower_case_mapping[letter_id] = parse_unicode_sequence(small_letter) # Update the conversion tables with the special cases - with open(special_casing_file) as special_casing: + with open(special_casing_file, encoding='utf8') as special_casing: reader = csv.reader(special_casing, delimiter=';') for line in reader: @@ -740,7 +740,7 @@ def generate_folding(script_args, plane_type): folding = {} - with open(case_folding_path, 'r') as case_folding: + with open(case_folding_path, 'r', encoding='utf8') as case_folding: case_folding_re = re.compile(r'(?P[^;]*);\s*(?P[^;]*);\s*(?P[^;]*);') for line in case_folding: match = case_folding_re.match(line) diff --git a/tools/js2c.py b/tools/js2c.py index 63733bc6b0..957d09984f 100755 --- a/tools/js2c.py +++ b/tools/js2c.py @@ -57,7 +57,7 @@ def reduce_code(code): def js_to_native_code(path, name, build_type): - with open(path, 'r') as js_source: + with open(path, 'r', encoding='utf8') as js_source: code = js_source.read() if build_type != 'debug': @@ -118,7 +118,7 @@ def main(): gen_output.append("\n".join(gen_structs)) gen_output.append(FOOTER) - with open(os.path.join(script_args.output_path, 'jerry-targetjs.h'), 'w') as gen_file: + with open(os.path.join(script_args.output_path, 'jerry-targetjs.h'), 'w', encoding='utf8') as gen_file: gen_file.write("\n".join(gen_output)) diff --git a/tools/pylint/pylintrc b/tools/pylint/pylintrc index 76fd12e7dc..5b85fdc15e 100644 --- a/tools/pylint/pylintrc +++ b/tools/pylint/pylintrc @@ -59,7 +59,7 @@ confidence= # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use"--disable=all --enable=classes # --disable=W" -disable=import-star-module-level,old-octal-literal,oct-method,unpacking-in-except,parameter-unpacking,backtick,old-raise-syntax,old-ne-operator,long-suffix,dict-view-method,dict-iter-method,metaclass-assignment,next-method-called,raising-string,indexing-exception,raw_input-builtin,long-builtin,file-builtin,execfile-builtin,coerce-builtin,cmp-builtin,buffer-builtin,basestring-builtin,apply-builtin,filter-builtin-not-iterating,using-cmp-argument,useless-suppression,range-builtin-not-iterating,suppressed-message,no-absolute-import,old-division,cmp-method,reload-builtin,zip-builtin-not-iterating,intern-builtin,unichr-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,input-builtin,round-builtin,hex-method,nonzero-method,map-builtin-not-iterating,missing-docstring,locally-disabled +disable=import-star-module-level,old-octal-literal,oct-method,unpacking-in-except,parameter-unpacking,backtick,old-raise-syntax,old-ne-operator,long-suffix,dict-view-method,dict-iter-method,metaclass-assignment,next-method-called,raising-string,indexing-exception,raw_input-builtin,long-builtin,file-builtin,execfile-builtin,coerce-builtin,cmp-builtin,buffer-builtin,basestring-builtin,apply-builtin,filter-builtin-not-iterating,using-cmp-argument,useless-suppression,range-builtin-not-iterating,suppressed-message,no-absolute-import,old-division,cmp-method,reload-builtin,zip-builtin-not-iterating,intern-builtin,unichr-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,input-builtin,round-builtin,hex-method,nonzero-method,map-builtin-not-iterating,missing-docstring,locally-disabled,consider-using-f-string, [REPORTS] diff --git a/tools/run-tests.py b/tools/run-tests.py index 92550cbf93..f81c87ba83 100755 --- a/tools/run-tests.py +++ b/tools/run-tests.py @@ -26,7 +26,7 @@ import settings if sys.version_info.major >= 3: - import runners.util as util # pylint: disable=import-error + from runners import util else: sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '/runners') import util @@ -284,8 +284,7 @@ def iterate_test_runner_jobs(jobs, options): if build_dir_path in tested_paths: sys.stderr.write('(skipping: already tested with %s)\n' % build_dir_path) continue - else: - tested_paths.add(build_dir_path) + tested_paths.add(build_dir_path) bin_path = get_binary_path(build_dir_path) bin_hash = hash_binary(bin_path) @@ -293,8 +292,7 @@ def iterate_test_runner_jobs(jobs, options): if bin_hash in tested_hashes: sys.stderr.write('(skipping: already tested with equivalent %s)\n' % tested_hashes[bin_hash]) continue - else: - tested_hashes[bin_hash] = build_dir_path + tested_hashes[bin_hash] = build_dir_path test_cmd = util.get_python_cmd_prefix() test_cmd.extend([settings.TEST_RUNNER_SCRIPT, '--engine', bin_path]) @@ -309,9 +307,9 @@ def run_check(runnable, env=None): full_env.update(env) env = full_env - proc = subprocess.Popen(runnable, env=env) - proc.wait() - return proc.returncode + with subprocess.Popen(runnable, env=env) as proc: + proc.wait() + return proc.returncode def run_jerry_debugger_tests(options): ret_build = ret_test = 0 @@ -356,7 +354,7 @@ def run_jerry_tests(options): skip_list = [] if job.name == 'jerry_tests-snapshot': - with open(settings.SNAPSHOT_TESTS_SKIPLIST, 'r') as snapshot_skip_list: + with open(settings.SNAPSHOT_TESTS_SKIPLIST, 'r', encoding='utf8') as snapshot_skip_list: for line in snapshot_skip_list: skip_list.append(line.rstrip()) diff --git a/tools/runners/run-test-suite-test262.py b/tools/runners/run-test-suite-test262.py index 763e07f93e..524a103d39 100755 --- a/tools/runners/run-test-suite-test262.py +++ b/tools/runners/run-test-suite-test262.py @@ -78,7 +78,7 @@ def update_exclude_list(args): passing_tests = set() failing_tests = set() new_passing_tests = set() - with open(os.path.join(os.path.dirname(args.engine), 'test262.report'), 'r') as report_file: + with open(os.path.join(os.path.dirname(args.engine), 'test262.report'), 'r', encoding='utf8') as report_file: for line in report_file: match = re.match('(=== )?(.*) (?:failed|passed) in (?:non-strict|strict)', line) if match: @@ -92,7 +92,7 @@ def update_exclude_list(args): # Tests pass in strict-mode but fail in non-strict-mode (or vice versa) should be considered as failures passing_tests = passing_tests - failing_tests - with open(args.excludelist_path, 'r+') as exclude_file: + with open(args.excludelist_path, 'r+', encoding='utf8') as exclude_file: lines = exclude_file.readlines() exclude_file.seek(0) exclude_file.truncate() @@ -167,49 +167,46 @@ def main(args): if args.test262_test_list: test262_command.extend(args.test262_test_list.split(',')) - proc = subprocess.Popen(test262_command, - universal_newlines=True, - stdout=subprocess.PIPE, - **kwargs) - - return_code = 1 - with open(os.path.join(os.path.dirname(args.engine), 'test262.report'), 'w') as output_file: - counter = 0 - summary_found = False - summary_end_found = False - while True: - output = proc.stdout.readline() - if not output: - break - output_file.write(output) - - if output.startswith('=== Summary ==='): - summary_found = True - print('') - - if summary_found: - if not summary_end_found: - print(output, end='') - if not output.strip(): - summary_end_found = True - if 'All tests succeeded' in output: - return_code = 0 - elif re.search('in (non-)?strict mode', output): - counter += 1 - if (counter % 100) == 0: - print(".", end='') - if (counter % 5000) == 0: - print(" Executed %d tests." % counter) - - proc.wait() + with subprocess.Popen(test262_command, universal_newlines=True, stdout=subprocess.PIPE, **kwargs) as proc: + + return_code = 1 + with open(os.path.join(os.path.dirname(args.engine), 'test262.report'), 'w', encoding='utf8') as output_file: + counter = 0 + summary_found = False + summary_end_found = False + while True: + output = proc.stdout.readline() + if not output: + break + output_file.write(output) + + if output.startswith('=== Summary ==='): + summary_found = True + print('') + + if summary_found: + if not summary_end_found: + print(output, end='') + if not output.strip(): + summary_end_found = True + if 'All tests succeeded' in output: + return_code = 0 + elif re.search('in (non-)?strict mode', output): + counter += 1 + if (counter % 100) == 0: + print(".", end='') + if (counter % 5000) == 0: + print(" Executed %d tests." % counter) + + proc.wait() + + if sys.platform == 'win32': + util.set_timezone(original_timezone) + + if args.mode == 'update': + return_code = update_exclude_list(args) - if sys.platform == 'win32': - util.set_timezone(original_timezone) - - if args.mode == 'update': - return_code = update_exclude_list(args) - - return return_code + return return_code if __name__ == "__main__": diff --git a/tools/runners/run-test-suite.py b/tools/runners/run-test-suite.py index 04ea68a828..a8b782e5b0 100755 --- a/tools/runners/run-test-suite.py +++ b/tools/runners/run-test-suite.py @@ -22,7 +22,7 @@ import util -def get_arguments(): +def get_args(): execution_runtime = os.environ.get('RUNTIME') parser = argparse.ArgumentParser() parser.add_argument('-q', '--quiet', action='store_true', @@ -60,7 +60,7 @@ def get_tests(test_dir, test_list, skip_list): if test_list: dirname = os.path.dirname(test_list) - with open(test_list, "r") as test_list_fd: + with open(test_list, "r", encoding='utf8') as test_list_fd: for test in test_list_fd: tests.append(os.path.normpath(os.path.join(dirname, test.rstrip()))) @@ -79,10 +79,10 @@ def execute_test_command(test_cmd): kwargs = {} if sys.version_info.major >= 3: kwargs['encoding'] = 'unicode_escape' - process = subprocess.Popen(test_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - universal_newlines=True, **kwargs) - stdout = process.communicate()[0] - return (process.returncode, stdout) + with subprocess.Popen(test_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + universal_newlines=True, **kwargs) as process: + stdout = process.communicate()[0] + return (process.returncode, stdout) def main(args): @@ -212,4 +212,4 @@ def run_snapshot_tests(args, tests): if __name__ == "__main__": - sys.exit(main(get_arguments())) + sys.exit(main(get_args())) diff --git a/tools/runners/test262-harness.py b/tools/runners/test262-harness.py index f29c7266f1..d332bf1ad2 100755 --- a/tools/runners/test262-harness.py +++ b/tools/runners/test262-harness.py @@ -43,7 +43,7 @@ from __future__ import print_function import logging -import optparse +import argparse import os from os import path import platform @@ -112,7 +112,7 @@ def my_read_dict(lines, indent=""): def my_read_value(lines, value, indent): - if value == ">" or value == "|": + if value in [">", "|"]: (lines, value) = my_multiline(lines, value == "|") value = value + "\n" return (lines, value) @@ -157,7 +157,7 @@ def my_remove_list_header(indent, line): def my_read_one_line(value): if M_YAML_LIST_PATTERN.match(value): return my_flow_list(value) - elif re.match(r"^[-0-9]*$", value): + if re.match(r"^[-0-9]*$", value): try: value = int(value) except ValueError: @@ -196,7 +196,7 @@ def my_multiline(lines, preserve_newlines=False): break else: if preserve_newlines: - if was_empty != None: + if was_empty is not None: value += "\n" else: if was_empty: @@ -339,36 +339,37 @@ def report_error(error_string): def build_options(): - result = optparse.OptionParser() - result.add_option("--command", default=None, - help="The command-line to run") - result.add_option("--tests", default=path.abspath('.'), - help="Path to the tests") - result.add_option("--exclude-list", default=None, - help="Path to the excludelist.xml file") - result.add_option("--cat", default=False, action="store_true", - help="Print packaged test code that would be run") - result.add_option("--summary", default=False, action="store_true", - help="Print summary after running tests") - result.add_option("--full-summary", default=False, action="store_true", - help="Print summary and test output after running tests") - result.add_option("--strict_only", default=False, action="store_true", - help="Test only strict mode") - result.add_option("--non_strict_only", default=False, action="store_true", - help="Test only non-strict mode") - result.add_option("--unmarked_default", default="both", - help="default mode for tests of unspecified strictness") - result.add_option("-j", "--job-count", default=None, action="store", type=int, - help="Number of parallel test jobs to run. In case of '0' cpu count is used.") - result.add_option("--logname", help="Filename to save stdout to") - result.add_option("--loglevel", default="warning", - help="sets log level to debug, info, warning, error, or critical") - result.add_option("--print-handle", default="print", - help="Command to print from console") - result.add_option("--list-includes", default=False, action="store_true", - help="List includes required by tests") - result.add_option("--module-flag", default="-m", - help="List includes required by tests") + result = argparse.ArgumentParser() + result.add_argument("--command", default=None, + help="The command-line to run") + result.add_argument("--tests", default=path.abspath('.'), + help="Path to the tests") + result.add_argument("--exclude-list", default=None, + help="Path to the excludelist.xml file") + result.add_argument("--cat", default=False, action="store_true", + help="Print packaged test code that would be run") + result.add_argument("--summary", default=False, action="store_true", + help="Print summary after running tests") + result.add_argument("--full-summary", default=False, action="store_true", + help="Print summary and test output after running tests") + result.add_argument("--strict_only", default=False, action="store_true", + help="Test only strict mode") + result.add_argument("--non_strict_only", default=False, action="store_true", + help="Test only non-strict mode") + result.add_argument("--unmarked_default", default="both", + help="default mode for tests of unspecified strictness") + result.add_argument("-j", "--job-count", default=None, action="store", type=int, + help="Number of parallel test jobs to run. In case of '0' cpu count is used.") + result.add_argument("--logname", help="Filename to save stdout to") + result.add_argument("--loglevel", default="warning", + help="sets log level to debug, info, warning, error, or critical") + result.add_argument("--print-handle", default="print", + help="Command to print from console") + result.add_argument("--list-includes", default=False, action="store_true", + help="List includes required by tests") + result.add_argument("--module-flag", default="-m", + help="List includes required by tests") + result.add_argument("test_list", nargs='*', default=None) return result @@ -381,10 +382,10 @@ def validate_options(options): def is_windows(): actual_platform = platform.system() - return (actual_platform == 'Windows') or (actual_platform == 'Microsoft') + return actual_platform in ('Windows', 'Microsoft') -class TempFile(object): +class TempFile: def __init__(self, suffix="", prefix="tmp", text=False): self.suffix = suffix @@ -405,7 +406,7 @@ def write(self, string): os.write(self.file_desc, string.encode('utf8')) def read(self): - with open(self.name, "r", newline='') as file_desc: + with open(self.name, "r", newline='', encoding='utf8') as file_desc: return file_desc.read() def close(self): @@ -417,11 +418,11 @@ def dispose(self): try: self.close() os.unlink(self.name) - except OSError as exception: - logging.error("Error disposing temp file: %s", str(exception)) + except OSError as os_error: + logging.error("Error disposing temp file: %s", str(os_error)) -class TestResult(object): +class TestResult: def __init__(self, exit_code, stdout, stderr, case): self.exit_code = exit_code @@ -433,8 +434,8 @@ def report_outcome(self, long_format): name = self.case.get_name() mode = self.case.get_mode() - if self.exit_code != 0 and self.exit_code != 1: - sys.stderr.write(u"===%s failed in %s with negative:%s===\n" + if self.exit_code not in (0, 1): + sys.stderr.write("===%s failed in %s with negative:%s===\n" % (name, mode, self.case.get_negative_type())) self.write_output(sys.stderr) @@ -486,14 +487,14 @@ def get_error_output(self): return self.stdout -class TestCase(object): +class TestCase: def __init__(self, suite, name, full_path, strict_mode, command_template, module_flag): self.suite = suite self.name = name self.full_path = full_path self.strict_mode = strict_mode - with open(self.full_path, "r", newline='') as file_desc: + with open(self.full_path, "r", newline='', encoding='utf8') as file_desc: self.contents = file_desc.read() test_record = parse_test_record(self.contents, name) self.test = test_record["test"] @@ -609,18 +610,18 @@ def execute(command): stderr = TempFile(prefix="test262-err-") try: logging.info("exec: %s", str(args)) - process = subprocess.Popen( + with subprocess.Popen( args, shell=False, stdout=stdout.file_desc, stderr=stderr.file_desc - ) - timer = threading.Timer(TEST262_CASE_TIMEOUT, process.kill) - timer.start() - code = process.wait() - timer.cancel() - out = stdout.read() - err = stderr.read() + ) as process: + timer = threading.Timer(TEST262_CASE_TIMEOUT, process.kill) + timer.start() + code = process.wait() + timer.cancel() + out = stdout.read() + err = stderr.read() finally: stdout.dispose() stderr.dispose() @@ -666,10 +667,10 @@ def validate(self): if 'raw' in flags: if 'noStrict' in flags: raise TypeError("The `raw` flag implies the `noStrict` flag") - elif 'onlyStrict' in flags: + if 'onlyStrict' in flags: raise TypeError( "The `raw` flag is incompatible with the `onlyStrict` flag") - elif self.get_include_list(): + if self.get_include_list(): raise TypeError( "The `raw` flag is incompatible with the `includes` tag") @@ -683,7 +684,7 @@ def test_case_run_process(case): return case.run() -class ProgressIndicator(object): +class ProgressIndicator: def __init__(self, count): self.count = count @@ -711,7 +712,7 @@ def percent_format(partial, total): ((100.0 * partial)/total,)) -class TestSuite(object): +class TestSuite: def __init__(self, options): self.test_root = path.join(options.tests, 'test') @@ -760,7 +761,7 @@ def get_include(self, name): if not name in self.include_cache: static = path.join(self.lib_root, name) if path.exists(static): - with open(static) as file_desc: + with open(static, encoding='utf8') as file_desc: contents = file_desc.read() contents = re.sub(r'\r\n', '\n', contents) self.include_cache[name] = contents + "\n" @@ -849,7 +850,7 @@ def run(self, command_template, tests, print_summary, full_summary, logname, job report_error("No tests to run") progress = ProgressIndicator(len(cases)) if logname: - self.logf = open(logname, "w") + self.logf = open(logname, "w", encoding='utf8') # pylint: disable=consider-using-with if job_count == 1: for case in cases: @@ -861,15 +862,15 @@ def run(self, command_template, tests, print_summary, full_summary, logname, job if job_count == 0: job_count = None # uses multiprocessing.cpu_count() - pool = multiprocessing.Pool(processes=job_count, initializer=pool_init) - try: - for result in pool.imap(test_case_run_process, cases): - if logname: - self.write_log(result) - progress.has_run(result) - except KeyboardInterrupt: - pool.terminate() - pool.join() + with multiprocessing.Pool(processes=job_count, initializer=pool_init) as pool: + try: + for result in pool.imap(test_case_run_process, cases): + if logname: + self.write_log(result) + progress.has_run(result) + except KeyboardInterrupt: + pool.terminate() + pool.join() if print_summary: self.print_summary(progress, logname) @@ -917,7 +918,8 @@ def list_includes(self, tests): def main(): code = 0 parser = build_options() - (options, args) = parser.parse_args() + options = parser.parse_args() + args = options.test_list validate_options(options) test_suite = TestSuite(options) diff --git a/tools/version.py b/tools/version.py index 0b3a1fd82d..8bd542cb3e 100755 --- a/tools/version.py +++ b/tools/version.py @@ -32,7 +32,8 @@ def main(): ) _ = parser.parse_args() - with open(os.path.join(settings.PROJECT_DIR, 'jerry-core', 'include', 'jerryscript.h'), 'r') as header: + with open(os.path.join(settings.PROJECT_DIR, 'jerry-core', 'include', 'jerryscript.h'), 'r', + encoding='utf8') as header: version = {} version_re = re.compile(r'\s*#define\s+JERRY_API_(?PMAJOR|MINOR|PATCH)_VERSION\s+(?P\S+)') for line in header: