<s> """<STR_LIT>""" <EOL> import pyfora . NamedSingletons as NamedSingletons <EOL> import pyfora . PyAbortSingletons as PyAbortSingletons <EOL> import ufora . native . FORA as ForaNative <EOL> import ufora . FORA . python . FORA as FORA <EOL> import logging <EOL> class PyforaSingletonAndExceptionConverter : <EOL> def __init__ ( self , pyforaBuiltinsModule ) : <EOL> self . pyforaBuiltinsModule = pyforaBuiltinsModule <EOL> self . pythonNameToInstance = { } <EOL> self . instanceToPythonName = { } <EOL> self . pyExceptionClass = pyforaBuiltinsModule . getObjectMember ( "<STR_LIT>" ) <EOL> self . pyAbortExceptionClass = pyforaBuiltinsModule . getObjectMember ( "<STR_LIT>" ) <EOL> self . pyExceptionClassInstanceName = ForaNative . simulateApply ( <EOL> ForaNative . ImplValContainer ( <EOL> ( self . pyExceptionClass , <EOL> ForaNative . makeSymbol ( "<STR_LIT>" ) , <EOL> ForaNative . ImplValContainer ( ) , <EOL> ForaNative . ImplValContainer ( ) <EOL> ) <EOL> ) <EOL> ) . getClassName ( ) <EOL> self . pyAbortExceptionClassInstanceName = ForaNative . simulateApply ( <EOL> ForaNative . ImplValContainer ( <EOL> ( self . pyAbortExceptionClass , <EOL> ForaNative . makeSymbol ( "<STR_LIT>" ) , <EOL> ForaNative . ImplValContainer ( ) , <EOL> ForaNative . ImplValContainer ( ) <EOL> ) <EOL> ) <EOL> ) . getClassName ( ) <EOL> self . pythonNameToPyforaName = { } <EOL> self . pythonNameToPyforaName . update ( <EOL> NamedSingletons . pythonNameToPyforaName <EOL> ) <EOL> self . pythonNameToPyforaName . update ( <EOL> PyAbortSingletons . pythonNameToPyforaName <EOL> ) <EOL> for pyName , pyforaName in self . pythonNameToPyforaName . iteritems ( ) : <EOL> try : <EOL> instance = FORA . ForaValue . FORAValue ( pyforaBuiltinsModule ) . __getattr__ ( pyforaName ) . implVal_ <EOL> except : <EOL> logging . error ( <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" , pyforaName ) <EOL> raise <EOL> self . pythonNameToInstance [ pyName ] = instance <EOL> self . instanceToPythonName [ instance ] = pyName <EOL> def convertSingletonByName ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name not in self . pythonNameToInstance : <EOL> return None <EOL> return self . pythonNameToInstance [ name ] <EOL> def convertInstanceToSingletonName ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> if instance not in self . instanceToPythonName : <EOL> return None <EOL> return self . instanceToPythonName [ instance ] <EOL> def convertExceptionInstance ( self , exceptionInstance ) : <EOL> instanceClassName = exceptionInstance . getClassName ( ) <EOL> if instanceClassName == self . pyExceptionClassInstanceName : <EOL> typeInstance = exceptionInstance . getObjectLexicalMember ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> typeInstanceName = self . convertInstanceToSingletonName ( typeInstance ) <EOL> assert typeInstanceName is not None <EOL> args = exceptionInstance . getObjectLexicalMember ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> return ( typeInstanceName , args ) <EOL> return None <EOL> def convertPyAbortExceptionInstance ( self , exceptionInstance ) : <EOL> instanceClassName = exceptionInstance . getClassName ( ) <EOL> if instanceClassName == self . pyAbortExceptionClassInstanceName : <EOL> typeInstance = exceptionInstance . getObjectLexicalMember ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> typeInstanceName = self . convertInstanceToSingletonName ( typeInstance ) <EOL> assert typeInstanceName is not None <EOL> args = exceptionInstance . getObjectLexicalMember ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> return ( typeInstanceName , args ) <EOL> return None <EOL> def convertInvalidPyforaOperationInstance ( self , instance ) : <EOL> if instance . getClassName ( ) == self . invalidPyforaOperationClassInstanceName : <EOL> result = instance . getObjectLexicalMember ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> if not result . isString ( ) : <EOL> raise pyfora . ForaToPythonConversionError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> return result . pyval <EOL> def instantiateException ( self , exceptionTypeName , exceptionArgsAsPyTuple ) : <EOL> """<STR_LIT>""" <EOL> exceptionTypeInstance = self . convertSingletonByName ( exceptionTypeName ) <EOL> if exceptionTypeInstance is None : <EOL> return None <EOL> args = ( <EOL> self . pyExceptionClass , <EOL> ForaNative . makeSymbol ( "<STR_LIT>" ) , <EOL> exceptionTypeInstance , <EOL> exceptionArgsAsPyTuple <EOL> ) <EOL> return ForaNative . simulateApply ( ForaNative . ImplValContainer ( args ) ) </s>
