File: pythoncontext.py

package info (click to toggle)
freej 0.10git20100110-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 32,080 kB
  • ctags: 22,705
  • sloc: cpp: 156,254; ansic: 25,531; sh: 13,538; perl: 4,624; makefile: 3,278; python: 2,889; objc: 1,284; asm: 1,125; ruby: 126
file content (60 lines) | stat: -rw-r--r-- 1,970 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sys
import traceback

class PythonExecutionContext(object):
    """
    A class representing an independent execution context.
    It can store global symbols for the scripts executed herein, and
    has functions to run and evaluate python code.
    """
    def __init__(self):
        self._exec_dict = {}

    def __setitem__(self,name,value):
        """
        Set a symbol into the execution context dictionary.
        """
        self._exec_dict[name] = value

    def __getitem__(self,name):
        """
        Get a symbol from the execution context dictionary.
        """
        return self._exec_dict[name]

    def __contains__(self,name):
        """
        See if the execution context holds some symbol.
        """
        return name in self._exec_dict

    def RunFromFile(self,filename):
        """
        Run a script into this execution context directly from a file.
        """
        def printerr(error):
            print " * "+filename+": "+error
        f = open(filename,"r")
        self.RunUserCode(f.read(),printerr)
        f.close()

    def RunUserCode(self, script, printfunc,mode="single"):
        """
        Run some text (python code) into this execution context.
        """
        assigned_object = None # if we make a print we also return the printed eval
        # Try and run the user entered line(s)
        try:
                if script.startswith("print"):
                     try:
                         assigned_object = eval(script[5:].strip(),self._exec_dict)
                     except:
                         traceback.print_exc()
                exec(script,self._exec_dict)
        except: # Prints the REAL exception.
                error = '%s:  %s' % (sys.exc_type.__name__, sys.exc_value)
                for errorLine in error.split('\n'):
                        printfunc(errorLine+"\n") # new line to type into
                #traceback.print_exc()
        return assigned_object