<s> import ufora . FORA . python . Expression as Expression <EOL> import ufora . FORA . python . PurePython . PythonAstConverter as PythonAstConverter <EOL> import ufora . FORA . python . PurePython . ConstantConverter as ConstantConverter <EOL> import ufora . FORA . python . ForaValue as ForaValue <EOL> import ufora . BackendGateway . ComputedValue . ComputedValue as ComputedValue <EOL> import pyfora . TypeDescription as TypeDescription <EOL> import pyfora . StronglyConnectedComponents as StronglyConnectedComponents <EOL> import pyfora . pyAst . PyAstUtil as PyAstUtil <EOL> import pyfora <EOL> import base64 <EOL> import ast <EOL> import logging <EOL> import ufora . native . FORA as ForaNative <EOL> emptyCodeDefinitionPoint = ForaNative . CodeDefinitionPoint . ExternalFromStringList ( [ ] ) <EOL> empytObjectExpression = ForaNative . parseStringToExpression ( <EOL> "<STR_LIT>" , <EOL> emptyCodeDefinitionPoint , <EOL> "<STR_LIT>" <EOL> ) <EOL> Symbol_CreateInstance = ForaNative . makeSymbol ( "<STR_LIT>" ) <EOL> Symbol_Call = ForaNative . makeSymbol ( "<STR_LIT>" ) <EOL> Symbol_uninitialized = ForaNative . makeSymbol ( "<STR_LIT>" ) <EOL> Symbol_invalid = ForaNative . makeSymbol ( "<STR_LIT>" ) <EOL> Symbol_unconvertible = ForaNative . makeSymbol ( "<STR_LIT>" ) <EOL> def convertNativePythonToForaConversionError ( err , path ) : <EOL> """<STR_LIT>""" <EOL> return pyfora . PythonToForaConversionError ( <EOL> err . error , <EOL> trace = [ { <EOL> '<STR_LIT:path>' : path , <EOL> '<STR_LIT>' : err . range . start . line <EOL> } ] <EOL> ) <EOL> class Converter ( object ) : <EOL> def __init__ ( self , <EOL> nativeConstantConverter = None , <EOL> nativeListConverter = None , <EOL> nativeTupleConverter = None , <EOL> nativeDictConverter = None , <EOL> singletonAndExceptionConverter = None , <EOL> vdmOverride = None , <EOL> purePythonModuleImplVal = None , <EOL> foraBuiltinsImplVal = None ) : <EOL> self . convertedValues = { } <EOL> self . boundExpressions = { } <EOL> self . constantConverter = ConstantConverter . ConstantConverter ( <EOL> nativeConstantConverter = nativeConstantConverter <EOL> ) <EOL> self . singletonAndExceptionConverter = singletonAndExceptionConverter <EOL> self . nativeListConverter = nativeListConverter <EOL> self . nativeTupleConverter = nativeTupleConverter <EOL> self . nativeDictConverter = nativeDictConverter <EOL> self . vdm_ = vdmOverride <EOL> self . pyforaBoundMethodClass = purePythonModuleImplVal . getObjectMember ( "<STR_LIT>" ) <EOL> builtinMemberMapping = Converter . computeBuiltinMemberMapping ( <EOL> purePythonModuleImplVal = purePythonModuleImplVal , <EOL> foraBuiltinsImplVal = foraBuiltinsImplVal <EOL> ) <EOL> self . nativeConverter = ForaNative . makePythonAstConverter ( <EOL> nativeConstantConverter , <EOL> self . nativeListConverter , <EOL> self . nativeTupleConverter , <EOL> self . nativeDictConverter , <EOL> purePythonModuleImplVal , <EOL> builtinMemberMapping <EOL> ) <EOL> @ staticmethod <EOL> def computeBuiltinMemberMapping ( purePythonModuleImplVal , foraBuiltinsImplVal ) : <EOL> builtinMemberMapping = { } <EOL> builtinMemberMapping . update ( <EOL> Converter . computeMemberMapping ( <EOL> purePythonModuleImplVal <EOL> ) <EOL> ) <EOL> builtinMemberMapping . update ( <EOL> Converter . computeMemberMapping ( <EOL> foraBuiltinsImplVal <EOL> ) <EOL> ) <EOL> builtinMemberMapping [ '<STR_LIT>' ] = purePythonModuleImplVal <EOL> builtinMemberMapping [ '<STR_LIT>' ] = foraBuiltinsImplVal <EOL> return builtinMemberMapping <EOL> @ staticmethod <EOL> def computeMemberMapping ( purePythonImplVal ) : <EOL> objectMembers = purePythonImplVal . objectMembers <EOL> tr = { } <EOL> for memberName in objectMembers : <EOL> objectMember = purePythonImplVal . getObjectMember ( memberName ) <EOL> if objectMember is not None : <EOL> tr [ memberName ] = objectMember <EOL> return tr <EOL> def extractWrappedForaConstant ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , ForaValue . FORAValue ) : <EOL> value = value . implVal_ <EOL> if not isinstance ( value , ForaNative . ImplValContainer ) : <EOL> return value <EOL> return value . getObjectLexicalMember ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . pyval <EOL> def convert ( self , objectId , objectRegistry , callback ) : <EOL> try : <EOL> dependencyGraph = objectRegistry . computeDependencyGraph ( objectId ) <EOL> objectIdToObjectDefinition = { <EOL> objId : objectRegistry . getDefinition ( objId ) <EOL> for objId in dependencyGraph . iterkeys ( ) <EOL> } <EOL> convertedValue = self . _convert ( objectId , dependencyGraph , objectIdToObjectDefinition ) <EOL> self . convertedValues [ objectId ] = convertedValue <EOL> callback ( convertedValue ) <EOL> except pyfora . PythonToForaConversionError as e : <EOL> callback ( e ) <EOL> def _convert ( self , objectId , dependencyGraph , objectIdToObjectDefinition ) : <EOL> objectDefinition = objectIdToObjectDefinition [ objectId ] <EOL> if TypeDescription . isPrimitive ( objectDefinition ) or isinstance ( objectDefinition , list ) : <EOL> return self . convertPrimitive ( objectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . RemotePythonObject ) : <EOL> return self . convertRemotePythonObject ( objectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . NamedSingleton ) : <EOL> return self . convertNamedSingleton ( objectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . BuiltinExceptionInstance ) : <EOL> return self . convertBuiltinExceptionInstance ( <EOL> objectDefinition <EOL> ) <EOL> elif isinstance ( objectDefinition , <EOL> ( TypeDescription . FunctionDefinition , <EOL> TypeDescription . ClassDefinition , <EOL> TypeDescription . ClassInstanceDescription , <EOL> TypeDescription . InstanceMethod ) <EOL> ) : <EOL> return ( <EOL> self . convertObjectWithDependencies ( <EOL> objectId , <EOL> dependencyGraph , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . List ) : <EOL> return ( <EOL> self . convertList ( <EOL> objectId , <EOL> dependencyGraph , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . Tuple ) : <EOL> return ( <EOL> self . convertTuple ( <EOL> objectId , <EOL> dependencyGraph , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . Dict ) : <EOL> return ( <EOL> self . convertDict ( <EOL> objectId , <EOL> objectDefinition , <EOL> dependencyGraph , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . WithBlockDescription ) : <EOL> return ( <EOL> self . convertObjectWithDependencies ( <EOL> objectId , <EOL> dependencyGraph , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . Unconvertible ) : <EOL> return self . convertUnconvertibleValue ( objectId ) <EOL> else : <EOL> raise pyfora . PythonToForaConversionError ( <EOL> "<STR_LIT>" % ( <EOL> objectDefinition , type ( objectDefinition ) <EOL> ) <EOL> ) <EOL> def convertNamedSingleton ( self , objectDefinition ) : <EOL> if self . singletonAndExceptionConverter is None : <EOL> logging . error ( "<STR_LIT>" , objectDefinition . singletonName ) <EOL> singleton = self . singletonAndExceptionConverter . convertSingletonByName ( <EOL> objectDefinition . singletonName <EOL> ) <EOL> if singleton is None : <EOL> raise pyfora . PythonToForaConversionError ( <EOL> "<STR_LIT>" % objectDefinition . singletonName <EOL> ) <EOL> return singleton <EOL> def convertBuiltinExceptionInstance ( self , objectDefinition ) : <EOL> args = self . convertedValues [ objectDefinition . argsId ] <EOL> return self . singletonAndExceptionConverter . instantiateException ( <EOL> objectDefinition . builtinExceptionTypeName , <EOL> args <EOL> ) <EOL> def convertRemotePythonObject ( self , objectDefinition ) : <EOL> computedValueArg = objectDefinition . computedValueArgument <EOL> if isinstance ( computedValueArg , int ) : <EOL> return self . convertedValues [ computedValueArg ] <EOL> else : <EOL> return computedValueArg <EOL> def convertDict ( self , dictId , objectDefinition , dependencyGraph , objectIdToObjectDefinition ) : <EOL> self . _convertListMembers ( dictId , dependencyGraph , objectIdToObjectDefinition ) <EOL> convertedKeysAndVals = { <EOL> self . convertedValues [ keyId ] : self . convertedValues [ valId ] <EOL> for keyId , valId in zip ( objectDefinition . keyIds , objectDefinition . valueIds ) <EOL> } <EOL> return self . nativeDictConverter . createDict ( convertedKeysAndVals ) <EOL> def convertUnconvertibleValue ( self , objectId ) : <EOL> tr = Symbol_unconvertible <EOL> self . convertedValues [ objectId ] = tr <EOL> return tr <EOL> def convertPrimitive ( self , value ) : <EOL> if isinstance ( value , list ) : <EOL> return self . nativeListConverter . createListOfPrimitives ( <EOL> value , <EOL> self . constantConverter . nativeConstantConverter , <EOL> self . vdm_ <EOL> ) <EOL> if isinstance ( value , str ) : <EOL> value = base64 . b64decode ( value ) <EOL> return self . constantConverter . convert ( value ) <EOL> def _assertContainerDoesNotReferenceItself ( self , <EOL> containerId , <EOL> dependencyGraph , <EOL> stronglyConnectedComponents ) : <EOL> assert containerId in stronglyConnectedComponents [ - <NUM_LIT:1> ] <EOL> if len ( stronglyConnectedComponents [ - <NUM_LIT:1> ] ) > <NUM_LIT:1> or containerId in dependencyGraph [ containerId ] : <EOL> raise pyfora . PythonToForaConversionError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> def convertList ( self , listId , dependencyGraph , objectIdToObjectDefinition ) : <EOL> self . _convertListMembers ( listId , dependencyGraph , objectIdToObjectDefinition ) <EOL> memberIds = objectIdToObjectDefinition [ listId ] . memberIds <EOL> return self . nativeListConverter . createList ( <EOL> [ self . convertedValues [ memberId ] for memberId in memberIds ] , <EOL> self . vdm_ <EOL> ) <EOL> def convertTuple ( self , tupleId , dependencyGraph , objectIdToObjectDefinition ) : <EOL> self . _convertListMembers ( tupleId , dependencyGraph , objectIdToObjectDefinition ) <EOL> memberIds = objectIdToObjectDefinition [ tupleId ] . memberIds <EOL> return self . nativeTupleConverter . createTuple ( <EOL> [ self . convertedValues [ memberId ] for memberId in memberIds ] <EOL> ) <EOL> def _convertListMembers ( self , listId , dependencyGraph , objectIdToObjectDefinition ) : <EOL> stronglyConnectedComponents = StronglyConnectedComponents . stronglyConnectedComponents ( <EOL> dependencyGraph <EOL> ) <EOL> self . _assertContainerDoesNotReferenceItself ( <EOL> listId , <EOL> dependencyGraph , <EOL> stronglyConnectedComponents <EOL> ) <EOL> for stronglyConnectedComponent in stronglyConnectedComponents [ : - <NUM_LIT:1> ] : <EOL> self . convertStronglyConnectedComponent ( <EOL> dependencyGraph , <EOL> stronglyConnectedComponent , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> def convertObjectWithDependencies ( self , objectId , dependencyGraph , objectIdToObjectDefinition ) : <EOL> stronglyConnectedComponents = StronglyConnectedComponents . stronglyConnectedComponents ( <EOL> dependencyGraph <EOL> ) <EOL> for stronglyConnectedComponent in stronglyConnectedComponents : <EOL> self . convertStronglyConnectedComponent ( <EOL> dependencyGraph , <EOL> stronglyConnectedComponent , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> return self . convertedValues [ objectId ] <EOL> def convertStronglyConnectedComponent ( self , <EOL> dependencyGraph , <EOL> stronglyConnectedComponent , <EOL> objectIdToObjectDefinition ) : <EOL> if len ( stronglyConnectedComponent ) == <NUM_LIT:1> : <EOL> self . convertStronglyConnectedComponentWithOneNode ( <EOL> dependencyGraph , <EOL> stronglyConnectedComponent , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> else : <EOL> self . convertStronglyConnectedComponentWithMoreThanOneNode ( <EOL> objectIdToObjectDefinition , <EOL> stronglyConnectedComponent <EOL> ) <EOL> def convertStronglyConnectedComponentWithMoreThanOneNode ( self , <EOL> objectIdToObjectDefinition , <EOL> stronglyConnectedComponent ) : <EOL> ( createObjectExpression , memberToObjectIdMap ) = self . getCreateObjectExpressionAndMemberToObjectIdMap ( <EOL> objectIdToObjectDefinition , <EOL> stronglyConnectedComponent <EOL> ) <EOL> objectImplVal = self . bindDependentValuesToCreateObjectExpression ( <EOL> createObjectExpression , <EOL> memberToObjectIdMap , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> self . registerObjectMembers ( <EOL> objectImplVal , <EOL> memberToObjectIdMap <EOL> ) <EOL> def getCreateObjectExpressionAndMemberToObjectIdMap ( self , <EOL> objectIdToObjectDefinition , <EOL> stronglyConnectedComponent ) : <EOL> naiveConvertedFunctions = dict ( ) <EOL> for objectId in stronglyConnectedComponent : <EOL> objectDefinition = objectIdToObjectDefinition [ objectId ] <EOL> if isinstance ( objectDefinition , TypeDescription . ClassInstanceDescription ) : <EOL> classDesc = objectIdToObjectDefinition [ objectDefinition . classId ] <EOL> sourceFile = objectIdToObjectDefinition [ classDesc . sourceFileId ] <EOL> lineNumber = classDesc . lineNumber <EOL> raise pyfora . PythonToForaConversionError ( <EOL> "<STR_LIT>" , <EOL> trace = [ { '<STR_LIT:path>' : sourceFile . path , '<STR_LIT>' : lineNumber } ] <EOL> ) <EOL> assert isinstance ( <EOL> objectDefinition , <EOL> ( TypeDescription . FunctionDefinition , <EOL> TypeDescription . ClassDefinition ) ) , type ( objectDefinition ) <EOL> naiveConvertedFunctions [ objectId ] = self . convertPyClassOrFunctionDefinitionToForaFunctionExpression ( <EOL> objectDefinition , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> renamedObjectMapping = self . computeRenamedObjectMapping ( <EOL> naiveConvertedFunctions <EOL> ) <EOL> convertedFunctions = self . replaceKnownMemberChainsWithRenamedVariables ( <EOL> naiveConvertedFunctions , <EOL> renamedObjectMapping , <EOL> objectIdToObjectDefinition , <EOL> stronglyConnectedComponent <EOL> ) <EOL> createObjectExpression = empytObjectExpression <EOL> for objectId , varname in sorted ( renamedObjectMapping . items ( ) , key = lambda p : p [ <NUM_LIT:1> ] ) : <EOL> createObjectExpression = ForaNative . prependMemberToCreateObjectExpression ( <EOL> createObjectExpression , <EOL> varname , <EOL> convertedFunctions [ objectId ] <EOL> ) <EOL> return createObjectExpression , renamedObjectMapping <EOL> def registerObjectMembers ( self , objectImplVal , renamedObjectMapping ) : <EOL> for objectId , memberName in renamedObjectMapping . iteritems ( ) : <EOL> memberImplValOrNone = objectImplVal . getObjectMember ( memberName ) <EOL> if memberImplValOrNone is None : <EOL> raise pyfora . PythonToForaConversionError ( <EOL> ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> ) <EOL> self . convertedValues [ objectId ] = memberImplValOrNone <EOL> def bindDependentValuesToCreateObjectExpression ( self , <EOL> createObjectExpression , <EOL> renamedObjectMapping , <EOL> objectIdToObjectDefinition ) : <EOL> stronglyConnectedComponent = renamedObjectMapping . keys ( ) <EOL> renamedVariableMapping = dict ( ) <EOL> for objectId in stronglyConnectedComponent : <EOL> for freeVariableMemberAccessChain , dependentId in objectIdToObjectDefinition [ objectId ] . freeVariableMemberAccessChainsToId . iteritems ( ) : <EOL> if dependentId not in stronglyConnectedComponent : <EOL> renamedVariableMapping [ freeVariableMemberAccessChain ] = self . convertedValues [ dependentId ] <EOL> return self . specializeFreeVariableMemberAccessChainsAndEvaluate ( <EOL> createObjectExpression , <EOL> renamedVariableMapping <EOL> ) <EOL> def replaceKnownMemberChainsWithRenamedVariables ( self , <EOL> objectIdToForaFunctionExpression , <EOL> renamedVariableMapping , <EOL> objectIdToObjectDefinition , <EOL> stronglyConnectedComponent ) : <EOL> """<STR_LIT>""" <EOL> tr = dict ( ) <EOL> for objectId in stronglyConnectedComponent : <EOL> objectDefinition = objectIdToObjectDefinition [ objectId ] <EOL> foraFunctionExpression = objectIdToForaFunctionExpression [ objectId ] <EOL> transformedFunction = foraFunctionExpression <EOL> for freeVariableMemberAccessChain , dependentObjectId in objectDefinition . freeVariableMemberAccessChainsToId . iteritems ( ) : <EOL> if dependentObjectId in stronglyConnectedComponent : <EOL> transformedFunction = transformedFunction . rebindFreeVariableMemberAccessChain ( <EOL> tuple ( freeVariableMemberAccessChain . split ( '<STR_LIT:.>' ) ) , <EOL> renamedVariableMapping [ dependentObjectId ] <EOL> ) <EOL> tr [ objectId ] = transformedFunction <EOL> return tr <EOL> def computeRenamedObjectMapping ( self , objectIdToForaFunctionExpression ) : <EOL> """<STR_LIT>""" <EOL> renamedObjectMapping = dict ( ) <EOL> mentionedVariables = set ( ) <EOL> for objectId , foraFunctionExpression in objectIdToForaFunctionExpression . iteritems ( ) : <EOL> mentionedVariables . update ( foraFunctionExpression . mentionedVariables ) <EOL> renamedObjectMapping [ objectId ] = Expression . freshVarname ( <EOL> "<STR_LIT>" % foraFunctionExpression . hash ( ) , <EOL> mentionedVariables <EOL> ) <EOL> return renamedObjectMapping <EOL> def convertStronglyConnectedComponentWithOneNode ( self , <EOL> dependencyGraph , <EOL> stronglyConnectedComponent , <EOL> objectIdToObjectDefinition ) : <EOL> objectId = stronglyConnectedComponent [ <NUM_LIT:0> ] <EOL> objectDefinition = objectIdToObjectDefinition [ objectId ] <EOL> if TypeDescription . isPrimitive ( objectDefinition ) or isinstance ( objectDefinition , list ) : <EOL> self . convertedValues [ objectId ] = self . convertPrimitive ( objectDefinition ) <EOL> elif isinstance ( objectDefinition , ( TypeDescription . FunctionDefinition , <EOL> TypeDescription . ClassDefinition ) ) : <EOL> if isinstance ( objectDefinition , TypeDescription . ClassDefinition ) : <EOL> for baseId in objectDefinition . baseClassIds : <EOL> if baseId not in self . convertedValues : <EOL> self . _convert ( baseId , <EOL> dependencyGraph , <EOL> objectIdToObjectDefinition ) <EOL> assert baseId in self . convertedValues <EOL> self . convertStronglyConnectedComponentWithOneFunctionOrClass ( <EOL> objectId , <EOL> objectDefinition , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . ClassInstanceDescription ) : <EOL> self . convertClassInstanceDescription ( <EOL> objectId , <EOL> objectDefinition <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . List ) : <EOL> self . convertedValues [ objectId ] = self . convertList ( <EOL> objectId , <EOL> self . _computeRestrictedGraph ( <EOL> objectId , <EOL> dependencyGraph <EOL> ) , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . Tuple ) : <EOL> self . convertedValues [ objectId ] = self . convertTuple ( <EOL> objectId , <EOL> self . _computeRestrictedGraph ( <EOL> objectId , <EOL> dependencyGraph <EOL> ) , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . Dict ) : <EOL> self . convertedValues [ objectId ] = self . convertDict ( <EOL> objectId , <EOL> objectDefinition , <EOL> self . _computeRestrictedGraph ( <EOL> objectId , <EOL> dependencyGraph <EOL> ) , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> elif isinstance ( objectDefinition , TypeDescription . File ) : <EOL> self . convertedValues [ objectId ] = self . convertFile ( objectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . RemotePythonObject ) : <EOL> self . convertedValues [ objectId ] = self . convertRemotePythonObject ( objectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . BuiltinExceptionInstance ) : <EOL> self . convertedValues [ objectId ] = self . convertBuiltinExceptionInstance ( objectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . NamedSingleton ) : <EOL> self . convertedValues [ objectId ] = self . convertNamedSingleton ( objectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . WithBlockDescription ) : <EOL> self . convertWithBlock ( objectId , <EOL> objectDefinition , <EOL> objectIdToObjectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . InstanceMethod ) : <EOL> self . convertedValues [ objectId ] = self . convertInstanceMethod ( objectId , <EOL> objectIdToObjectDefinition ) <EOL> elif isinstance ( objectDefinition , TypeDescription . Unconvertible ) : <EOL> self . convertedValues [ objectId ] = Symbol_unconvertible <EOL> else : <EOL> assert False , "<STR_LIT>" % type ( objectDefinition ) <EOL> def convertInstanceMethod ( self , objectId , objectIdToObjectDefinition ) : <EOL> objectDef = objectIdToObjectDefinition [ objectId ] <EOL> instance = self . convertedValues [ objectDef . instanceId ] <EOL> bound = instance . getObjectMember ( objectDef . methodName ) <EOL> assert bound is not None <EOL> return bound <EOL> def convertWithBlock ( self , objectId , withBlockDescription , objectIdToObjectDefinition ) : <EOL> foraFunctionExpression = self . getFunctionExpressionFromWithBlockDescription ( <EOL> withBlockDescription , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> renamedVariableMapping = { } <EOL> for freeVariableMemberAccessChain , dependentId in withBlockDescription . freeVariableMemberAccessChainsToId . iteritems ( ) : <EOL> renamedVariableMapping [ freeVariableMemberAccessChain ] = self . convertedValues [ dependentId ] <EOL> tr = self . specializeFreeVariableMemberAccessChainsAndEvaluate ( <EOL> foraFunctionExpression , <EOL> renamedVariableMapping <EOL> ) <EOL> self . convertedValues [ objectId ] = tr <EOL> return tr <EOL> def getFunctionExpressionFromWithBlockDescription ( self , <EOL> withBlockDescription , <EOL> objectIdToObjectDefinition ) : <EOL> nativeWithBodyAst = self . _getNativePythonFunctionDefFromWithBlockDescription ( <EOL> withBlockDescription , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> sourcePath = objectIdToObjectDefinition [ withBlockDescription . sourceFileId ] . path <EOL> foraFunctionExpression = self . nativeConverter . convertPythonAstWithBlockFunctionDefToForaOrParseError ( <EOL> nativeWithBodyAst . asFunctionDef , <EOL> nativeWithBodyAst . extent , <EOL> ForaNative . CodeDefinitionPoint . ExternalFromStringList ( [ sourcePath ] ) , <EOL> [ x . split ( "<STR_LIT:.>" ) [ <NUM_LIT:0> ] for x in withBlockDescription . freeVariableMemberAccessChainsToId ] <EOL> ) <EOL> if isinstance ( foraFunctionExpression , ForaNative . PythonToForaConversionError ) : <EOL> raise convertNativePythonToForaConversionError ( <EOL> foraFunctionExpression , <EOL> objectIdToObjectDefinition [ withBlockDescription . sourceFileId ] . path <EOL> ) <EOL> return foraFunctionExpression <EOL> def _getNativePythonFunctionDefFromWithBlockDescription ( self , <EOL> withBlockDescription , <EOL> objectIdToObjectDefinition ) : <EOL> sourceText = objectIdToObjectDefinition [ withBlockDescription . sourceFileId ] . text <EOL> sourceLineOffsets = PythonAstConverter . computeLineOffsets ( sourceText ) <EOL> sourceTree = ast . parse ( sourceText ) <EOL> withTree = PyAstUtil . withBlockAtLineNumber ( <EOL> sourceTree , <EOL> withBlockDescription . lineNumber <EOL> ) <EOL> withBodyAsFunctionAst = ast . FunctionDef ( <EOL> name = "<STR_LIT>" , <EOL> lineno = withTree . lineno , <EOL> col_offset = withTree . col_offset , <EOL> args = ast . arguments ( <EOL> args = [ ] , <EOL> vararg = None , <EOL> kwarg = None , <EOL> defaults = [ ] ) , <EOL> body = withTree . body , <EOL> decorator_list = [ ] <EOL> ) <EOL> nativeWithBodyAst = PythonAstConverter . convertPythonAstToForaPythonAst ( <EOL> withBodyAsFunctionAst , <EOL> sourceLineOffsets <EOL> ) <EOL> return nativeWithBodyAst <EOL> def convertFile ( self , objectDefinition ) : <EOL> return objectDefinition . text <EOL> def _computeRestrictedGraph ( self , objectId , dependencyGraph ) : <EOL> """<STR_LIT>""" <EOL> tr = dict ( ) <EOL> stack = [ objectId ] <EOL> visited = set ( ) <EOL> while stack : <EOL> toVisit = stack . pop ( ) <EOL> if toVisit not in visited : <EOL> tr [ toVisit ] = dependencyGraph [ toVisit ] <EOL> stack . extend ( dependencyGraph [ toVisit ] ) <EOL> visited . add ( toVisit ) <EOL> return tr <EOL> def convertClassInstanceDescription ( self , objectId , classInstanceDescription ) : <EOL> classMemberNameToImplVal = { <EOL> classMemberName : self . convertedValues [ memberId ] <EOL> for classMemberName , memberId in <EOL> classInstanceDescription . classMemberNameToClassMemberId . iteritems ( ) <EOL> } <EOL> classImplVal = self . convertedValues [ classInstanceDescription . classId ] <EOL> if classImplVal . isSymbol ( ) : <EOL> self . convertedValues [ objectId ] = classImplVal <EOL> return <EOL> memberNames = tuple ( sorted ( name for name in classMemberNameToImplVal . iterkeys ( ) ) ) <EOL> memberValues = tuple ( classMemberNameToImplVal [ name ] for name in memberNames ) <EOL> convertedValueOrNone = ForaNative . simulateApply ( <EOL> ForaNative . ImplValContainer ( <EOL> ( classImplVal , <EOL> Symbol_CreateInstance , <EOL> ForaNative . CreateNamedTuple ( memberValues , memberNames ) ) <EOL> ) <EOL> ) <EOL> if convertedValueOrNone is None : <EOL> raise pyfora . PythonToForaConversionError ( <EOL> ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> ) <EOL> self . convertedValues [ objectId ] = convertedValueOrNone <EOL> def convertStronglyConnectedComponentWithOneFunctionOrClass ( self , <EOL> objectId , <EOL> classOrFunctionDefinition , <EOL> objectIdToObjectDefinition ) : <EOL> foraExpression = self . convertPyClassOrFunctionDefinitionToForaFunctionExpression ( <EOL> classOrFunctionDefinition , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> renamedVariableMapping = { } <EOL> for freeVariableMemberAccessChain , dependentId in classOrFunctionDefinition . freeVariableMemberAccessChainsToId . iteritems ( ) : <EOL> renamedVariableMapping [ freeVariableMemberAccessChain ] = self . convertedValues [ dependentId ] <EOL> if isinstance ( classOrFunctionDefinition , TypeDescription . ClassDefinition ) : <EOL> for i , baseId in enumerate ( classOrFunctionDefinition . baseClassIds ) : <EOL> renamedVariableMapping [ "<STR_LIT>" % i ] = self . convertedValues [ baseId ] <EOL> self . convertedValues [ objectId ] = self . specializeFreeVariableMemberAccessChainsAndEvaluate ( <EOL> foraExpression , <EOL> renamedVariableMapping <EOL> ) <EOL> def specializeFreeVariableMemberAccessChainsAndEvaluate ( self , <EOL> foraExpression , <EOL> renamedVariableMapping ) : <EOL> foraExpression , renamedVariableMapping = self . reduceFreeVariableMemberAccessChains ( <EOL> foraExpression , <EOL> renamedVariableMapping <EOL> ) <EOL> foraExpression = self . handleUnconvertibleValuesInExpression ( <EOL> foraExpression , <EOL> renamedVariableMapping <EOL> ) <EOL> return self . specializeFreeVariablesAndEvaluate ( <EOL> foraExpression , <EOL> renamedVariableMapping <EOL> ) <EOL> def handleUnconvertibleValuesInExpression ( <EOL> self , <EOL> foraExpression , <EOL> renamedVariableMapping <EOL> ) : <EOL> unconvertibles = [ <EOL> k for k , v in renamedVariableMapping . iteritems ( ) if v == Symbol_unconvertible <EOL> ] <EOL> foraExpression = self . nativeConverter . replaceUnconvertiblesWithThrowExprs ( <EOL> foraExpression , <EOL> unconvertibles <EOL> ) <EOL> return foraExpression <EOL> def specializeFreeVariablesAndEvaluate ( <EOL> self , <EOL> foraExpression , <EOL> renamedVariableMapping <EOL> ) : <EOL> allAreIVC = True <EOL> for _ , v in renamedVariableMapping . iteritems ( ) : <EOL> if not isinstance ( v , ForaNative . ImplValContainer ) : <EOL> allAreIVC = False <EOL> if allAreIVC : <EOL> missingVariableDefinitions = [ <EOL> x for x in foraExpression . freeVariables if x not in renamedVariableMapping <EOL> ] <EOL> if missingVariableDefinitions : <EOL> raise pyfora . PythonToForaConversionError ( <EOL> ( "<STR_LIT>" + <EOL> "<STR_LIT>" % missingVariableDefinitions + <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) ) <EOL> shouldMapArgsAsConstants = True <EOL> boundValues = tuple ( renamedVariableMapping [ k ] . hash for k in sorted ( renamedVariableMapping ) ) <EOL> if foraExpression . hash ( ) not in self . boundExpressions : <EOL> self . boundExpressions [ foraExpression . hash ( ) ] = boundValues <EOL> else : <EOL> bound = self . boundExpressions [ foraExpression . hash ( ) ] <EOL> if boundValues != bound : <EOL> shouldMapArgsAsConstants = False <EOL> return ForaNative . evaluateRootLevelCreateObjectExpression ( <EOL> foraExpression , <EOL> renamedVariableMapping , <EOL> shouldMapArgsAsConstants <EOL> ) <EOL> else : <EOL> expressionAsIVC = foraExpression . toFunctionImplval ( False ) <EOL> args = [ ] <EOL> for f in foraExpression . freeVariables : <EOL> args . append ( renamedVariableMapping [ f ] ) <EOL> res = ComputedValue . ComputedValue ( <EOL> args = ( expressionAsIVC , Symbol_Call ) + tuple ( args ) <EOL> ) <EOL> return res <EOL> def reduceFreeVariableMemberAccessChains ( self , <EOL> expr , <EOL> freeVariableMemberAccessChainToImplValMap ) : <EOL> """<STR_LIT>""" <EOL> renamedVariableMapping = { } <EOL> for chain , implval in freeVariableMemberAccessChainToImplValMap . iteritems ( ) : <EOL> assert isinstance ( chain , str ) <EOL> chain = chain . split ( '<STR_LIT:.>' ) <EOL> if len ( chain ) == <NUM_LIT:1> : <EOL> renamedVariableMapping [ chain [ <NUM_LIT:0> ] ] = implval <EOL> else : <EOL> newName = Expression . freshVarname ( <EOL> '<STR_LIT:_>' . join ( chain ) , <EOL> set ( expr . mentionedVariables ) <EOL> ) <EOL> renamedVariableMapping [ newName ] = implval <EOL> expr = expr . rebindFreeVariableMemberAccessChain ( <EOL> chain , <EOL> newName <EOL> ) <EOL> return expr , renamedVariableMapping <EOL> def convertPyClassOrFunctionDefinitionToForaFunctionExpression ( self , <EOL> classOrFunctionDefinition , <EOL> objectIdToObjectDefinition ) : <EOL> pyAst = self . convertClassOrFunctionDefinitionToNativePyAst ( <EOL> classOrFunctionDefinition , <EOL> objectIdToObjectDefinition <EOL> ) <EOL> assert pyAst is not None <EOL> sourcePath = objectIdToObjectDefinition [ classOrFunctionDefinition . sourceFileId ] . path <EOL> tr = None <EOL> if isinstance ( classOrFunctionDefinition , TypeDescription . FunctionDefinition ) : <EOL> if isinstance ( pyAst , ForaNative . PythonAstStatement ) and pyAst . isFunctionDef ( ) : <EOL> tr = self . nativeConverter . convertPythonAstFunctionDefToForaOrParseError ( <EOL> pyAst . asFunctionDef , <EOL> pyAst . extent , <EOL> ForaNative . CodeDefinitionPoint . ExternalFromStringList ( [ sourcePath ] ) <EOL> ) <EOL> else : <EOL> assert pyAst . isLambda ( ) <EOL> tr = self . nativeConverter . convertPythonAstLambdaToForaOrParseError ( <EOL> pyAst . asLambda , <EOL> pyAst . extent , <EOL> ForaNative . CodeDefinitionPoint . ExternalFromStringList ( [ sourcePath ] ) <EOL> ) <EOL> elif isinstance ( classOrFunctionDefinition , TypeDescription . ClassDefinition ) : <EOL> objectIdToFreeVar = { <EOL> v : k <EOL> for k , v in classOrFunctionDefinition . freeVariableMemberAccessChainsToId . iteritems ( ) <EOL> } <EOL> baseClasses = [ <EOL> objectIdToFreeVar [ baseId ] . split ( '<STR_LIT:.>' ) <EOL> for baseId in classOrFunctionDefinition . baseClassIds <EOL> ] <EOL> tr = self . nativeConverter . convertPythonAstClassDefToForaOrParseError ( <EOL> pyAst . asClassDef , <EOL> pyAst . extent , <EOL> ForaNative . CodeDefinitionPoint . ExternalFromStringList ( [ sourcePath ] ) , <EOL> baseClasses <EOL> ) <EOL> else : <EOL> assert False <EOL> if isinstance ( tr , ForaNative . PythonToForaConversionError ) : <EOL> raise convertNativePythonToForaConversionError ( <EOL> tr , <EOL> sourcePath <EOL> ) <EOL> return tr <EOL> @ staticmethod <EOL> def convertClassOrFunctionDefinitionToNativePyAst ( classOrFunctionDefinition , <EOL> objectIdToObjectDefinition ) : <EOL> sourceText = objectIdToObjectDefinition [ classOrFunctionDefinition . sourceFileId ] . text <EOL> pyAst = PythonAstConverter . parseStringToPythonAst ( sourceText ) <EOL> assert pyAst is not None <EOL> pyAst = pyAst . functionClassOrLambdaDefAtLine ( classOrFunctionDefinition . lineNumber ) <EOL> assert pyAst is not None , ( sourceText , classOrFunctionDefinition . lineNumber ) <EOL> return pyAst <EOL> def unwrapPyforaDictToDictOfAssignedVars ( self , dictIVC ) : <EOL> """<STR_LIT>""" <EOL> pyforaDict = self . nativeDictConverter . invertDict ( dictIVC ) <EOL> if pyforaDict is None : <EOL> return None <EOL> res = { } <EOL> for pyforaKey , pyforaValue in pyforaDict . iteritems ( ) : <EOL> if pyforaValue != Symbol_uninitialized and pyforaValue != Symbol_invalid : <EOL> maybePyforaKeyString = self . constantConverter . invertForaConstant ( pyforaKey ) <EOL> if isinstance ( maybePyforaKeyString , tuple ) and isinstance ( maybePyforaKeyString [ <NUM_LIT:0> ] , str ) : <EOL> res [ maybePyforaKeyString [ <NUM_LIT:0> ] ] = pyforaValue <EOL> else : <EOL> return None <EOL> return res <EOL> def unwrapPyforaTupleToTuple ( self , tupleIVC ) : <EOL> pyforaTuple = self . nativeTupleConverter . invertTuple ( tupleIVC ) <EOL> if pyforaTuple is None : <EOL> return None <EOL> res = tuple ( [ pyforaValue for pyforaValue in pyforaTuple ] ) <EOL> return res <EOL> def transformPyforaImplval ( self , implval , transformer , vectorContentsExtractor ) : <EOL> objectDefinitions = { } <EOL> hashToObjectId = { } <EOL> def transform ( implval ) : <EOL> """<STR_LIT>""" <EOL> if implval . hash in hashToObjectId : <EOL> return hashToObjectId [ implval . hash ] <EOL> newId = str ( len ( objectDefinitions ) ) <EOL> objectDefinitions [ newId ] = None <EOL> hashToObjectId [ implval . hash ] = newId <EOL> objectDefinitions [ newId ] = transformBody ( implval ) <EOL> return newId <EOL> def transformBody ( implval ) : <EOL> value = self . constantConverter . invertForaConstant ( implval ) <EOL> if value is not None : <EOL> if isinstance ( value , tuple ) : <EOL> return transformer . transformPrimitive ( value [ <NUM_LIT:0> ] ) <EOL> else : <EOL> assert isinstance ( value , ForaNative . ImplValContainer ) <EOL> if len ( value ) == <NUM_LIT:0> : <EOL> return transformer . transformPrimitive ( "<STR_LIT>" ) <EOL> assert value . isVectorOfChar ( ) <EOL> contents = vectorContentsExtractor ( value ) <EOL> if contents is None : <EOL> return transformer . transformStringThatNeedsLoading ( len ( value ) ) <EOL> else : <EOL> assert '<STR_LIT:string>' in contents <EOL> return transformer . transformPrimitive ( contents [ '<STR_LIT:string>' ] ) <EOL> if self . singletonAndExceptionConverter is not None : <EOL> value = self . singletonAndExceptionConverter . convertInstanceToSingletonName ( implval ) <EOL> if value is not None : <EOL> return transformer . transformSingleton ( value ) <EOL> value = self . singletonAndExceptionConverter . convertExceptionInstance ( implval ) <EOL> if value is not None : <EOL> return transformer . transformBuiltinException ( <EOL> value [ <NUM_LIT:0> ] , <EOL> transform ( value [ <NUM_LIT:1> ] ) <EOL> ) <EOL> value = self . singletonAndExceptionConverter . convertPyAbortExceptionInstance ( <EOL> implval <EOL> ) <EOL> if value is not None : <EOL> return transformer . transformPyAbortException ( <EOL> value [ <NUM_LIT:0> ] , <EOL> transform ( value [ <NUM_LIT:1> ] ) <EOL> ) <EOL> value = self . nativeTupleConverter . invertTuple ( implval ) <EOL> if value is not None : <EOL> return transformer . transformTuple ( [ transform ( x ) for x in value ] ) <EOL> value = self . nativeDictConverter . invertDict ( implval ) <EOL> if value is not None : <EOL> return transformer . transformDict ( <EOL> keys = [ transform ( k ) for k in value . keys ( ) ] , <EOL> values = [ transform ( v ) for v in value . values ( ) ] <EOL> ) <EOL> listItemsAsVector = self . nativeListConverter . invertList ( implval ) <EOL> if listItemsAsVector is not None : <EOL> contents = vectorContentsExtractor ( listItemsAsVector ) <EOL> if contents is None : <EOL> return transformer . transformListThatNeedsLoading ( len ( listItemsAsVector ) ) <EOL> elif '<STR_LIT>' in contents : <EOL> return transformer . transformList ( [ transform ( x ) for x in contents [ '<STR_LIT>' ] ] ) <EOL> else : <EOL> assert '<STR_LIT>' in contents <EOL> firstElement = contents [ '<STR_LIT>' ] <EOL> contentsAsNumpy = contents [ '<STR_LIT>' ] <EOL> return transformer . transformHomogenousList ( transform ( firstElement ) , contentsAsNumpy ) <EOL> if implval . isTuple ( ) : <EOL> stackTraceAsJsonOrNone = self . getStackTraceAsJsonOrNone ( implval ) <EOL> if stackTraceAsJsonOrNone is not None : <EOL> return stackTraceAsJsonOrNone <EOL> if implval . isObject ( ) : <EOL> objectClass = implval . getObjectClass ( ) <EOL> if objectClass == self . pyforaBoundMethodClass : <EOL> nameAsImplval = implval . getObjectLexicalMember ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> if not nameAsImplval . isSymbol ( ) : <EOL> raise pyfora . ForaToPythonConversionError ( <EOL> "<STR_LIT>" <EOL> % ( nameAsImplval , nameAsImplval . type ) <EOL> ) <EOL> return transformer . transformBoundMethod ( <EOL> transform ( implval . getObjectLexicalMember ( "<STR_LIT>" ) [ <NUM_LIT:0> ] ) , <EOL> nameAsImplval . pyval [ <NUM_LIT:1> : ] <EOL> ) <EOL> defPoint = implval . getObjectDefinitionPoint ( ) <EOL> if defPoint is not None : <EOL> if objectClass is not None : <EOL> classObject = transform ( objectClass ) <EOL> members = { } <EOL> for memberName in objectClass . objectMembers : <EOL> if memberName is not None : <EOL> member = implval . getObjectLexicalMember ( memberName ) <EOL> if member is not None and member [ <NUM_LIT:1> ] is None : <EOL> assert memberName == "<STR_LIT>" <EOL> assert member [ <NUM_LIT:0> ] . isTuple ( ) <EOL> membersTuple = member [ <NUM_LIT:0> ] <EOL> memberNames = membersTuple . getTupleNames ( ) <EOL> for i , name in enumerate ( memberNames ) : <EOL> members [ str ( name ) ] = transform ( membersTuple [ i ] ) <EOL> return transformer . transformClassInstance ( classObject , members ) <EOL> else : <EOL> members = { } <EOL> lexicalMembers = implval . objectLexicalMembers <EOL> for memberAndBindingSequence in lexicalMembers . iteritems ( ) : <EOL> if isinstance ( memberAndBindingSequence [ <NUM_LIT:1> ] , ForaNative . ImplValContainer ) or memberAndBindingSequence [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] : <EOL> memberName = memberAndBindingSequence [ <NUM_LIT:0> ] <EOL> member = implval . getObjectLexicalMember ( memberName ) <EOL> if member is not None and member [ <NUM_LIT:1> ] is None : <EOL> members [ str ( memberName ) ] = transform ( member [ <NUM_LIT:0> ] ) <EOL> return transformer . transformFunctionInstance ( <EOL> defPoint . defPoint . asExternal . paths [ <NUM_LIT:0> ] , <EOL> defPoint . range . start . line , <EOL> members <EOL> ) <EOL> elif implval . isClass ( ) : <EOL> members = { } <EOL> defPoint = implval . getObjectDefinitionPoint ( ) <EOL> lexicalMembers = implval . objectLexicalMembers <EOL> for memberAndBindingSequence in lexicalMembers . iteritems ( ) : <EOL> if isinstance ( memberAndBindingSequence [ <NUM_LIT:1> ] , ForaNative . ImplValContainer ) or memberAndBindingSequence [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] : <EOL> memberName = memberAndBindingSequence [ <NUM_LIT:0> ] <EOL> member = implval . getObjectLexicalMember ( memberName ) <EOL> if member is not None and member [ <NUM_LIT:1> ] is None : <EOL> members [ str ( memberName ) ] = transform ( member [ <NUM_LIT:0> ] ) <EOL> return transformer . transformClassObject ( defPoint . defPoint . asExternal . paths [ <NUM_LIT:0> ] , <EOL> defPoint . range . start . line , <EOL> members ) <EOL> logging . error ( "<STR_LIT>" , implval , str ( implval . type ) ) <EOL> raise pyfora . ForaToPythonConversionError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> root_id = transform ( implval ) <EOL> return { '<STR_LIT>' : objectDefinitions , '<STR_LIT>' : root_id } <EOL> def getStackTraceAsJsonOrNone ( self , implval ) : <EOL> tup = implval . getTuple ( ) <EOL> if len ( tup ) != <NUM_LIT:2> : <EOL> return None <EOL> return self . exceptionCodeLocationsAsJson ( tup ) <EOL> def exceptionCodeLocationsAsJson ( self , stacktraceAndVarsInScope ) : <EOL> hashes = stacktraceAndVarsInScope [ <NUM_LIT:0> ] . getStackTrace ( ) <EOL> if hashes is None : <EOL> return None <EOL> codeLocations = [ ForaNative . getCodeLocation ( h ) for h in hashes ] <EOL> codeLocations = [ c for c in codeLocations if c is not None ] <EOL> def formatCodeLocation ( c ) : <EOL> if not c . defPoint . isExternal ( ) : <EOL> return None <EOL> def posToJson ( simpleParsePosition ) : <EOL> return { <EOL> '<STR_LIT>' : simpleParsePosition . rawOffset , <EOL> '<STR_LIT>' : simpleParsePosition . line , <EOL> '<STR_LIT>' : simpleParsePosition . col <EOL> } <EOL> return { <EOL> '<STR_LIT:path>' : list ( c . defPoint . asExternal . paths ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:start>' : posToJson ( c . range . start ) , <EOL> '<STR_LIT>' : posToJson ( c . range . stop ) <EOL> } <EOL> } <EOL> return { <EOL> '<STR_LIT>' : [ <EOL> x for x in [ formatCodeLocation ( c ) for c in codeLocations ] if x is not None <EOL> ] <EOL> } </s>
