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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: dcoles
  • Date: 2008-08-12 12:14:38 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1016
Console: Added a 'inspect' mode to the console to allow a bit of code to be run 
and have the results inspected. (For worksheets)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import cjson
7
7
import codeop
 
8
import cPickle
 
9
import cStringIO
8
10
import md5
9
11
import os
10
12
import Queue
16
18
 
17
19
import common.chat
18
20
 
 
21
# This version must be supported by both the local and remote code
 
22
PICKLEVERSION = 2
 
23
 
19
24
class Interrupt(Exception):
20
25
    def __init__(self):
21
26
        Exception.__init__(self, "Interrupted!")
199
204
                    self.webio.flush()
200
205
                    self.cmdQ.put({"exc": ''.join(tb).decode('utf-8', 'replace')})
201
206
                    self.curr_cmd = ''
 
207
            if 'inspect' in ln:
 
208
                # Like block but return a serialization of the state
 
209
                # throw away a partial command and all state.
 
210
                inspection = {}
 
211
                globs = {}
 
212
                globs['__builtins__'] = globals()['__builtins__']
 
213
                output_buffer = cStringIO.StringIO()
 
214
                try:
 
215
                    cmd = compile(ln['inspect'], "<web session>", 'exec');
 
216
                    #self.execCmd(cmd)
 
217
                    # Begin changes
 
218
                    sys.stdin = None
 
219
                    sys.stdout = output_buffer
 
220
                    sys.stderr = output_buffer
 
221
                    # We don't expect a return value - 'single' symbol prints 
 
222
                    # it.
 
223
                    eval(cmd, globs)
 
224
                except Exception, e:
 
225
                    exception = {}
 
226
                    tb = format_exc_start(start=1)
 
227
                    exception['traceback'] = \
 
228
                        ''.join(tb).decode('utf-8', 'replace')
 
229
                    exception['except'] = cPickle.dumps(e, PICKLEVERSION)
 
230
                    inspection['exception'] = exception                
 
231
                
 
232
                # Write out the inspection object
 
233
                inspection['output'] = output_buffer.getvalue()
 
234
                inspection['globals'] = flatten(globs)
 
235
                self.cmdQ.put({"inspection": inspection})
 
236
                output_buffer.close()
 
237
                self.curr_cmd = ''
 
238
 
202
239
 
203
240
def daemonize():
204
241
    if os.fork():   # launch child and...
332
369
        # Incomplete
333
370
        return count
334
371
 
 
372
# Takes an object and returns a flattened version suitable for JSON
 
373
def flatten(object):
 
374
    flat = {}
 
375
    for o in object:
 
376
        try:
 
377
            flat[o] = cPickle.dumps(object[o], PICKLEVERSION)
 
378
        except:
 
379
            pass
 
380
    return flat
 
381
 
335
382
if __name__ == "__main__":
336
383
    port = int(sys.argv[1])
337
384
    magic = sys.argv[2]