<s> """<STR_LIT>""" <EOL> import sys <EOL> import code <EOL> from types import CodeType <EOL> from werkzeug . utils import escape <EOL> from werkzeug . local import Local <EOL> from werkzeug . debug . repr import debug_repr , dump , helper <EOL> _local = Local ( ) <EOL> class HTMLStringO ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _buffer = [ ] <EOL> def isatty ( self ) : <EOL> return False <EOL> def close ( self ) : <EOL> pass <EOL> def flush ( self ) : <EOL> pass <EOL> def seek ( self , n , mode = <NUM_LIT:0> ) : <EOL> pass <EOL> def readline ( self ) : <EOL> if len ( self . _buffer ) == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> ret = self . _buffer [ <NUM_LIT:0> ] <EOL> del self . _buffer [ <NUM_LIT:0> ] <EOL> return ret <EOL> def reset ( self ) : <EOL> val = '<STR_LIT>' . join ( self . _buffer ) <EOL> del self . _buffer [ : ] <EOL> return val <EOL> def _write ( self , x ) : <EOL> if isinstance ( x , bytes ) : <EOL> x = x . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> self . _buffer . append ( x ) <EOL> def write ( self , x ) : <EOL> self . _write ( escape ( x ) ) <EOL> def writelines ( self , x ) : <EOL> self . _write ( escape ( '<STR_LIT>' . join ( x ) ) ) <EOL> class ThreadedStream ( object ) : <EOL> """<STR_LIT>""" <EOL> def push ( ) : <EOL> if not isinstance ( sys . stdout , ThreadedStream ) : <EOL> sys . stdout = ThreadedStream ( ) <EOL> _local . stream = HTMLStringO ( ) <EOL> push = staticmethod ( push ) <EOL> def fetch ( ) : <EOL> try : <EOL> stream = _local . stream <EOL> except AttributeError : <EOL> return '<STR_LIT>' <EOL> return stream . reset ( ) <EOL> fetch = staticmethod ( fetch ) <EOL> def displayhook ( obj ) : <EOL> try : <EOL> stream = _local . stream <EOL> except AttributeError : <EOL> return _displayhook ( obj ) <EOL> if obj is not None : <EOL> _local . _current_ipy . locals [ '<STR_LIT:_>' ] = obj <EOL> stream . _write ( debug_repr ( obj ) ) <EOL> displayhook = staticmethod ( displayhook ) <EOL> def __setattr__ ( self , name , value ) : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> def __dir__ ( self ) : <EOL> return dir ( sys . __stdout__ ) <EOL> def __getattribute__ ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return dir ( sys . __stdout__ ) <EOL> try : <EOL> stream = _local . stream <EOL> except AttributeError : <EOL> stream = sys . __stdout__ <EOL> return getattr ( stream , name ) <EOL> def __repr__ ( self ) : <EOL> return repr ( sys . __stdout__ ) <EOL> _displayhook = sys . displayhook <EOL> sys . displayhook = ThreadedStream . displayhook <EOL> class _ConsoleLoader ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _storage = { } <EOL> def register ( self , code , source ) : <EOL> self . _storage [ id ( code ) ] = source <EOL> for var in code . co_consts : <EOL> if isinstance ( var , CodeType ) : <EOL> self . _storage [ id ( var ) ] = source <EOL> def get_source_by_code ( self , code ) : <EOL> try : <EOL> return self . _storage [ id ( code ) ] <EOL> except KeyError : <EOL> pass <EOL> def _wrap_compiler ( console ) : <EOL> compile = console . compile <EOL> def func ( source , filename , symbol ) : <EOL> code = compile ( source , filename , symbol ) <EOL> console . loader . register ( code , source ) <EOL> return code <EOL> console . compile = func <EOL> class _InteractiveConsole ( code . InteractiveInterpreter ) : <EOL> def __init__ ( self , globals , locals ) : <EOL> code . InteractiveInterpreter . __init__ ( self , locals ) <EOL> self . globals = dict ( globals ) <EOL> self . globals [ '<STR_LIT>' ] = dump <EOL> self . globals [ '<STR_LIT>' ] = helper <EOL> self . globals [ '<STR_LIT>' ] = self . loader = _ConsoleLoader ( ) <EOL> self . more = False <EOL> self . buffer = [ ] <EOL> _wrap_compiler ( self ) <EOL> def runsource ( self , source ) : <EOL> source = source . rstrip ( ) + '<STR_LIT:\n>' <EOL> ThreadedStream . push ( ) <EOL> prompt = self . more and '<STR_LIT>' or '<STR_LIT>' <EOL> try : <EOL> source_to_eval = '<STR_LIT>' . join ( self . buffer + [ source ] ) <EOL> if code . InteractiveInterpreter . runsource ( self , <EOL> source_to_eval , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . more = True <EOL> self . buffer . append ( source ) <EOL> else : <EOL> self . more = False <EOL> del self . buffer [ : ] <EOL> finally : <EOL> output = ThreadedStream . fetch ( ) <EOL> return prompt + source + output <EOL> def runcode ( self , code ) : <EOL> try : <EOL> eval ( code , self . globals , self . locals ) <EOL> except Exception : <EOL> self . showtraceback ( ) <EOL> def showtraceback ( self ) : <EOL> from werkzeug . debug . tbtools import get_current_traceback <EOL> tb = get_current_traceback ( skip = <NUM_LIT:1> ) <EOL> sys . stdout . _write ( tb . render_summary ( ) ) <EOL> def showsyntaxerror ( self , filename = None ) : <EOL> from werkzeug . debug . tbtools import get_current_traceback <EOL> tb = get_current_traceback ( skip = <NUM_LIT:4> ) <EOL> sys . stdout . _write ( tb . render_summary ( ) ) <EOL> def write ( self , data ) : <EOL> sys . stdout . write ( data ) <EOL> class Console ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , globals = None , locals = None ) : <EOL> if locals is None : <EOL> locals = { } <EOL> if globals is None : <EOL> globals = { } <EOL> self . _ipy = _InteractiveConsole ( globals , locals ) <EOL> def eval ( self , code ) : <EOL> _local . _current_ipy = self . _ipy <EOL> old_sys_stdout = sys . stdout <EOL> try : <EOL> return self . _ipy . runsource ( code ) <EOL> finally : <EOL> sys . stdout = old_sys_stdout </s>
