From 897828071ac7fed75f6b0f13665a22ef73f7571f 2013-08-10 17:03:10 From: Paul Ivanov Date: 2013-08-10 17:03:10 Subject: [PATCH] Backport PR #3978: fix `--existing` with non-localhost IP there's still some cleanup to do with duplicate code in ConsoleApp that should be invoked from ConnectionFileMixin, but this fixes the bug. closes #3977 --- diff --git a/IPython/consoleapp.py b/IPython/consoleapp.py index 33e6555..26554c4 100644 --- a/IPython/consoleapp.py +++ b/IPython/consoleapp.py @@ -24,7 +24,6 @@ Authors: import atexit import json import os -import shutil import signal import sys import uuid @@ -32,7 +31,6 @@ import uuid # Local imports from IPython.config.application import boolean_flag -from IPython.config.configurable import Configurable from IPython.core.profiledir import ProfileDir from IPython.kernel.blocking import BlockingKernelClient from IPython.kernel import KernelManager @@ -40,7 +38,7 @@ from IPython.kernel import tunnel_to_kernel, find_connection_file, swallow_argv from IPython.utils.path import filefind from IPython.utils.py3compat import str_to_bytes from IPython.utils.traitlets import ( - Dict, List, Unicode, CUnicode, Int, CBool, Any, CaselessStrEnum + Dict, List, Unicode, CUnicode, Int, CBool, Any ) from IPython.kernel.zmq.kernelapp import ( kernel_flags, @@ -49,12 +47,13 @@ from IPython.kernel.zmq.kernelapp import ( ) from IPython.kernel.zmq.session import Session, default_secure from IPython.kernel.zmq.zmqshell import ZMQInteractiveShell +from IPython.kernel.connect import ConnectionFileMixin #----------------------------------------------------------------------------- # Network Constants #----------------------------------------------------------------------------- -from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS +from IPython.utils.localinterfaces import LOCALHOST #----------------------------------------------------------------------------- # Globals @@ -89,8 +88,8 @@ aliases = dict(kernel_aliases) # also scrub aliases from the frontend app_aliases = dict( - ip = 'KernelManager.ip', - transport = 'KernelManager.transport', + ip = 'IPythonConsoleApp.ip', + transport = 'IPythonConsoleApp.transport', hb = 'IPythonConsoleApp.hb_port', shell = 'IPythonConsoleApp.shell_port', iopub = 'IPythonConsoleApp.iopub_port', @@ -120,7 +119,7 @@ except ImportError: else: classes.append(InlineBackend) -class IPythonConsoleApp(Configurable): +class IPythonConsoleApp(ConnectionFileMixin): name = 'ipython-console-mixin' description = """ @@ -254,11 +253,10 @@ class IPythonConsoleApp(Configurable): self.log.debug(u"Loading connection file %s", fname) with open(fname) as f: cfg = json.load(f) + self.transport = cfg.get('transport', 'tcp') + self.ip = cfg.get('ip', LOCALHOST) - self.config.KernelManager.transport = cfg.get('transport', 'tcp') - self.config.KernelManager.ip = cfg.get('ip', LOCALHOST) - - for channel in ('hb', 'shell', 'iopub', 'stdin'): + for channel in ('hb', 'shell', 'iopub', 'stdin', 'control'): name = channel + '_port' if getattr(self, name) == 0 and name in cfg: # not overridden by config or cl_args @@ -272,11 +270,10 @@ class IPythonConsoleApp(Configurable): """set up ssh tunnels, if needed.""" if not self.existing or (not self.sshserver and not self.sshkey): return - self.load_connection_file() - transport = self.config.KernelManager.transport - ip = self.config.KernelManager.ip + transport = self.transport + ip = self.ip if transport != 'tcp': self.log.error("Can only use ssh tunnels with TCP sockets, not %s", transport) @@ -298,7 +295,7 @@ class IPythonConsoleApp(Configurable): self.log.info("Forwarding connections to %s via %s"%(ip, self.sshserver)) # tunnels return a new set of ports, which will be on localhost: - self.config.KernelManager.ip = LOCALHOST + self.ip = LOCALHOST try: newports = tunnel_to_kernel(info, self.sshserver, self.sshkey) except: @@ -337,6 +334,8 @@ class IPythonConsoleApp(Configurable): # Create a KernelManager and start a kernel. self.kernel_manager = self.kernel_manager_class( + ip=self.ip, + transport=self.transport, shell_port=self.shell_port, iopub_port=self.iopub_port, stdin_port=self.stdin_port, @@ -367,6 +366,8 @@ class IPythonConsoleApp(Configurable): self.kernel_client = self.kernel_manager.client() else: self.kernel_client = self.kernel_client_class( + ip=self.ip, + transport=self.transport, shell_port=self.shell_port, iopub_port=self.iopub_port, stdin_port=self.stdin_port, diff --git a/IPython/kernel/client.py b/IPython/kernel/client.py index 81c6160..a0c6a3e 100644 --- a/IPython/kernel/client.py +++ b/IPython/kernel/client.py @@ -147,8 +147,10 @@ class KernelClient(LoggingConfigurable, ConnectionFileMixin): def shell_channel(self): """Get the shell channel object for this kernel.""" if self._shell_channel is None: + url = self._make_url('shell') + self.log.debug("connecting shell channel to %s", url) self._shell_channel = self.shell_channel_class( - self.context, self.session, self._make_url('shell') + self.context, self.session, url ) return self._shell_channel @@ -156,8 +158,10 @@ class KernelClient(LoggingConfigurable, ConnectionFileMixin): def iopub_channel(self): """Get the iopub channel object for this kernel.""" if self._iopub_channel is None: + url = self._make_url('iopub') + self.log.debug("connecting iopub channel to %s", url) self._iopub_channel = self.iopub_channel_class( - self.context, self.session, self._make_url('iopub') + self.context, self.session, url ) return self._iopub_channel @@ -165,8 +169,10 @@ class KernelClient(LoggingConfigurable, ConnectionFileMixin): def stdin_channel(self): """Get the stdin channel object for this kernel.""" if self._stdin_channel is None: + url = self._make_url('stdin') + self.log.debug("connecting stdin channel to %s", url) self._stdin_channel = self.stdin_channel_class( - self.context, self.session, self._make_url('stdin') + self.context, self.session, url ) return self._stdin_channel @@ -174,8 +180,10 @@ class KernelClient(LoggingConfigurable, ConnectionFileMixin): def hb_channel(self): """Get the hb channel object for this kernel.""" if self._hb_channel is None: + url = self._make_url('hb') + self.log.debug("connecting heartbeat channel to %s", url) self._hb_channel = self.hb_channel_class( - self.context, self.session, self._make_url('hb') + self.context, self.session, url ) return self._hb_channel diff --git a/IPython/qt/console/qtconsoleapp.py b/IPython/qt/console/qtconsoleapp.py index a78eb0d..e8ddbf4 100644 --- a/IPython/qt/console/qtconsoleapp.py +++ b/IPython/qt/console/qtconsoleapp.py @@ -249,10 +249,7 @@ class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp): self.app.icon = QtGui.QIcon(icon_path) QtGui.QApplication.setWindowIcon(self.app.icon) - try: - ip = self.config.KernelManager.ip - except AttributeError: - ip = LOCALHOST + ip = self.ip local_kernel = (not self.existing) or ip in LOCAL_IPS self.widget = self.widget_factory(config=self.config, local_kernel=local_kernel)