Skip to content

Commit 622ec35

Browse files
committed
Python debugger support for Python 3 (in addition to Python 2)
- Added ord builtin compatibility to pass through int arguments - Fixed JerryDebugger to decode bytes as utf8 strings when necessary - Fixed WebSocket send_message method to use packed_data[0:1] bytes slice JerryScript-DCO-1.0-Signed-off-by: Zac Medico <[email protected]>
1 parent a6ab5e9 commit 622ec35

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

jerry-debugger/jerry_client_main.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ def arguments_parse():
149149
return args
150150

151151

152+
if sys.version_info.major >= 3:
153+
# pylint: disable=invalid-name
154+
_ord_orig = ord
155+
def _ord_compat(c):
156+
if isinstance(c, int):
157+
return c
158+
return _ord_orig(c)
159+
# pylint: disable=redefined-builtin
160+
ord = _ord_compat
161+
162+
152163
class JerryBreakpoint(object):
153164

154165
def __init__(self, line, offset, function):
@@ -400,7 +411,7 @@ def delete(self, args):
400411
"to clear all the given breakpoints\n "
401412
elif args in ['all', 'pending', 'active']:
402413
if args != "pending":
403-
for i in self.active_breakpoint_list.values():
414+
for i in list(self.active_breakpoint_list.values()):
404415
breakpoint = self.active_breakpoint_list[i.active_index]
405416
del self.active_breakpoint_list[i.active_index]
406417
breakpoint.active_index = -1
@@ -561,6 +572,7 @@ def memstats(self):
561572
self._exec_command(JERRY_DEBUGGER_MEMSTATS)
562573

563574
def _send_string(self, args, message_type, index=0):
575+
args = args.encode("utf8")
564576

565577
# 1: length of type byte
566578
# 4: length of an uint32 value
@@ -738,10 +750,10 @@ def process_messages(self):
738750
return DebuggerAction(DebuggerAction.TEXT, result)
739751

740752
elif buffer_type == JERRY_DEBUGGER_EXCEPTION_STR:
741-
self.exception_string += data[1:]
753+
self.exception_string += data[1:].decode("utf8")
742754

743755
elif buffer_type == JERRY_DEBUGGER_EXCEPTION_STR_END:
744-
self.exception_string += data[1:]
756+
self.exception_string += data[1:].decode("utf8")
745757

746758
elif buffer_type == JERRY_DEBUGGER_BACKTRACE_TOTAL:
747759
total = struct.unpack(self.byte_order + self.idx_format, data[1:])[0]
@@ -808,7 +820,7 @@ def process_messages(self):
808820
return DebuggerAction(DebuggerAction.TEXT, result)
809821

810822
elif buffer_type in [JERRY_DEBUGGER_SCOPE_VARIABLES, JERRY_DEBUGGER_SCOPE_VARIABLES_END]:
811-
self.scope_vars += "".join(data[1:])
823+
self.scope_vars += "".join(data[1:].decode("utf8"))
812824

813825
if buffer_type == JERRY_DEBUGGER_SCOPE_VARIABLES_END:
814826
result = self._process_scope_variables()
@@ -864,9 +876,9 @@ def print_source(self, line_num, offset):
864876

865877
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
866878
def _parse_source(self, data):
867-
source_code = ""
868-
source_code_name = ""
869-
function_name = ""
879+
source_code = b""
880+
source_code_name = b""
881+
function_name = b""
870882
stack = [{"line": 1,
871883
"column": 1,
872884
"name": "",
@@ -903,14 +915,14 @@ def _parse_source(self, data):
903915
position = struct.unpack(self.byte_order + self.idx_format + self.idx_format,
904916
data[1: 1 + 4 + 4])
905917

906-
stack.append({"source": source_code,
907-
"source_name": source_code_name,
918+
stack.append({"source": source_code.decode("utf8"),
919+
"source_name": source_code_name.decode("utf8"),
908920
"line": position[0],
909921
"column": position[1],
910-
"name": function_name,
922+
"name": function_name.decode("utf8"),
911923
"lines": [],
912924
"offsets": []})
913-
function_name = ""
925+
function_name = b""
914926

915927
elif buffer_type in [JERRY_DEBUGGER_BREAKPOINT_LIST, JERRY_DEBUGGER_BREAKPOINT_OFFSET_LIST]:
916928
name = "lines"
@@ -937,8 +949,8 @@ def _parse_source(self, data):
937949

938950
# We know the last item in the list is the general byte code.
939951
if not stack:
940-
func_desc["source"] = source_code
941-
func_desc["source_name"] = source_code_name
952+
func_desc["source"] = source_code.decode("utf8")
953+
func_desc["source_name"] = source_code_name.decode("utf8")
942954

943955
function = JerryFunction(stack,
944956
byte_code_cp,
@@ -989,7 +1001,7 @@ def _parse_source(self, data):
9891001
logging.debug("Pending breakpoints available")
9901002
bp_list = self.pending_breakpoint_list
9911003

992-
for breakpoint_index, breakpoint in bp_list.items():
1004+
for breakpoint_index, breakpoint in list(bp_list.items()):
9931005
source_lines = 0
9941006
for src in new_function_list.values():
9951007
if (src.source_name == breakpoint.source_name or
@@ -1151,20 +1163,21 @@ def _process_incoming_text(self, buffer_type, data):
11511163
log_type = "%sout:%s " % (self.blue, self.nocolor)
11521164

11531165
message = self.current_out + message
1154-
lines = message.split("\n")
1155-
self.current_out = lines.pop()
1166+
lines = message.decode("utf8").split("\n")
1167+
self.current_out = lines.pop().encode("utf8")
11561168

11571169
return "".join(["%s%s\n" % (log_type, line) for line in lines])
11581170

11591171
if subtype == JERRY_DEBUGGER_OUTPUT_DEBUG:
11601172
log_type = "%slog:%s " % (self.yellow, self.nocolor)
11611173

11621174
message = self.current_log + message
1163-
lines = message.split("\n")
1164-
self.current_log = lines.pop()
1175+
lines = message.decode("utf8").split("\n")
1176+
self.current_log = lines.pop().encode("utf8")
11651177

11661178
return "".join(["%s%s\n" % (log_type, line) for line in lines])
11671179

1180+
message = message.decode("utf8")
11681181
if not message.endswith("\n"):
11691182
message += "\n"
11701183

@@ -1174,6 +1187,9 @@ def _process_incoming_text(self, buffer_type, data):
11741187
return "%serr: %s%s" % (self.red, self.nocolor, message)
11751188
elif subtype == JERRY_DEBUGGER_OUTPUT_TRACE:
11761189
return "%strace: %s%s" % (self.blue, self.nocolor, message)
1190+
else:
1191+
message = message.decode("utf8")
1192+
11771193

11781194
# Subtypes of eval
11791195
self.prompt = True

jerry-debugger/jerry_client_rawpacket.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@
1515
# limitations under the License.
1616

1717
import struct
18+
import sys
1819

1920
MAX_BUFFER_SIZE = 256
2021

22+
if sys.version_info.major >= 3:
23+
# pylint: disable=invalid-name
24+
_ord_orig = ord
25+
def _ord_compat(c):
26+
if isinstance(c, int):
27+
return c
28+
return _ord_orig(c)
29+
# pylint: disable=redefined-builtin
30+
ord = _ord_compat
31+
32+
2133
class RawPacket(object):
2234
""" Simplified transmission layer. """
2335
def __init__(self, protocol):

jerry-debugger/jerry_client_websocket.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,24 @@
1515
# limitations under the License.
1616

1717
import struct
18+
import sys
1819

1920
MAX_BUFFER_SIZE = 128
2021
WEBSOCKET_BINARY_FRAME = 2
2122
WEBSOCKET_FIN_BIT = 0x80
2223

24+
25+
if sys.version_info.major >= 3:
26+
# pylint: disable=invalid-name
27+
_ord_orig = ord
28+
def _ord_compat(c):
29+
if isinstance(c, int):
30+
return c
31+
return _ord_orig(c)
32+
# pylint: disable=redefined-builtin
33+
ord = _ord_compat
34+
35+
2336
class WebSocket(object):
2437
def __init__(self, protocol):
2538

@@ -92,7 +105,7 @@ def send_message(self, byte_order, packed_data):
92105
""" Send message. """
93106
message = struct.pack(byte_order + "BBI",
94107
WEBSOCKET_BINARY_FRAME | WEBSOCKET_FIN_BIT,
95-
WEBSOCKET_FIN_BIT + struct.unpack(byte_order + "B", packed_data[0])[0],
108+
WEBSOCKET_FIN_BIT + struct.unpack(byte_order + "B", packed_data[0:1])[0],
96109
0) + packed_data[1:]
97110

98111
self.__send_data(message)

0 commit comments

Comments
 (0)