From 4e57d823f5422605c45d5614f130fe8350a70a2c 2014-04-17 23:15:33 From: Thomas Kluyver Date: 2014-04-17 23:15:33 Subject: [PATCH] Add mechanism to specify environment variables in kernel spec --- diff --git a/IPython/kernel/kernelspec.py b/IPython/kernel/kernelspec.py index 386e14b..ab84108 100644 --- a/IPython/kernel/kernelspec.py +++ b/IPython/kernel/kernelspec.py @@ -7,7 +7,7 @@ pjoin = os.path.join from IPython.utils.path import get_ipython_dir from IPython.utils.py3compat import PY3 -from IPython.utils.traitlets import HasTraits, List, Unicode +from IPython.utils.traitlets import HasTraits, List, Unicode, Dict USER_KERNEL_DIR = pjoin(get_ipython_dir(), 'kernels') @@ -33,6 +33,7 @@ class KernelSpec(HasTraits): display_name = Unicode() language = Unicode() codemirror_mode = None + env = Dict() resource_dir = Unicode() diff --git a/IPython/kernel/launcher.py b/IPython/kernel/launcher.py index dcf7fe1..379555f 100644 --- a/IPython/kernel/launcher.py +++ b/IPython/kernel/launcher.py @@ -1,21 +1,8 @@ """Utilities for launching kernels - -Authors: - -* Min Ragan-Kelley - """ -#----------------------------------------------------------------------------- -# Copyright (C) 2013 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. import os import sys @@ -24,9 +11,6 @@ from subprocess import Popen, PIPE from IPython.utils.encoding import getdefaultencoding from IPython.utils.py3compat import cast_bytes_py2 -#----------------------------------------------------------------------------- -# Launching Kernels -#----------------------------------------------------------------------------- def swallow_argv(argv, aliases=None, flags=None): """strip frontend-specific aliases and flags from an argument list @@ -136,7 +120,7 @@ def make_ipkernel_cmd(code, executable=None, extra_arguments=[], **kw): return arguments -def launch_kernel(cmd, stdin=None, stdout=None, stderr=None, +def launch_kernel(cmd, stdin=None, stdout=None, stderr=None, env=None, independent=False, cwd=None, ipython_kernel=True, **kw @@ -221,7 +205,7 @@ def launch_kernel(cmd, stdin=None, stdout=None, stderr=None, if independent: proc = Popen(cmd, creationflags=512, # CREATE_NEW_PROCESS_GROUP - stdin=_stdin, stdout=_stdout, stderr=_stderr, env=os.environ) + stdin=_stdin, stdout=_stdout, stderr=_stderr, env=env) else: if ipython_kernel: try: @@ -238,7 +222,7 @@ def launch_kernel(cmd, stdin=None, stdout=None, stderr=None, proc = Popen(cmd, - stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=os.environ) + stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=env) # Attach the interrupt event to the Popen objet so it can be used later. proc.win32_interrupt_event = interrupt_event @@ -246,12 +230,12 @@ def launch_kernel(cmd, stdin=None, stdout=None, stderr=None, else: if independent: proc = Popen(cmd, preexec_fn=lambda: os.setsid(), - stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=os.environ) + stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=env) else: if ipython_kernel: cmd += ['--parent=1'] proc = Popen(cmd, - stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=os.environ) + stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=env) # Clean up pipes created to work around Popen bug. if redirect_in: diff --git a/IPython/kernel/manager.py b/IPython/kernel/manager.py index 181b035..bb94b29 100644 --- a/IPython/kernel/manager.py +++ b/IPython/kernel/manager.py @@ -1,19 +1,12 @@ """Base class to manage a running kernel""" -#----------------------------------------------------------------------------- -# Copyright (C) 2013 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. from __future__ import absolute_import # Standard library imports +import os import re import signal import sys @@ -40,9 +33,6 @@ from .managerabc import ( KernelManagerABC ) -#----------------------------------------------------------------------------- -# Main kernel manager class -#----------------------------------------------------------------------------- class KernelManager(LoggingConfigurable, ConnectionFileMixin): """Manages a single kernel in a subprocess on this host. @@ -232,8 +222,15 @@ class KernelManager(LoggingConfigurable, ConnectionFileMixin): self._launch_args = kw.copy() # build the Popen cmd kernel_cmd = self.format_kernel_cmd(**kw) + if self.kernel_cmd: + # If kernel_cmd has been set manually, don't refer to a kernel spec + env = os.environ + else: + # Environment variables from kernel spec are added to os.environ + env = os.environ.copy() + env.update(self.kernel_spec.env or {}) # launch the kernel subprocess - self.kernel = self._launch_kernel(kernel_cmd, + self.kernel = self._launch_kernel(kernel_cmd, env=env, ipython_kernel=self.ipython_kernel, **kw) self.start_restarter() @@ -402,9 +399,5 @@ class KernelManager(LoggingConfigurable, ConnectionFileMixin): return False -#----------------------------------------------------------------------------- -# ABC Registration -#----------------------------------------------------------------------------- - KernelManagerABC.register(KernelManager)