Permalink
Browse files

add Session.bsession trait for session id as bytes

  • Loading branch information...
1 parent 8c63d48 commit c340839631808c735e810c9d7fbdde1492707fc5 @minrk minrk committed with Aug 19, 2011
View
6 IPython/parallel/client/client.py
@@ -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:
@@ -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)
@@ -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)
View
2 IPython/parallel/controller/hub.py
@@ -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,
View
2 IPython/parallel/controller/scheduler.py
@@ -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)
View
8 IPython/zmq/kernelmanager.py
@@ -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,
@@ -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,
@@ -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,
@@ -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)
View
16 IPython/zmq/session.py
@@ -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.""")
@@ -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
@@ -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):
View
22 IPython/zmq/tests/test_session.py
@@ -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.