<s> """<STR_LIT>""" <EOL> from nuitka import Options , VariableRegistry , Variables <EOL> from nuitka . optimizations . FunctionInlining import convertFunctionCallToOutline <EOL> from nuitka . PythonVersions import python_version <EOL> from nuitka . tree . Extractions import updateVariableUsage <EOL> from . Checkers import checkStatementsSequenceOrNone <EOL> from . IndicatorMixins import ( <EOL> MarkLocalsDictIndicator , <EOL> MarkUnoptimizedFunctionIndicator <EOL> ) <EOL> from . NodeBases import ( <EOL> ChildrenHavingMixin , <EOL> ClosureGiverNodeBase , <EOL> ClosureTakerMixin , <EOL> CompileTimeConstantExpressionMixin , <EOL> ExpressionChildrenHavingBase , <EOL> ExpressionMixin , <EOL> NodeBase , <EOL> SideEffectsFromChildrenMixin <EOL> ) <EOL> from . NodeMakingHelpers import ( <EOL> makeConstantReplacementNode , <EOL> makeRaiseExceptionReplacementExpressionFromInstance , <EOL> wrapExpressionWithSideEffects <EOL> ) <EOL> from . ParameterSpecs import TooManyArguments , matchCall <EOL> class ExpressionFunctionBodyBase ( ClosureTakerMixin , ChildrenHavingMixin , <EOL> ClosureGiverNodeBase , ExpressionMixin ) : <EOL> def __init__ ( self , provider , name , code_prefix , is_class , flags , source_ref ) : <EOL> ClosureTakerMixin . __init__ ( <EOL> self , <EOL> provider = provider , <EOL> early_closure = is_class <EOL> ) <EOL> ClosureGiverNodeBase . __init__ ( <EOL> self , <EOL> name = name , <EOL> code_prefix = code_prefix , <EOL> source_ref = source_ref <EOL> ) <EOL> ChildrenHavingMixin . __init__ ( <EOL> self , <EOL> values = { <EOL> "<STR_LIT:body>" : None <EOL> } <EOL> ) <EOL> self . flags = flags <EOL> self . parent = provider <EOL> if python_version >= <NUM_LIT> : <EOL> self . qualname_provider = provider <EOL> self . non_local_declarations = [ ] <EOL> provider . getParentModule ( ) . addFunction ( self ) <EOL> self . constraint_collection = None <EOL> @ staticmethod <EOL> def isExpressionFunctionBodyBase ( ) : <EOL> return True <EOL> def getContainingClassDictCreation ( self ) : <EOL> current = self <EOL> while not current . isCompiledPythonModule ( ) : <EOL> if current . isExpressionClassBody ( ) : <EOL> return current <EOL> current = current . getParentVariableProvider ( ) <EOL> return None <EOL> def hasFlag ( self , flag ) : <EOL> return flag in self . flags <EOL> def getLocalsMode ( self ) : <EOL> if python_version >= <NUM_LIT> : <EOL> return "<STR_LIT>" <EOL> elif self . isEarlyClosure ( ) or self . isUnoptimized ( ) : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> def hasVariableName ( self , variable_name ) : <EOL> return variable_name in self . providing or variable_name in self . temp_variables <EOL> def getVariables ( self ) : <EOL> return self . providing . values ( ) <EOL> def getLocalVariables ( self ) : <EOL> return [ <EOL> variable for <EOL> variable in <EOL> self . providing . values ( ) <EOL> if variable . isLocalVariable ( ) <EOL> ] <EOL> def getUserLocalVariables ( self ) : <EOL> return tuple ( <EOL> variable for <EOL> variable in <EOL> self . providing . values ( ) <EOL> if variable . isLocalVariable ( ) and not variable . isParameterVariable ( ) <EOL> if variable . getOwner ( ) is self <EOL> ) <EOL> def removeClosureVariable ( self , variable ) : <EOL> assert variable in self . providing . values ( ) , ( self . providing , variable ) <EOL> del self . providing [ variable . getName ( ) ] <EOL> assert not variable . isParameterVariable ( ) or variable . getOwner ( ) is not self <EOL> self . taken . remove ( variable ) <EOL> VariableRegistry . removeVariableUsage ( variable , self ) <EOL> def demoteClosureVariable ( self , variable ) : <EOL> assert variable . isLocalVariable ( ) <EOL> self . taken . remove ( variable ) <EOL> assert variable . getOwner ( ) is not self <EOL> new_variable = Variables . LocalVariable ( <EOL> owner = self , <EOL> variable_name = variable . getName ( ) <EOL> ) <EOL> self . providing [ variable . getName ( ) ] = new_variable <EOL> updateVariableUsage ( <EOL> provider = self , <EOL> old_variable = variable , <EOL> new_variable = new_variable <EOL> ) <EOL> VariableRegistry . removeVariableUsage ( variable , self ) <EOL> VariableRegistry . addVariableUsage ( new_variable , self ) <EOL> def removeUserVariable ( self , variable ) : <EOL> assert variable in self . providing . values ( ) , ( self . providing , variable ) <EOL> del self . providing [ variable . getName ( ) ] <EOL> assert not variable . isParameterVariable ( ) or variable . getOwner ( ) is not self <EOL> VariableRegistry . removeVariableUsage ( variable , self ) <EOL> def getVariableForAssignment ( self , variable_name ) : <EOL> if self . hasTakenVariable ( variable_name ) : <EOL> result = self . getTakenVariable ( variable_name ) <EOL> else : <EOL> result = self . getProvidedVariable ( variable_name ) <EOL> return result <EOL> def getVariableForReference ( self , variable_name ) : <EOL> if self . hasProvidedVariable ( variable_name ) : <EOL> result = self . getProvidedVariable ( variable_name ) <EOL> else : <EOL> result = self . getClosureVariable ( <EOL> variable_name = variable_name <EOL> ) <EOL> if not result . isModuleVariable ( ) : <EOL> self . registerProvidedVariable ( result ) <EOL> if not self . isExpressionClassBody ( ) and self . isUnoptimized ( ) and result . isModuleVariable ( ) : <EOL> result = Variables . MaybeLocalVariable ( <EOL> owner = self , <EOL> maybe_variable = result <EOL> ) <EOL> self . registerProvidedVariable ( result ) <EOL> return result <EOL> def getVariableForClosure ( self , variable_name ) : <EOL> if self . hasProvidedVariable ( variable_name ) : <EOL> return self . getProvidedVariable ( variable_name ) <EOL> else : <EOL> return self . provider . getVariableForClosure ( variable_name ) <EOL> def createProvidedVariable ( self , variable_name ) : <EOL> return Variables . LocalVariable ( <EOL> owner = self , <EOL> variable_name = variable_name <EOL> ) <EOL> def addNonlocalsDeclaration ( self , names , source_ref ) : <EOL> self . non_local_declarations . append ( <EOL> ( names , source_ref ) <EOL> ) <EOL> def getNonlocalDeclarations ( self ) : <EOL> return self . non_local_declarations <EOL> def getFunctionName ( self ) : <EOL> return self . name <EOL> def getFunctionQualname ( self ) : <EOL> """<STR_LIT>""" <EOL> function_name = self . getFunctionName ( ) <EOL> if python_version < <NUM_LIT> : <EOL> provider = self . getParentVariableProvider ( ) <EOL> else : <EOL> provider = self . qualname_provider <EOL> if provider . isCompiledPythonModule ( ) : <EOL> return function_name <EOL> elif provider . isExpressionClassBody ( ) : <EOL> return provider . getFunctionQualname ( ) + '<STR_LIT:.>' + function_name <EOL> else : <EOL> return provider . getFunctionQualname ( ) + "<STR_LIT>" + function_name <EOL> def computeExpression ( self , constraint_collection ) : <EOL> assert False <EOL> return self , None , None <EOL> def mayRaiseException ( self , exception_type ) : <EOL> body = self . getBody ( ) <EOL> if body is None : <EOL> return False <EOL> else : <EOL> return self . getBody ( ) . mayRaiseException ( exception_type ) <EOL> class ExpressionFunctionBody ( ExpressionFunctionBodyBase , <EOL> MarkLocalsDictIndicator , <EOL> MarkUnoptimizedFunctionIndicator ) : <EOL> kind = "<STR_LIT>" <EOL> named_children = ( <EOL> "<STR_LIT:body>" , <EOL> ) <EOL> checkers = { <EOL> "<STR_LIT:body>" : checkStatementsSequenceOrNone <EOL> } <EOL> if python_version >= <NUM_LIT> : <EOL> qualname_setup = None <EOL> def __init__ ( self , provider , name , doc , parameters , flags , source_ref ) : <EOL> while provider . isExpressionOutlineBody ( ) : <EOL> provider = provider . getParentVariableProvider ( ) <EOL> if name == "<STR_LIT>" : <EOL> assert python_version >= <NUM_LIT> <EOL> ExpressionFunctionBodyBase . __init__ ( <EOL> self , <EOL> provider = provider , <EOL> name = name , <EOL> code_prefix = "<STR_LIT>" , <EOL> is_class = False , <EOL> flags = flags , <EOL> source_ref = source_ref <EOL> ) <EOL> MarkLocalsDictIndicator . __init__ ( self ) <EOL> MarkUnoptimizedFunctionIndicator . __init__ ( self ) <EOL> self . doc = doc <EOL> self . return_exception = False <EOL> self . needs_creation = False <EOL> self . needs_direct = False <EOL> self . cross_module_use = False <EOL> self . parameters = parameters <EOL> self . parameters . setOwner ( self ) <EOL> self . registerProvidedVariables ( <EOL> * self . parameters . getAllVariables ( ) <EOL> ) <EOL> def getDetails ( self ) : <EOL> return { <EOL> "<STR_LIT:name>" : self . getFunctionName ( ) , <EOL> "<STR_LIT>" : self . getCodeName ( ) , <EOL> "<STR_LIT>" : self . getParameters ( ) , <EOL> "<STR_LIT>" : self . provider . getCodeName ( ) , <EOL> "<STR_LIT>" : self . doc <EOL> } <EOL> def getDetail ( self ) : <EOL> return "<STR_LIT>" % ( self . getFunctionName ( ) , self . parameters ) <EOL> def getParent ( self ) : <EOL> assert False <EOL> def getDoc ( self ) : <EOL> return self . doc <EOL> def getParameters ( self ) : <EOL> return self . parameters <EOL> getBody = ChildrenHavingMixin . childGetter ( "<STR_LIT:body>" ) <EOL> setBody = ChildrenHavingMixin . childSetter ( "<STR_LIT:body>" ) <EOL> def needsCreation ( self ) : <EOL> return self . needs_creation <EOL> def markAsNeedsCreation ( self ) : <EOL> self . needs_creation = True <EOL> def needsDirectCall ( self ) : <EOL> return self . needs_direct <EOL> def markAsDirectlyCalled ( self ) : <EOL> self . needs_direct = True <EOL> def isCrossModuleUsed ( self ) : <EOL> return self . cross_module_use <EOL> def markAsCrossModuleUsed ( self ) : <EOL> self . cross_module_use = True <EOL> def computeExpressionCall ( self , call_node , call_args , call_kw , <EOL> constraint_collection ) : <EOL> assert False , self <EOL> def isCompileTimeConstant ( self ) : <EOL> return None <EOL> def mayHaveSideEffects ( self ) : <EOL> return False <EOL> def mayRaiseException ( self , exception_type ) : <EOL> return self . getBody ( ) . mayRaiseException ( exception_type ) <EOL> def markAsExceptionReturnValue ( self ) : <EOL> self . return_exception = True <EOL> def needsExceptionReturnValue ( self ) : <EOL> return self . return_exception <EOL> def convertNoneConstantOrEmptyDictToNone ( node ) : <EOL> if node is None : <EOL> return None <EOL> elif node . isExpressionConstantRef ( ) and node . getConstant ( ) is None : <EOL> return None <EOL> elif node . isExpressionConstantRef ( ) and node . getConstant ( ) == { } : <EOL> return None <EOL> else : <EOL> return node <EOL> class ExpressionFunctionCreation ( SideEffectsFromChildrenMixin , <EOL> ExpressionChildrenHavingBase ) : <EOL> kind = "<STR_LIT>" <EOL> kw_defaults_before_defaults = python_version < <NUM_LIT> <EOL> if kw_defaults_before_defaults : <EOL> named_children = ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> else : <EOL> named_children = ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> checkers = { <EOL> "<STR_LIT>" : convertNoneConstantOrEmptyDictToNone , <EOL> } <EOL> def __init__ ( self , function_ref , code_object , defaults , kw_defaults , <EOL> annotations , source_ref ) : <EOL> assert kw_defaults is None or kw_defaults . isExpression ( ) <EOL> assert annotations is None or annotations . isExpression ( ) <EOL> assert function_ref . isExpressionFunctionRef ( ) <EOL> ExpressionChildrenHavingBase . __init__ ( <EOL> self , <EOL> values = { <EOL> "<STR_LIT>" : function_ref , <EOL> "<STR_LIT>" : tuple ( defaults ) , <EOL> "<STR_LIT>" : kw_defaults , <EOL> "<STR_LIT>" : annotations <EOL> } , <EOL> source_ref = source_ref <EOL> ) <EOL> self . code_object = code_object <EOL> def getName ( self ) : <EOL> return self . getFunctionRef ( ) . getName ( ) <EOL> def getCodeObject ( self ) : <EOL> return self . code_object <EOL> def getDetails ( self ) : <EOL> return { <EOL> "<STR_LIT>" : self . code_object <EOL> } <EOL> def computeExpression ( self , constraint_collection ) : <EOL> defaults = self . getDefaults ( ) <EOL> side_effects = [ ] <EOL> for default in defaults : <EOL> if default . willRaiseException ( BaseException ) : <EOL> result = wrapExpressionWithSideEffects ( <EOL> side_effects = side_effects , <EOL> old_node = self , <EOL> new_node = default <EOL> ) <EOL> return result , "<STR_LIT>" , "<STR_LIT>" <EOL> kw_defaults = self . getKwDefaults ( ) <EOL> if kw_defaults is not None : <EOL> if kw_defaults . willRaiseException ( BaseException ) : <EOL> result = wrapExpressionWithSideEffects ( <EOL> side_effects = side_effects , <EOL> old_node = self , <EOL> new_node = kw_defaults <EOL> ) <EOL> return result , "<STR_LIT>" , "<STR_LIT>" <EOL> side_effects . append ( kw_defaults ) <EOL> annotations = self . getAnnotations ( ) <EOL> if annotations is not None and annotations . willRaiseException ( BaseException ) : <EOL> result = wrapExpressionWithSideEffects ( <EOL> side_effects = side_effects , <EOL> old_node = self , <EOL> new_node = annotations <EOL> ) <EOL> return result , "<STR_LIT>" , "<STR_LIT>" <EOL> return self , None , None <EOL> getFunctionRef = ExpressionChildrenHavingBase . childGetter ( "<STR_LIT>" ) <EOL> getDefaults = ExpressionChildrenHavingBase . childGetter ( "<STR_LIT>" ) <EOL> getKwDefaults = ExpressionChildrenHavingBase . childGetter ( "<STR_LIT>" ) <EOL> getAnnotations = ExpressionChildrenHavingBase . childGetter ( "<STR_LIT>" ) <EOL> def mayRaiseException ( self , exception_type ) : <EOL> for default in self . getDefaults ( ) : <EOL> if default . mayRaiseException ( exception_type ) : <EOL> return True <EOL> kw_defaults = self . getKwDefaults ( ) <EOL> if kw_defaults is not None and kw_defaults . mayRaiseException ( exception_type ) : <EOL> return True <EOL> annotations = self . getAnnotations ( ) <EOL> if annotations is not None and annotations . mayRaiseException ( exception_type ) : <EOL> return True <EOL> return False <EOL> def computeExpressionCall ( self , call_node , call_args , call_kw , <EOL> constraint_collection ) : <EOL> constraint_collection . onExceptionRaiseExit ( BaseException ) <EOL> if call_kw is not None and ( not call_kw . isExpressionConstantRef ( ) or call_kw . getConstant ( ) != { } ) : <EOL> return call_node , None , None <EOL> if call_kw is not None : <EOL> return call_node , None , None <EOL> if call_args is None : <EOL> args_tuple = ( ) <EOL> elif call_args . isExpressionConstantRef ( ) or call_args . isExpressionMakeTuple ( ) : <EOL> args_tuple = call_args . getIterationValues ( ) <EOL> else : <EOL> assert False , call_args <EOL> return call_node , None , None <EOL> function_body = self . getFunctionRef ( ) . getFunctionBody ( ) <EOL> call_spec = function_body . getParameters ( ) <EOL> try : <EOL> args_dict = matchCall ( <EOL> func_name = self . getName ( ) , <EOL> args = call_spec . getArgumentNames ( ) , <EOL> star_list_arg = call_spec . getStarListArgumentName ( ) , <EOL> star_dict_arg = call_spec . getStarDictArgumentName ( ) , <EOL> num_defaults = call_spec . getDefaultCount ( ) , <EOL> positional = args_tuple , <EOL> pairs = ( ) <EOL> ) <EOL> values = [ <EOL> args_dict [ name ] <EOL> for name in <EOL> call_spec . getParameterNames ( ) <EOL> ] <EOL> result = ExpressionFunctionCall ( <EOL> function = self , <EOL> values = values , <EOL> source_ref = call_node . getSourceReference ( ) <EOL> ) <EOL> return ( <EOL> result , <EOL> "<STR_LIT>" , <EOL> """<STR_LIT>""" % self . getName ( ) <EOL> ) <EOL> except TooManyArguments as e : <EOL> result = wrapExpressionWithSideEffects ( <EOL> new_node = makeRaiseExceptionReplacementExpressionFromInstance ( <EOL> expression = call_node , <EOL> exception = e . getRealException ( ) <EOL> ) , <EOL> old_node = call_node , <EOL> side_effects = call_node . extractPreCallSideEffects ( ) <EOL> ) <EOL> return ( <EOL> result , <EOL> "<STR_LIT>" , <EOL> """<STR_LIT>""" % self . getName ( ) <EOL> ) <EOL> def getCallCost ( self , values ) : <EOL> if not Options . isExperimental ( ) : <EOL> return None <EOL> function_body = self . getFunctionRef ( ) . getFunctionBody ( ) <EOL> if function_body . isExpressionGeneratorObjectBody ( ) : <EOL> assert False <EOL> return None <EOL> if function_body . isExpressionClassBody ( ) : <EOL> return None <EOL> if function_body . mayRaiseException ( BaseException ) : <EOL> return <NUM_LIT> <EOL> return <NUM_LIT:20> <EOL> def createOutlineFromCall ( self , provider , values ) : <EOL> return convertFunctionCallToOutline ( <EOL> provider = provider , <EOL> function_ref = self . getFunctionRef ( ) , <EOL> values = values <EOL> ) <EOL> class ExpressionFunctionRef ( NodeBase , ExpressionMixin ) : <EOL> kind = "<STR_LIT>" <EOL> def __init__ ( self , function_body , source_ref ) : <EOL> assert function_body . isExpressionFunctionBody ( ) or function_body . isExpressionClassBody ( ) or function_body . isExpressionGeneratorObjectBody ( ) or function_body . isExpressionCoroutineObjectBody ( ) <EOL> NodeBase . __init__ ( <EOL> self , <EOL> source_ref = source_ref <EOL> ) <EOL> self . function_body = function_body <EOL> def getName ( self ) : <EOL> return self . function_body . getName ( ) <EOL> def getDetails ( self ) : <EOL> return { <EOL> "<STR_LIT>" : self . function_body <EOL> } <EOL> def getDetailsForDisplay ( self ) : <EOL> return { <EOL> "<STR_LIT>" : self . function_body . getCodeName ( ) <EOL> } <EOL> def getFunctionBody ( self ) : <EOL> return self . function_body <EOL> def computeExpressionRaw ( self , constraint_collection ) : <EOL> function_body = self . getFunctionBody ( ) <EOL> owning_module = function_body . getParentModule ( ) <EOL> from nuitka . ModuleRegistry import addUsedModule <EOL> addUsedModule ( owning_module ) <EOL> owning_module . addUsedFunction ( function_body ) <EOL> from nuitka . optimizations . TraceCollections import ConstraintCollectionFunction <EOL> old_collection = function_body . constraint_collection <EOL> function_body . constraint_collection = ConstraintCollectionFunction ( <EOL> parent = constraint_collection , <EOL> function_body = function_body <EOL> ) <EOL> statements_sequence = function_body . getBody ( ) <EOL> if statements_sequence is not None and not statements_sequence . getStatements ( ) : <EOL> function_body . setStatements ( None ) <EOL> statements_sequence = None <EOL> if statements_sequence is not None : <EOL> result = statements_sequence . computeStatementsSequence ( <EOL> constraint_collection = function_body . constraint_collection <EOL> ) <EOL> if result is not statements_sequence : <EOL> function_body . setBody ( result ) <EOL> function_body . constraint_collection . updateFromCollection ( old_collection ) <EOL> return self , None , None <EOL> def mayHaveSideEffects ( self ) : <EOL> return False <EOL> class ExpressionFunctionCall ( ExpressionChildrenHavingBase ) : <EOL> """<STR_LIT>""" <EOL> kind = "<STR_LIT>" <EOL> named_children = ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) <EOL> def __init__ ( self , function , values , source_ref ) : <EOL> assert function . isExpressionFunctionCreation ( ) <EOL> ExpressionChildrenHavingBase . __init__ ( <EOL> self , <EOL> values = { <EOL> "<STR_LIT>" : function , <EOL> "<STR_LIT>" : tuple ( values ) , <EOL> } , <EOL> source_ref = source_ref <EOL> ) <EOL> def computeExpression ( self , constraint_collection ) : <EOL> function = self . getFunction ( ) <EOL> values = self . getArgumentValues ( ) <EOL> cost = function . getCallCost ( values ) <EOL> if function . getFunctionRef ( ) . getFunctionBody ( ) . mayRaiseException ( BaseException ) : <EOL> constraint_collection . onExceptionRaiseExit ( BaseException ) <EOL> if cost is not None and cost < <NUM_LIT:50> : <EOL> result = function . createOutlineFromCall ( <EOL> provider = self . getParentVariableProvider ( ) , <EOL> values = values <EOL> ) <EOL> return result , "<STR_LIT>" , "<STR_LIT>" <EOL> return self , None , None <EOL> def mayRaiseException ( self , exception_type ) : <EOL> function = self . getFunction ( ) <EOL> if function . getFunctionRef ( ) . getFunctionBody ( ) . mayRaiseException ( exception_type ) : <EOL> return True <EOL> values = self . getArgumentValues ( ) <EOL> for value in values : <EOL> if value . mayRaiseException ( exception_type ) : <EOL> return True <EOL> return False <EOL> getFunction = ExpressionChildrenHavingBase . childGetter ( "<STR_LIT>" ) <EOL> getArgumentValues = ExpressionChildrenHavingBase . childGetter ( "<STR_LIT>" ) <EOL> class ExpressionFunctionQualnameRef ( CompileTimeConstantExpressionMixin , <EOL> NodeBase ) : <EOL> kind = "<STR_LIT>" <EOL> def __init__ ( self , function_body , source_ref ) : <EOL> NodeBase . __init__ ( self , source_ref = source_ref ) <EOL> CompileTimeConstantExpressionMixin . __init__ ( self ) <EOL> self . function_body = function_body <EOL> def computeExpression ( self , constraint_collection ) : <EOL> result = makeConstantReplacementNode ( <EOL> node = self , <EOL> constant = self . function_body . getFunctionQualname ( ) <EOL> ) <EOL> return ( <EOL> result , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % self . function_body . getFunctionQualname ( ) <EOL> ) </s>
