Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys):
"""Helper for sending a comm message on IOPub"""
data = {} if data is None else data
metadata = {} if metadata is None else metadata
content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
msg = self.kernel_client.session.msg(msg_type, content=content, parent=self.parent_header, metadata=metadata)
self.kernel_client.shell_channel.send(msg)
def proxy_iopub_channel(self):
while True:
try:
msg = client.get_iopub_msg(block=False)
if(not msg["content"].get("metadata", {}).get("echo", False)):
log.msg("[iopub] {}".format(pformat(json_clean(msg))))
yield self.publish(u"io.timbr.kernel.{}.iopub".format(_key), json_clean(msg))
except ValueError as ve:
# This happens when an "invalid signature" is encountered which for us probably
# means that the message did not originate from this kernel
log.msg("ValueError")
except Empty:
yield sleep(0.1)
def write_format_data(self, format_dict, md_dict=None):
self.msg['content']['data'] = json_clean(encode_images(format_dict))
self.msg['content']['metadata'] = md_dict
def _input_request(self, prompt, ident, parent, password=False):
# Flush output before making the request.
sys.stderr.flush()
sys.stdout.flush()
# flush the stdin socket, to purge stale replies
while True:
try:
self.stdin_socket.recv_multipart(zmq.NOBLOCK)
except zmq.ZMQError as e:
if e.errno == zmq.EAGAIN:
break
else:
raise
# Send the input request.
content = json_clean(dict(prompt=prompt, password=password))
self.session.send(self.stdin_socket, u'input_request', content, parent,
ident=ident)
# Await a response.
while True:
try:
ident, reply = self.session.recv(self.stdin_socket, 0)
except Exception:
self.log.warning("Invalid Message:", exc_info=True)
except KeyboardInterrupt:
# re-raise KeyboardInterrupt, to truncate traceback
raise KeyboardInterrupt
else:
break
try:
value = py3compat.unicode_to_str(reply['content']['value'])
def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys):
"""Helper for sending a comm message on IOPub"""
data = {} if data is None else data
metadata = {} if metadata is None else metadata
content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
self.kernel.session.send(self.kernel.iopub_socket, msg_type,
content,
metadata=json_clean(metadata),
parent=self.kernel._parent_header,
ident=self.topic,
buffers=buffers,
)
if transient is None:
transient = {}
self._validate_data(data, metadata)
content = {}
content['data'] = encode_images(data)
content['metadata'] = metadata
content['transient'] = transient
content['execution_count'] = self.get_execution_count()
msg_type = 'update_display_data' if update else 'display_data'
# Use 2-stage process to send a message,
# in order to put it through the transform
# hooks before potentially sending.
msg = self.session.msg(
msg_type, json_clean(content),
parent=self.parent_header
)
# Each transform either returns a new
# message or None. If None is returned,
# the message has been 'used' and we return.
for hook in self._hooks:
msg = hook(msg)
if msg is None:
return
self.session.send(
self.pub_socket, msg, ident=self.topic,
)
def execute_interactive(self, *args, **kwargs):
result = yield self._lock.run(threads.deferToThread, client.execute_interactive, *args, **kwargs)
returnValue(json_clean(result))
def matplotlib_post_run(data_list):
png_data = None
figure = plt.gcf()
# Always try to get the current figure.
# This is not efficient, but we can support any libraries
# that use matplotlib.
png_data = print_figure(figure, fmt='png')
figure.clear()
if png_data is not None:
width, height = _pngxy(png_data)
data = encode_images({'image/png':png_data})
metadata = {'image/png':dict(width=width, height=height)}
data_list.append(json_clean(dict(data=data, metadata=metadata)))
self._publish_execute_input(code, parent, uuid)
reply_content, res = self.do_execute(code, uuid, code_dict, silent, store_history,
user_expressions, allow_stdin)
# Flush output before sending the reply.
sys.stdout.flush()
sys.stderr.flush()
# FIXME: on rare occasions, the flush doesn't seem to make it to the
# clients... This seems to mitigate the problem, but we definitely need
# to better understand what's going on.
if self._execute_sleep:
time.sleep(self._execute_sleep)
# Send the reply.
reply_content = json_clean(reply_content)
metadata = self.finish_metadata(parent, metadata, reply_content)
reply_msg = self.session.send(stream, u'execute_reply',
reply_content, parent, metadata=metadata,
ident=ident)
self.log.debug("%s", reply_msg)
if not silent and reply_msg['content']['status'] == u'error' and stop_on_error:
self._abort_queues()
return res