console.py (4872B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\werkzeug\debug\console.py 2 import sys 3 import code 4 from types import CodeType 5 from werkzeug.utils import escape 6 from werkzeug.local import Local 7 from werkzeug.debug.repr import debug_repr, dump, helper 8 from werkzeug.debug.utils import render_template 9 _local = Local() 10 11 class HTMLStringO(object): 12 13 def __init__(self): 14 self._buffer = [] 15 16 def isatty(self): 17 return False 18 19 def close(self): 20 pass 21 22 def flush(self): 23 pass 24 25 def seek(self, n, mode = 0): 26 raise IOError('Bad file descriptor') 27 28 def readline(self): 29 raise IOError('Bad file descriptor') 30 31 def reset(self): 32 val = ''.join(self._buffer) 33 del self._buffer[:] 34 return val 35 36 def _write(self, x): 37 if isinstance(x, str): 38 x = x.decode('utf-8', 'replace') 39 self._buffer.append(x) 40 41 def write(self, x): 42 self._write(escape(x)) 43 44 def writelines(self, x): 45 self._write(escape(''.join(x))) 46 47 48 class ThreadedStream(object): 49 50 def push(): 51 if not isinstance(sys.stdout, ThreadedStream): 52 sys.stdout = ThreadedStream() 53 _local.stream = HTMLStringO() 54 55 push = staticmethod(push) 56 57 def fetch(): 58 try: 59 stream = _local.stream 60 except AttributeError: 61 return '' 62 63 return stream.reset() 64 65 fetch = staticmethod(fetch) 66 67 def displayhook(obj): 68 try: 69 stream = _local.stream 70 except AttributeError: 71 return _displayhook(obj) 72 73 if obj is not None: 74 stream._write(debug_repr(obj)) 75 76 displayhook = staticmethod(displayhook) 77 78 def __setattr__(self, name, value): 79 raise AttributeError('read only attribute %s' % name) 80 81 def __dir__(self): 82 return dir(sys.__stdout__) 83 84 def __getattribute__(self, name): 85 if name == '__members__': 86 return dir(sys.__stdout__) 87 try: 88 stream = _local.stream 89 except AttributeError: 90 stream = sys.__stdout__ 91 92 return getattr(stream, name) 93 94 def __repr__(self): 95 return repr(sys.__stdout__) 96 97 98 _displayhook = sys.displayhook 99 sys.displayhook = ThreadedStream.displayhook 100 101 class _ConsoleLoader(object): 102 103 def __init__(self): 104 self._storage = {} 105 106 def register(self, code, source): 107 self._storage[id(code)] = source 108 for var in code.co_consts: 109 if isinstance(var, CodeType): 110 self._storage[id(var)] = source 111 112 def get_source_by_code(self, code): 113 try: 114 return self._storage[id(code)] 115 except KeyError: 116 pass 117 118 119 def _wrap_compiler(console): 120 compile = console.compile 121 122 def func(source, filename, symbol): 123 code = compile(source, filename, symbol) 124 console.loader.register(code, source) 125 return code 126 127 console.compile = func 128 129 130 class _InteractiveConsole(code.InteractiveInterpreter): 131 132 def __init__(self, globals, locals): 133 code.InteractiveInterpreter.__init__(self, locals) 134 self.globals = dict(globals) 135 self.globals['dump'] = dump 136 self.globals['help'] = helper 137 self.globals['__loader__'] = self.loader = _ConsoleLoader() 138 self.more = False 139 self.buffer = [] 140 _wrap_compiler(self) 141 142 def runsource(self, source): 143 source = source.rstrip() + '\n' 144 ThreadedStream.push() 145 prompt = self.more and '... ' or '>>> ' 146 try: 147 source_to_eval = ''.join(self.buffer + [source]) 148 if code.InteractiveInterpreter.runsource(self, source_to_eval, '<debugger>', 'single'): 149 self.more = True 150 self.buffer.append(source) 151 else: 152 self.more = False 153 del self.buffer[:] 154 finally: 155 output = ThreadedStream.fetch() 156 157 return prompt + source + output 158 159 def runcode(self, code): 160 try: 161 exec code in self.globals, self.locals 162 except: 163 self.showtraceback() 164 165 def showtraceback(self): 166 from werkzeug.debug.tbtools import get_current_traceback 167 tb = get_current_traceback(skip=1) 168 sys.stdout._write(tb.render_summary()) 169 170 def showsyntaxerror(self, filename = None): 171 from werkzeug.debug.tbtools import get_current_traceback 172 tb = get_current_traceback(skip=4) 173 sys.stdout._write(tb.render_summary()) 174 175 def write(self, data): 176 sys.stdout.write(data) 177 178 179 class Console(object): 180 181 def __init__(self, globals = None, locals = None): 182 if locals is None: 183 locals = {} 184 if globals is None: 185 globals = {} 186 self._ipy = _InteractiveConsole(globals, locals) 187 188 def eval(self, code): 189 return self._ipy.runsource(code)