Skip to content

Commit 484fbe9

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 431a28b commit 484fbe9

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

jerry-debugger/jerry_client_main.py

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

151151

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

154164
def __init__(self, line, offset, function):
@@ -561,6 +571,7 @@ def memstats(self):
561571
self._exec_command(JERRY_DEBUGGER_MEMSTATS)
562572

563573
def _send_string(self, args, message_type, index=0):
574+
args = args.encode("utf8")
564575

565576
# 1: length of type byte
566577
# 4: length of an uint32 value
@@ -808,7 +819,7 @@ def process_messages(self):
808819
return DebuggerAction(DebuggerAction.TEXT, result)
809820

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

813824
if buffer_type == JERRY_DEBUGGER_SCOPE_VARIABLES_END:
814825
result = self._process_scope_variables()
@@ -864,9 +875,9 @@ def print_source(self, line_num, offset):
864875

865876
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
866877
def _parse_source(self, data):
867-
source_code = ""
868-
source_code_name = ""
869-
function_name = ""
878+
source_code = b""
879+
source_code_name = b""
880+
function_name = b""
870881
stack = [{"line": 1,
871882
"column": 1,
872883
"name": "",
@@ -903,11 +914,11 @@ def _parse_source(self, data):
903914
position = struct.unpack(self.byte_order + self.idx_format + self.idx_format,
904915
data[1: 1 + 4 + 4])
905916

906-
stack.append({"source": source_code,
907-
"source_name": source_code_name,
917+
stack.append({"source": source_code.decode("utf8"),
918+
"source_name": source_code_name.decode("utf8"),
908919
"line": position[0],
909920
"column": position[1],
910-
"name": function_name,
921+
"name": function_name.decode("utf8"),
911922
"lines": [],
912923
"offsets": []})
913924
function_name = ""
@@ -937,8 +948,8 @@ def _parse_source(self, data):
937948

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

943954
function = JerryFunction(stack,
944955
byte_code_cp,
@@ -1151,7 +1162,7 @@ def _process_incoming_text(self, buffer_type, data):
11511162
log_type = "%sout:%s " % (self.blue, self.nocolor)
11521163

11531164
message = self.current_out + message
1154-
lines = message.split("\n")
1165+
lines = message.decode("utf8").split("\n")
11551166
self.current_out = lines.pop()
11561167

11571168
return "".join(["%s%s\n" % (log_type, line) for line in lines])
@@ -1160,7 +1171,7 @@ def _process_incoming_text(self, buffer_type, data):
11601171
log_type = "%slog:%s " % (self.yellow, self.nocolor)
11611172

11621173
message = self.current_log + message
1163-
lines = message.split("\n")
1174+
lines = message.decode("utf8").split("\n")
11641175
self.current_log = lines.pop()
11651176

11661177
return "".join(["%s%s\n" % (log_type, line) for line in lines])
@@ -1169,15 +1180,16 @@ def _process_incoming_text(self, buffer_type, data):
11691180
message += "\n"
11701181

11711182
if subtype == JERRY_DEBUGGER_OUTPUT_WARNING:
1172-
return "%swarning: %s%s" % (self.yellow, self.nocolor, message)
1183+
return "%swarning: %s%s" % (self.yellow, self.nocolor, message.decode("utf8"))
11731184
elif subtype == JERRY_DEBUGGER_OUTPUT_ERROR:
1174-
return "%serr: %s%s" % (self.red, self.nocolor, message)
1185+
return "%serr: %s%s" % (self.red, self.nocolor, message.decode("utf8"))
11751186
elif subtype == JERRY_DEBUGGER_OUTPUT_TRACE:
1176-
return "%strace: %s%s" % (self.blue, self.nocolor, message)
1187+
return "%strace: %s%s" % (self.blue, self.nocolor, message.decode("utf8"))
11771188

11781189
# Subtypes of eval
11791190
self.prompt = True
11801191

1192+
message = message.decode("utf8")
11811193
if not message.endswith("\n"):
11821194
message += "\n"
11831195

jerry-debugger/jerry_client_websocket.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,23 @@
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+
_ord_orig = ord
27+
def _ord_compat(c):
28+
if isinstance(c, int):
29+
return c
30+
return _ord_orig(c)
31+
# pylint: disable=redefined-builtin
32+
ord = _ord_compat
33+
34+
2335
class WebSocket(object):
2436
def __init__(self, protocol):
2537

@@ -92,7 +104,7 @@ def send_message(self, byte_order, packed_data):
92104
""" Send message. """
93105
message = struct.pack(byte_order + "BBI",
94106
WEBSOCKET_BINARY_FRAME | WEBSOCKET_FIN_BIT,
95-
WEBSOCKET_FIN_BIT + struct.unpack(byte_order + "B", packed_data[0])[0],
107+
WEBSOCKET_FIN_BIT + struct.unpack(byte_order + "B", packed_data[0:1])[0],
96108
0) + packed_data[1:]
97109

98110
self.__send_data(message)

0 commit comments

Comments
 (0)