Skip to content

Commit

Permalink
add Session.bsession trait for session id as bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk authored and takluyver committed Sep 7, 2011
1 parent 8c63d48 commit c340839
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
6 changes: 3 additions & 3 deletions IPython/parallel/client/client.py
Expand Up @@ -370,7 +370,7 @@ def __init__(self, url_or_file=None, profile=None, profile_dir=None, ipython_dir
self.session = Session(**extra_args)

self._query_socket = self._context.socket(zmq.DEALER)
self._query_socket.setsockopt(zmq.IDENTITY, util.asbytes(self.session.session))
self._query_socket.setsockopt(zmq.IDENTITY, self.session.bsession)
if self._ssh:
tunnel.tunnel_connection(self._query_socket, url, sshserver, **ssh_kwargs)
else:
Expand Down Expand Up @@ -496,7 +496,7 @@ def connect_socket(s, url):
content = msg.content
self._config['registration'] = dict(content)
if content.status == 'ok':
ident = util.asbytes(self.session.session)
ident = self.session.bsession
if content.mux:
self._mux_socket = self._context.socket(zmq.DEALER)
self._mux_socket.setsockopt(zmq.IDENTITY, ident)
Expand All @@ -512,7 +512,7 @@ def connect_socket(s, url):
self._notification_socket.setsockopt(zmq.SUBSCRIBE, b'')
# if content.query:
# self._query_socket = self._context.socket(zmq.DEALER)
# self._query_socket.setsockopt(zmq.IDENTITY, self.session.session)
# self._query_socket.setsockopt(zmq.IDENTITY, self.session.bsession)
# connect_socket(self._query_socket, content.query)
if content.control:
self._control_socket = self._context.socket(zmq.DEALER)
Expand Down
2 changes: 1 addition & 1 deletion IPython/parallel/controller/hub.py
Expand Up @@ -288,7 +288,7 @@ def init_hub(self):
# resubmit stream
r = ZMQStream(ctx.socket(zmq.DEALER), loop)
url = util.disambiguate_url(self.client_info['task'][-1])
r.setsockopt(zmq.IDENTITY, util.asbytes(self.session.session))
r.setsockopt(zmq.IDENTITY, self.session.bsession)
r.connect(url)

self.hub = Hub(loop=loop, session=self.session, monitor=sub, heartmonitor=self.heartmonitor,
Expand Down
2 changes: 1 addition & 1 deletion IPython/parallel/controller/scheduler.py
Expand Up @@ -177,7 +177,7 @@ def _scheme_default(self):
ident = CBytes() # ZMQ identity. This should just be self.session.session
# but ensure Bytes
def _ident_default(self):
return asbytes(self.session.session)
return self.session.bsession

def start(self):
self.engine_stream.on_recv(self.dispatch_result, copy=False)
Expand Down
8 changes: 4 additions & 4 deletions IPython/zmq/kernelmanager.py
Expand Up @@ -188,7 +188,7 @@ def __init__(self, context, session, address):
def run(self):
"""The thread's main activity. Call start() instead."""
self.socket = self.context.socket(zmq.DEALER)
self.socket.setsockopt(zmq.IDENTITY, self.session.session.encode("ascii"))
self.socket.setsockopt(zmq.IDENTITY, self.session.bsession)
self.socket.connect('tcp://%s:%i' % self.address)
self.iostate = POLLERR|POLLIN
self.ioloop.add_handler(self.socket, self._handle_events,
Expand Down Expand Up @@ -395,7 +395,7 @@ def run(self):
"""The thread's main activity. Call start() instead."""
self.socket = self.context.socket(zmq.SUB)
self.socket.setsockopt(zmq.SUBSCRIBE,b'')
self.socket.setsockopt(zmq.IDENTITY, self.session.session.encode("ascii"))
self.socket.setsockopt(zmq.IDENTITY, self.session.bsession)
self.socket.connect('tcp://%s:%i' % self.address)
self.iostate = POLLIN|POLLERR
self.ioloop.add_handler(self.socket, self._handle_events,
Expand Down Expand Up @@ -483,7 +483,7 @@ def __init__(self, context, session, address):
def run(self):
"""The thread's main activity. Call start() instead."""
self.socket = self.context.socket(zmq.DEALER)
self.socket.setsockopt(zmq.IDENTITY, self.session.session.encode("ascii"))
self.socket.setsockopt(zmq.IDENTITY, self.session.bsession)
self.socket.connect('tcp://%s:%i' % self.address)
self.iostate = POLLERR|POLLIN
self.ioloop.add_handler(self.socket, self._handle_events,
Expand Down Expand Up @@ -562,7 +562,7 @@ def __init__(self, context, session, address):

def _create_socket(self):
self.socket = self.context.socket(zmq.REQ)
self.socket.setsockopt(zmq.IDENTITY, self.session.session.encode("ascii"))
self.socket.setsockopt(zmq.IDENTITY, self.session.bsession)
self.socket.connect('tcp://%s:%i' % self.address)
self.poller = zmq.Poller()
self.poller.register(self.socket, zmq.POLLIN)
Expand Down
16 changes: 14 additions & 2 deletions IPython/zmq/session.py
Expand Up @@ -242,7 +242,15 @@ def _unpacker_changed(self, name, old, new):
session = CUnicode(u'', config=True,
help="""The UUID identifying this session.""")
def _session_default(self):
return unicode(uuid.uuid4())
u = unicode(uuid.uuid4())
self.bsession = u.encode('ascii')
return u

def _session_changed(self, name, old, new):
self.bsession = self.session.encode('ascii')

# bsession is the session as bytes
bsession = CBytes(b'')

username = Unicode(os.environ.get('USER',u'username'), config=True,
help="""Username for the Session. Default is your system username.""")
Expand Down Expand Up @@ -296,9 +304,11 @@ def __init__(self, **kwargs):
pack/unpack : callables
You can also set the pack/unpack callables for serialization
directly.
session : bytes
session : unicode (must be ascii)
the ID of this Session object. The default is to generate a new
UUID.
bsession : bytes
The session as bytes
username : unicode
username added to message headers. The default is to ask the OS.
key : bytes
Expand All @@ -311,6 +321,8 @@ def __init__(self, **kwargs):
super(Session, self).__init__(**kwargs)
self._check_packers()
self.none = self.pack({})
# ensure self._session_default() if necessary, so bsession is defined:
self.session

@property
def msg_id(self):
Expand Down
22 changes: 22 additions & 0 deletions IPython/zmq/tests/test_session.py
Expand Up @@ -185,4 +185,26 @@ def test_feed_identities(self):
content = dict(code='whoda',stuff=object())
themsg = self.session.msg('execute',content=content)
pmsg = theids

def test_session_id(self):
session = ss.Session()
# get bs before us
bs = session.bsession
us = session.session
self.assertEquals(us.encode('ascii'), bs)
session = ss.Session()
# get us before bs
us = session.session
bs = session.bsession
self.assertEquals(us.encode('ascii'), bs)
# change propagates:
session.session = 'something else'
bs = session.bsession
us = session.session
self.assertEquals(us.encode('ascii'), bs)
session = ss.Session(session='stuff')
# get us before bs
self.assertEquals(session.bsession, session.session.encode('ascii'))
self.assertEquals(b'stuff', session.bsession)


0 comments on commit c340839

Please sign in to comment.