Пример #1
0
    def test_args(self):
        """initialization arguments for Session"""
        s = self.session
        self.assertTrue(s.pack is ss.default_packer)
        self.assertTrue(s.unpack is ss.default_unpacker)
        self.assertEqual(s.username, os.environ.get('USER', u'username'))

        s = ss.Session()
        self.assertEqual(s.username, os.environ.get('USER', u'username'))

        self.assertRaises(TypeError, ss.Session, pack='hi')
        self.assertRaises(TypeError, ss.Session, unpack='hi')
        u = str(uuid.uuid4())
        s = ss.Session(username=u'carrot', session=u)
        self.assertEqual(s.session, u)
        self.assertEqual(s.username, u'carrot')
    def test_args(self):
        """initialization arguments for Session"""
        s = self.session
        self.assertTrue(s.pack is ss.default_packer)
        self.assertTrue(s.unpack is ss.default_unpacker)
        self.assertEqual(s.username, os.environ.get("USER", "username"))

        s = ss.Session()
        self.assertEqual(s.username, os.environ.get("USER", "username"))

        self.assertRaises(TypeError, ss.Session, pack="hi")
        self.assertRaises(TypeError, ss.Session, unpack="hi")
        u = str(uuid.uuid4())
        s = ss.Session(username="******", session=u)
        self.assertEqual(s.session, u)
        self.assertEqual(s.username, "carrot")
Пример #3
0
    def test_datetimes_msgpack(self):
        msgpack = pytest.importorskip('msgpack')

        session = ss.Session(
            pack=msgpack.packb,
            unpack=lambda buf: msgpack.unpackb(buf, encoding='utf8'),
        )
        self._datetime_test(session)
Пример #4
0
 def test_bad_unpacker(self):
     try:
         session = ss.Session(unpacker=__name__ + '._bad_unpacker')
     except ValueError as e:
         self.assertIn("could not handle output", str(e))
         self.assertIn("don't work either", str(e))
     else:
         self.fail("Should have raised ValueError")
Пример #5
0
 def test_bad_packer(self):
     try:
         session = ss.Session(packer=__name__ + '._bad_packer')
     except ValueError as e:
         self.assertIn("could not serialize", str(e))
         self.assertIn("don't work", str(e))
     else:
         self.fail("Should have raised ValueError")
    def test_datetimes_msgpack(self):
        msgpack = pytest.importorskip("msgpack")

        session = ss.Session(
            pack=msgpack.packb,
            unpack=lambda buf: msgpack.unpackb(buf, raw=False),
        )
        self._datetime_test(session)
Пример #7
0
 def test_session_id(self):
     session = ss.Session()
     # get bs before us
     bs = session.bsession
     us = session.session
     self.assertEqual(us.encode('ascii'), bs)
     session = ss.Session()
     # get us before bs
     us = session.session
     bs = session.bsession
     self.assertEqual(us.encode('ascii'), bs)
     # change propagates:
     session.session = 'something else'
     bs = session.bsession
     us = session.session
     self.assertEqual(us.encode('ascii'), bs)
     session = ss.Session(session='stuff')
     # get us before bs
     self.assertEqual(session.bsession, session.session.encode('ascii'))
     self.assertEqual(b'stuff', session.bsession)
 def test_session_id(self):
     session = ss.Session()
     # get bs before us
     bs = session.bsession
     us = session.session
     self.assertEqual(us.encode("ascii"), bs)
     session = ss.Session()
     # get us before bs
     us = session.session
     bs = session.bsession
     self.assertEqual(us.encode("ascii"), bs)
     # change propagates:
     session.session = "something else"
     bs = session.bsession
     us = session.session
     self.assertEqual(us.encode("ascii"), bs)
     session = ss.Session(session="stuff")
     # get us before bs
     self.assertEqual(session.bsession, session.session.encode("ascii"))
     self.assertEqual(b"stuff", session.bsession)
Пример #9
0
 def test_cull_digest_history(self):
     session = ss.Session(digest_history_size=100)
     for i in range(100):
         session._add_digest(uuid.uuid4().bytes)
     self.assertTrue(len(session.digest_history) == 100)
     session._add_digest(uuid.uuid4().bytes)
     self.assertTrue(len(session.digest_history) == 91)
     for i in range(9):
         session._add_digest(uuid.uuid4().bytes)
     self.assertTrue(len(session.digest_history) == 100)
     session._add_digest(uuid.uuid4().bytes)
     self.assertTrue(len(session.digest_history) == 91)
Пример #10
0
def get_kernel(kernel_class=MetaKernel):
    log = logging.getLogger('test')
    log.setLevel(logging.DEBUG)

    for hdlr in log.handlers:
        log.removeHandler(hdlr)

    hdlr = logging.StreamHandler(StringIO())
    hdlr.setLevel(logging.DEBUG)
    log.addHandler(hdlr)

    context = zmq.Context.instance()
    iopub_socket = context.socket(zmq.PUB)

    kernel = kernel_class(session=ss.Session(),
                          iopub_socket=iopub_socket,
                          log=log)
    return kernel
Пример #11
0
 def setUp(self):
     BaseZMQTestCase.setUp(self)
     self.session = ss.Session()
Пример #12
0
 def test_datetimes_pickle(self):
     session = ss.Session(packer='pickle')
     self._datetime_test(session)
Пример #13
0
 def test_bad_roundtrip(self):
     with self.assertRaises(ValueError):
         session = ss.Session(unpack=lambda b: 5)
Пример #14
0
 def test_zero_digest_history(self):
     session = ss.Session(digest_history_size=0)
     for i in range(11):
         session._add_digest(uuid.uuid4().bytes)
     self.assertEqual(len(session.digest_history), 0)
Пример #15
0
    def __init__(self, overlayList, displayCtx, frame):
        """Set up the kernel and ``zmq`` ports. A jupyter connection file
        containing the information needed to connect to the kernel is saved
        to a temporary file - its path is accessed as an attribute
        called :meth:`connfile`.

        :arg overlayList: The :class:`.OverlayList`.
        :arg displayCtx:  The master :class:`.DisplayContext`.
        :arg frame:       The :class:`.FSLeyesFrame`.
        """

        ip = '127.0.0.1'
        transport = 'tcp'
        addr = '{}://{}'.format(transport, ip)
        self.__connfile = None
        self.__kernel = None
        self.__error = None
        self.__lastIter = 0
        self.__overlayList = overlayList
        self.__displayCtx = displayCtx
        self.__frame = frame
        self.__env = runscript.fsleyesScriptEnvironment(
            frame, overlayList, displayCtx)[1]

        self.__env['screenshot'] = self.__screenshot

        with warnings.catch_warnings():
            warnings.simplefilter('ignore', category=DeprecationWarning)

            # Use an empty key to disable message signing
            session = jcsession.Session(key=b'')
            context = zmq.Context.instance()

            # create sockets for kernel communication
            shellsock = context.socket(zmq.ROUTER)
            stdinsock = context.socket(zmq.ROUTER)
            controlsock = context.socket(zmq.ROUTER)
            iopubsock = context.socket(zmq.PUB)

            shellstrm = zmqstream.ZMQStream(shellsock)
            controlstrm = zmqstream.ZMQStream(controlsock)

            # I/O and heartbeat communication
            # are managed by separate threads.
            self.__iopub = iostream.IOPubThread(iopubsock)
            self.__heartbeat = heartbeat.Heartbeat(zmq.Context(),
                                                   (transport, ip, 0))
            iopubsock = self.__iopub.background_socket

            self.__heartbeat.start()
            self.__iopub.start()

            # Streams which redirect stdout/
            # stderr to the iopub socket
            stdout = iostream.OutStream(session, self.__iopub, u'stdout')
            stderr = iostream.OutStream(session, self.__iopub, u'stderr')

            # TCP ports for all sockets
            shellport = shellsock.bind_to_random_port(addr)
            stdinport = stdinsock.bind_to_random_port(addr)
            controlport = controlsock.bind_to_random_port(addr)
            iopubport = iopubsock.bind_to_random_port(addr)
            hbport = self.__heartbeat.port

            # Create the kernel
            self.__kernel = FSLeyesIPythonKernel.instance(
                stdout,
                stderr,
                shell_class=FSLeyesIPythonShell,
                session=session,
                shell_streams=[shellstrm, controlstrm],
                iopub_socket=iopubsock,
                stdin_socket=stdinsock,
                user_ns=self.__env,
                log=logging.getLogger('ipykernel.kernelbase'))

        # write connection file to a temp dir
        hd, fname = tempfile.mkstemp(prefix='fsleyes-kernel-{}.json'.format(
            os.getpid()),
                                     suffix='.json')
        os.close(hd)

        self.__connfile = fname

        log.debug('IPython kernel connection file: %s', fname)

        jc.write_connection_file(fname,
                                 shell_port=shellport,
                                 stdin_port=stdinport,
                                 iopub_port=iopubport,
                                 control_port=controlport,
                                 hb_port=hbport,
                                 ip=ip)

        atexit.register(os.remove, self.__connfile)