Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add stdin/stdout/stderr options to kernel launch functions.

  • Loading branch information...
commit 8f2075c2efef8ed4ea28549a9479e75cc6e3a4ab 1 parent 52462c4
@epatters authored
View
35 IPython/zmq/entry_point.py
@@ -23,6 +23,7 @@
from parentpoller import ParentPollerUnix, ParentPollerWindows
from session import Session
+
def bind_port(socket, ip, port):
""" Binds the specified ZMQ socket. If the port is zero, a random port is
chosen. Returns the port that was bound.
@@ -155,6 +156,7 @@ def main():
def base_launch_kernel(code, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
+ stdin=None, stdout=None, stderr=None,
executable=None, independent=False, extra_arguments=[]):
""" Launches a localhost kernel, binding to the specified ports.
@@ -175,6 +177,9 @@ def base_launch_kernel(code, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
hb_port : int, optional
The port to use for the hearbeat REP channel.
+ stdin, stdout, stderr : optional (default None)
+ Standards streams, as defined in subprocess.Popen.
+
executable : str, optional (default sys.executable)
The Python executable to use for the kernel process.
@@ -231,12 +236,19 @@ def base_launch_kernel(code, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
# If using pythonw, stdin, stdout, and stderr are invalid. Popen will
# fail unless they are suitably redirected. We don't read from the
# pipes, but they must exist.
- redirect = PIPE if executable.endswith('pythonw.exe') else None
+ if executable.endswith('pythonw.exe'):
+ redirect = True
+ _stdin = PIPE if stdin is None else stdin
+ _stdout = PIPE if stdout is None else stdout
+ _stderr = PIPE if stderr is None else stderr
+ else:
+ redirect = False
+ _stdin, _stdout, _stderr = stdin, stdout, stderr
if independent:
proc = Popen(arguments,
creationflags=512, # CREATE_NEW_PROCESS_GROUP
- stdout=redirect, stderr=redirect, stdin=redirect)
+ stdin=_stdin, stdout=_stdout, stderr=_stderr)
else:
from _subprocess import DuplicateHandle, GetCurrentProcess, \
DUPLICATE_SAME_ACCESS
@@ -245,21 +257,26 @@ def base_launch_kernel(code, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
True, # Inheritable by new processes.
DUPLICATE_SAME_ACCESS)
proc = Popen(arguments + ['--parent', str(int(handle))],
- stdout=redirect, stderr=redirect, stdin=redirect)
+ stdin=_stdin, stdout=_stdout, stderr=_stderr)
# Attach the interrupt event to the Popen objet so it can be used later.
proc.win32_interrupt_event = interrupt_event
# Clean up pipes created to work around Popen bug.
- if redirect is not None:
- proc.stdout.close()
- proc.stderr.close()
- proc.stdin.close()
+ if redirect:
+ if stdin is None:
+ proc.stdin.close()
+ if stdout is None:
+ proc.stdout.close()
+ if stderr is None:
+ proc.stderr.close()
else:
if independent:
- proc = Popen(arguments, preexec_fn=lambda: os.setsid())
+ proc = Popen(arguments, preexec_fn=lambda: os.setsid(),
+ stdin=stdin, stdout=stdout, stderr=stderr)
else:
- proc = Popen(arguments + ['--parent'])
+ proc = Popen(arguments + ['--parent'],
+ stdin=stdin, stdout=stdout, stderr=stderr)
return proc, xrep_port, pub_port, req_port, hb_port
View
5 IPython/zmq/ipkernel.py
@@ -551,6 +551,7 @@ def start(self):
#-----------------------------------------------------------------------------
def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
+ stdin=None, stdout=None, stderr=None,
executable=None, independent=False, pylab=False, colors=None):
"""Launches a localhost kernel, binding to the specified ports.
@@ -571,6 +572,9 @@ def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
hb_port : int, optional
The port to use for the hearbeat REP channel.
+ stdin, stdout, stderr : optional (default None)
+ Standards streams, as defined in subprocess.Popen.
+
executable : str, optional (default sys.executable)
The Python executable to use for the kernel process.
@@ -608,6 +612,7 @@ def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
extra_arguments.append(colors)
return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
xrep_port, pub_port, req_port, hb_port,
+ stdin, stdout, stderr,
executable, independent, extra_arguments)
View
8 IPython/zmq/pykernel.py
@@ -248,6 +248,7 @@ def _symbol_from_context(self, context):
#-----------------------------------------------------------------------------
def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
+ stdin=None, stdout=None, stderr=None,
executable=None, independent=False):
""" Launches a localhost kernel, binding to the specified ports.
@@ -268,6 +269,9 @@ def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
hb_port : int, optional
The port to use for the hearbeat REP channel.
+ stdin, stdout, stderr : optional (default None)
+ Standards streams, as defined in subprocess.Popen.
+
executable : str, optional (default sys.executable)
The Python executable to use for the kernel process.
@@ -291,8 +295,8 @@ def launch_kernel(ip=None, xrep_port=0, pub_port=0, req_port=0, hb_port=0,
return base_launch_kernel('from IPython.zmq.pykernel import main; main()',
xrep_port, pub_port, req_port, hb_port,
- executable, independent,
- extra_arguments=extra_arguments)
+ stdin, stdout, stderr,
+ executable, independent, extra_arguments)
main = make_default_main(Kernel)
Please sign in to comment.
Something went wrong with that request. Please try again.