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

« back to all changes in this revision

Viewing changes to services/python-console

  • Committer: David Coles
  • Date: 2010-08-30 03:26:13 UTC
  • Revision ID: coles.david@gmail.com-20100830032613-d14vng0jkelniu3l
python-console: Fix globals broken with new JSON library.

simplejson always returns unicode strings. cJSON would return ordinary strings 
if possible. cPickle.loads() only accepts strings. At present we use pickle 
version 0 so they should all works as ASCII strings. Higher versions of pickle 
are not plain ASCII and are likely to break this and so this should be fixed 
at some point.

Also replaced unconditional exception with one that catches Pickle errors. Not 
sure the best way to report failures of these functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# usage:
4
4
#   python-console <port> <magic> [<working-dir>]
5
5
 
6
 
import cjson
7
6
import codeop
8
7
import cPickle
9
8
import cStringIO
10
9
import md5
11
 
import os
12
10
import Queue
13
11
import signal
14
12
import socket
171
169
 
172
170
    def run(self):
173
171
        # Set up global space and partial command buffer
174
 
        self.globs = {}
 
172
        self.globs = {'__name__': '__main__'}
175
173
        self.curr_cmd = ''
176
174
 
177
175
        # Set up I/O to use web interface
181
179
 
182
180
        # Handlers for each action
183
181
        actions = {
 
182
            'splash': self.handle_splash,
184
183
            'chat': self.handle_chat,
185
184
            'block': self.handle_block,
186
185
            'globals': self.handle_globals,
198
197
                response = {'error': repr(e)}
199
198
            finally:
200
199
                self.cmdQ.put(response)
201
 
                   
 
200
 
 
201
    def handle_splash(self, params):
 
202
        # Initial console splash screen
 
203
        python_version = '.'.join(str(v) for v in sys.version_info[:3])
 
204
        splash_text = ("""IVLE %s Python Console (Python %s)
 
205
Type "help", "copyright", "credits" or "license" for more information.
 
206
""" % (ivle.__version__, python_version))
 
207
        return {'output': splash_text}
 
208
 
202
209
    def handle_chat(self, params):
203
210
        # Set up the partial cmd buffer
204
211
        if self.curr_cmd == '':
254
261
    def handle_globals(self, params):
255
262
        # Unpickle the new space (if provided)
256
263
        if isinstance(params, dict):
257
 
            self.globs = {}
 
264
            self.globs = {'__name__': '__main__'}
258
265
            for g in params:
259
266
                try:
260
 
                    self.globs[g] = cPickle.loads(params[g])
261
 
                except:
 
267
                    self.globs[g] = cPickle.loads(str(params[g]))
 
268
                except cPickle.UnpicklingError:
262
269
                    pass
263
270
 
264
271
        # Return the current globals
369
376
    """Handles response from signals"""
370
377
    global terminate
371
378
    if signum == signal.SIGXCPU:
372
 
        terminate = "CPU Time Limit Exceeded"
 
379
        terminate = "CPU time limit exceeded"
373
380
 
374
381
def dispatch_msg(msg):
375
382
    global terminate
376
383
    if msg['cmd'] == 'terminate':
377
 
        terminate = "User requested console be terminated"
 
384
        terminate = "User requested restart"
378
385
    if terminate:
379
386
        raise ivle.chat.Terminate({"terminate":terminate})
380
387
    expiry.ping()
399
406
    for o in object:
400
407
        try:
401
408
            flat[o] = cPickle.dumps(object[o], PICKLEVERSION)
402
 
        except TypeError:
 
409
        except (TypeError, cPickle.PicklingError):
403
410
            try:
404
411
                o_type = type(object[o]).__name__
405
412
                o_name = object[o].__name__
412
419
if __name__ == "__main__":
413
420
    port = int(sys.argv[1])
414
421
    magic = sys.argv[2]
415
 
    
416
 
    # Sanitise the Enviroment
417
 
    os.environ = {}
418
 
    os.environ['PATH'] = '/usr/local/bin:/usr/bin:/bin'
419
 
 
420
 
    if len(sys.argv) >= 4:
421
 
        # working_dir
422
 
        os.chdir(sys.argv[3])
423
 
        os.environ['HOME'] = sys.argv[3]
424
422
 
425
423
    # Make python's search path follow the cwd
426
424
    sys.path[0] = ''