/usr/share/pyshared/spyderlib/interpreter.py is in python-spyderlib 2.2.5+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | # -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""Shell Interpreter"""
import sys
import atexit
import threading
import ctypes
import os
import re
import os.path as osp
import pydoc
from subprocess import Popen, PIPE
from code import InteractiveConsole
# Local imports:
from spyderlib.utils.dochelpers import isdefined
from spyderlib.utils import encoding
from spyderlib.utils.misc import remove_backslashes
# Force Python to search modules in the current directory first:
sys.path.insert(0, '')
def guess_filename(filename):
"""Guess filename"""
if osp.isfile(filename):
return filename
if not filename.endswith('.py'):
filename += '.py'
for path in [os.getcwdu()]+sys.path:
fname = osp.join(path, filename)
if osp.isfile(fname):
return fname
elif osp.isfile(fname+'.py'):
return fname+'.py'
elif osp.isfile(fname+'.pyw'):
return fname+'.pyw'
return filename
class Interpreter(InteractiveConsole, threading.Thread):
"""Interpreter, executed in a separate thread"""
p1 = ">>> "
p2 = "... "
def __init__(self, namespace=None, exitfunc=None,
Output=None, WidgetProxy=None, debug=False):
"""
namespace: locals send to InteractiveConsole object
commands: list of commands executed at startup
"""
InteractiveConsole.__init__(self, namespace)
threading.Thread.__init__(self)
self._id = None
self.exit_flag = False
self.debug = debug
# Execution Status
self.more = False
if exitfunc is not None:
atexit.register(exitfunc)
self.namespace = self.locals
self.namespace['__name__'] = '__main__'
self.namespace['execfile'] = self.execfile
self.namespace['runfile'] = self.runfile
self.namespace['raw_input'] = self.raw_input_replacement
self.namespace['help'] = self.help_replacement
# Capture all interactive input/output
self.initial_stdout = sys.stdout
self.initial_stderr = sys.stderr
self.initial_stdin = sys.stdin
# Create communication pipes
pr, pw = os.pipe()
self.stdin_read = os.fdopen(pr, "r")
self.stdin_write = os.fdopen(pw, "w", 0)
self.stdout_write = Output()
self.stderr_write = Output()
self.input_condition = threading.Condition()
self.widget_proxy = WidgetProxy(self.input_condition)
self.redirect_stds()
#------ Standard input/output
def redirect_stds(self):
"""Redirects stds"""
if not self.debug:
sys.stdout = self.stdout_write
sys.stderr = self.stderr_write
sys.stdin = self.stdin_read
def restore_stds(self):
"""Restore stds"""
if not self.debug:
sys.stdout = self.initial_stdout
sys.stderr = self.initial_stderr
sys.stdin = self.initial_stdin
def raw_input_replacement(self, prompt=''):
"""For raw_input builtin function emulation"""
self.widget_proxy.wait_input(prompt)
self.input_condition.acquire()
while not self.widget_proxy.data_available():
self.input_condition.wait()
inp = self.widget_proxy.input_data
self.input_condition.release()
return inp
def help_replacement(self, text=None, interactive=False):
"""For help builtin function emulation"""
if text is not None and not interactive:
return pydoc.help(text)
elif text is None:
pyver = "%d.%d" % (sys.version_info[0], sys.version_info[1])
self.write("""
Welcome to Python %s! This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://www.python.org/doc/tut/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
""" % pyver)
else:
text = text.strip()
try:
eval("pydoc.help(%s)" % text)
except (NameError, SyntaxError):
print "no Python documentation found for '%r'" % text
self.write(os.linesep)
self.widget_proxy.new_prompt("help> ")
inp = self.raw_input_replacement()
if inp.strip():
self.help_replacement(inp, interactive=True)
else:
self.write("""
You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)". Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
""")
def run_command(self, cmd, new_prompt=True):
"""Run command in interpreter"""
if cmd == 'exit()':
self.exit_flag = True
self.write('\n')
return
# -- Special commands type I
# (transformed into commands executed in the interpreter)
# ? command
special_pattern = r"^%s (?:r\')?(?:u\')?\"?\'?([a-zA-Z0-9_\.]+)"
run_match = re.match(special_pattern % 'run', cmd)
help_match = re.match(r'^([a-zA-Z0-9_\.]+)\?$', cmd)
cd_match = re.match(r"^\!cd \"?\'?([a-zA-Z0-9_ \.]+)", cmd)
if help_match:
cmd = 'help(%s)' % help_match.group(1)
# run command
elif run_match:
filename = guess_filename(run_match.groups()[0])
cmd = "runfile('%s', args=None)" % remove_backslashes(filename)
# !cd system command
elif cd_match:
cmd = 'import os; os.chdir(r"%s")' % cd_match.groups()[0].strip()
# -- End of Special commands type I
# -- Special commands type II
# (don't need code execution in interpreter)
xedit_match = re.match(special_pattern % 'xedit', cmd)
edit_match = re.match(special_pattern % 'edit', cmd)
clear_match = re.match(r"^clear ([a-zA-Z0-9_, ]+)", cmd)
# (external) edit command
if xedit_match:
filename = guess_filename(xedit_match.groups()[0])
self.widget_proxy.edit(filename, external_editor=True)
# local edit command
elif edit_match:
filename = guess_filename(edit_match.groups()[0])
if osp.isfile(filename):
self.widget_proxy.edit(filename)
else:
self.stderr_write.write(
"No such file or directory: %s\n" % filename)
# remove reference (equivalent to MATLAB's clear command)
elif clear_match:
varnames = clear_match.groups()[0].replace(' ', '').split(',')
for varname in varnames:
try:
self.namespace.pop(varname)
except KeyError:
pass
# Execute command
elif cmd.startswith('!'):
# System ! command
pipe = Popen(cmd[1:], shell=True,
stdin=PIPE, stderr=PIPE, stdout=PIPE)
txt_out = encoding.transcode( pipe.stdout.read() )
txt_err = encoding.transcode( pipe.stderr.read().rstrip() )
if txt_err:
self.stderr_write.write(txt_err)
if txt_out:
self.stdout_write.write(txt_out)
self.stdout_write.write('\n')
self.more = False
# -- End of Special commands type II
else:
# Command executed in the interpreter
# self.widget_proxy.set_readonly(True)
self.more = self.push(cmd)
# self.widget_proxy.set_readonly(False)
if new_prompt:
self.widget_proxy.new_prompt(self.p2 if self.more else self.p1)
if not self.more:
self.resetbuffer()
def run(self):
"""Wait for input and run it"""
while not self.exit_flag:
self.run_line()
def run_line(self):
line = self.stdin_read.readline()
if self.exit_flag:
return
# Remove last character which is always '\n':
self.run_command(line[:-1])
def get_thread_id(self):
"""Return thread id"""
if self._id is None:
for thread_id, obj in threading._active.items():
if obj is self:
self._id = thread_id
return self._id
def raise_keyboard_interrupt(self):
if self.isAlive():
ctypes.pythonapi.PyThreadState_SetAsyncExc(self.get_thread_id(),
ctypes.py_object(KeyboardInterrupt))
return True
else:
return False
def closing(self):
"""Actions to be done before restarting this interpreter"""
pass
def execfile(self, filename):
"""Exec filename"""
source = open(filename, 'r').read()
try:
try:
name = filename.encode('ascii')
except UnicodeEncodeError:
name = '<executed_script>'
code = compile(source, name, "exec")
except (OverflowError, SyntaxError):
InteractiveConsole.showsyntaxerror(self, filename)
else:
self.runcode(code)
def runfile(self, filename, args=None):
"""
Run filename
args: command line arguments (string)
"""
if args is not None and not isinstance(args, basestring):
raise TypeError("expected a character buffer object")
self.namespace['__file__'] = filename
sys.argv = [filename]
if args is not None:
for arg in args.split():
sys.argv.append(arg)
self.execfile(filename)
sys.argv = ['']
self.namespace.pop('__file__')
def eval(self, text):
"""
Evaluate text and return (obj, valid)
where *obj* is the object represented by *text*
and *valid* is True if object evaluation did not raise any exception
"""
assert isinstance(text, (str, unicode))
try:
return eval(text, self.locals), True
except:
return None, False
def is_defined(self, objtxt, force_import=False):
"""Return True if object is defined"""
return isdefined(objtxt, force_import=force_import,
namespace=self.locals)
#===========================================================================
# InteractiveConsole API
#===========================================================================
def push(self, line):
"""
Push a line of source text to the interpreter
The line should not have a trailing newline; it may have internal
newlines. The line is appended to a buffer and the interpreter’s
runsource() method is called with the concatenated contents of the
buffer as source. If this indicates that the command was executed
or invalid, the buffer is reset; otherwise, the command is incomplete,
and the buffer is left as it was after the line was appended.
The return value is True if more input is required, False if the line
was dealt with in some way (this is the same as runsource()).
"""
return InteractiveConsole.push(self, "#coding=utf-8\n" + line)
def resetbuffer(self):
"""Remove any unhandled source text from the input buffer"""
InteractiveConsole.resetbuffer(self)
|