Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fb5b52a9f7
Fetching contributors…

Cannot retrieve contributors at this time

148 lines (129 sloc) 4.957 kb
try:
from itertools import izip as zip
except ImportError:
pass
import inspect
import subprocess
import atexit
import tempfile
import os
import math
import sys
from collections import defaultdict
class Crosstalk(object):
def _exit_handler(self):
self._close()
def _put(self, name, value):
mashaller = self._marshal[type(value).__name__]
cmd = 'put\n%s\n%s\n%s\n' % (
name, type(value).__name__, mashaller(value))
self._proc.stdin.write(cmd)
self._proc.stdin.flush()
def _execute(self, cmd):
cmd = 'execute\n%s\n' % cmd
self._proc.stdin.write(cmd)
self._proc.stdin.flush()
datatype = self._proc.stdout.readline()[:-1]
value_repr = self._proc.stdout.readline()[:-1]
unmashaller = self._unmarshal[datatype]
retval = unmashaller(value_repr)
return retval
def _get(self, name):
cmd = 'get\n%s\n' % name
self._proc.stdin.write(cmd)
self._proc.stdin.flush()
datatype = self._proc.stdout.readline()[:-1]
value_repr = self._proc.stdout.readline()[:-1]
unmashaller = self._unmarshal[datatype]
value = unmashaller(value_repr)
return value
def _clear(self):
cmd = 'clear\n'
self._proc.stdin.write(cmd)
self._proc.stdin.flush()
def _close(self):
cmd = 'close\n'
self._proc.stdin.write(cmd)
self._proc.stdin.flush()
ack = self._proc.stdout.readline()[:-1]
def expose(self, func):
if sys.version_info >= (3, 0):
self._exposed[func.__code__.co_name] = func
return func
else:
def exposed(*args):
for var in zip(func.func_code.co_varnames, args):
self._put(var[0], var[1])
retval = self._execute(func.func_code.co_name)
self._clear()
return retval
return exposed
def run(self):
if sys.version_info >= (3, 0):
while True:
item = sys.stdin.readline()[:-1]
if item == 'execute':
function = sys.stdin.readline()[:-1]
try:
save_fd = os.dup(1)
null_fd = os.open(os.devnull, os.O_RDWR)
os.dup2(null_fd, 1)
retval = self._exposed[function](**self._args)
os.dup2(save_fd, 1)
os.close(null_fd)
datatype = type(retval).__name__
mashaller = self._marshal[datatype]
mashalled = mashaller(retval)
except Exception as e:
self._err.write('cannot exec %s\n' % function)
raise e
self._out.write('%s\n' % datatype)
self._out.write('%s\n' % mashalled)
self._out.flush()
elif item == 'put':
name = sys.stdin.readline()[:-1]
datatype = sys.stdin.readline()[:-1]
value = sys.stdin.readline()[:-1]
unmashaller = self._unmarshal[datatype]
try:
self._args[name] = unmashaller(value)
except Exception as e:
self._err.write('cannot eval %s\n' % value)
raise e
elif item == 'get':
name = sys.stdin.readline()[:-1]
try:
value = args[name]
except Exception as e:
self._err.write('cannot eval %s\n' % name)
raise e
self._out.write('%s\n' % value)
self._out.flush()
elif item == 'clear':
self._args = {}
elif item == 'close':
self._out.write('ack\n')
sys.exit()
def register(self, datatype, marshal, unmarshal):
self._marshal[datatype.__name__] = marshal
self._unmarshal[datatype.__name__] = unmarshal
def __init__(self, interpreter):
self._out = sys.stdout
self._err = sys.stderr
def repr_fac():
return repr
def eval_fac():
return eval
self._marshal = defaultdict(repr_fac)
self._unmarshal = defaultdict(eval_fac)
if sys.version_info >= (3, 0):
sys.stdout = sys.stderr
self._args = {}
self._exposed = {}
else:
script = inspect.getouterframes(inspect.currentframe())[1][1]
self._proc = subprocess.Popen(
[interpreter, script],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
atexit.register(self._exit_handler)
Jump to Line
Something went wrong with that request. Please try again.