def _set_parent_message(self): result = self._execute(""" JupyterKernel.communicator.updateParentMessage( to: KernelCommunicator.ParentMessage(json: %s)) """ % json.dumps(json.dumps(squash_dates(self._parent_header)))) if isinstance(result, ExecutionResultError): raise Exception('Error setting parent message: %s' % result)
def _set_parent_message(self): result = self._execute(""" JupyterKernel.communicator.updateParentMessage( to: KernelCommunicator.ParentMessage(json: %s)) """ % json.dumps(json.dumps(squash_dates(self._parent_header)))) if isinstance(result, ExecutionResultError): self.log.error(result.description_and_stdout()) return
def _check_packers(self) -> None: """check packers for datetime support.""" pack = self.pack unpack = self.unpack # check simple serialization msg_list = dict(a=[1, "hi"]) try: packed = pack(msg_list) except Exception as e: error_msg = "packer '{packer}' could not serialize a simple message: {e}{jsonmsg}" if self.packer == "json": jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod else: jsonmsg = "" raise ValueError( error_msg.format(packer=self.packer, e=e, jsonmsg=jsonmsg)) from e # ensure packed message is bytes if not isinstance(packed, bytes): raise ValueError("message packed to %r, but bytes are required" % type(packed)) # check that unpack is pack's inverse try: unpacked = unpack(packed) assert unpacked == msg_list except Exception as e: error_msg = ( "unpacker '{unpacker}' could not handle output from packer '{packer}': {e}{jsonmsg}" ) if self.packer == "json": jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod else: jsonmsg = "" raise ValueError( error_msg.format(packer=self.packer, unpacker=self.unpacker, e=e, jsonmsg=jsonmsg)) from e # check datetime support msg_datetime = dict(t=utcnow()) try: unpacked = unpack(pack(msg_datetime)) if isinstance(unpacked["t"], datetime): raise ValueError("Shouldn't deserialize to datetime") except Exception: self.pack = lambda o: pack(squash_dates(o)) self.unpack = lambda s: unpack(s)
def _check_packers(self): """check packers for datetime support.""" pack = self.pack unpack = self.unpack # check simple serialization msg = dict(a=[1,'hi']) try: packed = pack(msg) except Exception as e: msg = "packer '{packer}' could not serialize a simple message: {e}{jsonmsg}" if self.packer == 'json': jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod else: jsonmsg = "" raise ValueError( msg.format(packer=self.packer, e=e, jsonmsg=jsonmsg) ) # ensure packed message is bytes if not isinstance(packed, bytes): raise ValueError("message packed to %r, but bytes are required"%type(packed)) # check that unpack is pack's inverse try: unpacked = unpack(packed) assert unpacked == msg except Exception as e: msg = "unpacker '{unpacker}' could not handle output from packer '{packer}': {e}{jsonmsg}" if self.packer == 'json': jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod else: jsonmsg = "" raise ValueError( msg.format(packer=self.packer, unpacker=self.unpacker, e=e, jsonmsg=jsonmsg) ) # check datetime support msg = dict(t=datetime.now()) try: unpacked = unpack(pack(msg)) if isinstance(unpacked['t'], datetime): raise ValueError("Shouldn't deserialize to datetime") except Exception: self.pack = lambda o: pack(squash_dates(o)) self.unpack = lambda s: unpack(s)
def _check_packers(self) -> None: """check packers for datetime support.""" pack = self.pack unpack = self.unpack # check simple serialization msg_list = dict(a=[1, "hi"]) try: packed = pack(msg_list) except Exception as e: raise ValueError( f"packer '{self.packer}' could not serialize a simple message: {e}" ) from e # ensure packed message is bytes if not isinstance(packed, bytes): raise ValueError("message packed to %r, but bytes are required" % type(packed)) # check that unpack is pack's inverse try: unpacked = unpack(packed) assert unpacked == msg_list except Exception as e: raise ValueError( f"unpacker '{self.unpacker}' could not handle output from packer" f" '{self.packer}': {e}") from e # check datetime support msg_datetime = dict(t=utcnow()) try: unpacked = unpack(pack(msg_datetime)) if isinstance(unpacked["t"], datetime): raise ValueError("Shouldn't deserialize to datetime") except Exception: self.pack = lambda o: pack(squash_dates(o)) self.unpack = lambda s: unpack(s)
# globals and defaults #----------------------------------------------------------------------------- # default values for the thresholds: MAX_ITEMS = 64 MAX_BYTES = 1024 # ISO8601-ify datetime objects # allow unicode # disallow nan, because it's not actually valid JSON json_packer = lambda obj: jsonapi.dumps(obj, default=date_default, ensure_ascii=False, allow_nan=False, ) json_unpacker = lambda s: jsonapi.loads(s) pickle_packer = lambda o: pickle.dumps(squash_dates(o), PICKLE_PROTOCOL) pickle_unpacker = pickle.loads default_packer = json_packer default_unpacker = json_unpacker DELIM = b"<IDS|MSG>" # singleton dummy tracker, which will always report as done DONE = zmq.MessageTracker() #----------------------------------------------------------------------------- # Mixin tools for apps that use Sessions #----------------------------------------------------------------------------- def new_id(): """Generate a new random id.
# globals and defaults #----------------------------------------------------------------------------- # default values for the thresholds: MAX_ITEMS = 64 MAX_BYTES = 1024 # ISO8601-ify datetime objects # allow unicode # disallow nan, because it's not actually valid JSON json_packer = lambda obj: jsonapi.dumps(obj, default=date_default, ensure_ascii=False, allow_nan=False, ) json_unpacker = lambda s: jsonapi.loads(s) pickle_packer = lambda o: pickle.dumps(squash_dates(o), PICKLE_PROTOCOL) pickle_unpacker = pickle.loads default_packer = json_packer default_unpacker = json_unpacker DELIM = b"<IDS|MSG>" # singleton dummy tracker, which will always report as done DONE = zmq.MessageTracker() #----------------------------------------------------------------------------- # Mixin tools for apps that use Sessions #----------------------------------------------------------------------------- session_aliases = dict( ident = 'Session.session',
def pickle_packer(o): return pickle.dumps(squash_dates(o), PICKLE_PROTOCOL)