Skip to content

Commit a27adad

Browse files
committed
discard parallel.util.asbytes in favor of py3compat.cast_bytes
1 parent 60c884a commit a27adad

7 files changed

Lines changed: 29 additions & 30 deletions

File tree

IPython/parallel/apps/ipengineapp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
from IPython.config.configurable import Configurable
4646

4747
from IPython.parallel.engine.engine import EngineFactory
48-
from IPython.parallel.util import disambiguate_url, asbytes
48+
from IPython.parallel.util import disambiguate_url
4949

5050
from IPython.utils.importstring import import_item
51+
from IPython.utils.py3compat import cast_bytes
5152
from IPython.utils.traitlets import Bool, Unicode, Dict, List, Float
5253

5354

@@ -203,7 +204,7 @@ def load_connector_file(self):
203204
d = json.loads(f.read())
204205

205206
if 'exec_key' in d:
206-
config.Session.key = asbytes(d['exec_key'])
207+
config.Session.key = cast_bytes(d['exec_key'])
207208

208209
try:
209210
config.EngineFactory.location

IPython/parallel/client/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from IPython.utils.jsonutil import rekey
3737
from IPython.utils.localinterfaces import LOCAL_IPS
3838
from IPython.utils.path import get_ipython_dir
39+
from IPython.utils.py3compat import cast_bytes
3940
from IPython.utils.traitlets import (HasTraits, Integer, Instance, Unicode,
4041
Dict, List, Bool, Set, Any)
4142
from IPython.external.decorator import decorator
@@ -370,7 +371,7 @@ def __init__(self, url_or_file=None, profile=None, profile_dir=None, ipython_dir
370371
if os.path.isfile(exec_key):
371372
extra_args['keyfile'] = exec_key
372373
else:
373-
exec_key = util.asbytes(exec_key)
374+
exec_key = cast_bytes(exec_key)
374375
extra_args['key'] = exec_key
375376
self.session = Session(**extra_args)
376377

@@ -468,7 +469,7 @@ def _build_targets(self, targets):
468469
if not isinstance(targets, (tuple, list, xrange)):
469470
raise TypeError("targets by int/slice/collection of ints only, not %s"%(type(targets)))
470471

471-
return [util.asbytes(self._engines[t]) for t in targets], list(targets)
472+
return [cast_bytes(self._engines[t]) for t in targets], list(targets)
472473

473474
def _connect(self, sshserver, ssh_kwargs, timeout):
474475
"""setup all our socket connections to the cluster. This is called from

IPython/parallel/controller/heartmonitor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
from zmq.eventloop import ioloop, zmqstream
2424

2525
from IPython.config.configurable import LoggingConfigurable
26+
from IPython.utils.py3compat import str_to_bytes
2627
from IPython.utils.traitlets import Set, Instance, CFloat, Integer
2728

28-
from IPython.parallel.util import asbytes, log_errors
29+
from IPython.parallel.util import log_errors
2930

3031
class Heart(object):
3132
"""A basic heart object for responding to a HeartMonitor.
@@ -123,7 +124,7 @@ def beat(self):
123124
self.responses = set()
124125
# print self.on_probation, self.hearts
125126
# self.log.debug("heartbeat::beat %.3f, %i beating hearts", self.lifetime, len(self.hearts))
126-
self.pingstream.send(asbytes(str(self.lifetime)))
127+
self.pingstream.send(str_to_bytes(str(self.lifetime)))
127128
# flush stream to force immediate socket send
128129
self.pingstream.flush()
129130

@@ -151,8 +152,8 @@ def handle_heart_failure(self, heart):
151152
@log_errors
152153
def handle_pong(self, msg):
153154
"a heart just beat"
154-
current = asbytes(str(self.lifetime))
155-
last = asbytes(str(self.last_ping))
155+
current = str_to_bytes(str(self.lifetime))
156+
last = str_to_bytes(str(self.last_ping))
156157
if msg[1] == current:
157158
delta = time.time()-self.tic
158159
# self.log.debug("heartbeat::heart %r took %.2f ms to respond"%(msg[0], 1000*delta))

IPython/parallel/controller/hub.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
# internal:
3030
from IPython.utils.importstring import import_item
31+
from IPython.utils.py3compat import cast_bytes
3132
from IPython.utils.traitlets import (
3233
HasTraits, Instance, Integer, Unicode, Dict, Set, Tuple, CBytes, DottedObjectName
3334
)
@@ -441,7 +442,7 @@ def _validate_targets(self, targets):
441442
for t in targets:
442443
# map raw identities to ids
443444
if isinstance(t, (str,unicode)):
444-
t = self.by_ident.get(t, t)
445+
t = self.by_ident.get(cast_bytes(t), t)
445446
_targets.append(t)
446447
targets = _targets
447448
bad_targets = [ t for t in targets if t not in self.ids ]
@@ -719,8 +720,8 @@ def save_task_result(self, idents, msg):
719720
self.unassigned.remove(msg_id)
720721

721722
header = msg['header']
722-
engine_uuid = header.get('engine', None)
723-
eid = self.by_ident.get(engine_uuid, None)
723+
engine_uuid = header.get('engine', u'')
724+
eid = self.by_ident.get(cast_bytes(engine_uuid), None)
724725

725726
status = header.get('status', None)
726727

@@ -763,7 +764,7 @@ def save_task_destination(self, idents, msg):
763764
# print (content)
764765
msg_id = content['msg_id']
765766
engine_uuid = content['engine_id']
766-
eid = self.by_ident[util.asbytes(engine_uuid)]
767+
eid = self.by_ident[cast_bytes(engine_uuid)]
767768

768769
self.log.info("task::task %r arrived on %r", msg_id, eid)
769770
if msg_id in self.unassigned:
@@ -853,13 +854,13 @@ def register_engine(self, reg, msg):
853854
"""Register a new engine."""
854855
content = msg['content']
855856
try:
856-
queue = util.asbytes(content['queue'])
857+
queue = cast_bytes(content['queue'])
857858
except KeyError:
858859
self.log.error("registration::queue not specified", exc_info=True)
859860
return
860861
heart = content.get('heartbeat', None)
861862
if heart:
862-
heart = util.asbytes(heart)
863+
heart = cast_bytes(heart)
863864
"""register a new engine, and create the socket(s) necessary"""
864865
eid = self._next_id
865866
# print (eid, queue, reg, heart)

IPython/parallel/controller/scheduler.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@
4242
from IPython.config.application import Application
4343
from IPython.config.loader import Config
4444
from IPython.utils.traitlets import Instance, Dict, List, Set, Integer, Enum, CBytes
45+
from IPython.utils.py3compat import cast_bytes
4546

4647
from IPython.parallel import error, util
4748
from IPython.parallel.factory import SessionFactory
48-
from IPython.parallel.util import connect_logger, local_logger, asbytes
49+
from IPython.parallel.util import connect_logger, local_logger
4950

5051
from .dependency import Dependency
5152

@@ -262,7 +263,7 @@ def dispatch_notification(self, msg):
262263
self.log.error("Unhandled message type: %r"%msg_type)
263264
else:
264265
try:
265-
handler(asbytes(msg['content']['queue']))
266+
handler(cast_bytes(msg['content']['queue']))
266267
except Exception:
267268
self.log.error("task::Invalid notification msg: %r", msg, exc_info=True)
268269

@@ -316,7 +317,7 @@ def handle_stranded_tasks(self, engine):
316317
# prevent double-handling of messages
317318
continue
318319

319-
raw_msg = lost[msg_id][0]
320+
raw_msg = lost[msg_id].raw_msg
320321
idents,msg = self.session.feed_identities(raw_msg, copy=False)
321322
parent = self.session.unpack(msg[1].bytes)
322323
idents = [engine, idents[0]]
@@ -370,7 +371,7 @@ def dispatch_submission(self, raw_msg):
370371
# get targets as a set of bytes objects
371372
# from a list of unicode objects
372373
targets = header.get('targets', [])
373-
targets = map(asbytes, targets)
374+
targets = map(cast_bytes, targets)
374375
targets = set(targets)
375376

376377
retries = header.get('retries', 0)

IPython/parallel/engine/engine.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
from IPython.utils.traitlets import (
2828
Instance, Dict, Integer, Type, CFloat, Unicode, CBytes, Bool
2929
)
30-
from IPython.utils import py3compat
30+
from IPython.utils.py3compat import cast_bytes
3131

3232
from IPython.parallel.controller.heartmonitor import Heart
3333
from IPython.parallel.factory import RegistrationFactory
34-
from IPython.parallel.util import disambiguate_url, asbytes
34+
from IPython.parallel.util import disambiguate_url
3535

3636
from IPython.zmq.session import Message
3737
from IPython.zmq.ipkernel import Kernel
@@ -69,7 +69,7 @@ class EngineFactory(RegistrationFactory):
6969
bident = CBytes()
7070
ident = Unicode()
7171
def _ident_changed(self, name, old, new):
72-
self.bident = asbytes(new)
72+
self.bident = cast_bytes(new)
7373
using_ssh=Bool(False)
7474

7575

@@ -194,12 +194,12 @@ def complete_registration(self, msg, connect, maybe_tunnel):
194194
# Redirect input streams and set a display hook.
195195
if self.out_stream_factory:
196196
sys.stdout = self.out_stream_factory(self.session, iopub_socket, u'stdout')
197-
sys.stdout.topic = py3compat.cast_bytes('engine.%i.stdout' % self.id)
197+
sys.stdout.topic = cast_bytes('engine.%i.stdout' % self.id)
198198
sys.stderr = self.out_stream_factory(self.session, iopub_socket, u'stderr')
199-
sys.stderr.topic = py3compat.cast_bytes('engine.%i.stderr' % self.id)
199+
sys.stderr.topic = cast_bytes('engine.%i.stderr' % self.id)
200200
if self.display_hook_factory:
201201
sys.displayhook = self.display_hook_factory(self.session, iopub_socket)
202-
sys.displayhook.topic = py3compat.cast_bytes('engine.%i.pyout' % self.id)
202+
sys.displayhook.topic = cast_bytes('engine.%i.pyout' % self.id)
203203

204204
self.kernel = Kernel(config=self.config, int_id=self.id, ident=self.ident, session=self.session,
205205
control_stream=control_stream, shell_streams=shell_streams, iopub_socket=iopub_socket,

IPython/parallel/util.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,6 @@ def log_errors(f, self, *args, **kwargs):
124124
self.log.error("Uncaught exception in %r" % f, exc_info=True)
125125

126126

127-
def asbytes(s):
128-
"""ensure that an object is ascii bytes"""
129-
if isinstance(s, unicode):
130-
s = s.encode('ascii')
131-
return s
132-
133127
def is_url(url):
134128
"""boolean check for whether a string is a zmq url"""
135129
if '://' not in url:

0 commit comments

Comments
 (0)