~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: dcoles
  • Date: 2008-08-19 06:44:05 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1029
Tutorial Service: Ported the tutorial service to the console so that all 
student code should now be run inside the jail. This means that the code will 
also be contrained by trampoline's ulimits and will no longer need to run as 
the webserver (with all the badness that entails).

* TestFramework has been modifed to make eqvivalent calls to the console.
* Tutorial service now starts up a console for each attempt.
* Modifications to python-console script and console module to allow new calls 
needed.
* Added a FakeObject class to util to use to represent things that can not be 
pickled. (Ideally should be in the console module, but the console module can't 
be imported into the jail at the moment - relies on a full lib/conf/conf.py).  
Might be possible to make these fake objects able to call the console too?

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from threading import Thread
18
18
 
19
19
import common.chat
 
20
import common.util
20
21
 
21
22
# This version must be supported by both the local and remote code
22
23
PICKLEVERSION = 0
217
218
                        except:
218
219
                            pass
219
220
            elif 'call' in ln:
 
221
                call = {}
 
222
                stdout = cStringIO.StringIO()
 
223
                stderr = cStringIO.StringIO()
 
224
                sys.stdout = stdout
 
225
                sys.stderr = stderr
 
226
 
220
227
                if isinstance(ln['call'], dict):
221
228
                    params = ln['call']
222
229
                    try:
235
242
 
236
243
                        # Run the fuction
237
244
                        function = self.eval(params['function'])
238
 
                        result = function(*args, **kwargs)
239
 
                        self.cmdQ.put({'output': result})
 
245
                        try:
 
246
                            call['result'] = function(*args, **kwargs)
 
247
                        except Exception, e:
 
248
                            exception = {}
 
249
                            tb = format_exc_start(start=1)
 
250
                            exception['traceback'] = \
 
251
                                ''.join(tb).decode('utf-8', 'replace')
 
252
                            exception['except'] = cPickle.dumps(e,
 
253
                                PICKLEVERSION)
 
254
                            call['exception'] = exception
240
255
                    except Exception, e:
241
256
                        tb = format_exc_start(start=1)
242
257
                        self.cmdQ.put(
243
258
                            {"exc": ''.join(tb).decode('utf-8', 'replace')})
 
259
                    
 
260
                    # Write out the inspection object
 
261
                    call['stdout'] = stdout.getvalue()
 
262
                    call['stderr'] = stderr.getvalue()
 
263
                    self.cmdQ.put(call)
244
264
                else:
245
265
                    self.cmdQ.put({'response': 'failure'})
 
266
                stdout.close()
 
267
                stderr.close()
 
268
                self.curr_cmd = ''
246
269
            elif 'inspect' in ln:
247
270
                # Like block but return a serialization of the state
248
271
                # throw away partial command
273
296
                stdout.close()
274
297
                stderr.close()
275
298
                self.curr_cmd = ''
 
299
            elif 'set_vars' in ln:
 
300
                # Adds some variables to the global dictionary
 
301
                for var in ln['set_vars']:
 
302
                    try:
 
303
                        self.globs[var] = self.eval(ln['set_vars'][var])
 
304
                    except Exception, e:
 
305
                        tb = format_exc_start(start=1)
 
306
                        self.cmdQ.put(
 
307
                            {"exc": ''.join(tb).decode('utf-8', 'replace')})
 
308
 
 
309
                self.cmdQ.put({'response': 'okay'})
276
310
            else:
277
311
                raise Exception, "Invalid Command"
278
312
 
418
452
    for o in object:
419
453
        try:
420
454
            flat[o] = cPickle.dumps(object[o], PICKLEVERSION)
421
 
        except:
422
 
            pass
 
455
        except TypeError:
 
456
            try:
 
457
                o_type = type(object[o]).__name__
 
458
                o_name = object[o].__name__
 
459
                fake_o = common.util.FakeObject(o_type, o_name)
 
460
                flat[o] = cPickle.dumps(fake_o, PICKLEVERSION)
 
461
            except AttributeError:
 
462
                pass
423
463
    return flat
424
464
 
425
465
if __name__ == "__main__":