Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Enable comms to work across different Python versions #492

Merged
merged 21 commits into from
May 29, 2024
Prev Previous commit
Next Next commit
cloudpickle values
Quentin Peter committed May 26, 2024

Verified

This commit was signed with the committer’s verified signature.
marekful Marcell Fülöp
commit da975721a4c077c01669b460fd85b1984d1f7ab6
13 changes: 12 additions & 1 deletion spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
"""

# Standard library imports
import base64
import faulthandler
import json
import logging
@@ -20,6 +21,7 @@
import traceback
import tempfile
import threading
import cloudpickle

# Third-party imports
from ipykernel.ipkernel import IPythonKernel
@@ -359,11 +361,20 @@ def get_var_properties(self):
def get_value(self, name):
"""Get the value of a variable"""
ns = self.shell._get_current_namespace()
return ns[name]
value = ns[name]

# Encode with cloudpickle and base64
encoded_value = base64.b64encode(
cloudpickle.dumps(value)
).decode()

return encoded_value

@comm_handler
def set_value(self, name, value):
"""Set the value of a variable"""
# Decode_value
value = cloudpickle.loads(base64.b64decode(value.encode()))
ns = self.shell._get_reference_namespace(name)
impact27 marked this conversation as resolved.
Show resolved Hide resolved
ns[name] = value
self.log.debug(ns)
8 changes: 7 additions & 1 deletion spyder_kernels/console/tests/test_console_kernel.py
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@
import inspect
import uuid
from collections import namedtuple
import cloudpickle
import base64

# Test imports
import pytest
@@ -346,7 +348,11 @@ def test_set_value(kernel):
name = 'a'
asyncio.run(kernel.do_execute('a = 0', True))
value = 10
kernel.set_value(name, value)
# Encode with cloudpickle and base64
encoded_value = base64.b64encode(
cloudpickle.dumps(value)
).decode()
kernel.set_value(name, encoded_value)
log_text = get_log_text(kernel)
assert "'__builtin__': <module " in log_text
assert "'__builtins__': <module " in log_text