~ubuntu-branches/ubuntu/trusty/spyder/trusty-proposed

« back to all changes in this revision

Viewing changes to spyderlib/interpreter.py

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Aubry
  • Date: 2010-06-28 23:43:02 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100628234302-3xnz0gcu0w83282r
Tags: 1.1.1-1
* New upstream release
* New maintainer address (Closes: #586833)
* Build with python 2.6 (Closes: #586824)

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
STDOUT, STDERR = sys.stdout, sys.stderr
18
18
 
19
19
 
20
 
class RollbackImporter:
 
20
def is_module_blacklisted(module, pathlist=[]):
 
21
    """Return True if module path starts with one of the path in pathlist"""
 
22
    modpath = getattr(module, '__file__', None)
 
23
    if modpath is None:
 
24
        # *module* is a C module that is statically linked into the interpreter
 
25
        # There is no way to know its path, so we choose to blacklist it
 
26
        return True
 
27
    for path in [sys.prefix]+pathlist:
 
28
        if modpath.startswith(path):
 
29
            return True
 
30
    else:
 
31
        return False
 
32
 
 
33
class RollbackImporter(object):
21
34
    """
22
35
    Rollback importer is derived from:
23
36
        PyUnit (Steve Purcell)
24
37
        http://pyunit.sourceforge.net
25
38
    """
26
 
    # Blacklisted modules won't be unloaded:
27
 
    BLACKLIST = ('PyQt4', 'spyderlib', 'numpy', 'scipy', 'matplotlib', 'pytz',
28
 
                 'vtk', 'itk', 'wx', 'visual', 'sympy', 'h5py', 'tables',
29
 
                 'guidata', 'guiqwt')
30
39
    def __init__(self):
31
40
        "Creates an instance and installs as the global importer"
32
41
        self.previous_modules = sys.modules.copy()
40
49
        return result
41
50
        
42
51
    def uninstall(self):
 
52
        blacklist = CONF.get('shell', 'rollback_importer/blacklist')
43
53
        for name in self.new_modules:
44
 
            if name not in self.previous_modules \
45
 
               and name.split('.')[0] not in self.BLACKLIST:
 
54
            if name not in self.previous_modules and name in sys.modules \
 
55
               and not is_module_blacklisted(sys.modules[name]) \
 
56
               and not name.split('.')[0] in blacklist:
46
57
                try:
47
58
                    # Force reload when modname next imported
48
59
                    del sys.modules[name]
69
80
        self.namespace = self.locals
70
81
        self.namespace['__name__'] = '__main__'
71
82
        self.namespace['execfile'] = self.execfile
 
83
        self.namespace['runfile'] = self.runfile
72
84
        if rawinputfunc is not None:
73
85
            self.namespace['raw_input'] = rawinputfunc
74
86
            self.namespace['input'] = lambda text='': eval(rawinputfunc(text))
80
92
        if self.rollback_importer is not None:
81
93
            self.rollback_importer.uninstall()
82
94
        
83
 
    def _install_rollback_importer(self):
 
95
    def install_rollback_importer(self):
84
96
        if self.rollback_importer is not None:
85
97
            self.rollback_importer.uninstall()
86
98
        if CONF.get('shell', 'rollback_importer'):
96
108
                name = filename.encode('ascii')
97
109
            except UnicodeEncodeError:
98
110
                name = '<executed_script>'
99
 
            self._install_rollback_importer()
 
111
            self.install_rollback_importer()
100
112
            code = compile(source, name, "exec")
101
113
        except (OverflowError, SyntaxError):
102
114
            InteractiveConsole.showsyntaxerror(self, filename)
103
115
        else:
104
116
            self.runcode(code)
105
117
        
 
118
    def runfile(self, filename, args=None):
 
119
        """
 
120
        Run filename
 
121
        args: command line arguments (string)
 
122
        """
 
123
        if args is not None and not isinstance(args, basestring):
 
124
            raise TypeError("expected a character buffer object")
 
125
        self.namespace['__file__'] = filename
 
126
        sys.argv = [filename]
 
127
        if args is not None:
 
128
            for arg in args.split():
 
129
                sys.argv.append(arg)
 
130
        self.execfile(filename)
 
131
        sys.argv = ['']
 
132
        self.namespace.pop('__file__')
 
133
        
106
134
    def eval(self, text):
107
135
        """
108
136
        Evaluate text and return (obj, valid)