Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Cordoba <[email protected]>
  • Loading branch information
impact27 and ccordoba12 authored May 28, 2024
1 parent 4f3814a commit ab4ea19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
23 changes: 14 additions & 9 deletions spyder_kernels/comms/commbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Class that handles communications between Spyder kernel and frontend.
Comms transmit data in a list of buffers, and in a json-able dictionnary.
Here, we only support json to avoid issues of compatibility between python
Here, we only support json to avoid issues of compatibility between Python
versions. In the abstraction below, buffers is used to send bytes.
The messages exchanged have the following msg_dict:
Expand All @@ -17,7 +17,7 @@
msg_dict = {
'spyder_msg_type': spyder_msg_type,
'content': content,
}
}
```
To simplify the usage of messaging, we use a higher level function calling
Expand Down Expand Up @@ -269,8 +269,7 @@ def _send_message(
comm_id: int
the comm to send to. If None sends to all comms.
buffers: list(bytes)
a list of bytes to send
a list of bytes to send.
"""
if not self.is_open(comm_id):
raise CommError("The comm is not connected.")
Expand All @@ -279,7 +278,7 @@ def _send_message(
msg_dict = {
'spyder_msg_type': spyder_msg_type,
'content': content,
}
}

self._comms[comm_id]['comm'].send(msg_dict, buffers=buffers)

Expand Down Expand Up @@ -318,7 +317,7 @@ def _register_comm(self, comm):
self._comms[comm.comm_id] = {
'comm': comm,
'status': 'opening',
}
}

def _comm_close(self, msg):
"""Close comm."""
Expand Down Expand Up @@ -395,10 +394,12 @@ def _set_call_return_value(self, call_dict, return_value, is_error=False):
if not send_reply:
# Nothing to send back
return

buffers = None
if isinstance(return_value, bytes):
buffers = [return_value]
return_value = None

content = {
'is_error': is_error,
'call_id': call_dict['call_id'],
Expand All @@ -407,7 +408,9 @@ def _set_call_return_value(self, call_dict, return_value, is_error=False):
}

self._send_message(
'remote_call_reply', content=content, comm_id=self.calling_comm_id,
'remote_call_reply',
content=content,
comm_id=self.calling_comm_id,
buffers=buffers
)

Expand Down Expand Up @@ -461,7 +464,6 @@ def _get_call_return_value(self, call_dict, comm_id):
self._wait_reply(comm_id, call_id, call_name, timeout)

content = self._reply_inbox.pop(call_id)

return_value = content['call_return_value']

if content['is_error']:
Expand All @@ -483,6 +485,7 @@ def _handle_remote_call_reply(self, msg_dict, buffers):
call_name = content['call_name']
is_error = content['is_error']
return_value = content['call_return_value']

# Prepare return value
if is_error:
return_value = CommsErrorWrapper.from_json(return_value)
Expand Down Expand Up @@ -572,11 +575,13 @@ def __call__(self, *args, **kwargs):
buffered_args = []
buffered_kwargs = []
args = list(args)

for i, arg in enumerate(args):
if isinstance(arg, bytes):
buffers.append(arg)
buffered_args.append(i)
args[i] = None

for name in kwargs:
arg = kwargs[name]
if isinstance(arg, bytes):
Expand All @@ -593,7 +598,7 @@ def __call__(self, *args, **kwargs):
'call_kwargs': kwargs,
'buffered_args': buffered_args,
'buffered_kwargs': buffered_kwargs
}
}

if not self._comms_wrapper.is_open(self._comm_id):
# Only an error if the call is blocking.
Expand Down
5 changes: 4 additions & 1 deletion spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,11 @@ def get_current_frames(self, ignore_internal_threads=True):
thread_name = thread_names[thread_id]
else:
thread_name = str(thread_id)

# Transform stack in a dict because FrameSummary objects
# are not compatible between versions of python
# are not compatible between versions of Python
stack_dict[thread_name] = stacksummary_to_json(stack)

return stack_dict

# --- For the Variable Explorer
Expand Down Expand Up @@ -365,6 +367,7 @@ def set_value(self, name, value, encoded=False):
if encoded:
# Decode_value
value = cloudpickle.loads(value)

ns = self.shell._get_reference_namespace(name)
ns[name] = value
self.log.debug(ns)
Expand Down

0 comments on commit ab4ea19

Please sign in to comment.