From 40e9f497c4b067dadd1ce47c8b21805ddbb772e0 2012-09-11 16:49:13 From: epatters Date: 2012-09-11 16:49:13 Subject: [PATCH] Add abstract base class (ABC) for sockets used in kernel. --- diff --git a/IPython/embedded/socket.py b/IPython/embedded/socket.py index db8ba36..464b327 100644 --- a/IPython/embedded/socket.py +++ b/IPython/embedded/socket.py @@ -12,6 +12,7 @@ #----------------------------------------------------------------------------- # Standard library imports. +import abc import Queue # System library imports. @@ -21,6 +22,23 @@ import zmq from IPython.utils.traitlets import HasTraits, Instance, Int #----------------------------------------------------------------------------- +# Generic socket interface +#----------------------------------------------------------------------------- + +class SocketABC(object): + __metaclass__ = abc.ABCMeta + + @abc.abstractmethod + def recv_multipart(self, flags=0, copy=True, track=False): + raise NotImplementedError + + @abc.abstractmethod + def send_multipart(self, msg_parts, flags=0, copy=True, track=False): + raise NotImplementedError + +SocketABC.register(zmq.Socket) + +#----------------------------------------------------------------------------- # Dummy socket class #----------------------------------------------------------------------------- @@ -31,7 +49,7 @@ class DummySocket(HasTraits): message_sent = Int(0) # Should be an Event #------------------------------------------------------------------------- - # zmq.Socket interface + # Socket interface #------------------------------------------------------------------------- def recv_multipart(self, flags=0, copy=True, track=False): @@ -41,3 +59,5 @@ class DummySocket(HasTraits): msg_parts = map(zmq.Message, msg_parts) self.queue.put_nowait(msg_parts) self.message_sent += 1 + +SocketABC.register(DummySocket) diff --git a/IPython/zmq/datapub.py b/IPython/zmq/datapub.py index 95800ee..e4da903 100644 --- a/IPython/zmq/datapub.py +++ b/IPython/zmq/datapub.py @@ -13,10 +13,9 @@ #----------------------------------------------------------------------------- from IPython.config import Configurable - +from IPython.embedded.socket import SocketABC from IPython.utils.jsonutil import json_clean -from IPython.utils.traitlets import Any, Instance, Dict, CBytes - +from IPython.utils.traitlets import Instance, Dict, CBytes from IPython.zmq.serialize import serialize_object from IPython.zmq.session import Session, extract_header @@ -29,7 +28,7 @@ class ZMQDataPublisher(Configurable): topic = topic = CBytes(b'datapub') session = Instance(Session) - pub_socket = Any() + pub_socket = Instance(SocketABC) parent_header = Dict({}) def set_parent(self, parent): diff --git a/IPython/zmq/displayhook.py b/IPython/zmq/displayhook.py index 16b4791..2b2b50f 100644 --- a/IPython/zmq/displayhook.py +++ b/IPython/zmq/displayhook.py @@ -2,8 +2,9 @@ import __builtin__ import sys from IPython.core.displayhook import DisplayHook +from IPython.embedded.socket import SocketABC from IPython.utils.jsonutil import encode_images -from IPython.utils.traitlets import Any, Instance, Dict +from IPython.utils.traitlets import Instance, Dict from session import extract_header, Session class ZMQDisplayHook(object): @@ -37,7 +38,7 @@ class ZMQShellDisplayHook(DisplayHook): topic=None session = Instance(Session) - pub_socket = Any() + pub_socket = Instance(SocketABC) parent_header = Dict({}) def set_parent(self, parent): diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py index 06256b1..fd03bdc 100644 --- a/IPython/zmq/zmqshell.py +++ b/IPython/zmq/zmqshell.py @@ -34,6 +34,7 @@ from IPython.core.error import UsageError from IPython.core.magics import MacroToEdit, CodeMagics from IPython.core.magic import magics_class, line_magic, Magics from IPython.core.payloadpage import install_payload_page +from IPython.embedded.socket import SocketABC from IPython.lib.kernel import ( get_connection_file, get_connection_info, connect_qtconsole ) @@ -42,7 +43,7 @@ from IPython.utils import io, openpy from IPython.utils.jsonutil import json_clean, encode_images from IPython.utils.process import arg_split from IPython.utils import py3compat -from IPython.utils.traitlets import Any, Instance, Type, Dict, CBool, CBytes +from IPython.utils.traitlets import Instance, Type, Dict, CBool, CBytes from IPython.utils.warn import warn, error from IPython.zmq.displayhook import ZMQShellDisplayHook from IPython.zmq.datapub import ZMQDataPublisher @@ -57,7 +58,7 @@ class ZMQDisplayPublisher(DisplayPublisher): """A display publisher that publishes data using a ZeroMQ PUB socket.""" session = Instance(Session) - pub_socket = Any() + pub_socket = Instance(SocketABC) parent_header = Dict({}) topic = CBytes(b'displaypub')