<s> """<STR_LIT>""" <EOL> import traceback <EOL> import logging <EOL> import pyfora . PyforaInspect as PyforaInspect <EOL> import pyfora . Exceptions as Exceptions <EOL> import pyfora . pyAst . PyAstUtil as PyAstUtil <EOL> import pyfora . DownloadPolicy as DownloadPolicy <EOL> import sys <EOL> import pyfora . PyforaWithBlock as PyforaWithBlock <EOL> import ast <EOL> INNER_FUNCTION_NAME = '<STR_LIT>' <EOL> class AugmentRaiseFunctionModificationVisitor ( ast . NodeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , line , col , functionName = None ) : <EOL> self . line = line <EOL> self . col = col <EOL> self . functionName = functionName <EOL> def generic_visit ( self , node ) : <EOL> super ( AugmentRaiseFunctionModificationVisitor , self ) . generic_visit ( node ) <EOL> if hasattr ( node , PyAstUtil . LINENO_ATTRIBUTE_NAME ) : <EOL> node . lineno = self . line <EOL> node . col_offset = self . col <EOL> if isinstance ( node , ast . FunctionDef ) and node . name == INNER_FUNCTION_NAME and self . functionName is not None : <EOL> node . name = self . functionName <EOL> if isinstance ( node , ast . Name ) and node . id == INNER_FUNCTION_NAME and self . functionName is not None : <EOL> node . id = self . functionName <EOL> augmentRaiseFunctionTemplate = """<STR_LIT>""" + INNER_FUNCTION_NAME + """<STR_LIT>""" + INNER_FUNCTION_NAME <EOL> def augmentRaiseFunction ( raiseFunction , path , line , col ) : <EOL> codeAst = PyAstUtil . getAstFromFilePath ( path ) <EOL> enclosingFunctionName = PyAstUtil . findEnclosingFunctionName ( codeAst , line ) <EOL> vis = AugmentRaiseFunctionModificationVisitor ( line , col , enclosingFunctionName ) <EOL> module = ast . parse ( augmentRaiseFunctionTemplate ) <EOL> vis . visit ( module ) <EOL> code = compile ( module , path , '<STR_LIT>' ) <EOL> exec code in globals ( ) , locals ( ) <EOL> return _augmentRaiseFunctionTempl ( raiseFunction ) <EOL> def syntheticTraceback ( trace ) : <EOL> raiseFunction = None <EOL> for elt in trace : <EOL> if len ( elt [ '<STR_LIT:path>' ] ) == <NUM_LIT:1> and ( elt [ '<STR_LIT:path>' ] [ <NUM_LIT:0> ] . endswith ( "<STR_LIT>" ) or elt [ '<STR_LIT:path>' ] [ <NUM_LIT:0> ] . endswith ( "<STR_LIT>" ) ) : <EOL> raiseFunction = augmentRaiseFunction ( <EOL> raiseFunction , <EOL> elt [ '<STR_LIT:path>' ] [ <NUM_LIT:0> ] , <EOL> elt [ '<STR_LIT>' ] [ '<STR_LIT:start>' ] [ '<STR_LIT>' ] , <EOL> elt [ '<STR_LIT>' ] [ '<STR_LIT:start>' ] [ '<STR_LIT>' ] <EOL> ) <EOL> try : <EOL> raiseFunction ( ) <EOL> except : <EOL> return sys . exc_info ( ) [ <NUM_LIT:2> ] . tb_next <EOL> class WithBlockExecutor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , executor ) : <EOL> self . executor = executor <EOL> self . lineNumber = None <EOL> self . sourceText = None <EOL> self . stackFrame = None <EOL> self . sourceFileName = None <EOL> self . traceAndException = None <EOL> self . frame = None <EOL> self . compileOnly = False <EOL> self . downloadPolicy = DownloadPolicy . DownloadNonePolicy ( ) <EOL> def asCompileOnly ( self ) : <EOL> self . compileOnly = True <EOL> return self <EOL> def downloadAll ( self ) : <EOL> """<STR_LIT>""" <EOL> self . downloadPolicy = DownloadPolicy . DownloadAllPolicy ( ) <EOL> return self <EOL> def remoteAll ( self ) : <EOL> """<STR_LIT>""" <EOL> self . downloadPolicy = DownloadPolicy . DownloadNonePolicy ( ) <EOL> return self <EOL> def downloadSmall ( self , bytecount = <NUM_LIT:10> * <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> self . downloadPolicy = DownloadPolicy . DownloadSmallPolicy ( bytecount ) <EOL> return self <EOL> def __enter__ ( self ) : <EOL> self . frame = PyforaInspect . currentframe ( <NUM_LIT:1> ) <EOL> sourceFileName , lineNumber , _ , _ , _ = PyforaInspect . getframeinfo ( self . frame ) <EOL> self . sourceFileName = sourceFileName <EOL> self . lineNumber = lineNumber <EOL> self . sourceText = "<STR_LIT>" . join ( PyforaInspect . getlines ( self . sourceFileName ) ) <EOL> sys . settrace ( lambda * args , ** keys : None ) <EOL> self . frame . f_trace = self . trace <EOL> def __exit__ ( self , excType , excValue , trace ) : <EOL> return isinstance ( excValue , WithBlockCompleted ) <EOL> def blockOperation ( self , frame ) : <EOL> boundVariables = { } <EOL> boundVariables . update ( frame . f_globals ) <EOL> boundVariables . update ( frame . f_locals ) <EOL> unboundLocals = [ ] <EOL> for var in frame . f_code . co_varnames : <EOL> if var not in frame . f_locals : <EOL> unboundLocals . append ( var ) <EOL> unboundLocals = sorted ( unboundLocals ) <EOL> withBlock = PyforaWithBlock . PyforaWithBlock ( <EOL> lineNumber = self . lineNumber , <EOL> sourceText = self . sourceText , <EOL> boundVariables = boundVariables , <EOL> sourceFileName = self . sourceFileName , <EOL> unboundLocals = unboundLocals <EOL> ) <EOL> f_proxy = self . executor . define ( withBlock ) . resultWithWakeup ( ) <EOL> if self . compileOnly : <EOL> f_proxy . triggerCompilation ( ) . resultWithWakeup ( ) <EOL> return { } <EOL> f_result_proxy = f_proxy ( ) . resultWithWakeup ( ) <EOL> try : <EOL> tuple_of_proxies = f_result_proxy . toTupleOfProxies ( ) . resultWithWakeup ( ) <EOL> except Exceptions . ComputationError as e : <EOL> self . traceAndException = ( e . trace , e . remoteException ) <EOL> return { } <EOL> proxy_trace = tuple_of_proxies [ <NUM_LIT:1> ] <EOL> trace = proxy_trace . toLocal ( ) . resultWithWakeup ( ) <EOL> if isinstance ( trace , tuple ) : <EOL> self . traceAndException = ( trace , tuple_of_proxies [ <NUM_LIT:2> ] . toLocal ( ) . resultWithWakeup ( ) ) <EOL> proxy_dict = tuple_of_proxies [ <NUM_LIT:0> ] <EOL> dict_of_proxies = proxy_dict . toDictOfAssignedVarsToProxyValues ( ) . resultWithWakeup ( ) <EOL> return dict_of_proxies <EOL> def trace ( self , frame , event , arg ) : <EOL> try : <EOL> f_locals = frame . f_locals <EOL> globalsToSet = self . blockOperation ( frame ) <EOL> policyInstances = { } <EOL> for keyname in list ( globalsToSet . keys ( ) ) : <EOL> policyInstances [ keyname ] = self . downloadPolicy . initiatePolicyCheck ( keyname , globalsToSet [ keyname ] ) <EOL> for k , _ in globalsToSet . iteritems ( ) : <EOL> f_locals [ k ] = self . downloadPolicy . resolveToFinalValue ( policyInstances [ k ] ) <EOL> except ( Exceptions . PythonToForaConversionError , Exceptions . ForaToPythonConversionError ) as err : <EOL> frame . f_lineno = frame . f_lineno - <NUM_LIT:1> <EOL> logging . error ( "<STR_LIT>" , traceback . format_exc ( ) ) <EOL> raise err <EOL> except Exception : <EOL> frame . f_lineno = frame . f_lineno - <NUM_LIT:1> <EOL> logging . error ( "<STR_LIT>" , traceback . format_exc ( ) ) <EOL> raise <EOL> if self . traceAndException is not None : <EOL> exceptionValue = self . traceAndException [ <NUM_LIT:1> ] <EOL> tb = syntheticTraceback ( self . traceAndException [ <NUM_LIT:0> ] ) <EOL> frame . f_lineno = frame . f_lineno - <NUM_LIT:1> <EOL> raise exceptionValue , None , tb <EOL> raise WithBlockCompleted ( ) <EOL> class WithBlockCompleted ( Exception ) : <EOL> pass </s>
