Skip to content

Commit

Permalink
previewer - failed send to be pushed as error
Browse files Browse the repository at this point in the history
better visibility for failures on debug
  • Loading branch information
idanpa committed Mar 4, 2024
1 parent f09f6ec commit f6921a8
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions previewer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import io
import ast
import sys
from types import ModuleType
import traceback
import IPython
from prompt_toolkit.styles import Style, merge_styles
from traitlets.config.loader import Config

CTRL_C_TIMEOUT = 2
RESTART_TIMEOUT = 10
NS_BLOCK_LIST = ['open', 'exit', 'quit', 'get_ipython', 'calcpy']

class PipeListener(threading.Thread):
def __init__(self, conn, cb):
Expand Down Expand Up @@ -66,7 +68,6 @@ def __init__(self, exec_conn, ctrl_conn, ns_conn, config=Config(), formatter=str
self.debug = debug
self.stdout_path = stdout_path
self.interactive = interactive
self.ns_block_list = ['open', 'exit', 'quit', 'get_ipython']
self._open = io.open
self.start()

Expand Down Expand Up @@ -102,7 +103,7 @@ def sandbox_post(self):
os.abort = None
os.kill = None
os.system = None
for key in self.ns_block_list:
for key in NS_BLOCK_LIST:
self.previewer_ip.user_ns.pop(key, None)

def initialize(self):
Expand All @@ -128,7 +129,7 @@ def ns_job(self):
while True:
try:
ns_msg = self.ns_conn.recv()
if ns_msg[0] in self.ns_block_list:
if ns_msg[0] in NS_BLOCK_LIST:
continue
if len(ns_msg) == 2:
self.previewer_ip.user_ns[ns_msg[0]] = ns_msg[1]
Expand Down Expand Up @@ -268,16 +269,20 @@ def text_changed_handler(self, buffer):

def push(self, variables):
for var_name, val in variables.copy().items():
if var_name in NS_BLOCK_LIST or isinstance(val, ModuleType):
continue
try:
self.ns_conn.send((var_name, val))
except Exception as e:
pass
if self.debug and var_name != 'Out':
self.ns_conn.send((var_name, repr(e)))

def push_kv(self, var_name, key, value):
try:
self.ns_conn.send((var_name, key, value))
except Exception as e:
pass
if self.debug:
self.ns_conn.send((var_name, key, repr(e)))

def get_stdout(self):
if self.stdout_path == None:
Expand Down

0 comments on commit f6921a8

Please sign in to comment.