<s> """<STR_LIT>""" <EOL> import inspect <EOL> import keyword <EOL> import linecache <EOL> import os <EOL> import pydoc <EOL> import sys <EOL> import tempfile <EOL> import time <EOL> import tokenize <EOL> import traceback <EOL> import types <EOL> def reset ( ) : <EOL> """<STR_LIT>""" <EOL> return '''<STR_LIT>''' <EOL> __UNDEF__ = [ ] <EOL> def small ( text ) : <EOL> if text : <EOL> return '<STR_LIT>' + text + '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def strong ( text ) : <EOL> if text : <EOL> return '<STR_LIT>' + text + '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def grey ( text ) : <EOL> if text : <EOL> return '<STR_LIT>' + text + '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def lookup ( name , frame , locals ) : <EOL> """<STR_LIT>""" <EOL> if name in locals : <EOL> return '<STR_LIT>' , locals [ name ] <EOL> if name in frame . f_globals : <EOL> return '<STR_LIT>' , frame . f_globals [ name ] <EOL> if '<STR_LIT>' in frame . f_globals : <EOL> builtins = frame . f_globals [ '<STR_LIT>' ] <EOL> if type ( builtins ) is type ( { } ) : <EOL> if name in builtins : <EOL> return '<STR_LIT>' , builtins [ name ] <EOL> else : <EOL> if hasattr ( builtins , name ) : <EOL> return '<STR_LIT>' , getattr ( builtins , name ) <EOL> return None , __UNDEF__ <EOL> def scanvars ( reader , frame , locals ) : <EOL> """<STR_LIT>""" <EOL> vars , lasttoken , parent , prefix , value = [ ] , None , None , '<STR_LIT>' , __UNDEF__ <EOL> for ttype , token , start , end , line in tokenize . generate_tokens ( reader ) : <EOL> if ttype == tokenize . NEWLINE : break <EOL> if ttype == tokenize . NAME and token not in keyword . kwlist : <EOL> if lasttoken == '<STR_LIT:.>' : <EOL> if parent is not __UNDEF__ : <EOL> value = getattr ( parent , token , __UNDEF__ ) <EOL> vars . append ( ( prefix + token , prefix , value ) ) <EOL> else : <EOL> where , value = lookup ( token , frame , locals ) <EOL> vars . append ( ( token , where , value ) ) <EOL> elif token == '<STR_LIT:.>' : <EOL> prefix += lasttoken + '<STR_LIT:.>' <EOL> parent = value <EOL> else : <EOL> parent , prefix = None , '<STR_LIT>' <EOL> lasttoken = token <EOL> return vars <EOL> def html ( einfo , context = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> etype , evalue , etb = einfo <EOL> if type ( etype ) is types . ClassType : <EOL> etype = etype . __name__ <EOL> pyver = '<STR_LIT>' + sys . version . split ( ) [ <NUM_LIT:0> ] + '<STR_LIT>' + sys . executable <EOL> date = time . ctime ( time . time ( ) ) <EOL> head = '<STR_LIT>' + pydoc . html . heading ( <EOL> '<STR_LIT>' % <EOL> strong ( pydoc . html . escape ( str ( etype ) ) ) , <EOL> '<STR_LIT>' , '<STR_LIT>' , pyver + '<STR_LIT>' + date ) + '''<STR_LIT>''' <EOL> indent = '<STR_LIT>' + small ( '<STR_LIT>' * <NUM_LIT:5> ) + '<STR_LIT>' <EOL> frames = [ ] <EOL> records = inspect . getinnerframes ( etb , context ) <EOL> for frame , file , lnum , func , lines , index in records : <EOL> if file : <EOL> file = os . path . abspath ( file ) <EOL> link = '<STR_LIT>' % ( file , pydoc . html . escape ( file ) ) <EOL> else : <EOL> file = link = '<STR_LIT:?>' <EOL> args , varargs , varkw , locals = inspect . getargvalues ( frame ) <EOL> call = '<STR_LIT>' <EOL> if func != '<STR_LIT:?>' : <EOL> call = '<STR_LIT>' + strong ( func ) + inspect . formatargvalues ( args , varargs , varkw , locals , <EOL> formatvalue = lambda value : '<STR_LIT:=>' + pydoc . html . repr ( value ) ) <EOL> highlight = { } <EOL> def reader ( lnum = [ lnum ] ) : <EOL> highlight [ lnum [ <NUM_LIT:0> ] ] = <NUM_LIT:1> <EOL> try : return linecache . getline ( file , lnum [ <NUM_LIT:0> ] ) <EOL> finally : lnum [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> vars = scanvars ( reader , frame , locals ) <EOL> rows = [ '<STR_LIT>' % <EOL> ( '<STR_LIT>' , link , call ) ] <EOL> if index is not None : <EOL> i = lnum - index <EOL> for line in lines : <EOL> num = small ( '<STR_LIT>' * ( <NUM_LIT:5> - len ( str ( i ) ) ) + str ( i ) ) + '<STR_LIT>' <EOL> if i in highlight : <EOL> line = '<STR_LIT>' % ( num , pydoc . html . preformat ( line ) ) <EOL> rows . append ( '<STR_LIT>' % line ) <EOL> else : <EOL> line = '<STR_LIT>' % ( num , pydoc . html . preformat ( line ) ) <EOL> rows . append ( '<STR_LIT>' % grey ( line ) ) <EOL> i += <NUM_LIT:1> <EOL> done , dump = { } , [ ] <EOL> for name , where , value in vars : <EOL> if name in done : continue <EOL> done [ name ] = <NUM_LIT:1> <EOL> if value is not __UNDEF__ : <EOL> if where in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> name = ( '<STR_LIT>' % where ) + strong ( name ) <EOL> elif where == '<STR_LIT>' : <EOL> name = strong ( name ) <EOL> else : <EOL> name = where + strong ( name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] ) <EOL> dump . append ( '<STR_LIT>' % ( name , pydoc . html . repr ( value ) ) ) <EOL> else : <EOL> dump . append ( name + '<STR_LIT>' ) <EOL> rows . append ( '<STR_LIT>' % small ( grey ( '<STR_LIT:U+002CU+0020>' . join ( dump ) ) ) ) <EOL> frames . append ( '''<STR_LIT>''' % '<STR_LIT:\n>' . join ( rows ) ) <EOL> exception = [ '<STR_LIT>' % ( strong ( pydoc . html . escape ( str ( etype ) ) ) , <EOL> pydoc . html . escape ( str ( evalue ) ) ) ] <EOL> if isinstance ( evalue , BaseException ) : <EOL> for name in dir ( evalue ) : <EOL> if name [ : <NUM_LIT:1> ] == '<STR_LIT:_>' : continue <EOL> value = pydoc . html . repr ( getattr ( evalue , name ) ) <EOL> exception . append ( '<STR_LIT>' % ( indent , name , value ) ) <EOL> return head + '<STR_LIT>' . join ( frames ) + '<STR_LIT>' . join ( exception ) + '''<STR_LIT>''' % pydoc . html . escape ( <EOL> '<STR_LIT>' . join ( traceback . format_exception ( etype , evalue , etb ) ) ) <EOL> def text ( einfo , context = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> etype , evalue , etb = einfo <EOL> if type ( etype ) is types . ClassType : <EOL> etype = etype . __name__ <EOL> pyver = '<STR_LIT>' + sys . version . split ( ) [ <NUM_LIT:0> ] + '<STR_LIT>' + sys . executable <EOL> date = time . ctime ( time . time ( ) ) <EOL> head = "<STR_LIT>" % ( str ( etype ) , pyver , date ) + '''<STR_LIT>''' <EOL> frames = [ ] <EOL> records = inspect . getinnerframes ( etb , context ) <EOL> for frame , file , lnum , func , lines , index in records : <EOL> file = file and os . path . abspath ( file ) or '<STR_LIT:?>' <EOL> args , varargs , varkw , locals = inspect . getargvalues ( frame ) <EOL> call = '<STR_LIT>' <EOL> if func != '<STR_LIT:?>' : <EOL> call = '<STR_LIT>' + func + inspect . formatargvalues ( args , varargs , varkw , locals , <EOL> formatvalue = lambda value : '<STR_LIT:=>' + pydoc . text . repr ( value ) ) <EOL> highlight = { } <EOL> def reader ( lnum = [ lnum ] ) : <EOL> highlight [ lnum [ <NUM_LIT:0> ] ] = <NUM_LIT:1> <EOL> try : return linecache . getline ( file , lnum [ <NUM_LIT:0> ] ) <EOL> finally : lnum [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> vars = scanvars ( reader , frame , locals ) <EOL> rows = [ '<STR_LIT>' % ( file , call ) ] <EOL> if index is not None : <EOL> i = lnum - index <EOL> for line in lines : <EOL> num = '<STR_LIT>' % i <EOL> rows . append ( num + line . rstrip ( ) ) <EOL> i += <NUM_LIT:1> <EOL> done , dump = { } , [ ] <EOL> for name , where , value in vars : <EOL> if name in done : continue <EOL> done [ name ] = <NUM_LIT:1> <EOL> if value is not __UNDEF__ : <EOL> if where == '<STR_LIT>' : name = '<STR_LIT>' + name <EOL> elif where != '<STR_LIT>' : name = where + name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> dump . append ( '<STR_LIT>' % ( name , pydoc . text . repr ( value ) ) ) <EOL> else : <EOL> dump . append ( name + '<STR_LIT>' ) <EOL> rows . append ( '<STR_LIT:\n>' . join ( dump ) ) <EOL> frames . append ( '<STR_LIT>' % '<STR_LIT:\n>' . join ( rows ) ) <EOL> exception = [ '<STR_LIT>' % ( str ( etype ) , str ( evalue ) ) ] <EOL> if isinstance ( evalue , BaseException ) : <EOL> for name in dir ( evalue ) : <EOL> value = pydoc . text . repr ( getattr ( evalue , name ) ) <EOL> exception . append ( '<STR_LIT>' % ( "<STR_LIT:U+0020>" * <NUM_LIT:4> , name , value ) ) <EOL> return head + '<STR_LIT>' . join ( frames ) + '<STR_LIT>' . join ( exception ) + '''<STR_LIT>''' % '<STR_LIT>' . join ( traceback . format_exception ( etype , evalue , etb ) ) <EOL> class Hook : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , display = <NUM_LIT:1> , logdir = None , context = <NUM_LIT:5> , file = None , <EOL> format = "<STR_LIT:html>" ) : <EOL> self . display = display <EOL> self . logdir = logdir <EOL> self . context = context <EOL> self . file = file or sys . stdout <EOL> self . format = format <EOL> def __call__ ( self , etype , evalue , etb ) : <EOL> self . handle ( ( etype , evalue , etb ) ) <EOL> def handle ( self , info = None ) : <EOL> info = info or sys . exc_info ( ) <EOL> if self . format == "<STR_LIT:html>" : <EOL> self . file . write ( reset ( ) ) <EOL> formatter = ( self . format == "<STR_LIT:html>" ) and html or text <EOL> plain = False <EOL> try : <EOL> doc = formatter ( info , self . context ) <EOL> except : <EOL> doc = '<STR_LIT>' . join ( traceback . format_exception ( * info ) ) <EOL> plain = True <EOL> if self . display : <EOL> if plain : <EOL> doc = doc . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) <EOL> self . file . write ( '<STR_LIT>' + doc + '<STR_LIT>' ) <EOL> else : <EOL> self . file . write ( doc + '<STR_LIT:\n>' ) <EOL> else : <EOL> self . file . write ( '<STR_LIT>' ) <EOL> if self . logdir is not None : <EOL> suffix = [ '<STR_LIT>' , '<STR_LIT>' ] [ self . format == "<STR_LIT:html>" ] <EOL> ( fd , path ) = tempfile . mkstemp ( suffix = suffix , dir = self . logdir ) <EOL> try : <EOL> file = os . fdopen ( fd , '<STR_LIT:w>' ) <EOL> file . write ( doc ) <EOL> file . close ( ) <EOL> msg = '<STR_LIT>' % path <EOL> except : <EOL> msg = '<STR_LIT>' % path <EOL> self . file . write ( msg + '<STR_LIT:\n>' ) <EOL> try : <EOL> self . file . flush ( ) <EOL> except : pass <EOL> handler = Hook ( ) . handle <EOL> def enable ( display = <NUM_LIT:1> , logdir = None , context = <NUM_LIT:5> , format = "<STR_LIT:html>" ) : <EOL> """<STR_LIT>""" <EOL> sys . excepthook = Hook ( display = display , logdir = logdir , <EOL> context = context , format = format ) </s>
