diff --git a/jerry-debugger/jerry_client_main.py b/jerry-debugger/jerry_client_main.py index e65d0e1490..ee3ffd269e 100644 --- a/jerry-debugger/jerry_client_main.py +++ b/jerry-debugger/jerry_client_main.py @@ -149,6 +149,15 @@ def arguments_parse(): return args +if sys.version_info.major >= 3: + def safe_ord(c): + if isinstance(c, int): + return c + return ord(c) +else: + safe_ord = ord + + class JerryBreakpoint(object): def __init__(self, line, offset, function): @@ -307,12 +316,12 @@ def __init__(self, channel): # cpointer_size [1] result = self.channel.connect(config_size) - if len(result) != config_size or ord(result[0]) != JERRY_DEBUGGER_CONFIGURATION: + if len(result) != config_size or safe_ord(result[0]) != JERRY_DEBUGGER_CONFIGURATION: raise Exception("Unexpected configuration") - self.little_endian = ord(result[1]) & JERRY_DEBUGGER_LITTLE_ENDIAN - self.max_message_size = ord(result[6]) - self.cp_size = ord(result[7]) + self.little_endian = safe_ord(result[1]) & JERRY_DEBUGGER_LITTLE_ENDIAN + self.max_message_size = safe_ord(result[6]) + self.cp_size = safe_ord(result[7]) if self.little_endian: self.byte_order = "<" @@ -400,7 +409,7 @@ def delete(self, args): "to clear all the given breakpoints\n " elif args in ['all', 'pending', 'active']: if args != "pending": - for i in self.active_breakpoint_list.values(): + for i in list(self.active_breakpoint_list.values()): breakpoint = self.active_breakpoint_list[i.active_index] del self.active_breakpoint_list[i.active_index] breakpoint.active_index = -1 @@ -561,6 +570,7 @@ def memstats(self): self._exec_command(JERRY_DEBUGGER_MEMSTATS) def _send_string(self, args, message_type, index=0): + args = args.encode("utf8") # 1: length of type byte # 4: length of an uint32 value @@ -684,7 +694,7 @@ def process_messages(self): if not data: # Break the while loop if there is no more data. return DebuggerAction(DebuggerAction.END, "") - buffer_type = ord(data[0]) + buffer_type = safe_ord(data[0]) buffer_size = len(data) -1 logging.debug("Main buffer type: %d, message size: %d", buffer_type, buffer_size) @@ -738,10 +748,10 @@ def process_messages(self): return DebuggerAction(DebuggerAction.TEXT, result) elif buffer_type == JERRY_DEBUGGER_EXCEPTION_STR: - self.exception_string += data[1:] + self.exception_string += data[1:].decode("utf8") elif buffer_type == JERRY_DEBUGGER_EXCEPTION_STR_END: - self.exception_string += data[1:] + self.exception_string += data[1:].decode("utf8") elif buffer_type == JERRY_DEBUGGER_BACKTRACE_TOTAL: total = struct.unpack(self.byte_order + self.idx_format, data[1:])[0] @@ -808,7 +818,7 @@ def process_messages(self): return DebuggerAction(DebuggerAction.TEXT, result) elif buffer_type in [JERRY_DEBUGGER_SCOPE_VARIABLES, JERRY_DEBUGGER_SCOPE_VARIABLES_END]: - self.scope_vars += "".join(data[1:]) + self.scope_vars += "".join(data[1:].decode("utf8")) if buffer_type == JERRY_DEBUGGER_SCOPE_VARIABLES_END: result = self._process_scope_variables() @@ -864,9 +874,9 @@ def print_source(self, line_num, offset): # pylint: disable=too-many-branches,too-many-locals,too-many-statements def _parse_source(self, data): - source_code = "" - source_code_name = "" - function_name = "" + source_code = b"" + source_code_name = b"" + function_name = b"" stack = [{"line": 1, "column": 1, "name": "", @@ -879,7 +889,7 @@ def _parse_source(self, data): if data is None: return "Error: connection lost during source code receiving" - buffer_type = ord(data[0]) + buffer_type = safe_ord(data[0]) buffer_size = len(data) - 1 logging.debug("Parser buffer type: %d, message size: %d", buffer_type, buffer_size) @@ -903,14 +913,14 @@ def _parse_source(self, data): position = struct.unpack(self.byte_order + self.idx_format + self.idx_format, data[1: 1 + 4 + 4]) - stack.append({"source": source_code, - "source_name": source_code_name, + stack.append({"source": source_code.decode("utf8"), + "source_name": source_code_name.decode("utf8"), "line": position[0], "column": position[1], - "name": function_name, + "name": function_name.decode("utf8"), "lines": [], "offsets": []}) - function_name = "" + function_name = b"" elif buffer_type in [JERRY_DEBUGGER_BREAKPOINT_LIST, JERRY_DEBUGGER_BREAKPOINT_OFFSET_LIST]: name = "lines" @@ -937,8 +947,8 @@ def _parse_source(self, data): # We know the last item in the list is the general byte code. if not stack: - func_desc["source"] = source_code - func_desc["source_name"] = source_code_name + func_desc["source"] = source_code.decode("utf8") + func_desc["source_name"] = source_code_name.decode("utf8") function = JerryFunction(stack, byte_code_cp, @@ -989,7 +999,7 @@ def _parse_source(self, data): logging.debug("Pending breakpoints available") bp_list = self.pending_breakpoint_list - for breakpoint_index, breakpoint in bp_list.items(): + for breakpoint_index, breakpoint in list(bp_list.items()): source_lines = 0 for src in new_function_list.values(): if (src.source_name == breakpoint.source_name or @@ -1132,14 +1142,14 @@ def _process_incoming_text(self, buffer_type, data): while True: if buffer_type in [JERRY_DEBUGGER_EVAL_RESULT_END, JERRY_DEBUGGER_OUTPUT_RESULT_END]: - subtype = ord(data[-1]) + subtype = safe_ord(data[-1]) message += data[1:-1] break else: message += data[1:] data = self.channel.get_message(True) - buffer_type = ord(data[0]) + buffer_type = safe_ord(data[0]) # Checks if the next frame would be an invalid data frame. # If it is not the message type, or the end type of it, an exception is thrown. if buffer_type not in [msg_type, msg_type + 1]: @@ -1151,8 +1161,8 @@ def _process_incoming_text(self, buffer_type, data): log_type = "%sout:%s " % (self.blue, self.nocolor) message = self.current_out + message - lines = message.split("\n") - self.current_out = lines.pop() + lines = message.decode("utf8").split("\n") + self.current_out = lines.pop().encode("utf8") return "".join(["%s%s\n" % (log_type, line) for line in lines]) @@ -1160,11 +1170,12 @@ def _process_incoming_text(self, buffer_type, data): log_type = "%slog:%s " % (self.yellow, self.nocolor) message = self.current_log + message - lines = message.split("\n") - self.current_log = lines.pop() + lines = message.decode("utf8").split("\n") + self.current_log = lines.pop().encode("utf8") return "".join(["%s%s\n" % (log_type, line) for line in lines]) + message = message.decode("utf8") if not message.endswith("\n"): message += "\n" @@ -1174,6 +1185,9 @@ def _process_incoming_text(self, buffer_type, data): return "%serr: %s%s" % (self.red, self.nocolor, message) elif subtype == JERRY_DEBUGGER_OUTPUT_TRACE: return "%strace: %s%s" % (self.blue, self.nocolor, message) + else: + message = message.decode("utf8") + # Subtypes of eval self.prompt = True @@ -1193,17 +1207,17 @@ def _process_scope_variables(self): while buff_pos != buff_size: # Process name - name_length = ord(self.scope_vars[buff_pos:buff_pos + 1]) + name_length = safe_ord(self.scope_vars[buff_pos:buff_pos + 1]) buff_pos += 1 name = self.scope_vars[buff_pos:buff_pos + name_length] buff_pos += name_length # Process type - value_type = ord(self.scope_vars[buff_pos:buff_pos + 1]) + value_type = safe_ord(self.scope_vars[buff_pos:buff_pos + 1]) buff_pos += 1 - value_length = ord(self.scope_vars[buff_pos:buff_pos + 1]) + value_length = safe_ord(self.scope_vars[buff_pos:buff_pos + 1]) buff_pos += 1 value = self.scope_vars[buff_pos: buff_pos + value_length] buff_pos += value_length @@ -1234,16 +1248,16 @@ def _process_scope(self): table = [['level', 'type']] for i, level in enumerate(self.scope_data): - if ord(level) == JERRY_DEBUGGER_SCOPE_WITH: + if safe_ord(level) == JERRY_DEBUGGER_SCOPE_WITH: table.append([str(i), 'with']) - elif ord(level) == JERRY_DEBUGGER_SCOPE_GLOBAL: + elif safe_ord(level) == JERRY_DEBUGGER_SCOPE_GLOBAL: table.append([str(i), 'global']) - elif ord(level) == JERRY_DEBUGGER_SCOPE_NON_CLOSURE: + elif safe_ord(level) == JERRY_DEBUGGER_SCOPE_NON_CLOSURE: # Currently it is only marks the catch closure. table.append([str(i), 'catch']) - elif ord(level) == JERRY_DEBUGGER_SCOPE_LOCAL: + elif safe_ord(level) == JERRY_DEBUGGER_SCOPE_LOCAL: table.append([str(i), 'local']) - elif ord(level) == JERRY_DEBUGGER_SCOPE_CLOSURE: + elif safe_ord(level) == JERRY_DEBUGGER_SCOPE_CLOSURE: table.append([str(i), 'closure']) else: raise Exception("Unexpected scope chain element") diff --git a/jerry-debugger/jerry_client_rawpacket.py b/jerry-debugger/jerry_client_rawpacket.py index 5c3304edd5..275be83ca2 100644 --- a/jerry-debugger/jerry_client_rawpacket.py +++ b/jerry-debugger/jerry_client_rawpacket.py @@ -15,9 +15,20 @@ # limitations under the License. import struct +import sys MAX_BUFFER_SIZE = 256 + +if sys.version_info.major >= 3: + def safe_ord(c): + if isinstance(c, int): + return c + return ord(c) +else: + safe_ord = ord + + class RawPacket(object): """ Simplified transmission layer. """ def __init__(self, protocol): @@ -70,7 +81,7 @@ def get_message(self, blocking): while True: if len(self.data_buffer) >= 1: - size = ord(self.data_buffer[0]) + size = safe_ord(self.data_buffer[0]) if size == 0: raise Exception("Unexpected data frame") diff --git a/jerry-debugger/jerry_client_websocket.py b/jerry-debugger/jerry_client_websocket.py index fe2c761a93..9c75596614 100644 --- a/jerry-debugger/jerry_client_websocket.py +++ b/jerry-debugger/jerry_client_websocket.py @@ -15,11 +15,24 @@ # limitations under the License. import struct +import sys MAX_BUFFER_SIZE = 128 WEBSOCKET_BINARY_FRAME = 2 WEBSOCKET_FIN_BIT = 0x80 + +if sys.version_info.major >= 3: + # pylint: disable=invalid-name + _ord_orig = ord + def _ord_compat(c): + if isinstance(c, int): + return c + return _ord_orig(c) + # pylint: disable=redefined-builtin + ord = _ord_compat + + class WebSocket(object): def __init__(self, protocol): @@ -92,7 +105,7 @@ def send_message(self, byte_order, packed_data): """ Send message. """ message = struct.pack(byte_order + "BBI", WEBSOCKET_BINARY_FRAME | WEBSOCKET_FIN_BIT, - WEBSOCKET_FIN_BIT + struct.unpack(byte_order + "B", packed_data[0])[0], + WEBSOCKET_FIN_BIT + struct.unpack(byte_order + "B", packed_data[0:1])[0], 0) + packed_data[1:] self.__send_data(message)