def computeExpressionAttribute(self, lookup_node, attribute_name, trace_collection): # By default, an attribute lookup may change everything about the lookup # source. if self.module is not None: trust = hard_modules_trust[self.module_name].get( attribute_name, trust_undefined) if trust is trust_importable: # TODO: Change this is a hard module import itself, currently these are not all trusted # themselves yet. We do not have to indicate exception, but it makes no sense to annotate # that here at this point. trace_collection.onExceptionRaiseExit(BaseException) elif not hasattr( self.module, attribute_name) and hard_modules_trust[ self.module_name].get(attribute_name, attribute_name): new_node = makeRaiseExceptionReplacementExpression( expression=lookup_node, exception_type="ImportError", exception_value=self._getImportNameErrorString( self.module, self.module_name, attribute_name), ) trace_collection.onExceptionRaiseExit(ImportError) return ( new_node, "new_raise", "Hard module %r attribute missing %r pre-computed." % (self.module_name, attribute_name), ) else: if trust is trust_undefined: onMissingTrust( "Hard module %r attribute %r missing trust config for existing value.", lookup_node.getSourceReference(), self.module_name, attribute_name, ) trace_collection.onExceptionRaiseExit(ImportError) elif trust is trust_constant: # Make sure it's actually there, and not becoming the getattr default by accident. assert hasattr(self.module, self.import_name), self return ( makeConstantRefNode( constant=getattr(self.module, self.import_name), source_ref=lookup_node.getSourceReference(), user_provided=True, ), "new_constant", "Hard module %r imported %r pre-computed to constant value." % (self.module_name, self.import_name), ) else: result = ExpressionImportModuleNameHard( module_name=self.module_name, import_name=attribute_name, source_ref=lookup_node.getSourceReference(), ) return ( result, "new_expression", "Attribute lookup %r of hard module %r becomes hard module name import." % (self.module_name, attribute_name), ) else: # Nothing can be known, but lets not do control flow escape, that is just # too unlikely. trace_collection.onExceptionRaiseExit(BaseException) return lookup_node, None, None
def computeExpressionAttribute(self, lookup_node, attribute_name, trace_collection): # By default, an attribute lookup may change everything about the lookup # source. if self.module is not None: if not hasattr(self.module, attribute_name): # We want exceptions from actual lookup, pylint: disable=eval-used return trace_collection.getCompileTimeComputationResult( node=lookup_node, computation=lambda: eval("__import__('%s').%s" % ( self.module_name, attribute_name)), description= "Hard module %r attribute missing %r pre-computed." % (self.module_name, attribute_name), ) else: trust = hard_modules_trust[self.module_name].get( attribute_name) if trust is None: onMissingTrust( "Hard module %r attribute %r missing trust config", lookup_node.getSourceReference(), self.module_name, attribute_name, ) trace_collection.onExceptionRaiseExit(AttributeError) elif trust is trust_constant: # Make sure it's actually there, and not becoming the getattr default by accident. assert hasattr(self.module, self.import_name), self return ( makeConstantRefNode( constant=getattr(self.module, self.import_name), source_ref=lookup_node.getSourceReference(), user_provided=True, ), "new_constant", "Hard module %r imported %r pre-computed to constant value." % (self.module_name, self.import_name), ) else: result = ExpressionImportModuleNameHard( module_name=self.module_name, import_name=attribute_name, source_ref=lookup_node.getSourceReference(), ) return ( result, "new_expression", "Attribute lookup %r of hard module %r becomes hard module name import." % (self.module_name, attribute_name), ) else: # Nothing can be known, but lets not do control flow escape, that is just # too unlikely. trace_collection.onExceptionRaiseExit(BaseException) return lookup_node, None, None