Example #1
0
def redirected_stdio(conn):
    r"""
	Redirects the other party's ``stdin``, ``stdout`` and ``stderr`` to 
	those of the local party, so remote IO will occur locally.

	Example usage::
	
		with redirected_stdio(conn):
			conn.modules.sys.stdout.write("hello\n")   # will be printed locally
	
	"""
    orig_stdin = conn.modules.sys.stdin
    orig_stdout = conn.modules.sys.stdout
    orig_stderr = conn.modules.sys.stderr
    try:
        conn.modules.sys.stdin = restricted(
            sys.stdin, ["softspace", "write", "readline", "encoding", "close"])
        conn.modules.sys.stdout = restricted(
            sys.stdout,
            ["softspace", "write", "readline", "encoding", "close", "flush"])
        conn.modules.sys.stderr = restricted(
            sys.stderr,
            ["softspace", "write", "readline", "encoding", "close", "flush"])
        yield
    finally:
        conn.modules.sys.stdin = orig_stdin
        conn.modules.sys.stdout = orig_stdout
        conn.modules.sys.stderr = orig_stderr
Example #2
0
def redirected_stdio(module, stdout=None, stderr=None):
    r"""
    Redirects the other party's ``stdin``, ``stdout`` and ``stderr`` to
    those of the local party, so remote IO will occur locally.

    Example usage::

        with redirected_stdio(conn):
            conn.modules.sys.stdout.write("hello\n")   # will be printed locally

    """
    conn = module.client.conn
    orig_stdin = conn.modules.sys.stdin
    orig_stdout = conn.modules.sys.stdout
    orig_stderr = conn.modules.sys.stderr

    if stdout is None:
        stdout = module.stdout
    if stderr is None:
        stderr = module.stdout

    try:
        conn.modules.sys.stdin = restricted(sys.stdin, ["softspace", "write", "readline", "encoding", "close"])
        conn.modules.sys.stdout = restricted(stdout, ["softspace", "write", "readline", "encoding", "close", "flush"])
        conn.modules.sys.stderr = restricted(stderr, ["softspace", "write", "readline", "encoding", "close", "flush"])
        yield
    finally:
        conn.modules.sys.stdin = orig_stdin
        conn.modules.sys.stdout = orig_stdout
        conn.modules.sys.stderr = orig_stderr
def redirected_stdo(conn, stdout, stderr):
	orig_stdout = conn.modules.sys.stdout
	orig_stderr = conn.modules.sys.stderr
	try:
		conn.modules.sys.stdout = restricted(stdout,["softspace", "write", "flush"])
		conn.modules.sys.stderr = restricted(stderr,["softspace", "write", "flush"])
		yield
	finally:
		conn.modules.sys.stdout = orig_stdout
		conn.modules.sys.stderr = orig_stderr
Example #4
0
def redirected_stdo(conn, stdout, stderr):
    orig_stdout = conn.modules.sys.stdout
    orig_stderr = conn.modules.sys.stderr
    try:
        conn.modules.sys.stdout = restricted(stdout,
                                             ["softspace", "write", "flush"])
        conn.modules.sys.stderr = restricted(stderr,
                                             ["softspace", "write", "flush"])
        yield
    finally:
        conn.modules.sys.stdout = orig_stdout
        conn.modules.sys.stderr = orig_stderr
Example #5
0
def redirected_stdo(conn, stdout=None, stderr=None):
	if stdout is None:
		stdout=sys.stdout
	if stderr is None:
		stderr=sys.stderr
	orig_stdout = conn.modules.sys.stdout
	orig_stderr = conn.modules.sys.stderr
	try:
		conn.modules.sys.stdout = restricted(stdout,["softspace", "write", "flush"])
		conn.modules.sys.stderr = restricted(stderr,["softspace", "write", "flush"])
		yield
	finally:
		conn.modules.sys.stdout = orig_stdout
		conn.modules.sys.stderr = orig_stderr
Example #6
0
def redirected_stdo(conn, stdout=None, stderr=None):
    if stdout is None:
        stdout = sys.stdout
    if stderr is None:
        stderr = sys.stderr
    orig_stdout = conn.modules.sys.stdout
    orig_stderr = conn.modules.sys.stderr
    try:
        conn.modules.sys.stdout = restricted(stdout,
                                             ["softspace", "write", "flush"])
        conn.modules.sys.stderr = restricted(stderr,
                                             ["softspace", "write", "flush"])
        yield
    finally:
        conn.modules.sys.stdout = orig_stdout
        conn.modules.sys.stderr = orig_stderr
Example #7
0
def redirected_stdo(module, stdout=None, stderr=None):
    conn = module.client.conn
    if stdout is None:
        stdout = module.stdout
    if stderr is None:
        stderr = module.stdout

    hotpatch_oswrite(conn)
    orig_stdout = conn.modules.sys.stdout
    orig_stderr = conn.modules.sys.stderr
    try:
        conn.modules.sys.stdout = restricted(stdout,["softspace", "write", "flush"])
        conn.modules.sys.stderr = restricted(stderr,["softspace", "write", "flush"])
        yield
    finally:
        conn.modules.sys.stdout = orig_stdout
        conn.modules.sys.stderr = orig_stderr
Example #8
0
def redirected_stdo(module, stdout=None, stderr=None):
    conn = module.client.conn
    if stdout is None:
        stdout = module.stdout
    if stderr is None:
        stderr = module.stdout

    hotpatch_oswrite(conn)
    orig_stdout = conn.modules.sys.stdout
    orig_stderr = conn.modules.sys.stderr
    try:
        conn.modules.sys.stdout = restricted(stdout,
                                             ["softspace", "write", "flush"])
        conn.modules.sys.stderr = restricted(stderr,
                                             ["softspace", "write", "flush"])
        yield
    finally:
        conn.modules.sys.stdout = orig_stdout
        conn.modules.sys.stderr = orig_stderr
Example #9
0
def redirected_stdo(module, stdout=None, stderr=None):
    ns = module.client.conn.namespace
    if stdout is None:
        stdout = module.stdout
    if stderr is None:
        stderr = module.stdout

    try:
        ns['redirect_stdo'](restricted(stdout,
                                       ['softspace', 'write', 'flush']),
                            restricted(stderr,
                                       ['softspace', 'write', 'flush']))

        module.client.conn.register_remote_cleanup(ns['reset_stdo'])

        yield

    finally:
        ns['reset_stdo']()
        module.client.conn.unregister_remote_cleanup(ns['reset_stdo'])
Example #10
0
def redirected_stdio(module, stdout=None, stderr=None):
    r"""
    Redirects the other party's ``stdin``, ``stdout`` and ``stderr`` to
    those of the local party, so remote IO will occur locally.

    Example usage::

        with redirected_stdio(conn):
            conn.modules.sys.stdout.write("hello\n")   # will be printed locally

    """

    ns = module.client.conn.namespace

    stdin = sys.stdin

    if stdout is None:
        stdout = module.stdout
    if stderr is None:
        stderr = module.stdout

    try:
        ns['redirect_stdio'](
            restricted(
                stdin,
                ['softspace', 'write', 'readline', 'encoding', 'close']),
            restricted(
                stdout,
                ['softspace', 'write', 'readline', 'encoding', 'close']),
            restricted(
                stderr,
                ['softspace', 'write', 'readline', 'encoding', 'close']))

        module.client.conn.register_remote_cleanup(ns['reset_stdio'])

        yield

    finally:
        ns['reset_stdio']()
        module.client.conn.unregister_remote_cleanup(ns['reset_stdio'])