205
204
self.webio.flush()
206
205
self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
207
206
self.curr_cmd = ''
211
self.globs['__builtins__'] = globals()['__builtins__']
212
self.cmdQ.put({'response': 'okay'})
207
elif 'globals' in ln:
213
208
# Unpickle the new space (if provided)
214
if isinstance(ln['flush'],dict):
215
for g in ln['flush']:
209
if isinstance(ln['globals'],dict):
211
for g in ln['globals']:
217
self.globs[g] = cPickle.loads(ln['flush'][g])
213
self.globs[g] = cPickle.loads(ln['globals'][g])
217
# Return the current globals
218
self.cmdQ.put({'globals': flatten(self.globs)})
220
219
elif 'call' in ln:
222
stdout = cStringIO.StringIO()
223
stderr = cStringIO.StringIO()
221
sys.stdin = self.webio
222
sys.stdout = self.webio
223
sys.stderr = self.webio
227
225
if isinstance(ln['call'], dict):
228
226
params = ln['call']
258
256
{"exc": ''.join(tb).decode('utf-8', 'replace')})
260
258
# Write out the inspection object
261
call['stdout'] = stdout.getvalue()
262
call['stderr'] = stderr.getvalue()
263
259
self.cmdQ.put(call)
265
261
self.cmdQ.put({'response': 'failure'})
268
262
self.curr_cmd = ''
269
elif 'inspect' in ln:
263
elif 'execute' in ln:
270
264
# Like block but return a serialization of the state
271
265
# throw away partial command
273
stdout = cStringIO.StringIO()
274
stderr = cStringIO.StringIO()
266
response = {'okay': None}
267
sys.stdin = self.webio
268
sys.stdout = self.webio
269
sys.stderr = self.webio
276
cmd = compile(ln['inspect'], "<web session>", 'exec');
271
cmd = compile(ln['execute'], "<web session>", 'exec');
280
272
# We don't expect a return value - 'single' symbol prints
283
275
except Exception, e:
285
tb = format_exc_start(start=1)
286
exception['traceback'] = \
287
''.join(tb).decode('utf-8', 'replace')
288
exception['except'] = cPickle.dumps(e, PICKLEVERSION)
289
inspection['exception'] = exception
276
response = {'exception': cPickle.dumps(e, PICKLEVERSION)}
291
# Write out the inspection object
292
inspection['stdout'] = stdout.getvalue()
293
inspection['stderr'] = stderr.getvalue()
294
inspection['globals'] = flatten(self.globs)
295
self.cmdQ.put(inspection)
282
# Return the inspection object
283
self.cmdQ.put(response)
285
# Clear any previous command
298
286
self.curr_cmd = ''
299
287
elif 'set_vars' in ln:
300
288
# Adds some variables to the global dictionary
307
295
{"exc": ''.join(tb).decode('utf-8', 'replace')})
309
self.cmdQ.put({'response': 'okay'})
297
self.cmdQ.put({'okay': None})
311
299
raise Exception, "Invalid Command"
350
338
def dispatch_msg(msg):
352
if msg['cmd'] == 'restart':
353
terminate = "User requested console be reset"
340
if msg['cmd'] == 'terminate':
341
terminate = "User requested console be terminated"
355
raise common.chat.Terminate({"restart":terminate})
343
raise common.chat.Terminate({"terminate":terminate})
357
345
lineQ.put({msg['cmd']:msg['text']})
359
raise common.chat.Terminate({"restart":terminate})
347
raise common.chat.Terminate({"terminate":terminate})
360
348
return cmdQ.get()
362
350
def format_exc_start(start=0):